From Algorithms to Z-Scores:

Probabilistic and Statistical Modeling in
Computer Science

Norm Matloﬀ, University of California, Davis

fX(t) = ce−0.5(t−µ)(cid:48)Σ−1(t−µ)

library(MASS)
x <- mvrnorm(mu,sgm)

See Creative Commons license at

http://heather.cs.ucdavis.edu/ matloﬀ/probstatbook.html
The author has striven to minimize the number of errors, but no guarantee is made as to accuracy
of the contents of this book.

x1−10−50510x2−10−50510z0.0050.0100.0152

Author’s Biographical Sketch

Dr. Norm Matloﬀ is a professor of computer science at the University of California at Davis, and
was formerly a professor of statistics at that university. He is a former database software developer
in Silicon Valley, and has been a statistical consultant for ﬁrms such as the Kaiser Permanente
Health Plan.

Dr. Matloﬀ was born in Los Angeles, and grew up in East Los Angeles and the San Gabriel Valley.
He has a PhD in pure mathematics from UCLA, specializing in probability theory and statistics. He
has published numerous papers in computer science and statistics, with current research interests
in parallel processing, statistical computing, and regression methodology.

Prof. Matloﬀ is a former appointed member of IFIP Working Group 11.3, an international com-
mittee concerned with database software security, established under UNESCO. He was a founding
member of the UC Davis Department of Statistics, and participated in the formation of the UCD
Computer Science Department as well. He is a recipient of the campuswide Distinguished Teaching
Award and Distinguished Public Service Award at UC Davis.

Dr. Matloﬀ is the author of two published textbooks, and of a number of widely-used Web tutorials
on computer topics, such as the Linux operating system and the Python programming language.
He and Dr. Peter Salzman are authors of The Art of Debugging with GDB, DDD, and Eclipse.
Prof. Matloﬀ’s book on the R programming language, The Art of R Programming, was published
in 2011. His book, Parallel Computation for Data Science, came out in 2015. His current book
project, From Linear Models to Machine Learning: Predictive Insights through R, will be published
in 2016. He is also the author of several open-source textbooks, including From Algorithms to Z-
Scores: Probabilistic and Statistical Modeling in Computer Science (http://heather.cs.ucdavis.
edu/probstatbook), and Programming on Parallel Machines (http://heather.cs.ucdavis.edu/
~matloff/ParProcBook.pdf).

Contents

1 Time Waste Versus Empowerment

2 Basic Probability Models

2.1 ALOHA Network Example

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

2.2 The Crucial Notion of a Repeatable Experiment

. . . . . . . . . . . . . . . . . . . .

2.3 Our Deﬁnitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

1

3

3

5

7

2.4

“Mailing Tubes” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.5 Example: ALOHA Network . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.6 Bayes’ Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2.7 ALOHA in the Notebook Context

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 14

2.8 A Note on Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15

2.9 Solution Strategies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16

2.10 Example: Divisibility of Random Integers . . . . . . . . . . . . . . . . . . . . . . . . 18

2.11 Example: A Simple Board Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19

2.12 Example: Bus Ridership . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20

2.13 Random Graph Models

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

2.13.1 Example: Preferential Attachment Graph Model

. . . . . . . . . . . . . . . . 23

2.14 Simulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

2.14.1 Example: Rolling Dice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24

i

ii

CONTENTS

2.14.2 First Improvement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25

2.14.3 Second Improvement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26

2.14.3.1 Simulation of Conditional Probability in Dice Problem . . . . . . . 27

2.14.4 Simulation of the ALOHA Example . . . . . . . . . . . . . . . . . . . . . . . 28

2.14.5 Example: Bus Ridership, cont’d.

. . . . . . . . . . . . . . . . . . . . . . . . . 29

2.14.6 Back to the Board Game Example . . . . . . . . . . . . . . . . . . . . . . . . 30

2.14.7 How Long Should We Run the Simulation? . . . . . . . . . . . . . . . . . . . 30

2.15 Combinatorics-Based Probability Computation . . . . . . . . . . . . . . . . . . . . . 30

2.15.1 Which Is More Likely in Five Cards, One King or Two Hearts? . . . . . . . . 31

2.15.2 Example: Random Groups of Students . . . . . . . . . . . . . . . . . . . . . . 32

2.15.3 Example: Lottery Tickets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

2.15.4 “Association Rules” in Data Mining . . . . . . . . . . . . . . . . . . . . . . . 33

2.15.5 Multinomial Coeﬃcients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

2.15.6 Example: Probability of Getting Four Aces in a Bridge Hand . . . . . . . . . 34

3 Discrete Random Variables

39

3.1 Random Variables

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

3.2 Discrete Random Variables

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

3.3

Independent Random Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

3.4 Example: The Monty Hall Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

3.5 Expected Value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

3.5.1 Generality—Not Just for DiscreteRandom Variables . . . . . . . . . . . . . . 42

3.5.1.1 What Is It? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

3.5.2 Deﬁnition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

3.5.3 Existence of the Expected Value . . . . . . . . . . . . . . . . . . . . . . . . . 43

3.5.4 Computation and Properties of Expected Value . . . . . . . . . . . . . . . . . 43

3.5.5

“Mailing Tubes” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

CONTENTS

iii

3.5.6 Casinos, Insurance Companies and “Sum Users,” Compared to Others . . . . 50

3.6 Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

3.6.1 Deﬁnition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

3.6.2 More Practice with the Properties of Variance

. . . . . . . . . . . . . . . . . 54

3.6.3 Central Importance of the Concept of Variance . . . . . . . . . . . . . . . . . 55

3.6.4

Intuition Regarding the Size of Var(X) . . . . . . . . . . . . . . . . . . . . . . 55

3.6.4.1 Chebychev’s Inequality . . . . . . . . . . . . . . . . . . . . . . . . . 55

3.6.4.2 The Coeﬃcient of Variation . . . . . . . . . . . . . . . . . . . . . . . 55

3.7 A Useful Fact . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56

3.8 Covariance

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57

3.9

Indicator Random Variables, and Their Means and Variances . . . . . . . . . . . . . 59

3.9.1 Example: Return Time for Library Books . . . . . . . . . . . . . . . . . . . . 60

3.9.2 Example: Indicator Variables in a Committee Problem . . . . . . . . . . . . . 62

3.9.3 Example: Spinner Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63

3.10 Expected Value, Etc. in the ALOHA Example

. . . . . . . . . . . . . . . . . . . . . 64

3.11 Example: Measurements at Diﬀerent Ages . . . . . . . . . . . . . . . . . . . . . . . . 65

3.12 Example: Bus Ridership Model

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65

3.13 Distributions

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66

3.13.1 Example: Toss Coin Until First Head . . . . . . . . . . . . . . . . . . . . . . 67

3.13.2 Example: Sum of Two Dice . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67

3.13.3 Example: Watts-Strogatz Random Graph Model

. . . . . . . . . . . . . . . . 67

3.13.3.1 The Model

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

3.13.3.2 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68

3.14 Proof of Chebychev’s Inequality (optional section)

. . . . . . . . . . . . . . . . . . . 69

4 Discrete Parametric Distribution Families

71

4.1 The Case of Importance to Us: Parameteric Families of pmfs

. . . . . . . . . . . . . 72

iv

CONTENTS

4.2 The Geometric Family of Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . 72

4.2.1 R Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75

4.2.2 Example: a Parking Space Problem . . . . . . . . . . . . . . . . . . . . . . . 76

4.3 The Binomial Family of Distributions

. . . . . . . . . . . . . . . . . . . . . . . . . . 77

4.3.1 R Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79

4.3.2 Example: Parking Space Model . . . . . . . . . . . . . . . . . . . . . . . . . . 79

4.4 The Negative Binomial Family of Distributions . . . . . . . . . . . . . . . . . . . . . 80

4.4.1 R Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

4.4.2 Example: Backup Batteries . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81

4.5 The Poisson Family of Distributions

. . . . . . . . . . . . . . . . . . . . . . . . . . . 81

4.5.1 R Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82

4.6 The Power Law Family of Distributions

. . . . . . . . . . . . . . . . . . . . . . . . . 83

4.6.1 The Model

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83

4.6.2 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84

4.7 Recognizing Some Parametric Distributions When You See Them . . . . . . . . . . . 84

4.8 Example: a Coin Game

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85

4.9 Example: Tossing a Set of Four Coins

. . . . . . . . . . . . . . . . . . . . . . . . . . 86

4.10 Example: the ALOHA Example Again . . . . . . . . . . . . . . . . . . . . . . . . . . 87

4.11 Example: the Bus Ridership Problem Again . . . . . . . . . . . . . . . . . . . . . . . 88

4.12 Example: Flipping Coins with Bonuses . . . . . . . . . . . . . . . . . . . . . . . . . . 89

4.13 Example: Analysis of Social Networks . . . . . . . . . . . . . . . . . . . . . . . . . . 90

4.14 Multivariate Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90

4.15 Iterated Expectations

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91

4.15.1 Conditional Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

4.15.2 The Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92

4.15.3 Example: Coin and Die Game

. . . . . . . . . . . . . . . . . . . . . . . . . . 93

4.15.4 Example: Flipping Coins with Bonuses

. . . . . . . . . . . . . . . . . . . . . 93

CONTENTS

5 Pause to Reﬂect

v

99

5.1 A Cautionary Tale . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99

5.1.1 Trick Coins, Tricky Example

. . . . . . . . . . . . . . . . . . . . . . . . . . . 99

5.1.2

Intuition in Retrospect . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100

5.1.3

Implications for Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

5.2 What About “Iterated Variance”? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101

5.3 Why Not Just Do All Analysis by Simulation?

. . . . . . . . . . . . . . . . . . . . . 101

5.4 Reconciliation of Math and Intuition (optional section) . . . . . . . . . . . . . . . . . 102

6 Introduction to Discrete Markov Chains

109

6.1 Matrix Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110

6.2 Example: Die Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

6.3 Long-Run State Probabilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111

6.3.1

Stationary Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112

6.3.2 Calculation of π . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113

6.3.2.1 Example: π in Die Game . . . . . . . . . . . . . . . . . . . . . . . . 114

6.3.2.2 Another Way to Find π . . . . . . . . . . . . . . . . . . . . . . . . . 114

6.4 Example: 3-Heads-in-a-Row Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115

6.4.1 Markov Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

6.5 A Modiﬁed Notebook Analysis

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116

6.5.1 A Markov-Chain Notebook . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

6.5.2 Example: 3-Heads-in-a-Row Game . . . . . . . . . . . . . . . . . . . . . . . . 117

6.6 Simulation of Markov Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117

6.7 Example: ALOHA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118

6.8 Example: Bus Ridership Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120

6.9 Example: an Inventory Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

6.10 Expected Hitting Times . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122

vi

7 Continuous Probability Models

CONTENTS

125

7.1 Running Example: a Random Dart . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125

7.2

Individual Values Now Have Probability Zero . . . . . . . . . . . . . . . . . . . . . . 126

7.3 But Now We Have a Problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126

7.3.1 Our Way Out of the Problem: Cumulative Distribution Functions

. . . . . . 127

7.3.2 Density Functions

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129

7.3.3 Properties of Densities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

7.3.4

Intuitive Meaning of Densities . . . . . . . . . . . . . . . . . . . . . . . . . . . 131

7.3.5 Expected Values . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132

7.4 A First Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134

7.5 The Notion of Support in the Continuous Case

. . . . . . . . . . . . . . . . . . . . . 135

7.6 Famous Parametric Families of Continuous Distributions . . . . . . . . . . . . . . . . 135

7.6.1 The Uniform Distributions

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 135

7.6.1.1 Density and Properties

. . . . . . . . . . . . . . . . . . . . . . . . . 135

7.6.1.2 R Functions

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136

7.6.1.3 Example: Modeling of Disk Performance . . . . . . . . . . . . . . . 136

7.6.1.4 Example: Modeling of Denial-of-Service Attack . . . . . . . . . . . . 137

7.6.2 The Normal (Gaussian) Family of Continuous Distributions . . . . . . . . . . 137

7.6.2.1 Density and Properties

. . . . . . . . . . . . . . . . . . . . . . . . . 137

7.6.3 The Exponential Family of Distributions . . . . . . . . . . . . . . . . . . . . . 138

7.6.3.1 Density and Properties

. . . . . . . . . . . . . . . . . . . . . . . . . 138

7.6.3.2 R Functions

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138

7.6.3.3 Example: Refunds on Failed Components . . . . . . . . . . . . . . . 139

7.6.3.4 Example: Garage Parking Fees . . . . . . . . . . . . . . . . . . . . . 139

7.6.3.5

Importance in Modeling . . . . . . . . . . . . . . . . . . . . . . . . . 140

7.6.4 The Gamma Family of Distributions . . . . . . . . . . . . . . . . . . . . . . . 140

7.6.4.1 Density and Properties

. . . . . . . . . . . . . . . . . . . . . . . . . 140

CONTENTS

vii

7.6.4.2 Example: Network Buﬀer . . . . . . . . . . . . . . . . . . . . . . . . 141

7.6.4.3

Importance in Modeling . . . . . . . . . . . . . . . . . . . . . . . . . 142

7.6.5 The Beta Family of Distributions . . . . . . . . . . . . . . . . . . . . . . . . . 142

7.6.5.1 Density Etc.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144

7.6.5.2

Importance in Modeling . . . . . . . . . . . . . . . . . . . . . . . . . 146

7.7 Choosing a Model

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 146

7.8 Finding the Density of a Function of a Random Variable . . . . . . . . . . . . . . . . 147

7.9 Quantile Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148

7.10 Using cdf Functions to Find Probabilities

. . . . . . . . . . . . . . . . . . . . . . . . 149

7.11 A General Method for Simulating a Random Variable

. . . . . . . . . . . . . . . . . 149

7.12 Example: Writing a Set of R Functions for a Certain Power Family . . . . . . . . . . 150

7.13 Multivariate Densities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 151

7.14 Iterated Expectations

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

7.14.1 The Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152

7.14.2 Example: Another Coin Game . . . . . . . . . . . . . . . . . . . . . . . . . . 152

7.15 Continuous Random Variables Are “Useful Unicorns” . . . . . . . . . . . . . . . . . 153

8 The Normal Family of Distributions

155

8.1 Density and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 155

8.1.1 Closure Under Aﬃne Transformation . . . . . . . . . . . . . . . . . . . . . . . 156

8.1.2 Closure Under Independent Summation . . . . . . . . . . . . . . . . . . . . . 157

8.2 The Standard Normal Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

8.3 Evaluating Normal cdfs

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 158

8.4 Example: Network Intrusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 159

8.5 Example: Class Enrollment Size

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

8.6 More on the Jill Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161

8.7 Example: River Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162

viii

CONTENTS

8.8 Example: Upper Tail of a Light Bulb Distribution . . . . . . . . . . . . . . . . . . . 162

8.9 The Central Limit Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 163

8.10 Example: Cumulative Roundoﬀ Error

. . . . . . . . . . . . . . . . . . . . . . . . . . 163

8.11 Example: R Evaluation of a Central Limit Theorem Approximation . . . . . . . . . 164

8.12 Example: Bug Counts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

8.13 Example: Coin Tosses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164

8.14 Example: Normal Approximation to Gamma Family . . . . . . . . . . . . . . . . . . 166

8.15 Example: Museum Demonstration . . . . . . . . . . . . . . . . . . . . . . . . . . . . 166

8.16 Importance in Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

8.17 The Chi-Squared Family of Distributions

. . . . . . . . . . . . . . . . . . . . . . . . 167

8.17.1 Density and Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 167

8.17.2 Example: Error in Pin Placement

. . . . . . . . . . . . . . . . . . . . . . . . 168

8.17.3 Example: Generating Normal Random Numbers . . . . . . . . . . . . . . . . 169

8.17.4 Importance in Modeling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

8.17.5 Relation to Gamma Family . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

8.18 The Multivariate Normal Family . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 170

8.19 Optional Topic: Precise Statement of the CLT . . . . . . . . . . . . . . . . . . . . . 171

8.19.1 Convergence in Distribution, and the Precisely-Stated CLT . . . . . . . . . . 171

9 The Exponential Distributions

175

9.1 Connection to the Poisson Distribution Family . . . . . . . . . . . . . . . . . . . . . 175

9.2 Memoryless Property of Exponential Distributions . . . . . . . . . . . . . . . . . . . 177

9.2.1 Derivation and Intuition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177

9.2.2 Uniquely Memoryless

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178

9.2.3 Example: “Nonmemoryless” Light Bulbs . . . . . . . . . . . . . . . . . . . . . 179

9.3 Example: Minima of Independent Exponentially Distributed Random Variables . . . 179

9.3.1 Example: Computer Worm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 182

CONTENTS

ix

9.3.2 Example: Electronic Components . . . . . . . . . . . . . . . . . . . . . . . . . 183

9.4 A Cautionary Tale: the Bus Paradox . . . . . . . . . . . . . . . . . . . . . . . . . . . 183

9.4.1 Length-Biased Sampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184

9.4.2 Probability Mass Functions and Densities in Length-Biased Sampling . . . . 185

10 Stop and Review: Probability Structures

11 Introduction to Continuous-Time Markov Chains

187

193

11.1 Continuous-Time Markov Chains . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

11.2 Holding-Time Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 193

11.2.1 The Notion of “Rates” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

11.3 Stationary Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194

11.3.1 Intuitive Derivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

11.3.2 Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 195

11.4 Example: Machine Repair . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196

11.5 Example: Migration in a Social Network . . . . . . . . . . . . . . . . . . . . . . . . . 198

11.6 Birth/Death Processes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199

11.7 Cell Communications Model . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200

11.7.1 Stationary Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201

11.7.2 Going Beyond Finding the π . . . . . . . . . . . . . . . . . . . . . . . . . . . 202

12 Covariance and Random Vectors

203

12.1 Measuring Co-variation of Random Variables . . . . . . . . . . . . . . . . . . . . . . 203

12.1.1 Covariance

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203

12.1.2 Example: Variance of Sum of Nonindependent Variables . . . . . . . . . . . . 205

12.1.3 Example: the Committee Example Again . . . . . . . . . . . . . . . . . . . . 205

12.2 Correlation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206

12.2.1 Example: a Catchup Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . 207

x

CONTENTS

12.3 Sets of Independent Random Variables . . . . . . . . . . . . . . . . . . . . . . . . . . 207

12.3.1 Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208

12.3.1.1 Expected Values Factor . . . . . . . . . . . . . . . . . . . . . . . . . 208

12.3.1.2 Covariance Is 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208

12.3.1.3 Variances Add . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

12.3.2 Examples Involving Sets of Independent Random Variables . . . . . . . . . . 209

12.3.2.1 Example: Dice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209

12.3.2.2 Example: Variance of a Product . . . . . . . . . . . . . . . . . . . . 210

12.3.2.3 Example: Ratio of Independent Geometric Random Variables

. . . 210

12.4 Matrix Formulations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211

12.4.1 Properties of Mean Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

12.4.2 Covariance Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212

12.4.3 Covariance Matrices Linear Combinations of Random Vectors . . . . . . . . . 213

12.4.4 Example: (X,S) Dice Example Again . . . . . . . . . . . . . . . . . . . . . . . 214

12.4.5 Example: Easy Sum Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . 214

12.5 The Multivariate Normal Family of Distributions . . . . . . . . . . . . . . . . . . . . 215

12.5.1 R Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215

12.5.2 Special Case: New Variable Is a Single Linear Combination of a Random Vector216

12.6 Indicator Random Vectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216

12.7 Example: Dice Game . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217

12.7.1 Correlation Matrices . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

12.7.2 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220

13 Multivariate PMFs and Densities

223

13.1 Multivariate Probability Mass Functions . . . . . . . . . . . . . . . . . . . . . . . . . 223

13.2 Multivariate Densities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

13.2.1 Motivation and Deﬁnition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 226

CONTENTS

xi

13.2.2 Use of Multivariate Densities in Finding Probabilities and Expected Values . 226

13.2.3 Example: a Triangular Distribution . . . . . . . . . . . . . . . . . . . . . . . 227

13.2.4 Example: Train Rendezvouz . . . . . . . . . . . . . . . . . . . . . . . . . . . . 230

13.3 More on Sets of Independent Random Variables . . . . . . . . . . . . . . . . . . . . . 231

13.3.1 Probability Mass Functions and Densities Factor in the Independent Case . . 231

13.3.2 Convolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 232

13.3.3 Example: Ethernet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 233

13.3.4 Example: Analysis of Seek Time . . . . . . . . . . . . . . . . . . . . . . . . . 233

13.3.5 Example: Backup Battery . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

13.3.6 Example: Minima of Uniformly Distributed Random Variables . . . . . . . . 235

13.3.7 Example: Ethernet Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 235

13.4 Example: Finding the Distribution of the Sum of Nonindependent Random Variables 236

13.5 Parametric Families of Multivariate Distributions . . . . . . . . . . . . . . . . . . . . 236

13.5.1 The Multinomial Family of Distributions

. . . . . . . . . . . . . . . . . . . . 237

13.5.1.1 Probability Mass Function . . . . . . . . . . . . . . . . . . . . . . . 237

13.5.1.2 Example: Component Lifetimes

. . . . . . . . . . . . . . . . . . . . 238

13.5.1.3 Mean Vectors and Covariance Matrices in the Multinomial Family . 239

13.5.1.4 Application: Text Mining . . . . . . . . . . . . . . . . . . . . . . . . 242

13.5.2 The Multivariate Normal Family of Distributions

. . . . . . . . . . . . . . . 242

13.5.2.1 Densities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 242

13.5.2.2 Geometric Interpretation . . . . . . . . . . . . . . . . . . . . . . . . 243

13.5.2.3 Properties of Multivariate Normal Distributions

. . . . . . . . . . . 246

13.5.2.4 The Multivariate Central Limit Theorem . . . . . . . . . . . . . . . 247

13.5.2.5 Example: Finishing the Loose Ends from the Dice Game . . . . . . 248

13.5.2.6 Application: Data Mining . . . . . . . . . . . . . . . . . . . . . . . . 248

14 Transform Methods

255

xii

CONTENTS

14.1 Generating Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 255

14.2 Moment Generating Functions

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 256

14.3 Transforms of Sums of Independent Random Variables . . . . . . . . . . . . . . . . . 257

14.4 Example: Network Packets

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 258

14.4.1 Poisson Generating Function . . . . . . . . . . . . . . . . . . . . . . . . . . . 258

14.4.2 Sums of Independent Poisson Random Variables Are Poisson Distributed . . 258

14.5 Other Uses of Transforms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259

15 Statistics: Prologue

261

15.1 Sampling Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

15.1.1 Random Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 262

15.2 The Sample Mean—a Random Variable . . . . . . . . . . . . . . . . . . . . . . . . . 263

15.2.1 Toy Population Example

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263

15.2.2 Expected and Variance of X . . . . . . . . . . . . . . . . . . . . . . . . . . . 264

15.2.3 Toy Population Example Again . . . . . . . . . . . . . . . . . . . . . . . . . . 265

15.2.4 Interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

15.3 Sample Means Are Approximately Normal—No Matter What the Population Dis-

tribution Is

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 266

15.3.1 The Sample Variance—Another Random Variable

. . . . . . . . . . . . . . . 267

15.3.1.1 Intuitive Estimation of σ2 . . . . . . . . . . . . . . . . . . . . . . . . 267

15.3.1.2 Easier Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . 268

15.3.1.3 To Divide by n or n-1? . . . . . . . . . . . . . . . . . . . . . . . . . 268

15.4 Observational Studies

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 269

15.5 A Good Time to Stop and Review! . . . . . . . . . . . . . . . . . . . . . . . . . . . . 270

16 Introduction to Conﬁdence Intervals

271

16.1 The “Margin of Error” and Conﬁdence Intervals

. . . . . . . . . . . . . . . . . . . . 271

16.2 Conﬁdence Intervals for Means

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 272

CONTENTS

xiii

16.2.1 Basic Formulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

16.2.2 Example: Simulation Output . . . . . . . . . . . . . . . . . . . . . . . . . . . 273

16.3 Meaning of Conﬁdence Intervals

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

16.3.1 A Weight Survey in Davis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 274

16.3.2 More About Interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275

16.4 Conﬁdence Intervals for Proportions . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

16.4.1 Derivation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 277

16.4.2 That n vs. n-1 Thing Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

16.4.3 Simulation Example Again . . . . . . . . . . . . . . . . . . . . . . . . . . . . 278

16.4.4 Example: Davis Weights . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 279

16.4.5 Interpretation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

16.4.6 (Non-)Eﬀect of the Population Size . . . . . . . . . . . . . . . . . . . . . . . . 280

16.4.7 Inferring the Number Polled . . . . . . . . . . . . . . . . . . . . . . . . . . . . 280

16.4.8 Planning Ahead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 281

16.5 General Formation of Conﬁdence Intervals from Approximately Normal Estimators . 281

16.5.1 The Notion of a Standard Error

. . . . . . . . . . . . . . . . . . . . . . . . . 281

16.5.2 Forming General Conﬁdence Intervals

. . . . . . . . . . . . . . . . . . . . . . 282

16.5.3 Standard Errors of Combined Estimators

. . . . . . . . . . . . . . . . . . . . 283

16.6 Conﬁdence Intervals for Diﬀerences of Means or Proportions . . . . . . . . . . . . . . 284

16.6.1 Independent Samples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 284

16.6.2 Example: Network Security Application . . . . . . . . . . . . . . . . . . . . . 285

16.6.3 Dependent Samples

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 286

16.6.4 Example: Machine Classiﬁcation of Forest Covers . . . . . . . . . . . . . . . . 287

16.7 And What About the Student-t Distribution? . . . . . . . . . . . . . . . . . . . . . . 288

16.8 R Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

16.9 Example: Pro Baseball Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

16.9.1 R Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 290

xiv

CONTENTS

16.9.2 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 291

16.10Example: UCI Bank Marketing Dataset . . . . . . . . . . . . . . . . . . . . . . . . . 293

16.11Example: Amazon Links . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 294

16.12Example: Master’s Degrees in CS/EE . . . . . . . . . . . . . . . . . . . . . . . . . . 295

16.13Other Conﬁdence Levels . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 296

16.14One More Time: Why Do We Use Conﬁdence Intervals? . . . . . . . . . . . . . . . . 296

17 Introduction to Signiﬁcance Tests

299

17.1 The Basics

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 300

17.2 General Testing Based on Normally Distributed Estimators . . . . . . . . . . . . . . 301

17.3 Example: Network Security . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

17.4 The Notion of “p-Values” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 302

17.5 Example: Bank Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 303

17.6 One-Sided HA . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304

17.7 Exact Tests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 304

17.7.1 Example: Test for Biased Coin . . . . . . . . . . . . . . . . . . . . . . . . . . 304

17.7.2 Example: Improved Light Bulbs

. . . . . . . . . . . . . . . . . . . . . . . . . 305

17.7.3 Example: Test Based on Range Data . . . . . . . . . . . . . . . . . . . . . . . 306

17.7.4 Exact Tests under a Normal Distribution Assumption . . . . . . . . . . . . . 307

17.8 Don’t Speak of “the Probability That H0 Is True” . . . . . . . . . . . . . . . . . . . 307

17.9 R Computation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308

17.10The Power of a Test

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308

17.10.1 Example: Coin Fairness . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 308

17.10.2 Example: Improved Light Bulbs

. . . . . . . . . . . . . . . . . . . . . . . . . 309

17.11What’s Wrong with Signiﬁcance Testing—and What to Do Instead . . . . . . . . . . 309

17.11.1 History of Signiﬁcance Testing, and Where We Are Today . . . . . . . . . . . 310

17.11.2 The Basic Fallacy . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 310

CONTENTS

xv

17.11.3 You Be the Judge!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312

17.11.4 What to Do Instead . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 312

17.11.5 Decide on the Basis of “the Preponderance of Evidence” . . . . . . . . . . . . 313

17.11.6 Example: the Forest Cover Data . . . . . . . . . . . . . . . . . . . . . . . . . 314

17.11.7 Example: Assessing Your Candidate’s Chances for Election . . . . . . . . . . 314

18 General Statistical Estimation and Inference

315

18.1 General Methods of Parametric Estimation . . . . . . . . . . . . . . . . . . . . . . . 315

18.1.1 Example: Guessing the Number of Raﬄe Tickets Sold . . . . . . . . . . . . . 315

18.1.2 Method of Moments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 316

18.1.2.1 Example: Lottery Model

. . . . . . . . . . . . . . . . . . . . . . . . 316

18.1.2.2 General Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 317

18.1.3 Method of Maximum Likelihood . . . . . . . . . . . . . . . . . . . . . . . . . 317

18.1.3.1 Example: Raﬄe Model

. . . . . . . . . . . . . . . . . . . . . . . . . 317

18.1.3.2 General Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . 318

18.1.4 Example: Estimation of the Parameters of a Gamma Distribution . . . . . . 319

18.1.4.1 Method of Moments . . . . . . . . . . . . . . . . . . . . . . . . . . . 319

18.1.4.2 MLEs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320

18.1.5 R’s mle() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 320

18.1.6 R’s gmm() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 322

18.1.6.1 Example: Bodyfat Data . . . . . . . . . . . . . . . . . . . . . . . . . 323

18.1.7 More Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 325

18.1.8 Asymptotic Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

18.1.8.1 Consistency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 327

18.1.8.2 Approximate Conﬁdence Intervals . . . . . . . . . . . . . . . . . . . 328

18.2 Bias and Variance

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

18.2.1 Bias . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 329

xvi

CONTENTS

18.2.2 Why Divide by n-1 in s2? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 330

18.2.2.1 But in This Book, We Divide by n, not n-1 Anyway . . . . . . . . . 332

18.2.3 Example of Bias Calculation: Max from U(0,c) . . . . . . . . . . . . . . . . . 333

18.2.4 Example of Bias Calculation: Gamma Family . . . . . . . . . . . . . . . . . . 334

18.2.5 Tradeoﬀ Between Variance and Bias . . . . . . . . . . . . . . . . . . . . . . . 334

18.3 Simultaneous Inference Methods

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 335

18.3.1 The Bonferonni Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 336

18.3.2 Scheﬀe’s Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 337

18.3.3 Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 338

18.3.4 Other Methods for Simultaneous Inference . . . . . . . . . . . . . . . . . . . . 339

18.4 Bayesian Methods

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 339

18.4.1 How It Works . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 341

18.4.1.1 Empirical Bayes Methods . . . . . . . . . . . . . . . . . . . . . . . . 342

18.4.2 Extent of Usage of Subjective Priors . . . . . . . . . . . . . . . . . . . . . . . 342

18.4.3 Arguments Against Use of Subjective Priors . . . . . . . . . . . . . . . . . . . 343

18.4.4 What Would You Do? A Possible Resolution . . . . . . . . . . . . . . . . . . 344

18.4.5 The Markov Chain Monte Carlo Method . . . . . . . . . . . . . . . . . . . . . 345

18.4.6 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 345

19 Mixture Models

349

19.1 The Old Trick Coin Example, Updated . . . . . . . . . . . . . . . . . . . . . . . . . . 349

19.2 General Mixture Distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 349

19.3 Generating Random Variates from a Mixture Distribution . . . . . . . . . . . . . . . 351

19.4 A Useful Tool: the Law of Total Expectation . . . . . . . . . . . . . . . . . . . . . . 351

19.4.1 Conditional Expected Value As a Random Variable

. . . . . . . . . . . . . . 352

19.4.2 Famous Formula: Theorem of Total Expectation . . . . . . . . . . . . . . . . 353

19.4.3 Properties of Conditional Expectation and Variance . . . . . . . . . . . . . . 353

CONTENTS

xvii

19.4.4 Example: More on Flipping Coins with Bonuses

. . . . . . . . . . . . . . . . 354

19.4.5 Example: Trapped Miner

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . 355

19.4.6 Example: Analysis of Hash Tables . . . . . . . . . . . . . . . . . . . . . . . . 357

19.4.7 What About the Variance? . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

19.5 The EM Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 359

19.5.1 Overall Idea . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

19.5.2 The mixtools Package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 360

19.5.3 Example: Old Faithful Geyser

. . . . . . . . . . . . . . . . . . . . . . . . . . 361

19.6 Mean and Variance of Random Variables Having Mixture Distributions

. . . . . . . 363

19.7 Example: Two Kinds of Batteries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 363

19.8 Example: Overdispersion Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 364

19.9 Example: Hidden Markov Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 366

19.10Vector Space Interpretations (for the mathematically adventurous only) . . . . . . . 367

19.10.1 Properties of Correlation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 367

19.10.2 Conditional Expectation As a Projection . . . . . . . . . . . . . . . . . . . . 368

19.11Proof of the Law of Total Expectation . . . . . . . . . . . . . . . . . . . . . . . . . . 370

20 Histograms and Beyond: Nonparametric Density Estimation

375

20.1 Example: Baseball Player Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 375

20.2 Basic Ideas in Density Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . 376

20.3 Histograms

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 377

20.4 Kernel-Based Density Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 378

20.5 Example: Baseball Player Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

20.6 More on Density Estimation in ggplot2 . . . . . . . . . . . . . . . . . . . . . . . . . . 379

20.7 Bias, Variance and Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 379

20.7.1 Bias vs. Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 380

20.7.2 Aliasing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 383

xviii

CONTENTS

20.8 Nearest-Neighbor Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 384

20.9 Estimating a cdf

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 385

20.10Hazard Function Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386

20.11For Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 386

21 Introduction to Model Building

387

21.1 “Desperate for Data” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388

21.1.1 Known Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388

21.1.2 Estimated Mean . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 388

21.1.3 The Bias/Variance Tradeoﬀ . . . . . . . . . . . . . . . . . . . . . . . . . . . . 389

21.1.4 Implications . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 391

21.2 Assessing “Goodness of Fit” of a Model

. . . . . . . . . . . . . . . . . . . . . . . . . 392

21.2.1 The Chi-Square Goodness of Fit Test

. . . . . . . . . . . . . . . . . . . . . . 392

21.2.2 Kolmogorov-Smirnov Conﬁdence Bands . . . . . . . . . . . . . . . . . . . . . 393

21.2.3 Less Formal Methods

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

21.3 Robustness

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 395

21.4 Real Populations and Conceptual Populations . . . . . . . . . . . . . . . . . . . . . . 397

22 Linear Regression

399

22.1 The Goals: Prediction and Description . . . . . . . . . . . . . . . . . . . . . . . . . . 399

22.2 Example Applications: Software Engineering, Networks, Text Mining . . . . . . . . . 400

22.3 Adjusting for Covariates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 401

22.4 What Does “Relationship” Really Mean?

. . . . . . . . . . . . . . . . . . . . . . . . 402

22.4.1 Precise Deﬁnition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 402

22.4.2 (Rather Artiﬁcial) Example: Marble Problem . . . . . . . . . . . . . . . . . . 403

22.5 Estimating That Relationship from Sample Data . . . . . . . . . . . . . . . . . . . . 404

22.5.1 Parametric Models for the Regression Function m()

. . . . . . . . . . . . . . 404

CONTENTS

xix

22.5.2 Estimation in Parametric Regression Models

. . . . . . . . . . . . . . . . . . 405

22.5.3 More on Parametric vs. Nonparametric Models . . . . . . . . . . . . . . . . . 406

22.6 Example: Baseball Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

22.6.1 R Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 407

22.6.2 A Look through the Output . . . . . . . . . . . . . . . . . . . . . . . . . . . . 408

22.7 Multiple Regression: More Than One Predictor Variable . . . . . . . . . . . . . . . . 410

22.8 Example: Baseball Data (cont’d.) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 411

22.9 Interaction Terms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 412

22.10Parametric Estimation of Linear Regression Functions . . . . . . . . . . . . . . . . . 413

22.10.1 Meaning of “Linear” . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 413

22.10.2 Random-X and Fixed-X Regression . . . . . . . . . . . . . . . . . . . . . . . 414

22.10.3 Point Estimates and Matrix Formulation . . . . . . . . . . . . . . . . . . . . 414

22.10.4 Approximate Conﬁdence Intervals

. . . . . . . . . . . . . . . . . . . . . . . . 416

22.11Example: Baseball Data (cont’d.) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 418

22.12Dummy Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 419

22.13Example: Baseball Data (cont’d.) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 420

22.14What Does It All Mean?—Eﬀects of Adding Predictors

. . . . . . . . . . . . . . . . 422

22.15Model Selection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 424

22.15.1 The Overﬁtting Problem in Regression . . . . . . . . . . . . . . . . . . . . . . 425

22.15.2 Relation to the Bias-vs.-Variance Tradefoﬀ . . . . . . . . . . . . . . . . . . . 426

22.15.3 Multicollinearity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 426

22.15.4 Methods for Predictor Variable Selection . . . . . . . . . . . . . . . . . . . . 426

22.15.4.1 Hypothesis Testing

. . . . . . . . . . . . . . . . . . . . . . . . . . . 427

22.15.4.2 Conﬁdence Intervals . . . . . . . . . . . . . . . . . . . . . . . . . . . 428

22.15.4.3 Predictive Ability Indicators

. . . . . . . . . . . . . . . . . . . . . . 428

22.15.4.4 The LASSO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

22.15.5 Rough Rules of Thumb . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 429

xx

CONTENTS

22.16Prediction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430

22.16.1 Height/Weight Age Example . . . . . . . . . . . . . . . . . . . . . . . . . . . 430

22.16.2 R’s predict() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 430

22.17Example: Turkish Teaching Evaluation Data . . . . . . . . . . . . . . . . . . . . . . 431

22.17.1 The Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431

22.17.2 Data Prep . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 431

22.17.3 Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 433

22.18What About the Assumptions? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 435

22.18.1 Exact Conﬁdence Intervals and Tests . . . . . . . . . . . . . . . . . . . . . . . 435

22.18.2 Is the Homoscedasticity Assumption Important? . . . . . . . . . . . . . . . . 436

22.18.3 Regression Diagnostics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 436

22.19Case Studies

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

22.19.1 Example: Prediction of Network RTT . . . . . . . . . . . . . . . . . . . . . . 437

22.19.2 Transformations

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 437

22.19.3 Example: OOP Study . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 438

23 Classiﬁcation

443

23.1 Classiﬁcation = Regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 444

23.1.1 What Happens with Regression in the Case Y = 0,1?

. . . . . . . . . . . . . 444

23.2 Logistic Regression: a Common Parametric Model for the Regression Function in

Classiﬁcation Problems

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 445

23.2.1 The Logistic Model: Motivations . . . . . . . . . . . . . . . . . . . . . . . . . 445

23.2.2 Esimation and Inference for Logit Coeﬃcients . . . . . . . . . . . . . . . . . . 447

23.3 Example: Forest Cover Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448

23.3.0.1 R Code . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 448

23.3.1 Analysis of the Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 449

23.4 The Multiclass Case . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451

CONTENTS

xxi

23.4.1 One vs. All Approach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 451

23.4.2 Issues of Data Balance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 452

23.4.2.1 Statement of the Problem . . . . . . . . . . . . . . . . . . . . . . . . 452

23.4.2.2 Solutions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 453

23.5 Model Selection in Classiﬁcation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 454

23.6 Optimality of the Regression Function for 0-1-Valued Y (optional section) . . . . . . 454

24 Nonparametric Estimation of Regression and Classiﬁcation Functions

457

24.1 Methods Based on Estimating mY ;X (t) . . . . . . . . . . . . . . . . . . . . . . . . . . 457

24.1.1 Nearest-Neighbor Methods

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 458

24.1.2 Kernel-Based Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 460

24.1.3 The Naive Bayes Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 461

24.2 Methods Based on Estimating Classiﬁcation Boundaries . . . . . . . . . . . . . . . . 462

24.2.1 Support Vector Machines (SVMs)

. . . . . . . . . . . . . . . . . . . . . . . . 462

24.2.2 CART . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 463

24.3 Comparison of Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 465

25 Relations AmongVariables

467

25.1 Principal Components Analysis (PCA) . . . . . . . . . . . . . . . . . . . . . . . . . . 467

25.1.1 How to Calculate Them . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 468

25.1.2 Example: Forest Cover Data . . . . . . . . . . . . . . . . . . . . . . . . . . . 469

25.1.3 Scaling

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

25.1.4 Scope of Application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 470

25.1.5 Example: Turkish Teaching Evaluation Data . . . . . . . . . . . . . . . . . . 471

25.2 Log-Linear Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473

25.2.1 The Setting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473

25.2.2 The Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 473

xxii

CONTENTS

25.2.3 The Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 474

25.2.4 Interpretation of Parameters

. . . . . . . . . . . . . . . . . . . . . . . . . . . 476

25.2.5 Parameter Estimation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 477

25.2.6 Example: Hair, Eye Color . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 478

25.2.6.1 The loglin() Function . . . . . . . . . . . . . . . . . . . . . . . . . . 478

25.2.7 Hair/Eye Color Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 479

25.2.8 Obtaining Standard Errors

. . . . . . . . . . . . . . . . . . . . . . . . . . . . 482

25.3 Clustering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482

25.3.1 K-Means Clustering . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 482

25.3.1.1 The Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 483

25.3.1.2 Example: the Baseball Player Data . . . . . . . . . . . . . . . . . . 483

25.3.2 Mixture Models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 484

25.3.3 Spectral Models

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

25.3.4 Other R Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

25.3.5 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

25.4 Simpson’s (Non-)Paradox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 485

25.4.1 Example: UC Berkeley Graduate Admission Data . . . . . . . . . . . . . . . 486

25.4.1.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 486

25.4.1.2 Log-Linear Analysis . . . . . . . . . . . . . . . . . . . . . . . . . . . 486

25.4.2 Toward Making It Simpson’s NON-Paradox . . . . . . . . . . . . . . . . . . . 489

A R Quick Start

491

A.1 Correspondences

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 491

A.2 Starting R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492

A.3 First Sample Programming Session . . . . . . . . . . . . . . . . . . . . . . . . . . . . 492

A.4 Second Sample Programming Session . . . . . . . . . . . . . . . . . . . . . . . . . . . 496

A.5 Third Sample Programming Session . . . . . . . . . . . . . . . . . . . . . . . . . . . 498

CONTENTS

xxiii

A.6 Default Argument Values

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499

A.7 The R List Type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499

A.7.1 The Basics

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 499

A.7.2 The Reduce() Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 500

A.7.3 S3 Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 501

A.7.4 Handy Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 502

A.8 Data Frames

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 503

A.9 Graphics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 505

A.10 Packages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 506

A.11 Other Sources for Learning R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507

A.12 Online Help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507

A.13 Debugging in R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 507

A.14 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508

A.15 Further Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 508

B Review of Matrix Algebra

509

B.1 Terminology and Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 509

B.1.1 Matrix Addition and Multiplication . . . . . . . . . . . . . . . . . . . . . . . 510

B.2 Matrix Transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 511

B.3 Linear Independence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512

B.4 Determinants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512

B.5 Matrix Inverse

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 512

B.6 Eigenvalues and Eigenvectors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 513

B.7 Matrix Algebra in R . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 514

C Introduction to the ggplot2 Graphics Package

517

C.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

xxiv

CONTENTS

C.2 Installation and Use . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 517

C.3 Basic Structures

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 518

C.4 Example: Simple Line Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 519

C.5 Example: Census Data . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 521

C.6 Function Plots, Density Estimates and Smoothing . . . . . . . . . . . . . . . . . . . 528

C.7 What’s Going on Inside . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 529

C.8 For Further Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 531

Preface

Why is this book diﬀerent from all other books on mathematical probability and statistics? The key
aspect is the book’s consistently applied approach, especially important for engineering students.

The applied nature comes is manifested in a number of senses. First, there is a strong emphasis
on intution, with less mathematical formalism. In my experience, deﬁning probability via sample
spaces, the standard approach, is a major impediment to doing good applied work. The same holds
for deﬁning expected value as a weighted average. Instead, I use the intuitive, informal approach
I believe this is especially helpful when explaining
of long-run frequency and long-run average.
conditional probability and expectation, concepts that students tend to have trouble with. (They
often think they understand until they actually have to work a problem using the concepts.)

On the other hand, in spite of the relative lack of formalism, all models and so on are described
precisely in terms of random variables and distributions. And the material is actually somewhat
more mathematical than most at this level in the sense that it makes extensive usage of linear
algebra.

Second, the book stresses real-world applications. Many similar texts, notably the elegant and
interesting book for computer science students by Mitzenmacher, focus on probability, in fact
discrete probability. Their intended class of “applications” is the theoretical analysis of algorithms.
I instead focus on the actual use of the material in the real world; which tends to be more continuous
than discrete, and more in the realm of statistics than probability. This should prove especially
valuable, as “big data” and machine learning now play a signiﬁcant role in applications of computers.

Third, there is a strong emphasis on modeling. Considerable emphasis is placed on questions such
as: What do probabilistic models really mean, in real-life terms? How does one choose a model?
How do we assess the practical usefulness of models? This aspect is so important that there is
a separate chapter for this, titled Introduction to Model Building. Throughout the text, there is
considerable discussion of the real-world meaning of probabilistic concepts. For instance, when
probability density functions are introduced, there is an extended discussion regarding the intuitive
meaning of densities in light of the inherently-discrete nature of real data, due to the ﬁnite precision
of measurement.

xxv

xxvi

CONTENTS

Finally, the R statistical/data analysis language is used throughout. Again, several excellent texts
on probability and statistics have been written that feature R, but this book, by virtue of having a
computer science audience, uses R in a more sophisticated manner. My open source tutorial on R
programming, R for Programmers (http://heather.cs.ucdavis.edu/~matloff/R/RProg.pdf),
can be used as a supplement. (More advanced R programming is covered in my book, The Art of
R Programming, No Starch Press, 2011.)

There is a large amount of material here. For my one-quarter undergraduate course, I usually
cover Chapters 2, 3, 6, 7, 8, 10, 12, 15, 16, 17, 18 and 22. My lecture style is conversational,
referring to material in the book and making lots of supplementary remarks (“What if we changed
the assumption here to such-and-such?” etc.). Students read the details on their own. For my
one-quarter graduate course, I cover Chapters 10, 11, 19, ??, ??, 20, 21, 22, 23, 24 and 25.

As prerequisites, the student must know calculus, basic matrix algebra, and have some skill in
programming. As with any text in probability and statistics, it is also necessary that the student
has a good sense of math intuition, and does not treat mathematics as simply memorization of
formulas.

The LATEXsource .tex ﬁles for this book are in http://heather.cs.ucdavis.edu/~matloff/132/
PLN, so readers can copy the R code and experiment with it. (It is not recommanded to copy-and-
paste from the PDF ﬁle, as hidden characters may be copied.) The PDF ﬁle is searchable.

The following, among many, provided valuable feedback for which I am very grateful: Ibrahim
Ahmed; Ahmed Ahmedin; Stuart Ambler; Earl Barr; Benjamin Beasley; Matthew Butner; Michael
Cliﬀord; Dipak Ghosal; Noah Gift; Laura Matloﬀ; Nelson Max, Connie Nguyen, Jack Norman,
Richard Oehrle, Michael Rea, Sana Vaziri, Yingkang Xie, and Ivana Zetko.

Many of the data sets used in the book are from the UC Irvine Machine Learning Repository, http:
//archive.ics.uci.edu/ml/. Thanks to UCI for making available this very valuable resource.

The book contains a number of references for further reading. Since the audience includes a number
of students at my institution, the University of California, Davis, I often refer to work by current
or former UCD faculty, so that students can see what their professors do in research.

This work is licensed under a Creative Commons Attribution-No Derivative Works 3.0 United States
License. The details may be viewed at http://creativecommons.org/licenses/by-nd/3.0/us/,
but in essence it states that you are free to use, copy and distribute the work, but you must
attribute the work to me and not “alter, transform, or build upon” it. If you are using the book,
either in teaching a class or for your own learning, I would appreciate your informing me. I retain
copyright in all non-U.S. jurisdictions, but permission to use these materials in teaching is still
granted, provided the licensing information here is displayed.

Chapter 1

Time Waste Versus Empowerment

I took a course in speed reading, and read War and Peace in 20 minutes.
comedian Woody Allen

It’s about Russia—

I learned very early the diﬀerence between knowing the name of something and knowing something—
Richard Feynman, Nobel laureate in physics

The main goal [of this course] is self-actualization through the empowerment of claiming your
education—UCSC (and former UCD) professor Marc Mangel, in the syllabus for his calculus course

What does this really mean? Hmm, I’ve never thought about that—UCD PhD student in statistics,
in answer to a student who asked the actual meaning of a very basic concept

You have a PhD in engineering. You may have forgotten technical details like d
dt sin(t) = cos(t),
but you should at least understand the concepts of rates of change—the author, gently chiding a
friend who was having trouble following a simple quantitative discussion of trends in California’s
educational system

Give me six hours to chop down a tree and I will spend the ﬁrst four sharpening the axe—Abraham
Lincoln

The ﬁeld of probability and statistics (which, for convenience, I will refer to simply as “statistics”
below) impacts many aspects of our daily lives—business, medicine, the law, government and so
on. Consider just a few examples:

• The statistical models used on Wall Street made the “quants” (quantitative analysts) rich—

but also contributed to the worldwide ﬁnancial crash of 2008.

• In a court trial, large sums of money or the freedom of an accused may hinge on whether the

1

2

CHAPTER 1. TIME WASTE VERSUS EMPOWERMENT

judge and jury understand some statistical evidence presented by one side or the other.

• Wittingly or unconsciously, you are using probability every time you gamble in a casino—and

every time you buy insurance.

• Statistics is used to determine whether a new medical treatment is safe/eﬀective for you.

• Statistics is used to ﬂag possible terrorists—but sometimes unfairly singling out innocent

people while other times missing ones who really are dangerous.

Clearly, statistics matters. But it only has value when one really understands what it means and
what it does. Indeed, blindly plugging into statistical formulas can be not only valueless but in
fact highly dangerous, say if a bad drug goes onto the market.

Yet most people view statistics as exactly that—mindless plugging into boring formulas. If even
the statistics graduate student quoted above thinks this, how can the students taking the course
be blamed for taking that atititude?

I once had a student who had an unusually good understanding of probability. It turned out that
this was due to his being highly successful at playing online poker, winning lots of cash. No blind
formula-plugging for him! He really had to understand how probability works.

Statistics is not just a bunch of formulas. On the contrary, it can be mathematically deep, for those
who like that kind of thing. (Much of statistics can be viewed as the Pythagorean Theorem in
n-dimensional or even inﬁnite-dimensional space.) But the key point is that anyone who has taken
a calculus course can develop true understanding of statistics, of real practical value. As Professor
Mangel says, that’s empowering.

Statistics is based on probabilistic models. So, in order to become eﬀective at data analysis, one
must really master the principles of probability; this is where Lincoln’s comment about “sharpening
the axe” truly applies.

So as you make your way through this book, always stop to think, “What does this equation really
mean? What is its goal? Why are its ingredients deﬁned in the way they are? Might there be a
better way? How does this relate to our daily lives?” Now THAT is empowering.

Chapter 2

Basic Probability Models

This chapter will introduce the general notions of probability. Most of it will seem intuitive to you,
but pay careful attention to the general principles which are developed; in more complex settings
intuition may not be enough, and the tools discussed here will be very useful.

2.1 ALOHA Network Example

Throughout this book, we will be discussing both “classical” probability examples involving coins,
cards and dice, and also examples involving applications to computer science. The latter will involve
diverse ﬁelds such as data mining, machine learning, computer networks, software engineering and
bioinformatics.

In this section, an example from computer networks is presented which will be used at a number
of points in this chapter. Probability analysis is used extensively in the development of new, faster
types of networks.

Today’s Ethernet evolved from an experimental network developed at the University of Hawaii,
called ALOHA. A number of network nodes would occasionally try to use the same radio channel to
communicate with a central computer. The nodes couldn’t hear each other, due to the obstruction
of mountains between them. If only one of them made an attempt to send, it would be successful,
and it would receive an acknowledgement message in response from the central computer. But if
more than one node were to transmit, a collision would occur, garbling all the messages. The
sending nodes would timeout after waiting for an acknowledgement which never came, and try
sending again later. To avoid having too many collisions, nodes would engage in random backoﬀ,
meaning that they would refrain from sending for a while even though they had something to send.

One variation is slotted ALOHA, which divides time into intervals which I will call “epochs.” Each

3

4

CHAPTER 2. BASIC PROBABILITY MODELS

epoch will have duration 1.0, so epoch 1 extends from time 0.0 to 1.0, epoch 2 extends from 1.0 to
2.0 and so on. In the version we will consider here, in each epoch, if a node is active, i.e. has a
message to send, it will either send or refrain from sending, with probability p and 1-p. The value
of p is set by the designer of the network. (Real Ethernet hardware does something like this, using
a random number generator inside the chip.)

The other parameter q in our model is the probability that a node which had been inactive generates
a message during an epoch, i.e. the probability that the user hits a key, and thus becomes “active.”
Think of what happens when you are at a computer. You are not typing constantly, and when you
are not typing, the time until you hit a key again will be random. Our parameter q models that
randomness.

Let n be the number of nodes, which we’ll assume for simplicity is two. Assume also for simplicity
that the timing is as follows. Arrival of a new message happens in the middle of an epoch, and the
decision as to whether to send versus back oﬀ is made near the end of an epoch, say 90% into the
epoch.

For example, say that at the beginning of the epoch which extends from time 15.0 to 16.0, node A
has something to send but node B does not. At time 15.5, node B will either generate a message
to send or not, with probability q and 1-q, respectively. Suppose B does generate a new message.
At time 15.9, node A will either try to send or refrain, with probability p and 1-p, and node B will
do the same. Suppose A refrains but B sends. Then B’s transmission will be successful, and at the
start of epoch 16 B will be inactive, while node A will still be active. On the other hand, suppose
both A and B try to send at time 15.9; both will fail, and thus both will be active at time 16.0,
and so on.

Be sure to keep in mind that in our simple model here, during the time a node is active, it won’t
generate any additional new messages.

(Note: The deﬁnition of this ALOHA model is summarized concisely on page 10.)

Let’s observe the network for two epochs, epoch 1 and epoch 2. Assume that the network consists
of just two nodes, called node 1 and node 2, both of which start out active. Let X1 and X2 denote
the numbers of active nodes at the very end of epochs 1 and 2, after possible transmissions. We’ll
take p to be 0.4 and q to be 0.8 in this example.

Let’s ﬁnd P (X1 = 2), the probability that X1 = 2, and then get to the main point, which is to ask
what we really mean by this probability.

How could X1 = 2 occur? There are two possibilities:

• both nodes try to send; this has probability p2

• neither node tries to send; this has probability (1 − p)2

2.2. THE CRUCIAL NOTION OF A REPEATABLE EXPERIMENT

5

1,1
2,1
3,1
4,1
5,1
6,1

1,2
2,2
3,2
4,2
5,2
6,2

1,3
2,3
3,3
4,3
5,3
6,3

1,4
2,4
3,4
4,4
5,4
6,4

1,5
2,5
3,5
4,5
5,5
6,5

1,6
2,6
3,6
4,6
5,6
6,6

Table 2.1: Sample Space for the Dice Example

Thus

P (X1 = 2) = p2 + (1 − p)2 = 0.52

(2.1)

2.2 The Crucial Notion of a Repeatable Experiment

It’s crucial to understand what that 0.52 ﬁgure really means in a practical sense. To this end, let’s
put the ALOHA example aside for a moment, and consider the “experiment” consisting of rolling
two dice, say a blue one and a yellow one. Let X and Y denote the number of dots we get on the
blue and yellow dice, respectively, and consider the meaning of P (X + Y = 6) = 5
36 .

In the mathematical theory of probability, we talk of a sample space, which (in simple cases)
consists of the possible outcomes (X, Y ), seen in Table 2.1. In a theoretical treatment, we place
weights of 1/36 on each of the points in the space, reﬂecting the fact that each of the 36 points is
equally likely, and then say, “What we mean by P (X + Y = 6) = 5
36 is that the outcomes (1,5),
(2,4), (3,3), (4,2), (5,1) have total weight 5/36.”

Unfortunately, the notion of sample space becomes mathematically tricky when developed for more
complex probability models. Indeed, it requires graduate-level math, called measure theory.

And much worse, under the sample space approach, one loses all the intuition.
In particular,
there is no good way using set theory to convey the intuition underlying conditional
probability (to be introduced in Section 2.3). The same is true for expected value, a central topic
to be introduced in Section 3.5.

In any case, most probability computations do not rely on explicitly writing down a sample space.
In this particular example it is useful for us as a vehicle for explaining the concepts, but we will
NOT use it much. Those who wish to get a more theoretical grounding can get a start in Section

6

CHAPTER 2. BASIC PROBABILITY MODELS

notebook line
1
2
3
4
5
6
7
8
9

blue+yellow = 6?

outcome
blue 2, yellow 6 No
blue 3, yellow 1 No
blue 1, yellow 1 No
blue 4, yellow 2 Yes
blue 1, yellow 1 No
blue 3, yellow 4 No
blue 5, yellow 1 Yes
blue 3, yellow 6 No
blue 2, yellow 5 No

Table 2.2: Notebook for the Dice Problem

5.4.

But the intuitive notion—which is FAR more important—of what P (X + Y = 6) = 5
36 means is
the following. Imagine doing the experiment many, many times, recording the results in a large
notebook:

• Roll the dice the ﬁrst time, and write the outcome on the ﬁrst line of the notebook.

• Roll the dice the second time, and write the outcome on the second line of the notebook.

• Roll the dice the third time, and write the outcome on the third line of the notebook.

• Roll the dice the fourth time, and write the outcome on the fourth line of the notebook.

• Imagine you keep doing this, thousands of times, ﬁlling thousands of lines in the notebook.

The ﬁrst 9 lines of the notebook might look like Table 2.2. Here 2/9 of these lines say Yes. But
after many, many repetitions, approximately 5/36 of the lines will say Yes. For example, after
doing the experiment 720 times, approximately 5

36 × 720 = 100 lines will say Yes.

This is what probability really is: In what fraction of the lines does the event of interest happen?
It sounds simple, but if you always think about this “lines in the notebook” idea,
probability problems are a lot easier to solve. And it is the fundamental basis of computer
simulation.

2.3. OUR DEFINITIONS

2.3 Our Deﬁnitions

7

These deﬁnitions are intuitive, rather than rigorous math, but intuition is what we need. Keep in
mind that we are making deﬁnitions below, not listing properties.

• We assume an “experiment” which is (at least in concept) repeatable. The experiment of
rolling two dice is repeatable, and even the ALOHA experiment is so. (We simply watch the
network for a long time, collecting data on pairs of consecutive epochs in which there are
two active stations at the beginning.) On the other hand, the econometricians, in forecasting
2009, cannot “repeat” 2008. Yet all of the econometricians’ tools assume that events in 2008
were aﬀected by various sorts of randomness, and we think of repeating the experiment in a
conceptual sense.

• We imagine performing the experiment a large number of times, recording the result of each

repetition on a separate line in a notebook.

• We say A is an event for this experiment if it is a possible boolean (i.e. yes-or-no) outcome

of the experiment. In the above example, here are some events:

* X+Y = 6

* X = 1

* Y = 3

* X-Y = 4

• A random variable is a numerical outcome of the experiment, such as X and Y here, as

well as X+Y, 2XY and even sin(XY).

• For any event of interest A, imagine a column on A in the notebook. The kth line in the
notebook, k = 1,2,3,..., will say Yes or No, depending on whether A occurred or not during
the kth repetition of the experiment. For instance, we have such a column in our table above,
for the event {A = blue+yellow = 6}.

• For any event of interest A, we deﬁne P(A) to be the long-run fraction of lines with Yes

entries.

• For any events A, B, imagine a new column in our notebook, labeled “A and B.” In each line,
this column will say Yes if and only if there are Yes entries for both A and B. P(A and B) is
then the long-run fraction of lines with Yes entries in the new column labeled “A and B.”1

1In most textbooks, what we call “A and B” here is written A∩B, indicating the intersection of two sets in the

sample space. But again, we do not take a sample space point of view here.

8

CHAPTER 2. BASIC PROBABILITY MODELS

• For any events A, B, imagine a new column in our notebook, labeled “A or B.” In each line,

this column will say Yes if and only if at least one of the entries for A and B says Yes.2

• For any events A, B, imagine a new column in our notebook, labeled “A | B” and pronounced

“A given B.” In each line:

* This new column will say “NA” (“not applicable”) if the B entry is No.

* If it is a line in which the B column says Yes, then this new column will say Yes or No,

depending on whether the A column says Yes or No.

Think of probabilities in this “notebook” context:

• P(A) means the long-run fraction of lines in the notebook in which the A column says Yes.

• P(A or B) means the long-run fraction of lines in the notebook in which the A-or-B column

says Yes.

• P(A and B) means the long-run fraction of lines in the notebook in which the A-and-B column

says Yes.

• P(A | B) means the long-run fraction of lines in the notebook in which the A | B column says

Yes—among the lines which do NOT say NA.

A hugely common mistake is to confuse P(A and B) and P(A | B). This is where the
notebook view becomes so important. Compare the quantities P (X = 1 and S = 6) = 1
36 and
P (X = 1|S = 6) = 1

5 , where S = X+Y:3

• After a large number of repetitions of the experiment, approximately 1/36 of the lines of the
notebook will have the property that both X = 1 and S = 6 (since X = 1 and S = 6 is
equivalent to X = 1 and Y = 5).

• After a large number of repetitions of the experiment, if we look only at the lines in
which S = 6, then among those lines, approximately 1/5 of those lines will show X =
1.

The quantity P(A|B) is called the conditional probability of A, given B.

Note that and has higher logical precedence than or. For example, P(A and B or C) means P[(A
and B) or C]. Also, not has higher precedence than and.

Here are some more very important deﬁnitions and properties:

2In the sample space approach, this is written A ∪ B.
3Think of adding an S column to the notebook too

2.3. OUR DEFINITIONS

9

• Deﬁnition 1 Suppose A and B are events such that it is impossible for them to occur in the

same line of the notebook. They are said to be disjoint events.

• If A and B are disjoint events, then

P (A or B) = P (A) + P (B)

(2.2)

Again, this terminology disjoint stems from the set-theoretic sample space approach, where
it means that A ∩ B = φ. That mathematical terminology works ﬁne for our dice example,
but in my experience people have major diﬃculty applying it correctly in more complicated
problems. This is another illustration of why I put so much emphasis on the “notebook”
framework.

By writing

(2.2) can be iterated, e.g.

{A or B or C} = {A or [B or C]} =

(2.3)

P (A or B or C) = P (A) + P (B) + P (C)

(2.4)

• If A and B are not disjoint, then

P (A or B) = P (A) + P (B) − P (A and B)

(2.5)

In the disjoint case, that subtracted term is 0, so (2.5) reduces to (2.2).

• Deﬁnition 2 Events A and B are said to be stochastically independent, usually just

stated as independent,4 if

P (A and B) = P (A) · P (B)

(2.6)

• In calculating an “and” probability, how does one know whether the events are independent?
The answer is that this will typically be clear from the problem. If we toss the blue and yellow
dice, for instance, it is clear that one die has no impact on the other, so events involving the
blue die are independent of events involving the yellow die. On the other hand, in the ALOHA
example, it’s clear that events involving X1 are NOT independent of those involving X2.

4The term stochastic is just a fancy synonym for random.

10

CHAPTER 2. BASIC PROBABILITY MODELS

• If A and B are not independent, the equation (2.6) generalizes to

P (A and B) = P (A)P (B|A)

(2.7)

This should make sense to you. Suppose 30% of all UC Davis students are in engineering,
and 20% of all engineering majors are female. That would imply that 0.30 x 0.20 = 0.06, i.e.
6% of all UCD students are female engineers.

Note that if A and B actually are independent, then P (B|A) = P (B), and (2.7) reduces to
(2.6).

Note too that (2.7) implies

P (B|A) =

P (A and B)
P (A)

(2.8)

2.4 “Mailing Tubes”

If I ever need to buy some mailing tubes, I can come here—friend of the author’s, while browsing
through an oﬃce supplies store

Examples of the above properties, e.g. (2.6) and (2.7), will be given starting in Section 2.5. But
ﬁrst, a crucial strategic point in learning probability must be addressed.

Some years ago, a friend of mine was in an oﬃce supplies store, and he noticed a rack of mailing
tubes. My friend made the remark shown above. Well, (2.6) and 2.7 are “mailing tubes”–make a
mental note to yourself saying, “If I ever need to ﬁnd a probability involving and, one thing I can
try is (2.6) and (2.7).” Be ready for this!

This mailing tube metaphor will be mentioned often, such as in Section 3.5.5 .

2.5 Example: ALOHA Network

Please keep in mind that the notebook idea is simply a vehicle to help you understand what the
concepts really mean. This is crucial for your intuition and your ability to apply this material in
the real world. But the notebook idea is NOT for the purpose of calculating probabilities. Instead,
we use the properties of probability, as seen in the following.

Let’s look at all of this in the ALOHA context. Here’s a summary:

2.5. EXAMPLE: ALOHA NETWORK

11

• We have n network nodes, sharing a common communications channel.

• Time is divided in epochs. Xk denotes the number of active nodes at the end of epoch k,

which we will sometimes refer to as the state of the system in epoch k.

• If two or more nodes try to send in an epoch, they collide, and the message doesn’t get

through.

• We say a node is active if it has a message to send.

• If a node is active node near the end of an epoch, it tries to send with probability p.

• If a node is inactive at the beginning of an epoch, then at the middle of the epoch it will

generate a message to send with probability q.

• In our examples here, we have n = 2 and X0 = 2, i.e. both nodes start out active.

Now, in Equation (2.1) we found that

P (X1 = 2) = p2 + (1 − p)2 = 0.52

(2.9)

How did we get this? Let Ci denote the event that node i tries to send, i = 1,2. Then using the
deﬁnitions above, our steps would be

P (X1 = 2) = P (C1 and C2
(cid:125)

(cid:123)(cid:122)

(cid:124)

)
or not C1 and not C2
(cid:125)

(cid:123)(cid:122)

(cid:124)

= P (C1 and C2) + P ( not C1 and not C2) (from (2.2))
= P (C1)P (C2) + P ( not C1)P ( not C2) (from (2.6))
= p2 + (1 − p)2

(2.10)

(2.11)

(2.12)

(2.13)

(The underbraces in (2.10) do not represent some esoteric mathematical operation. There are there
simply to make the grouping clearer, corresponding to events G and H deﬁned below.)

Here are the reasons for these steps:

(2.10): We listed the ways in which the event {X1 = 2} could occur.

(2.11): Write G = C1 and C2, H = D1 and D2, where Di = not Ci, i = 1,2. Then the events G and
H are clearly disjoint; if in a given line of our notebook there is a Yes for G, then deﬁnitely
there will be a No for H, and vice versa.

12

CHAPTER 2. BASIC PROBABILITY MODELS

(2.12): The two nodes act physically independently of each other. Thus the events C1 and C2 are
stochastically independent, so we applied (2.6). Then we did the same for D1 and D2.

Now, what about P (X2 = 2)? Again, we break big events down into small events, in this case
according to the value of X1:

P (X2 = 2) = P (X1 = 0 and X2 = 2 or X1 = 1 and X2 = 2 or X1 = 2 and X2 = 2)

= P (X1 = 0 and X2 = 2)
+ P (X1 = 1 and X2 = 2)
+ P (X1 = 2 and X2 = 2)

(2.14)

Since X1 cannot be 0, that ﬁrst term, P (X1 = 0 and X2 = 2) is 0. To deal with the second term,
P (X1 = 1 and X2 = 2), we’ll use (2.7). Due to the time-sequential nature of our experiment here,
it is natural (but certainly not “mandated,” as we’ll often see situations to the contrary) to take A
and B to be {X1 = 1} and {X2 = 2}, respectively. So, we write

P (X1 = 1 and X2 = 2) = P (X1 = 1)P (X2 = 2|X1 = 1)

(2.15)

To calculate P (X1 = 1), we use the same kind of reasoning as in Equation (2.1). For the event in
question to occur, either node A would send and B wouldn’t, or A would refrain from sending and
B would send. Thus

P (X1 = 1) = 2p(1 − p) = 0.48

(2.16)

Now we need to ﬁnd P (X2 = 2|X1 = 1). This again involves breaking big events down into small
ones. If X1 = 1, then X2 = 2 can occur only if both of the following occur:

• Event A: Whichever node was the one to successfully transmit during epoch 1—and we are

given that there indeed was one, since X1 = 1—now generates a new message.

• Event B: During epoch 2, no successful transmission occurs, i.e. either they both try to send

or neither tries to send.

Recalling the deﬁnitions of p and q in Section 2.1, we have that

P (X2 = 2|X1 = 1) = q[p2 + (1 − p)2] = 0.41

(2.17)

2.5. EXAMPLE: ALOHA NETWORK

13

Thus P (X1 = 1 and X2 = 2) = 0.48 × 0.41 = 0.20.

We go through a similar analysis for P (X1 = 2 and X2 = 2): We recall that P (X1 = 2) = 0.52
from before, and ﬁnd that P (X2 = 2|X1 = 2) = 0.52 as well. So we ﬁnd P (X1 = 2 and X2 = 2) to
be 0.522 = 0.27. Putting all this together, we ﬁnd that P (X2 = 2) = 0.47.

Let’s do another; let’s ﬁnd P (X1 = 1|X2 = 2). [Pause a minute here to make sure you understand
that this is quite diﬀerent from P (X2 = 2|X1 = 1).] From (2.8), we know that

P (X1 = 1|X2 = 2) =

P (X1 = 1 and X2 = 2)
P (X2 = 2)

(2.18)

We computed both numerator and denominator here before, in Equations (2.15) and (2.14), so we
see that P (X1 = 1|X2 = 2) = 0.20/0.47 = 0.43.

So, in our notebook view, if we were to look only at lines in the notebook for which X2 = 2, a
fraction 0.43 of those lines would have X1 = 1.

You might be bothered that we are looking “backwards in time” in (2.18), kind of guessing the past
from the present. There is nothing wrong or unnatural about that. Jurors in court trials do it all
the time, though presumably not with formal probability calculation. And evolutionary biologists
do use formal probability models to guess the past.

And one more calculation: P (X1 = 2 or X2 = 2). From (2.5),

P (X1 = 2 or X2 = 2) = P (X1 = 2) + P (X2 = 2) − P (X1 = 2 and X2 = 2)

(2.19)

Luckily, we’ve already calculated all three probabilities on the right-hand side to be 0.52, 0.47 and
0.27, respectively. Thus P (X1 = 2 or X2 = 2) = 0.72.

Note by the way that events involving X2 are NOT independent of those involving X1. For instance,
we found in (2.18) that

P (X1 = 1|X2 = 2) = 0.43

(2.20)

yet from (2.16) we have

P (X1 = 1) = 0.48.

(2.21)

14

CHAPTER 2. BASIC PROBABILITY MODELS

2.6 Bayes’ Rule

(This section should not be confused with Section 18.4. The latter is highly controversial, while
the material in this section is not controversial at all.)

Following (2.18) above, we noted that the ingredients had already been computed, in (2.15) and
(2.14). If we go back to the derivations in those two equations and substitute in (2.18), we have

P (X1 = 1|X2 = 2) =

P (X1 = 1 and X2 = 2)
P (X2 = 2)

=

=

P (X1 = 1 and X2 = 2)
P (X1 = 1 and X2 = 2) + P (X1 = 2 and X2 = 2)

P (X1 = 1)P (X2 = 2|X1 = 1)
P (X1 = 1)P (X2 = 2|X1 = 1) + P (X1 = 2)P (X2 = 2|X1 = 2)

Looking at this in more generality, for events A and B we would ﬁnd that

P (A|B) =

P (A)P (B|A)
P (A)P (B|A) + P (not A)P (B|not A)

(2.22)

(2.23)

(2.24)

(2.25)

It can be extended easily to cases with
This is known as Bayes’ Theorem or Bayes’ Rule.
several terms in the denominator, arising from situations that need to be broken down into several
subevents rather than just A and not-A.

2.7 ALOHA in the Notebook Context

Think of doing the ALOHA “experiment” many, many times.

• Run the network for two epochs, starting with both nodes active, the ﬁrst time, and write

the outcome on the ﬁrst line of the notebook.

• Run the network for two epochs, starting with both nodes active, the second time, and write

the outcome on the second line of the notebook.

• Run the network for two epochs, starting with both nodes active, the third time, and write

the outcome on the third line of the notebook.

• Run the network for two epochs, starting with both nodes active, the fourth time, and write

the outcome on the fourth line of the notebook.

2.8. A NOTE ON MODELING

15

notebook line X1 = 2 X2 = 2 X1 = 2 and X2 = 2 X2 = 2|X1 = 2
No
1
No
2
Yes
3
No
4
Yes
5
No
6
No
7

Yes
No
Yes
Yes
Yes
No
No

No
NA
Yes
No
Yes
NA
NA

No
No
Yes
No
Yes
No
Yes

Table 2.3: Top of Notebook for Two-Epoch ALOHA Experiment

• Imagine you keep doing this, thousands of times, ﬁlling thousands of lines in the notebook.

The ﬁrst seven lines of the notebook might look like Table 2.3. We see that:

• Among those ﬁrst seven lines in the notebook, 4/7 of them have X1 = 2. After many, many

lines, this fraction will be approximately 0.52.

• Among those ﬁrst seven lines in the notebook, 3/7 of them have X2 = 2. After many, many

lines, this fraction will be approximately 0.47.5

• Among those ﬁrst seven lines in the notebook, 3/7 of them have X1 = 2 and X2 = 2. After

many, many lines, this fraction will be approximately 0.27.

• Among the ﬁrst seven lines in the notebook, four of them do not say NA in the X2 = 2|X1 = 2
column. Among these four lines, two say Yes, a fraction of 2/4. After many, many lines,
this fraction will be approximately 0.52.

2.8 A Note on Modeling

Here is a question to test your understanding of the ALOHA model—not the calculation of prob-
abilities, but rather the meaning of the model itself. What kinds of properties are we trying to
capture in the model?

So, consider this question:

5Don’t make anything of the fact that these probabilities nearly add up to 1.

16

CHAPTER 2. BASIC PROBABILITY MODELS

Consider the ALOHA network model. Say we have two such networks, A and B. In A,
the network typically is used for keyboard input, such as a user typing e-mail or editing
a ﬁle. But in B, users tend to do a lot of ﬁle uploading, not just typing. Fill in the
blanks: In B, the model parameter
than in A, and in order to
is
in B.
accommodate this, we should set the parameter

to be relatively

In network B we have heavy traﬃc. Thus, when a node becomes idle, it is quite likely to have a
new message to send right away.6 Thus q is large.

That means we need to be especially worried about collisions, so we probably should set p to a low
value.

2.9 Solution Strategies

The example in Section 2.5 shows typical stategies in exploring solutions to probability problems,
such as:

• Name what seem to be the important variables and events, in this case X1, X2, C1, C2 and

so on.

• Write the given probability in terms of those named variables, e.g.

P (X1 = 2) = P (C1 and C2
(cid:125)

(cid:123)(cid:122)

(cid:124)

)
or not C1 and not C2
(cid:125)

(cid:123)(cid:122)

(cid:124)

(2.26)

above.

• Ask the famous question, “How can it happen?” Break big events down into small events; in

the above case the event X1 = 2 can happen if C1 and C2 or not C1 and not C2.

• But when you do break things down like this, make sure to neither expand or contract the

scope of the probability. Say you write something like

P (A) = P (B)

(2.27)

where B might be some complicated event expression such as in the right-hand side of (2.10).
Make SURE that A and B are equivalent—meaning that in every notebook line in which A
occurs, then B also occurs, and vice versa.

6The ﬁle is likely read in chunks called disk sectors, so there may be a slight pause between the uploading of

chunks. Our model here is too coarse to reﬂect such things.

2.9. SOLUTION STRATEGIES

17

• Do not write/think nonsense. For example: the expression “P(A) or P(B)” is nonsense—do
you see why? Probabilities are numbers, not boolean expressions, so “P(A) or P(B)” is like
saying, “0.2 or 0.5”—meaningless!

Similarly, say we have a random variable X. The “probability” P(X) is invalid. Say X is the
number of dots we get when we roll a single die. Then P(X) would mean “the probability that
the number of dots,” which is nonsense English! P(X = 3) is valid, but P(X) is meaningless.

Please note that = is not like a comma, or equivalent to the English word therefore. It needs
a left side and a right side; “a = b” makes sense, but “= b” doesn’t.

• Similarly, don’t use “formulas” that you didn’t learn and that are in fact false. For example,
in an expression involving a random variable X, one can NOT replace X by its mean. (How
would you like it if your professor were to lose your exam, and then tell you, “Well, I’ll just
assign you a score that is equal to the class mean”?)

• Adhere to convention! Use capital letters for random variables and names of events. Use P()

notation, not p() (which will mean something else in this book).

• In the beginning of your learning probability methods, meticulously write down all your steps,
with reasons, as in the computation of P (X1 = 2) in Equations (2.10)ﬀ. After you gain more
experience, you can start skipping steps, but not in the initial learning period.

• Solving probability problems—and even more so, building useful probability models—is like

computer programming: It’s a creative process.

One can NOT—repeat, NOT—teach someone how to write programs. All one can do is show
the person how the basic building blocks work, such as loops, if-else and arrays, then show a
number of examples. But the actual writing of a program is a creative act, not formula-based.
The programmer must creatively combined the various building blocks to produce the desired
result. The teacher cannot teach the student how to do this.

The same is true for solving probability problems. The basic building blocks were presented
above in Section 2.5, and many more “mailing tubes” will be presented in the rest of this
book. But it is up to the student to try using the various building blocks in a way that solves
the problem. Sometimes use of one block may prove to be unfruitful, in which case one must
try other blocks.

For instance, in using probability formulas like P(A and B) = P(A) P(B|A), there is no magic
rule as to how to choose A and B.

Moreover, if you need P(B|A), there is no magic rule on how to ﬁnd it. On the one hand,
you might calculate it from (2.8), as we did in (2.18), but on the other hand you may be able
to reason out the value of P(B|A), as we did following (2.16). Just try some cases until you
ﬁnd one that works, in the sense that you can evaluate both factors. It’s the same as trying
various programming ideas until you ﬁnd one that works.

18

CHAPTER 2. BASIC PROBABILITY MODELS

2.10 Example: Divisibility of Random Integers

Suppose at step i we generate a random integer between 1 and 1000, and check whether it’s evenly
divisible by i, i = 5,4,3,2,1. Let N denote the number of steps needed to reach an evenly divisible
number.

Let’s ﬁnd P(N = 2). Let q(i) denote the fraction of numbers in 1...,1000 that are evenly divisible by
i, so that for instance q(5) = 200/1000 = 1/5 while q(3) = 333/1000. Let’s label the steps 5,4,...,
so that the ﬁrst step is number 5. Then since the random numbers are independent from step to
step, we have

P (N = 2) = P (fail in step 5 and succeed in step 4)

(“How can it happen?”)

(2.28)

= P (fail in step 5) P(succeed in step 4 | fail in step 5)

((2.7))

= [1 − q(5)]q(4)
1
4

=

·

4
5
1
5

=

But there’s more.

(2.29)

(2.30)

(2.31)

(2.32)

First, note that q(i) is either equal or approximately equal to 1/i. Then following the derivation in
(2.28), you’ll ﬁnd that

P (N = j) ≈

1
5

(2.33)

for ALL j in 1,...,5.

That may seem counterintuitive. Yet the example here is in essence the same as one found as an
exercise in many textbooks on probability:

A man has ﬁve keys. He knows one of them opens a given lock, but he doesn’t know
which. So he tries the keys one at a time until he ﬁnds the right one. Find P(N = j), j
= 1,...,5, where N is the number of keys he tries until he succeeds.

Here too the answer is 1/5 for all j. But this one makes intuitive sense: Each of the keys has chance
1/5 of being the right key, so each of the values 1,...,5 is equally likely for N.

This is then an example of the fact that sometimes we can gain insight into one problem by
considering a mathematically equivalent problem in a quite diﬀerent setting.

2.11. EXAMPLE: A SIMPLE BOARD GAME

19

2.11 Example: A Simple Board Game

Consider a board game, which for simplicity we’ll assume consists of two squares per side, on four
sides. A player’s token advances around the board. The squares are numbered 0-7, and play begins
at square 0.

A token advances according to the roll of a single die. If a player lands on square 3, he/she gets
a bonus turn. Let’s ﬁnd the probability that a player has yet to make a complete circuit of the
board—i.e. has not yet reached or passed 0—after the ﬁrst turn (including the bonus, if any). Let
R denote his ﬁrst roll, and let B be his bonus if there is one, with B being set to 0 if there is no
bonus. Then (using commas as a shorthand notation for and)

P (doesn’t reach or pass 0) = P (R + B ≤ 7)

= P (R ≤ 6, R (cid:54)= 3 or R = 3, B ≤ 4)

= P (R ≤ 6, R (cid:54)= 3) + P (R = 3, B ≤ 4)

= P (R ≤ 6, R (cid:54)= 3) + P (R = 3) P (B ≤ 4)

1
6

·

4
6

=

=

+

5
6
17
18

Now, here’s a shorter way (there are always multiple ways to do a problem):

P (don’t reach or pass 0) = 1 − P (do reach or pass 0)

= 1 − P (R + B > 7)

= 1 − P (R = 3, B > 4)
2
6

= 1 −

1
6

·

=

17
18

(2.34)

(2.35)

(2.36)

(2.37)

(2.38)

(2.39)

(2.40)

(2.41)

(2.42)

(2.43)

(2.44)

Now suppose that, according to a telephone report of the game, you hear that on the player’s ﬁrst
turn, his token ended up at square 4. Let’s ﬁnd the probability that he got there with the aid of a
bonus roll.

Note that this a conditional probability—we’re ﬁnding the probability that the player got
a bonus roll, given that we know he ended up at square 4. The word given wasn’t there in the
statement of the problem, but it was implied.

20

CHAPTER 2. BASIC PROBABILITY MODELS

A little thought reveals that we cannot end up at square 4 after making a complete circuit of the
board, which simpliﬁes the situation quite a bit. So, write

P (B > 0 | R + B = 4) =

P (R + B = 4, B > 0)
P (R + B = 4)

=

=

=

=

=

P (R + B = 4, B > 0)
P (R + B = 4, B > 0 or R + B = 4, B = 0)

P (R + B = 4, B > 0)
P (R + B = 4, B > 0) + P (R + B = 4, B = 0)

P (R = 3, B = 1)
P (R = 3, B = 1) + P (R = 4)

1

6

6 · 1
6 + 1

6

1

6 · 1
1
7

(2.45)

(2.46)

(2.47)

(2.48)

(2.49)

(2.50)

We could have used Bayes’ Rule to shorten the derivation a little here, but will prefer to derive
everything, at least in this introductory chapter.

Pay special attention to that third equality above, as it is a frequent mode of attack in probability
problems. In considering the probability P(R+B = 4, B > 0), we ask, what is a simpler—but still
equivalent!—description of this event? Well, we see that R+B = 4, B > 0 boils down to R = 3, B
= 1, so we replace the above probability with P(R = 3, B = 1).

Again, this is a very common approach. But be sure to take care that we are in an “if and only if”
situation. Yes, R+B = 4, B > 0 implies R = 3, B = 1, but we must make sure that the converse
is true as well. In other words, we must also conﬁrm that R = 3, B = 1 implies R+B = 4, B > 0.
That’s trivial in this case, but one can make a subtle error in some problems if one is not careful;
otherwise we will have replaced a higher-probability event by a lower-probability one.

2.12 Example: Bus Ridership

Consider the following analysis of bus ridership. (In order to keep things easy, it will be quite
oversimpliﬁed, but the principles will be clear.) Here is the model:

• At each stop, each passsenger alights from the bus, independently, with probability 0.2 each.

• Either 0, 1 or 2 new passengers get on the bus, with probabilities 0.5, 0.4 and 0.1, respectively.

Passengers at successive stops are independent.

2.12. EXAMPLE: BUS RIDERSHIP

21

• Assume the bus is so large that it never becomes full, so the new passengers can always get

on.

• Suppose the bus is empty when it arrives at its ﬁrst stop.

Let Li denote the number of passengers on the bus as it leaves its ith stop, i = 1,2,3,... Let Bi
denote the number of new passengers who board the bus at the ith stop.

First, let’s ﬁnd the probability that no passengers board the bus at the ﬁrst three stops. That’s
easy:

P (B1 = 0 and B2 = 0 and B3 = 0) = 0.53

(2.51)

Now let’s compute P (L2 = 0).

P (L2 = 0) = P (B1 = 0 and L2 = 0 or B1 = 1 and L2 = 0 or B1 = 2 and L2 = 0) (2.52)

=

=

2
(cid:88)

i=0
2
(cid:88)

i=0

P (B1 = i and L2 = 0)

P (B1 = i)P (L2 = 0|B1 = i)

= 0.52 + (0.4)(0.2)(0.5) + (0.1)(0.22)(0.5)
= 0.292

(2.53)

(2.54)

(2.55)

(2.56)

For instance, where did that ﬁrst term, 0.52, come from? Well, P (B1 = 0) = 0.5, and what about
P (L2 = 0|B1 = 0)? If B1 = 0, then the bus approaches the second stop empty. For it to then
leave that second stop empty, it must be the case that B2 = 0, which has probability 0.5. In other
words, P (L2 = 0|B1 = 0) = 0.5.

As another example, suppose we are told that the bus arrives empty at the third stop. What is the
probability that exactly two people boarded the bus at the ﬁrst stop?

Note ﬁrst what is being asked for here: P (B1 = 2|L2 = 0). Then we have

P (B1 = 2 | L2 = 0) =

P (B1 = 2 and L2 = 0)
P (L2 = 0)

= P (B1 = 2) P (L2 = 0 | B1 = 2) / 0.292
= 0.1 · 0.22 · 0.5/0.292

(2.57)

(2.58)

(2.59)

22

CHAPTER 2. BASIC PROBABILITY MODELS

(the 0.292 had been previously calculated in (2.56)).

Now let’s ﬁnd the probability that fewer people board at the second stop than at the ﬁrst:

P (B2 < B1) = P (B1 = 1 and B2 < B1 or B1 = 2 and B2 < B1)

= 0.4 · 0.5 + 0.1 · (0.5 + 0.4)

(2.60)

(2.61)

Also: Someone tells you that as she got oﬀ the bus at the second stop, she saw that the bus then
left that stop empty. Let’s ﬁnd the probability that she was the only passenger when the bus left
the ﬁrst stop:

We are given that L2 = 0. But we are also given that L1 > 0. Then

P (L1 = 1|L2 = 0 and L1 > 0) =

P (L1 = 1 and L2 = 0)
P (L2 = 0 and L1 > 0)

=

=

P (B1 = 1 and L2 = 0)
P (B1 = 1 and L2 = 0 or B1 = 2 and L2 = 0)

(0.4)(0.2)(0.5)
(0.4)(0.2)(0.5) + (0.1)(0.2)2(0.5)

(2.62)

(2.63)

(2.64)

Equation (2.63) requires some explanation. Let’s ﬁrst consider how we got the numerator from the
preceding equation.

Ask the usual question: How can it happen? In this case, how can the event

L1 = 1 and L2 = 0

(2.65)

occur? Since we know a lot about the probabilistic behavior of the Bi, let’s try to recast that event.
A little thought shows that the event is equivalent to the event

B1 = 0 and L2 = 0

(2.66)

So, how did the denominator in (2.63) come from the preceding equation? In other words, how did
we recast the event

L2 = 0 and L1 > 0

(2.67)

2.13. RANDOM GRAPH MODELS

23

in terms of the Bi? Well, L1 > 0 means that B1 is either 1 or 2. Thus we broke things down
accordingly in the denominator of (2.63).

The remainder of the computation is similar to what we did earlier in this example.

2.13 Random Graph Models

A graph consists of vertices and edges. To understand this, think of a social network. Here the
vertices represent people and the edges represent friendships. For the time being, assume that
friendship relations are mutual, i.e. if person i says he is friends with person j, then j will say the
same about i.

For any graph, its adjacency matrix consists of 1 and 0 entries, with a 1 in row i, column j meaning
there is an edge from vertex i to vertex j. For instance, say we have a simple tiny network of three
people, with adjacency matrix





0 1 1
1 0 0
1 0 0





(2.68)

Row 1 of the matrix says that Person 1 is friends with persons 2 and 3, but we see from the other
rows that Persons 2 and 3 are not friends with each other.

In any graph, the degree of a vertex is its number of edges. So, the degree of vertex i is the number
of 1s in row i. In the little model above, vertex 1 has degree 2 but the other two vertices each have
degree 1.

The assumption that friendships are mutual is described in graph theory as having a undirected
graph. Note that that implies that the adjacency matrix is symmetric. However, we might model
some other networks as directed, with adjacency matrices that are not necessarily symmetric. In a
large extended family, for example, we could deﬁne edges in terms of being an elder sibling; there
would be an edge from Person i to Person j if j is an older sibling of i.

Graphs need not represent people. They are used in myriad other settings, such as analysis of Web
site relations, Internet traﬃc routing, genetics research and so on.

2.13.1 Example: Preferential Attachment Graph Model

A famous graph model is Preferential Attachment. Think of it again as an undirected social network,
with each edge representing a “friend” relation. The number of vertices grows over time, one vertex
per time step. At time 0, we have just two vertices, v1 and v2, with a link between them.

24

CHAPTER 2. BASIC PROBABILITY MODELS

Thus at time 0, each of the two vertices has degree 1. Whenever a new vertex is added to the
graph, it randomly chooses an existing vertex to attach to, creating a new edge with that existing
vertex. In making that random choice, it follows probabilities in proportion to the degrees of the
existing edges.

As an example of how the Preferential Attachment Model works, suppose that at time 2, when v4
is added, the adjacency matrix for the graph is (2.68). Then there will be an edge created between
v4 with v1, v2 or v3, with probability 2/4, 1/4 and 1/4, respectively. Let’s ﬁnd P (v4 attaches to v1)

Let Ni denote the node that vi attaches to, i = 3,4,... Then, following the solution strategy “break
big event down into small events,” let’s break this question about v4 according to what happens
with v3:

P (N4 = 1) = P (N3 = 1 and N4 = 1) + P (N3 = 2 and N4 = 1)

= (1/2)(2/4) + (1/2)(1/4)

= 3/8

(2.69)

(2.70)

(2.71)

2.14 Simulation

To simulate whether a simple event occurs or not, we typically use R function runif(). This
function generates random numbers from the interval (0,1), with all the points inside being equally
likely. So for instance the probability that the function returns a value in (0,0.5) is 0.5. Thus here
is code to simulate tossing a coin:

if (runif(1) < 0.5) heads <- TRUE else heads <- FALSE

The argument 1 means we wish to generate just one random number from the interval (0,1).

2.14.1 Example: Rolling Dice

If we roll three dice, what is the probability that their total is 8? We count all the possibilities, or
we could get an approximate answer via simulation:

1

2

3

4

5

6

# roll d dice; find P(total = k)

probtotk <- function(d,k,nreps) {

count <- 0
# do the experiment nreps times
for (rep in 1:nreps) {

2.14. SIMULATION

25

sum <- 0
# roll d dice and find their sum
for (j in 1:d) sum <- sum + roll()
if (sum == k) count <- count + 1

}
return(count/nreps)

}

# simulate roll of one die; the possible return values are 1,2,3,4,5,6,
# all equally likely
roll <- function() return(sample(1:6,1))

# example
probtotk(3,8,1000)

The call to the built-in R function sample() here says to take a sample of size 1 from the sequence
of numbers 1,2,3,4,5,6. That’s just what we want to simulate the rolling of a die. The code

for (j in 1:d) sum <- sum + roll()

then simulates the tossing of a die d times, and computing the sum.

2.14.2 First Improvement

Since applications of R often use large amounts of computer time, good R programmers are always
looking for ways to speed things up. Here is an alternate version of the above program:

# roll d dice; find P(total = k)

probtotk <- function(d,k,nreps) {

count <- 0
# do the experiment nreps times
for (rep in 1:nreps)

7

8

9

10

11

12

13

14

15

16

17

18

19

20

1

2

3

4

5

6

7

8

9

total <- sum(sample(1:6,d,replace=TRUE))
if (total == k) count <- count + 1

}
return(count/nreps)

10

11

}

Let’s ﬁrst discuss the code.

sample(1:6,d,replace=TRUE)

The call to sample() here says, “Generate d random numbers, chosen randomly (i.e. with equal
probability) from the integers 1 through 6, with replacement.” Well, of course, that simulates

26

CHAPTER 2. BASIC PROBABILITY MODELS

tossing the die d times. So, that call returns a d-element array, and we then call R’s built-in
function sum() to ﬁnd the total of the d dice.

Note the call to R’s sum() function, a nice convenience.

This second version of the code here eliminates one explicit loop, which is the key to writing fast
code in R. But just as important, it is more compact and clearer in expressing what we are doing
in this simulation.

2.14.3 Second Improvement

Further improvements are possible. Consider this code:

# roll d dice; find P(total = k)

# simulate roll of nd dice; the possible return values are 1,2,3,4,5,6,
# all equally likely
roll <- function(nd) return(sample(1:6,nd,replace=TRUE))

probtotk <- function(d,k,nreps) {
sums <- vector(length=nreps)
# do the experiment nreps times
for (rep in 1:nreps) sums[rep] <- sum(roll(d))
return(mean(sums==k))

}

There is quite a bit going on here.

1

2

3

4

5

6

7

8

9

10

11

12

We are storing the various “notebook lines” in a vector sums. We ﬁrst call vector() to allocate
space for it.

But the heart of the above code is the expression sums==k, which involves the very essence of
the R idiom, vectorization. At ﬁrst, the expression looks odd, in that we are comparing a vector
(remember, this is what languages like C call an array), sums, to a scalar, k. But in R, every
“scalar” is actually considered a one-element vector.

Fine, k is a vector, but wait! It has a diﬀerent length than sums, so how can we compare the two
vectors? Well, in R a vector is recycled—extended in length, by repeating its values—in order to
conform to longer vectors it will be involved with. For instance:

> c(2,5) + 4:6
[1] 6 10 8

2.14. SIMULATION

27

Here we added the vector (2,5) to (4,5,6). The former was ﬁrst recycled to (2,5,2), resulting in a
sum of (6,10,8).7

So, in evaluating the expression sums==k, R will recycle k to a vector consisting of nreps copies
of k, thus conforming to the length of sums. The result of the comparison will then be a vector
of length nreps, consisting of TRUE and FALSE values. In numerical contexts, these are treated
at 1s and 0s, respectively. R’s mean() function will then average those values, resulting in the
fraction of 1s! That’s exactly what we want.

Even better:

1

2

3

4

5

6

7

roll <- function(nd) return(sample(1:6,nd,replace=TRUE))

probtotk <- function(d,k,nreps) {

# do the experiment nreps times
sums <- replicate(nreps,sum(roll(d)))
return(mean(sums==k))

}

R’s replicate() function does what its name implies, in this case executing the call sum(roll(d)).
That produces a vector, which we then assign to sums. And note that we don’t have to allocate
space for sums; replicate() produces a vector, allocating space, and then we merely point sums
to that vector.

The various improvements shown above compactify the code, and in many cases, make it much
faster.8 Note, though, that this comes at the expense of using more memory.

2.14.3.1 Simulation of Conditional Probability in Dice Problem

Suppose three fair dice are rolled. We wish to ﬁnd the approximate probability that the ﬁrst die
shows fewer than 3 dots, given that the total number of dots for the 3 dice is more than 8, using
simulation.

Here is the code:

1 d i c e s i m <− function ( n r e p s ) {
2
3
4
5

count1 <− 0
count2 <− 0
fo r ( i

i n 1 : n r e p s ) {

d <− sample ( 1 : 6 , 3 , replace=T)

7There was also a warning message, not shown here. The circumstances under which warnings are or are not

generated are beyond our scope here, but recycling is a very common R operation.

8You

can measure
tem.time(probtotk(3,7,10000)).

times

using R’s

system.time()

function,

e.g.

via

the

call

sys-

28

6
7
8
9
10
11
12 }

CHAPTER 2. BASIC PROBABILITY MODELS

(sum( d ) > 8 ) {
count1 <− count1 + 1
i f

( d [ 1 ] < 3 ) count2 <− count2 + 1

i f

}

}
return ( count2 / count1 )

Note carefully that we did NOT use (2.8). That would defeat the purpose of simulation, which is
the model the actual process.

2.14.4 Simulation of the ALOHA Example

Following is a computation via simulation of the approximate values of P (X1 = 2), P (X2 = 2) and
P (X2 = 2|X1 = 1).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

# finds P(X1 = 2), P(X2 = 2) and P(X2 = 2|X1 = 1) in ALOHA example
sim <- function(p,q,nreps) {

countx2eq2 <- 0
countx1eq1 <- 0
countx1eq2 <- 0
countx2eq2givx1eq1 <- 0
# simulate nreps repetitions of the experiment
for (i in 1:nreps) {

numsend <- 0
# simulate A and B’s decision on whether to send in epoch 1
for (j in 1:2)

# no messages sent so far

if (runif(1) < p) numsend <- numsend + 1
X1 <- 1

if (numsend == 1)
else X1 <- 2
if (X1 == 2) countx1eq2 <- countx1eq2 + 1
# now simulate epoch 2
# if X1 = 1 then one node may generate a new message
numactive <- X1
if (X1 == 1 && runif(1) < q) numactive <- numactive + 1
# send?
if (numactive == 1)

if (runif(1) < p) X2 <- 0
else X2 <- 1

else {

# numactive = 2

numsend <- 0
for (i in 1:2)

if (runif(1) < p) numsend <- numsend + 1

if (numsend == 1) X2 <- 1
else X2 <- 2

}
if (X2 == 2) countx2eq2 <- countx2eq2 + 1
if (X1 == 1) { # do tally for the cond. prob.

countx1eq1 <- countx1eq1 + 1
if (X2 == 2) countx2eq2givx1eq1 <- countx2eq2givx1eq1 + 1

2.14. SIMULATION

29

}

}
# print results
cat("P(X1 = 2):",countx1eq2/nreps,"\n")
cat("P(X2 = 2):",countx2eq2/nreps,"\n")
cat("P(X2 = 2 | X1 = 1):",countx2eq2givx1eq1/countx1eq1,"\n")

35

36

37

38

39

40

41

}

Note that each of the nreps iterations of the main for loop is analogous to one line in our hy-
pothetical notebook. So, to ﬁnd (the approximate value of) P (X1 = 2), divide the count of the
number of times X1 = 2 occurred by the number of iterations.

Again, note especially that the way we calculated P (X2 = 2|X1 = 1) was to count the number of
times X2 = 2, among those times that X1 = 1, just like in the notebook case.

Also: Keep in mind that we did NOT use (2.25) or any other formula in our simulation. We
stuck to basics, the “notebook” deﬁnition of probability. This is really important if you are using
simulation to conﬁrm something you derived mathematically. On the other hand, if you are using
simulation because you CAN’T derive something mathematically (the usual situation), using some
of the mailing tubes might speed up the computation.

2.14.5 Example: Bus Ridership, cont’d.

Consider the example in Section 2.12. Let’s ﬁnd the probability that after visiting the tenth stop,
the bus is empty. This is too complicated to solve analytically, but can easily be simulated:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

nreps <- 10000
nstops <- 10
count <- 0
for (i in 1:nreps) {
passengers <- 0
for (j in 1:nstops) {

if (passengers > 0)

for (k in 1:passengers)

if (runif(1) < 0.2)

passengers <- passengers - 1

newpass <- sample(0:2,1,prob=c(0.5,0.4,0.1))
passengers <- passengers + newpass

}
if (passengers == 0) count <- count + 1

}
print(count/nreps)

Note the diﬀerent usage of the sample() function in the call

sample(0:2,1,prob=c(0.5,0.4,0.1))

30

CHAPTER 2. BASIC PROBABILITY MODELS

Here we take a sample of size 1 from the set {0,1,2}, but with probabilities 0.5 and so on. Since
the third argument for sample() is replace, not prob, we need to specify the latter in our call.

2.14.6 Back to the Board Game Example

Recall the board game in Section 2.11. Below is simulation code to ﬁnd the probability in (2.45):

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

boardsim <- function(nreps) {

count4 <- 0
countbonusgiven4 <- 0
for (i in 1:nreps) {

position <- sample(1:6,1)
if (position == 3) {
bonus <- TRUE
position <- (position + sample(1:6,1)) %% 8

} else bonus <- FALSE
if (position == 4) {

count4 <- count4 + 1
if (bonus) countbonusgiven4 <- countbonusgiven4 + 1

}

}
return(countbonusgiven4/count4)

}

2.14.7 How Long Should We Run the Simulation?

Clearly, the large the value of nreps in our examples above, the more accurate our simulation
results are likely to be. But how large should this value be? Or, more to the point, what measure is
there for the degree of accuracy one can expect (whatever that means) for a given value of nreps?
These questions will be addressed in Chapter 16.

2.15 Combinatorics-Based Probability Computation

And though the holes were rather small, they had to count them all—from the Beatles song, A Day
in the Life

In some probability problems all the outcomes are equally likely. The probability computation is
then simply a matter of counting all the outcomes of interest and dividing by the total number of
possible outcomes. Of course, sometimes even such counting can be challenging, but it is simple in
principle. We’ll discuss two examples here.

2.15. COMBINATORICS-BASED PROBABILITY COMPUTATION

31

2.15.1 Which Is More Likely in Five Cards, One King or Two Hearts?

Suppose we deal a 5-card hand from a regular 52-card deck. Which is larger, P(1 king) or P(2
hearts)? Before continuing, take a moment to guess which one is more likely.

Now, here is how we can compute the probabilities. The key point is that all possible hands
are equally likely, which implies that all we need to do is count them. There are (cid:0)52
(cid:1)
5
possible hands, so this is our denominator. For P(1 king), our numerator will be the number of
hands consisting of one king and four non-kings. Since there are four kings in the deck, the number
(cid:1) ways
(cid:1) = 4. There are 48 non-kings in the deck, so there are (cid:0)48
of ways to choose one king is (cid:0)4
4
1
to choose them. Every choice of one king can be combined with every choice of four non-kings, so
the number of hands consisting of one king and four non-kings is 4 · (cid:0)48
4

(cid:1). Thus

The same reasoning gives us

P (1 king) =

4 · (cid:0)48
(cid:1)
4
(cid:1) = 0.299
(cid:0)52
5

P (2 hearts) =

(cid:0)13
2

(cid:1)
(cid:1) · (cid:0)39
3
(cid:1)
(cid:0)52
5

= 0.274

(2.72)

(2.73)

So, the 1-king hand is just slightly more likely.

Note that an unstated assumption here was that all 5-card hands are equally likely. That is a
realistic assumption, but it’s important to understand that it plays a key role here.

By the way, I used the R function choose() to evaluate these quantities, running R in interactive
mode, e.g.:

> choose(13,2) * choose(39,3) / choose(52,5)
[1] 0.2742797

R also has a very nice function combn() which will generate all the (cid:0)n
(cid:1) combinations of k things
k
chosen from n, and also will at your option call a user-speciﬁed function on each combination. This
allows you to save a lot of computational work. See the examples in R’s online documentation.

Here’s how we could do the 1-king problem via simulation:

1

2

3

4

# use simulation to find P(1 king) when deal a 5-card hand from a
# standard deck

# think of the 52 cards as being labeled 1-52, with the 4 kings having

5

6

7

8

9

10

11

12

13

14

15

32

CHAPTER 2. BASIC PROBABILITY MODELS

# numbers 1-4

sim <- function(nreps) {

count1king <- 0 # count of number of hands with 1 king
for (rep in 1:nreps) {

hand <- sample(1:52,5,replace=FALSE)
kings <- intersect(1:4,hand)
if (length(kings) == 1) count1king <- count1king + 1

# deal hand

# find which kings, if any, are in hand

}
print(count1king/nreps)

}

Here the intersect() function performs set intersection, in this case the set 1,2,3,4 and the one in
the variable hand. Applying the length() function then gets us number of kings.

2.15.2 Example: Random Groups of Students

A class has 68 students, 48 of which are CS majors. The 68 students will be randomly assigned to
groups of 4. Find the probability that a random group of 4 has exactly 2 CS majors.

(cid:1)
(cid:1) (cid:0)20
(cid:0)48
2
2
(cid:1)
(cid:0)68
4

2.15.3 Example: Lottery Tickets

Twenty tickets are sold in a lottery, numbered 1 to 20, inclusive. Five tickets are drawn for prizes.
Let’s ﬁnd the probability that two of the ﬁve winning tickets are even-numbered.
Since there are 10 even-numbered tickets, there are (cid:0)10
2
these lines, we ﬁnd the desired probability to be.

(cid:1) sets of two such tickets. Continuing along

(cid:0)10
(cid:1)(cid:0)10
(cid:1)
2
3
(cid:1)
(cid:0)20
5

(2.74)

Now let’s ﬁnd the probability that two of the ﬁve winning tickets are in the range 1 to 5, two are
in 6 to 10, and one is in 11 to 20.
Picture yourself picking your tickets. Again there are (cid:0)20
5
many of those ways satisfy the stated condition?
(cid:1) ways to choose two tickets from the range 1 to 5. Once you’ve done
Well, ﬁrst, there are (cid:0)5
2
(cid:1) ways to choose two tickets from the range 6 to 10, and so on. So, The desired
that, there are (cid:0)5
2

(cid:1) ways to choose the ﬁve tickets. How

2.15. COMBINATORICS-BASED PROBABILITY COMPUTATION

33

probability is then

(cid:1)
(cid:1)(cid:0)10
(cid:1)(cid:0)5
(cid:0)5
1
2
2
(cid:1)
(cid:0)20
5

(2.75)

2.15.4 “Association Rules” in Data Mining

The ﬁeld of data mining is a branch of computer science, but it is largely an application of various
statistical methods to really huge databases.

One of the applications of data mining is called the market basket problem. Here the data consists
of records of sales transactions, say of books at Amazon.com. The business’ goal is exempliﬁed
by Amazon’s suggestion to customers that “Patrons who bought this book also tended to buy
the following books.”9 The goal of the market basket problem is to sift through sales transaction
records to produce association rules, patterns in which sales of some combinations of books imply
likely sales of other related books.

The notation for association rules is A, B ⇒ C, D, E, meaning in the book sales example that
customers who bought books A and B also tended to buy books C, D and E. Here A and B are
called the antecedents of the rule, and C, D and E are called the consequents. Let’s suppose
here that we are only interested in rules with a single consequent.

We will present some methods for ﬁnding good rules in another chapter, but for now, let’s look
at how many possible rules there are. Obviously, it would be impractical to use rules with a large
number of antecedents.10. Suppose the business has a total of 20 products available for sale. What
percentage of potential rules have three or fewer antecedents?11
For each k = 1,...,19, there are (cid:0)20
k
(cid:0)20−k
1

(cid:1) possible sets of k antecedents, and for each such set there are
(cid:1) possible consequents. The fraction of potential rules using three or fewer antecedents is then

(cid:80)3

k=1

(cid:80)19

k=1

(cid:0)20
k
(cid:0)20
k

(cid:1) · (cid:0)20−k
1
(cid:1) · (cid:0)20−k
1

(cid:1)
(cid:1) =

23180
10485740

= 0.0022

(2.76)

So, this is just scratching the surface. And note that with only 20 products, there are already over
ten million possible rules. With 50 products, this number is 2.81 × 1016! Imagine what happens in

9Some customers appreciate such tips, while others view it as insulting or an invasion of privacy, but we’ll not

address such issues here.

10In addition, there are serious statistical problems that would arise, to be discussed in another chapter.
11Be sure to note that this is also a probability, namely the probability that a randomly chosen rule will have three

or fewer antecedents.

34

CHAPTER 2. BASIC PROBABILITY MODELS

a case like Amazon, with millions of products. These staggering numbers show what a tremendous
challenge data miners face.

2.15.5 Multinomial Coeﬃcients

Question: We have a group consisting of 6 Democrats, 5 Republicans and 2 Independents, who
will participate in a panel discussion. They will be sitting at a long table. How many seating
arrangements are possible, with regard to political aﬃliation? (So we do not care, for instance,
about permuting the individual Democrats within the seats assigned to Democrats.)
(cid:1) ways
(cid:1) ways to choose the Democratic seats. Once those are chosen, there are (cid:0)7
Well, there are (cid:0)13
5
6
to choose the Republican seats. The Independent seats are then already determined, i.e. there will
(cid:1). Thus the total number of seating arrangements
be only way at that point, but let’s write it as (cid:0)2
2
is

That reduces to

13!
6!7!

·

7!
5!2!

·

2!
2!0!

13!
6!5!2!

(2.77)

(2.78)

The same reasoning yields the following:

Multinomial Coeﬃcients: Suppose we have c objects and r bins. Then the number of ways to
choose c1 of them to put in bin 1, c2 of them to put in bin 2,..., and cr of them to put in bin r is

c!
c1!...cr!

,

c1 + ... + cr = c

(2.79)

Of course, the “bins” may just be metaphorical. In the political party example above, the “bins ”
were political parties, and “objects” were seats.

2.15.6 Example: Probability of Getting Four Aces in a Bridge Hand

A standard deck of 52 cards is dealt to four players, 13 cards each. One of the players is Millie.
What is the probability that Millie is dealt all four aces?

2.15. COMBINATORICS-BASED PROBABILITY COMPUTATION

35

Well, there are

52!
13!13!13!13!

(2.80)

possible deals. (the “objects” are the 52 cards, and the “bins” are the 4 players.) The number of
deals in which Millie holds all four aces is the same as the number of deals of 48 cards, 9 of which
go to Millie and 13 each to the other three players, i.e.

Thus the desired probability is

48!
13!13!13!9!

48!
13!13!13!9!
52!
13!13!13!13!

= 0.00264

(2.81)

(2.82)

Exercises

1. This problem concerns the ALOHA network model of Section 2.1. Feel free to use (but cite)
computations already in the example.

(a) P (X1 = 2 and X2 = 1), for the same values of p and q in the examples.

(b) Find P (X2 = 0).

(c) Find (P (X1 = 1|X2 = 1).

2. Urn I contains three blue marbles and three yellow ones, while Urn II contains ﬁve and seven
of these colors. We draw a marble at random from Urn I and place it in Urn II. We then draw a
marble at random from Urn II.

(a) Find P(second marble drawn is blue).

(b) Find P( ﬁrst marble drawn is blue | second marble drawn is blue).

3. Consider the example of association rules in Section 2.15.4. How many two-antecedent, two-
consequent rules are possible from 20 items? Express your answer in terms of combinatorial (“n
choose k”) symbols.

36

CHAPTER 2. BASIC PROBABILITY MODELS

4. Suppose 20% of all C++ programs have at least one major bug. Out of ﬁve programs, what is
the probability that exactly two of them have a major bug?

5. Assume the ALOHA network model as in Section 2.1, i.e. m = 2 and X0 = 2, but with general
values for p and q. Find the probability that a new message is created during epoch 2.

6. You bought three tickets in a lottery, for which 60 tickets were sold in all. There will be ﬁve
prizes given. Find the probability that you win at least one prize, and the probability that you win
exactly one prize.

7. Two ﬁve-person committees are to be formed from your group of 20 people. In order to foster
communication, we set a requirement that the two committees have the same chair but no other
overlap. Find the probability that you and your friend are both chosen for some committee.

8. Consider a device that lasts either one, two or three months, with probabilities 0.1, 0.7 and 0.2,
respectively. We carry one spare. Find the probability that we have some device still working just
before four months have elapsed.

9. A building has six ﬂoors, and is served by two freight elevators, named Mike and Ike. The
destination ﬂoor of any order of freight is equally likely to be any of ﬂoors 2 through 6. Once an
elevator reaches any of these ﬂoors, it stays there until summoned. When an order arrives to the
building, whichever elevator is currently closer to ﬂoor 1 will be summoned, with elevator Ike being
the one summoned in the case in which they are both on the same ﬂoor.

Find the probability that after the summons, elevator Mike is on ﬂoor 3. Assume that only one
order of freight can ﬁt in an elevator at a time. Also, suppose the average time between arrivals of
freight to the building is much larger than the time for an elevator to travel between the bottom
and top ﬂoors; this assumption allows us to neglect travel time.
10. Without resorting to using the fact that (cid:0)n
k

(cid:1) = n!/[k!(n − k!)], ﬁnd c and d such that

(cid:19)

(cid:18)n
k

=

(cid:19)

(cid:18)n − 1
k

+

(cid:19)

(cid:18)c
d

(2.83)

11. Consider the ALOHA example from the text, for general p and q, and suppose that X0 = 0,
i.e. there are no active nodes at the beginning of our observation period. Find P (X1 = 0).

12. Consider a three-sided die, as opposed to the standard six-sided type. The die is cylinder-
shaped, and gives equal probabilities to one, two and three dots. The game is to keep rolling the
die until we get a total of at least 3. Let N denote the number of times we roll the die. For example,
if we get a 3 on the ﬁrst roll, N = 1. If we get a 2 on the ﬁrst roll, then N will be 2 no matter what
we get the second time. The largest N can be is 3. The rule is that one wins if one’s ﬁnal total is
exactly 3.

2.15. COMBINATORICS-BASED PROBABILITY COMPUTATION

37

(a) Find the probability of winning.

(b) Find P(our ﬁrst roll was a 1 | we won).

(c) How could we construct such a die?

13. Consider the ALOHA simulation example in Section 2.14.4.

(a) Suppose we wish to ﬁnd P (X2 = 1|X1 = 1) instead of P (X2 = 2|X1 = 1). What line(s)

would we change, and how would we change them?

(b) In which line(s) are we in essence checking for a collision?

14. Jack and Jill keep rolling a four-sided and a three-sided die, respectively. The ﬁrst player to
get the face having just one dot wins, except that if they both get a 1, it’s a tie, and play continues.
Let N denote the number of turns needed. Find the following:

(a) P(N = 1), P(N = 2).

(b) P (the ﬁrst turn resulted in a tie|N = 2)

15. In the ALOHA network example in Sec. 1.1, suppose X0 = 1, i.e. we start out with just one
active node. Find P (X2 = 0), as an expression in p and q.

16. Suppose a box contains two pennies, three nickels and ﬁve dimes. During transport, two coins
fall out, unseen by the bearer. Assume each type of coin is equally likely to fall out. Find: P(at
least $0.10 worth of money is lost); P(both lost coins are of the same denomination)

17. Suppose we have the track record of a certain weather forecaster. Of the days for which he
predicts rain, a fraction c actually do have rain. Among days for which he predicts no rain, he is
correct a fraction d of the time. Among all days, he predicts rain g of the time, and predicts no
rain 1-g of the time. Find P(he predicted rain | it does rain), P(he predicts wrong) and P(it does
rain — he was wrong). Write R simulation code to verify. (Partial answer: For the case c = 0.8, d
= 0.6 and g = 0.2, P(he predicted rain | it does rain) = 1/3.)

18. The Game of Pit is really fun because there are no turns. People shout out bids at random,
chaotically. Here is a slightly simpliﬁed version of the game:

There are four suits, Wheat, Barley, Corn and Rye, with nine cards each, 36 cards in all. There
are four players. At the opening, the cards are all dealt out, nine to each player. The players hide
their cards from each other’s sight.

38

CHAPTER 2. BASIC PROBABILITY MODELS

Players then start trading. In computer science terms, trading is asynchronous, no turns; a player
can bid at any time. The only rule is that a trade must be homogeneous in suit, e.g. all Rye.
(The player trading Rye need not trade all the Rye he has, though.) The player bids by shouting
out the number she wants to trade, say “2!” If another player wants to trade two cards (again,
homogeneous in suit), she yells out, “OK, 2!” and they trade. When one player acquires all nine
of a suit, he shouts “Corner!”

Consider the situation at the time the cards have just been dealt. Imagine that you are one of the
players, and Jane is another. Find the following probabilities:

(a) P(you have no Wheats).

(b) P(you have seven Wheats).

(c) P(Jane has two Wheats — you have seven Wheats).

(d) P(you have a corner) (note: someone else might too; whoever shouts it out ﬁrst wins).

19. In the board game example in Section 2.11, suppose that the telephone report is that A ended
up at square 1 after his ﬁrst turn. Find the probability that he got a bonus.

20. Consider the bus ridership example in Section 2.12 of the text. Suppose the bus is initially
empty, and let Xn denote the number of passengers on the bus just after it has left the nth stop, n
= 1,2,... Find the following:

(a) P (X2 = 1)

(b) P(at least one person boarded the bus at the ﬁrst stop |X2 = 1)

21. Suppose committees of sizes 3, 4 and 5 are to be chosen at random from 20 people, among
whom are persons A and B. Find the probability that A and B are on the same committee.

22. Consider the ALOHA simulation in Section 28.

(a) On what line do we simulate the possible creation of a new message?

(b) Change line 10 so that it uses sample() instead of runif().

Chapter 3

Discrete Random Variables

This chapter will introduce entities called discrete random variables. Some properties will be derived
for means of such variables, with most of these properties actually holding for random variables in
general. Well, all of that seems abstract to you at this point, so let’s get started.

3.1 Random Variables

Deﬁnition 3 A random variable is a numerical outcome of our experiment.

For instance, consider our old example in which we roll two dice, with X and Y denoting the number
of dots we get on the blue and yellow dice, respectively. Then X and Y are random variables, as
they are numerical outcomes of the experiment. Moreover, X+Y, 2XY, sin(XY) and so on are also
random variables.

In a more mathematical formulation, with a formal sample space deﬁned, a random variable would
be deﬁned to be a real-valued function whose domain is the sample space.

3.2 Discrete Random Variables

In our dice example, the random variable X could take on six values in the set {1,2,3,4,5,6}. We
say that the support of X is {1,2,3,4,5,6}. This is a ﬁnite set.

In the ALOHA example, X1 and X2 each have support {0,1,2}, again a ﬁnite set.1

1We could even say that X1 takes on only values in the set {1,2}, but if we were to look at many epochs rather

than just two, it would be easier not to make an exceptional case.

39

40

CHAPTER 3. DISCRETE RANDOM VARIABLES

Now think of another experiment, in which we toss a coin until we get heads. Let N be the number
of tosses needed. Then the support of N is the set {1,2,3,...} This is a countably inﬁnite set.2

Now think of one more experiment, in which we throw a dart at the interval (0,1), and assume
that the place that is hit, R, can take on any of the values between 0 and 1. Here the support is
an uncountably inﬁnite set.

We say that X, X1, X2 and N are discrete random variables, while R is continuous. We’ll discuss
continuous random variables in a later chapter.

3.3 Independent Random Variables

We already have a deﬁnition for the independence of events; what about independence of random
variables? Here it is:

Random variables X and Y are said to be independent if for any sets I and J, the
events {X is in I} and {Y is in J} are independent, i.e. P(X is in I and Y is in J) =
P(X is in I) P(Y is in J).

Sounds innocuous, but the notion of independent random variables is absolutely central to the ﬁeld
of probability and statistics, and will pervade this entire book.

3.4 Example: The Monty Hall Problem

This is an example of how the use of random variables in “translating” a probability problem
to mathematical terms can simplify and clarify one’s thinking. Imagine, this simple device of
introducing named random variables into our analysis makes a problem that has vexed
famous mathematicians quite easy to solve!

The Monty Hall Problem, which gets its name from a popular TV game show host, involves a
contestant choosing one of three doors. Behind one door is a new automobile, while the other two
doors lead to goats. The contestant chooses a door and receives the prize behind the door.

The host knows which door leads to the car. To make things interesting, after the contestant
chooses, the host will open one of the other doors not chosen, showing that it leads to a goat.

2This is a concept from the fundamental theory of mathematics. Roughly speaking, it means that the set can
be assigned an integer labeling, i.e. item number 1, item number 2 and so on. The set of positive even numbers is
countable, as we can say 2 is item number 1, 4 is item number 2 and so on. It can be shown that even the set of all
rational numbers is countable.

3.4. EXAMPLE: THE MONTY HALL PROBLEM

41

Should the contestant now change her choice to the remaining door, i.e. the one that she didn’t
choose and the host didn’t open?

Many people answer No, reasoning that the two doors not opened yet each have probability 1/2 of
leading to the car. But the correct answer is actually that the remaining door (not chosen by the
contestant and not opened by the host) has probability 2/3, and thus the contestant should switch
to it. Let’s see why.

Let

• C = contestant’s choice of door (1, 2 or 3)

• H = host’s choice of door (1, 2 or 3)

• A = door that leads to the automobile

We can make things more concrete by considering the case C = 1, H = 2. The mathematical
formulation of the problem is then to ﬁnd

P (A = 3 | C = 1, H = 2) =

P (A = 3, C = 1, H = 2)
P (C = 1, H = 2)

(3.1)

The key point, commonly missed even by mathematically sophisticated people, is the role of the
host. Write the numerator above as

P (A = 3, C = 1) P (H = 2 | A = 3, C = 1)

Since C and A are independent random variables, the value of the ﬁrst factor in (3.2) is

1
3

·

1
3

=

1
9

(3.2)

(3.3)

What about the second factor? Remember, the host knows that A = 3, and since the contestant
has chosen door 1, the host will open the only remaining door that conceals a goat, i.e. door 2. In
other words,

On the other hand, if say A = 1, the host would randomly choose between doors 2 and 3, so that

P (H = 2 | A = 3, C = 1) = 1

(3.4)

P (H = 2 | A = 1, C = 1) =

1
2

(3.5)

42

CHAPTER 3. DISCRETE RANDOM VARIABLES

It is left to the reader to complete the analysis, calculating the denominator of (3.1), and then
showing in the end that

P (A = 3 | C = 1, H = 2) =

2
3

(3.6)

According to the “Monty Hall problem” entry in Wikipedia, even Paul Erd¨os, one of the most
famous mathematicians in history, gave the wrong answer to this problem. Presumably he would
have avoided this by writing out his analysis in terms of random variables, as above, rather than
say, a wordy, imprecise and ultimately wrong solution.

3.5 Expected Value

3.5.1 Generality—Not Just for Discrete Random Variables

The concepts and properties introduced in this section form the very core of probability and statis-
tics. Except for some speciﬁc calculations, these apply to both discrete and continuous
random variables.

The properties developed for variance, deﬁned later in this chapter, also hold for both discrete and
continuous random variables.

3.5.1.1 What Is It?

The term “expected value” is one of the many misnomers one encounters in tech circles. The
expected value is actually not something we “expect” to occur. On the contrary, it’s often pretty
unlikely.

For instance, let H denote the number of heads we get in tossing a coin 1000 times. The expected
value, you’ll see later, is 500. This is not surprising, given the symmetry of the situation, but P(H
= 500) turns out to be about 0.025. In other words, we certainly should not “expect” H to be 500.

Of course, even worse is the example of the number of dots that come up when we roll a fair die.
The expected value is 3.5, a value which not only rarely comes up, but in fact never does.

In spite of being misnamed, expected value plays an absolutely central role in probability and
statistics.

3.5. EXPECTED VALUE

3.5.2 Deﬁnition

43

Consider a repeatable experiment with random variable X. We say that the expected value of X
is the long-run average value of X, as we repeat the experiment indeﬁnitely.

In our notebook, there will be a column for X. Let Xi denote the value of X in the ith row of the
notebook. Then the long-run average of X is

lim
n→∞

X1 + ... + Xn
n

(3.7)

Suppose for instance our experiment is to toss 10 coins. Let X denote the number of heads we get
out of 10. We might get four heads in the ﬁrst repetition of the experiment, i.e. X1 = 4, seven
heads in the second repetition, so X2 = 7, and so on. Intuitively, the long-run average value of X
will be 5. (This will be proven below.) Thus we say that the expected value of X is 5, and write
E(X) = 5.

3.5.3 Existence of the Expected Value

The above deﬁntion puts the cart before the horse, as it presumes that the limit exists. Theoretically
speaking, this might not be the case. However, it does exist if the Xi have ﬁnite lower and upper
bounds, which is always true in the real world. For instance, no person has height of 50 feet, say,
and no one has negative height either.

For the remainder of this book, we will usually speak of “the” expected value of a random variable
without adding the qualiﬁer “if it exists.”

3.5.4 Computation and Properties of Expected Value

Continuing the coin toss example above, let Kin be the number of times the value i occurs among
X1, ..., Xn, i = 0,...,10, n = 1,2,3,... For instance, K4,20 is the number of times we get four heads,
in the ﬁrst 20 repetitions of our experiment. Then

E(X) = lim
n→∞

X1 + ... + Xn
n

0 · K0n + 1 · K1n + 2 · K2n... + 10 · K10,n
n

= lim
n→∞
10
(cid:88)

=

i=0

i · lim
n→∞

Kin
n

(3.8)

(3.9)

(3.10)

44

CHAPTER 3. DISCRETE RANDOM VARIABLES

To understand that second equation, suppose when n = 5 we have 2, 3, 1, 2 and 1 for our values
of X1, X2, X3, X4, X5,. Then we can group the 2s together and group the 1s together, and write

2 + 3 + 1 + 2 + 1 = 2 × 2 + 2 × 1 + 1 × 3

(3.11)

But limn→∞

Kin
n is the long-run fraction of the time that X = i. In other words, it’s P(X = i)! So,

E(X) =

10
(cid:88)

i=0

i · P (X = i)

So in general we have:

Property A:

The expected value of a discrete random variable X which takes values in the set A is

E(X) =

(cid:88)

c∈A

c P (X = c)

(3.12)

(3.13)

Note that (3.13) is the formula we’ll use. The preceding equations were derivation, to motivate the
formula. Note too that (3.13) is not the deﬁnition of expected value; that was in 3.7. It is quite
important to distinguish between all of these, in terms of goals.3

By the way, note the word discrete above. For the case of continuous random variables, the sum
in (3.13) will become an integral.

It will be shown in Section 4.3 that in our example above in which X is the number of heads we
get in 10 tosses of a coin,

P (X = i) =

(cid:19)

(cid:18)10
i

0.5i(1 − 0.5)10−i

So

E(X) =

10
(cid:88)

i

i=0

(cid:19)

(cid:18)10
i

0.5i(1 − 0.5)10−i

(3.14)

(3.15)

It turns out that E(X) = 5.

3The matter is made a little more confusing by the fact that many books do in fact treat (3.13) as the deﬁnition,

with (3.7) being the consequence.

3.5. EXPECTED VALUE

For X in our dice example,

E(X) =

6
(cid:88)

c=1

c ·

1
6

= 3.5

45

(3.16)

It is customary to use capital letters for random variables, e.g. X here, and lower-case letters for
values taken on by a random variable, e.g. c here. Please adhere to this convention.

By the way, it is also customary to write EX instead of E(X), whenever removal of the parentheses
does not cause any ambiguity. An example in which it would produce ambiguity is E(U 2). The
expression EU 2 might be taken to mean either E(U 2), which is what we want, or (EU )2, which is
not what we want.

For S = X+Y in the dice example,

E(S) = 2 ·

1
36

+ 3 ·

2
36

+ 4 ·

3
36

+ ...12 ·

1
36

= 7

In the case of N, tossing a coin until we get a head:

E(N ) =

∞
(cid:88)

c=1

c ·

1
2c = 2

(3.17)

(3.18)

(We will not go into the details here concerning how the sum of this particular inﬁnite series is
computed. See Section 4.2.)

Some people like to think of E(X) using a center of gravity analogy. Forget that analogy! Think
notebook! Intuitively, E(X) is the long-run average value of X among all the lines of
the notebook. So for instance in our dice example, E(X) = 3.5, where X was the number of dots
on the blue die, means that if we do the experiment thousands of times, with thousands of lines in
our notebook, the average value of X in those lines will be about 3.5. With S = X+Y, E(S) = 7.
This means that in the long-run average in column S in Table 3.1 is 7.

Of course, by symmetry, E(Y) will be 3.5 too, where Y is the number of dots showing on the
yellow die. That means we wasted our time calculating in Equation (3.17); we should have realized
beforehand that E(S) is 2 × 3.5 = 7.

In other words:

Property B:

For any random variables U and V, the expected value of a new random variable D = U+V is the

46

CHAPTER 3. DISCRETE RANDOM VARIABLES

notebook line
1
2
3
4
5
6
7
8
9

outcome
blue 2, yellow 6 No
blue 3, yellow 1 No
blue 1, yellow 1 No
blue 4, yellow 2 Yes
blue 1, yellow 1 No
blue 3, yellow 4 No
blue 5, yellow 1 Yes
blue 3, yellow 6 No
blue 2, yellow 5 No

blue+yellow = 6? S
8
4
2
6
2
7
6
9
7

Table 3.1: Expanded Notebook for the Dice Problem

sum of the expected values of U and V:

E(U + V ) = E(U ) + E(V )

(3.19)

Note carefully that U and V do NOT need to be independent random variables for this relation
to hold. You should convince yourself of this fact intuitively by thinking about the notebook
notion. Say we look at 10000 lines of the notebook, which has columns for the values of U, V and
U+V. It makes no diﬀerence whether we average U+V in that column, or average U and V in their
columns and then add—either way, we’ll get the same result.

While you are at it, use the notebook notion to convince yourself of the following:

Properties C:

• For any random variable U and constant a, then

• For random variables X and Y—not necessarily independent—and constants a and b, we have

E(aU ) = aEU

(3.20)

E(aX + bY ) = aEX + bEY

(3.21)

This follows by taking U = aX and V = bY in (3.19), and then using (3.20).

3.5. EXPECTED VALUE

47

By induction, for constants a1, ..., ak and random variables X1, ..., Xk, form the new random
variable a1X1 + ... + akXk. Then

E(a1X1 + ... + anXk) = a1EX1 + ... + anEXk

(3.22)

• For any constant b, we have

E(b) = b

(3.23)

For instance, say U is temperature in Celsius. Then the temperature in Fahrenheit is W = 9
5 U +32.
So, W is a new random variable, and we can get its expected value from that of U by using (3.21)
with a = 9

5 and b = 32.

If you combine (3.23) with (3.21), we have an important special case:

E(aX + b) = aEX + b

(3.24)

Another important point:

Property D: If U and V are independent, then

E(U V ) = EU · EV

(3.25)

In the dice example, for instance, let D denote the product of the numbers of blue dots and yellow
dots, i.e. D = XY. Then

E(D) = 3.52 = 12.25

(3.26)

Equation (3.25) doesn’t have an easy “notebook proof.” It is proved in Section 13.3.1.

Consider a function g() of one variable, and let W = g(X). W is then a random variable too. Say
X has support A, as in (3.13). Then W has support B = {g(c) : c(cid:15)A}. (

For instance, say g() is the squaring function, and X takes on the values -1, 0 and 1, with probability
0.5, 0.4 and 0.1. Then

A = {−1, 0, 1}

(3.27)

CHAPTER 3. DISCRETE RANDOM VARIABLES

48

and

Deﬁne

B = {0, 1}

Ad = {c : c ∈ A, g(c) = d}

In our above squaring example, we will have

Then

so

A0 = {0}, A1 = {−1, 1}

P (W = d) = P (X ∈ Ad)

E[g(X)] = E(W )

=

=

=

(cid:88)

d∈B
(cid:88)

d∈B
(cid:88)

c∈A

dP (W = d)

(cid:88)

d

c∈Ad

P (X = c)

g(c)P (X = c)

(3.28)

(3.29)

(3.30)

(3.31)

(3.32)

(3.33)

(3.34)

(3.35)

(Going from the next-to-last equation here to the last one is rather tricky. Work through for the
case of our squaring function example above in order to see why the ﬁnal equation does follow.

Property E:

If E[g(X)] exists, then

E[g(X)] =

(cid:88)

c∈A

g(c) · P (X = c)

(3.36)

where the sum ranges over all values c that can be taken on by X.

3.5. EXPECTED VALUE

49

X), where X is the
For example, suppose for some odd reason we are interested in ﬁnding E(
number of dots we get when we roll one die. Let W =
X). Then W is another random variable,
and is discrete, since it takes on only a ﬁnite number of values. (The fact that most of the values
are not integers is irrelevant.) We want to ﬁnd EW .

√

√

Well, W is a function of X, with g(t) =
support of W, i.e.
1,
1
6 . Substituting into (3.36), we ﬁnd that

2, ...,

√

√

√

√

t. So, (3.36) tells us to make a list of values in the
6, and a list of the corresponding probabilities for X, which are all

√

E(

X) =

1
6

6
(cid:88)

√
i

i=1

(3.37)

What about a function of several variables? Say for instance you are ﬁnding E(UV), where U has
support, say, 1,2 and V has support 5,12,13. In order to ﬁnd E(UV), you need to know the support
of UV, recognizing that it, the product UV, is a new random variable in its own right. Let’s call it
W. Then in this little example, W has support 5,12,13,10,24,26. Then compute

5P (W = 5) + 12P (W = 12) + ... = 5P (U = 1, V = 5) + 12P (U = 1, V = 12) + ...

Note: Equation (3.36) will be one of the most heavily used formulas in this book. Make sure you
keep it in mind.

3.5.5 “Mailing Tubes”

The properties of expected value discussed above are key to the entire remainder of
this book. You should notice immediately when you are in a setting in which they
are applicable. For instance, if you see the expected value of the sum of two random
variables, you should instinctively think of (3.19) right away.

As discussed in Section 2.4, these properties are “mailing tubes.” For instance, (3.19) is a “mailing
tube”–make a mental note to yourself saying, “If I ever need to ﬁnd the expected value of the sum
of two random variables, I can use (3.19).” Similarly, (3.36) is a mailing tube; tell yourself, “If I
ever see a new random variable that is a function of one whose probabilities I already know, I can
ﬁnd the expected value of the new random variable using (3.36).”

You will encounter “mailing tubes” throughout this book. For instance, (3.49) below is a very
important “mailing tube.” Constantly remind yourself—“Remember the ‘mailing tubes’ !”

50

CHAPTER 3. DISCRETE RANDOM VARIABLES

3.5.6 Casinos, Insurance Companies and “Sum Users,” Compared to Others

The expected value is intended as a measure of central tendency (also called a measure
of location, i.e. as some sort of deﬁnition of the probablistic “middle” in the range of a random
variable. There are various other such measures one can use, such as the median, the halfway point
of a distribution, and today they are recognized as being superior to the mean in certain senses. For
historical reasons, the mean plays an absolutely central role in probability and statistics. Yet one
should understand its limitations. (This discussion will be general, not limited to discrete random
variables.)

(Warning: The concept of the mean is likely so ingrained in your consciousness that you simply
take it for granted that you know what the mean means, no pun intended. But try to take a step
back, and think of the mean afresh in what follows.)

First, the term expected value itself is a misnomer. We do not expect the number of dots D to be
3.5 in the die example in Section 3.5.1.1; in fact, it is impossible for W to take on that value.

Second, the expected value is what we call the mean in everyday life. And the mean is terribly
overused. Consider, for example, an attempt to describe how wealthy (or not) people are in the
city of Davis. If suddenly Bill Gates were to move into town, that would skew the value of the
mean beyond recognition.

But even without Gates, there is a question as to whether the mean has that much meaning. After
all, what is so meaningful about summing our data and dividing by the number of data points?
The median has an easy intuitive meaning, but although the mean has familiarity, one would be
hard pressed to justify it as a measure of central tendency.

What, for example, does Equation (3.7) mean in the context of people’s heights in Davis? We
would sample a person at random and record his/her height as X1. Then we’d sample another
person, to get X2, and so on. Fine, but in that context, what would (3.7) mean? The answer is,
not much. So the signiﬁcance of the mean height of people in Davis would be hard to explain.

For a casino, though, (3.7) means plenty. Say X is the amount a gambler wins on a play of a
roulette wheel, and suppose (3.7) is equal to $1.88. Then after, say, 1000 plays of the wheel (not
necessarily by the same gambler), the casino knows from 3.7 it will have paid out a total of about
$1,880. So if the casino charges, say $1.95 per play, it will have made a proﬁt of about $70 over
those 1000 plays. It might be a bit more or less than that amount, but the casino can be pretty
sure that it will be around $70, and they can plan their business accordingly.

The same principle holds for insurance companies, concerning how much they pay out in claims.
With a large number of customers, they know (“expect”!) approximately how much they will pay
out, and thus can set their premiums accordingly. Here the mean has a tangible, practical meaning.

The key point in the casino and insurance companies examples is that they are interested in totals,

3.6. VARIANCE

51

such as total payouts on a blackjack table over a month’s time, or total insurance claims paid in
a year. Another example might be the number of defectives in a batch of computer chips; the
manufacturer is interested in the total number of defectives chips produced, say in a month. Since
the mean is by deﬁnition a total (divided by the number of data points), the mean will be of direct
interest to casinos etc.

By contrast, in describing how wealthy people of a town are, the total height of all the residents is
not relevant. Similarly, in describing how well students did on an exam, the sum of the scores of all
the students doesn’t tell us much. (Unless the professor gets $10 for each point in the exam scores
of each of the students!) A better description for heights and exam scores might be the median
height or score.

Nevertheless, the mean has certain mathematical properties, such as (3.19), that have allowed the
rich development of the ﬁelds of probability and statistics over the years. The median, by contrast,
does not have nice mathematical properties. In many cases, the mean won’t be too diﬀerent from
the median anyway (barring Bill Gates moving into town), so you might think of the mean as a
convenient substitute for the median. The mean has become entrenched in statistics, and we will
use it often.

3.6 Variance

As in Section 3.5, the concepts and properties introduced in this section form the very core of
probability and statistics. Except for some speciﬁc calculations, these apply to both
discrete and continuous random variables.

3.6.1 Deﬁnition

While the expected value tells us the average value a random variable takes on, we also need
a measure of the random variable’s variability—how much does it wander from one line of the
notebook to another? In other words, we want a measure of dispersion. The classical measure is
variance, deﬁned to be the mean squared diﬀerence between a random variable and its mean:

Deﬁnition 4 For a random variable U for which the expected values written below exist, the vari-
ance of U is deﬁned to be

V ar(U ) = E[(U − EU )2]

(3.38)

52

CHAPTER 3. DISCRETE RANDOM VARIABLES

For X in the die example, this would be

V ar(X) = E[(X − 3.5)2]

(3.39)

Remember what this means: We have a random variable X, and we’re creating a new random
variable, W = (X − 3.5)2, which is a function of the old one. We are then ﬁnding the expected
value of that new random variable W.

In the notebook view, E[(X − 3.5)2] is the long-run average of the W column:

line X W
2.25
2
2.25
5
6.25
6
0.25
3
2.25
5
6.25
1

1
2
3
4
5
6

To evaluate this, apply (3.36) with g(c) = (c − 3.5)2:

V ar(X) =

6
(cid:88)

(c − 3.5)2 ·

c=1

1
6

= 2.92

(3.40)

You can see that variance does indeed give us a measure of dispersion. In the expression V ar(U ) =
E[(U − EU )2], if the values of U are mostly clustered near its mean, then (U − EU )2 will usually
be small, and thus the variance of U will be small; if there is wide variation in U, the variance will
be large.

Property F:

V ar(U ) = E(U 2) − (EU )2

(3.41)

The term E(U 2) is again evaluated using (3.36).

Thus for example, if X is the number of dots which come up when we roll a die. Then, from (3.41),

V ar(X) = E(X 2) − (EX)2

(3.42)

3.6. VARIANCE

Let’s ﬁnd that ﬁrst term (we already know the second is 3.52). From (3.36),

E(X 2) =

6
(cid:88)

i=1

i2 ·

1
6

=

91
6

53

(3.43)

Thus V ar(X) = E(X 2) − (EX)2 = 91

6 − 3.52

Remember, though, that (3.41) is a shortcut formula for ﬁnding the variance, not the deﬁnition of
variance.

Below is the derivation of (3.41). Keep in mind that EU is a constant.

V ar(U ) = E[(U − EU )2]

= E[U 2 − 2EU · U + (EU )2] (algebra)
= E(U 2) + E(−2EU · U ) + E[(EU )2] (3.19)
= E(U 2) − 2EU · EU + (EU )2 (3.20), (3.23)
= E(U 2) − (EU )2

An important behavior of variance is:

Property G:

V ar(cU ) = c2V ar(U )

(3.44)

(3.45)

(3.46)

(3.47)

(3.48)

(3.49)

for any random variable U and constant c. It should make sense to you: If we multiply a random
variable by 5, say, then its average squared distance to its mean should increase by a factor of 25.

Let’s prove (3.49). Deﬁne V = cU. Then

V ar(V ) = E[(V − EV )2] (def.)

= E{[cU − E(cU )]2} (subst.)
= E{[cU − cEU ]2} ((3.21))
= E{c2[U − EU ]2} (algebra)
= c2E{[U − EU ]2} ((3.21))
= c2V ar(U ) (def.)

(3.50)

(3.51)

(3.52)

(3.53)

(3.54)

(3.55)

Shifting data over by a constant does not change the amount of variation in them:

54

CHAPTER 3. DISCRETE RANDOM VARIABLES

Property H:

for any constant d.

V ar(U + d) = V ar(U )

(3.56)

Intuitively, the variance of a constant is 0—after all, it never varies! You can show this formally
using (3.41):

V ar(c) = E(c2) − [E(c)]2 = c2 − c2 = 0

(3.57)

The square root of the variance is called the standard deviation.

Again, we use variance as our main measure of dispersion for historical and mathematical reasons,
not because it’s the most meaningful measure. The squaring in the deﬁnition of variance produces
some distortion, by exaggerating the importance of the larger diﬀerences. It would be more natural
to use the mean absolute deviation (MAD), E(|U − EU |). However, this is less tractable
mathematically, so the statistical pioneers chose to use the mean squared diﬀerence, which lends
itself to lots of powerful and beautiful math, in which the Pythagorean Theorem pops up in abstract
vector spaces. (See Section 19.10.2 for details.)

As with expected values, the properties of variance discussed above, and also in Sec-
tion 12.1.1 below, are key to the entire remainder of this book. You should notice
immediately when you are in a setting in which they are applicable. For instance,
if you see the variance of the sum of two random variables, you should instinctively
think of (3.75) right away, and check whether they are independent.

3.6.2 More Practice with the Properties of Variance

Suppose X and Y are independent random variables, with EX = 1, EY = 2, V ar(X) = 3 and
V ar(Y ) = 4. Let’s ﬁnd V ar(XY ). (The reader should make sure to supply the reasons for each
step, citing equation numbers from the material above.)

V ar(XY ) = E(X 2Y 2) − [E(XY )]2

= E(X 2) · E(Y 2) − (EX · EY )2
= [V ar(X) + (EX)2] · [V ar(Y ) + (EY )2] − (EX · EY )2
= (3 + 12)(4 + 22) − (1 · 2)2
= 28

(3.58)

(3.59)

(3.60)

(3.61)

(3.62)

3.6. VARIANCE

55

3.6.3 Central Importance of the Concept of Variance

No one needs to be convinced that the mean is a fundamental descriptor of the nature of a random
variable. But the variance is of central importance too, and will be used constantly throughout the
remainder of this book.

The next section gives a quantitative look at our notion of variance as a measure of dispersion.

3.6.4

Intuition Regarding the Size of Var(X)

A billion here, a billion there, pretty soon, you’re talking real money—attributed to the late Senator
Everitt Dirksen, replying to a statement that some federal budget item cost “only” a billion dollars

Recall that the variance of a random variable X is supposed to be a measure of the dispersion of X,
meaning the amount that X varies from one instance (one line in our notebook) to the next. But
if Var(X) is, say, 2.5, is that a lot of variability or not? We will pursue this question here.

3.6.4.1 Chebychev’s Inequality

This inequality states that for a random variable X with mean µ and variance σ2,

P (|X − µ| ≥ cσ) ≤

1
c2

(3.63)

In other words, X strays more than, say, 3 standard deviations from its mean at most only 1/9 of
the time. This gives some concrete meaning to the concept of variance/standard deviation.

You’ve probably had exams in which the instructor says something like “An A grade is 1.5 standard
deviations above the mean.” Here c in (3.63) would be 1.5.

We’ll prove the inequality in Section 3.14.

3.6.4.2 The Coeﬃcient of Variation

Continuing our discussion of the magnitude of a variance, look at our remark following (3.63):

In other words, X does not often stray more than, say, 3 standard deviations from its
mean. This gives some concrete meaning to the concept of variance/standard deviation.

56

CHAPTER 3. DISCRETE RANDOM VARIABLES

Or, think of the price of, say, widgets. If the price hovers around a $1 million, but the variation
around that ﬁgure is only about a dollar, you’d say there is essentially no variation. But a variation
of about a dollar in the price of a hamburger would be a lot.

These considerations suggest that any discussion of the size of Var(X) should relate to the size of
E(X). Accordingly, one often looks at the coeﬃcient of variation, deﬁned to be the ratio of the
standard deviation to the mean:

coef. of var. =

(cid:112)V ar(X)
EX

(3.64)

This is a scale-free measure (e.g.
whether a variance is large or not.

inches divided by inches), and serves as a good way to judge

3.7 A Useful Fact

For a random variable X, consider the function

g(c) = E[(X − c)2]

(3.65)

Remember, the quantity E[(X − c)2] is a number, so g(c) really is a function, mapping a real
number c to some real output.

We can ask the question, What value of c minimizes g(c)? To answer that question, write:

g(c) = E[(X − c)2] = E(X 2 − 2cX + c2) = E(X 2) − 2cEX + c2

(3.66)

where we have used the various properties of expected value derived in recent sections.

To make this concrete, suppose we are guessing people’s weights—without seeing them and without
knowing anything about them at all. (This is a somewhat artiﬁcial question, but it will become
highly practical in Chapter ??.) Since we know nothing at all about these people, we will make
the same guess for each of them.

What should that guess-in-common be? Your ﬁrst inclination would be to guess everyone to be the
mean weight of the population. If that value in our target population is, say, 142.8 pounds, then
we’ll guess everyone to be that weight. Actually, that guess turns out to be optimal in a certain
sense, as follows.

3.8. COVARIANCE

57

Say X is a person’s weight. It’s a random variable, because these people are showing up at random
from the population. Then X − c is our prediction error. How well will do in our predictions? We
can’t measure that as

E(error)

(3.67)

because that quantity is 0! (What mailing tube is at work here?)

A reasonable measure would be

However, due to tradition, we use

E(|X − c|)

E[(X − c)2]

(3.68)

(3.69)

Now diﬀerentiate with respect to c, and set the result to 0. Remembering that E(X 2) and EX are
constants, we have

0 = −2EX + 2c

(3.70)

so the minimizing c is c = EX!

In other words, the minimum value of E[(X − c)2] occurs at c = EX. Our intuition was right!

Moreover: Plugging c = EX into (3.66) shows that the minimum value of g(c) is E(X − EX)2] ,
which is Var(X)!

In notebook terms, think of guessing many, many people, meaning many lines in the notebook, one
per person. Then (3.69) is the long-run average squared error in our guesses, and we ﬁnd that we
minimize that by guessing everyone’s weight to be the population mean weight.

But why look at average squared error? It accentuates the large errors. Instead, we could minimize
(3.68). It turns out that the best c here is the population median weight.

3.8 Covariance

This is a topic we’ll cover fully in Chapter 12, but at least introduce here.

58

CHAPTER 3. DISCRETE RANDOM VARIABLES

A measure of the degree to which U and V vary together is their covariance,

Cov(U, V ) = E[(U − EU )(V − EV )]

(3.71)

Except for a divisor, this is essentially correlation. If U is usually large (relative to its expectation)
at the same time V is small (relative to its expectation), for instance, then you can see that the
covariance between them will be negative. On the other hand, if they are usually large together or
small together, the covariance will be positive.

For example, suppose U and V are the height and weight, respectively, of a person chosen at random
from some population, and think in notebook terms. Each line shows the data for one person, and
we’ll have columns for U, V, U - EU, V - EV and (U - EU) (V - EV). Then (3.71) is the long-run
average of that last column. Will it be positive or negative? Reason as follows:

Think of the lines in the notebook for people who are taller than average, i.e. for whom U −EU > 0.
Most such people are also heavier than average, i.e. V − EV > 0, so that (U − EU )(V − EV ) > 0.
On the other hand, shorter people also tend to be lighter, so most lines with shorter people will
have U − EU < 0 and V − EV < 0—but still (U − EU )(V − EV ) > 0. In other words, the long-run
average of the (U - EU) (V - EV) column will be positive.

The point is that, if two variables are positively related, e.g. height and weight, their covariance
should be positive. This is the intuitive underlying deﬁning covariance as in (3.71).

Again, one can use the properties of E() to show that

Cov(U, V ) = E(U V ) − EU · EV

(3.72)

Again, this will be derived fully in Chapter ??, but you think about how to derive it yourself. Just
use our old mailing tubes, e.g. E(X+Y) = EX + EY, E(cX) for a constant c, etc. Note that EU
and EV are constants!

Also

and more generally,

V ar(U + V ) = V ar(U ) + V ar(V ) + 2Cov(U, V )

(3.73)

V ar(aU + bV ) = a2V ar(U ) + b2V ar(V ) + 2abCov(U, V )

(3.74)

for any constants a and b.

3.9.

INDICATOR RANDOM VARIABLES, AND THEIR MEANS AND VARIANCES

59

(3.72) imply that Cov(U,V) = 0. In that case,

V ar(U + V ) = V ar(U ) + V ar(V )

(3.75)

By the way, (3.75) is actually the Pythagorean Theorem in a certain esoteric, inﬁnite-dimesional
vector space (related to a similar remark made earlier). This is pursued in Section 19.10.2 for the
mathematically inclined.

Generalizing (3.74), for constants a1, ..., ak and random variables X1, ..., Xk, form the new random
variable a1X1 + ... + akXk. Then

V ar(a1X1 + ... + akXk) =

k
(cid:88)

i=1

a2
i V ar(Xi) + 2

k
(cid:88)

1≤i<j≤k

Cov(Xi, Xj)

(3.76)

If the Xi are independent, then we have the special case

V ar(a1X1 + ... + akXk) =

k
(cid:88)

i=1

a2
i V ar(Xi)

(3.77)

3.9 Indicator Random Variables, and Their Means and Variances

Deﬁnition 5 A random variable that has the value 1 or 0, according to whether a speciﬁed event
occurs or not is called an indicator random variable for that event.

You’ll often see later in this book that the notion of an indicator random variable is a very handy
device in certain derivations. But for now, let’s establish its properties in terms of mean and
variance.

Handy facts: Suppose X is an indicator random variable for the event A. Let p denote
P(A). Then

E(X) = p

V ar(X) = p(1 − p)

(3.78)

(3.79)

60

CHAPTER 3. DISCRETE RANDOM VARIABLES

These two facts are easily derived.
value,

In the ﬁrst case we have, using our properties for expected

EX = 1 · P (X = 1) + 0 · P (X = 0) = P (X = 1) = P (A) = p

(3.80)

The derivation for Var(X) is similar (use (3.41)).

For example, say Coin A has probability 0.6 of heads, Coin B is fair, and Coin C has probability
0.2 of heads. I toss A once, getting X heads, then toss B once, getting Y heads, then toss C once,
getting Z heads. Let W = X + Y + Z, i.e. the total number of heads from the three tosses (W
ranges from 0 to 3). Let’s ﬁnd P(W = 1) and Var(W).

The ﬁrst one uses old methods:

P (W = 1) = P (X = 1 and Y = 0 and Z = 0 or ...)

= 0.6 · 0.5 · 0.8 + 0.4 · 0.5 · 0.8 + 0.4 · 0.5 · 0.2

(3.81)

(3.82)

For Var(W), let’s use what we just learned about indicator random variables; each of X, Y and Z
are such variables. Var(W) = Var(X) + Var(Y) + Var(Z), by independence and (3.75). Since X is
an indicator random variable, V ar(X) = 0.6 · 0.4, etc. The answer is then

0.6 · 0.4 + 0.5 · 0.5 + 0.2 · 0.8

(3.83)

3.9.1 Example: Return Time for Library Books

Suppose at some public library, patrons return books exactly 7 days after borrowing them, never
early or late. However, they are allowed to return their books to another branch, rather than the
branch where they borrowed their books. In that situation, it takes 9 days for a book to return
to its proper library, as opposed to the normal 7. Suppose 50% of patrons return their books to a
“foreign” library. Find Var(T), where T is the time, either 7 or 9 days, for a book to come back to
its proper location.

Note that

where I is an indicator random variable for the event that the book is returned to a “foreign”

T = 7 + 2I,

(3.84)

3.9.

INDICATOR RANDOM VARIABLES, AND THEIR MEANS AND VARIANCES

61

branch. Then

V ar(T ) = V ar(7 + 2I) = 4V ar(I) = 4 · 0.5(1 − 0.5)

(3.85)

Now let’s look at a somewhat more general model. Here we will assume that borrowers return
books after 4, 5, 6 or 7 days, with probabilities 0.1, 0.2, 0.3, 0.4, respectively. As before, 50% of
patrons return their books to a “foreign” branch, resulting in an extra 2-day delay before the book
arrives back to its proper location. The library is open 7 days a week.

Suppose you wish to borrow a certain book, and inquire at the library near the close of business
on Monday. Assume too that no one else is waiting for the book. You are told that it had been
checked out the previous Thursday. Find the probability that you will need to wait until Wednesday
evening to get the book. (You check every evening.)

Let B denote the time needed for the book to arrive back at its home branch, and deﬁne I as before.
Then

P (B = 6 | B > 4) =

P (B = 6 and B > 4)
P (B > 4)

=

=

=

=

P (B = 6)
P (B > 4)
P (B = 6 and I = 0 or B = 6 and I = 1)
1 − P (B = 4)

0.5 · 0.3 + 0.5 · 0.1
1 − 0.5 · 0.1

4
19

(3.86)

(3.87)

(3.88)

(3.89)

(3.90)

Here is a simulation check:

f o r

l i b s i m <− function ( n r e p s ) {
# p a t r o n r e t u r n time
p r t <− sample ( c ( 4 , 5 , 6 , 7 ) , nreps , replace=T, prob=c ( 0 . 1 , 0 . 2 , 0 . 3 , 0 . 4 ) )
# i n d i c a t o r
i <− sample ( c ( 0 , 1 ) , nreps , replace=T)
b <− p r t + 2∗ i
x <− cbind ( prt , i , b )
# l o o k o n l y a t
bgt4 <− x [ b > 4 , ]
# among t h o s e l i n e s , what p r o p o r t i o n have B = 6?

t h e r e l e v a n t n o t e b o o k l i n e s

f o r e i g n branch

62

}

mean( bgt4 [ , 3 ] == 6 )

CHAPTER 3. DISCRETE RANDOM VARIABLES

3.9.2 Example: Indicator Variables in a Committee Problem

A committee of four people is drawn at random from a set of six men and three women. Suppose
we are concerned that there may be quite a gender imbalance in the membership of the committee.
Toward that end, let M and W denote the numbers of men and women in our committee, and let
D = M-W. Let’s ﬁnd E(D), in two diﬀerent ways.

D has support consisting of the values 4-0, 3-1, 2-2 and 1-3, i.e. 4, 2, 0 and -2. So from (3.13)

ED = −2 · P (D = −2) + 0 · P (D = 0) + 2 · P (D = 2) + 4 · P (D = 4)

(3.91)

Now, using reasoning along the lines in Section 2.15, we have

P (D = −2) = P (M = 1 and W = 3) =

(cid:0)6
(cid:1)
(cid:1)(cid:0)3
3
1
(cid:1)
(cid:0)9
4

(3.92)

After similar calculations for the other probabilities in (3.91), we ﬁnd the ED = 4
3 .

Note what this means: If we were to perform this experiment many times, i.e. choose committees
again and again, on average we would have a little more than one more man than women on the
committee.

Now let’s use our “mailing tubes” to derive ED a diﬀerent way:

ED = E(M − W )

= E[M − (4 − M )]

= E(2M − 4)

= 2EM − 4 (from (3.21))

(3.93)

(3.94)

(3.95)

(3.96)

Now, let’s ﬁnd EM by using indicator random variables. Let Gi denote the indicator random
variable for the event that the ith person we pick is male, i = 1,2,3,4. Then

M = G1 + G2 + G3 + G4

(3.97)

3.9.

INDICATOR RANDOM VARIABLES, AND THEIR MEANS AND VARIANCES

63

so

EM = E(G1 + G2 + G3 + G4)

= EG1 + EG2 + EG3 + EG4 [ from (3.19)]
= P (G1 = 1) + P (G2 = 1) + P (G3 = 1) + P (G4 = 1) [ from (3.78)]

(3.98)

(3.99)

(3.100)

Note carefully that the second equality here, which uses (3.19), is true in spite of the fact that the
Gi are not independent. Equation (3.19) does not require independence.

Another key point is that, due to symmetry, P (Gi = 1) is the same for all i. Note that we did
not write a conditional probability here! Once again, think of the notebook view: By deﬁnition,
(P (G2 = 1) is the long-run proportion of the number of notebook lines in which G2 = 1—regardless
of the value of G1 in those lines.

Now, to see that P (Gi = 1) is the same for all i, suppose the six men that are available for the
committee are named Alex, Bo, Carlo, David, Eduardo and Frank. When we select our ﬁrst person,
any of these men has the same chance of being chosen (1/9). But that is also true for the second
pick. Think of a notebook, with a column named “second pick.” In some lines, that column will
say Alex, in some it will say Bo, and so on, and in some lines there will be women’s names. But
in that column, Bo will appear the same fraction of the time as Alex, due to symmetry, and that
will be the same fraction as for, say, Alice, again 1/9.

Now,

Thus

P (G1 = 1) =

6
9

=

2
3

ED = 2 · (4 ·

2
3

) − 4 =

4
3

(3.101)

(3.102)

3.9.3 Example: Spinner Game

In a certain game, Person A spins a spinner and wins S dollars, with mean 10 and variance 5.
Person B ﬂips a coin. If it comes up heads, Person A must give B whatever A won, but if it comes
up tails, B wins nothing. Let T denote the amount B wins. Let’s ﬁnd V ar(T ).

We can use (3.60), in this case with X = I, where I is an indicator variable for the event that B

64

CHAPTER 3. DISCRETE RANDOM VARIABLES

gets a head, and with Y = S. Then T = I · S, and I and S are independent, so

V ar(T ) = V ar(IS) = [V ar(I) + (EI)2] · [V ar(S) + (ES)2] − (EI · ES)2

(3.103)

Then use the facts that I has mean 0.5 and variance 0.5(1-0.5) (Equations (3.78) and (3.79), with
S having the mean 10 and variance 5, as given in the problem.

3.10 Expected Value, Etc. in the ALOHA Example

Finding expected values etc. in the ALOHA example is straightforward. For instance,

EX1 = 0 · P (X1 = 0) + 1 · P (X1 = 1) + 2 · P (X1 = 2) = 1 · 0.48 + 2 · 0.52 = 1.52

(3.104)

Here is R code to ﬁnd various values approximately by simulation:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

# finds E(X1), E(X2), Var(X2), Cov(X1,X2)
sim <- function(p,q,nreps) {

sumx1 <- 0
sumx2 <- 0
sumx2sq <- 0
sumx1x2 <- 0
for (i in 1:nreps) {
numtrysend <-

sum(sample(0:1,2,replace=TRUE,prob=c(1-p,p)))
X1 <- 1

if (numtrysend == 1)
else X1 <- 2
numactive <- X1
if (X1 == 1 && runif(1) < q) numactive <- numactive + 1
if (numactive == 1)

if (runif(1) < p) X2 <- 0
else X2 <- 1

else {

# numactive = 2

numtrysend <- 0
for (i in 1:2)

if (runif(1) < p) numtrysend <- numtrysend + 1

if (numtrysend == 1) X2 <- 1
else X2 <- 2

}
sumx1 <- sumx1 + X1
sumx2 <- sumx2 + X2
sumx2sq <- sumx2sq + X2^2
sumx1x2 <- sumx1x2 + X1*X2

}
# print results
meanx1 <- sumx1 /nreps
cat("E(X1):",meanx1,"\n")
meanx2 <- sumx2 /nreps
cat("E(X2):",meanx2,"\n")

3.11. EXAMPLE: MEASUREMENTS AT DIFFERENT AGES

65

cat("Var(X2):",sumx2sq/nreps - meanx2^2,"\n")
cat("Cov(X1,X2):",sumx1x2/nreps - meanx1*meanx2,"\n")

34

35

36

}

As a check on your understanding so far, you should ﬁnd at least one of these values by hand, and
see if it jibes with the simulation output.

3.11 Example: Measurements at Diﬀerent Ages

Say a large research program measures boys’ heights at age 10 and age 15. Call the two heights X
and Y. So, each boy has an X and a Y. Each boy is a “notebook line”, and the notebook has two
columns, for X and Y. We are interested in Var(Y-X). Which of the following is true?

(i) V ar(Y − X) = V ar(Y ) + V ar(X)

(ii) V ar(Y − X) = V ar(Y ) − V ar(X)

(iii) V ar(Y − X) < V ar(Y ) + V ar(X)

(iv) V ar(Y − X) < V ar(Y ) − V ar(X)

(v) V ar(Y − X) > V ar(Y ) + V ar(X)

(vi) V ar(Y − X) > V ar(Y ) − V ar(X)

(vii) None of the above.

Use the mailing tube (3.74):

V ar(Y − X) = V ar[Y + (−X)] = V ar(Y ) + V ar(X) − 2Cov(X, Y )

(3.105)

Since X and Y are positively correlated, their covariance is positive, so the answer is (iii).

3.12 Example: Bus Ridership Model

In the bus ridership model, Section 2.12, let’s ﬁnd V ar(L1):

V ar(L1) = E(L2

1) − (EL1)2

(3.106)

66

CHAPTER 3. DISCRETE RANDOM VARIABLES

EL1 = EB1 = 0 · 0.5 + 1 · 0.4 + 2 · 0.1

E(L2

1) = 02 · 0.5 + 12 · 0.4 + 22 · 0.1

(3.107)

(3.108)

Then put it all together.

3.13 Distributions

The idea of the distribution of a random variable is central to probability and statistics.

Deﬁnition 6 Let U be a discrete random variable. Then the distribution of U is simply the support
of U, together with the associated probabilities.

Example: Let X denote the number of dots one gets in rolling a die. Then the values X can take
on are 1,2,3,4,5,6, each with probability 1/6. So

distribution of X = {(1,

1
6

), (2,

1
6

), (3,

1
6

), (4,

1
6

), (5,

1
6

), (6,

1
6

)}

(3.109)

Example: Recall the ALOHA example. There X1 took on the values 1 and 2, with probabilities
0.48 and 0.52, respectively (the case of 0 was impossible). So,

distribution of X1 = {(0, 0.00), (1, 0.48), (2, 0.52)}

(3.110)

Example: Recall our example in which N is the number of tosses of a coin needed to get the ﬁrst
head. N has support 1,2,3,..., the probabilities of which we found earlier to be 1/2, 1/4, 1/8,... So,

distribution of N = {(1,

1
2

), (2,

1
4

), (3,

1
8

), ...}

(3.111)

It is common to express this in functional notation:

Deﬁnition 7 The probability mass function (pmf ) of a discrete random variable V, denoted
pV , as

pV (k) = P (V = k)

(3.112)

3.13. DISTRIBUTIONS

67

for any value k in the support of V.

(Please keep in mind the notation.
consisting of the name of the random variable.)

It is customary to use the lower-case p, with a subscript

Note that pV () is just a function, like any function (with integer domain) you’ve had in your
previous math courses. For each input value, there is an output value.

3.13.1 Example: Toss Coin Until First Head

In (3.111),

pN (k) =

1
2k , k = 1, 2, ...

(3.113)

3.13.2 Example: Sum of Two Dice

In the dice example, in which S = X+Y,

pS(k) =






1
36 , k = 2
2
36 , k = 3
3
36 , k = 4
...
1
36 , k = 12

(3.114)

It is important to note that there may not be some nice closed-form expression for pV like that of
(3.113). There was no such form in (3.114), nor is there in our ALOHA example for pX1 and pX2.

3.13.3 Example: Watts-Strogatz Random Graph Model

Random graph models are used to analyze many types of link systems, such as power grids, social
networks and even movie stars. We saw our ﬁrst example in Section 2.13.1, and here is another, a
variation on a famous model of that type, due to Duncan Watts and Steven Strogatz.

68

CHAPTER 3. DISCRETE RANDOM VARIABLES

3.13.3.1 The Model

We have a graph of n nodes, e.g. in which each node is a person).4 Think of them as being linked
in a circle—we’re just talking about relations here, not physical locations—so we already have n
links. One can thus reach any node in the graph from any other, by following the links of the circle.
(We’ll assume all links are bidirectional.)

We now randomly add k more links (k is thus a parameter of the model), which will serve as
(cid:1) = n(n − 1)/2 possible links between nodes, but remember, we already
“shortcuts.” There are(cid:0)n
2
have n of those in the graph, so there are only n(n − 1)/2 − n = n2/2 − 3n/2 possibilities left. We’ll
be forming k new links, chosen at random from those n2/2 − 3n/2 possibilities.

Let M denote the number of links attached to a particular node, known as the degree of a node.
M is a random variable (we are choosing the shortcut links randomly), so we can talk of its pmf,
pM , termed the degree distribution of M, which we’ll calculate now.

Well, pM (r) is the probability that this node has r links. Since the node already had 2 links before
the shortcuts were constructed, pM (r) is the probability that r-2 of the k shortcuts attach to this
node.

This problem is similar in spirit to (though admittedly more diﬃcult to think about than) kings-
and-hearts example of Section 2.15.1. Other than the two neighboring links in the original circle
and the “link” of a node to itself, there are n-3 possible shortcut links to attach to our given node.
We’re interested in the probability that r-2 of them are chosen, and that k-(r-2) are chosen from
the other possible links. Thus our probability is:

pM (r) =

(cid:0)n−3
r−2

(cid:1)
(cid:1)(cid:0)n2/2−3n/2−(n−3)
k−(r−2)
(cid:0)n2/2−3n/2
(cid:1)
k

=

(cid:0)n−3
r−2

(cid:1)
(cid:1)(cid:0)n2/2−5n/2+3
k−(r−2)
(cid:0)n2/2−3n/2
(cid:1)
k

(3.115)

3.13.3.2 Further Reading

UCD professor Raissa D’Souza specializes in random graph models. See for instance Beyond Friend-
ship: Modeling User activity Graphs on Social Network-Based Gifting Applications, A. Nazir, A.
Waagen, V. Vijayaraghavan, C.-N. Chuah, R. M. D’Souza, B. Krishnamurthy, ACM Internet Mea-
surement Conference (IMC 2012), Nov 2012.

4The word graph here doesn’t mean “graph” in the sense of a picture. Here we are using the computer science

sense of the word, meaning a system of vertices and edges. It’s common to call those nodes and links.

3.14. PROOF OF CHEBYCHEV’S INEQUALITY (OPTIONAL SECTION)

69

notebook line
1
2
3

Y dZ Y ≥ dZ?
yes
yes
yes

0
3
0

0.36
3.6
2.6

Table 3.2: Illustration of Y and Z

3.14 Proof of Chebychev’s Inequality (optional section)

To prove (3.63), let’s ﬁrst state and prove Markov’s Inequality: For any nonnegative random
variable Y and positive constant d,

P (Y ≥ d) ≤

EY
d

(3.116)

To prove (3.116), let Z be the indicator random variable for the event Y ≥ d (Section 3.9).

Now note that

Y ≥ dZ

(3.117)

To see this, just think of a notebook, say with d = 3. Then the notebook might look like Table 3.2.

So

EY ≥ dEZ

(3.118)

(Again think of the notebook. The long-run average in the Y column will be ≥ the corresponding
average for the dZ column.)

The right-hand side of (3.118) is dP (Y ≥ d), so (3.116) follows.

Now to prove (3.63), deﬁne

Y = (X − µ)2

(3.119)

70

CHAPTER 3. DISCRETE RANDOM VARIABLES

and set d = c2σ2. Then (3.116) says

P [(X − µ)2 ≥ c2σ2] ≤

E[(X − µ)2]
c2σ2

(3.120)

Since

(X − µ)2 ≥ c2σ2 if and only if |X − µ| ≥ cσ

(3.121)

the left-hand side of (3.120) is the same as the left-hand side of (3.63). The numerator of the
right-hand size of (3.120) is simply Var(X), i.e. σ2, so we are done.

Chapter 4

Discrete Parametric Distribution
Families

The notion of a parametric family of distributions is a key concept that will recur throughout the
book.

Consider plotting the curves ga,b(t) = (t − a)2 + b. For each a and b, we get a diﬀerent parabola,
as seen in this plot of three of the curves:

71

a=0, b=0a=1, b=0a=−1.5, b=3010203040−5.0−2.50.02.55.0xy72

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

This is a family of curves, thus a family of functions. We say the numbers a and b are the
parameters of the family. Note carefully that t is not a parameter, but rather just an argument
of each function. The point is that a and b are indexing the curves.

4.1 The Case of Importance to Us: Parameteric Families of pmfs

Probability mass functions are still functions.1 Thus they too can come in parametric families,
indexed by one or more parameters. We had an example in Section 3.13.3. Since we get a diﬀerent
function pM for each diﬀerent values of k and n, that was a parametric family of pmfs, indexed by
k and n.

Some parametric families of pmfs have been found to be so useful over the years that they’ve been
given names. We will discuss some of those families here. But remember, they are famous just
because they have been found useful, i.e. that they ﬁt real data well in various settings. Do not
jump to the conclusion that we always “must” use pmfs from some family.

4.2 The Geometric Family of Distributions

To explain our ﬁrst parametric family of pmfs, recall our example of tossing a coin until we get the
ﬁrst head, with N denoting the number of tosses needed. In order for this to take k tosses, we need
k-1 tails and then a head. Thus

pN (k) = (1 −

1
2

)k−1 ·

1
2

, k = 1, 2, ...

(4.1)

We might call getting a head a “success,” and refer to a tail as a “failure.” Of course, these words
don’t mean anything; we simply refer to the outcome of interest (which of course we ourselves
choose) as “success.”

Deﬁne M to be the number of rolls of a die needed until the number 5 shows up. Then

(cid:18)

pM (k) =

1 −

(cid:19)k−1 1
6

1
6

, k = 1, 2, ...

(4.2)

reﬂecting the fact that the event {M = k} occurs if we get k-1 non-5s and then a 5. Here “success”
is getting a 5.

1The domains of these functions are typically the integers, but that is irrelevant; a function is a function.

4.2. THE GEOMETRIC FAMILY OF DISTRIBUTIONS

73

The tosses of the coin and the rolls of the die are known as Bernoulli trials, which is a sequence
of independent events. We call the occurrence of the event success and the nonoccurrence failure
(just convenient terms, not value judgments). The associated indicator random variable are denoted
Bi, i = 1,2,3,... So Bi is 1 for success on the ith trial, 0 for failure, with success probability p. For
instance, p is 1/2 in the coin case, and 1/6 in the die example.

In general, suppose the random variable W is deﬁned to be the number of trials needed to get a
success in a sequence of Bernoulli trials. Then

pW (k) = (1 − p)k−1p, k = 1, 2, ...

(4.3)

Note that there is a diﬀerent distribution for each value of p, so we call this a parametric family
of distributions, indexed by the parameter p. We say that W is geometrically distributed with
parameter p.2

It should make good intuitive sense to you that

E(W ) =

1
p

(4.4)

This is indeed true, which we will now derive. First we’ll need some facts (which you should ﬁle
mentally for future use as well):

Properties of Geometric Series:

(a) For any t (cid:54)= 1 and any nonnegative integers r ≤ s,

s
(cid:88)

i=r

ti = tr 1 − ts−r+1

1 − t

(4.5)

This is easy to derive for the case r = 0, using mathematical induction. For the general case,
just factor out tr.

(b) For |t| < 1,

∞
(cid:88)

i=0

ti =

1
1 − t

(4.6)

To prove this, just take r = 0 and let s → ∞ in (4.5).

2Unfortunately, we have overloaded the letter p here, using it to denote the probability mass function on the left
side, and the unrelated parameter p, our success probability on the right side. It’s not a problem as long as you are
aware of it, though.

74

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

(c) For |t| < 1,

∞
(cid:88)

i=1

iti−1 =

1
(1 − t)2

This is derived by applying d

dt to (4.6).3

Deriving (4.4) is then easy, using (4.7):

EW =

∞
(cid:88)

i=1

i(1 − p)i−1p

= p

∞
(cid:88)

i=1

i(1 − p)i−1

1
[1 − (1 − p)]2

= p ·

=

1
p

Using similar computations, one can show that

V ar(W ) =

1 − p
p2

(4.7)

(4.8)

(4.9)

(4.10)

(4.11)

(4.12)

We can also ﬁnd a closed-form expression for the quantities P (W ≤ m), m = 1,2,... (This has a
formal name FW (m) , as will be seen later in Section 7.3.) For any positive integer m we have

FW (m) = P (W ≤ m)

= 1 − P (W > m)

= 1 − P (the ﬁrst m trials are all failures)
= 1 − (1 − p)m

(4.13)

(4.14)

(4.15)

(4.16)

By the way, if we were to think of an experiment involving a geometric distribution in terms of our
notebook idea, the notebook would have an inﬁnite number of columns, one for each Bi. Within
each row of the notebook, the Bi entries would be 0 until the ﬁrst 1, then NA (“not applicable”)
after that.

3To be more careful, we should diﬀerentiate (4.5) and take limits.

4.2. THE GEOMETRIC FAMILY OF DISTRIBUTIONS

75

4.2.1 R Functions

You can simulate geometrically distributed random variables via R’s rgeom() function. Its ﬁrst
argument speciﬁes the number of such random variables you wish to generate, and the second is
the success probability p.

For example, if you run

> y <- rgeom(2,0.5)

then it’s simulating tossing a coin until you get a head (y[1]) and then tossing the coin until a head
again (y[2]). Of course, you could simulate on your own, say using sample() and while(), but R
makes it convenient for you.

Here’s the full set of functions for a geometrically distributed random variable X with success
probability p:

• dgeom(i,p), to ﬁnd P (X = i)

• pgeom(i,p), to ﬁnd P (X ≤ i)

• qgeom(q,p), to ﬁnd c such that P (X ≤ c) = q

• rgeom(n,p), to generate n variates from this geometric distribution

Important note: Some books deﬁne geometric distributions slightly diﬀerently, as the number of
failures before the ﬁrst success, rather than the number of trials to the ﬁrst success. The same is
true for software—both R and Python deﬁne it this way. Thus for example in calling dgeom(),
subtract 1 from the value used in our deﬁnition.

For example, here is P(N = 3) for a geometric distribution under our deﬁntion, with p = 0.4:

> dgeom ( 2 , 0 . 4 )
[ 1 ] 0 . 1 4 4
> # c h e c k
> (1 −0.4)ˆ(3 −1) ∗ 0 . 4
[ 1 ] 0 . 1 4 4

Note that this also means one must add 1 to the result of rgeom().

76

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

4.2.2 Example: a Parking Space Problem

Suppose there are 10 parking spaces per block on a certain street. You turn onto the street at
the start of one block, and your destination is at the start of the next block. You take the ﬁrst
parking space you encounter. Let D denote the distance of the parking place you ﬁnd from your
destination, measured in parking spaces. Suppose each space is open with probability 0.15, with
the spaces being independent. Find ED.

To solve this problem, you might at ﬁrst think that D follows a geometric distribution. But don’t
jump to conclusions! Actually this is not the case; D is a somewhat complicated distance. But
clearly D is a function of N, where the latter denotes the number of parking spaces you see until
you ﬁnd an empty one—and N is geometrically distributed.

As noted, D is a function of N:

D =

(cid:40)

11 − N, N ≤ 10
N − 11, N > 10

(4.17)

Since D is a function of N, we can use (3.36) with g(t) as in (4.17):

10
(cid:88)

(11 − i)(1 − 0.15)i−10.15 +

∞
(cid:88)

ED =

(i − 11)0.85i−10.15

(4.18)

i=1

i=11

This can now be evaluated using the properties of geometric series presented above.

Alternatively, here’s how we could ﬁnd the result by simulation:

1 parksim <− function ( n r e p s ) {
2
3
4
5
6
7
8 }

# do t h e e x p e r i m e n t n r e p s times ,
n v a l s <− rgeom( nreps , 0 . 1 5 ) + 1
# now f i n d t h e v a l u e s o f D
d v a l s <− i f e l s e ( n v a l s <= 10 ,11 − n v a l s , n v a l s −11)
# r e t u r n ED
mean( d v a l s )

r e c o r d i n g t h e v a l u e s o f N

Note the vectorized addition and recycling (Section 2.14.2) in the line

n v a l s <− rgeom( nreps , 0 . 1 5 ) + 1

The call to ifelse() is another instance of R’s vectorization, a vectorized if-then-else. The ﬁrst
argument evaluates to a vector of TRUE and FALSE values. For each TRUE, the corresponding

4.3. THE BINOMIAL FAMILY OF DISTRIBUTIONS

77

element of dvals will be set to the corresponding element of the vector 11-nvals (again involving
vectorized addition and recycling), and for each false, the element of dvals will be set to the element
of nvals-11.

Let’s ﬁnd some more, ﬁrst pN (3):

pN (3) = P (N = 3) = (1 − 0.15)3−10.15

(4.19)

Next, ﬁnd P(D = 1):

P (D = 1) = P (N = 10 or N = 12)

= (1 − 0.15)10−10.15 + (1 − 0.15)12−10.15

(4.20)

(4.21)

Say Joe is the one looking for the parking place. Paul is watching from a side street at the end of
the ﬁrst block (the one before the destination), and Martha is watching from an alley situated right
after the sixth parking space in the second block. Martha calls Paul and reports that Joe never
went past the alley, and Paul replies that he did see Joe go past the ﬁrst block. They are interested
in the probability that Joe parked in the second space in the second block. In mathematical terms,
what probability is that? Make sure you understand that it is P (N = 12 | N > 10 and N ≤ 16).
It can be evaluated as above.

Also: Good news! I found a parking place just one space away from the destination. Find the
probability that I am parked in the same block as the destination.

P (N = 12 | N = 10 or N = 12) =

P (N = 12)
P (N = 10 or N = 12)

=

(1 − 0.15)11 0.15
(1 − 0.15)9 0.15 + (1 − 0.15)11 0.15

(4.22)

(4.23)

4.3 The Binomial Family of Distributions

A geometric distribution arises when we have Bernoulli trials with parameter p, with a variable
number of trials (N) but a ﬁxed number of successes (1). A binomial distribution arises when
we have the opposite—a ﬁxed number of Bernoulli trials (n) but a variable number of successes
(say X).4

4Note again the custom of using capital letters for random variables, and lower-case letters for constants.

78

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

For example, say we toss a coin ﬁve times, and let X be the number of heads we get. We say that
X is binomially distributed with parameters n = 5 and p = 1/2. Let’s ﬁnd P(X = 2). There are
many orders in which that could occur, such as HHTTT, TTHHT, HTTHT and so on. Each order
(cid:1) orders. Thus
has probability 0.52(1 − 0.5)3, and there are (cid:0)5
2

P (X = 2) =

(cid:19)

0.52(1 − 0.5)3 =

(cid:18)5
2

(cid:19)

/32 = 5/16

(cid:18)5
2

For general n and p,

pX (k) = P (X = k) =

(cid:19)

pk(1 − p)n−k

(cid:18)n
k

(4.24)

(4.25)

So again we have a parametric family of distributions, in this case a family having two parameters,
n and p.

Let’s write X as a sum of those 0-1 Bernoulli variables we used in the discussion of the geometric
distribution above:

X =

n
(cid:88)

i=1

Bi

(4.26)

where Bi is 1 or 0, depending on whether there is success on the ith trial or not. Note again that
the Bi are indicator random variables (Section 3.9), so

and

EBi = p

V ar(Bi) = p(1 − p)

(4.27)

(4.28)

Then the reader should use our earlier properties of E() and Var() in Sections 3.5 and 3.6 to ﬁll
in the details in the following derivations of the expected value and variance of a binomial random
variable:

EX = E(B1 + ..., +Bn) = EB1 + ... + EBn = np

(4.29)

4.3. THE BINOMIAL FAMILY OF DISTRIBUTIONS

79

and from (3.75),

V ar(X) = V ar(B1 + ... + Bn) = V ar(B1) + ... + V ar(Bn) = np(1 − p)

(4.30)

Again, (4.29) should make good intuitive sense to you.

4.3.1 R Functions

Relevant functions for a binomially distributed random variable X for k trials and with success
probability p are:

• dbinom(i,k,p), to ﬁnd P (X = i)

• pbinom(i,k,p), to ﬁnd P (X ≤ i)

• qbinom(q,k,p), to ﬁnd c such that P (X ≤ c) = q

• rbinom(n,k,p), to generate n independent values of X

Our deﬁnition above of qbinom() is not quite tight, though. Consider a random variable X which
has a binomial distribution with n = 2 and p = 0.5 Then

FX (0) = 0.25, FX (1) = 0.50

(4.31)

So if q is, say, 0.33, there is no c such that P (X ≤ c) = q. For that reason, the actual deﬁnition of
qbinom() is the smallest c satisfying P (X ≤ c) ≥ q.

4.3.2 Example: Parking Space Model

Recall Section 4.2.2. Let’s ﬁnd the probability that there are three open spaces in the ﬁrst block.

Let M denote the number of open spaces in the ﬁrst block. This ﬁts the deﬁnition of binomially-
distributed random variables: We have a ﬁxed number (10) of independent Bernoulli trials, and we
are interested in the number of successes. So, for instance,

pM (3) =

(cid:19)

(cid:18)10
3

0.153(1 − 0.15)10−3

(4.32)

80

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

4.4 The Negative Binomial Family of Distributions

Recall that a typical example of the geometric distribution family (Section 4.2) arises as N, the
number of tosses of a coin needed to get our ﬁrst head. Now generalize that, with N now being
the number of tosses needed to get our rth head, where r is a ﬁxed value. Let’s ﬁnd P(N = k), k
= r, r+1, ... For concreteness, look at the case r = 3, k = 5. In other words, we are ﬁnding the
probability that it will take us 5 tosses to accumulate 3 heads.

First note the equivalence of two events:

{N = 5} = {2 heads in the ﬁrst 4 tosses and head on the 5th toss}

(4.33)

That event described before the “and” corresponds to a binomial probability:

P (2 heads in the ﬁrst 4 tosses) =

(cid:19)4

(cid:18)4
2

(cid:19) (cid:18) 1
2

(4.34)

Since the probability of a head on the kth toss is 1/2 and the tosses are independent, we ﬁnd that

P (N = 5) =

(cid:19)5

(cid:18)4
2

(cid:19) (cid:18) 1
2

=

3
16

(4.35)

The negative binomial distribution family, indexed by parameters r and p, corresponds to random
variables that count the number of independent trials with success probability p needed until we
get r successes. The pmf is

pN (k) = P (N = k) =

(cid:18)k − 1
r − 1

(cid:19)

(1 − p)k−rpr, k = r, r + 1, ...

We can write

N = G1 + ... + Gr

(4.36)

(4.37)

where Gi is the number of tosses between the successes numbers i-1 and i. But each Gi has a
geometric distribution! Since the mean of that distribution is 1/p, we have that

E(N ) = r ·

1
p

(4.38)

4.5. THE POISSON FAMILY OF DISTRIBUTIONS

81

In fact, those r geometric variables are also independent, so we know the variance of N is the sum
of their variances:

V ar(N ) = r ·

1 − p
p2

(4.39)

4.4.1 R Functions

Relevant functions for a negative binomial distributed random variable X with success parameter
p are:

• dnbinom(i,size=1,prob=p), to ﬁnd P (X = i)

• pnbinom(i,size=1,prob=p), to ﬁnd P (X <= i)

• qnbinom(q,sixe=1,prob=p), to ﬁnd c such that P (X ≤ c) = q

• rnbinom(n,size=1,prob=p), to generate n independent values of X

4.4.2 Example: Backup Batteries

A machine contains one active battery and two spares. Each battery has a 0.1 chance of failure
each month. Let L denote the lifetime of the machine, i.e. the time in months until the third
battery failure. Find P(L = 12).

The number of months until the third failure has a negative binomial distribution, with r = 3 and
p = 0.1. Thus the answer is obtained by (4.36), with k = 12:

P (L = 12) =

(cid:19)

(cid:18)11
2

(1 − 0.1)90.13

(4.40)

4.5 The Poisson Family of Distributions

Another famous parametric family of distributions is the set of Poisson Distributions.

This family is a little diﬀerent from the geometric, binomial and negative binomial families, in
the sense that in those cases there were qualitative descriptions of the settings in which such
distributions arise. Geometrically distributed random variables, for example occur as the number
of Bernoulli trials needed to get the ﬁrst success.

82

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

By contrast, the Poisson family does not really have this kind of qualitative description.5
It is
merely something that people have found to be a reasonably accurate model of actual data. We
might be interested, say, in the number of disk drive failures in periods of a speciﬁed length of time.
If we have data on this, we might graph it, and if it looks like the pmf form below, then we might
adopt it as our model.

The pmf is

It turns out that

P (X = k) =

e−λλk
k!

, k = 0, 1, 2, ...

EX = λ

V ar(X) = λ

(4.41)

(4.42)

(4.43)

The derivations of these facts are similar to those for the geometric family in Section 4.2. One
starts with the Maclaurin series expansion for et:

et =

∞
(cid:88)

i=0

ti
i!

(4.44)

and ﬁnds its derivative with respect to t, and so on. The details are left to the reader.

The Poisson family is very often used to model count data. For example, if you go to a certain
bank every day and count the number of customers who arrive between 11:00 and 11:15 a.m., you
will probably ﬁnd that that distribution is well approximated by a Poisson distribution for some λ.

There is a lot more to the Poisson story than we see in this short section. We’ll return to this
distribution family in Section 9.1.

4.5.1 R Functions

Relevant functions for a Poisson distributed random variable X with parameter lambda are:

• dpois(i,lambda), to ﬁnd P (X = i)

5Some such descriptions are possible in the Poisson case, but they are complicated and diﬃcult to verify.

4.6. THE POWER LAW FAMILY OF DISTRIBUTIONS

83

• ppois(i,lambda), to ﬁnd P (X ≤ i)

• qpois(q,lambda), to ﬁnd c such that P (X ≤ c) = q

• rpois(n,lambda), to generate n independent values of X

4.6 The Power Law Family of Distributions

This family has attracted quite a bit of attention in recent years, due to its use in random graph
models.

4.6.1 The Model

Here

pX (k) = ck−γ, k = 1, 2, 3, ...

(4.45)

It is required that γ > 1, as otherwise the sum of probabilities will be inﬁnite. For γ satisfying that
condition, the value c is chosen so that that sum is 1.0:

1.0 =

∞
(cid:88)

k=1

ck−γ ≈ c

(cid:90) ∞

1

k−γ dk = c/(γ − 1)

(4.46)

so c ≈ γ − 1.

Here again we have a parametric family of distributions, indexed by the parameter γ.

The power law family is an old-fashioned model (an old-fashioned term for distribution is law),
but there has been a resurgence of interest in it in recent years. Analysts have found that many
types of social networks in the real world exhibit approximately power law behavior in their degree
distributions.

For instance, in a famous study of the Web (A. Barabasi and R. Albert, Emergence of Scaling in
Random Networks, Science, 1999, 509-512), degree distribution on the Web (a directed graph, with
incoming links being the ones of interest here) it was found that the number of links leading to a
Web page has an approximate power law distribution with γ = 2.1. The number of links leading
out of a Web page was found to be approximately power-law distributed, with γ = 2.7.

Much of the interest in power laws stems from their fat tails, a term meaning that values far
from the mean are more likely under a power law than they would be under a normal distribution

84

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

with the same mean. In recent popular literature, values far from the mean have often been called
black swans. The ﬁnancial crash of 2008, for example, is blamed by some on the ignorance by
quants (people who develop probabilistic models for guiding investment) in underestimating the
probabilities of values far from the mean.

Some examples of real data that are, or are not, ﬁt well by power law models are given in the
paper Power-Law Distributions in Empirical Data, by A. Clauset, C. Shalizi and M. Newman, at
http://arxiv.org/abs/0706.1062. Methods for estimating the parameter γ are discussed and
evaluated.

A variant of the power law model is the power law with exponential cutoﬀ, which essentially
consists of a blend of the power law and a geometric distribution. Here

pX (k) = ck−γqk

(4.47)

This now is a two-parameter family, the parameters being γ and q. Again c is chosen so that the
pmf sums to 1.0.

This model is said to work better than a pure power law for some types of data. Note, though,
that this version does not really have the fat tail property, as the tail decays exponentially now.

4.6.2 Further Reading

There is nice paper on ﬁtting (or not ﬁtting) power law models:

Power-Law Distributions in Empirical Data, SIAM Review, A. Clauset, C.R. Shalizi, and M.E.J.
Newman, 51(4), 661-703, 2009.

4.7 Recognizing Some Parametric Distributions When You See

Them

Three of the discrete distribution families we’ve considered here arise in settings with very deﬁnite
structure, all dealing with independent trials:

• the binomial family gives the distribution of the number of successes in a ﬁxed number of

trials

• the geometric family gives the distribution of the number of trials needed to obtain the ﬁrst

success

4.8. EXAMPLE: A COIN GAME

85

• the negative binomial family gives the distribution of the number of trials needed to obtain

the kth success

Such situations arise often, hence the fame of these distribution families.

By contrast, the Poisson and power law distributions have no underlying structure. They are
famous for a diﬀerent reason, that it has been found empirically that they provide a good ﬁt to
many real data sets.

In other words, the Poisson and power law distributions are typically ﬁt to data, in an attempt to
ﬁnd a good model, whereas in the binomial, geometric and negative binomial cases, the fundamental
nature of the setting implies one of those distributions.

You should make a strong eﬀort to get to the point at which you automatically rec-
ognize such settings when your encounter them.

4.8 Example: a Coin Game

Life is unfair—former President Jimmie Carter

Consider a game played by Jack and Jill. Each of them tosses a coin many times, but Jack gets a
head start of two tosses. So by the time Jack has had, for instance, 8 tosses, Jill has had only 6;
when Jack tosses for the 15th time, Jill has her 13th toss; etc.

Let Xk denote the number of heads Jack has gotten through his kth toss, and let Yk be the head
count for Jill at that same time, i.e. among only k-2 tosses for her. (So, Y1 = Y2 = 0.) Let’s ﬁnd
the probability that Jill is winning after the 6th toss, i.e. P (Y6 > X6).

Your ﬁrst reaction might be, “Aha, binomial distribution!” You would be on the right track, but
the problem is that you would not be thinking precisely enough. Just WHAT has a binomial
distribution? The answer is that both X6 and Y6 have binomial distributions, both with p = 0.5,
but n = 6 for X6 while n = 4 for Y6.

Now, as usual, ask the famous question, “How can it happen?” How can it happen that Y6 > X6?
Well, we could have, for example, Y6 = 3 and X6 = 1, as well as many other possibilities. Let’s
write it mathematically:

P (Y6 > X6) =

4
(cid:88)

i−1
(cid:88)

i=1

j=0

P (Y6 = i and X6 = j)

(4.48)

Make SURE your understand this equation.

86

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

Now, to evaluate P (Y6 = i and X6 = j), we see the “and” so we ask whether Y6 and X6 are
independent. They in fact are; Jill’s coin tosses certainly don’t aﬀect Jack’s. So,

P (Y6 = i and X6 = j) = P (Y6 = i) · P (X6 = j)

(4.49)

It is at this point that we ﬁnally use the fact that X6 and Y6 have binomial distributions. We have

and

P (Y6 = i) =

(cid:19)

(cid:18)4
i

0.5i(1 − 0.5)4−i

P (X6 = j) =

(cid:19)

0.5j(1 − 0.5)6−j

(cid:18)6
j

(4.50)

(4.51)

We would then substitute (4.50) and (4.51) in (4.48). We could then evaluate it by hand, but it
would be more convenient to use R’s dbinom() function:

1

2

3

4

5

prob <- 0
for (i in 1:4)

for (j in 0:(i-1))

prob <- prob + dbinom(i,4,0.5) * dbinom(j,6,0.5)

prob

We get an answer of about 0.17. If Jack and Jill were to play this game repeatedly, stopping each
time after the 6th toss, then Jill would win about 17% of the time.

4.9 Example: Tossing a Set of Four Coins

Consider a game in which we have a set of four coins. We keep tossing the set of four until we have
a situation in which exactly two of them come up heads. Let N denote the number of times we
must toss the set of four coins.

For instance, on the ﬁrst toss of the set of four, the outcome might be HTHH. The second might
be TTTH, and the third could be THHT. In the situation, N = 3.

Let’s ﬁnd P(N = 5). Here we recognize that N has a geometric distribution, with “success” deﬁned
as getting two heads in our set of four coins. What value does the parameter p have here?

4.10. EXAMPLE: THE ALOHA EXAMPLE AGAIN

87

Well, p is P(X = 2), where X is the number of heads we get from a toss of the set of four coins.
We recognize that X is binomial! Thus

p =

(cid:19)

0.54 =

(cid:18)4
2

3
8

(4.52)

Thus using the fact that N has a geometric distribution,

P (N = 5) = (1 − p)4p = 0.057

(4.53)

4.10 Example: the ALOHA Example Again

As an illustration of how commonly these parametric families arise, let’s again look at the ALOHA
example. Consider the general case, with transmission probability p, message creation probability
q, and m network nodes. We will not restrict our observation to just two epochs.

Suppose Xi = m, i.e. at the end of epoch i all nodes have a message to send. Then the number
which attempt to send during epoch i+1 will be binomially distributed, with parameters m and p.6
For instance, the probability that there is a successful transmission is equal to the probability that
exactly one of the m nodes attempts to send,

(cid:19)

(cid:18)m
1

p(1 − p)m−1 = mp(1 − p)m−1

(4.54)

Now in that same setting, Xi = m, let K be the number of epochs it will take before some message
actually gets through. In other words, we will have Xi = m, Xi+1 = m, Xi+2 = m,... but ﬁnally
Xi+K−1 = m − 1. Then K will be geometrically distributed, with success probability equal to
(4.54).

There is no Poisson distribution in this example, but it is central to the analysis of Ethernet, and
almost any other network. We will discuss this at various points in later chapters.

6Note that this is a conditional distribution, given Xi = m.

88

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

4.11 Example: the Bus Ridership Problem Again

Recall the bus ridership example of Section 2.12. Let’s calculate some expected values, for instance
E(B1):

E(B1) = 0 · P (B1 = 0) + 1 · P (B1 = 1) + 2 · P (B1 = 2) = 0.4 + 2 · 0.1

(4.55)

Now suppose the company charges $3 for passengers who board at the ﬁrst stop, but charges $2
for those who join at the second stop. (The latter passengers get a possibly shorter ride, thus pay
less.) So, the total revenue from the ﬁrst two stops is T = 3B1 + 2B2. Let’s ﬁnd E(T). We’d write

E(T ) = 3E(B1) + 2E(B2)

(4.56)

making use of (3.21). We’d then compute the terms as in 4.55.

Suppose the bus driver has the habit of exclaiming, “What? No new passengers?!” every time he
comes to a stop at which Bi = 0. Let N denote the number of the stop (1,2,...) at which this ﬁrst
occurs. Find P(N = 3):

N has a geometric distribution, with p equal to the probability that there 0 new passengers at a
stop, i.e. 0.5. Thus pN (3) = (1 − 0.5)20.5, by (4.3).

Let S denote the number of stops, out of the ﬁrst 6, at which 2 new passengers board. For example,
S would be 3 if B1 = 2, B2 = 2, B3 = 0, B4 = 1, B5 = 0, and B6 = 2. Find pS(4):

S has a binomial distribution, with n = 6 and p = probability of 2 new passengers at a stop = 0.1.
Then

pS(4) =

(cid:19)

(cid:18)6
4

0.14(1 − 0.1)6−4

(4.57)

By the way, we can exploit our knowledge of binomial distributions to simplify the simulation code
in Section 2.14.5. The lines

fo r ( k i n 1 : p a s s e n g e r s )

i f

( runif ( 1 ) < 0 . 2 )
p a s s e n g e r s <− p a s s e n g e r s − 1

simulate ﬁnding that number of passengers that alight at that stop. But that number is binomially
distributed, so the above code can be compactiﬁed (and speeded up in execution) as

p a s s e n g e r s <− p a s s e n g e r s − rbinom ( 1 , p a s s e n g e r s , 0 . 2 )

4.12. EXAMPLE: FLIPPING COINS WITH BONUSES

89

4.12 Example: Flipping Coins with Bonuses

A game involves ﬂipping a coin k times. Each time you get a head, you get a bonus ﬂip, not counted
among the k. (But if you get a head from a bonus ﬂip, that does not give you its own bonus ﬂip.)
Let X denote the number of heads you get among all ﬂips, bonus or not. Let’s ﬁnd the distribution
of X.

As with the parking space example above, we should be careful not to come to hasty conclusions.
The situation here “sounds” binomial, but X, based on a variable number of trials, doesn’t ﬁt the
deﬁnition of binomial.

But let Y denote the number of heads you obtain through nonbonus ﬂips. Y then has a binomial
distribution with parameters k and 0.5. To ﬁnd the distribution of X, we’ll condition on Y.

We will as usual ask, “How can it happen?”, but we need to take extra care in forming our sums,
recognizing constraints on Y:

• Y ≥ X/2

• Y ≤ X

• Y ≤ k

Keeping those points in mind, we have

pX (m) = P (X = m)

=

=

=

min(m,k)
(cid:88)

i=ceil(m/2)

min(m,k)
(cid:88)

i=ceil(m/2)

P (X = m and Y = i)

P (X = m|Y = i) P (Y = i)

min(m,k)
(cid:88)

(cid:18) i

i=ceil(m/2)

m − i

(cid:19)

0.5i

(cid:19)

0.5k

(cid:18)k
i

= 0.5k

min(m,k)
(cid:88)

i=ceil(m/2)

k!
(m − i)!(2i − m)!(k − i)!

0.5i

There doesn’t seem to be much further simpliﬁcation possible here.

(4.58)

(4.59)

(4.60)

(4.61)

(4.62)

90

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

4.13 Example: Analysis of Social Networks

Let’s continue our earlier discussion from Section 3.13.3.

One of the earliest—and now the simplest—models of social networks is due to Erd¨os and Renyi.
Say we have n people (or n Web sites, etc.), with (cid:0)n
(We are
2
assuming an undirected graph here.)
In this model, each potential link is an actual link with
probability p, and a nonlink with probability 1-p, with all the potential links being independent.

(cid:1) potential links between pairs.

Recall the notion of degree distribution from Section 3.13.3. Clearly the degree distribution Di
here for a single node i is binomial with parameters n-1 and p.

But consider k nodes, say 1 through k, among the n total nodes, and let T denote the number of
links involving these nodes. Let’s ﬁnd the distribution of T. That distribution is again binomial,
but the number of trials must be carefully calculated. We cannot simply say that, since each of the
k vertices can have as many as n-1 links, there are k(n-1) potential links, because there is overlap;
two nodes among the k have a potential link with each other, but we can’t count it twice. So, let’s
reason this out.
(cid:1) = 6 potential links, each on or
Say n = 9 and k = 4. Among the four special nodes, there are (cid:0)4
2
oﬀ with probability p, independently. Also each of the four special nodes has 9 − 4 = 5 potential
links with the “outside world,” i.e. the ﬁve non-special nodes. So there are 4 × 5 = 20 potential
links here, for a total of 26.

So, the distribution of T is binomial with

k(n − k) +

(cid:19)

(cid:18)k
2

(4.63)

trials and success probability p.

4.14 Multivariate Distributions

(I am borrowing some material here from Section 13.1, for instructors or readers who skip Chapter
13.
It is important to know that multivariate distributions exist, even if one doesn’t know the
details.)

Recall that for a single discrete random variable X, the distribution of X was deﬁned to be a list
of all the values of X, together with the probabilities of those values. The same is done for a pair
(or more than a pair) of discrete random variables U and V.

Suppose we have a bag containing two yellow marbles, three blue ones and four green ones. We

4.15.

ITERATED EXPECTATIONS

91

choose four marbles from the bag at random, without replacement. Let Y and B denote the number
of yellow and blue marbles that we get. Then deﬁne the two-dimensional pmf of Y and B to be

pY,B(i, j) = P (Y = i and B = j) =

(cid:0)2
i

(cid:1)(cid:0)3
j

(cid:1)(cid:0) 4

4−i−j
(cid:1)

(cid:0)9
4

(cid:1)

(4.64)

Here is a table displaying all the values of P(Y = i and B = j):

i ↓, j →
0
1
2

0
0.008
0.063
0.048

1
0.095
0.286
0.095

2
0.143
0.190
0.024

3
0.032
0.016
0.000

So this table is the distribution of the pair (Y,B).

Recall further that in the discrete case, we introduced a symbolic notation for the distribution of
a random variable X, deﬁned as pX (i) = P (X = i), where i ranged over the support of X. We do
the same thing for a pair of random variables:

Deﬁnition 8 For discrete random variables U and V, their probability mass function is deﬁned to
be

pU,V (i, j) = P (U = i and V = j)

(4.65)

where (i,j) ranges over all values taken on by (U,V). Higher-dimensional pmfs are deﬁned similarly,
e.g.

pU,V,W (i, j, k) = P (U = i and V = j and W = k)

(4.66)

So in our marble example above, pY,B(1, 2) = 0.048, pY,B(2, 0) = 0.012 and so on.

4.15 Iterated Expectations

This section has an abstract title, but the contents are quite useful.

92

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

4.15.1 Conditional Distributions

Just as we can deﬁne bivariate pmfs, we can also speak of conditional pmfs. Suppose we have
random variables U and V.

In our bus ridership example, for instance, we can talk about

E(L2 | B1 = 0)

(4.67)

In notebook terms, think of many replications of watching the bus during times 1 and 2. Then
(4.67) is deﬁned to be the long-run average of values in the L2 column, among those rows in
which the B1 column is 0. (And by the way, make sure you understand why (4.67) works out to
be 0.6.)

4.15.2 The Theorem

The key relation says, in essence,

The overall mean of V is a weighted average of the conditional means of V given U. The
weights are the pmf of U.

Note again that E(V | U = c) is deﬁned in “notebook” terms as the long-run average of V, among
those lines in which U = c.

Here is the formal version:

Suppose we have random variables U and V, with U discrete and with V having an expected value.
Then

E(V ) =

(cid:88)

c

P (U = c) E(V | U = c)

(4.68)

where c ranges through the support of U.

In spite of its intimidating form, (4.68) makes good intuitive sense, as follows: Suppose we want to
ﬁnd the average height of all students at a university. Each department measures the heights of its
majors, then reports the mean height among them. Then (4.68) says that to get the overall mean
in the entire school, we should take a weighted average of all the within-department means, with
the weights being the proportions of each department’s student numbers among the entire school.
Clearly, we would not want to take an unweighted average, as that would count tiny departments
just as much as large majors.

4.15.

ITERATED EXPECTATIONS

Here is the derivation (reader: supply the reasons!).

EV =

=

=

=

=

=

=

(cid:88)

d
(cid:88)

d
(cid:88)

d P (V = d)

(cid:88)

c
(cid:88)

d

d

P (U = c and V = d)

P (U = c) P (V = d | U = c)

d
(cid:88)

(cid:88)

d
(cid:88)

c
(cid:88)

c

d P (U = c) P (V = d | U = c)

d P (U = c) P (V = d | U = c)

d

P (U = c)

(cid:88)

d

d P (V = d | U = c)

P (U = c) E(V | U = c)

c
(cid:88)

c
(cid:88)

c

93

(4.69)

(4.70)

(4.71)

(4.72)

(4.73)

(4.74)

(4.75)

4.15.3 Example: Coin and Die Game

You roll a die until it comes up 5, taking M rolls to do so. You then toss a coin M times, winning
one dollar for each head. Find the expected winnings, EW.

Solution: Given M = k, the number of heads has a binomial distribution with n = k and p = 0.5.
So

So, from (4.68), we have

E(W |M = k) = 0.5k.

(4.76)

EW =

∞
(cid:88)

k=1

P (M = k) 0.5k = 0.5

∞
(cid:88)

k=1

P (M = k)k = 0.5 EM

(4.77)

from (3.13). And from (4.4), we know EM = 6. So, EW = 3.

4.15.4 Example: Flipping Coins with Bonuses

Recall the example in Section 4.12. The principle of iterated expectation can easily get us EX:

94

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

k
(cid:88)

i=1
k
(cid:88)

EX =

=

P (Y = i) E(X|Y = i)

P (Y = i) (i + 0.5i)

i=1
= E(1.5Y )

= 1.5 · k/2

= 0.75k

(4.78)

(4.79)

(4.80)

(4.81)

(4.82)

To understand that second equality, note that if Y = i, X will already include those i heads, the
nonbonus heads, and since there will be i bonus ﬂips, the expected value of the number of bonus
heads will be 0.5i. The third equality uses (3.36) (in a reverse manner).

Exercises

1. Consider a game in which one rolls a single die until one accumulates a total of at least four
dots. Let X denote the number of rolls needed. Find P (X ≤ 2) and E(X).

2. Recall the committee example in Section 3.9.2. Suppose now, though, that the selection protocol
is that there must be at least one man and at least one woman on the committee. Find E(D) and
V ar(D).

3. Suppose a bit stream is subject to errors, with each bit having probability p of error, and with
the bits being independent. Consider a set of four particular bits. Let X denote the number of
erroneous bits among those four.

(a) Find P(X = 2) and EX.

(b) What famous parametric family of distributions does the distribution of X belong to?

(c) Let Y denote the maximum number of consecutive erroneous bits. Find P(Y = 2) and Var(Y).

4. Derive (4.12).

5. Finish the computation in (4.18).

6. Derive the facts that for a Poisson-distributed random variable X with parameter λ, EX =
V ar(X) = λ. Use the hints in Section 4.5.

7. A civil engineer is collecting data on a certain road. She needs to have data on 25 trucks, and 10
percent of the vehicles on that road are trucks. State the famous parametric family that is relevant

4.15.

ITERATED EXPECTATIONS

95

here, and ﬁnd the probability that she will need to wait for more than 200 vehicles to pass before
she gets the needed data.

8. In the ALOHA example:

(a) Find E(X1) and V ar(X1), for the case p = 0.4, q = 0.8. You are welcome to use quantities
already computed in the text, e.g. P (X1 = 1) = 0.48, but be sure to cite equation numbers.

(b) Find P(collision during epoch 1) for general p, q.

9. Our experiment is to toss a nickel until we get a head, taking X rolls, and then toss a dime until
we get a head, taking Y tosses. Find:

(a) Var(X+Y).

(b) Long-run average in a “notebook” column labeled X 2.

10. Consider the game in Section 4.8. Find E(Z) and V ar(Z), where Z = Y6 − X6.

11. Say we choose six cards from a standard deck, one at a time WITHOUT replacement. Let N
be the number of kings we get. Does N have a binomial distribution? Choose one: (i) Yes. (ii)
No, since trials are not independent. (iii) No, since the probability of success is not constant from
trial to trial. (iv) No, since the number of trials is not ﬁxed. (v) (ii) and (iii). (iv) (ii) and (iv).
(vii) (iii) and (iv).

12. Suppose we have n independent trials, with the probability of success on the ith trial being pi.
Let X = the number of successes. Use the fact that “the variance of the sum is the sum of the
variance” for independent random variables to derive V ar(X).

13. Prove Equation (3.41).

14. Show that if X is a nonnegative-integer valued random variable, then

EX =

∞
(cid:88)

i=1

P (X ≥ i)

(4.83)

Hint: Write i = (cid:80)i

j=1 1, and when you see an iterated sum, reverse the order of summation.

15. Suppose we toss a fair coin n times, resulting in X heads. Show that the term expected value
is a misnomer, by showing that

lim
n→∞

P (X = n/2) = 0

(4.84)

96

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

Use Stirling’s approximation,

√

2πk

k! ≈

(cid:19)k

(cid:18) k
e

(4.85)

16. Suppose X and Y are independent random variables with standard deviations 3 and 4, respec-
tively.

(a) Find Var(X+Y).

(b) Find Var(2X+Y).

17. Fill in the blanks in the following simulation, which ﬁnds the approximate variance of N, the
number of rolls of a die needed to get the face having just one dot.

onesixth <- 1/6
sumn <- 0
sumn2 <- 0
for (i in 1:10000) {

n <- 0
while(TRUE) {

________________________________________
if (______________________________ < onesixth) break

}
sumn <- sumn + n
sumn2 <- sumn2 + n^2

}
approxvarn <- ____________________________________________
cat("the approx. value of Var(N) is ",approxvarn,"\n")

18. Let X be the total number of dots we get if we roll three dice. Find an upper bound for
P (X ≥ 15), using our course materials.

19. Suppose X and Y are independent random variables, and let Z = XY. Show that V ar(Z) =
E(X 2)E(Y 2) − [E(X)]2[E(Y )]2.

20. This problem involves a very simple model of the Web. (Far more complex ones exist.)

Suppose we have n Web sites. For each pair of sites i and j, i (cid:54)= j, there is a link from site i to site j
with probability p, and no link (in that direction) with probability 1-p. Let Ni denote the number
of sites that site i is linked to; note that Ni can range from 0 to n-1. Also, let Mij denote the
number of outgoing links that sites i and j have in common, not counting the one between them, if
any. Assume that each site forms its outgoing links independently of the others.

Say n = 10, p = 0.2. Find the following:

4.15.

ITERATED EXPECTATIONS

97

(a) P (N1 = 3)

(b) P (N1 = 3 and N2 = 2)

(c) V ar(N1)

(d) V ar(N1 + N2)

(e) P (M12 = 4)

Note: There are some good shortcuts in some of these problems, making the work much easier.
But you must JUSTIFY your work.

21. Let X denote the number of heads we get by tossing a coin 50 times. Consider Chebychev’s
Inequality for the case of 2 standard deviations. Compare the upper bound given by the inequality
to the exact probability.

22. Suppose the number N of cars arriving during a given time period at a toll booth has a
Poisson distribution with parameter λ. Each car has a probability p of being in a car pool. Let
M be the number of car-pool cars that arrive in the given period. Show that M also has a Poisson
distribution, with parameter pλ. (Hint: Use the Maclaurin series for ex.)

23. Consider a three-sided die, as on page 36. Let X denote the number of dots obtained in one
roll.

(a) (10) State the value of pX (2).

(b) (10) Find EX and Var(X).

(c) (15) Suppose you win $2 for each dot. Find EW, where W is the amount you win.

24. Consider the parking space problem in Section 4.2.2. Find Var(M), where M is the number of
empty spaces in the ﬁrst block, and Var(D).

25. Suppose X and Y are independent, with variances 1 and 2, respectively. Find the value of c
that minimizes Var[cX + (1-c)Y].

In the cards example in Section 2.15.1, let H denote the number of hearts. Find EH and

26.
Var(H).

27. In the bank example in Section 4.5, suppose you observe the bank for n days. Let X denote the
number of days in which at least 2 customers entered during the 11:00-11:15 observation period.
Find P(X = k).

28. Find E(X 3), where X has a geometric distribution with parameter p.

98

CHAPTER 4. DISCRETE PARAMETRIC DISTRIBUTION FAMILIES

29. Supppose we have a nonnegative random variable X, and deﬁne a new random variable Y,
which is equal to X if X > 8 and equal to 0 otherwise. Assume X takes on only a ﬁnite number of
values (just a mathematical nicety, not really an issue). Which one of the following is true:

(i) EY ≤ EX.

(ii) EY ≥ EX.

(iii) Either of EY and EX could be larger than the other, depending on the situation.

(iv) EY is undeﬁned.

30. Say we roll two dice, a blue one and a yellow one. Let B and Y denote the number of dots we
get, respectively, and write S = B + Y. Now let G denote the indicator random variable for the
event S = 2. Find E(G).

31. Consider the ALOHA example, Section 4.10 . Write a call to the built-in R function dbinom()
to evaluate (4.54) for general m and p.

32. Consider the bus ridership example, Section 2.12. Suppose upon arrival to a certain stop, there
are 2 passengers. Let A denote the number of them who choose to alight at that stop.

(a) State the parametric family that the distribution of A belongs to.

(b) Find pA(1) and FA(1), writing each answer in decimal expression form e.g. 128 · 0.32 + 0.3333.

33. Suppose you have a large disk farm, so heavily used that the lifetimes L are measured in
months. They come from two diﬀerent factories, in proportions q and 1-q. The disks from factory
i have geometrically distributed lifetime with parameter pi, i = 1,2. Find Var(L) in terms of q and
the pi.

34. Show how to eliminate the inner loop in the code at the end of Section 4.8.

Chapter 5

Pause to Reﬂect

Now that we have some basics, let’s step back a bit, and think about what we’ve learned.

5.1 A Cautionary Tale

Intuition is great, but it can lead you astray! The example in this section shows how things can go
wrong.

5.1.1 Trick Coins, Tricky Example

Suppose we have two trick coins in a box. They look identical, but one of them, denoted coin 1, is
heavily weighted toward heads, with a 0.9 probability of heads, while the other, denoted coin 2, is
biased in the opposite direction, with a 0.9 probability of tails. Let C1 and C2 denote the events
that we get coin 1 or coin 2, respectively.

Our experiment consists of choosing a coin at random from the box, and then tossing it n times.
Let Bi denote the outcome of the ith toss, i = 1,2,3,..., where Bi = 1 means heads and Bi = 0
means tails. Let Xi = B1 + ... + Bi, so Xi is a count of the number of heads obtained through the
ith toss.

The question is: “Does the random variable Xi have a binomial distribution?” Or, more simply,
the question is, “Are the random variables Bi independent?” To most people’s surprise, the answer
is No (to both questions). Why not?

The variables Bi are indeed 0-1 variables, and they have a common success probability. But they
are not independent! Let’s see why they aren’t.

99

100

CHAPTER 5. PAUSE TO REFLECT

Consider the events Ai = {Bi = 1}, i = 1,2,3,... In fact, just look at the ﬁrst two. By deﬁnition,
they are independent if and only if

P (A1 and A2) = P (A1)P (A2)

(5.1)

First, what is P (A1)? Now, wait a minute! Don’t answer, “Well, it depends on which coin
we get,” because this is NOT a conditional probability. Yes, the conditional probabilities P (A1|C1)
and P (A1|C2) are 0.9 and 0.1, respectively, but the unconditional probability is P (A1) = 0.5. You
can deduce that either by the symmetry of the situation, or by

P (A1) = P (C1)P (A1|C1) + P (C2)P (A1|C2) = (0.5)(0.9) + (0.5)(0.1) = 0.5

(5.2)

You should think of all this in the notebook context. Each line of the notebook would consist
of a report of three things: which coin we get; the outcome of the ﬁrst toss; and the outcome of
the second toss. (Note by the way that in our experiment we don’t know which coin we get, but
conceptually it should have a column in the notebook.) If we do this experiment for many, many
lines in the notebook, about 90% of the lines in which the coin column says “1” will show Heads
in the second column. But 50% of the lines overall will show Heads in that column.

So, the right hand side of Equation (5.1) is equal to 0.25. What about the left hand side?

P (A1 and A2) = P (A1 and A2 and C1) + P (A1 and A2 and C2)

= P (A1 and A2|C1)P (C1) + P (A1 andA2|C2)P (C2)
= (0.9)2(0.5) + (0.1)2(0.5)
= 0.41

(5.3)

(5.4)

(5.5)

(5.6)

Well, 0.41 is not equal to 0.25, so you can see that the events are not independent, contrary to our
ﬁrst intuition. And that also means that Xi is not binomial.

5.1.2

Intuition in Retrospect

To get some intuition here, think about what would happen if we tossed the chosen coin 10000
times instead of just twice. If the tosses were independent, then for example knowledge of the ﬁrst
9999 tosses should not tell us anything about the 10000th toss. But that is not the case at all.
After 9999 tosses, we are going to have a very good idea as to which coin we had chosen, because
by that time we will have gotten about 9000 heads (in the case of coin C1) or about 1000 heads
(in the case of C2). In the former case, we know that the 10000th toss is likely to be a head, while

5.2. WHAT ABOUT “ITERATED VARIANCE”?

101

in the latter case it is likely to be tails. In other words, earlier tosses do indeed give us
information about later tosses, so the tosses aren’t independent.

5.1.3

Implications for Modeling

The lesson to be learned is that independence can deﬁnitely be a tricky thing, not to be assumed
cavalierly. And in creating probability models of real systems, we must give very, very careful
thought to the conditional and unconditional aspects of our models—-it can make a huge diﬀerence,
as we saw above. Also, the conditional aspects often play a key role in formulating models of
nonindependence.

This trick coin example is just that—tricky—but similar situations occur often in real life. If in some
medical study, say, we sample people at random from the population, the people are independent
of each other. But if we sample families from the population, and then look at children within the
families, the children within a family are not independent of each other.

5.2 What About “Iterated Variance”?

In analogy to (4.68), one would think that

V ar(V ) =

(cid:88)

c

P (U = c) V ar(V | U = c)

(5.7)

In terms of the student heights example above, this wouldn’t make sense, because it doesn’t take into
account the variation in means from one department to another. (This matter, because V ar(V ) =
E[(V − EV )2].) So, it’s not surprising that another term must be added to (5.7). This topic is
discussed in Section ??, but for now the point is that although good intuition is essential, we must
not overrely on it..

5.3 Why Not Just Do All Analysis by Simulation?

Now that computer speeds are so fast, one might ask why we need to do mathematical probability
analysis; why not just do everything by simulation? There are a number of reasons:

• Even with a fast computer, simulations of complex systems can take days, weeks or even

months.

• Mathematical analysis can provide us with insights that may not be clear in simulation.

102

CHAPTER 5. PAUSE TO REFLECT

• Like all software, simulation programs are prone to bugs. The chance of having an uncaught
bug in a simulation program is reduced by doing mathematical analysis for a special case of
the system being simulated. This serves as a partial check.

• Statistical analysis is used in many professions, including engineering and computer science,
and in order to conduct meaningful, useful statistical analysis, one needs a ﬁrm understanding
of probability principles.

An example of that second point arose in the computer security research of a graduate student at
UCD, Senthilkumar Cheetancheri, who was working on a way to more quickly detect the spread
of a malicious computer worm. He was evaluating his proposed method by simulation, and found
that things “hit a wall” at a certain point. He wasn’t sure if this was a real limitation; maybe, for
example, he just wasn’t running his simulation on the right set of parameters to go beyond this
limit. But a mathematical analysis showed that the limit was indeed real.

5.4 Reconciliation of Math and Intuition (optional section)

Here is a more theoretical deﬁnition of probability, as opposed to the intuitive “notebook” idea in
this book. The deﬁnition is an abstraction of the notions of events (the sets A in W below) and
probabilities of those events (the values of the function P(A)):

Deﬁnition 9 Let S be a set, and let W be a collection of subsets of S. Let P be a real-valued
function on W. Then S, W and P form a probability space if the following conditions hold:

• P(S) = 1.

• S ∈ W.

• W is closed under complements (if a set is in W, then the set’s complement with respect to

S is in W too) and under unions of countably many members of W.

• P (A) ≥ 0 for any A in W.

• If A1, A2, ... ∈ W and the Ai are pairwise disjoint, then

P (∪iAi) =

(cid:88)

i

P (Ai)

(5.8)

A random variable is any function X : S → R.1

1The function must also have a property called measurability, which we will not discuss here.

5.4. RECONCILIATION OF MATH AND INTUITION (OPTIONAL SECTION)

103

Using just these simple axioms, one can prove (with lots of heavy math) theorems like the Strong
Law of Large Numbers:

Theorem 10 Consider a random variable U, and a sequence of independent random variables
U1, U2, ... which all have the same distribution as U, Then

lim
n→∞

U1 + ... + Un
n

= E(U ) with probability 1

(5.9)

In other words, the average value of U in all the lines of the notebook will indeed converge to EU.

Exercises

1. Consider a game in which one rolls a single die until one accumulates a total of at least four
dots. Let X denote the number of rolls needed. Find P (X ≤ 2) and E(X).

2. Recall the committee example in Section 3.9.2. Suppose now, though, that the selection protocol
is that there must be at least one man and at least one woman on the committee. Find E(D) and
V ar(D).

3. Suppose a bit stream is subject to errors, with each bit having probability p of error, and with
the bits being independent. Consider a set of four particular bits. Let X denote the number of
erroneous bits among those four.

(a) Find P(X = 2) and EX.

(b) What famous parametric family of distributions does the distribution of X belong to?

(c) Let Y denote the maximum number of consecutive erroneous bits. Find P(Y = 2) and Var(Y).

4. Derive (4.12).

5. Finish the computation in (4.18).

6. Derive the facts that for a Poisson-distributed random variable X with parameter λ, EX =
V ar(X) = λ. Use the hints in Section 4.5.

7. A civil engineer is collecting data on a certain road. She needs to have data on 25 trucks, and 10
percent of the vehicles on that road are trucks. State the famous parametric family that is relevant
here, and ﬁnd the probability that she will need to wait for more than 200 vehicles to pass before
she gets the needed data.

8. In the ALOHA example:

104

CHAPTER 5. PAUSE TO REFLECT

(a) Find E(X1) and V ar(X1), for the case p = 0.4, q = 0.8. You are welcome to use quantities
already computed in the text, e.g. P (X1 = 1) = 0.48, but be sure to cite equation numbers.

(b) Find P(collision during epoch 1) for general p, q.

9. Our experiment is to toss a nickel until we get a head, taking X rolls, and then toss a dime until
we get a head, taking Y tosses. Find:

(a) Var(X+Y).

(b) Long-run average in a “notebook” column labeled X 2.

10. Consider the game in Section 4.8. Find E(Z) and V ar(Z), where Z = Y6 − X6.

11. Say we choose six cards from a standard deck, one at a time WITHOUT replacement. Let N
be the number of kings we get. Does N have a binomial distribution? Choose one: (i) Yes. (ii)
No, since trials are not independent. (iii) No, since the probability of success is not constant from
trial to trial. (iv) No, since the number of trials is not ﬁxed. (v) (ii) and (iii). (iv) (ii) and (iv).
(vii) (iii) and (iv).

12. Suppose we have n independent trials, with the probability of success on the ith trial being pi.
Let X = the number of successes. Use the fact that “the variance of the sum is the sum of the
variance” for independent random variables to derive V ar(X).

13. Prove Equation (3.41).

14. Show that if X is a nonnegative-integer valued random variable, then

EX =

∞
(cid:88)

i=1

P (X ≥ i)

(5.10)

Hint: Write i = (cid:80)i

j=1 1, and when you see an iterated sum, reverse the order of summation.

15. Suppose we toss a fair coin n times, resulting in X heads. Show that the term expected value
is a misnomer, by showing that

Use Stirling’s approximation,

lim
n→∞

P (X = n/2) = 0

√

2πk

k! ≈

(cid:19)k

(cid:18) k
e

(5.11)

(5.12)

5.4. RECONCILIATION OF MATH AND INTUITION (OPTIONAL SECTION)

105

16. Suppose X and Y are independent random variables with standard deviations 3 and 4, respec-
tively.

(a) Find Var(X+Y).

(b) Find Var(2X+Y).

17. Fill in the blanks in the following simulation, which ﬁnds the approximate variance of N, the
number of rolls of a die needed to get the face having just one dot.

onesixth <- 1/6
sumn <- 0
sumn2 <- 0
for (i in 1:10000) {

n <- 0
while(TRUE) {

________________________________________
if (______________________________ < onesixth) break

}
sumn <- sumn + n
sumn2 <- sumn2 + n^2

}
approxvarn <- ____________________________________________
cat("the approx. value of Var(N) is ",approxvarn,"\n")

18. Let X be the total number of dots we get if we roll three dice. Find an upper bound for
P (X ≥ 15), using our course materials.

19. Suppose X and Y are independent random variables, and let Z = XY. Show that V ar(Z) =
E(X 2)E(Y 2) − [E(X)]2[E(Y )]2.

20. This problem involves a very simple model of the Web. (Far more complex ones exist.)

Suppose we have n Web sites. For each pair of sites i and j, i (cid:54)= j, there is a link from site i to site j
with probability p, and no link (in that direction) with probability 1-p. Let Ni denote the number
of sites that site i is linked to; note that Ni can range from 0 to n-1. Also, let Mij denote the
number of outgoing links that sites i and j have in common, not counting the one between them, if
any. Assume that each site forms its outgoing links independently of the others.

Say n = 10, p = 0.2. Find the following:

(a) P (N1 = 3)

(b) P (N1 = 3 and N2 = 2)

(c) V ar(N1)

106

CHAPTER 5. PAUSE TO REFLECT

(d) V ar(N1 + N2)

(e) P (M12 = 4)

Note: There are some good shortcuts in some of these problems, making the work much easier.
But you must JUSTIFY your work.

21. Let X denote the number of heads we get by tossing a coin 50 times. Consider Chebychev’s
Inequality for the case of 2 standard deviations. Compare the upper bound given by the inequality
to the exact probability.

22. Suppose the number N of cars arriving during a given time period at a toll booth has a
Poisson distribution with parameter λ. Each car has a probability p of being in a car pool. Let
M be the number of car-pool cars that arrive in the given period. Show that M also has a Poisson
distribution, with parameter pλ. (Hint: Use the Maclaurin series for ex.)

23. Consider a three-sided die, as on page 36. Let X denote the number of dots obtained in one
roll.

(a) (10) State the value of pX (2).

(b) (10) Find EX and Var(X).

(c) (15) Suppose you win $2 for each dot. Find EW, where W is the amount you win.

24. Consider the parking space problem in Section 4.2.2. Find Var(M), where M is the number of
empty spaces in the ﬁrst block, and Var(D).

25. Suppose X and Y are independent, with variances 1 and 2, respectively. Find the value of c
that minimizes Var[cX + (1-c)Y].

In the cards example in Section 2.15.1, let H denote the number of hearts. Find EH and

26.
Var(H).

27. In the bank example in Section 4.5, suppose you observe the bank for n days. Let X denote the
number of days in which at least 2 customers entered during the 11:00-11:15 observation period.
Find P(X = k).

28. Find E(X 3), where X has a geometric distribution with parameter p.

29. Supppose we have a nonnegative random variable X, and deﬁne a new random variable Y,
which is equal to X if X > 8 and equal to 0 otherwise. Assume X takes on only a ﬁnite number of
values (just a mathematical nicety, not really an issue). Which one of the following is true:

(i) EY ≤ EX.

5.4. RECONCILIATION OF MATH AND INTUITION (OPTIONAL SECTION)

107

(ii) EY ≥ EX.

(iii) Either of EY and EX could be larger than the other, depending on the situation.

(iv) EY is undeﬁned.

30. Say we roll two dice, a blue one and a yellow one. Let B and Y denote the number of dots we
get, respectively, and write S = B + Y. Now let G denote the indicator random variable for the
event S = 2. Find E(G).

31. Consider the ALOHA example, Section 4.10 . Write a call to the built-in R function dbinom()
to evaluate (4.54) for general m and p.

32. Consider the bus ridership example, Section 2.12. Suppose upon arrival to a certain stop, there
are 2 passengers. Let A denote the number of them who choose to alight at that stop.

(a) State the parametric family that the distribution of A belongs to.

(b) Find pA(1) and FA(1), writing each answer in decimal expression form e.g. 128 · 0.32 + 0.3333.

33. Suppose you have a large disk farm, so heavily used that the lifetimes L are measured in
months. They come from two diﬀerent factories, in proportions q and 1-q. The disks from factory
i have geometrically distributed lifetime with parameter pi, i = 1,2. Find Var(L) in terms of q and
the pi.

108

CHAPTER 5. PAUSE TO REFLECT

Chapter 6

Introduction to Discrete Markov
Chains

(From this point onward, we will be making heavy use of linear algebra. The reader may ﬁnd it
helpful to review Appendix B.)

Here we introduce Markov chains, a topic covered in much more detail in Chapter ??.

The basic idea is that we have random variables X1, X2, ..., with the index representing time. Each
one can take on any value in a given set, called the state space; Xn is then the state of the system
at time n. The state space is assumed either ﬁnite or countably inﬁnite.1

We sometimes also consider an initial state, X0, which might be modeled as either ﬁxed or random.
However, this seldom comes into play.

The key assumption is the Markov property, which in rough terms can be described as:

The probabilities of future states, given the present state and the past state, depends
only on the present state; the past is irrelevant.

In formal terms:

P (Xt+1 = st+1|Xt = st, Xt−1 = st−1, . . . , X0 = s0) = P (Xt+1 = st+1|Xt = st)

(6.1)

1The latter is a mathematical term meaning, in essence, that it is possible to denote the space using integer
subscripts. It can be shown that the set of all real numbers is not countably inﬁnite, though perhaps surprisingly the
set of all rational numbers is countably inﬁnite.

109

110

CHAPTER 6.

INTRODUCTION TO DISCRETE MARKOV CHAINS

Note that in (6.1), the two sides of the equation are equal but their common value may depend
on t. We assume that this is not the case; nondependence on t is known as stationarity.2 For
instance, the probability of going from state 2 to state 5 at time 29 is assumed to be the same as
the corresponding probability at time 333.

6.1 Matrix Formulation

We deﬁne pij to be the probability of going from state i to state j in one time step; note that this
is a conditional probability, i.e. P (Xn+1 = j | Xn = i). These quantities form a matrix P,3 whose
row i, column j element is pij, which is called the transition matrix. Each row of P must sum to
1 (do you see why?).

For example, consider a three-state Markov chain with transition matrix

P =





1
2
1
4
1

0
1
2
0





1
2
1
4
0

(6.2)

This means that if we are now at state 1, the probabilities of going to states 1, 2 and 3 are 1/2,
0 and 1/2, respectively. Note that each row’s probabilities must sum to 1—after all, from any
particular state, we must go somewhere.

Actually, the mth power, P m, of the transition matrix gives the probabilities for m-step transitions.
In other words, the (i,j) element of P m is P (Xt+m = j|Xt = i). This is clear for the case m = 2
(after which one can use mathematical induction), as follows.

As usual, “break big events down into small events.” How can it happen that Xt+2 = j? Well,
break things down according to where we might go ﬁrst after leaving i. We might go from i to 1,
say, then from 1 to j. So,

P (Xt+2 = j|Xt = i) =

(cid:88)

k

pik pkj

(6.3)

In view of the rule for multiplying matrices, the expression on the right-hand side is simply the (i,j)
element of P 2!

2Not to be confused with the notion of a stationary distribution, coming below.
3Unfortunately, we have some overloading of symbols here. Both in this book and in the ﬁeld in general, we
usually use the letter P to denote this matrix, yet we continue to denote probabilities by P( ). However, it will be
clear from context which we mean. The same is true for our transition probabilities pij, which use a subscripted
letter p, which is also the case for probably mass functions.

6.2. EXAMPLE: DIE GAME

111

6.2 Example: Die Game

As our ﬁrst example of Markov chains, consider the following game. One repeatedly rolls a die,
keeping a running total. Each time the total exceeds 10, we receive one dollar, and continue playing,
resuming where we left oﬀ, mod 10. Say for instance we have a total of 8, then roll a 5. We receive
a dollar, and now our total is 3.

It will simplify things if we assume that the player starts with one free point.

This process clearly satisﬁes the Markov property, with our state being our current total. If our
current total is 6, for instance, then the probability that we next have a total of 9 is 1/6, regardless
of what happened our previous rolls. We have p25, p72 and so on all equal to 1/6, while for instance
p29 = 0. Here’s the code to ﬁnd the transition matrix P:

s e t

them a l l

t o 0 f i r s t ,

s h o u l d be nonzero

i n 1 : 1 0 ) { # l o o k a t each row

so 10 x10 m a t r i x

o n e s i x t h <− 1/6
for ( i

1 # 10 s t a t e s ,
2 # s i n c e most e l e m e n t s w i l l be 0 s ,
3 # t h e n r e p l a c e t h o s e t h a t
4 p <− matrix ( rep ( 0 , 1 0 0 ) , nrow=10)
5
6
7
8
9
10
11
12
13
14 }

# s i n c e we a r e r o l l i n g a d i e ,
# can go t o from i ,
for ( j

( k > 1 0 ) k <− k − 10

i n 1 : 6 ) {

}

so c h e c k t h e s e

k <− i + j # new t o t a l , b u t d i d we win ?
i f
p [ i , k ] <− o n e s i x t h

t h e r e a r e o n l y 6 p o s s i b l e

s t a t e s we

Note that since we knew that many entries in the matrix would be zero, it was easier just to make
them all 0 ﬁrst, and then ﬁll in the nonzero ones.

6.3 Long-Run State Probabilities

Let Nit denote the number of times we have visited state i during times 1,...,t. For instance, in the
die game, N8,22 would be the number of rolls among the ﬁrst 22 that resulted in our having a total
of 8.

112

CHAPTER 6.

INTRODUCTION TO DISCRETE MARKOV CHAINS

Then as discussed in Section ??, in typical applications

πi = lim
t→∞

Nit
t

exists for each state i. Under a couple more conditions,4 we have the stronger result,

lim
t→∞

P (Xt = i) = πi

These quantities πi are typically the focus of analyses of Markov chains.

We will use the symbol π to name the column vector of all the πi:

π = (π1, π2, ...)(cid:48)

where (cid:48) means matrix transpose.

6.3.1 Stationary Distribution

(6.4)

(6.5)

(6.6)

The πi are called stationary probabilities, because if the initial state X0 is a random variable
with that distribution, then all Xi will have that distribution. Here’s why:

Using (6.5), we have

πi = lim
n→∞

= lim
n→∞

P (Xn = i)
(cid:88)

P (Xn−1 = k) pki

k

=

(cid:88)

k

πk pki

(6.7)

(6.8)

(6.9)

So, if P (X0 = i) = πi, then

4Basically, we need the chain to not be periodic. Consider a random walk, for instance: We start at position 0
on the number line, at time 0. The states are the integers. (So, this chain has an inﬁnite state space.) At each time,
we ﬂip a coin to determine whether to move right (heads) or left (tails) 1 unit. A little thought shows that if we start
at 0, the only times we can return to 0 are even-number times, i.e. P (Xn = 0 |X0 = 0) for all odd numbers n. This
is a periodic chain. By the way, (6.4) turns out to be 0 for this chain.

6.3. LONG-RUN STATE PROBABILITIES

P (X1 = i) =

=

(cid:88)

k
(cid:88)

P (X0 = k) pki

πk pki

k
= πi

113

(6.10)

(6.11)

(6.12)

this last using (6.9). So, if X0 has distribution π, then the same will be true for X1, and contiuing
in this manner we see that X2, X3, ... will all have that distribution, thus demostrating the claimed
stationary property for π.

Of course, (6.7) holds for all states i. So in matrix terms, (6.7) says

π(cid:48) = π(cid:48)P

(6.13)

6.3.2 Calculation of π

Equation (6.13) then shows us how to ﬁnd the πi, at least in the case of ﬁnite state spaces, the
subject of this section here, as follows.

First, rewrite (6.13)

(I − P (cid:48))π = 0

(6.14)

Here I is the n×n identity matrix (for a chain with n states), and again (cid:48) denotes matrix transpose.

This equation has inﬁnitely many solutions; if π is a solution, then so for example is 8π. Moreover,
the equation shows that the matrix there, I −P (cid:48), cannot have an inverse; if it did, we could multiply
both sides by the inverse, and ﬁnd that the unique solution is π = 0, which can’t be right. This
says in turn that the rows of I − P (cid:48) are not linearly independent.

The latter fact is quite important, for the following reason. Recall the close connection of matrix
inverse and systems of linear equations. (6.14), a matrix equation, represents a system of n linear
equations in n unknowns, the latter being π1, π2, .., πn. So, the lack of linear independence of the
rows of I − P (cid:48) means, in plain English, that at least one of those equations is redundant.

But we need n independent equations, and fortunately one is available:

(cid:88)

i

πi = 1

(6.15)

114

CHAPTER 6.

INTRODUCTION TO DISCRETE MARKOV CHAINS

Note that (6.15) can be written as

O(cid:48)π = 1

(6.16)

where O is a vector of n 1s. Excellent, let’s use it!

So, again, thinking of (6.14) as a system of linear equations, let’s replace the last equation by (6.16).
Switching back to the matrix view, that means that we replace the last row in the matrix I − P (cid:48)
in (6.14) by O(cid:48), and correspondingly replace the last element of the right-side vector by 1. Now
we have a nonzero vector on the right side, and a full-rank (i.e. invertible) matrix on the left side.
This is the basis for the following code, which we will use for ﬁnding π.

f i n d p i 1 <− function ( p ) {

n <− nrow( p )
# f i n d I−P’
imp <− diag ( n ) − t ( p )
# r e p l a c e t h e l a s t row o f
imp [ n , ] <− rep ( 1 , n )
# r e p l a c e t h e c o r r e s p o n d i n g e l e m e n t o f
# r i g h t
s i d e by ( t h e s c a l a r ) 1
r h s <− c ( rep ( 0 , n −1) ,1)
# now use R’ s b u i l t −i n s o l v e ( )
solve ( imp , r h s )

I−P’ as d i s c u s s e d

t h e

1
2
3
4
5
6
7
8
9
10
11
12 }

6.3.2.1 Example: π in Die Game

Consider the die game example above. Guess what! Applying the above code, all the πi turn out
to be 1/10. In retrospect, this should be obvious. If we were to draw the states 1 through 10 as a
ring, with 1 following 10, it should be clear that all the states are completely symmetric.

6.3.2.2 Another Way to Find π

Here is another way to compute π. It is not commonly used, but it will also help illustrate
some of the concepts.

Suppose (6.5) holds. Recall that P m is the m-step transition matrix, so that for instance row 1 of
that matrix is the set of probabilities of going from state 1 to the various states in m steps. The

6.4. EXAMPLE: 3-HEADS-IN-A-ROW GAME

same will be true for row 2 and so on. Putting that together with (6.5), we have that

P n = Π

lim
n→∞

115

(6.17)

where the n × n matrix Π has each of its rows equal to π(cid:48).

We can use this to ﬁnd π. We take P to a large power m, and then each of the rows will approximate
π. In fact, we can get an even better appoximation by averaging the rows.

Moreover, we can save a lot of computation by noting the following. Say we want the 16th power
of P. We could set up a loop with 15 iterations, building up a product. But actually we can do it
with just 4 iterations. We ﬁrst square P, yielding P 2. But then we square that, yielding P 4. Square
twice more, yielding P 8 and ﬁnally P 16. This is especially fast on a GPU (graphics processing
unit).

# f i n d s

s t a t i o n a r y p r o b a b i l i t i e s o f a Markov c h a i n u s i n g m a t r i x powers

a l t f i n d p i <− function ( p , k ) {

n i t e r s <− c e i l i n g ( log2 ( k ) )
prd <− p
for ( i

i n 1 : n i t e r s ) {
prd <− prd %∗% prd

}
colMeans ( prd )

}

This approach has the advantage of being easy to parallelize, unlike matrix inversion.

6.4 Example: 3-Heads-in-a-Row Game

How about the following game? We keep tossing a coin until we get three consecutive heads. What
is the expected value of the number of tosses we need?

We can model this as a Markov chain with states 0, 1, 2 and 3, where state i means that we have
accumulated i consecutive heads so far. If we simply stop playing the game when we reach state 3,
that state would be known as an absorbing state, one that we never leave.

We could proceed on this basis, but to keep things elementary, let’s just model the game as being
played repeatedly, as in the die game above. You’ll see that that will still allow us to answer the
original question. Note that now that we are taking that approach, it will suﬃce to have just three

116

CHAPTER 6.

INTRODUCTION TO DISCRETE MARKOV CHAINS

states, 0, 1 and 2; there is no state 3, because as soon as we win, we immediately start a new game,
in state 0.

6.4.1 Markov Analysis

Clearly we have transition probabilities such as p01, p12, p10 and so on all equal to 1/2. Note from
state 2 we can only go to state 0, so p20 = 1.

Here’s the code below. Of course, since R subscripts start at 1 instead of 0, we must recode our
states as 1, 2 an 3.

p <- matrix(rep(0,9),nrow=3)
p[1,1] <- 0.5
p[1,2] <- 0.5
p[2,3] <- 0.5
p[2,1] <- 0.5
p[3,1] <- 1
findpi1(p)

It turns out that

π = (0.5714286, 0.2857143, 0.1428571)

(6.18)

So, in the long run, about 57.1% of our tosses will be done while in state 0, 28.6% while in state 1,
and 14.3% in state 2.

Now, look at that latter ﬁgure. Of the tosses we do while in state 2, half will be heads, so half will
be wins. In other words, about 0.071 of our tosses will be wins. And THAT ﬁgure answers our
original question, through the following reasoning:

Think of, say, 10000 tosses. There will be about 710 wins sprinkled among those 10000 tosses.
Thus the average number of tosses between wins will be about 10000/710 = 14.1. In other words,
the expected time until we get three consecutive heads is about 14.1 tosses.

6.5 A Modiﬁed Notebook Analysis

Our previous notebook analysis (and most of our future ones, other than for Markov chains), relied
on imagining performing many independent replications of the same experiment.

6.6. SIMULATION OF MARKOV CHAINS

117

6.5.1 A Markov-Chain Notebook

Consider Table 2.3, for instance. There our experiment was to watch the network during epochs 1
and 2. So, on the ﬁrst line of the notebook, we would watch the network during epochs 1 and 2
and record the result. On the second line, we watch a new, independent replication of the network
during epochs 1 and 2, and record the results.

But instead of imagining a notebook recording inﬁnitely many replications of the two epochs, we
could imagine watching just one replication but over inﬁnitely many epochs. We’d watch the
network during epoch 1, epoch 2, epoch 3 and so on. Now one line of the notebook would record
one epoch.

For general Markov chains, each line would record one time step. We would have columns of the
notebook labeled n and Xn. The reason this approach would be natural is (6.4). In that context,
πi would be the long-run proportion of notebook lines in which Xn = i.

6.5.2 Example: 3-Heads-in-a-Row Game

For instance, consider the 3-heads-in-a-row game. Then (6.18) says that about 57% of the notebook
lines would have a 0 in the Xn column, with about 29% and 14% of the lines showing 1 and 2,
respectively.

Moreover, suppose we also have a notebook column labeled Win, with an entry Yes in a certain
line meaning, yes, that coin ﬂip resulted in a win, with a No entry meaning no win. Then the mean
time until a win, which we found to be 14.1 above, would be described in notebook terms as the
long-run average number of lines between Yes entries in the Win column.

6.6 Simulation of Markov Chains

Following up on Section 6.5, recall that our previous simulations have basically put into code
form our notebook concept. Our simulations up until now have been based on the deﬁnition of
probability, which we had way back in Section 2.3. Our simulation code modeled the notebook
independent replications notion. We can do a similar thing now, based on the ideas in Section 6.5.

In a time series kind of situation such as Markov chains, since we are interested in long-run behavior
in the sense of time, our simulation is based on (6.5). In other words, we simulate the evolution of
Xn as n increases, and take long-run averages of whatever is of interest.

Here is simulation code for the example in Section 6.4, caculating the approximate value of the
long-run time between wins (found to be about 14.1 by mathematical means above):

118

CHAPTER 6.

INTRODUCTION TO DISCRETE MARKOV CHAINS

# s i m u l a t i o n o f 3−in−a−row c o i n t o s s gaem

t h r e e i n r o w <− function ( n t i m e s t e p s ) {

c o n s e c <− 0 # number o f c o n s e c Hs
nwins <− 0 # number o f wins
w i n t i m es <− 0 # t o t a l o f
s t a r t p l a y <− 0 # time s t e p 0
fo r ( i

i n 1 : n t i m e s t e p s ) {

t i m e s

t o win

i f

( t o s s ( ) == ’H ’ ) {
c o n s e c <− c o n s e c + 1
( c o n s e c == 3 ) {
i f
nwins <− nwins + 1
w in ti m es <−

w in ti m es + i − s t a r t p l a y

c o n s e c <− 0
s t a r t p l a y <− i

}

} e l s e c o n s e c <− 0

}
w in ti m es / nwins

}

t o s s <− function ( )

i f

( runif ( 1 ) < 0 . 5 )

’H ’ e l s e ’T ’

l

6.7 Example: ALOHA

Consider our old friend, the ALOHA network model. (You may wish to review the statement of the
model in Section 2.5 before continuing.) The key point in that system is that it was “memoryless,”
in that the probability of what happens at time k+1 depends only on the state of the system at
time k.

For instance, consider what might happen at time 6 if X5 = 2. Recall that the latter means that
at the end of epoch 5, both of our two network nodes were active. The possibilities for X6 are then

• X6 will be 2 again, with probability p2 + (1 − p)2

• X6 will be 1, with probability 2p(1 − p)

6.7. EXAMPLE: ALOHA

119

The central point here is that the past history of the system—i.e. the values of X1, X2, X3, and X4—
don’t have any impact. We can state that precisely:

The quantity

P (X6 = j|X1 = i1, X2 = i2, X3 = i3, X4 = i4, X5 = i)

(6.19)

does not depend on im, m = 1, ..., 4. Thus we can write (6.19) simply as P (X6 = j|X5 =
i).

Furthermore, that probability is the same as P (X9 = j|X8 = i) and in general P (Xk+1 = j|Xk = i).
So, we do have a Markov chain.

Since this is a three-state chain, the pij form a 3x3 matrix:





P =

(1 − q)2 + 2q(1 − q)p 2q(1 − q)(1 − p) + 2q2p(1 − p)

(1 − q)p
0

2qp(1 − p) + (1 − q)(1 − p)
2p(1 − p)

q2[p2 + (1 − p)2]
q[p2 + (1 − p)2]
p2 + (1 − p)2





(6.20)

For instance, the element in row 0, column 2, p02, is q2[p2 + (1 − p)2], reﬂecting the fact that to go
from state 0 to state 2 would require that both inactive nodes become active (which has probability
q2, and then either both try to send or both refrain from sending (probability p2 + (1 − p)2.

For the ALOHA example here, with p = 0.4 and q = 0.3, the solution is π0 = 0.47, π1 = 0.43 and
π2 = 0.10.

So we know that in the long run, about 47% of the epochs will have no active nodes, 43% will have
one, and 10% will have two. From this we see that the long-run average number of active nodes is

0 · 0.47 + 1 · 0.43 + 2 · 0.10 = 0.63

(6.21)

By the way, note that every row in a transition matrix must sum to 1. (The probability that we
go from state i to somewhere is 1, after all, so we must have (cid:80)
j pij = 1.) That implies that we can
save some work in writing R code; the last column must be 1 minus the others. In our example
above, we would write

transmat <- matrix(rep(0,9),nrow=3)
p1 <- 1 - p
q1 <- 1 - q
transmat[1,1] <- q1^2 + 2 * q * q1 *p
transmat[1,2] <- 2 * q * q1 * p1 + 2 * q^2 * p * p1
transmat[2,1] <- q1 * p

120

CHAPTER 6.

INTRODUCTION TO DISCRETE MARKOV CHAINS

transmat[2,2] <- 2 * q * p * p1 + q1 * p1
transmat[3,1] <- 0
transmat[3,2] <- 2 * p * p1
transmat[,3] <- 1 - p[,1] - p[,2]
findpi1(transmat)

Note the vectorized addition and recycling (Section 2.14.2).

6.8 Example: Bus Ridership Problem

Consider the bus ridership problem in Section 2.12. Make the same assumptions now, but add a
new one: There is a maximum capacity of 20 passengers on the bus.

The random variables Li, i = 1,2,3,... form a Markov chain. Let’s look at some of the transition
probabilities:

p00 = 0.5

p01 = 0.4

p11 = (1 − 0.2) · 0.5 + 0.2 · 0.4

p20 = (0.2)2(0.5) = 0.02

(6.22)

(6.23)

(6.24)

(6.25)

p20,20 = (0.8)20(0.5 + 0.4 + 0.1) +

(cid:19)

(cid:18)20
1

(0.2)1(0.8)20−1(0.4 + 0.1) +

(cid:18)20
2

(cid:19)

(0.2)2(0.8)18(0.1) (6.26)

(Note that for clarity, there is a comma in p20,20, as p2020 would be confusing and in some other
examples even ambiguous. A comma is not necessary in p11, since there must be two subscripts;
the 11 here can’t be eleven.)

After ﬁnding the π vector as above, we can ﬁnd quantities such as the long-run average number of
passengers on the bus,

20
(cid:88)

i=0

πii

(6.27)

6.8. EXAMPLE: BUS RIDERSHIP PROBLEM

121

We can also compute the long-run average number of would-be passengers who fail to board the
bus. Denote by Ai denote the number of passengers on the bus as it arrives at stop i. The key
point is that since Ai = Li−1, then (6.4) and (6.5) will give the same result, no matter whether we
look at the Lj chain or the Aj chain.

Now, armed with that knowledge, let Dj denote the number of disappointed people at stop j. Then

EDj = 1 · P (Dj = 1) + 2 · P (Dj = 2).

(6.28)

That latter probability, for instance, is

P (Dj = 2) = P (Aj = 20 and Bj = 2 and Gj = 0)

= P (Aj = 20) P (Bj = 2) P (Gj = 0)
= P (Aj = 20) · 0.1 · 0.820

(6.29)

(6.30)

(6.31)

where as before Bj is the number who wish to board at stop j, and Gj is the number who get oﬀ
the bus at that stop. Using the same reasoning, one can ﬁnd P (Dj = 1). (A number of cases to
consider, left as an exercise for the reader.)

Taking the limits as j → ∞, we have the long-run average number of disappointed customers on
the left, and on the right, the terms P (Aj = 19) and P (Aj = 20) go to π19 and π20, which we have
and thus can obtain the value regarding disappointed customers.

Let’s ﬁnd the long-run average number of customers who alight from the bus. This can be done by
considering all the various cases, but (4.68) really shortens our work:

EGn =

20
(cid:88)

i=0

P (An = i)E(Gn|An = i)

(6.32)

Given An = i, Gn has a binomial distribution with i trials and success probability 0.2, so

So, the right-hand side of 6.32 converges to

E(Gn|An = i) = i · 0.2

20
(cid:88)

i=0

πii · 0.2

In other words, the long-run average number alighting is 0.2 times (6.27).

(6.33)

(6.34)

122

CHAPTER 6.

INTRODUCTION TO DISCRETE MARKOV CHAINS

6.9 Example: an Inventory Model

Consider the following simple inventory model. A store has 1 or 2 customers for a certain item
each day, with probabilities v and w (v+w = 1). Each customer is allowed to buy only 1 item.

When the stock on hand reaches 0 on a day, it is replenished to r items immediately after the store
closes that day.

If at the start of a day the stock is only 1 item and 2 customers wish to buy the item, only one
customer will complete the purchase, and the other customer will leave emptyhanded.

Let Xn be the stock on hand at the end of day n (after replenishment, if any). Then X1, X2, ...
form a Markov chain, with state space 1,2,...,r.

The transition probabilities are easy to ﬁnd. Take p21, for instance. If there is a stock of 2 items
at the end of one day, what is the (conditional) probability that there is only 1 item at the end of
the next day? Well, for this to happen, there would have to be just 1 customer coming in, not 2,
and that has probability v. So, p21 = v. The same reasoning shows that p2r = w.

Let’s write a function inventory(v,w,r) that returns the π vector for this Markov chain. It will
call ﬁndpi1(), similarly to the two code snippets on page 116. For convenience, let’s assume r is
at least 3.5

i n v e n t o r y <− function ( v , w, r ) {

tm <− matrix ( rep ( 0 , r ˆ 2 ) ,nrow=r )
for ( i

i n 3 : r ) {

tm [ i , i −1] <− v
tm [ i , i −2] <− w

}
tm [ 2 , 1 ] <− v
tm [ 2 , r ] <− w
tm [ 1 , r ] <− 1
f i n d p i 1 (tm)

1
2
3
4
5
6
7
8
9
10
11 }

6.10 Expected Hitting Times

Consider an n-state Markov chain that is irreducible, meaning that it is possible to get from any
state i to any other state j in some number of steps. Deﬁne the random variable Tij to be the time

5If r is 2, then the expression 3:2 in the code evaluates to the vector (3,2), which would not be what we want in

this case.

6.10. EXPECTED HITTING TIMES

123

needed to go from state i to state j. (Note that Tii is NOT 0, though it can be 1 if pii > 0.)

In many applications, we are interested in the mean time to reach a certain state, given that we
start at some speciﬁed state. In other words, we wish to calculate ETij.

The material in Section 4.15 is useful here. In (4.68), take V = Tij and take U to be the ﬁrst state
we visit after leaving state i (which could be i again). Then

E(Tij) =

(cid:88)

k

pikE(Tij | U = k), 1 ≤ i, j ≤ n

(6.35)

Consider what happens if U = m (cid:54)= j. Then we will have already used up 1 time step, and still
will need an average of ET kj more steps to get to j. In other words,

E(Tij | U = k (cid:54)= j) = 1 + ETkj

(6.36)

Did you notice the Markov property being invoked?

On the other hand, the case U = j is simple:

E(Tij | U = j) = 1

(6.37)

This then implies that

E(Tij) = 1 +

(cid:88)

k(cid:54)=j

pikE(Tkj), 1 ≤ i, j ≤ n

(6.38)

We’ll focus on the case j = n, i.e. look at how long it takes to get to state n.
(We could of
course do the same analysis for any other destination state.) Let ηi denote E(Tin), and deﬁne
η = (η1, η2, ..., ηn−1)(cid:48). (Note that η has only n − 1 components!) So,

ηi = 1 +

(cid:88)

k<n

pikηk, 1 ≤ i, j ≤ n

(6.39)

Equation (6.39) is a system of linear equations, which we can write in matrix form. Then the code
to solve the system is (remember, we have only an (n − 1) × (n − 1) system)

f i n d e t a <− function ( p ) {

n <− nrow( p )
q <− diag ( n ) − p
q <− q [ 1 : ( n − 1 ) , 1 : ( n −1)]

CHAPTER 6.

INTRODUCTION TO DISCRETE MARKOV CHAINS

o n e s <− rep ( 1 , n−1)
solve ( q , o n e s )

124

}

Chapter 7

Continuous Probability Models

There are other types of random variables besides the discrete ones you studied in Chapter 3.
This chapter will cover another major class, continuous random variables, which form the heart of
statistics and are used extensively in applied probability as well. It is for such random variables
that the calculus prerequisite for this book is needed.

7.1 Running Example: a Random Dart

Imagine that we throw a dart at random at the interval (0,1). Let D denote the spot we hit. By “at
random” we mean that all subintervals of equal length are equally likely to get hit. For instance,
the probability of the dart landing in (0.7,0.8) is the same as for (0.2,0.3), (0.537,0.637) and so on.

Because of that randomness,

P (u ≤ D ≤ v) = v − u

(7.1)

for any case of 0 ≤ u < v ≤ 1.

We call D a continuous random variable, because its support is a continuum of points, in this
case, the entire interval (0,1).

125

126

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

7.2 Individual Values Now Have Probability Zero

The ﬁrst crucial point to note is that

for any individual point c. This may seem counterintuitive, but it can be seen in a couple of ways:

P (D = c) = 0

(7.2)

• Take for example the case c = 0.3. Then

P (D = 0.3) ≤ P (0.29 ≤ D ≤ 0.31) = 0.02

(7.3)

the last equality coming from (7.1).

So, P (D = 0.3) ≤ 0.02. But we can replace 0.29 and 0.31 in (7.3) by 0.299 and 0.301, say,
and get P (D = 0.3) ≤ 0.002. So, P (D = 0.3) must be smaller than any positive number, and
thus it’s actually 0.

• Reason that there are inﬁnitely many points, and if they all had some nonzero probability
w, say, then the probabilities would sum to inﬁnity instead of to 1; thus they must have
probability 0.

Similarly, one will see that (7.2) will hold for any continuous random variable.

Remember, we have been looking at probability as being the long-run fraction of the time an event
occurs, in inﬁnitely many repetitions of our experiment—the “notebook” view. So (7.2) doesn’t
say that D = c can’t occur; it merely says that it happens so rarely that the long-run fraction of
occurrence is 0.

7.3 But Now We Have a Problem

But Equation (7.2) presents a problem. In the case of discrete random variables M, we deﬁned
their distribution via their probability mass function, pM . Recall that Section 3.13 deﬁned this as
a list of the values M takes on, together with their probabilities. But that would be impossible in
the continuous case—all the probabilities of individual values here are 0.

So our goal will be to develop another kind of function, which is similar to probability mass functions
in spirit, but circumvents the problem of individual values having probability 0. To do this, we
ﬁrst must deﬁne another key function:

7.3. BUT NOW WE HAVE A PROBLEM

127

7.3.1 Our Way Out of the Problem: Cumulative Distribution Functions

Deﬁnition 11 For any random variable W (including discrete ones), its cumulative distribu-
tion function (cdf ), FW , is deﬁned by

FW (t) = P (W ≤ t), −∞ < t < ∞

(7.4)

(Please keep in mind the notation. It is customary to use capital F to denote a cdf, with a subscript
consisting of the name of the random variable.)

What is t here? It’s simply an argument to a function. The function here has domain (−∞, ∞),
and we must thus deﬁne that function for every value of t. This is a simple point, but a crucial
one.

For an example of a cdf, consider our “random dart” example above. We know that, for example
for t = 0.23,

FD(0.23) = P (D ≤ 0.23) = P (0 ≤ D ≤ 0.23) = 0.23

(7.5)

Also,

and

FD(−10.23) = P (D ≤ −10.23) = 0

FD(10.23) = P (D ≤ 10.23) = 1

(7.6)

(7.7)

Note that the fact that D can never be equal to 10.23 or anywhere near it is irrelevant. FD(t) is
deﬁned for all t in (−∞, ∞), including 10.23! The deﬁnition of FD(10.23) is P (D ≤ 10.23)), and
that probability is 1! Yes, D is always less than or equal to 10.23, right?

In general for our dart,

FD(t) =


0,

t,

1,

if t ≤ 0
if 0 < t < 1
if t ≥ 1

(7.8)

Here is the graph of FD:

128

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

The cdf of a discrete random variable is deﬁned as in Equation (7.4) too. For example, say Z is the
number of heads we get from two tosses of a coin. Then

FZ(t) =

For instance,






0,
0.25,
0.75,
1,

if t < 0
if 0 ≤ t < 1
if 1 ≤ t < 2
if t ≥ 2

FZ(1.2) = P (Z ≤ 1.2)

= P (Z = 0 or Z = 1)

= 0.25 + 0.50

= 0.75

(7.9)

(7.10)

(7.11)

(7.12)

(7.13)

Note that (7.11) is simply a matter of asking our famous question, “How can it happen?” Here we
are asking how it can happen that Z ≤ 1.2. The answer is simple: That can happen if Z is 0 or 1.
The fact that Z cannot equal 1.2 is irrelevant.

(7.12) uses the fact that Z has a binomial distribution with n = 2 and p = 0.5.

−0.50.00.51.01.50.00.20.40.60.81.0tF(t)7.3. BUT NOW WE HAVE A PROBLEM

129

FZ is graphed below.

The fact that one cannot get a noninteger number of heads is what makes the cdf of Z ﬂat between
consecutive integers.

In the graphs you see that FD in (7.8) is continuous while FZ in (7.9) has jumps. This is another
reason we call random variables such as D continuous random variables.

At this level of study of probability, random variables are either discrete or continuous. But some
exist that are neither. We won’t see any random variables from the “neither” case here, and they
occur rather rarely in practice.

Armed with cdfs, let’s turn to the original goal, which was to ﬁnd something for continuous random
variables that is similar in spirit to probability mass functions for discrete random variables.

7.3.2 Density Functions

Intuition is key here. Make SURE you develop a good intuitive understanding of density functions,
as it is vital in being able to apply probability well. We will use it a lot in our course.

−0.50.00.51.01.52.02.50.00.20.40.60.81.0tF(t)130

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

(The reader may wish to review pmfs in Section 3.13.)

Think as follows. From (7.4) we can see that for a discrete random variable, its cdf can be calculated
by summing its pmf. Recall that in the continuous world, we integrate instead of sum. So, our
continuous-case analog of the pmf should be something that integrates to the cdf. That of course
is the derivative of the cdf, which is called the density:

Deﬁnition 12 Consider a continuous random variable W. Deﬁne

fW (t) =

d
dt

FW (t), −∞ < t < ∞

(7.14)

wherever the derivative exists. The function fW is called the probability density function (pdf ),
or just the density of W.

(Please keep in mind the notation. It is customary to use lower-case f to denote a density, with a
subscript consisting of the name of the random variable.)

But what is a density function? First and foremost, it is a tool for ﬁnding probabilities involving
continuous random variables:

7.3.3 Properties of Densities

Equation (7.14) implies

Property A:

P (a < W ≤ b) = FW (b) − FW (a)

=

(cid:90) b

a

fW (t) dt

(7.15)

(7.16)

Where does (7.15) come from? Well, FW (b) is all the probability accumulated from −∞ to b, while
FW (a) is all the probability accumulated from −∞ to a. The diﬀerence is the probability that X
is between a and b.

(7.16) is just the Fundamental Theorem of Calculus: Integrate the derivative of a function, and
you get the original function back again.

Since P(W = c) = 0 for any single point c, Property A also means:

7.3. BUT NOW WE HAVE A PROBLEM

131

Property B:

P (a < W ≤ b) = P (a ≤ W ≤ b) = P (a ≤ W < b) = P (a < W < b) =

(cid:90) b

a

fW (t) dt

(7.17)

This in turn implies:

Property C:

(cid:90) ∞

−∞

fW (t) dt = 1

(7.18)

Note that in the above integral, fW (t) will be 0 in various ranges of t corresponding to values W
cannot take on. For the dart example, for instance, this will be the case for t < 0 and t > 1.

Any nonnegative function that integrates to 1 is a density. A density could be increasing, decreasing
or mixed. Note too that a density can have values larger than 1 at some points, even though it
must integrate to 1.

7.3.4

Intuitive Meaning of Densities

Suppose we have some continuous random variable X, with density fX , graphed in Figure 7.1.

Let’s think about probabilities of the form

P (s − 0.1 < X < s + 0.1)

(7.19)

Let’s ﬁrst consider the case of s = 1.3.

The rectangular strip in the picture should remind you of your early days in calculus. What the
picture says is that the area under fX from 1.2 to 1.4 (i.e. 1.3 ± 0.1) is approximately equal to the
area of the rectangle. In other words,

2(0.1)fX (1.3) ≈

(cid:90) 1.4

1.2

fX (t) dt

(7.20)

But from our Properties above, we can write this as

P (1.2 < X < 1.4) ≈ 2(0.1)fX (1.3)

(7.21)

132

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

Similarly, for s = 0.4,

and in general

This reasoning shows that:

P (0.3 < X < 0.5) ≈ 2(0.1)fX (0.4)

(7.22)

P (s − 0.1 < X < s + 0.1) ≈ 2(0.1)fX (s)

(7.23)

Regions in the number line (X-axis in the picture) with low density have low probabilities
while regions with high density have high probabilities.

So, although densities themselves are not probabilities, they do tell us which regions will occur
often or rarely. For the random variable X in our picture, there will be many lines in the notebook
in which X is near 1.3, but many fewer in which X is near 0.4.

7.3.5 Expected Values

What about E(W)? Recall that if W were discrete, we’d have

E(W ) =

(cid:88)

c

cpW (c)

(7.24)

where the sum ranges overall all values c that W can take on. If for example W is the number of
dots we get in rolling two dice, c will range over the values 2,3,...,12.

So, the analog for continuous W is:

Property D:

E(W ) =

(cid:90)

t

tfW (t) dt

(7.25)

where here t ranges over the values W can take on, such as the interval (0,1) in the dart case.
Again, we can also write this as

E(W ) =

(cid:90) ∞

−∞

tfW (t) dt

(7.26)

7.3. BUT NOW WE HAVE A PROBLEM

133

Figure 7.1: Approximation of Probability by a Rectangle

0.00.51.01.52.00.00.20.40.60.8xf (x)f_X134

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

in view of the previous comment that fW (t) might be 0 for various ranges of t.

And of course,

and in general, similarly to (3.36):

Property E:

E(W 2) =

E[g(W )] =

(cid:90)

t

(cid:90)

t

t2fW (t) dt

(7.27)

g(t)fW (t) dt

(7.28)

Most of the properties of expected value and variance stated previously for discrete random variables
hold for continuous ones too:

Property F:

Equations (3.19), (3.21), (3.25), (3.41) and (3.49) still hold in the continuous case.

7.4 A First Example

Consider the density function equal to 2t/15 on the interval (1,4), 0 elsewhere. Say X has this
density. Here are some computations we can do:

EX =

(cid:90) 4

1

t · 2t/15 dt = 2.8

P (X > 2.5) =

(cid:90) 4

2.5

2t/15 dt = 0.65

(7.29)

(7.30)

FX (s) =

(cid:90) s

1

2t/15 dt =

s2 − 1
15

for s in (1,4) (cdf is 0 for t < 1, and 1 for t > 4)

(7.31)

V ar(X) = E(X 2) − (EX)2
(cid:90) 4

(from (3.41)

t22t/15 dt − 2.82

(from (7.29))

=

1

= 0.66

(7.32)

(7.33)

(7.34)

7.5. THE NOTION OF SUPPORT IN THE CONTINUOUS CASE

135

Suppose L is the lifetime of a light bulb (say in years), with the density that X has above. Let’s
ﬁnd some quantities in that context:

Proportion of bulbs with lifetime less than the mean lifetime:

P (L < 2.8) =

(cid:90) 2.8

1

2t/15 dt = (2.82 − 1)/15

(7.35)

Mean of 1/L:

E(1/L) =

(cid:90) 4

1

1
t

· 2t/15 dt =

2
5

(7.36)

In testing many bulbs, mean number of bulbs that it takes to ﬁnd two that have
lifetimes longer than 2.5:

Use (4.36) with r = 2 and p = 0.65.

7.5 The Notion of Support in the Continuous Case

Recall from Section 3.2 that the support of a discrete distribution is its “domain.” If for instance
X is the number of heads I get from 3 tosses of a coin, X can only take on the values 0, 1, 2 and 3.
We say that that set is the support of this distribution; 8, for example, is not in the support.

The notion extends to continuous random variables.
there is the interval (1,4).

In Section 7.4, the support of the density

7.6 Famous Parametric Families of Continuous Distributions

7.6.1 The Uniform Distributions

7.6.1.1 Density and Properties

In our dart example, we can imagine throwing the dart at the interval (q,r) (so this will be a
two-parameter family). Then to be a uniform distribution, i.e. with all the points being “equally
likely,” the density must be constant in that interval. But it also must integrate to 1 [see (7.18).

136

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

So, that constant must be 1 divided by the length of the interval:

fD(t) =

1
r − q

(7.37)

for t in (q,r), 0 elsewhere.

It easily shown that E(D) = q+r

2 and V ar(D) = 1

12 (r − q)2.

The notation for this family is U(q,r).

7.6.1.2 R Functions

Relevant functions for a uniformly distributed random variable X on (r,s) are:

• dunif(x,r,s), to ﬁnd fX (x)

• punif(q,r,s), to ﬁnd P (X ≤ q)

• qunif(q,r,s), to ﬁnd c such that P (X ≤ c) = q

• runif(n,r,s), to generate n independent values of X

As with most such distribution-related functions in R, x and q can be vectors, so that punif() for
instance can be used to ﬁnd the cdf values at multiple points.

7.6.1.3 Example: Modeling of Disk Performance

Uniform distributions are often used to model computer disk requests. Recall that a disk consists
of a large number of concentric rings, called tracks. When a program issues a request to read or
write a ﬁle, the read/write head must be positioned above the track of the ﬁrst part of the ﬁle.
This move, which is called a seek, can be a signiﬁcant factor in disk performance in large systems,
e.g. a database for a bank.

If the number of tracks is large, the position of the read/write head, which I’ll denote as X, is like
a continuous random variable, and often this position is modeled by a uniform distribution. This
situation may hold just before a defragmentation operation. After that operation, the ﬁles tend
to be bunched together in the central tracks of the disk, so as to reduce seek time, and X will not
have a uniform distribution anymore.

Each track consists of a certain number of sectors of a given size, say 512 bytes each. Once the
read/write head reaches the proper track, we must wait for the desired sector to rotate around and

7.6. FAMOUS PARAMETRIC FAMILIES OF CONTINUOUS DISTRIBUTIONS

137

pass under the read/write head. It should be clear that a uniform distribution is a good model for
this rotational delay.

For example, suppose in modeling disk performance, we describe the position X of the read/write
head as a number between 0 and 1, representing the innermost and outermost tracks, respectively.
Say we assume X has a uniform distribution on (0,1), as discussed above. Consider two consecutive
positions (i.e. due to two consecutive seeks), X1 and X2, which we’ll assume are independent. Let’s
ﬁnd V ar(X1 + X2).

We know from Section 7.6.1.1 that the variance of a U(0,1) distribution is 1/12. Then by indepen-
dence,

V ar(X1 + X2) = 1/12 + 1/12 = 1/6

(7.38)

7.6.1.4 Example: Modeling of Denial-of-Service Attack

In one facet of computer security, it has been found that a uniform distribution is actually a
warning of trouble, a possible indication of a denial-of-service attack. Here the attacker tries to
monopolize, say, a Web server, by inundating it with service requests. According to the research of
David Marchette,1 attackers choose uniformly distributed false IP addresses, a pattern not normally
seen at servers.

7.6.2 The Normal (Gaussian) Family of Continuous Distributions

These are the famous “bell-shaped curves,” so called because their densities have that shape.2

7.6.2.1 Density and Properties

Density and Parameters:

The density for a normal distribution is

fW (t) =

√

1
2πσ

e−0.5( t−µ

σ )2

, −∞ < t < ∞

(7.39)

1Statistical Methods for Network and Computer Security, David J. Marchette, Naval Surface Warfare Center,

rion.math.iastate.edu/IA/2003/foils/marchette.pdf.

2All that glitters is not gold”—Shakespeare
Note that other parametric families, notably the Cauchy, also have bell shapes. The diﬀerence lies in the rate at
which the tails of the distribution go to 0. However, due to the Central Limit Theorem, to be presented below, the
normal family is of prime interest.

138

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

Again, this is a two-parameter family, indexed by the parameters µ and σ, which turn out to be
the mean3 and standard deviation µ and σ, The notation for it is N (µ, σ2) (it is customary to state
the variance σ2 rather than the standard deviation).

The normal family is so important that we have a special chapter on it, Chapter 8.

7.6.3 The Exponential Family of Distributions

Please note: We have been talking here of parametric families of distributions, and in this section
will introduce one of the most famous, the family of exponential distributions. This should not be
confused, though, with the term exponential family that arises in mathematical statistics, which
includes exponential distributions but is much broader.

7.6.3.1 Density and Properties

The densities in this family have the form

fW (t) = λe−λt, 0 < t < ∞

(7.40)

This is a one-parameter family of distributions.

After integration, one ﬁnds that E(W ) = 1
λ2 . You might wonder why it is
customary to index the family via λ rather than 1/λ (see (7.40)), since the latter is the mean. But
this is actually quite natural, for reasons discussed in Section 9.1.

λ and V ar(W ) = 1

7.6.3.2 R Functions

Relevant functions for a uniformly distributed random variable X with parameter λ are

• dexp(x,lambda), to ﬁnd fX (x)

• pexp(q,lambda), to ﬁnd P (X ≤ q)

• qexp(q,lambda), to ﬁnd c such that P (X ≤ c) = q

• rexp(n,lambda), to generate n independent values of X

3Remember, this is a synonym for expected value.

7.6. FAMOUS PARAMETRIC FAMILIES OF CONTINUOUS DISTRIBUTIONS

139

7.6.3.3 Example: Refunds on Failed Components

Suppose a manufacturer of some electronic component ﬁnds that its lifetime L is exponentially
distributed with mean 10000 hours. They give a refund if the item fails before 500 hours. Let M
be the number of items they have sold, up to and including the one on which they make the ﬁrst
refund. Let’s ﬁnd EM and V ar(M ).

First, notice that M has a geometric distribution! It is the number of independent trials until the
ﬁrst success, where a “trial” is one component, “success” (no value judgment, remember) is giving
a refund, and the success probability is

P (L < 500) =

(cid:90) 500

0

0.0001e−0.0001t dt = 0.05

(7.41)

Then plug p = 0.05 into (4.11) and (4.12).

7.6.3.4 Example: Garage Parking Fees

A certain public parking garage charges parking fees of $1.50 for the ﬁrst hour, and $1 per hour
after that. (It is assumed here for simplicity that the time after the ﬁrst hour is prorated. The
reader should consider how the analysis would change if the garage “rounds up” each partial hour.)
Suppose parking times T are exponentially distributed with mean 1.5 hours. Let W denote the
total fee paid. Let’s ﬁnd E(W) and Var(W).

The key point is that W is a function of T:

W =

(cid:40)

1.5T,
1.5 + 1 · (T − 1) = T + 0.5,

if T ≤ 1
if T > 1

(7.42)

That’s good, because we know how to ﬁnd the expected value of a function of a continuous random
variable, from (7.28). Deﬁning g() as in (7.42) above, we have

EW =

(cid:90) ∞

0

g(t)

1
1.5

e− 1

1.5 tdt =

(cid:90) 1

0

1.5t

1
1.5

e− 1

1.5 tdt +

(cid:90) ∞

1

(t + 0.5)

1
1.5

e− 1

1.5 tdt

(7.43)

The integration is left to the reader.

Now, what about Var(W)? As is often the case, it’s easier to use (3.41), so we need to ﬁnd E(W 2).

140

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

The above integration becomes

E(W 2) =

(cid:90) ∞

0

g2(t)

1
1.5

e− 1

1.5 tdt =

(cid:90) 1

0

1.52t

1
1.5

e− 1

1.5 tdt +

(cid:90) ∞

1

(t + 0.5)2 1
1.5

e− 1

1.5 tdt

(7.44)

After evaluating this, we subtract (EW )2, giving us the variance of W.

7.6.3.5

Importance in Modeling

Many distributions in real life have been found to be approximately exponentially distributed.
A famous example is the lifetimes of air conditioners on airplanes. Another famous example is
interarrival times, such as customers coming into a bank or messages going out onto a computer
network. It is used in software reliability studies too.

One of the reasons why this family is used so widely in probabilistic modeling is that it has several
remarkable properties, so many that we have a special chapter for this family, Chapter 9.

7.6.4 The Gamma Family of Distributions

7.6.4.1 Density and Properties

Suppose at time 0 we install a light bulb in a lamp, which burns X1 amount of time. We immediately
install a new bulb then, which burns for time X2, and so on. Assume the Xi are independent random
variables having an exponential distribution with parameter λ.

Let

Tr = X1 + ... + Xr, r = 1, 2, 3, ...

(7.45)

Note that the random variable Tr is the time of the rth light bulb replacement. Tr is the sum of r
independent exponentially distributed random variables with parameter λ. The distribution of Tr
is called an Erlang distribution. Its density can be shown to be

fTr (t) =

1
(r − 1)!

λrtr−1e−λt, t > 0

(7.46)

This is a two-parameter family.

Again, it’s helpful to think in “notebook” terms. Say r = 8. Then we watch the lamp for the
durations of eight lightbulbs, recording T8, the time at which the eighth burns out. We write that

7.6. FAMOUS PARAMETRIC FAMILIES OF CONTINUOUS DISTRIBUTIONS

141

time in the ﬁrst line of our notebook. Then we watch a new batch of eight bulbs, and write the
value of T8 for those bulbs in the second line of our notebook, and so on. Then after recording a
very large number of lines in our notebook, we plot a histogram of all the T8 values. The point is
then that that histogram will look like (7.46).

We can generalize this by allowing r to take noninteger values, by deﬁning a generalization of the
factorial function:

Γ(r) =

(cid:90) ∞

0

xr−1e−x dx

(7.47)

This is called the gamma function, and it gives us the gamma family of distributions, more general
than the Erlang:

fW (t) =

1
Γ(r)

λrtr−1e−λt, t > 0

(7.48)

(Note that Γ(r) is merely serving as the constant that makes the density integrate to 1.0. It doesn’t
have meaning of its own.)

This is again a two-parameter family, with r and λ as parameters.

A gamma distribution has mean r/λ and variance r/λ2. In the case of integer r, this follows from
(9.1) and the fact that an exponentially distributed random variable has mean and variance 1/λ
and variance 1/λ2, and it can be derived in general. Note again that the gamma reduces to the
exponential when r = 1.

7.6.4.2 Example: Network Buﬀer

Suppose in a network context (not our ALOHA example), a node does not transmit until it has
accumulated ﬁve messages in its buﬀer. Suppose the times between message arrivals are independent
and exponentially distributed with mean 100 milliseconds. Let’s ﬁnd the probability that more than
552 ms will pass before a transmission is made, starting with an empty buﬀer.

Let X1 be the time until the ﬁrst message arrives, X2 the time from then to the arrival of the
second message, and so on. Then the time until we accumulate ﬁve messages is Y = X1 + ... + X5.
Then from the deﬁnition of the gamma family, we see that Y has a gamma distribution with r =
5 and λ = 0.01. Then

P (Y > 552) =

(cid:90) ∞

552

1
4!

0.015t4e−0.01t dt

(7.49)

142

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

This integral could be evaluated via repeated integration by parts, but let’s use R instead:

> 1 - pgamma(552,5,0.01)
[1] 0.3544101

Note that our parameter r is called shape in R, and our λ is rate.

Again, there are also dgamma(), qgamma() and rgamma(). From the R man page:

Usage :

dgamma( x ,
pgamma( q ,

shape ,
shape ,

r a t e = 1 , s c a l e = 1/ r a t e ,
r a t e = 1 , s c a l e = 1/ r a t e ,

log = FALSE)
lower . t a i l = TRUE,

log . p = FALSE)

qgamma( p ,

shape ,

r a t e = 1 , s c a l e = 1/ r a t e ,

lower . t a i l = TRUE,

log . p = FALSE)

rgamma( n ,

shape ,

r a t e = 1 , s c a l e = 1/ r a t e )

7.6.4.3

Importance in Modeling

As seen in (9.1), sums of exponentially distributed random variables often arise in applications.
Such sums have gamma distributions.

You may ask what the meaning is of a gamma distribution in the case of noninteger r. There is
no particular meaning, but when we have a real data set, we often wish to summarize it by ﬁtting
a parametric family to it, meaning that we try to ﬁnd a member of the family that approximates
our data well.

In this regard, the gamma family provides us with densities which rise near t = 0, then gradually
decrease to 0 as t becomes large, so the family is useful if our data seem to look like this. Graphs
of some gamma densities are shown in Figure 7.2.

As you might guess from the network performance analysis example in Section 7.6.4.2, the gamma
family does arise often in the network context, and in queuing analysis in general.

7.6.5 The Beta Family of Distributions

As seen in Figure 7.2, the gamma family is a good choice to consider if our data are nonnegative,
with the density having a peak near 0 and then gradually tapering oﬀ to the right. What about
data in the range (0,1)?

7.6. FAMOUS PARAMETRIC FAMILIES OF CONTINUOUS DISTRIBUTIONS

143

Figure 7.2: Various Gamma Densities

0510152025300.00.10.20.3xy010r = 1.0r = 5.0r = 10.0lambda = 1.0144

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

Figure 7.3: Beta Density, α = 0.2, β = 0.2

For instance, say trucking company transports many things, including furniture. Let X be the
proportion of a truckload that consists of furniture. For instance, if 15% of given truckload is
furniture, then X = 0.15. So here we have a distribution with support in (0,1). The beta family
provides a very ﬂexible model for this kind of setting, allowing us to model many diﬀerent concave
up or concave down curves.

7.6.5.1 Density Etc.

The densities of the family have the following form:

Γ(α + β)
Γ(α)Γ(β)

tα−1(1 − t)β−1

(7.50)

There are two parameters, α and β. Figures 7.3 and 7.4 show two possibilities.

0.00.20.40.60.81.01234xdbeta(x, 0.2, 0.2)7.6. FAMOUS PARAMETRIC FAMILIES OF CONTINUOUS DISTRIBUTIONS

145

Figure 7.4: Beta Density, α = 2.0, β = 3.0

0.00.20.40.60.81.00.00.51.01.5xdbeta(x, 2, 3)146

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

The mean and variance are

and

α
α + β

αβ
(α + β)2(α + β + 1)

(7.51)

(7.52)

Again, there are also dbeta(), qbeta() and rbeta(). From the R man page:

Usage :

dbeta ( x ,
pbeta ( q ,
qbeta ( p ,
rbeta ( n ,

shape1 ,
shape1 ,
shape1 ,
shape1 ,

shape2 , ncp = 0 ,
shape2 , ncp = 0 ,
shape2 , ncp = 0 ,
shape2 , ncp = 0 )

log = FALSE)
lower . t a i l = TRUE,
lower . t a i l = TRUE,

log . p = FALSE)
log . p = FALSE)

The graphs mentioned above were generated by running

> curve ( dbeta ( x , 0 . 2 , 0 . 2 ) )
> curve ( dbeta ( x , 2 , 3 )

7.6.5.2

Importance in Modeling

As mentioned, the beta family is a natural candidate for modeling a variable having range the
interval (0,1).

This family is also popular among Bayesian statisticians (Section 18.4).

7.7 Choosing a Model

The parametric families presented here are often used in the real world. As indicated previously,
this may be done on an empirical basis. We would collect data on a random variable X, and plot
the frequencies of its values in a histogram. If for example the plot looks roughly like the curves in
Figure 7.2, we could choose this as the family for our model.

Or, our choice may arise from theory. If for instance our knowledge of the setting in which we
are working says that our distribution is memoryless, that forces us to use the exponential density
family.

7.8. FINDING THE DENSITY OF A FUNCTION OF A RANDOM VARIABLE

147

In either case, the question as to which member of the family we choose will be settled by using
some kind of procedure which ﬁnds the member of the family which best ﬁts our data. We will
discuss this in detail in our chapters on statistics, especially Chapter 21.

Note that we may choose not to use a parametric family at all. We may simply ﬁnd that our data
does not ﬁt any of the common parametric families (there are many others than those presented
here) very well. Procedures that do not assume any parametric family are termed nonparametric.

7.8 Finding the Density of a Function of a Random Variable

Suppose X has, say, a uniform distribution on (1,4). Form a new random variable, Y = X 2. How
can we ﬁnd fY ()? Reason as follows. For 1 < t < 16,

fY (t) =

=

=

=

d
dt
d
dt
d
dt
d
dt

FY (t) (def. of density)

P (Y ≤ t) (def. of cdf)

√

P (X ≤

t) (def. of Y)

√

FX (
√

t) (def. of cdf)

√

t (Chain Rule)

d
d

= fX (

t)

=

t−0.5

1
6

(7.53)

(7.54)

(7.55)

(7.56)

(7.57)

(7.58)

Other such settings can be handeled similarly. Note, though, that the above derivation relied on
the fact that X > 0. Suppose X has a uniform distribution on (-1,1). Then the above derivation
would become, for 0 < t < 1,

fY (t) =

P (Y ≤ t)

√

t ≤ X ≤

√

t)

=

d
dt
d
dt
d
dt
= t−0.5

=

P (−

√
2

t

(7.59)

(7.60)

(7.61)

(7.62)

148

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

7.9 Quantile Functions

First, recall the deﬁnition of the inverse of a function, say h(). The inverse of h(), denoted h−1(),
“brings you back to where you started.” If I plug 3 into the squaring function, I get 9, and if I
then plug 9 into the square root function, I get back my original 3.4 So we say that h(t) = t2 and
k(s) =

s are inverses of each other. The same relation holds between exp() and ln() and so on.

√

For a random variable X, its quantile function QX (s) is deﬁned by

QX (s) = F −1

X (s)

(7.63)

This is called the s quantile of X.

A well-known example is the median of X, the point which half the probability is above and half
below. It’s the 0.5 quantile of X.

The cdf tells us that cumulative probability for a particular value of X, while the quantile function
does the opposite, “bringing us back.” Let’s make this concrete by considering the random variable
Z in Section 7.3.1.

On the one hand, we can ask the question, “What is the probability that Z is at most 1?”, with
the answer being

FZ(1) = 0.75

(7.64)

On the other hand, one can ask conversely, “At what value of X do we have cumulative probability
of 0.75?” Here the answer is

QZ(0.75) = 1

(7.65)

It is no coincidence that the word quantile has that -ile suﬃx, given your familiarity with the word
percentile. They are really the same thing.

Suppose for example that 92% of all who take the SAT Math Test have scores of at most 725. In
other words, if you look at a randomly chosen test paper, and let X denote its score, then

FX (725) = P (X ≤ 725) = 0.92

(7.66)

4This assumes, of course, that the domain of my squaring function consists only of the nonnegative numbers.

We’ll put aside this and similar situations for the time being, but will return.

7.10. USING CDF FUNCTIONS TO FIND PROBABILITIES

149

On the other hand, if you are interested in the 92nd percentile score for the test, you are saying
“Find s such that P (X ≤ s) = 0.92” you want

QX (0.92) = 725

(7.67)

The reader should note that the R functions we’ve seen beginning with the letter ’q’, such as
qgeom() and qunif(), are quantile functions, hence the name.

However, a problem with discrete random variables is that the quantile values may not be unique.
The reader should verify, for instance, in the coin-toss example above, the 0.75 quantile for Z could
be not only 1, but also 1.1, 1.288 and so on. So, one should look carefully at the documentation of
quantile functions, to see what they do to impose uniqueness. But for continuous random variables
there is no such problem.

7.10 Using cdf Functions to Find Probabilities

As we have seen, for many parametric families R has “d/p/q/r” functions, giving the density, cdf,
quantile function and random number generator for the given family. How can we use the cdf
functions to ﬁnd probabilities of the form P (a < X < b)?

We see the answer in (7.15). We simply evaluate the cdf at b then a, and subtract.

For instance, consider the network buﬀer example, Section 7.6.4.2. Let’s ﬁnd P (540 < Y < 562):

> pgamma( 5 6 2 , 5 , 0 . 0 1 ) − pgamma( 5 4 0 , 5 , 0 . 0 1 )
[ 1 ] 0 . 0 3 4 1 8 2 6 4

Of course, we could also integrate the density,

> i n t e g r a t e ( function ( t ) dgamma( t , 5 , 0 . 0 1 ) , 5 4 0 , 5 6 2 )
0 . 0 3 4 1 8 2 6 4 with a b s o l u t e e r r o r < 3 . 8 e −16

but R does that for us, there is probably little point in that second approach.

7.11 A General Method for Simulating a Random Variable

Suppose we wish to simulate a random variable X with density fX for which there is no R function.
This can be done via F −1
X (U ), where U has a U(0,1) distribution. In other words, we call runif()
and then plug the result into the inverse of the cdf of X.

150

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

For example, say X has the density 2t on (0,1). Then FX (t) = t2, so F −1(s) = s0.5. We can then
generate an X as sqrt(runif(1)). Here’s why:

For brevity, denote F −1

X as G. Our generated random variable is then Y = G(U). Then

FY (t) = P [G(U ) ≤ t]

= P [U ≤ G−1(t)]
= P [U ≤ FX (t)]
= FX (t)

(7.68)

(7.69)

(7.70)

(7.71)

(this last coming from the fact that U has a uniform distribution on (0,1)).

In other words, Y and X have the same cdf, i.e. the same distribution! This is exactly what we
want.

Note that this method, though valid, is not necessarily practical, since computing F −1
easy.

X may not be

7.12 Example: Writing a Set of R Functions for a Certain Power

Family

Consider the family of distributions indexed by positive values of c with densities

c tc−1

(7.72)

for t in (0,1) and 0 otherwise..

The cdf is tc, so let’s call this the “tc” family.

Let’s ﬁnd “d”, “p”, “q” and “r” functions for this family, just like R has for the normal family, the
gamma family and so on:

# d e n s i t y
d t c <− function ( x , c ) c ∗ x ˆ ( c−1)

# c d f
p t c <− function ( x , c ) xˆc

# q u a n t i l e f u n c t i o n

7.13. MULTIVARIATE DENSITIES

151

q t c <− function ( q , c ) qˆ ( 1 /c )

# random number g e n e r a t o r , n v a l u e s g e n e r a t e d
r t c <− function ( n , c ) {
tmp <− runif ( n )
q t c ( tmp , c )

}

Note that to get rtc() we simply plug U(0,1) variates into qtc(), according to Section 7.11.

Let’s check our work. The mean for the density having c equal to 2 is 2/3 (reader should verify);
let’s see if a simulation will give us that:

> mean( r t c ( 1 0 0 0 0 , 2 ) )
[ 1 ] 0 . 6 6 9 6 9 4 1

Sure enough!

7.13 Multivariate Densities

Section 4.14 brieﬂy introduced the notion of multivariate pmfs. Similarly, there are also multivariate
densities. Probabilities are then k-fold integrals, where k is the number of random variables.

For instance, a probability involving two variables means taking a double integral of a bivariate
density. Since that density can be viewed as a surface in three-dimensional space (just as a univariate
density is viewed as a curve in two-dimensional space), a probability is then a volume under that
surface (as opposed to area in the univariate case). Conversely, a bivariate density is the mixed
partial derivative of the cdf:

fX,Y (u, v) =

∂2
∂u∂v

FX,Y (u, v) = P (X ≤ u, Y ≤ v)

(7.73)

In analogy to

P (B | A) =

P (B and A)
P (A)

we can deﬁne the conditional density of Y given X:

fY |X (u, v) =

fX,Y (u, v)
fX (v)

(7.74)

(7.75)

152

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

The intuition behind this is that we are conditioning on X being near v. Actually,

fY |X (u, v) = lim
h→0

[density of Y | X ∈ (v − h, v + h)]

(7.76)

A detailed treatment is presented in Chapter 13.

7.14 Iterated Expectations

In analogy with (4.68), we have a very useful corresponding formula for the continuous case.

7.14.1 The Theorem

For any random variable W and any continuous random variable V,5

E(W ) =

(cid:90) ∞

−∞

fV (t) E(W | V = t) dt

(7.77)

Note that the event V = t has probability 0 for coninuous V. The conditional expectation here is
deﬁned in terms of the conditional distribution of W given V; see Section 7.13.

Note too that if we have some event A, we can set W above to the indicator random variable of A
(recall (3.9)), yielding

P (A) =

(cid:90) ∞

−∞

fV (t) P (A | V = t) dt

(7.78)

7.14.2 Example: Another Coin Game

Suppose we have biased coins of various weightings, so that a randomly chosen coin’s probability
of heads H has density 2t on (0,1). The game has you choose a coin at random, toss it 5 times,
and pays you a prize if you get 5 heads. What is your probability of winning?

First, note that the probability of winning, given H = t, is t5. then (7.78) tells us that

P (win) =

(cid:90) 1

0

2t t5 dt =

2
7

(7.79)

5The treatment here will be intuitive, rather than being a mathematical deﬁnition and proof.

7.15. CONTINUOUS RANDOM VARIABLES ARE “USEFUL UNICORNS”

153

7.15 Continuous Random Variables Are “Useful Unicorns”

Recall our random dart example at the outset of this chapter. It must be kept in mind that this is
only an idealization. D actually cannot be just any old point in (0,1). To begin with, our measuring
instrument has only ﬁnite precision. Actually, then, D can only take on a ﬁnite number of values.
If, say, our precision is four decimal digits, then D can only be 0.0001, 0.0002, ..., 0.9999, making
it a discrete random variable after all.6

So this modeling of the position of the dart as continuously distributed really is just an idealization.
Indeed, in practice there are NO continuous random variables. But the coninuous model can be an
excellent approximation, and the concept is extremely useful. It’s like the assumption of “massless
string” in physics analyses; there is no such thing, but it’s a good approximation to reality.

As noted, most applications of statistics, and many of probability, are based on continuous distri-
butions. We’ll be using them heavily for the remainder of this book.

Exercises

1. Fill in the blanks, in the following statements about continuous random variables. Make sure
to use our book’s notation.

(a) d

dt P (X ≤ t) =

(b) P (a < X < b) =

−

2. Suppose X has a uniform distribution on (-1,1), and let Y = X 2. Find fY .

3. Suppose X has an exponential distribution with parameter λ. Show that EX = 1/λ and
V ar(X) = 1/λ2.

4. Suppose fX (t) = 3t2 for t in (0,1) and is zero elsewhere. Find FX (0.5) and E(X).

5. Suppose light bulb lifetimes X are exponentially distributed with mean 100 hours.

(a) Find the probability that a light bulb burns out before 25.8 hours.

In the remaining parts, suppose we have two light bulbs. We install the ﬁrst at time 0, and then
when it burns out, immediately replace it with the second.

(b) Find the probability that the ﬁrst light bulb lasts less than 25.8 hours and the lifetime of the

second is more than 120 hours.

6There are also issues such as the nonzero thickness of the dart, and so on, further restricting our measurement.

154

CHAPTER 7. CONTINUOUS PROBABILITY MODELS

(c) Find the probability that the second burnout occurs after time 192.5.

6. Suppose for some continuous random variable X, fX (t) is equal to 2(1-t) for t in (0,1) and is 0
elsewhere.

(a) Why is the constant here 2? Why not, say, 168?

(b) Find FX (0.2) and Var(X).

(c) Using the method in Section 7.11, write an R function, named oneminust(), that generates a
random variate sampled from this distribution. Then use this function to verify your answers
in (b) above.

7. The company Wrong Turn Criminal Mismanagement makes predictions every day. They tend
to err on the side of overpredicting, with the error having a uniform distribution on the interval
(-0.5,1.5). Find the following:

(a) The mean and variance of the error.

(b) The mean of the absolute error.

(c) The probability that exactly two errors are greater than 0.25 in absolute value, out of 10

predictions. Assume predictions are independent.

8. Consider the following game. A dart will hit the random point Y in (0,1) according to the
density fY (t) = 2t. You must guess the value of Y . (Your guess is a constant, not random.) You
will lose $2 per unit error if Y is to the left of your guess, and will lose $1 per unit error on the
right. Find best guess in terms of expected loss.

9. Fill in the blank: Density functions for continuous random variables are analogs of the
functions that are used for discrete random variables.

10. Suppose for some random variable W, FW (t) = t3 for 0 < t < 1, with FW (t) being 0 and 1 for
t < 0 and t > 1, respectively. Find fW (t) for 0 < t < 1.

11. Consider the density fZ(t) = 2t/15 for 1 < t < 4 and 0 elsewhere. Find the median of Z, as
well as Z’s third moment, E(Z3), and its third central moment, E[(Z − EZ)3].

12. Suppose X has a uniform distribution on the interval (20,40), and we know that X is greater
than 25. What is the probability that X is greater than 32?

13. Suppose U and V have the 2t/15 density on (1,4). Let N denote the number of values among
U and V that are greater than 1.5, so N is either 0, 1 or 2. Find Var(N).

Chapter 8

The Normal Family of Distributions

Again, these are the famous “bell-shaped curves,” so called because their densities have that shape.

8.1 Density and Properties

The density for a normal distribution is

fW (t) =

√

1
2πσ

e−0.5( t−µ

σ )2

, −∞ < t < ∞

(8.1)

Again, this is a two-parameter family, indexed by the parameters µ and σ, which turn out to be
the mean1 and standard deviation µ and σ, The notation for it is N (µ, σ2) (it is customary to state
the variance σ2 rather than the standard deviation).

And we write

X (cid:101)N (µ, σ2)

(8.2)

to mean that the random variable X has the distribution N (µ, σ2). (The tilde is read “is distributed
as.”)

Note: Saying “X has a N (µ, σ2) distribution” is more than simply saying “X has mean µ and
variance σ2.” The former statement tells us not only the mean and variance of X, but also the fact
that X has a “bell-shaped” density in the (8.1) family.

1Remember, this is a synonym for expected value.

155

156

CHAPTER 8. THE NORMAL FAMILY OF DISTRIBUTIONS

8.1.1 Closure Under Aﬃne Transformation

The family is closed under aﬃne transformations:

If

and we set

then

X (cid:101)N (µ, σ2)

Y = cX + d

Y (cid:101)N (cµ + d, c2σ2)

(8.3)

(8.4)

(8.5)

For instance, suppose X is the height of a randomly selection UC Davis student, measured in
inches. Human heights do have approximate normal distributions; a histogram plot of the student
heights would look bell-shaped. Now let Y be the student’s height in centimeters. Then we have
the situation above, with c = 2.54 and d = 0. The claim about aﬃne transformations of normally
distributed random variables would imply that a histogram of Y would again be bell-shaped.

Consider the above statement carefully.

It is saying much more than simply that Y has mean cµ + d and variance c2σ2, which
would follow from our our “mailing tubes” such as (3.49) even if X did not have a
normal distribution. The key point is that this new variable Y is also a member of
the normal family, i.e. its density is still given by (8.1), now with the new mean and
variance.

Let’s derive this, using the reasoning of Section 7.8.

For convenience, suppose c > 0. Then

FY (t) = P (Y ≤ t) (deﬁnition of FY )

= P (cX + d ≤ t) (deﬁnition of Y)
(cid:19)

(cid:18)

t − d
c
(cid:19)

= P

X ≤

= FX

(cid:18) t − d
c

(algebra)

(deﬁnition of FX )

(8.6)

(8.7)

(8.8)

(8.9)

8.1. DENSITY AND PROPERTIES

Therefore

fY (t) =

=

d
dt
d
dt

FY (t) (deﬁnition of fY )
(cid:19)

(from (8.9))

(cid:18) t − d
FX
c
(cid:18) t − d
(cid:19)
c

·

d
dt

t − d
c
(cid:18) t−d

=

1
c

·

√

1
2πσ

e

−0.5

c −µ
σ

(cid:19)2

(from (8.1)

=

√

1
2π(cσ)

(cid:16) t−(cµ+d)
cσ

−0.5
e

(cid:17)2

(algebra)

That last expression is the N (cµ + d, c2σ2) density, so we are done!

8.1.2 Closure Under Independent Summation

157

(8.10)

(8.11)

(8.13)

(8.14)

= fX

(deﬁnition of fX and the Chain Rule)

(8.12)

If X and Y are independent random variables, each having a normal distribution, then their sum S
= X + Y also is normally distributed.

This is a pretty remarkable phenomenon, not true for most other parametric families. If for instance
X and Y each with, say, a U(0,1) distribution, then the density of S turns out to be triangle-shaped,
NOT another uniform distribution. (This can be derived using the methods of Section 13.3.2.)

Note that if X and Y are independent and normally distributed, then the two properties above
imply that cX + dY will also have a normal distribution, for any constants c and d.

More generally:

For constants a1, ..., ak and random variables X1, ..., Xk, with

form the new random variable Y = a1X1 + ... + akXk. Then

Xi (cid:101)N (µi, σ2

i

k
(cid:88)

Y (cid:101)N (

k
(cid:88)

µi,

σ2
i )

i=1

i=1

(8.15)

(8.16)

158

CHAPTER 8. THE NORMAL FAMILY OF DISTRIBUTIONS

8.2 The Standard Normal Distribution

Deﬁnition 13 If Z (cid:101)N (0, 1) we say the random variable Z has a standard normal distribution.
Note that if X(cid:101)N (µ, σ2), and ﬁ we set

then

Z =

X − µ
σ

Z (cid:101)N (0, 1)

(8.17)

(8.18)

The above statements follow from the earlier material:

• Deﬁne Z = X−µ
σ .

• Rewrite it as Z = 1

σ · X + ( −µ

σ ).

• Since E(cU + d) = c EU + d for any random variable U and constants c and d, we have

EZ =

1
σ

EX −

µ
σ

= 0

(8.19)

and (3.56) and (3.49) imply that Var(X) = 1.

• OK, so we know that Z has mean 0 and variance 1. But does it have a normal distribution?

Yes, due to our discussion above titled “Closure Under Aﬃne Transformations.”

By the way, the N(0,1) cdf is traditionally denoted by Φ.

8.3 Evaluating Normal cdfs

The function in (8.1) does not have a closed-form indeﬁnite integral. Thus probabilities involving
normal random variables must be approximated. Traditionally, this is done with a table for the cdf
of N(0,1), which is included as an appendix to almost any statistics textbook; the table gives the
cdf values for that distribution.

But this raises a question: There are inﬁnitely many distributions in the normal family. Don’t
we need a separate table for each? That of course would not be possible, and in fact it turns out

8.4. EXAMPLE: NETWORK INTRUSION

159

that this one table—the one for the N(0,1) distribution— is suﬃcient for the entire normal family.
Though we of course will use R to gt such probabilities, it will be quite instructive to see how these
table operations work.

Here’s why one table is enough: Say X has an N (10, 2.52) distribution. How can we get a probability
like, say, P (X < 12) using the N(0,1) table? Write

P (X < 12) = P

Z <

(cid:18)

(cid:19)

12 − 10
2.5

= P (Z < 0.8)

(8.20)

Since on the right-hand side Z has a standard normal distribution, we can ﬁnd that latter probably
from the N(0,1) table!

As noted, traditionally it has played a central role, as one could transform any probability involving
some normal distribution to an equivalent probability involving N(0,1). One would then use a table
of N(0,1) to ﬁnd the desired probability.

The transformation Z = (X − µ)/σ will play a big role in other contexts in future chapters, but for
the sole purpose of simply evaluating normal probabilities, we can be much more direct. Nowadays,
probabilities for any normal distribution, not just N(0,1), are easily available by computer. In the R
statistical package, the normal cdf for any mean and variance is available via the function pnorm().
The call form is

pnorm(q,mean=0,sd=1)

This returns the value of the cdf evaluated at q, for a normal distribution having the speciﬁed mean
and standard deviation (default values of 0 and 1).

We can use rnorm() to simulate normally distributed random variables. The call is

rnorm(n,mean=0,sd=1)

which returns a vector of n random variates from the speciﬁed normal distribution.

There are also of course the corresponding density and quantile functions, dnorm() and qnorm().

8.4 Example: Network Intrusion

As an example, let’s look at a simple version of the network intrusion problem. Suppose we have
found that in Jill’s remote logins to a certain computer, the number X of disk sectors she reads or
writes has an approximate normal distribution with a mean of 500 and a standard deviation of 15.

160

CHAPTER 8. THE NORMAL FAMILY OF DISTRIBUTIONS

Before we continue, a comment on modeling: Since the number of sectors is discrete, it could not
have an exact normal distribution. But then, no random variable in practice has an exact normal
or other continuous distribution, as discussed in Section 7.15, and the distribution can indeed by
approximately normal.

Now, say our network intrusion monitor ﬁnds that Jill—or someone posing as her—has logged in
and has read or written 535 sectors. Should we be suspicious?

To answer this question, let’s ﬁnd P (X ≥ 535): Let Z = (X − 500)/15. From our discussion above,
we know that Z has a N(0,1) distribution, so

P (X ≥ 535) = P

Z ≥

(cid:18)

(cid:19)

535 − 500
15

= 1 − Φ(35/15) = 0.01

(8.21)

Again, traditionally we would obtain that 0.01 value from a N(0,1) cdf table in a book. With R,
we would just use the function pnorm():

> 1 - pnorm(535,500,15)
[1] 0.009815329

Anyway, that 0.01 probability makes us suspicious. While it could really be Jill, this would be
unusual behavior for Jill, so we start to suspect that it isn’t her. It’s suspicious enough for us to
probe more deeply, e.g. by looking at which ﬁles she (or the impostor) accessed—were they rare
for Jill too?

Now suppose there are two logins to Jill’s account, accessing X and Y sectors, with X+Y = 1088.
Is this rare for her, i.e. is P (X + Y > 1088)? small?

We’ll assume X and Y are independent. We’d have to give some thought as to whether this
assumption is reasonable, depending on the details of how we observed the logins, etc., but let’s
move ahead on this basis.

From page 157, we know that the sum S = X+Y is again normally distributed. Due to the properties
in Chapter 3, we know S has mean 2 · 500 and variance 2 · 152. The desired probability is then
found via

1 − pnorm( 1 0 8 8 , 1 0 0 0 , sqrt ( 4 5 0 ) )

which is about 0.00002. That is indeed a small number, and we should be highly suspicious.

Note again that the normal model (or any other continuous model) can only be approximate,
especially in the tails of the distribution, in this case the right-hand tail. But it is clear that S is
only rarely larger than 1088, and the matter mandates further investigation.

8.5. EXAMPLE: CLASS ENROLLMENT SIZE

161

Of course, this is very crude analysis, and real intrusion detection systems are much more complex,
but you can see the main ideas here.

8.5 Example: Class Enrollment Size

After years of experience with a certain course, a university has found that online pre-enrollment
in the course is approximately normally distributed, with mean 28.8 and standard deviation 3.1.
Suppose that in some particular oﬀering, pre-enrollment was capped at 25, and it hit the cap. Find
the probability that the actual demand for the course was at least 30.

Note that this is a conditional probability! Evaulate it as follows. Let N be the actual demand.
Then the key point is that we are given that N ≥ 25, so

P (N ≥ 30|N ≥ 25) =

P (N ≥ 30 and N ≥ 25)
P (N ≥ 25)

((2.7))

=

=

P (N ≥ 30)
P (N ≥ 25)
1 − Φ [(30 − 28.8)/3.1]
1 − Φ [(25 − 28.8)/3.1]

= 0.39

(8.22)

(8.23)

(8.24)

(8.25)

Sounds like it may be worth moving the class to a larger room before school starts.

Since we are approximating a discrete random variable by a continuous one, it might be more
accurate here to use a correction for continuity, described in Section 8.13.

8.6 More on the Jill Example

Continuing the Jill example, suppose there is never an intrusion, i.e. all logins are from Jill herself.
Say we’ve set our network intrusion monitor to notify us every time Jill logs in and accesses 535 or
more disk sectors. In what proportion of all such notiﬁcations will Jill have accessed at least 545
sectors?

This is P (X ≥ 545—X ≥ 535). By an analysis similar to that in Section 8.5, this probability is

( 1 − pnorm( 5 4 5 , 5 0 0 , 1 5 ) ) / ( 1 − pnorm( 5 3 5 , 5 0 0 , 1 5 ) )

162

CHAPTER 8. THE NORMAL FAMILY OF DISTRIBUTIONS

8.7 Example: River Levels

Consider a certain river, and L, its level (in feet) relative to its average. There is a ﬂood whenever
L > 8, and it is reported that 2.5% of days have ﬂooding. Let’s assume that the level L is normally
distributed; the above information implies that the mean is 0.

Suppose the standard deviation of L, σ, goes up by 10%. How much will the percentage of ﬂooding
days increase?

To solve this, let’s ﬁrst ﬁnd σ. We have that

0.025 = P (L > 8) = P

(cid:18) L − 0
σ

>

8 − 0
σ

(cid:19)

Since (L − 0)/σ has a N(0,1) distribution, we can ﬁnd the 0.975 point in its cdf:

> qnorm( 0 . 9 7 5 , 0 , 1 )
[ 1 ] 1 . 9 5 9 9 6 4

So,

1.96 =

8 − 0
σ

so σ is about 4.

If it increases to 4.4, then we can evaluate P (L > 8) by

> 1 − pnorm ( 8 , 0 , 4 . 4 )
[ 1 ] 0 . 0 3 4 5 1 8 1 7

(8.26)

(8.27)

So, a 10% increase in σ would lead in this case to about a 40% increase in ﬂood days.

8.8 Example: Upper Tail of a Light Bulb Distribution

Suppose we model light bulb lifetimes as having a normal distribution with mean and standard
deviation 500 and 50 hours, respectively. Give a loop-free R expression for ﬁnding the value of d
such that 30% of all bulbs have lifetime more than d.

You should develop the ability to recognize when we need p-series and q-series functions. Here we
need

qnorm( 1 − 0 . 3 0 , 5 0 0 , 5 0 )

8.9. THE CENTRAL LIMIT THEOREM

163

8.9 The Central Limit Theorem

The Central Limit Theorem (CLT) says, roughly speaking, that a random variable which is a sum
of many components will have an approximate normal distribution. So, for instance, human weights
are approximately normally distributed, since a person is made of many components. The same is
true for SAT test scores,2 as the total score is the sum of scores on the individual problems.

There are many versions of the CLT. The basic one requires that the summands be independent
and identically distributed:3

Theorem 14 Suppose X1, X2, ... are independent random variables, all having the same distribu-
tion which has mean m and variance v2. Form the new random variable T = X1 + ... + Xn. Then
for large n, the distribution of T is approximately normal with mean nm and variance nv2.

The larger n is, the better the approximation, but typically n = 20 or even n = 10 is enough.

8.10 Example: Cumulative Roundoﬀ Error

Suppose that computer roundoﬀ error in computing the square roots of numbers in a certain range
is distributed uniformly on (-0.5,0.5), and that we will be computing the sum of n such square roots.
Suppose we compute a sum of 50 square roots. Let’s ﬁnd the approximate probability that the
sum is more than 2.0 higher than it should be. (Assume that the error in the summing operation
is negligible compared to that of the square root operation.)

Let U1, ..., U50 denote the errors on the individual terms in the sum. Since we are computing a
sum, the errors are added too, so our total error is

T = U1 + ... + U50

(8.28)

By the Central Limit Theorem, since T is a sum, it has an approximately normal distribution, with
mean 50 EU and variance 50 Var(U), where U is a random variable having the distribution of the
Ui. From Section 7.6.1.1, we know that

EU = (−0.5 + 0.5)/2 = 0, V ar(U ) =

1
12

[0.5 − (−0.5)]2 =

1
12

(8.29)

2This refers to the raw scores, before scaling by the testing company.
3A more mathematically precise statement of the theorem is given in Section 8.19.

164

CHAPTER 8. THE NORMAL FAMILY OF DISTRIBUTIONS

So, the approximate distribution of T is N(0,50/12). We can then use R to ﬁnd our desired
probability:

> 1 − pnorm( 2 ,mean=0,sd=sqrt ( 5 0 / 1 2 ) )
[ 1 ] 0 . 1 6 3 5 9 3 4

8.11 Example: R Evaluation of a Central Limit Theorem Approx-

imation

Say W = U1 + ... + U50, with the Ui being independent and identically distributed (i.i.d.) with
uniform distributions on (0,1). Give an R expression for the approximate value of P (W < 23.4).

W has an approximate normal distribution, with mean 50 × 0.5 and variance 50 × (1/12). So we
need

pnorm( 2 3 . 4 , 2 5 , sqrt ( 5 0 / 1 2 ) )

8.12 Example: Bug Counts

As an example, suppose the number of bugs per 1,000 lines of code has a Poisson distribution with
mean 5.2. Let’s ﬁnd the probability of having more than 106 bugs in 20 sections of code, each 1,000
lines long. We’ll assume the diﬀerent sections act independently in terms of bugs.

Here Xi is the number of bugs in the ith section of code, and T is the total number of bugs. This
is another clear candidate for using the CLT.

Since each Xi has a Poisson distribution, m = v2 = 5.2. So, T, being a sum, is approximately
distributed normally with mean and variance 20 × 5.2. So, we can ﬁnd the approximate probability
of having more than 106 bugs:

> 1 - pnorm(106,20*5.2,sqrt(20*5.2))
[1] 0.4222596

8.13 Example: Coin Tosses

Binomially distributed random variables, though discrete, also are approximately normally dis-
tributed. Here’s why:

8.13. EXAMPLE: COIN TOSSES

165

Say T has a binomial distribution with n trials. Then we can write T as a sum of indicator random
variables (Section 3.9):

T = T1 + ... + Tn

(8.30)

where Ti is 1 for a success and 0 for a failure on the ith trial. Since we have a sum of indepen-
dent, identically distributed terms, the CLT applies. Thus we use the CLT if we have binomial
distributions with large n.

For example, let’s ﬁnd the approximate probability of getting more than 12 heads in 20 tosses of a
coin. X, the number of heads, has a binomial distribution with n = 20 and p = 0.5 Its mean and
variance are then np = 10 and np(1-p) = 5. So, let Z = (X − 10)/

5, and write

√

P (X > 12) = P (Z >

12 − 10
√
5

) ≈ 1 − Φ(0.894) = 0.186

(8.31)

Or:

> 1 - pnorm(12,10,sqrt(5))
[1] 0.1855467

The exact answer is 0.132, not too close. Why such a big error? The main reason is n here is rather
small. But actually, we can still improve the approximation quite a bit, as follows.

Remember, the reason we did the above normal calculation was that X is approximately normal,
from the CLT. This is an approximation of the distribution of a discrete random variable by a
continuous one, which introduces additional error.

We can get better accuracy by using the correction of continuity, which can be motivated as
follows. As an alternative to (8.31), we might write

P (X > 12) = P (X ≥ 13) = P (Z >

13 − 10
√
5

) ≈ 1 − Φ(1.342) = 0.090

(8.32)

That value of 0.090 is considerably smaller than the 0.186 we got from (8.31). We could “split the
diﬀerence” this way:

P (X > 12) = P (X ≥ 12.5) = P (Z >

12.5 − 10
√
5

) ≈ 1 − Φ(1.118) = 0.132

(8.33)

(Think of the number 13 “owning” the region between 12.5 and 13.5, 14 owning the part between
13.5 and 14.5 and so on.) Since the exact answer to seven decimal places is 0.131588, the strategy
has improved accuracy substantially.

166

CHAPTER 8. THE NORMAL FAMILY OF DISTRIBUTIONS

The term correction for continuity alludes to the fact that we are approximately a discrete distri-
bution by a continuous one.

8.14 Example: Normal Approximation to Gamma Family

Recall from above that the gamma distribution, or at least the Erlang, arises as a sum of independent
random variables. Thus the Central Limit Theorem implies that the gamma distribution should
be approximately normal for large (integer) values of r. We see in Figure 7.2 that even with r =
10 it is rather close to normal.4

8.15 Example: Museum Demonstration

Many science museums have the following visual demonstration of the CLT.

There are many balls in a chute, with a triangular array of r rows of pins beneath the chute. Each
ball falls through the rows of pins, bouncing left and right with probability 0.5 each, eventually
being collected into one of r bins, numbered 0 to r. A ball will end up in bin i if it bounces rightward
in i of the r rows of pins, i = 0,1,...,r. Key point:

Let X denote the bin number at which a ball ends up. X is the number of rightward
bounces (“successes”) in r rows (“trials”). Therefore X has a binomial distribution with
n = r and p = 0.5

Each bin is wide enough for only one ball, so the balls in a bin will stack up. And since there are
many balls, the height of the stack in bin i will be approximately proportional to P(X = i). And
since the latter will be approximately given by the CLT, the stacks of balls will roughly look like
the famous bell-shaped curve!

There are many online simulations of this museum demonstration, such as http://www.mathsisfun.
com/data/quincunx.html. By collecting the balls in bins, the apparatus basically simulates a his-
togram for X, which will then be approximately bell-shaped.

4It should be mentioned that technically, the CLT, which concerns convergence of cdfs, does not imply convergence

of densities. However, under mild mathematical conditions, convergence of densities occurs too.

8.16.

IMPORTANCE IN MODELING

167

8.16 Importance in Modeling

Needless to say, there are no random variables in the real world that are exactly normally dis-
tributed. In addition to our comments at the beginning of this chapter that no real-world random
variable has a continuous distribution, there are no practical applications in which a random vari-
able is not bounded on both ends. This contrasts with normal distributions, which extend from
−∞ to ∞.

Yet, many things in nature do have approximate normal distributions, so normal distributions play
a key role in statistics. Most of the classical statistical procedures assume that one has sampled from
a population having an approximate distribution. In addition, it will be seen later than the CLT
tells us in many of these cases that the quantities used for statistical estimation are approximately
normal, even if the data they are calculated from are not.

Recall from above that the gamma distribution, or at least the Erlang, arises as a sum of independent
random variables. Thus the Central Limit Theorem implies that the gamma distribution should
be approximately normal for large (integer) values of r. We see in Figure 7.2 that even with r =
10 it is rather close to normal.

8.17 The Chi-Squared Family of Distributions

8.17.1 Density and Properties

Let Z1, Z2, ..., Zk be independent N(0,1) random variables. Then the distribution of

Y = Z2

1 + ... + Z2
k

(8.34)

is called chi-squared with k degrees of freedom. We write such a distribution as χ2
k. Chi-
squared is a one-parameter family of distributions, and arises quite frequently in statistical appli-
cations, as will be seen in future chapters.

We can derive the mean of a chi-squared distribution as follows. First,

EY = E(Z2

1 + ... + Z2

k ) = kE(Z2
1 )

(8.35)

Well, E(Z2
fact:

1 ) sounds somewhat like variance, and we do know that V ar(Z1) = 1. Let’s use that

E(Z2

1 ) = V ar(Z1) + [E(Z1)]2 = V ar(Z1) = 1

(8.36)

168

CHAPTER 8. THE NORMAL FAMILY OF DISTRIBUTIONS

Then EY in (8.34) is k. One can also show that Var(Y) = 2k.

It turns out that chi-squared is a special case of the gamma family in Section 7.6.4 below, with r
= k/2 and λ = 0.5.

The R functions dchisq(), pchisq(), qchisq() and rchisq() give us the density, cdf, quantile
function and random number generator for the chi-squared family. The second argument in each
case is the number of degrees of freedom. The ﬁrst argument is the argument to the corresponding
math function in all cases but rchisq(), in which it is the number of random variates to be
generated.

For instance, to get the value of fX (5.2) for a chi-squared random variable having 3 degrees of
freedom, we make the following call:

> dchisq ( 5 . 2 , 3 )
[ 1 ] 0 . 0 6 7 5 6 8 7 8

8.17.2 Example: Error in Pin Placement

Consider a machine that places a pin in the middle of a ﬂat, disk-shaped object. The placement
is subject to error. Let X and Y be the placement errors in the horizontal and vertical directions,
respectively, and let W denote the distance from the true center to the pin placement. Suppose X
and Y are independent and have normal distributions with mean 0 and variance 0.04. Let’s ﬁnd
P (W > 0.6).

Since a distance is the square root of a sum of squares, this sounds like the chi-squared distribution
might be relevant. So, let’s ﬁrst convert the problem to one involving squared distance:

But W 2 = X 2 + Y 2, so

P (W > 0.6) = P (W 2 > 0.36)

P (W > 0.6) = P (X 2 + Y 2 > 0.36)

(8.37)

(8.38)

This is not quite chi-squared, as that distribution involves the sum of squares of independent N(0,1)
random variables. But due to the normal family’s closure under aﬃne transformations (page 156),
we know that X/0.2 and Y/0.2 do have N(0,1) distributions. So write

P (W > 0.6) = P [(X/0.2)2 + (Y /0.2)2 > 0.36/0.22]

(8.39)

Now evaluate the right-hand side:

8.17. THE CHI-SQUARED FAMILY OF DISTRIBUTIONS

169

> 1 − pchisq ( 0 . 3 6 / 0 . 0 4 , 2 )
[ 1 ] 0 . 0 1 1 1 0 9 0 0

8.17.3 Example: Generating Normal Random Numbers

How do normal random number generators such as rnorm() work? While in principle Section
7.11 could be used, the lack of a closed-form expression for Φ−1() makes that approach infeasible.
Instead, we can exploit the relation between the normal family and exponential distribution, as
follows.

Let Z1 and Z2 be two independent N(0,1) random variables, and deﬁne W = Z2
2 . By deﬁnition,
W has a chi-squared distribution with 2 degrees of freedom, and from Section 8.17, we know that
W has a gamma distribution with k = 1 and λ = 0.5.

1 +Z2

In turn, we know from Section 7.6.4 that that distribution is actually just an exponential distribution
with the same value of λ. This is quite fortuitous, since Section 7.11 can be used in this case.

Speciﬁcally, fW (t) = 0.5e−0.5t, so FW (t) = 1 − e−0.5t, Thus

F −1

W (s) = −2 ln(1 − s)

And there’s more. Think of plotting the pair (Z1, Z2) in the X-Y plane, and deﬁne

One can show that θ has a uniform distribution on (0, 2π). Also,

θ = tan−1(

Y
X

)

(8.40)

(8.41)

X = W cos(θ),

Y = W sin(θ)

(8.42)

Putting all this together, we can generate a pair of independent N(0,1) random variates via the
code

function genn01 ( ) {

t h e t a <− runif ( 1 , 0 , p i )
w <− −log ( 1 − runif ( 1 ) )
c (w∗cos ( t h e t a ) ,w∗ sin ( t h e t a ) )

}

170

CHAPTER 8. THE NORMAL FAMILY OF DISTRIBUTIONS

Note by the way that we “get two for the price of one.” If we need, say, 1000 random normal
variates, we call the above function 500 times.
If we need just variate, we use the ﬁrst in the
returned pair, and save the second in case we need it later in another call.

8.17.4 Importance in Modeling

This distribution family does not come up directly in application nearly so often as, say, the binomial
or normal distribution family.

But the chi-squared family is used quite widely in statistical applications. As will be seen in our
chapters on statistics, many statistical methods involve a sum of squared normal random variables.5

8.17.5 Relation to Gamma Family

One can show that the chi-square distribution with d degrees of freedom is a gamma distribution,
with r = d/2 and λ = 0.5.

8.18 The Multivariate Normal Family

(Here we borrow some material from Chapter 13.)

The generalization of the normal family is the multivariate normal. Instead of being parameterized
by a scalar mean and a scalar variance, the multivariate normal family has as its parameters a
vector mean and a covariance matrix.

Let’s look at the bivariate case ﬁrst. The joint distribution of X1 and X2 is said to be bivariate
normal if their density is

fX,Y (s, t) =

1
(cid:112)1 − ρ2

e

2πσ1σ2

− 1

2(1−ρ2)

(cid:20)

(s−µ1)2
σ2
1

+ (t−µ2)2
σ2
2

− 2ρ(s−µ1)(t−µ2)
σ1σ2

(cid:21)

, − ∞ < s, t < ∞ (8.43)

This looks horrible, and it is. But don’t worry, as we won’t work with this directly.
It’s important for conceptual reasons, as follows.

First, note the parameters here: µ1, µ2, σ1 and σ2 are the means and standard deviations of X and
Y, while ρ is the correlation between X and Y. So, we have a ﬁve-parameter family of distributions.

5The motivation for the term degrees of freedom will be explained in those chapters too.

8.19. OPTIONAL TOPIC: PRECISE STATEMENT OF THE CLT

171

The graph of the bivariate normal density looks like a 3-dimensional bell, as seen on the cover of
this book.

The multivariate normal family of distributions is parameterized by one vector-valued quantity,
the mean µ, and one matrix-valued quantity, the covariance matrix Σ. Speciﬁcally, suppose the
random vector X = (X1, ..., Xk)(cid:48) has a k-variate normal distribution.

The density has this form:

fX (t) = ce−0.5(t−µ)(cid:48)Σ−1(t−µ)

(8.44)

Here c is a constant, needed to make the density integrate to 1.0.

There is a Multivariate Central Limit Theorem, that says that sums of random vectors have ap-
proximately multivariate normal distributions.

In R the density, cdf and quantiles of the multivariate normal distribution are given by the func-
tions dmvnorm(), pmvnorm() and qmvnorm() in the library mvtnorm. You can simulate a
multivariate normal distribution by using mvrnorm() in the library MASS.

8.19 Optional Topic: Precise Statement of the CLT

The statement of Theorem 14 is not mathematically precise. We will ﬁx it here, not just for
mathematical niceness, but also because it leads to something called the delta method, a very
practical tool in statistics.

8.19.1 Convergence in Distribution, and the Precisely-Stated CLT

Deﬁnition 15 A sequence of random variables L1, L2, L3, ... converges in distribution to a
random variable M if

lim
n→∞

P (Ln ≤ t) = P (M ≤ t), for all t

(8.45)

Note by the way, that these random variables need not be deﬁned on the same probability space.

The formal statement of the CLT is:

Theorem 16 Suppose X1, X2, ... are independent random variables, all having the same distribu-

172

CHAPTER 8. THE NORMAL FAMILY OF DISTRIBUTIONS

tion which has mean m and variance v2. Then

Z =

X1 + ... + Xn − nm
√

v

n

(8.46)

converges in distribution to a N(0,1) random variable.

Exercises

1. In the network intrusion example in Section 8.4, suppose X is not normally distributed, but
instead has a uniform distribution on (450,550). Find P (X ≥ 535) in this case.

2. “All that glitters is not gold,” and not every bell-shaped density is normal. The family of
Cauchy distributions, having density

fX (t) =

1
πc

1
1 + ( t−b

c )2

, ∞ < t < ∞

(8.47)

is bell-shaped but deﬁnitely not normal.

Here the parameters b and c correspond to mean and standard deviation in the normal case, but
actually neither the mean nor standard deviation exist for Cauchy distributions. The mean’s failure
to exist is due to technical problems involving the theoretical deﬁnition of integration. In the case of
variance, it does not exist because there is no mean, but even more signiﬁcantly, E[(X − b)2] = ∞.

However, a Cauchy distribution does have a median, b, so we’ll use that instead of a mean. Also,
instead of a standard deviation, we’ll use as our measure of dispersion the interquartile range,
deﬁned (for any distribution) to be the diﬀerence between the 75th and 25th percentiles.

We will be investigating the Cauchy distribution that has b = 0 and c = 1.

(a) Find the interquartile range of this Cauchy distribution.

(b) Find the normal distribution that has the same median and interquartile range as this Cauchy

distribution.

(c) Use R to plot the densities of the two distributions on the same graph, so that we can see

that they are both bell-shaped, but diﬀerent.

3. Suppose X has a binomial distribution with parameters n and p. Then X is approximately
normally distributed with mean np and variance np(1-p). For each of the following, answer either
A or E, for “approximately” or “exact,” respectively:

8.19. OPTIONAL TOPIC: PRECISE STATEMENT OF THE CLT

173

(a) the distribution of X is normal

(b) E(X) is np

(c) Var(X) is np(1-p)

4. Find the value of E(X 4) if X has an N(0,1) distribution. (Give your answer as a number, not
an integral.)

174

CHAPTER 8. THE NORMAL FAMILY OF DISTRIBUTIONS

Chapter 9

The Exponential Distributions

The family of exponential distributions, Section 7.6.3, has a number of remarkable properties, which
contribute to its widespread usage in probabilistic modeling. We’ll discuss those here.

9.1 Connection to the Poisson Distribution Family

Suppose the lifetimes of a set of light bulbs are independent and identically distributed (i.i.d.),
and consider the following process. At time 0, we install a light bulb, which burns an amount of
time X1. Then we install a second light bulb, with lifetime X2. Then a third, with lifetime X3,
and so on.

Let

Tr = X1 + ... + Xr

(9.1)

denote the time of the rth replacement. Also, let N(t) denote the number of replacements up to and
including time t. Then it can be shown that if the common distribution of the Xi is exponentially
distributed, the N(t) has a Poisson distribution with mean λt. And the converse is true too: If
the Xi are independent and identically distributed and N(t) is Poisson, then the Xi must have
exponential distributions. In summary:

Theorem 17 Suppose X1, X2, ... are i.i.d. nonnegative continuous random variables. Deﬁne

Tr = X1 + ... + Xr

(9.2)

175

176

and

CHAPTER 9. THE EXPONENTIAL DISTRIBUTIONS

N (t) = max{k : Tk ≤ t}

(9.3)

Then the distribution of N(t) is Poisson with parameter λt for all t if and only if the Xi have an
exponential distribution with parameter λ.

In other words, N(t) will have a Poisson distribution if and only if the lifetimes are exponentially
distributed.

Proof

“Only if ” part:

The key is to notice that the event X1 > t is exactly equivalent to N (t) = 0. If the ﬁrst light bulb
lasts longer than t, then the count of burnouts at time t is 0, and vice versa. Then

P (X1 > t) = P [N (t) = 0] (see above equiv.)

=

(λt)0
0!
= e−λt

· e−λt ((4.41)

Then

fX1(t) =

d
dt

(1 − e−λt) = λe−λt

(9.4)

(9.5)

(9.6)

(9.7)

That shows that X1 has an exponential distribution, and since the Xi are i.i.d., that implies that
all of them have that distribution.

“If ” part:

We need to show that if the Xi are exponentially distributed with parameter λ, then for u nonneg-
ative and each positive integer k,

P [N (u) = k] =

(λu)ke−λu
k!

(9.8)

The proof for the case k = 0 just reverses (9.4) above. The general case, not shown here, notes that
N (u) ≤ k is equivalent to Tk+1 > u. The probability of the latter event can be found by integrating

9.2. MEMORYLESS PROPERTY OF EXPONENTIAL DISTRIBUTIONS

177

(7.46) from u to inﬁnity. One needs to perform k-1 integrations by parts, and eventually one arrives
at (9.8), summed from 1 to k, as required.

The collection of random variables N(t) t ≥ 0, is called a Poisson process.

The relation E[N (t)] = λt says that replacements are occurring at an average rate of λ per unit
time. Thus λ is called the intensity parameter of the process. It is this “rate” interpretation
that makes λ a natural indexing parameter in (7.40).

9.2 Memoryless Property of Exponential Distributions

One of the reasons the exponential family of distributions is so famous is that it has a property that
makes many practical stochastic models mathematically tractable: The exponential distributions
are memoryless.

9.2.1 Derivation and Intuition

What the term memoryless means for a random variable W is that for all positive t and u

P (W > t + u|W > t) = P (W > u)

(9.9)

Any exponentially distributed random variable has this property. Let’s derive this:

P (W > t + u|W > t) =

P (W > t + u and W > t)
P (W > t)

=

=

P (W > t + u)
P (W > t)
(cid:82) ∞
t+u λe−λs ds
(cid:82) ∞
t λe−λs ds

= e−λu
= P (W > u)

(9.10)

(9.11)

(9.12)

(9.13)

(9.14)

We say that this means that “time starts over” at time t, or that W “doesn’t remember” what
happened before time t.

178

CHAPTER 9. THE EXPONENTIAL DISTRIBUTIONS

It is diﬃcult for the beginning modeler to fully appreciate the memoryless property. Let’s make it
concrete. Consider the problem of waiting to cross the railroad tracks on Eighth Street in Davis,
just west of J Street. One cannot see down the tracks, so we don’t know whether the end of the
train will come soon or not.

If we are driving, the issue at hand is whether to turn oﬀ the car’s engine. If we leave it on, and the
end of the train does not come for a long time, we will be wasting gasoline; if we turn it oﬀ, and
the end does come soon, we will have to start the engine again, which also wastes gasoline. (Or,
we may be deciding whether to stay there, or go way over to the Covell Rd. railroad overpass.)

Suppose our policy is to turn oﬀ the engine if the end of the train won’t come for at least s seconds.
Suppose also that we arrived at the railroad crossing just when the train ﬁrst arrived, and we have
already waited for r seconds. Will the end of the train come within s more seconds, so that we will
keep the engine on? If the length of the train were exponentially distributed (if there are typically
many cars, we can model it as continuous even though it is discrete), Equation (9.9) would say that
the fact that we have waited r seconds so far is of no value at all in predicting whether the train
will end within the next s seconds. The chance of it lasting at least s more seconds right now is no
more and no less than the chance it had of lasting at least s seconds when it ﬁrst arrived.

9.2.2 Uniquely Memoryless

By the way, the exponential distributions are the only continuous distributions which are memory-
less. (Note the word continuous; in the discrete realm, the family of geometric distributions are also
uniquely memoryless.) This too has implications for the theory. A rough proof of this uniqueness
is as follows:

Suppose some continuous random variable V has the memoryless property, and let R(t) denote
1 − FV (t). Then from (9.9), we would have

or

R(t + u)]/R(t) = R(u)

R(t + u) = R(t)R(u)

Diﬀerentiating both sides with respect to t, we’d have

R(cid:48)(t + u) = R(cid:48)(t)R(u)

(9.15)

(9.16)

(9.17)

9.3. EXAMPLE: MINIMA OF INDEPENDENT EXPONENTIALLY DISTRIBUTED RANDOM VARIABLES179

Setting t to 0, this would say

This is a well-known diﬀerential equation, whose solution is

R(cid:48)(u) = R(cid:48)(0)R(u)

R(u) = e−cu

(9.18)

(9.19)

which is exactly 1 minus the cdf for an exponentially distributed random variable.

9.2.3 Example: “Nonmemoryless” Light Bulbs

Suppose the lifetimes in years of light bulbs have the density 2t/15 on (1,4), 0 elsewhere. Say I’ve
been using bulb A for 2.5 years now in a certain lamp, and am continuing to use it. But at this
time I put a new bulb, B, in a second lamp. I am curious as to which bulb is more likely to burn
out within the next 1.2 years. Let’s ﬁnd the two probabilities.

For bulb A:

For bulb B:

P (L > 3.7|L > 2.5) =

P (L > 3.7)
P (L > 2.5)

= 0.24

(9.20)

P (X > 1.2) =

(cid:90) 4

1.2

2t/15 dt = 0.97

(9.21)

So you can see that the bulbs do have “memory.” We knew this beforehand, since the exponential
distributions are the only continuous ones that have no memory.

9.3 Example: Minima of Independent Exponentially Distributed

Random Variables

The memoryless property of the exponential distribution (Section 9.2 leads to other key properties.
Here’s a famous one:

180

CHAPTER 9. THE EXPONENTIAL DISTRIBUTIONS

Theorem 18 Suppose W1, ..., Wk are independent random variables, with Wi being exponentially
distributed with parameter λi. Let Z = min(W1, ..., Wk). Then Z too is exponentially distributed
with parameter λ1 + ... + λk, and thus has mean equal to the reciprocal of that parameter

Comments:

• In “notebook” terms, we would have k+1 columns, one each for the Wi and one for Z. For
any given line, the value in the Z column will be the smallest of the values in the columns
for W1, ..., Wk; Z will be equal to one of them, but not the same one in every line. Then for
instance P (Z = W3) is interpretable in notebook form as the long-run proportion of lines in
which the Z column equals the W3 column.

• It’s pretty remarkable that the minimum of independent exponential random variables turns
out again to be exponential. Contrast that with Section 13.3.6, where it is found that the
minimum of independent uniform random variables does NOT turn out to have a uniform
distribution.

• The sum λ1 + ... + λn in (a) should make good intuitive sense to you, for the following reasons.
Recall from Section 9.1 that the parameter λ in an exponential distribution is interpretable
as a “light bulb burnout rate.”

Say we have persons 1 and 2. Each has a lamp. Person i uses Brand i light bulbs, i = 1,2. Say
Brand i light bulbs have exponential lifetimes with parameter λi. Suppose each time person
i replaces a bulb, he shouts out, “New bulb!” and each time anyone replaces a bulb, I shout
out “New bulb!” Persons 1 and 2 are shouting at a rate of λ1 and λ2, respectively, so I am
shouting at a rate of λ1 + λ2.

Proof

FZ(t) = P (Z ≤ t)

(def. of cdf)

= 1 − P (Z > t)
= 1 − P (W1 > t and ... and Wk > t)
= 1 − Πi P (Wi > t)
= 1 − Πi e−λit
= 1 − e−(λ1+...+λn)t

(expon. distr.)

(indep.)

Taking d

dt of both sides proves the theorem.

(min > t iﬀ all Wi > t)

(9.22)

(9.23)

(9.24)

(9.25)

(9.26)

(9.27)

9.3. EXAMPLE: MINIMA OF INDEPENDENT EXPONENTIALLY DISTRIBUTED RANDOM VARIABLES181

Also:

Theorem 19 Under the conditions in Theorem 18,

P (Wi < W1, ..., Wi−1, Wi+1, ..., Wk) =

λi
λ1 + ... + λk

(9.28)

(There are k terms in the denominator, not k-1.)

Equation (9.28) should be intuitively clear as well from the above “thought experiment” (in which
we shouted out “New bulb!”): On average, we have one new Brand 1 bulb every 1/λ1 time, so in a
long time t, we’ll have about tλ1 shouts for this brand. We’ll also have about tλ2 shouts for Brand
2. So, a proportion of about

tλ1
tλ1 + tλ2

(9.29)

of the shots are for Brand 1. Also, at any given time, the memoryless property of exponential
distributions implies that the time at which I shout next will be the minimum of the times at
which persons 1 and 2 shout next. This intuitively implies (9.28).

Proof

Again consider the case k = 2, and then use induction.

Let Z = min(W1, W2) as before. Then

P (Z = W1|W1 = t) = P (W2 > t | W1 = t)

(9.30)

(Note: We are working with continuous random variables here, so quantities like P (W1 = t) are
0 (though actually P (Z = W1) is nonzero). So, as mentioned in Section 7.78, quantities like
P (Z = W1 | W1 = t) really mean “the probability that W2 > t in the conditional distribution of Z
given W1.)

Since W1 and W2 are independent,

P (W2 > t | W1 = t) = P (W2 > t) = e−λ2t

(9.31)

182

CHAPTER 9. THE EXPONENTIAL DISTRIBUTIONS

Now use (7.78):

as claimed.

P (Z = W1) =

(cid:90) ∞

0

λ1e−λ1te−λ2t dt =

λ1
λ1 + λ2

(9.32)

This property of minima of independent exponentially-distributed random variables developed in
this section is key to the structure of continuous-time Markov chains, in Chapter 11.

9.3.1 Example: Computer Worm

A computer science graduate student at UCD, Senthilkumar Cheetancheri, was working on a worm
alert mechanism. A simpliﬁed version of the model is that network hosts are divided into groups of
size g, say on the basis of sharing the same router. Each infected host tries to infect all the others
in the group. When g-1 group members are infected, an alert is sent to the outside world.

The student was studying this model via simulation, and found some surprising behavior. No
matter how large he made g, the mean time until an external alert was raised seemed bounded. He
asked me for advice.

I modeled the nodes as operating independently, and assumed that if node A is trying to infect
node B, it takes an exponentially-distributed amount of time to do so. This is a continuous-time
Markov chain. Again, this topic is much more fully developed in Chapter 11, but all we need here
is the result of Section 9.3, that exponential distributions are “memoryless.”.

In state i, there are i infected hosts, each trying to infect all of the g-i noninfected hosts. When the
process reaches state g-1, the process ends; we call this state an absorbing state, i.e. one from
which the process never leaves.

Scale time so that for hosts A and B above, the mean time to infection is 1.0. Since in state i there
are i(g-i) such pairs, the time to the next state transition is the minimum of i(g-i) exponentially-
distributed random variables with mean 1. Theorem 18 tells us that this minimum is also expo-
nentially distributed, with parameter i(g − i) · 1. Thus the mean time to go from state i to state
i+1 is 1/[i(g-i)].

9.4. A CAUTIONARY TALE: THE BUS PARADOX

Then the mean time to go from state 1 to state g-1 is

g−1
(cid:88)

i=1

1
i(g − i)

183

(9.33)

Using a calculus approximation, we have

(cid:90) g−1

1

1
x(g − x)

dx =

1
g

(cid:90) g−1

1

(

1
x

+

1
g − x

) dx =

2
g

ln(g − 1)

(9.34)

The latter quantity goes to zero as g → ∞. This conﬁrms that the behavior seen by the student
in simulations holds in general. In other words, (9.33) remains bounded as g → ∞. This is a very
interesting result, since it says that the mean time to alert is bounded no matter how big our group
size is.

So, even though our model here was quite simple, probably overly so, it did explain why the student
was seeing the surprising behavior in his simulations.

9.3.2 Example: Electronic Components

Suppose we have three electronic parts, with independent lifetimes that are exponentially dis-
tributed with mean 2.5. They are installed simultaneously. Let’s ﬁnd the mean time until the last
failure occurs.

Actually, we can use the same reasoning as for the computer worm example in Section 9.3.1: The
mean time is simply

1/(3 · 0.4) + 1/(2 · 0.4) + 1/(1 · 0.4)

(9.35)

9.4 A Cautionary Tale: the Bus Paradox

Suppose you arrive at a bus stop, at which buses arrive according to a Poisson process with intensity
parameter 0.1, i.e. 0.1 arrival per minute. Recall that the means that the interarrival times have an
exponential distribution with mean 10 minutes. What is the expected value of your waiting time
until the next bus?

Well, our ﬁrst thought might be that since the exponential distribution is memoryless, “time starts
over” when we reach the bus stop. Therefore our mean wait should be 10.

184

CHAPTER 9. THE EXPONENTIAL DISTRIBUTIONS

On the other hand, we might think that on average we will arrive halfway between two consecutive
buses. Since the mean time between buses is 10 minutes, the halfway point is at 5 minutes. Thus
it would seem that our mean wait should be 5 minutes.

Which analysis is correct? Actually, the correct answer is 10 minutes. So, what is wrong with the
second analysis, which concluded that the mean wait is 5 minutes? The problem is that the second
analysis did not take into account the fact that although inter-bus intervals have an exponential
distribution with mean 10, the particular inter-bus interval that we encounter is special.

9.4.1 Length-Biased Sampling

Imagine a bag full of sticks, of diﬀerent lengths. We reach into the bag and choose a stick at
random. The key point is that not all pieces are equally likely to be chosen; the longer pieces will
have a greater chance of being selected.

Say for example there are 50 sticks in the bag, with ID numbers from 1 to 50. Let X denote the
length of the stick we obtain if select a stick on an equal-probability basis, i.e. each stick having
probability 1/50 of being chosen. (We select a random number I from 1 to 50, and choose the stick
with ID number I.) On the other hand, let Y denote the length of the stick we choose by reaching
into the bag and pulling out whichever stick we happen to touch ﬁrst. Intuitively, the distribution
of Y should favor the longer sticks, so that for instance EY > EX.

Let’s look at this from a “notebook” point of view. We pull a stick out of the bag by random ID
number, and record its length in the X column of the ﬁrst line of the notebook. Then we replace
the stick, and choose a stick out by the “ﬁrst touch” method, and record its length in the Y column
of the ﬁrst line. Then we do all this again, recording on the second line, and so on. Again, because
the “ﬁrst touch” method will favor the longer sticks, the long-run average of the Y column will be
larger than the one for the X column.

Another example was suggested to me by UCD grad student Shubhabrata Sengupta. Think of a
large parking lot on which hundreds of buckets are placed of various diameters. We throw a ball
high into the sky, and see what size bucket it lands in. Here the density would be proportional to
area of the bucket, i.e. to the square of the diameter.

Similarly, the particular inter-bus interval that we hit is likely to be a longer interval. To see this,
suppose we observe the comings and goings of buses for a very long time, and plot their arrivals
on a time line on a wall. In some cases two successive marks on the time line are close together,
sometimes far apart. If we were to stand far from the wall and throw a dart at it, we would hit
the interval between some pair of consecutive marks. Intuitively we are more apt to hit a wider
interval than a narrower one.

The formal name for this is length-biased sampling.

9.4. A CAUTIONARY TALE: THE BUS PARADOX

185

Once one recognizes this and carefully derives the density of that interval (see below), we discover
that that interval does indeed tend to be longer—so much so that the expected value of this interval
is 20 minutes! Thus the halfway point comes at 10 minutes, consistent with the analysis which
appealed to the memoryless property, thus resolving the “paradox.”

In other words, if we throw a dart at the wall, say, 1000 times, the mean of the 1000 intervals we
would hit would be about 20. This in contrast to the mean of all of the intervals on the wall, which
would be 10.

9.4.2 Probability Mass Functions and Densities in Length-Biased Sampling

Actually, we can intuitively reason out what the density is of the length of the particular inter-bus
interval that we hit, as follows.

First consider the bag-of-sticks example, and suppose (somewhat artiﬁcially) that stick length X is
a discrete random variable. Let Y denote the length of the stick that we pick by randomly touching
a stick in the bag.

Again, note carefully that for the reasons we’ve been discussing here, the distributions of X and Y
are diﬀerent. Say we have a list of all sticks, and we choose a stick at random from the list. Then
the length of that stick will be X. But if we choose by touching a stick in the bag, that length will
be Y.

Now suppose that, say, stick lengths 2 and 6 each comprise 10% of the sticks in the bag, i.e.

pX (2) = pX (6) = 0.1

Intuitively, one would then reason that

pY (6) = 3pY (2)

(9.36)

(9.37)

In other words, even though the sticks of length 2 are just as numerous as those of length 6, the
latter are three times as long, so they should have triple the chance of being chosen. So, the chance
of our choosing a stick of length j depends not only on pX (j) but also on j itself.

We could write that formally as

pY (j) ∝ jpX (j)

(9.38)

186

CHAPTER 9. THE EXPONENTIAL DISTRIBUTIONS

where ∝ is the “is proportional to” symbol. Thus

pY (j) = cjpX (j)

(9.39)

for some constant of proportionality c.

But a probability mass function must sum to 1. So, summing over all possible values of j (whatever
they are), we have

(cid:88)

1 =

pY (j) =

j

(cid:88)

j

cjpX (j)

That last term is c E(X)! So, c = 1/EX, and

The continuous analog of (9.41) is

pY (j) =

1
EX

· jpX (j)

fY (t) =

1
EX

· tfX (t)

So, for our bus example, in which fX (t) = 0.1e−0.1t, t > 0 and EX = 10,

fY (t) = 0.01te−0.1t

(9.40)

(9.41)

(9.42)

(9.43)

You may recognize this as an Erlang density with r = 2 and λ = 0.1. That distribution does indeed
have mean 20, consistent with the discussion at the end of Section 9.4.1.

Chapter 10

Stop and Review: Probability
Structures

There’s quite a lot of material in the preceding chapters, but it’s crucial that you have a good
command of it before proceeding, as the coming chapters will continue to build on it.

With that aim, here are the highlights of what we’ve covered so far, with links to the places at
which they were covered:

• expected value (Section 3.5):

Consider random variables X and Y (not assumed independent), and constants c1 and c2. We
have:

E(X + Y ) = EX + EY

E(c1X) = c1EX

E(c1X + c2Y ) = c1EX + c2EY

(10.1)

(10.2)

(10.3)

By induction,

E(a1U1 + ... + akUk) = a1EX1 + ... + akEXk

(10.4)

for random variables Ui and constants ai.

187

188

CHAPTER 10. STOP AND REVIEW: PROBABILITY STRUCTURES

• variance (Section 3.6):

For any variable W,

V ar(W ) = E[(W − EW )2] = E(W 2) − (EW )2

(10.5)

Consider random variables X and Y (now assumed independent), and constants c1 and c2.
We have:

V ar(X + Y ) = V ar(X) + V ar(Y )

V ar(c1X) = c2

1V ar(X)

By induction,

V ar(a1U1 + ... + akUk) = a2

1V ar(U1) + ... + a2

kV ar(Uk)

(10.6)

(10.7)

(10.8)

for independent random variables Ui and constants ai.

• indicator random variables (Section 3.9):

Equal 1 or 0, depending on whether a speciﬁed event A occurs.

If T is an indicator random variable for the event A, then

ET = P (A), V ar(T ) = P (A)[1 − P (A)]

(10.9)

• distributions:

– cdfs(Section 7.3):

For any random variable X,

FX (t) = P (X ≤ t),

− ∞ < t < ∞

(10.10)

– pmfs (Section 3.13):

For a discrete random variable X,

pX (k) = P (X = k)

(10.11)

189

– density functions (Section 3.13):

For a continuous random variable X,

and

fX (t) =

d
dt

FX (t),

− ∞ < t < ∞

(10.12)

P (X in A) =

(cid:90)

A

fX (s) ds

(10.13)

• famous parametric families of distributions:

Just like one can have a family of curves, say sin(2πnθ(t) (diﬀerent curve for each n and θ),
certain families of distributions have been found useful. They’re called parametric families,
because they are indexed by one or more parameters, anlagously to n and θ above.

discrete:

– geometric (Section 4.2)

Number of i.i.d. trials until ﬁrst success. For success probability p:

pN (k) = (1 − p)kp

EN = 1/p, V ar(N ) =

1 − p
p2

– binomial (Section 4.3):

Number of successes in n i.i.d. trials, probability p of success per trial:

pN (k) =

(cid:19)

(cid:18)n
k

pk(1 − p)n−k

EN = np, V ar(N ) = np(1 − p)

– Poisson (Section 4.5):

Has often been found to be a good model for counts over time periods.
One parameter, often called λ. Then

pN (k) =

e−λλk
k!

, k = 0, 1, 2, ...

EN = V ar(N ) = λ

(10.14)

(10.15)

(10.16)

(10.17)

(10.18)

(10.19)

190

CHAPTER 10. STOP AND REVIEW: PROBABILITY STRUCTURES

– negative binomial (Section 4.4):

Number of i.i.d. trials until rth success. For success probability p:

pN (k) =

(cid:18)k − 1
r − 1

(cid:19)

(1 − p)k−rpr, k = r, r + 1, ...

E(N ) = r ·

1
p

, V ar(N ) = r ·

1 − p
p2

continuous:

– uniform (Section 7.6.1.1):

All points “equally likely.” If the interval is (q,r),

fX (t) =

1
r − q

,

q < t < r

EX ==

q + r
2

, V ar(D) =

1
12

(r − q)2

(10.20)

(10.21)

(10.22)

(10.23)

– normal (Gaussian) (Section 7.6.2):

“Bell-shaped curves.” Useful due to Central Limit Theorem (Section 8.9. (Thus good
approximation to binomial distribution.)
Closed under aﬃne transformations (Section 8.1.1)!
Parameterized by mean and variance, µ and σ2:

fX (t) =

√

1
2πσ

e−0.5( t−µ

σ )2

, −∞ < t < ∞

exponential (Section 7.6.3):

– Memoryless! One parameter, usually called λ. Connectedt to Poisson family.

fX (t) == λe−λt, 0 < t < ∞

EX = 1/λ, V ar(X) = 1/λ2

(10.24)

(10.25)

(10.26)

– gamma (Section 7.6.4):

Special case, Erlang family, arises as the distribution of the sum of i.i.d. exponential
random variables.

fX (t) =

1
Γ(r)

λrtr−1e−λt, t > 0

(10.27)

191

• iterated expected values:

– For discrete U (4.68),

– For continuous V (7.77),

E(V ) =

(cid:88)

c

P (U = c) E(V | U = c)

(10.28)

E(W ) =

(cid:90) ∞

−∞

fV (t) E(W | V = t) dt

(10.29)

192

CHAPTER 10. STOP AND REVIEW: PROBABILITY STRUCTURES

Chapter 11

Introduction to Continuous-Time
Markov Chains

In the Markov chains we analyzed in Chapter 6, events occur only at integer times. However, many
Markov chain models are of the continuous-time type, in which events can occur at any times.
Here the holding time, i.e. the time the system spends in one state before changing to another
state, is a continuous random variable.

11.1 Continuous-Time Markov Chains

The state of a Markov chain at any time now has a continuous subscript. Instead of the chain
consisting of the random variables Xn, n = 1, 2, 3, ..., it now consists of {Xt : t ∈ [0, ∞)}. The
Markov property is now

P (Xt+u = k|Xs for all 0 ≤ s ≤ t) = P (Xt+u = k|Xt) for all t, u ≥ 0

(11.1)

11.2 Holding-Time Distribution

In order for the Markov property to hold, the distribution of holding time at a given state needs
to be “memoryless.” Recall that exponentially distributed random variables have this property. In
other words, if a random variable W has density

f (t) = λe−λt

193

(11.2)

194

CHAPTER 11.

INTRODUCTION TO CONTINUOUS-TIME MARKOV CHAINS

for some λ then

P (W > r + s|W > r) = P (W > s)

(11.3)

for all positive r and s. And exponential distributions are the only continuous distributions which
have this property. Therefore, holding times in Markov chains must be exponentially distributed.

Because it is central to the Markov property, the exponential distribution is assumed for all basic
activities in Markov models. In queuing models, for instance, both the interarrival time and service
time are assumed to be exponentially distributed (though of course with diﬀerent values of λ). In
reliability modeling, the lifetime of a component is assumed to have an exponential distribution.

Such assumptions have in many cases been veriﬁed empirically. If you go to a bank, for example, and
record data on when customers arrive at the door, you will ﬁnd the exponential model to work well
(though you may have to restrict yourself to a given time of day, to account for nonrandom eﬀects
such as heavy traﬃc at the noon hour). In a study of time to failure for airplane air conditioners,
the distribution was also found to be well ﬁtted by an exponential density. On the other hand, in
many cases the distribution is not close to exponential, and purely Markovian models cannot be
used for anything more than a rough approximation.

11.2.1 The Notion of “Rates”

A key point is that the parameter λ in (11.2) has the interpretation of a rate, in the sense discussed
in Theorem 18. To review, ﬁrst, recall that 1/λ is the mean. Say light bulb lifetimes have an
exponential distribution with mean 100 hours, so λ = 0.01. In our lamp, whenever its bulb burns
out, we immediately replace it with a new on. Imagine watching this lamp for, say, 100,000 hours.
During that time, we will have done approximately 100000/100 = 1000 replacements. That would
be using 1000 light bulbs in 100000 hours, so we are using bulbs at the rate of 0.01 bulb per hour.
For a general λ, we would use light bulbs at the rate of λ bulbs per hour. This concept is crucial
to what follows.

11.3 Stationary Distribution

In analogy to (6.4), we again deﬁne πi to be the long-run proportion of time the system is in state
i, where now Nit is the proportion of the time spent in state i, during (0,t). We again will derive a
system of linear equations to solve for these proportions, using a ﬂow out = ﬂow in argument.

11.3. STATIONARY DISTRIBUTION

195

11.3.1 Intuitive Derivation

To this end, let λi denote the parameter in the holding-time distribution at state i, and deﬁne the
quantities

ρrs = λrprs

(11.4)

where prs is that probably that, when a jump out of state r occurs, the jump is to state s.

The equations has the following interpretation. Note:

• λr is the rate of jumps out of state r, so

• λrprs is the rate of jumps from state r to state s, and since

• πr is the long-run proportion of the time we are in state r, then

• πrλrprs is the rate of jumps from r to s

Then, equating the rate of transitions into i and the rate out of i, we have

πiλi =

(cid:88)

j(cid:54)=i

πjλjpji

These equations can then be solved for the πi.

11.3.2 Computation

Motivated by (11.5), deﬁne the matrix Q by

qij =

(cid:40)

λjpji,
−λi,

if i (cid:54)= j
if i = j

(11.5)

(11.6)

Q is called the inﬁnitesimal generator of the system, so named because it is the basis of the
system of diﬀerential equations that can be used to ﬁnd the ﬁnite-time probabilistic behavior of
Xt.

Then (11.5) is stated in matrix form as

Qπ = 0

(11.7)

196

CHAPTER 11.

INTRODUCTION TO CONTINUOUS-TIME MARKOV CHAINS

But the πi must sum to 1, so the above equation is subject to

1(cid:48)π = 1

(11.8)

where 1 denotes a (column) vector of n 1s, where n is the number of states.

In view of (11.8), the system (11.7) is redundant; there are n equations for n-1 unknowns, So,
replace the last equation in (11.7) by (11.8).

Here is R code to solve the system:

f i n d p i c o n t i n <− function ( q ) {

n <− nrow( q )
q [ n , ] <− rep ( 1 , n )
r h s <− c ( rep ( 0 , n −1) ,1)
p i v e c <− solve ( q , r h s )
return ( p i v e c )

}

To formulate the equations (11.5), we’ll need a property of exponential distributions derived in
Section 9.3, copied here for convenience:

Theorem 20 Suppose W1, ..., Wk are independent random variables, with Wi being exponentially
distributed with parameter λi. Let Z = min(W1, ..., Wk). Then

(a) Z is exponentially distributed with parameter λ1 + ... + λk

(b) P (Z = Wi) =

λi
λ1+...+λk

11.4 Example: Machine Repair

Suppose the operations in a factory require the use of a certain kind of machine. The manager has
installed two of these machines. This is known as a gracefully degrading system: When both
machines are working, the fact that there are two of them, instead of one, leads to a shorter wait
time for access to a machine. When one machine has failed, the wait is longer, but at least the
factory operations may continue. Of course, if both machines fail, the factory must shut down until
at least one machine is repaired.

Suppose the time until failure of a single machine, carrying the full load of the factory, has an
exponential distribution with mean 20.0, but the mean is 25.0 when the other machine is working,
since it is not so loaded. Repair time is exponentially distributed with mean 8.0.

11.4. EXAMPLE: MACHINE REPAIR

197

We can take as our state space {0,1,2}, where the state is the number of working machines. Now,
let us ﬁnd the parameters λi and pji for this system. For example, what about λ2? The holding
time in state 2 is the minimum of the two lifetimes of the machines, and thus from the results of
Section 9.3, has parameter

1

25.0 + 1

25.0 = 0.08.

For λ1, a transition out of state 1 will be either to state 2 (the down machine is repaired) or to state
0 (the up machine fails). The time until transition will be the minimum of the lifetime of the up
8.0 = 0.175.
machine and the repair time of the down machine, and thus will have parameter
Similarly, λ0 = 1

20.0 + 1

1

8.0 + 1

8.0 = 0.25.

It is important to understand how the Markov property is being used here. Suppose we are in state
1, and the down machine is repaired, sending us into state 2. Remember, the machine which had
already been up has “lived” for some time now. But the memoryless property of the exponential
distribution implies that this machine is now “born again.”

What about the parameters pji? Well, p21 is certainly easy to ﬁnd; since the transition 2 → 1 is
the only transition possible out of state 2, p21 = 1.

For p12, recall that transitions out of state 1 are to states 0 and 2, with rates 1/20.0 and 1/8.0,
respectively. So,

p12 =

1/8.0
1/20.0 + 1/8.0

= 0.72

Working in this manner, we ﬁnally arrive at the complete system of equations (11.5):

π2(0.08) = π1(0.125)
π1(0.175) = π2(0.08) + π0(0.25)
π0(0.25) = π1(0.05)

In matrix terms:





0

Let’s ﬁnd the solution:

> q

[ , 1 ]
[ 1 , ] −0.25

[ , 2 ]
0 . 0 5 0

[ , 3 ]
0 . 0 0

−0.25

0.05
0.25 −0.175

0
0.08
0.125 −0.08



 π = 0

(11.9)

(11.10)

(11.11)

(11.12)

(11.13)

198

CHAPTER 11.

INTRODUCTION TO CONTINUOUS-TIME MARKOV CHAINS

0 . 0 8
0 . 1 2 5 −0.08

0 . 2 5 −0.175
[ 2 , ]
[ 3 , ]
0 . 0 0
> f i n d p i c o n t i n ( q )
[ 1 ] 0 . 0 7 2 3 9 8 1 9 0 . 3 6 1 9 9 0 9 5 0 . 5 6 5 6 1 0 8 6

So,

π = (0.072, 0.362, 0.566)

(11.14)

Thus for example, during 7.2% of the time, there will be no machine available at all.

Several variations of this problem could be analyzed. We could compare the two-machine system
with a one-machine version. It turns out that the proportion of down time (i.e. time when no
machine is available) increases to 28.6%. Or we could analyze the case in which only one repair
person is employed by this factory, so that only one machine can be repaired at a time, compared
to the situation above, in which we (tacitly) assumed that if both machines are down, they can be
repaired in parallel. We leave these variations as exercises for the reader.

11.5 Example: Migration in a Social Network

The following is a simpliﬁed version of research in online social networks.

There is a town with two social groups, with each person being in exactly one group. People arrive
from outside town, with exponentially distributed interarrival times at rate α, and join one of the
groups with probability 0.5 each. Each person will occasionally switch groups, with one possible
“switch” being to leave town entirely. A person’s time before switching is exponentially distributed
with rate σ; the switch will either be to the other group or to the outside world, with probabilities
q and 1-q, respectively. Let the state of the system be (i,j), where i and j are the number of current
members in groups 1 and 2, respectively.

Let’s ﬁnd a typical balance equation, say for the state (8,8):

π(8,8)(α + 16 · σ) = (π(9,8) + π(8,9)) · 9σ(1 − q) + (π(9,7) + π(7,9)) · 9σq + (π(8,7) + π(7,8)) · 0.5α (11.15)

The reasoning is straightforward. How can we move out of state (8,8)? Well, there could be an
arrival (rate α), or any one of the 16 people could switch groups (rate 16σ), etc.

Now, in a “going beyond ﬁnding the π” vein, let’s ﬁnd the long-run fraction of transfers into group
1 that come from group 2, as opposed to from the outside.

11.6. BIRTH/DEATH PROCESSES

199

The rate of transitions into that group from outside is 0.5α. When the system is in state (i,j), the
rate of transitions into group 1 from group 2 is jσq, so the overall rate is (cid:80)
i,j π(i,j)jσq. Thus the
fraction of new members coming in to group 1 from transfers is

i,j π(i,j)jσq

(cid:80)
0.5α + (cid:80)

i,j π(i,j)jσq

(11.16)

The above reasoning is very common, quite applicable in many situations. By the way, note that
(cid:80)

i,j π(i,j)jσq = σqEN , where N is the number of members of group 2.

11.6 Birth/Death Processes

We noted earlier that the system of equations for the πi may not be easy to solve. In many cases, for
instance, the state space is inﬁnite and thus the system of equations is inﬁnite too. However, there is
a rich class of Markov chains for which closed-form solutions have been found, called birth/death
processes.1

Here the state space consists of (or has been mapped to) the set of nonnegative integers, and pji
is nonzero only in cases in which |i − j| = 1. (The name “birth/death” has its origin in Markov
models of biological populations, in which the state is the current population size.) Note for
instance that the example of the gracefully degrading system above has this form. An M/M/1
queue—one server, “Markov” (i.e. exponential) interarrival times and Markov service times—is
also a birth/death process, with the state being the number of jobs in the system.

Because the pji have such a simple structure, there is hope that we can ﬁnd a closed-form solution
for the πi, and it turns out we can. Let ui = ρi,i+1 and di = ρi,i−1 (‘u’ for “up,” ‘d’ for “down”).
Then (??) is

πi+1di+1 + πi−1ui−1 = πiλi = πi(ui + di), i ≥ 1

(11.17)

π1d1 = π0λ0 = π0u0

(11.18)

In other words,

πi+1di+1 − πiui = πidi − πi−1ui−1, i ≥ 1

(11.19)

1Though we treat the continuous-time case here, there is also a discrete-time analog.

200

CHAPTER 11.

INTRODUCTION TO CONTINUOUS-TIME MARKOV CHAINS

Applying (11.19) recursively to the base (11.20), we see that

π1d1 − π0u0 = 0

(11.20)

πidi − πi−1ui−1 = 0, i ≥ 1

(11.21)

so that

and thus

where

πi = πi−1

ui−1
di

i ≥ 1

πi = π0ri

ri = Πi

k=1

uk−1
dk

where ri = 0 for i > m if the chain has no states past m.

Then since the πi must sum to 1, we have that

π0 =

1

1 + (cid:80)∞

i=1 ri

(11.22)

(11.23)

(11.24)

(11.25)

and the other πi are then found via (11.23).

Note that the chain might be ﬁnite, i.e. have ui = 0 for some i. In that case it is still a birth/death
chain, and the formulas above for π still apply.

11.7 Cell Communications Model

Let’s consider a more modern example of this sort, involving cellular phone systems. (This is an
extension of the example treated in K.S. Trivedi, Probability and Statistics, with Reliability and
Computer Science Applications (second edition), Wiley, 2002, Sec. 8.2.3.2, which is in turn is based
on two papers in the IEEE Transactions on Vehicular Technology.)

11.7. CELL COMMUNICATIONS MODEL

201

We consider one particular cell in the system. Mobile phone users drift in and out of the cell as
they move around the city. A call can either be a new call, i.e. a call which someone has just
dialed, or a handoﬀ call, i.e. a call which had already been in progress in a neighboring cell but
now has moved to this cell.

Each call in a cell needs a channel.2 There are n channels available in the cell. We wish to give
handoﬀ calls priority over new calls.3 This is accomplished as follows.

The system always reserves g channels for handoﬀ calls. When a request for a new call (i.e. a
non-handoﬀ call) arrives, the system looks at Xt, the current number of calls in the cell. If that
number is less than n-g, so that there are more than g idle channels available, the new call is
accepted; otherwise it is rejected.

We assume that new calls originate from within the cells according to a Poisson process with rate
λ1, while handoﬀ calls drift in from neighboring cells at rate λ2. Meanwhile, call durations are
exponential with rate µ1, while the time that a call remains within the cell is exponential with rate
µ2.

11.7.1 Stationary Distribution

We again have a birth/death process, though a bit more complicated than our earlier ones. Let
λ = λ1 + λ2 and µ = µ1 + µ2. Then here is a sample balance equation, focused on transitions into
(left-hand side in the equation) and out of (right-hand side) state 1:

π0λ + π22µ = π1(λ + µ)

(11.26)

Here’s why: How can we enter state 1? Well, we could do so from state 0, where there are no calls;
this occurs if we get a new call (rate λ1) or a handoﬀ call (rate λ2. In state 2, we enter state 1 if
one of the two calls ends (rate µ1) or one of the two calls leaves the cell (rate µ2). The same kind
of reasoning shows that we leave state 1 at rate λ + µ.

As another example, here is the equation for state n-g:

πn−g[λ2 + (n − g)µ] = πn−g+1 · (n − g + 1)µ + πn−g−1λ

(11.27)

Note the term λ2 in (11.27), rather than λ as in (11.26).

2This could be a certain frequency or a certain time slot position.
3We would rather give the caller of a new call a polite rejection message, e.g. “No lines available at this time,

than suddenly terminate an existing conversation.

202

CHAPTER 11.

INTRODUCTION TO CONTINUOUS-TIME MARKOV CHAINS

Using our birth/death formula for the πi, we ﬁnd that

πk =

(cid:40)

π0
π0

Ak
k! ,
k! Ak−(n−g)
An−g

1

k ≤ n-g

, k ≥ n-g

where A = λ/µ, A1 = λ2/µ and

π0 =





n−g−1
(cid:88)

k=0

Ak
k!

+

n
(cid:88)

k=n−g

An−g
k!



−1

Ak−(n−g)

1



11.7.2 Going Beyond Finding the π

One can calculate a number of interesting quantities from the πi:

• The probability of a handoﬀ call being rejected is πn.

• The probability of a new call being blocked is

n
(cid:88)

πk

k=n−g

(11.28)

(11.29)

(11.30)

• Since the per-channel utilization in state i is i/n, the overall long-run per-channel utilization

is

n
(cid:88)

i=0

πi

i
n

(11.31)

• The long-run proportion of accepted calls which are handoﬀ calls is the rate at which handoﬀ

calls are accepted, divided by the rate at which calls are accepted:

λ2
(cid:80)n−g−1
i=0

(cid:80)n−1

i=0 πi
πi + λ2

(cid:80)n−1

i=0 πi

λ1

(11.32)

Chapter 12

Covariance and Random Vectors

Most applications of probability and statistics involve the interaction between variables. For in-
stance, when you buy a book at Amazon.com, the software will likely inform you of other books
that people bought in conjunction with the one you selected. Amazon is relying on the fact that
sales of certain pairs or groups of books are correlated.

Thus we need the notion of distributions that describe how two or more variables vary together.
This chapter develops that notion, which forms the very core of statistics.

12.1 Measuring Co-variation of Random Variables

12.1.1 Covariance

Deﬁnition 21 The covariance between random variables X and Y is deﬁned as

Cov(X, Y ) = E[(X − EX)(Y − EY )]

(12.1)

Suppose that typically when X is larger than its mean, Y is also larger than its mean, and vice
versa for below-mean values. Then (12.1) will likely be positive. In other words, if X and Y are
positively correlated (a term we will deﬁne formally later but keep intuitive for now), then their
covariance is positive. Similarly, if X is often smaller than its mean whenever Y is larger than its
mean, the covariance and correlation between them will be negative. All of this is roughly speaking,
of course, since it depends on how much and how often X is larger or smaller than its mean, etc.

203

204

CHAPTER 12. COVARIANCE AND RANDOM VECTORS

Linearity in both arguments:

Cov(aX + bY, cU + dV ) = acCov(X, U ) + adCov(X, V ) + bcCov(Y, U ) + bdCov(Y, V )

(12.2)

for any constants a, b, c and d.

Insensitivity to additive constants:

Cov(X, Y + q) = Cov(X, Y )

(12.3)

for any constant q and so on.

Covariance of a random variable with itself:

Cov(X, X) = V ar(X)

(12.4)

for any X with ﬁnite variance.

Shortcut calculation of covariance:

Cov(X, Y ) = E(XY ) − EX · EY

(12.5)

The proof will help you review some important issues, namely (a) E(U+V) = EU + EV, (b) E(cU)
= c EU and Ec = c for any constant c, and (c) EX and EY are constants in (12.5).

Cov(X, Y ) = E[(X − EX)(Y − EY )] (deﬁnition)

= E [XY − EX · Y − EY · X + EX · EY ]

(algebra)

(12.6)

(12.7)

= E(XY ) + E[−EX · Y ] + E[−EY · X] + E[EX · EY ] (E[U+V]=EU+EV)(12.8)

= E(XY ) − EX · EY (E[cU] = cEU, Ec = c)

(12.9)

Variance of sums:

V ar(X + Y ) = V ar(X) + V ar(Y ) + 2Cov(X, Y )

(12.10)

This comes from (12.5), the relation V ar(X) = E(X 2) − EX 2 and the corresponding one for Y.
Just substitute and do the algebra.

12.1. MEASURING CO-VARIATION OF RANDOM VARIABLES

205

By induction, (12.10) generalizes for more than two variables:

V ar(W1 + ... + Wr) =

r
(cid:88)

i=1

V ar(Wi) + 2

(cid:88)

1≤j<i≤r

Cov(Wi, Wj)

(12.11)

12.1.2 Example: Variance of Sum of Nonindependent Variables

Consider random variables X1 and X2, for which V ar(Xi) = 1.0 for i = 1,2, and Cov(X1, X2) = 0.5.
Let’s ﬁnd V ar(X1 + X2).

This is quite straightforward, from (12.10):

V ar(X1 + X2) = V ar(X1) + V ar(X2) + 2Cov(X1, X2) = 3

(12.12)

12.1.3 Example: the Committee Example Again

Let’s ﬁnd Var(M) in the committee example of Section 3.9.2. In (3.97), we wrote M as a sum of
indicator random variables:

and found that

for all i.

M = G1 + G2 + G3 + G4

P (Gi = 1) =

2
3

(12.13)

(12.14)

You should review why this value is the same for all i, as this reasoning will be used again below.
Also review Section 3.9.

Applying (12.11) to (12.13), we have

V ar(M ) = 4V ar(G1) + 12Cov(G1.G2)

(12.15)

Finding that ﬁrst term is easy, from (3.79):

V ar(G1) =

(cid:18)

·

1 −

(cid:19)

2
3

=

2
9

2
3

(12.16)

206

CHAPTER 12. COVARIANCE AND RANDOM VECTORS

Now, what about Cov(G1.G2)? Equation (12.5) will be handy here:

Cov(G1.G2) = E(G1G2) − E(G1)E(G2)

(12.17)

That ﬁrst term in (12.17) is

E(G1G2) = P (G1 = 1 and G2 = 1)

= P (choose a man on both the ﬁrst and second pick)

=

=

5
8

·

6
9
5
12

That second term in (12.17) is, again from Section 3.9,

(cid:19)2

(cid:18) 2
3

=

4
9

All that’s left is to put this together in (12.15), left to the reader.

12.2 Correlation

(12.18)

(12.19)

(12.20)

(12.21)

(12.22)

Covariance does measure how much or little X and Y vary together, but it is hard to decide
whether a given value of covariance is “large” or not. For instance, if we are measuring lengths in
feet and change to inches, then (12.2) shows that the covariance will increase by 122 = 144. Thus
it makes sense to scale covariance according to the variables’ standard deviations. Accordingly, the
correlation between two random variables X and Y is deﬁned by

ρ(X, Y ) =

Cov(X, Y )
(cid:112)V ar(X)(cid:112)V ar(Y )

(12.23)

So, correlation is unitless, i.e. does not involve units like feet, pounds, etc.

It is shown later in this chapter that

• −1 ≤ ρ(X, Y ) ≤ 1

• |ρ(X, Y )| = 1 if and only if X and Y are exact linear functions of each other, i.e. Y = cX +

d for some constants c and d

12.3. SETS OF INDEPENDENT RANDOM VARIABLES

207

12.2.1 Example: a Catchup Game

Consider the following simple game. There are two players, who take turns playing. One’s position
after k turns is the sum of one’s winnings in those turns. Basically, a turn consists of generating a
random U(0,1) variable, with one diﬀerence—if that player is currently losing, he gets a bonus of
0.2 to help him catch up.

Let X and Y be the total winnings of the two players after 10 turns. Intuitively, X and Y should
be positively correlated, due to the 0.2 bonus which brings them closer together. Let’s see if this is
true.

Though very simply stated, this problem is far too tough to solve mathematically in an elementary
course (or even an advanced one). So, we will use simulation. In addition to ﬁnding the correlation
between X and Y, we’ll also ﬁnd FX,Y (5.8, 5.2) = P (X ≤ 5.8 and Y ≤ 5.2).

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

taketurn <- function(a,b) {

win <- runif(1)
if (a >= b) return(win)
else return(win+0.2)

}

nturns <- 10
xyvals <- matrix(nrow=nreps,ncol=2)
for (rep in 1:nreps) {

x <- 0
y <- 0
for (turn in 1:nturns) {

# x’s turn
x <- x + taketurn(x,y)
# y’s turn
y <- y + taketurn(y,x)

}
xyvals[rep,] <- c(x,y)

}
print(cor(xyvals[,1],xyvals[,2]))

The output is 0.65. So, X and Y are indeed positively correlated as we had surmised.

Note the use of R’s built-in function cor() to compute correlation, a shortcut that allows us to
avoid summing all the products xy and so on, from (12.5). The reader should make sure he/she
understands how this would be done.

12.3 Sets of Independent Random Variables

Recall from Section 3.3:

208

CHAPTER 12. COVARIANCE AND RANDOM VECTORS

Deﬁnition 22 Random variables X and Y are said to be independent if for any sets I and J,
the events {X is in I} and {Y is in J} are independent, i.e. P(X is in I and Y is in J) = P(X is
in I) P(Y is in J).

Intuitively, though, it simply means that knowledge of the value of X tells us nothing about the
value of Y, and vice versa.

Great mathematical tractability can be achieved by assuming that the Xi in a random vector
X = (X1, ..., Xk) are independent. In many applications, this is a reasonable assumption.

12.3.1 Properties

In the next few sections, we will look at some commonly-used properties of sets of independent
random variables. For simplicity, consider the case k = 2, with X and Y being independent (scalar)
random variables.

12.3.1.1 Expected Values Factor

If X and Y are independent, then

E(XY ) = E(X)E(Y )

(12.24)

12.3.1.2 Covariance Is 0

If X and Y are independent, we have

and thus

ρ(X, Y ) = 0 as well.

This follows from (12.24) and (12.5).

Cov(X, Y ) = 0

(12.25)

However, the converse is false. A counterexample is the random pair (X, Y ) that is uniformly
distributed on the unit disk, {(s, t) : s2 + t2 ≤ 1}. Clearly 0 = E(XY) = EX = EY due to the
symmetry of the distribution about (0,0), so Cov(X,Y) = 0 by (12.5).

But X and Y just as clearly are not independent. If for example we know that X > 0.8, say, then
Y 2 < 1 − 0.82 and thus |Y | < 0.6. If X and Y were independent, knowledge of X should not tell

12.3. SETS OF INDEPENDENT RANDOM VARIABLES

209

us anything about Y, which is not the case here, and thus they are not independent. If we also
know that X and Y are bivariate normally distributed (Section 13.5.2.1), then zero covariance does
imply independence.

12.3.1.3 Variances Add

If X and Y are independent, then we have

V ar(X + Y ) = V ar(X) + V ar(Y ).

(12.26)

This follows from (12.10) and (12.24).

12.3.2 Examples Involving Sets of Independent Random Variables

12.3.2.1 Example: Dice

In Section 12.2.1, we speculated that the correlation between X, the number on the blue die, and
S, the total of the two dice, was positive. Let’s compute it.

Write S = X + Y, where Y is the number on the yellow die. Then using the properties of covariance
presented above, we have that

Cov(X, S) = Cov(X, X + Y ) (def. of S)

= Cov(X, X) + Cov(X, Y ) (from (12.2))

= V ar(X) + 0 (from (12.4), (12.25))

(12.27)

(12.28)

(12.29)

Also, from (12.26),

V ar(S) = V ar(X + Y ) = V ar(X) + V ar(Y )

(12.30)

But Var(Y) = Var(X). So the correlation between X and S is

ρ(X, S) =

V ar(X)
(cid:112)V ar(X)(cid:112)2V ar(X)

= 0.707

(12.31)

Since correlation is at most 1 in absolute value, 0.707 is considered a fairly high correlation. Of
course, we did expect X and S to be highly correlated.

210

CHAPTER 12. COVARIANCE AND RANDOM VECTORS

12.3.2.2 Example: Variance of a Product

Suppose X1 and X2 are independent random variables with EXi = µi and V ar(Xi) = σ2
Let’s ﬁnd an expression for V ar(X1X2).

i , i = 1,2.

V ar(X1X2) = E(X 2
= E(X 2
1 + µ2
= (σ2
2 + µ2
1σ2
= σ2

1 X 2
1 ) · E(X 2
1)(σ2
1σ2

2 ) − [E(X1X2)]2
2 ) − µ2
2 + µ2
2 + µ2

1µ2
2
2) − µ2
2σ2
1

1µ2
2

(3.41)

(12.24)

(12.32)

(12.33)

(12.34)

(12.35)

Note that E(X 2

1 ) = σ2

1 + µ2

1 by virtue of (3.41).

12.3.2.3 Example: Ratio of Independent Geometric Random Variables

Suppose X and Y are independent geometrically distributed random variables with success proba-
bility p. Let Z = X/Y. We are interested in EZ and FZ.

First, by (12.24), we have

EZ = E(X ·

1
Y

= EX · E(

=

1
p

· E(

1
Y

)

1
Y

)

)

(12.24)

(mean of geom is 1/p)

So we need to ﬁnd E(1/Y). Using (3.36), we have

E(

1
Y

) =

∞
(cid:88)

i=1

1
i

(1 − p)i−1p

Unfortunately, no further simpliﬁcation seems possible.

Now let’s ﬁnd FZ(m) for a positive integer m.

(12.36)

(12.37)

(12.38)

(12.39)

12.4. MATRIX FORMULATIONS

FZ(m) = P

(cid:19)

(cid:18) X
Y
= P (X ≤ mY )

≤ m

=

=

=

∞
(cid:88)

i=1
∞
(cid:88)

i=1
∞
(cid:88)

i=1

P (Y = i) P (X ≤ mY |Y = i)

(1 − p)i−1p P (X ≤ mi)

(1 − p)i−1p [1 − (1 − p)mi]

211

(12.40)

(12.41)

(12.42)

(12.43)

(12.44)

this last step coming from (4.16).

We can actually reduce (12.44) to closed form, by writing

(1 − p)i−1(1 − p)mi = (1 − p)mi+i−1 =

1
1 − p

(cid:2)(1 − p)m+1(cid:3)i

(12.45)

and then using (4.6). Details are left to the reader.

12.4 Matrix Formulations

(Note that there is a review of matrix algebra in Appendix B.)

In your ﬁrst course in matrices and linear algebra, your instructor probably motivated the notion
of a matrix by using an example involving linear equations, as follows.

Suppose we have a system of equations

ai1x1 + ... + ainxn = bi,

i = 1, ..., n,

(12.46)

where the xi are the unknowns to be solved for.

This system can be represented compactly as

where A is nxn and X and B are nx1.

AX = B,

(12.47)

212

CHAPTER 12. COVARIANCE AND RANDOM VECTORS

That compactness coming from the matrix formulation applies to statistics too, though in diﬀerent
ways, as we will see. (Linear algebra in general is used widely in statistics—matrices, rank and
subspace, eigenvalues, even determinants.)

When dealing with multivariate distributions, some very messy equations can be greatly compact-
iﬁed through the use of matrix algebra. We will introduce this here.

Throughout this section, consider a random vector W = (W1, ..., Wk)(cid:48) where (cid:48) denotes matrix
transpose, and a vector written horizontally like this without a (cid:48) means a row vector.

12.4.1 Properties of Mean Vectors

In statistics, we frequently need to ﬁnd covariance matrices of linear combinations of random
vectors.

Deﬁnition 23 The expected value of W is deﬁned to be the vector

EW = (EW1, ..., EWk)(cid:48)

(12.48)

The linearity of the components implies that of the vectors:

For any scalar constants c and d, and any random vectors V and W, we have

E(cV + dW ) = cEV + dEW

(12.49)

where the multiplication and equality is now in the vector sense.

Also, multiplication by a constant matrix factors:

If A is a nonrandom matrix having k columns, then

E(AW ) = AEW

(12.50)

12.4.2 Covariance Matrices

Deﬁnition 24 The covariance matrix Cov(W) of W = (W1, ..., Wk)(cid:48) is the k x k matrix whose
(i, j)th element is Cov(Wi, Wj).

Note that that implies that the diagonal elements of the matrix are the variances of the Wi, and
that the matrix is symmetric.

12.4. MATRIX FORMULATIONS

213

As you can see, in the statistics world, the Cov() notation is “overloaded.” If it has two arguments,
it is ordinary covariance, between two variables. If it has one argument, it is the covariance matrix,
consisting of the covariances of all pairs of components in the argument. When people mean the
matrix form, they always say so, i.e. they say “covariance MATRIX” instead of just “covariance.”

The covariance matrix is just a way to compactly do operations on ordinary covariances. Here are
some important properties:

Say c is a constant scalar. Then cW is a k-component random vector like W, and

Cov(cW ) = c2Cov(W )

(12.51)

Suppose V and W are independent random vectors, meaning that each component in V is inde-
pendent of each component of W. (But this does NOT mean that the components within V are
independent of each other, and similarly for W.) Then

Cov(V + W ) = Cov(V ) + Cov(W )

(12.52)

Of course, this is also true for sums of any (nonrandom) number of independent random vectors.

In analogy with (3.41), for any random vector Q,

Cov(Q) = E(QQ(cid:48)) − EQ (EQ)(cid:48)

(12.53)

12.4.3 Covariance Matrices Linear Combinations of Random Vectors

Suppose A is an r x k but nonrandom matrix. Then AW is an r-component random vector, with
its ith element being a linear combination of the elements of W. Then one can show that

Cov(AW ) = A Cov(W ) A(cid:48)

(12.54)

An important special case is that in which A consists of just one row. In this case AW is a vector
of length 1—a scalar! And its covariance matrix, which is of size 1 × 1, is thus simply the variance
of that scalar. In other words:

Suppose we have a random vector U = (U1, ..., Uk)(cid:48) and are interested in the variance
of a linear combination of the elements of U,

Y = c1U1 + ... + ckUk

(12.55)

214

CHAPTER 12. COVARIANCE AND RANDOM VECTORS

for a vector of constants c = (c1, ..., ck)(cid:48).
Then

V ar(Y ) = c(cid:48)Cov(U )c

(12.56)

12.4.4 Example: (X,S) Dice Example Again

Recall Sec. 12.3.2.1. We rolled two dice, getting X and Y dots, and set S to X+Y. We then found
ρ(X, S). Let’s ﬁnd ρ(X, S) using matrix methods.

The key is ﬁnding a proper choice for A in (12.54). A little thought shows that

(cid:18) X
S

(cid:19)

(cid:18) 1 0
1 1

(cid:19) (cid:18) X
Y

=

(cid:19)

Thus the covariance matrix of (X,S)’ is

Cov[(X, S)(cid:48)] =

=

=

(cid:19) (cid:18) V ar(X)

(cid:18) 1 0
1 1
(cid:18) V ar(X)

0

0

0
V ar(Y )
(cid:19) (cid:18) 1 1
0 1

V ar(X) V ar(Y )

(cid:18) V ar(X)

V ar(X)

V ar(X) V ar(X) + V ar(Y )

(12.57)

(12.58)

(12.59)

(12.60)

(cid:19)

(cid:19) (cid:18) 1 1
0 1

(cid:19)

(cid:19)

since X and Y are independent. We would then proceed as before.

This matches what we found earlier, as it should, but shows how matrix methods can be used. This
example was fairly simple, so those methods did not produce a large amount of streamlining, but
in other examples later in the book, the matrix approach will be key.

12.4.5 Example: Easy Sum Again

Let’s redo the example in Section 12.1.2 again, this time using matrix methods.

First note that

X1 + X2 = (1, 1)

(cid:19)

(cid:18) X1
X2

(12.61)

12.5. THE MULTIVARIATE NORMAL FAMILY OF DISTRIBUTIONS

215

i.e. it is of the form (12.55). So, (12.56) gives us

V ar(X1 + X2) = (1, 1)

(cid:18) 1 0.5
1
0.5

(cid:19) (cid:18) 1
1

(cid:19)

= 3

(12.62)

Of course using the matrix formulation didn’t save us much time here, but for complex problems
it’s invaluable. We will frequently have need for ﬁnding the variance of a linear combination of the
elements of a vector, exactly what we did above.

12.5 The Multivariate Normal Family of Distributions

This is a generalization of the normal distribution. It is covered in detail in Section 13.5.2, but
here is the overview:

• Just as the univariate normal family is parameterized by the mean and variance, the multi-

variate normal family has as its parameters the mean vector and the covariance matrix.

• In the bivariate case, the density looks like a three-dimensional bell, as on the cover of this

book.

• If a random vector W has a multivariate normal distribution, and A is a constant matrix,

then the new random vector AW is also multivariate normally distributed.

• The multivariate version of the Central Limit Theorem holds, i.e. the sum of i.i.d. random

vectors has an approximate multivariate normal distribution.

12.5.1 R Functions

R provides functions that compute probabilities involving this family of distributions, in the library
mvtnorm. In particular the R function pmvnorm(), which computes probabilities of “rectangu-
lar” regions for multivariate normally distributed random vectors W. The arguments we’ll use for
this function here are:

• mean: the mean vector

• sigma: the covariance matrix

• lower, upper: bounds for a multidimensional “rectangular” region of interest

216

CHAPTER 12. COVARIANCE AND RANDOM VECTORS

Since a multivariate normal distribution is characterized by its mean vector and covariance matrix,
the ﬁrst two arguments above shouldn’t suprise you. But what about the other two?

The function ﬁnds the probability of our random vector falling into a multidimensional rectangular
region that we specify, through the arguments are lower and upper. For example, suppose we
have a trivariate normally distributed random vector (U, V, W )(cid:48), and we want to ﬁnd

P (1.2 < U < 5 and − 2.2 < V < 3 and 1 < W < 10)

(12.63)

Then lower would be (1.2,-2.2,1) and upper would be (5,3,10).

Note that these will typically be speciﬁed via R’s c() function, but default values are recycled
versions of -Inf and Inf, built-in R constants for −∞ and ∞.

An important special case is that in which we specify upper but allow lower to be the default
values, thus computing a probability of the form

P (W1 ≤ c1, ..., Wr ≤ cr)

(12.64)

12.5.2 Special Case: New Variable Is a Single Linear Combination of a Random

Vector

Suppose the vector U = (U1, ..., Uk)(cid:48) has an approximately k-variate normal distribution, and we
form the scalar

Y = c1U1 + ... + ckUk

(12.65)

Then Y is approximately univate normal, and its (exact) variance is given by (12.56). It’s mean is
obtained via (12.50).

We can then use the R functions for the univariate normal distribution, e.g. pnorm().

12.6 Indicator Random Vectors

Let’s extend the notion of indicator random variables in Section 3.9 to vectors.

Say one of events A1, ..., Ak must occur, and they are disjoint. So, their probabilities sum to 1.
Deﬁne the k-component random vector I to consist of k-1 0s and one 1, where the position of the
1 is itself random; if Ai occurs, then Ii is 1.

12.7. EXAMPLE: DICE GAME

217

For example, say U has a U(0,1) distribution, and say A1, A2 and A3 are the events corresponding
to U < 0.2, 0.2 ≤ U ≤ 0.7 and U > 0.7, respectively. Then the random vector I would be (1, 0, 0)(cid:48)
in the ﬁrst case, and so on.

Let pi = P (Ai). The analogs of (3.78) and (3.79) can easily be shown to be as follows:

• The mean vector is E(I) = (p1, ..., pk)(cid:48).

• Cov(I) has pi(1 − pi) as its ith element, and for i (cid:54)= j, element (i,j) is −pipj.

12.7 Example: Dice Game

This example will be short on some details, but it will really illustrate the value of using matrices.

Suppose we roll a die 50 times. Let X denote the number of rolls in which we get one dot, and let
Y be the number of times we get either two or three dots. For convenience, let’s also deﬁne Z to
be the number of times we get four or more dots. Suppose also that we win $5 for each roll of a
one, and $2 for each roll of a two or three.

Let’s ﬁnd the approximate values of the following:

• P (X ≤ 12 and Y ≤ 16)

• P(win more than $90)

• P (X > Y > Z)

The exact probabilities could, in principle, be calculated. But that would be rather cumbersome.
But we can get approximate answers by noting that the triple (X,Y,Z) has an approximate multi-
variate normal distribution. This is shown in Section 13.5.2, but it basically the derivation works
like this:

• Write (X,Y,Z) as a sum of indicator vectors (Section 12.6), analogous to what we did in

Section 4.3.

• Invoke the multivariate CLT.

Since the parameters of the multivariate normal family are the mean vector and the covariance ma-
trix, we’ll of course need to know those for the random vector (X, Y, Z)(cid:48) when we call pmvnorm().

218

CHAPTER 12. COVARIANCE AND RANDOM VECTORS

Once again, this will be shown later, but basically it follows from Section 12.6 above. Here are the
results:

E[(X, Y, Z)] = (50/6, 50/3, 50/2)

(12.66)

and

Cov[(X, Y, Z)] = 50





5/36 −1/18 −1/12
−1/6
2/9
−1/18
1/4
−1/12 −1/6





(12.67)

Here’s a partial check: X has a binomial distribution with 50 trials and success probability 1/6, so
(4.30) tells us that Var(X) = 250/36, just as seen above.

We can now use the R multivariate normal probability function mentioned in Section 12.5 to ﬁnd
P (X ≤ 12 and Y ≤ 16).

To account for the integer nature of X and Y, we call the function with upper limits of 12.5 and 16.5,
rather than 12 and 16, which is often used to get a better approximation. (Recall the “correction
for continuity,” Section 8.13.) Our code is

1

2

3

4

5

6

7

8

p1 <- 1/6
p23 <- 1/3
meanvec <- 50*c(p1,p23)
var1 <- 50*p1*(1-p1)
var23 <- 50*p23*(1-p23)
covar123 <- -50*p1*p23
covarmat <- matrix(c(var1,covar123,covar123,var23),nrow=2)
print(pmvnorm(upper=c(12.5,16.5),mean=meanvec,sigma=covarmat))

We ﬁnd that

P (X ≤ 12 and Y ≤ 16) ≈ 0.43

(12.68)

Now, let’s ﬁnd the probability that our total winnings, T, is over $90. We know that T = 5X +
2Y, and Section 12.5.2 above applies. We simply choose the vector c to be

c = (5, 2, 0)(cid:48)

(12.69)

12.7. EXAMPLE: DICE GAME

since

(5, 2, 0)



 = 5X + 2Y





X
Y
Z

219

(12.70)

Then Section 12.5.2 tells us that 5X + 2Y also has an approximate univariate normal distribution.
Excellent—we can now use pnorm(). We thus need the mean and variance of T, again using
Section 12.5.2:

ET = E(5X + 2Y ) = 5EX + 2EY = 250/6 + 100/3 = 75

(12.71)

V ar(T ) = c(cid:48) Cov





X
Y
Z



 c = (5, 2, 0) 50





5/36 −1/18 −1/12
2/9 −1/6
1/4

−1/18
−1/12 −1/6









5
2
0



 = 162.5 (12.72)

So, we have our answer:

> 1 − pnorm( 9 0 , 7 5 , sqrt ( 1 6 2 . 5 ) )
[ 1 ] 0 . 1 1 9 6 5 8 3

Now to ﬁnd P (X > Y > Z), we need to work with (U, V )(cid:48) = (X − Y, Y − Z). U and V are both
linear functions of X, Y and Z, so let’s write the matrix equation:

We need to have

so set

(cid:19)

(cid:18) X − Y
Y − Z

= A









X
Y
Z

A =

(cid:18) 1 −1
0

0
1 −1

(cid:19)

(12.73)

(12.74)

and then proceed as before to ﬁnd P (U > 0, V > 0). Now we take lower to be (0,0), and upper
to be the default values, ∞ in pmvnorm().

220

CHAPTER 12. COVARIANCE AND RANDOM VECTORS

12.7.1 Correlation Matrices

The correlation matrix corresponding to a given covariance matrix is deﬁned as follows. Element
(i,j) is the correlation between the ith and the jth elements of the given random vector.

Here is R code to compute a correlation matrix from a covariance matrix:

c o v t o c o r r <− function ( covmat ) {

n <− nrow( covmat )
s t d d e v <− vector ( length=n )
cormat <− matrix (nrow=n , ncol=n )
for ( i

i n 1 : n ) {

s t d d e v [ i ] <− sqrt ( covmat [ i , i ] )
cormat [ i , i ] <− 1 . 0

}
for ( i

i n 1 : ( n−1)) {

for ( j

i n ( i +1): n ) {

tmp <− covmat [ i , j ] / ( s t d d e v [ i ] ∗ s t d d e v [ j ] )
cormat [ i , j ] <− tmp
cormat [ j , i ] <− tmp

}

}
return ( cormat )

}

12.7.2 Further Reading

You can see some more examples of the multivariate normal distribution, covariance matrices etc.
in a computer science context in my paper A Modiﬁed Random Perturbation Method for Database
Security (with Patrick Tendick). ACM Transactions on Database Systems, 1994, 19(1), 47-63. The
application is database security.

Exercises

1. Suppose the pair (X,Y)’ has mean vector (0,2) and covariance matrix
(cid:18) 1 2
2 6

(cid:19)

Find the covariance matrix of the pair U = (X+Y,X-2Y)’.

12.7. EXAMPLE: DICE GAME

221

2. Show that

ρ(aX + b, cY + d) = ρ(X, Y )

(12.75)

for any constants a, b, c and d.

3. Suppose X, Y and Z are ”i.i.d.” (independent, identically distributed) random variables, with
E(X k) being denoted by νk, k = 1,2,3. Find Cov(XY,XZ) in terms of the νk.

4. Using the properties of covariance in Section 12.1.1, show that for any random variables X and
Y, Cov(X+Y,X-Y) = Var(X) - Var(Y).

5. Suppose we wish to predict a random variable Y by using another random variable, X. We may
consider predictors of the form cX + d for constants c and d. Show that the values of c and d that
minimize the mean squared prediction error, E[(Y − cX − d)2 are

c =

E(XY ) − EX · EY
V ar(X)

d =

E(X 2) · EY − EX · E(XY )
V ar(X)

(12.76)

(12.77)

6. Programs A and B consist of r and s modules, respectively, of which c modules are common to
both. As a simple model, assume that each module has probability p of being correct, with the
modules acting independently. Let X and Y denote the numbers of correct modules in A and B,
respectively. Find the correlation A(X, Y ) as a function of r, s, c and p.

Hint: Write X = X1 + ...Xr, where Xi is 1 or 0, depending on whether module i of A is correct.
Of those, let X1, ..., Xc correspond to the modules in common to A and B. Similarly, write Y =
Y1 + ...Ys, for the modules in B, again having the ﬁrst c of them correspond to the modules in
common. Do the same for B, and for the set of common modules.

7. Suppose we have random variables X and Y, and deﬁne the new random variable Z = 8Y. Then
which of the following is correct? (i) ρ(X, Z) = ρ(X, Y ). (ii) ρ(X, Z) = 0. (iii) ρ(Y, Z) = 0. (iv)
ρ(X, Z) = 8ρ(X, Y ). (v) ρ(X, Z) = 1

8 ρ(X, Y ). (vi) There is no special relationship.

8. Derive (12.3). Hint: A constant, q here, is a random variable, trivially, with 0 variance.

9. Consider a three-card hand drawn from a 52-card deck. Let X and Y denote the number of
hearts and diamonds, respectively. Find ρ(X, Y ).

10. Consider the lightbulb example in Section 9.1. Use the “mailing tubes” on Var() and Cov() to
ﬁnd ρ(X1, T2).

222

CHAPTER 12. COVARIANCE AND RANDOM VECTORS

11. Find the following quantities for the dice example in Section 12.3.2.1:

(a) Cov(X,2S)

(b) Cov(X,S+Y)

(c) Cov(X+2Y,3X-Y)

(d) pX,S(3, 8)

12. Suppose Xi, i = 1,2,3,4,5 are independent and each have mean 0 and variance 1. Let Yi =
Xi+1 − Xi, i = 1,2,3,4. Using the material in Section 12.4, ﬁnd the covariance matrix of Y =
(Y1, Y2, Y3, Y4).

Chapter 13

Multivariate PMFs and Densities

Individual pmfs pX and densities fX don’t describe correlations betwen variables. We need some-
thing more. We need ways to describe multivariate distributions.

13.1 Multivariate Probability Mass Functions

Recall that for a single discrete random variable X, the distribution of X was deﬁned to be a list
of all the values of X, together with the probabilities of those values. The same is done for a pair
of discrete random variables U and V, as follows.

Suppose we have a bag containing two yellow marbles, three blue ones and four green ones. We
choose four marbles from the bag at random, without replacement. Let Y and B denote the number
of yellow and blue marbles that we get. Then deﬁne the two-dimensional pmf of Y and B to be

pY,B(i, j) = P (Y = i and B = j) =

(cid:0)2
i

(cid:1)(cid:0)3
j

(cid:1)(cid:0) 4

4−i−j
(cid:1)

(cid:0)9
4

(cid:1)

(13.1)

Here is a table displaying all the values of P(Y = i and B = j):

i ↓, j →
0
1
2

0
0.0079
0.0635
0.0476

1
0.0952
0.2857
0.0952

2
0.1429
0.1905
0.0238

3
0.0317
0.1587
0.000

So this table is the distribution of the pair (Y,B).

Recall further that in the discrete case, we introduced a symbolic notation for the distribution of

223

224

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

a random variable X, deﬁned as pX (i) = P (X = i), where i ranged over all values that X takes on.
We do the same thing for a pair of random variables:

Deﬁnition 25 For discrete random variables U and V, their probability mass function is deﬁned
to be

pU,V (i, j) = P (U = i and V = j)

(13.2)

where (i,j) ranges over all values taken on by (U,V). Higher-dimensional pmfs are deﬁned similarly,
e.g.

pU,V,W (i, j, k) = P (U = i and V = j and W = k)

(13.3)

So in our marble example above, pY,B(1, 2) = 0.048, pY,B(2, 0) = 0.012 and so on.

Just as in the case of a single discrete random variable X we have

P (X ∈ A) =

(cid:88)

i∈A

pX (i)

(13.4)

for any subset A of the range of X, for a discrete pair (U,V) and any subset A of the pair’s range,
we have

P [(U, V ) ∈ A) =

(cid:88) (cid:88)

(i,j)∈A

pU,V (i, j)

(13.5)

Again, consider our marble example. Suppose we want to ﬁnd P (Y < B). Doing this “by hand,”
we would simply sum the relevant probabilities in the table above, which are marked in bold face
below:

i ↓, j →
0
1
2

2

0

1

3
0.002 0.024 0.036 0.008
0.073 0.048 0.004
0.162
0.006 0.000
0.024
0.012

The desired probability would then be 0.024+0.036+0.008+0.048+0.004 = 0.12.

Writing it in the more formal way using (13.5), we would set

A = {(i, j) : i < j}

(13.6)

13.1. MULTIVARIATE PROBABILITY MASS FUNCTIONS

225

and then

P (Y < B) = P [(Y, B) ∈ A) =

2
(cid:88)

3
(cid:88)

i=0

j=i+1

pY,B(i, j)

(13.7)

Note that the lower bound in the inner sum is j = i+1. This reﬂects the common-sense point that
in the event Y < B, B must be at least equal to Y+1.

Of course, this sum still works out to 0.12 as before, but it’s important to be able to express this
as a double sum of pY,B(), as above. We will rely on this to motivate the continuous case in the
next section.

Expected values are calculated in the analogous manner. Recall that for a function g() of X

E[g(X] =

(cid:88)

i

g(i)pX (i)

So, for any function g() of two discrete random variables U and V, deﬁne

E[g(U, V )] =

(cid:88)

(cid:88)

i

j

g(i, j)pU,V (i, j)

(13.8)

(13.9)

For instance, if for some bizarre reason we wish to ﬁnd the expected value of the product of the
numbers of yellow and blue marbles above,1, the calculation would be

E(Y B) =

2
(cid:88)

3
(cid:88)

i=0

j=0

ij pY,B(i, j) = 0.255

(13.10)

The univariate pmfs, called marginal pmfs, can of course be recovered from the multivariate pmf:

pU (i) = P (U = i) =

P (U = i, V = j) =

(cid:88)

j

(cid:88)

j

pU,V (i, j)

(13.11)

For example, look at the table following (13.5). Evaluating (13.11) for i = 1, say, with U = Y and
V = B, would give us 0.012 + 0.024 + 0.006 + 0.000 = 0.042. Then all that (13.11) tells us is
the P(Y = 1) = 0.042, which is obvious from the table; (13.11) simply is an application of our old
principle, “Break big events down into small events.”

1Not so bizarre, we’ll ﬁnd in Section 12.1.1.

226

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

Needless to say, we can recover the marginal distribution of V similarly to (13.11):

pV (j) = P (V = j) =

P (U = i, V = j) =

(cid:88)

i

(cid:88)

i

pU,V (i, j)

(13.12)

13.2 Multivariate Densities

13.2.1 Motivation and Deﬁnition

Extending our previous deﬁnition of cdf for a single variable, we deﬁne the two-dimensional cdf for
a pair of random variables X and Y (discrete or continuous) as

FX,Y (u, v) = P (X ≤ u and Y ≤ v)

(13.13)

If X and Y were discrete, we would evaluate that cdf via a double sum of their bivariate pmf.
You may have guessed by now that the analog for continuous random variables would be a double
integral, and it is. The integrand is the bivariate density:

fX,Y (u, v) =

∂2
∂u∂v

FX,Y (u, v)

(13.14)

Densities in higher dimensions are deﬁned similarly.2

As in the univariate case, a bivariate density shows which regions of the X-Y plane occur more
frequently, and which occur less frequently.

13.2.2 Use of Multivariate Densities in Finding Probabilities and Expected Val-

ues

Again by analogy, for any region A in the X-Y plane,

P [(X, Y )(cid:15)A] =

(cid:90) (cid:90)

A

fX,Y (u, v) du dv

(13.15)

2Just as we noted in Section ?? that some random variables are neither discrete nor continuous, there are some
pairs of continuous random variables whose cdfs do not have the requisite derivatives. We will not pursue such cases
here.

13.2. MULTIVARIATE DENSITIES

227

So, just as probabilities involving a single variable X are found by integrating fX over the region in
question, for probabilities involving X and Y, we take the double integral of fX,Y over that region.

Also, for any function g(X,Y),

E[g(X, Y )] =

(cid:90) ∞

(cid:90) ∞

−∞

−∞

g(u, v)fX,Y (u, v) du dv

(13.16)

where it must be kept in mind that fX,Y (u, v) may be 0 in some regions of the U-V plane. Note
that there is no set A here as in (13.15). See (13.20) below for an example.

Finding marginal densities is also analogous to the discrete case, e.g.

fX (s) =

(cid:90)

t

fX,Y (s, t) dt

(13.17)

Other properties and calculations are analogous as well. For instance, the double integral of the
density is equal to 1, and so on.

13.2.3 Example: a Triangular Distribution

Suppose (X,Y) has the density

fX,Y (s, t) = 8st, 0 < t < s < 1

(13.18)

The density is 0 outside the region 0 < t < s < 1.

First, think about what this means, say in our notebook context. We do the experiment many
times. Each line of the notebook records the values of X and Y. Each of these (X,Y) pairs is a
point in the triangular region 0 < t < s < 1. Since the density is highest near the point (1,1) and
lowest near (0,0), (X,Y) will be observed near (1,1) much more often than near (0,0), with points
near, say, (1,0.5) occurring with middling frequencies.

Let’s ﬁnd P (X + Y > 1). This calculation will involve a double integral. The region A in (13.15)
is {(s, t) : s + t > 1, 0 < t < s < 1}. We have a choice of integrating in the order ds dt or dt ds.
The latter will turn out to be more convenient.

To see how the limits in the double integral are obtained, ﬁrst review (13.7). We use the same
reasoning here, changing from sums to integrals and applying the current density, as shown in this
ﬁgure:

228

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

Here s represents X and t represents Y. The gray area is the region in which (X,Y) ranges. The
subregion A in (13.15), corresponding to the event X+Y > 1, is shown in the striped area in the
ﬁgure.

The dark vertical line shows all the points (s,t) in the striped region for a typical value of s in the
integration process. Since s is the variable in the outer integral, considered it ﬁxed for the time
being and ask where t will range for that s. We see that for X = s, Y will range from 1-s to s; thus
we set the inner integral’s limits to 1-s and s. Finally, we then ask where s can range, and see from
the picture that it ranges from 0.5 to 1. Thus those are the limits for the outer integral.

P (X + Y > 1) =

(cid:90) 1

(cid:90) s

0.5

1−s

8st dt ds =

(cid:90) 1

0.5

8s · (s − 0.5) ds =

5
6

(13.19)

Following (13.16),

√

E[

X + Y ] =

(cid:90) 1

(cid:90) s

√

s + t 8st dt ds

0

0

(13.20)

Let’s ﬁnd the marginal density fY (t). Just as we “summed out” in (13.11), in the continuous case

0.00.20.40.60.81.00.00.20.40.60.81.0stt=1−st=s13.2. MULTIVARIATE DENSITIES

we must “integrate out” the s in (13.18):

fY (t) =

(cid:90) 1

t

8st ds = 4t − 4t3

for 0 < t < 1, 0 elsewhere.

Let’s ﬁnd the correlation between X and Y for this density.

E(XY ) =

(cid:90) 1

(cid:90) s

st · 8st dt ds

0

8s2 · s3/3 ds

0
(cid:90) 1

0
4
9

=

=

fX (s) =

(cid:90) s

8st dt

0

= 4st2(cid:12)
s
(cid:12)
(cid:12)
0
= 4s3

fY (t) =

(cid:90) 1

t

8st ds

= 4t · s2(cid:12)
1
(cid:12)
(cid:12)
t
= 4t(1 − t2)

EX =

(cid:90) 1

0

s · 4s3 ds =

4
5

(cid:90) 1

E(X 2) =

s2 · 4s3 ds =

2
3

0

2
3

V ar(X) =

(cid:19)2

−

(cid:18) 4
5

= 0.027

229

(13.21)

(13.22)

(13.23)

(13.24)

(13.25)

(13.26)

(13.27)

(13.28)

(13.29)

(13.30)

(13.31)

(13.32)

(13.33)

230

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

EY =

(cid:90) 1

0

t · (4t − 4t3) ds =

4
3

−

4
5

=

8
15

E(Y 2) =

(cid:90) 1

0

t2 · (4t − 4t3) dt = 1 −

4
6

=

1
3

V ar(Y ) =

(cid:19)2

1
3

−

(cid:18) 8
15

= 0.049

Cov(X, Y ) =

4
9

−

4
5

·

8
15

= 0.018

ρ(X, Y ) =

√

0.018
0.027 · 0.049

= 0.49

(13.34)

(13.35)

(13.36)

(13.37)

(13.38)

13.2.4 Example: Train Rendezvouz

Train lines A and B intersect at a certain transfer point, with the schedule stating that trains
from both lines will arrive there at 3:00 p.m. However, they are often late, by amounts X and Y ,
measured in hours, for the two trains. The bivariate density is

fX,Y (s, t) = 2 − s − t, 0 < s, t < 1

(13.39)

Two friends agree to meet at the transfer point, one taking line A and the other B. Let W denote
the time in minutes the person arriving on line B must wait for the friend. Let’s ﬁnd P (W > 6).

First, convert this to a problem involving X and Y, since they are the random variables for which
we have a density, and then use (13.15):

P (W > 0.1) = P (Y + 0.1 < X)

=

(cid:90) 1

(cid:90) s−0.1

0.1

0

(2 − s − t) dtds

(13.40)

(13.41)

13.3. MORE ON SETS OF INDEPENDENT RANDOM VARIABLES

231

13.3 More on Sets of Independent Random Variables

13.3.1 Probability Mass Functions and Densities Factor in the Independent

Case

If X and Y are independent, then

in the discrete case, and

pX,Y = pX pY

(13.42)

fX,Y = fX fY

(13.43)

in the continuous case. In other words, the joint pmf/density is the product of the marginal ones.

This is easily seen in the discrete case:

pX,Y (i, j) = P (X = i and Y = j) (deﬁnition)

= P (X = i)P (Y = j) (independence)
= pX (i)pY (j) (deﬁnition))

Here is the proof for the continuous case;

fX,Y (u, v) =

FX,Y (u, v)

=

∂2
∂u∂v
∂2
∂u∂v
∂2
∂u∂v
∂2
∂u∂v
= fX (u)fY (v)

=

=

P (X ≤ u and Y ≤ v)

[P (X ≤ u) · P (Y ≤ v)]

FX (u) · FY (v)

(13.44)

(13.45)

(13.46)

(13.47)

(13.48)

(13.49)

(13.50)

(13.51)

232

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

13.3.2 Convolution

Deﬁnition 26 Suppose g and h are densities of continuous random variables X and Y, respectively.
The convolution of g and h, denoted g*h,3 is another density, deﬁned to be that of the random
variable X+Y. In other words, convolution is a binary operation on the set of all densities.

If X and Y are nonnegative and independent, then the convolution reduces to

fZ(t) =

(cid:90) t

0

g(s)h(t − s) ds

(13.52)

You can get intuition on this by considering the discrete case. Say U and V are nonnegative
integer-valued random variables, and set W = U+V. Let’s ﬁnd pW ;

pW (k) = P (W = k) (by deﬁnition)

= P (U + V = k) (substitution)

(13.53)

(13.54)

=

=

=

k
(cid:88)

i=0
k
(cid:88)

i=0
k
(cid:88)

i=0

P (U = i and V = k − i) (“In what ways can it happen?”)

(13.55)

pU,V (i, k − i) (by deﬁnition)

pU (i)pV (k − i) (from Section 13.3.1)

(13.56)

(13.57)

Review the analogy between densities and pmfs in our unit on continuous random variables, Section
7.3.2, and then see how (13.52) is analogous to (13.53) through (13.57):

• k in (13.53) is analogous to t in (13.52)

• the limits 0 to k in (13.57) are analogous to the limits 0 to t in (13.52)

• the expression k-i in (13.57) is analogous to t-s in (13.52)

• and so on

3The reason for the asterisk, suggesting a product, will become clear in Section 14.3.

13.3. MORE ON SETS OF INDEPENDENT RANDOM VARIABLES

233

13.3.3 Example: Ethernet

Consider this network, essentially Ethernet. Here nodes can send at any time. Transmission time
is 0.1 seconds. Nodes can also “hear” each other; one node will not start transmitting if it hears
that another has a transmission in progress, and even when that transmission ends, the node that
had been waiting will wait an additional random time, to reduce the possibility of colliding with
some other node that had been waiting.

Suppose two nodes hear a third transmitting, and thus refrain from sending. Let X and Y be
their random backoﬀ times, i.e. the random times they wait before trying to send. (In this model,
assume that they do not do “listen before talk” after a backoﬀ.) Let’s ﬁnd the probability that
they clash, which is P (|X − Y | ≤ 0.1).

Assume that X and Y are independent and exponentially distributed with mean 0.2, i.e. they each
have density 5e−5u on (0, ∞). Then from (13.43), we know that their joint density is the product
of their marginal densities,

fX,Y (s, t) = 25e−5(s+t), s, t > 0

(13.58)

Now

P (|X − Y | ≤ 0.1) = 1 − P (|X − Y | > 0.1) = 1 − P (X > Y + 0.1) − P (Y > X + 0.1)

(13.59)

Look at that ﬁrst probability. Applying (13.15) with A = {(s, t) : s > t + 0.1, 0 < s, t}, we have

P (X > Y + 0.1) =

(cid:90) ∞

(cid:90) ∞

0

t+0.1

25e−5(s+t) ds dt = 0.303

(13.60)

By symmetry, P (Y > X + 0.1) is the same. So, the probability of a clash is 0.394, rather high. We
may wish to increase our mean backoﬀ time, though a more detailed analysis is needed.

13.3.4 Example: Analysis of Seek Time

This will be an analysis of seek time on a disk. Suppose we have mapped the innermost track to 0
and the outermost one to 1, and assume that (a) the number of tracks is large enough to treat the
position H of the read/write head the interval [0,1] to be a continous random variable, and (b) the
track number requested has a uniform distribution on that interval.

234

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

Consider two consecutive service requests for the disk, denoting their track numbers by X and Y.
In the simplest model, we assume that X and Y are independent, so that the joint distribution of
X and Y is the product of their marginals, and is thus is equal to 1 on the square 0 ≤ X, Y ≤ 1.

The seek distance will be |X − Y |. Its mean value is found by taking g(s,t) in (13.16) to be |s − t|.

(cid:90) 1

(cid:90) 1

0

0

|s − t| · 1 ds dt =

1
3

Let’s ﬁnd the density of the seek time S = |X − Y |:

FS(v) = P (|X − Y | ≤ v)

= P (−v ≤ X − Y ≤ v)

= 1 − P (X − Y < −v) − P (X − Y > v)
= 1 − (1 − v)2

(13.61)

(13.62)

(13.63)

(13.64)

(13.65)

where for instance P (X − Y > v) the integral of 1 on the triangle with vertices (v,0), (1,0) and
(1,1-v), thus equal to the area of that triangle, 0.5(1 − v)2.

Then

fS(v) =

d
dt

FS(v) = 2(1 − v)

(13.66)

By the way, what about the assumptions here? The independence would be a good assumption,
for instance, for a heavily-used ﬁle server accessed by many diﬀerent machines. Two successive
requests are likely to be from diﬀerent machines, thus independent. In fact, even within the same
machine, if we have a lot of users at this time, successive requests can be assumed independent.
On the other hand, successive requests from a particular user probably can’t be modeled this way.

As mentioned in our unit on continuous random variables, page 136, if it’s been a while since we’ve
done a defragmenting operation, the assumption of a uniform distribution for requests is probably
good.

Once again, this is just scratching the surface. Much more sophisticated models are used for more
detailed work.

13.3. MORE ON SETS OF INDEPENDENT RANDOM VARIABLES

235

13.3.5 Example: Backup Battery

Suppose we have a portable machine that has compartments for two batteries. The main battery
has lifetime X with mean 2.0 hours, and the backup’s lifetime Y has mean life 1 hours. One replaces
the ﬁrst by the second as soon as the ﬁrst fails. The lifetimes of the batteries are exponentially
distributed and independent. Let’s ﬁnd the density of W, the time that the system is operational
(i.e. the sum of the lifetimes of the two batteries).

Recall that if the two batteries had the same mean lifetimes, W would have a gamma distribution.
But that’s not the case here. But we notice that the distribution of W is a convolution of two
exponential densities, as it is the sum of two nonnegative independent random variables. Using
(13.3.2), we have

fW (t) =

(cid:90) t

0

fX (s)fY (t − s) ds =

(cid:90) t

0

0.5e−0.5se−(t−s) ds = e−0.5t − e−t, 0 < t < ∞

(13.67)

13.3.6 Example: Minima of Uniformly Distributed Random Variables

Suppose X and Y be independent and each have a uniform distribution on the interval (0,1). Let
Z = min(X,Y). Find fZ:

FZ(t) = P (Z ≤ t)

(def. of cdf)

= 1 − P (Z > t)

= 1 − P (X > t and Y > t)

(min(u,v) > t iﬀ both u,v > t)

= 1 − P (X > t)P (Y > t)
= 1 − (1 − t)2

(indep.)

(indep.)

(13.68)

(13.69)

(13.70)

(13.71)

(13.72)

(13.73)

The density of Z is then the derivative of that last expression:

fZ(t) = 2(1 − t),

0 < t < 1

(13.74)

13.3.7 Example: Ethernet Again

In the Ethernet example in Section 13.3.3, we assumed that transmission time was a constant, 0.1.
Now let’s account for messages of varying sizes, by assuming that transmission time T for a message
is random, exponentially distributed with mean 0.1. Let’s ﬁnd P (X < Y and there is no collision).

236

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

That probability is equal to P (X + T < Y ). Well, this sounds like we’re going to have to deal with
triple integrals, but actually not. The derivation in Section 13.3.5 shows that the density of S =
X+T is

Thus the joint density of S and Y is

fS(t) = e−0.1t − e−0.2t, 0 < t < ∞

(13.75)

fS,Y (u, v) = (e−0.1u − e−0.2u)0.2e−0.2v, 0 < u, v, < ∞

(13.76)

We can then evaluate P (S < Y ) as a double integral, along the same lines as we did for instance
in (13.19).

13.4 Example: Finding the Distribution of the Sum of Noninde-

pendent Random Variables

In Section 13.3.2, we found a general formula for the distribution of the sum of two independent
random variables. What about the nonindependent case?

Suppose for instance fX,Y (s, t) = 2 on 0 < t < s < 1, 0 elsewhere. Let’s ﬁnd fX+Y (w) for the case
0 < w < 1.

Since X and Y are not independent, we cannot use convolution. But:

FX+Y (w) = P (X + Y ) ≤ w

(cid:90) w/2

(cid:90) w−t

=

2 ds dt

0
= w2/2

t

(13.77)

(13.78)

(13.79)

So fX+Y (w) = w.

The case 1 < w < 2 is similar.

13.5 Parametric Families of Multivariate Distributions

Since there are so many ways in which random variables can correlate with each other, there
are rather few parametric families commonly used to model multivariate distributions (other than

13.5. PARAMETRIC FAMILIES OF MULTIVARIATE DISTRIBUTIONS

237

those arising from sets of independent random variables have a distribution in a common parametric
univariate family). We will discuss two here.

13.5.1 The Multinomial Family of Distributions

13.5.1.1 Probability Mass Function

This is a generalization of the binomial family.

Suppose one rolls a die 8 times. What is the probability that the results consist of two 1s, one 2,
one 4, three 5s and one 6? Well, if the rolls occur in that order, i.e. the two 1s come ﬁrst, then the
2, etc., then the probability is

(cid:18) 1
6

(cid:19)2 (cid:18) 1
6

(cid:19)1 (cid:18) 1
6

(cid:19)0 (cid:18) 1
6

(cid:19)1 (cid:18) 1
6

(cid:19)3 (cid:18) 1
6

(cid:19)1

But there are many diﬀerent orderings, in fact

of them, from Section 2.15.5, and thus

8!
2!1!0!1!3!1!

(13.80)

(13.81)

P (two 1s, one 2, no 3s, one 4, three 5s, one 6) =

8!
2!1!0!1!3!1!

(cid:18) 1
6

(cid:19)2 (cid:18) 1
6

(cid:19)1 (cid:18) 1
6

(cid:19)0 (cid:18) 1
6

(cid:19)1 (cid:18) 1
6

(cid:19)1

(cid:19)3 (cid:18) 1
6
(13.82)

From this, we can more generally see the following. Suppose:

• we have n trials, each of which has r possible outcomes or categories

• the trials are independent

• the ith outcome has probability pi

Let Xi denote the number of trials with outcome i, i = 1,...,r. In the die example above, for instance,
r = 6 for the six possible outcomes of one trial, i.e. one roll of the die, and X1 is the number of
times we got one dot, in our n = 8 rolls.

238

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

Then we say that the vector X = (X1, ..., Xr) have a multinomial distribution. Since the Xi
are discrete random variables, they have a joint pmf pX1,...,Xr (). Taking the above die example for
illustration again, the probability of interest there is pX (2, 1, 0, 1, 3, 1). We then have in general,

pX1,...,Xr (j1, ..., jr) =

n!
j1!...jr!

pj1
1 ...pjr
r

(13.83)

Note that this family of distributions has r+1 parameters: n and the pi. Of course, you might
count it as only r, since the pi sum to one and thus are not free of each other.

R has the function dmultinom() for the multinomial pmf. The call dmultinom(x,n,prob)
evaluates (13.83), where x is the vector (j1, ..., jr) and prob is (p1, ..., pr) .

We can simulate multinomial random vectors in R using the sample() function:

1

2

3

4

5

6

7

8

9

10

11

12

13

# n is the number of trials, p the vector of probabilities of the r
# categories
multinom <- function(n,p) {

r <- length(p)
outcome <- sample(x=1:r,size=n,replace=T,prob=p)
counts <- vector(length=r) # counts of the various categories
# tabulate the counts (could be done more efficiently)
for (i in 1:n) {

j <- outcome[i]
counts[j] <- counts[j] + 1

}
return(counts)

}

13.5.1.2 Example: Component Lifetimes

Say the lifetimes of some electronic component, say a disk drive, are exponentially distributed with
mean 4.5 years. If we have six of them, what is the probability that two fail before 1 year, two last
between 1 and 2 years, and the remaining two last more than 2 years?

Let (X,Y,Z) be the number that last in the three time intervals. Then this vector has a multinomial
distribution, with n = 6 trials, and

p1 =

(cid:90) 1

0

1
4.5

p2 =

(cid:90) 2

1

1
4.5

e−t/4.5 dt = 0.20

e−t/4.5 dt = 0.16

(13.84)

(13.85)

13.5. PARAMETRIC FAMILIES OF MULTIVARIATE DISTRIBUTIONS

239

p3 =

(cid:90) ∞

2

1
4.5

e−t/4.5 dt = 0.64

We then use (13.83) to ﬁnd the speciﬁed probability, which is:

6!
2!2!2!

0.2020.1620.642

(13.86)

(13.87)

13.5.1.3 Mean Vectors and Covariance Matrices in the Multinomial Family

Consider a multinomially distributed random vector X = (X1, ..., Xr)(cid:48), with n trials and category
probabilities pi. Let’s ﬁnd its mean vector and covariance matrix.

First, note that the marginal distributions of the Xi are binomial! So,

EXi = npi and V ar(Xi) = npi(1 − pi)

(13.88)

So we know EX now:

EX =









np1
...
npr

(13.89)

We also know the diagonal elements of Cov(X)—npi(1 − pi) is the ith diagonal element, i = 1,...,r.

But what about the rest? The derivation will follow in the footsteps of those of (4.30), but now
in a vector context. Prepare to use your indicator random variable, random vector and covariance
matrix skills! Also, this derivation will really build up your “probabilistic stamina level.” So, it’s
good for you! But now is the time to review (4.30), Section 3.9 and Section 12.4, before
continuing.

We’ll continue the notation of the last section. In order to keep on eye on the concrete, we’ll often
illustrate the notation with the die example above; there we rolled a die 8 times, and deﬁned 6
categories (one dot, two dots, etc.). We were interested in probabilities involving the number of
trials that result in each of the 6 categories.

Deﬁne the random vector Ti to be the outcome of the ith trial. It is a vector of indicator random
variables, one for each of the r categories. In the die example, for instance, consider the second

240

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

roll, which is recorded in T2. If that roll turns out to be, say, 5, then

Here is the key observation:

T2 =





















0
0
0
0
1
0



 =





X1
...
Xr

n
(cid:88)

i=1

Ti

(13.90)

(13.91)

Keep in mind, (13.91) is a vector equation. In the die example, the ﬁrst element of the left-hand
side,X1, is the number of times the 1-dot face turns up, and on the right-hand side, the ﬁrst element
of Ti is 1 or 0, according to whether the 1-dot face turns up on the ith roll. Make sure you believe
this equation before continuing.

Since the trials are independent, (12.52) and (13.91) now tell us that

Cov[(X1, ..., Xr)(cid:48)| =

n
(cid:88)

i=1

Cov(Ti)

(13.92)

But the trials are not only independent, but also identically distributed. (The die, for instance, has
the same probabilities on each trial.) So the last equation becomes





Cov





X1
...
Xr







 = nCov(T1)

(13.93)

One more step to go. Remember, T1 is a vector, recording what happens on the ﬁrst trial, e.g. the
ﬁrst roll of the die. Write it as

T1 =









U1
...
Ur

(13.94)

13.5. PARAMETRIC FAMILIES OF MULTIVARIATE DISTRIBUTIONS

241

Then the covariance matrix of T1 consists of elements of the form

Cov(Ui, Uj)

(13.95)

Let’s evaluate them.

Case 1: i = j

Case 2: i (cid:54)= j

Cov(Ui, Uj) = V ar(Ui)
= pi(1 − pi)

(12.4)

(3.79)

Cov(Ui, Uj) = E(UiUj) − EUiEUj

(12.5)

= E(UiUj) − pipj
= −pipj

(3.78)

(13.96)

(13.97)

(13.98)

(13.99)

(13.100)

with that last step coming from the fact that Ui and Uj can never both be 1 (e.g. never on the
same line of the our “notebook”). Thus the product UiUj is always 0, and thus so is is expected
value. In the die example, for instance, if our roll resulted in the 2-dot face turned upward, then
the 5-dot face deﬁnitely did NOT turn upward, so U2 = 1 while U5 = 0.

So, we’ve now found Cov(T1), and using this in (13.93), we see that





Cov











 = n







X1
...
Xr

p1(1 − p1)
−p1p2
...
...

−p1p2
p2(1 − p2)
...
...

−p1pr
−p2pr
...

...
...
...
... pr(1 − pr)







(13.101)

Note too that if we deﬁne R = X/n, so that R is the vector of proportions in the various categories
(e.g. X1/n is the fraction of trials that resulted in category 1), then from (13.101) and (12.51), we
have

Cov(R) =







1
n

p1(1 − p1)
−p1p2
...
...

−p1p2
p2(1 − p2)
...
...

−p1pr
−p2pr
...

...
...
...
... pr(1 − pr)







(13.102)

242

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

Whew! That was a workout, but these formulas will become very useful later on, both in this
chapter and subsequent ones.

13.5.1.4 Application: Text Mining

One of the branches of computer science in which the multinomial family plays a prominent role
is in text mining. One goal is automatic document classiﬁcation. We want to write software that
will make reasonably accurate guesses as to whether a document is about sports, the stock market,
elections etc., based on the frequencies of various key words the program ﬁnds in the document.

Many of the simpler methods for this use the bag of words model. We have r key words
we’ve decided are useful for the classiﬁcation process, and the model assumes that statistically
the frequencies of those words in a given document category, say sports, follow a multinomial
distribution. Each category has its own set of probabilities p1, ..., pr. For instance, if “Barry
Bonds” is considered one word, its probability will be much higher in the sports category than in
the elections category, say. So, the observed frequencies of the words in a particular document will
hopefully enable our software to make a fairly good guess as to the category the document belongs
to.

Once again, this is a very simple model here, designed to just introduce the topic to you. Clearly
the multinomial assumption of independence between trials is grossly incorrect here, most models
are much more complex than this.

13.5.2 The Multivariate Normal Family of Distributions

Note to the reader: This is a more diﬃcult section, but worth putting extra eﬀort into, as so many
statistical applications in computer science make use of it. It will seem hard at times, but in the
end won’t be too bad.

13.5.2.1 Densities

Intuitively, this family has densities which are shaped like multidimensional bells, just like the
univariate normal has the famous one-dimensional bell shape.

Let’s look at the bivariate case ﬁrst. The joint distribution of X1 and X2 is said to be bivariate
normal if their density is

fX,Y (s, t) =

1
(cid:112)1 − ρ2

− 1
e

2(1−ρ2)

2πσ1σ2

(cid:20)

(s−µ1)2
σ2
1

+ (t−µ2)2
σ2
2

− 2ρ(s−µ1)(t−µ2)
σ1σ2

(cid:21)

, − ∞ < s, t < ∞ (13.103)

13.5. PARAMETRIC FAMILIES OF MULTIVARIATE DISTRIBUTIONS

243

This looks horrible, and it is. But don’t worry, as we won’t work with this directly.
It’s important for conceptual reasons, as follows.

First, note the parameters here: µ1, µ2, σ1 and σ2 are the means and standard deviations of X and
Y, while ρ is the correlation between X and Y. So, we have a ﬁve-parameter family of distributions.

The multivariate normal family of distributions is parameterized by one vector-valued quantity,
the mean µ, and one matrix-valued quantity, the covariance matrix Σ. Speciﬁcally, suppose the
random vector X = (X1, ..., Xk)(cid:48) has a k-variate normal distribution.

The density has this form:

Here c is a constant, needed to make the density integrate to 1.0. It turns out that

fX (t) = ce−0.5(t−µ)(cid:48)Σ−1(t−µ)

c =

1
(2π)k/2(cid:112)det(Σ)

(13.104)

(13.105)

but we’ll never use this fact.

Here again (cid:48) denotes matrix transpose, -1 denotes matrix inversion and det() means determinant.
Again, note that t is a kx1 vector.

Since the matrix is symmetric, there are k(k+1)/2 distinct parameters there, and k parameters in
the mean vector, for a total of k(k+3)/2 parameters for this family of distributions.

13.5.2.2 Geometric Interpretation

Now, let’s look at some pictures, generated by R code which I’ve adapted from one of the entries
in the R Graph Gallery, http://addictedtor.free.fr/graphiques/graphcode.php?graph=42.4
Both are graphs of bivariate normal densities, with EX1 = EX2 = 0, V ar(X1) = 10, V ar(X2) = 15
and a varying value of the correlation ρ between X1 and X2. Figure 13.1 is for the case ρ = 0.2.

The surface is bell-shaped, though now in two dimensions instead of one. Again, the height of the
surface at any (s,t) point the relative likelihood of X1 being near s and X2 being near t. Say for
instance that X1 is height and X2 is weight. If the surface is high near, say, (70,150) (for height
of 70 inches and weight of 150 pounds), it mean that there are a lot of people whose height and
weight are near those values. If the surface is rather low there, then there are rather few people
whose height and weight are near those values.

4There appears to be an error in their deﬁnition of the function f(); the assignment to term5 should not have a

negative sign at the beginning.

244

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

Figure 13.1: Bivariate Normal Density, ρ = 0.2

s−10−50510t−10−50510z0.0050.0100.01513.5. PARAMETRIC FAMILIES OF MULTIVARIATE DISTRIBUTIONS

245

Figure 13.2: Bivariate Normal Density, ρ = 0.8

Now compare that picture to Figure 13.2, with ρ = 0.8.

Again we see a bell shape, but in this case “narrower.” In fact, you can see that when X1 (s) is
large, X2 (t) tends to be large too, and the same for “large” replaced by small. By contrast, the
surface near (5,5) is much higher than near (5,-5), showing that the random vector (X1, X2) is near
(5,5) much more often than (5,-5).

If we were to continue
All of this reﬂects the high correlation (0.8) between the two variables.
to increase ρ toward 1.0, we would see the bell become narrower and narrower, with X1 and X2
coming closer and closer to a linear relationship, one which can be shown to be

X1 − µ1 =

σ1
σ2

(X2 − µ2)

(13.106)

s−10−50510t−10−50510z0.0000.0050.0100.0150.0200.025246

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

In this case, that would be

X1 =

(cid:114) 10
15

X2 = 0.82X2

(13.107)

13.5.2.3 Properties of Multivariate Normal Distributions

Theorem 27 Suppose X = (X1, ..., Xk) has a multivariate normal distribution with mean vector
µ and covariance matrix Σ. Then:

(a) The contours of fX are k-dimensional ellipsoids. In the case k = 2 for instance, where we
can visualize the density of X as a three-dimensional surface, the contours for points at which
the bell has the same height (think of a topographical map) are elliptical in shape. The larger
the correlation (in absolute value) between X1 and X2, the more elongated the ellipse. When
the absolute correlation reaches 1, the ellipse degenerates into a straight line.

(b) Let A be a constant (i.e. nonrandom) matrix with k columns. Then the random vector Y =

AX also has a multivariate normal distribution.5
The parameters of this new normal distribution must be EY = Aµ and Cov(Y ) = AΣA(cid:48), by
(12.50) and (12.54).

(c) If U1, ..., Um are each univariate normal and they are independent, then they jointly have a
multivariate normal distribution. (In general, though, having a normal distribution for each
Ui does not imply that they are jointly multivariate normal.)

(d) Suppose W has a multivariate normal distribution. The conditional distribution of some

components of W, given other components, is again multivariate normal.

Part [(b)] has some important implications:

(i) The lower-dimensional marginal distributions are also multivariate normal. For example, if k

= 3, the pair (X1, X3)(cid:48) has a bivariate normal distribution, as can be seen by setting

A =

(cid:18) 1 0 0
0 0 1

(cid:19)

(13.108)

in (b) above.

5Note that this is a generalization of the material on aﬃne transformations on page 156.

13.5. PARAMETRIC FAMILIES OF MULTIVARIATE DISTRIBUTIONS

247

(ii) Scalar linear combinations of X are normal. In other words, for constant scalars a1, ..., ak, set
a = (a1, ..., ak)(cid:48). Then the quantity Y = a1X1 +...+akXk has a univariate normal distribution
with mean a(cid:48)µ and variance a(cid:48)Σa.

(iii) Vector linear combinations are multivariate normal. Again using the case k = 3 as our

example, consider (U, V )(cid:48) = (X1 − X3, X2 − X3). Then set

A =

(cid:18) 1 0 −1
0 1 −1

(cid:19)

(13.109)

(iv) The r-component random vector X has a multivariate normal distribution if and only if c(cid:48)X

has a univariate normal distribution for all constant r-component vectors c.

In R the density, cdf and quantiles of the multivariate normal distribution are given by the func-
tions dmvnorm(), pmvnorm() and qmvnorm() in the library mvtnorm. You can simulate a
multivariate normal distribution by using mvrnorm() in the library MASS.

13.5.2.4 The Multivariate Central Limit Theorem

The multidimensional version of the Central Limit Theorem holds. A sum of independent identically
distributed (iid) random vectors has an approximate multivariate normal distribution. Here is the
theorem:

Theorem 28 Suppose X1, X2, ... are independent random vectors, all having the same distribution
which has mean vector µ and covariance matrix Σ. Form the new random vector T = X1 + ... + Xn.
Then for large n, the distribution of T is approximately normal with mean nµ and covariance matrix
nΣ.

For example, since a person’s body consists of many diﬀerent components, the CLT (a non-
independent, non-identically version of it) explains intuitively why heights and weights are ap-
proximately bivariate normal. Histograms of heights will look approximately bell-shaped, and the
same is true for weights. The multivariate CLT says that three-dimensional histograms—plotting
frequency along the “Z” axis against height and weight along the “X” and “Y” axes—will be
approximately three-dimensional bell-shaped.

The proof of the multivariate CLT is easy, from Property (iv) above. Say we have a sum of iid
random vectors:

S = X1 + ... + Xn

(13.110)

248

Then

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

c(cid:48)S = c(cid:48)X1 + ... + c(cid:48)Xn

(13.111)

Now on the right side we have a sum of iid scalars, not vectors, so the univariate CLT applies!
We thus know the right-hand side is a approximately normal for all c, which means c(cid:48)S is also
approximately normal for all c, which then by (iv) above means that S itself is approximately
multivariate normal.

13.5.2.5 Example: Finishing the Loose Ends from the Dice Game

Recall the game example in Section 12.7:

Suppose we roll a die 50 times. Let X denote the number of rolls in which we get one dot, and let
Y be the number of times we get either two or three dots. For convenience, let’s also deﬁne Z to
be the number of times we get four or more dots, though our focus will be on X and Y. Suppose
also that we win $5 for each roll of a one, and $2 for each roll of a two or three.

Our analysis relied on the vector (X,Y,Z)’ having an approximate multivariate normal distribution.
Where does that come from? Well, ﬁrst note that the exact distribution of (X,Y,Z)’ is multinomial.
Then recall (13.91). The latter makes (X,Y,Z)’ a sum of iid vectors, so that the multivariate CLT
applies.

13.5.2.6 Application: Data Mining

The multivariate normal family plays a central role in multivariate statistical methods.

For instance, a major issue in data mining is dimension reduction, which means trying to reduce
what may be hundreds or thousands of variables down to a manageable level. One of the tools for
this, called principle components analysis (PCA), is based on multivariate normal distributions.
Google uses this kind of thing quite heavily. We’ll discuss PCA in Section 25.1.

To see a bit of how this works, note that in Figure 13.2, X1 and X2 had nearly a linear relationship
with each other. That means that one of them is nearly redundant, which is good if we are trying
to reduce the number of variables we must work with.

In general, the method of principle components takes r original variables, in the vector X and forms
r new ones in a vector Y, each of which is some linear combination of the original ones. These new
ones are independent. In other words, there is a square matrix A such that the components of Y =
AX are independent. (The matrix A consists of the eigenvectors of Cov(X); more on this in Section
25.1 of our unit on statistical relations.

13.5. PARAMETRIC FAMILIES OF MULTIVARIATE DISTRIBUTIONS

249

We then discard the Yi with small variance, as that means they are nearly constant and thus do
not carry much information. That leaves us with a smaller set of variables that still captures most
of the information of the original ones.

Many analyses in bioinformatics involve data that can be modeled well by multivariate normal
distributions. For example, in automated cell analysis, two important variables are forward light
scatter (FSC) and sideward light scatter (SSC). The joint distribution of the two is approximately
bivariate normal.6

Exercises

1. Suppose the random pair (X, Y ) has the density fX,Y (s, t) = 8st on the triangle {(s, t) : 0 < t <
s < 1}.

(a) Find fX (s).

(b) Find P (X < Y /2).

2. Suppose packets on a network are of three types. In general, 40% of the packets are of type A,
40% have type B and 20% have type C. We observe six packets, and denote the numbers of packets
of types A, B and C by X, Y and Z, respectively.

(a) Find P(X = Y = Z = 2).

(b) Find Cov(X,Y+Z).

(c) To what parametric family in this book does the distribution of Y+Z belong?

3. Suppose X and Y are independent, each having an exponential distribution with means 1.0 and
2.0, respectively. Find P (Y > X 2).

4. Suppose the pair (X,Y)’ has a bivariate normal distribution with mean vector (0,2) and covari-
ance matrix
(cid:18) 1 2
2 6

(cid:19)

(a) Set up (but do not evaluate) the double integral for the exact value of P (X 2 + Y 2 ≤ 2.8).

6See Bioinformatics and Computational Biology Solutions Using R and Bioconductor, edited by Robert Gentleman,

Wolfgang Huber, Vincent J. Carey, Rafael A. Irizarry and Sandrine Dudoit, Springer, 2005.

250

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

(b) Using the matrix methods of Section 12.4, ﬁnd the covariance matrix of the pair U = (X+Y,X-

2Y)’. Does U have a bivariate normal distribution?

5. Suppose X are Y independent, and each has a U(0,1) distribution. Let V = X + Y.

(a) Find fV . (Advice: It will be a “two-part function,” i.e. the type we have to describe by

saying something like, “The function has value 2z for z < 6 and 1/z for z > 6.”)

(b) Verify your answer in (a) by ﬁnding EV from your answer in (a) and then using the fact that

EX = EY = 0.5.

• In the general population of parents who have 10-year-old kids, the parent/kid weight pairs

have an exact bivariate normal distribution.

• Parents’ weights have mean 152.6 and standard deviation 25.0.

• Weights of kids have mean 62 and standard deviation 6.4.

• The correlation between the parents’ and kids’ weights is 0.4.

Use R functions (not simulation) in the following:

(a) Find the fraction of parents who weigh more than 160.

(b) Find the fraction of kids who weigh less than 56.

(c) Find the fraction of parent/child pairs in which the parent weighs more than 160 and the

child weighs less than 56.

(d) Suppose a ride at an amusement park charges by weight, one cent for each pound of weight
in the parent and child. State the exact distribution of the fee, and ﬁnd the fraction of
parent/child pairs who are charged less than $2.00.

6. Newspapers at a certain vending machine cost 25 cents. Suppose 60% of the customers pay with
quarters, 20% use two dimes and a nickel, 15% insert a dime and three nickels, and 5% deposit ﬁve
nickels. When the vendor collects the money, ﬁve coins fall to the ground. Let X, Y amd Z denote
the numbers of quarters, dimes and nickels among these ﬁve coins.

(a) Is the joint distribution of (X, Y, Z) a member of a parametric family presented in this chapter?

If so, which one?

13.5. PARAMETRIC FAMILIES OF MULTIVARIATE DISTRIBUTIONS

251

(b) Find P (X = 2, Y = 2, Z = 1).

(c) Find ρ(X, Y ).

7. Jack and Jill play a dice game, in which one wins $1 per dot. There are three dice, die A, die
B and die C. Jill always rolls dice A and B. Jack always rolls just die C, but he also gets credit for
90% of die B. For instance, say in a particular roll A, B and C are 3, 1 and 6, respectively. Then
Jill would win $4 and Jack would get $6.90. Let X and Y be Jill’s and Jack’s total winnings after
100 rolls. Use the Central Limit Theorem to ﬁnd the approximate values of P (X > 650, Y < 660)
and P (Y > 1.06X).

Hints: This will follow a similar pattern to the dice game in Section 12.7, which we win $5 for one
dot, and $2 for two or three dots. Remember, in that example, the key was that we noticed that
the pair (X, Y ) was a sum of random pairs. That meant that (X, Y ) had an approximate bivariate
normal distribution, so we could ﬁnd probabilities if we had the mean vector and covariance matrix
of (X, Y ). Thus we needed to ﬁnd EX, EY, V ar(X), V ar(Y ) and Cov(X, Y ). We used the various
properties of E(), V ar() and Cov() to get those quantities.

You will do the same thing here. Write X = U1 + ... + U100, where Ui is Jill’s winnings on the ith
roll. Write Y as a similar sum of Vi. You probably will ﬁnd it helpful to deﬁne Ai, Bi and Ci as
the numbers of dots appearing on dice A, B and C on the ith roll. Then ﬁnd EX etc. Again, make
sure to utilize the various properties for E(), V ar() and Cov().

8. Consider the coin game in Section 4.8. Find FX3,Y3(0, 0).
9. Suppose the random vector X = (X1, X2, X3)(cid:48) has mean (2.0, 3.0, 8.2)(cid:48) and covariance matrix





1 0.4 −0.2
0.25
3

1





(13.112)

(a) Fill in the three missing entries.

(b) Find Cov(X1, X3).

(c) Find ρ(X2, X3).

(d) Find V ar(X3).

(e) Find the covariance matrix of (X1 + X2, X2 + X3)(cid:48).

(f) If in addition we know that X1 has a normal distribution, ﬁnd P (1 < X1 < 2.5), in terms of

Φ().

252

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

(g) Consider the random variable W = X1 + X2. Which of the following is true? (i) V ar(W ) =
V ar(X1 + X2). (ii) V ar(W ) > V ar(X1 + X2). (iii) V ar(W ) < V ar(X1 + X2). (iv) In order
to determine which of the two variances is the larger one, we would need to know whether
the variables Xi have a multivariate normal distribution. (v) V ar(X1 + X2) doesn’t exist.

10. Find the (approximate) output of this R code, by using the analytical techniques of this
chapter:

count <- 0
for (i in 1:10000) {

count1 <- 0
count2 <- 0
count3 <- 0
for (j in 1:20) {

x <- runif(1)
if (x < 0.2) {

count1 <- count1 + 1

} else if (x < 0.6) count2 <- count2 + 1 else

count3 <- count3 + 1

}
if (count1 == 9 && count2 == 2 && count3 == 9) count <- count + 1

}
cat(count/10000)

11. Use the convolution formula (13.52) to derive (7.46) for the case r = 2. Explain your steps
carefully!

12. The book, Last Man Standing, author D. McDonald writes the following about the practice of
combining many mortgage loans into a single package sold to investors:

Even if every single [loan] in the [package] had a 30 percent risk of default, the think-
ing went, the odds that most of them would default at once were arguably inﬁnites-
imal...What [this argument] missed was the auto-synchronous relationship of many
loans...[If several of them] are all mortgage for houses sitting next to each other on
a beach...one strong hurricane and the [loan package] would be decimated.

Fill in the blank with a term from this book: The author is referring to an unwarranted assumption
of

.

13. Consider the computer worm example in Section 9.3.1. Let R denote the time it takes to go
from state 1 to state 3. Find fR(v). (Leave your answer in integral form.)

14. Suppose (X,Y) has a bivariate normal distribution, with EX = EY = 0, Var(X) = Var(Y) =
1, and ρ(X, Y ) = 0.2. Find the following, leaving your answers in integral form:

13.5. PARAMETRIC FAMILIES OF MULTIVARIATE DISTRIBUTIONS

253

(a) E(X 2 + XY 0.5)

(b) P (Y > 0.5X)

(c) FX,Y (0.6, 0.2)

254

CHAPTER 13. MULTIVARIATE PMFS AND DENSITIES

Chapter 14

Transform Methods

We often use the idea of transform functions. For example, you may have seen Laplace trans-
forms in a math or engineering course. The functions we will see here diﬀer from this by just a
change of variable.

Though in the form used here they involve only univariate distributions, their applications are often
multivariate, as will be the case here.

14.1 Generating Functions

Let’s start with the generating function. For any nonnegative-integer valued random variable
V, its generating function is deﬁned by

gV (s) = E(sV ) =

∞
(cid:88)

i=0

sipV (i), 0 ≤ s ≤ 1

(14.1)

For instance, suppose N has a geometric distribution with parameter p, so that pN (i) = (1 − p)pi−1,
i = 1,2,... Then

gN (s) =

∞
(cid:88)

i=1

si · (1 − p)pi−1 =

1 − p
p

∞
(cid:88)

i=1

si · pi =

1 − p
p

ps
1 − ps

=

(1 − p)s
1 − ps

(14.2)

Why restrict s to the interval [0,1]? The answer is that for s > 1 the series in (14.1) may not
converge. for 0 ≤ s ≤ 1, the series does converge. To see this, note that if s = 1, we just get the

255

256

CHAPTER 14. TRANSFORM METHODS

sum of all probabilities, which is 1.0. If a nonnegative s is less than 1, then si will also be less than
1, so we still have convergence.

One use of the generating function is, as its name implies, to generate the probabilities of values
for the random variable in question. In other words, if you have the generating function but not
the probabilities, you can obtain the probabilities from the function. Here’s why: For clarify, write
(14.1) as

gV (s) = P (V = 0) + sP (V = 1) + s2P (V = 2) + ...

(14.3)

From this we see that

gV (0) = P (V = 0)

(14.4)

So, we can obtain P(V = 0) from the generating function. Now diﬀerentiating (14.1) with respect
to s, we have

g(cid:48)
V (s) =

d
ds

(cid:2)P (V = 0) + sP (V = 1) + s2P (V = 2) + ...(cid:3)

= P (V = 1) + 2sP (V = 2) + ...

(14.5)

So, we can obtain P(V = 2) from g(cid:48)
from the higher derivatives.

V (0), and in a similar manner can calculate the other probabilities

14.2 Moment Generating Functions

The generating function is handy, but it is limited to discrete random variables. More generally,
we can use the moment generating function, deﬁned for any random variable X as

mX (t) = E[etX ]

(14.6)

for any t for which the expected value exists.

That last restriction is anathema to mathematicians, so they use the characteristic function,

φX (t) = E[eitX ]

(14.7)

14.3. TRANSFORMS OF SUMS OF INDEPENDENT RANDOM VARIABLES

257

which exists for any t. However, it makes use of pesky complex numbers, so we’ll stay clear of it
here.

Diﬀerentiating (14.6) with respect to t, we have

We see then that

m(cid:48)

X (t) = E[XetX ]

m(cid:48)

X (0) = EX

So, if we just know the moment-generating function of X, we can obtain EX from it. Also,

so

m(cid:48)(cid:48)

X (t) = E(X 2etX )

m(cid:48)(cid:48)

X (0) = E(X 2)

(14.8)

(14.9)

(14.10)

(14.11)

In this manner, we can for various k obtain E(X k), the kth moment of X, hence the name.

14.3 Transforms of Sums of Independent Random Variables

Suppose X and Y are independent and their moment generating functions are deﬁned. Let Z =
X+Y. then

mZ(t) = E[et(X+Y )] = E[etX etY ] = E(etX ) · E(etY ) = mX (t)mY (t)

(14.12)

In other words, the mgf of the sum is the product of the mgfs! This is true for other transforms,
by the same reasoning.

Similarly, it’s clear that the mgf of a sum of three independent variables is again the product of
their mgfs, and so on.

258

CHAPTER 14. TRANSFORM METHODS

14.4 Example: Network Packets

As an example, suppose say the number of packets N received on a network link in a given time
period has a Poisson distribution with mean µ, i.e.

P (N = k) =

e−µµk
k!

, k = 0, 1, 2, 3, ...

(14.13)

14.4.1 Poisson Generating Function

Let’s ﬁrst ﬁnd its generating function.

gN (t) =

∞
(cid:88)

k=0

tk e−µµk
k!

= e−µ

∞
(cid:88)

k=0

(µt)k
k!

= e−µ+µt

where we made use of the Taylor series from calculus,

eu =

∞
(cid:88)

k=0

uk/k!

(14.14)

(14.15)

14.4.2 Sums of Independent Poisson Random Variables Are Poisson Distributed

Supposed packets come in to a network node from two independent links, with counts N1 and N2,
Poisson distributed with means µ1 and µ2. Let’s ﬁnd the distribution of N = N1 + N2, using a
transform approach.

From Section 14.3:

where ν = µ1 + µ2.

gN (t) = gN1(t)gN2(t) = e−ν+νt

(14.16)

But the last expression in (14.16) is the generating function for a Poisson distribution too! And
since there is a one-to-one correspondence between distributions and transforms, we can conclude
that N has a Poisson distribution with parameter ν. We of course knew that N would have mean
ν but did not know that N would have a Poisson distribution.

So: A sum of two independent Poisson variables itself has a Poisson distribution. By induction,
this is also true for sums of k independent Poisson variables.

14.5. OTHER USES OF TRANSFORMS

259

14.5 Other Uses of Transforms

Transform techniques are used heavily in queuing analysis, including for models of computer net-
works. The techniques are also used extensively in modeling of hardware and software reliability.

Transforms also play key roles in much of theoretical probability, the Central Limit Theorems1
being a good example. Here’s an outline of the proof of the basic CLT, assuming the notation of
Section 8.19:

First rewrite Z as

Z =

n
(cid:88)

i=1

Then work with the characteristic function of Z:

Xi − m
√

v

n

cZ(t) = E(eitZ)
= Πn
= Πn

i=1E[eit(Xi−m)/(v
i=1E[eit(X1−m)/(v

n)]
√
n)]

(def.)

√

= [g(

it
√
n

)]n

where g(s) is the characteristic function of (X1 − m)/v, i.e.

g(s) = E[eis· X1−m

v

]

(indep.)

(ident. distr.)

(14.17)

(14.18)

(14.19)

(14.20)

(14.21)

(14.22)

Now expand (14.21) in a Taylor series around 0, and use the fact that g(cid:48)(0) is the expected value
of (X1 − m)/v, which is 0:

[g(

t
√
n

)]n =

(cid:20)

1 −

t2
2n
→ e−t2/2 as n → ∞

t2
n

+ o(

(cid:21)n
)

(14.23)

(14.24)

where we’ve also used the famous fact that (1 − s/n)n converges to e−s as n → ∞.

1The plural is used here because there are many diﬀerent versions, which for instance relax the condition that the

summands be independent and identically distributed.

260

CHAPTER 14. TRANSFORM METHODS

But (14.24) is the density of N(0,1), so we have proved the Central Limit Theorem.

Exercises

1. Use transform methods to derive some properties of the Poisson family:

(a) Show that for any Poisson random variable, its mean and variance are equal.

(b) Suppose X and Y are independent random variables, each having a Poisson distribution.

Show that Z = X + Y again has a Poisson distribution.

2. In our ordinary coins which we use every day, each one has a slightly diﬀerent probability of
heads, which we’ll call H. Say H has the distribution N (0.5, 0.032). We choose a coin from a batch
at random, then toss it 10 times. Let N be the number of heads we get. Find V ar(N ).

3. Suppose the number N of bugs in a certain number of lines of code has a Poisson distribution,
with parameter L, where L varies from one programmer to another. Show that Var(N) = EL +
Var(L).

4. Let X denote the number we obtain when we roll a single die once. Let GX (s) denote the
generating function of X.

(a) Find GX (s).

(b) Suppose we roll the die 5 times, and let T denote the total number of dots we get from the 5

rolls. Find GT (s).

Chapter 15

Statistics: Prologue

There are three kinds of lies:
Disraeli, Mark Twain etc.

lies, damned lies and statistics—variously attributed to Benjamin

Consider the following problems:

• Suppose you buy a ticket for a raﬄe, and get ticket number 68. Two of your friends bought
tickets too, getting numbers 46 and 79. Let c be the total number of tickets sold. You don’t
know the value of c, but hope it’s small, so you have a better chance of winning. How can
you estimate the value of c, from the data, 68, 46 and 79?

• It’s presidential election time. A poll says that 56% of the voters polled support candidate
X, with a margin of error of 2%. The poll was based on a sample of 1200 people. How can a
sample of 1200 people out of more than 100 million voters have a margin of error that small?
And what does the term margin of error really mean, anyway?

• A satellite detects a bright spot in a forest. Is it a ﬁre? How can we design the software on

the satellite to estimate the probability that this is a ﬁre?

If you think that statistics is nothing more than adding up columns of numbers and plugging
into formulas, you are badly mistaken. Actually, statistics is an application of probability theory.
We employ probabilistic models for the behavior of our sample data, and infer from the data
accordingly—hence the name, statistical inference.

Arguably the most powerful use of statistics is prediction. This has applications from medicine to
marketing to movie animation. We will study prediction in Chapter 22.

261

262

CHAPTER 15. STATISTICS: PROLOGUE

15.1 Sampling Distributions

We ﬁrst will set up some infrastructure, which will be used heavily throughout the next few chapters.

15.1.1 Random Samples

Deﬁnition 29 Random variables X1, X2, X3, ... are said to be i.i.d. if they are independent and
identically distributed. The latter term means that pXi or fXi is the same for all i.

Note the following carefully:

For i.i.d. X1, X2, X3, ..., we often use X to represent a generic random variable having
the common distribution of the Xi.

Deﬁnition 30 We say that X1, X2, X3, ..., Xn is a random sample of size n from a population
if the Xi are i.i.d. and their common distribution is that of the population.

(Please note: Those numbers X1, X2, X3, ..., Xn collectively form one sample; you should not say
anything like “We have n samples.”)

If the sampled population is ﬁnite,1 then a random sample must be drawn in this manner. Say
there are k entities in the population, e.g. k people, with values v1, ..., vk. ’ If we are interested in
people’s heights, for instance, then v1, ..., vk would be the heights of all people in our population.
Then a random sample is drawn this way:

(a) The sampling is done with replacement.

(b) Each Xi is drawn from v1, ..., vk, with each vj having probability 1

k of being drawn.

Condition (a) makes the Xi independent, while (b) makes them identically distributed.

If sampling is done without replacement, we call the data a simple random sample. Note how
this implies lack of independence of the Xi. If for instance X1 = v3, then we know that no other Xi
has that value, contradicting independence; if the Xi were independent, knowledge of one should
not give us knowledge concerning others.

1You might wonder how it could be inﬁnite. Say for example we model the distribution of X as a continuous
random variable, then there are inﬁnitely many values X can take on, though it must be kept in mind that, as noted
in Section 7.15, continuous random variables are only idealizations.

15.2. THE SAMPLE MEAN—A RANDOM VARIABLE

263

But we usually assume true random sampling, and will mean that unless explicitly stating otherwise.

Keep this very important point in mind:

Note most carefully that each Xi has the same distribution as the population. If for
instance a third of the population, i.e. a third of the vj, are less than 28, then P (Xi < 28)
will be 1/3.

If the mean value of X in the population is, say, 51.4, then EX will be 51.4, and so on.

These points areeasy to see, but keep them in mind at all times, as they will arise again
and again.

15.2 The Sample Mean—a Random Variable

A large part of this chapter will concern the sample mean,

X =

X1 + X2 + X3 + ... + Xn
n

(15.1)

A simple yet crucial concept is point that X is a random variable. Since X1, X2, X3, ..., Xn are
random variables, X is a random variable too.

Make absolutely sure to distinguish between the sample mean and the population mean.

15.2.1 Toy Population Example

Let’s illustrate it with a tiny example. Suppose we have a population of three people, with heights
69, 72 and 70, and we draw a random sample of size 2. Here X can take on six values:

69 + 69
2

= 69,

69 + 72
2

= 70.5,

69 + 70
2

= 69.5,

70 + 70
2

= 70,

70 + 72
2

= 71,

72 + 72
2

= 72 (15.2)

The probabilities of these values are 1/9, 2/9, 2/9, 1/9, 2/9 and 1/9, respectively. So,

pX (69) =

1
9

, pX (70.5) =

2
9

, pX (69.5) =

2
9

, pX (70) =

1
9

, pX (71) =

2
9

, pX (72) =

1
9

(15.3)

Since X is a random variable, it also has a cdf. For instance, the reader should verify that
FX (69.9) = 5/9.

264

CHAPTER 15. STATISTICS: PROLOGUE

Viewing it in “notebook” terms, we might have, in the ﬁrst three lines:

notebook line X1 X2
70
1
70
2
70
3

70
69
72

X
70
69.5
71

Again, the point is that all of X1, X2 and X are random variables.

15.2.2 Expected and Variance of X

Now, returning to the case of general n and our sample X1, ..., Xn, since X is a random variable, we
can ask about its expected value and variance. Note that in notebook terms, these are the long-run
mean and variance of the values in the X column above.

Let µ denote the population mean. Remember, each Xi is distributed as is the population, so
EXi = µ. Again in notebook terms, this says that the long-run average in the X1 column will be
µ. (The same will be true for the X2 column.)

This then implies that the expected value of X is also µ. Here’s why:

(cid:34)

E

n
(cid:88)

1
n
i=1
(cid:32) n
(cid:88)

(cid:35)

(cid:33)

Xi

Xi

i=1

(def. of X )

(for const. c, E(cU ) = cEU )

n
(cid:88)

i=1

EXi

(E[U + V ] = EU + EV )

nµ

(EXi = µ)

E(X) = E

=

=

1
n

1
n

=

1
n
= µ

(15.4)

(15.5)

(15.6)

(15.7)

(15.8)

Moreover, the variance of X is 1/n times the population variance:

265

(15.9)

15.2. THE SAMPLE MEAN—A RANDOM VARIABLE

V ar(X) = V ar

(cid:34)

n
(cid:88)

(cid:35)

Xi

1
n

1
n2 V ar

i=1
(cid:32) n
(cid:88)

i=1

(cid:33)

Xi

(for const. c, , V ar[cU ] = c2V ar[U ])

(15.10)

1
n2

n
(cid:88)

i=1

1
n2 nσ2
1
σ2
n

V ar(Xi)

(for U,V indep., , V ar[U + V ] = V ar[U ] + V ar[V ]) (15.11)

(15.12)

(15.13)

=

=

=

=

15.2.3 Toy Population Example Again

Let’s verify (15.8) and (15.13) for toy population in Section 15.2.1. The population mean is

µ = (69 + 70 + 72)/3 = 211/3

(15.14)

Using (3.13) and (15.3), we have

EX =

(cid:88)

c

c pX (c) = 69 ·

1
9

+ 69.5 ·

2
9

+ 70 ·

1
9

+ 70.5 ·

2
9

+ 71 ·

2
9

+ 72 ·

1
9

= 211/3

(15.15)

So, (15.8) is conﬁrmed. What about (15.13)?

First, the population variance is

σ2 =

1
3

· (692 + 702 + 722) − (

211
3

)2 =

14
9

The variance of X is

V ar(X) = E(X

= E(X

2

2

) − (EX)2

) − (

211
3

)2

(15.16)

(15.17)

(15.18)

266

CHAPTER 15. STATISTICS: PROLOGUE

Using (3.36) and (15.3), we have

E(X

2

) =

(cid:88)

c2 pX (c) = 692 ·

c

1
9

+ 69.52 ·

2
9

+ 702 ·

1
9

+ 70.52 ·

2
9

+ 712 ·

2
9

+ 722 ·

1
9

(15.19)

The reader should now wrap things up and conﬁrm that (15.17) does work out to (14/9) / 2 =
7/9, as claimed by (15.13) and (15.16).

15.2.4 Interpretation

Now, let’s step back and consider the signiﬁcance of the above ﬁndings (15.8) and (15.13):

(a) Equation (15.8) tells us that although some samples give us an X that is too high, i.e. that
overestimates µ, while other samples give us an X that is too low, on average X is “just
right.”

(b) Equation (15.13) tells us that for large samples, i.e. large n, X doesn’t vary much from sample

to sample.

If you put (a) and (b) together, it says that for large n, X is probably pretty accurate, i.e. pretty
close to the population mean µ. (You may wish to view this in terms of Section 3.63.) So, the story
of statistics often boils down to asking, “Is the variance of our estimator small enough?” You’ll see
this in the coming chapters.

15.3 Sample Means Are Approximately Normal—No Matter What

the Population Distribution Is

The Central Limit Theorem tells us that the numerator in (15.1) has an approximate normal
distribution. That means that aﬃne transformations of that numerator are also approximately
normally distributed (page 156). So:

Approximate distribution of (centered and scaled) X:

The quantity

has an approximately N(0,1) distribution, where σ2 is the population variance.

Z =

X − µ
√
n
σ/

(15.20)

15.3. SAMPLE MEANS ARE APPROXIMATELY NORMAL—NO MATTER WHAT THE POPULATION DISTRIBUTION IS267

pop. entity
EX
X
E[]

samp. entity
X
Xi
(cid:80)n

i=1

1
n

Table 15.1: Population and Sample Analogs

Make sure you understand why it is the “N” that is approximate here, not the 0 or 1.

So even if the population distribution is very skewed, multimodal and so on, the sample mean will
still have an approximate normal distribution. This will turn out to be the core of statistics; they
don’t call the theorem the Central Limit Theorem for nothing.

15.3.1 The Sample Variance—Another Random Variable

Later we will be using the sample mean X, a function of the Xi, to estimate the population mean
µ. What other function of the Xi can we use to estimate the population variance σ2?

Let X denote a generic random variable having the distribution of the Xi, which, note again, is the
distribution of the population. Because of that property, we have

V ar(X) = σ2

(σ2 is the population variance)

(15.21)

Recall that by deﬁnition

V ar(X) = E[(X − EX)2]

(15.22)

15.3.1.1 Intuitive Estimation of σ2

Let’s estimate V ar(X) = σ2 by taking sample analogs in (15.22). The correspondences are shown
in Table 15.1.

The sample analog of µ is X. What about the sample analog of the “E()”? Well, since E() averaging
over the whole population of Xs, the sample analog is to average over the sample. So, our sample

268

CHAPTER 15. STATISTICS: PROLOGUE

analog of (15.22) is

s2 =

1
n

n
(cid:88)

(Xi − X)2

i=1

(15.23)

In other words, just as it is natural to estimate the population mean of X by its sample mean, the
same holds for Var(X):

The population variance of X is the mean squared distance from X to its population
mean, as X ranges over all of the population. Therefore it is natural to estimate Var(X)
by the average squared distance of X from its sample mean, among our sample values
Xi, shown in (15.23).

We use s2 as our symbol for this estimate of population variance.2

15.3.1.2 Easier Computation

By the way, it can be shown that (15.23) is equal to

1
n

n
(cid:88)

i=1

X 2

i − X

2

(15.24)

This is a handy way to calculate s2, though it is subject to more roundoﬀ error. Note that (15.24)
is a sample analog of (3.41).

15.3.1.3 To Divide by n or n-1?

It should be noted that it is common to divide by n-1 instead of by n in (15.23). In fact, almost
all textbooks divide by n-1 instead of n. Clearly, unless n is very small, the diﬀerence will be
minuscule; such a small diﬀerence is not going to aﬀect any analyst’s decisionmaking. But there
are a couple of important conceptual questions here:

• Why do most people (and R, in its var() function) divide by n-1?

• Why do I choose to use n?

2Though I try to stick to the convention of using only capital letters to denote random variables, it is conventional

to use lower case in this instance.

15.4. OBSERVATIONAL STUDIES

269

The answer to the ﬁrst question is that (15.23) is what is called biased downwards, meaning that
it can be shown (Section 18.2.2) that

E(s2) =

n − 1
n

σ2

(15.25)

In notebook terms, if we were to take many, many samples, one per line in the notebook, in the long
run the average of all of our s2 values would be slightly smaller than σ2. This bothered the early
pioneers of statistics, so they decided to divide by n-1 to make the sample variance an unbiased
estimator of σ2. Their deﬁnition of s2 is

s2 =

1
n − 1

n
(cid:88)

(Xi − X)2

i=1

(15.26)

This is why W. Gossett deﬁned his now-famous Student-t distribution using (15.26), with a factor
of n-1 instead of n. But he could have just as easily deﬁned it as (15.23).

Moreover, even though s2 is unbiased under their deﬁnition, their s itself is still biased downward
(Section 18.2.2.1). And since s itself is what we (this book and all others) use in forming conﬁdence
intervals, one can see that insisting on unbiasedness is a losing game.

I choose to use (15.23), dividing by n, because of Table 15.1; it’s very important that students
understand this idea of sample analogs. Another virtue of this approach is that it is in a certain
sense more consistent, as we’ll see in Section 16.4.2.

15.4 Observational Studies

The above formulation of sampling is rather idealized. It assumes a well-deﬁned population, from
which each unit is equally likely to be sampled. In real life, though, things are often not so clearcut.

In Section 16.9, for instance, we analyze data on major league baseball players, and apply statistical
inference procedures based on the material in the current chapter. The player data is for a particular
year, and our population is the set of all major league players, past, present and future. But here,
no physical sampling occurred; we are implicitly assuming that our data act like a random sample
from that population.

That in turn means that there was nothing special about our particular year. A player in our
year, for instance, is just as likely to weigh more than 220 pounds than in previous years. This
is probably a safe assumption, but at the same time it probably means we should restrict our
(conceptual) population to recent years; back in the 1920s, players probably were not as big as
those we see today.

270

CHAPTER 15. STATISTICS: PROLOGUE

The term usually used for settings like that of the baseball player data is observational studies. We
passively observe our data rather than obtaining it by physically sampling from the population. The
careful analyst must consider whether his/her data are representative of the conceptual population,
versus subject to some bias.

15.5 A Good Time to Stop and Review!

The material we’ve discussed since page 262, is absolutely key, forming the very basis of statistics.
It will be used constantly, throughout all our chapters here on statistics.
It would be highly
worthwhile for the reader to review this chapter before continuing.

Chapter 16

Introduction to Conﬁdence Intervals

The idea of a conﬁdence interval is central to statistical inference. But actually, you already know
about it—from the term margin of error in news reports about opinion polls.

16.1 The “Margin of Error” and Conﬁdence Intervals

To explain the idea of margin of error, let’s begin with a problem that has gone unanswered so far:

In our simulations in previous units, it was never quite clear how long the simulation should be
run, i.e. what value to set for nreps in Section 2.14.4. Now we will ﬁnally address this issue.

As our example, consider the Bus Paradox, which presented in Section 9.4: Buses arrive at a certain
bus stop at random times, with interarrival times being independent exponentially distributed
random variables with mean 10 minutes. You arrive at the bus stop every day at a certain time,
say four hours (240 minutes) after the buses start their morning run. What is your mean wait µ
for the next bus?

We found mathematically that, due to the memoryless property of the exponential distribution, our
wait is again exponentially distributed with mean 10. But suppose we didn’t know that, and we
wished to ﬁnd the answer via simulation. (Note to reader: Keep in mind throughout this example
that we will be pretending that we don’t know the mean wait is actually 10. Reminders of this will
be brought up occasionally.)

We could write a program to do this:

1

2

3

doexpt <- function(opt) {
lastarrival <- 0.0
while (lastarrival < opt)

271

272

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

4

5

6

7

8

9

10

11

12

lastarrival <- lastarrival + rexp(1,0.1)

return(lastarrival-opt)

}

observationpt <- 240
nreps <- 1000
waits <- vector(length=nreps)
for (rep in 1:nreps) waits[rep] <- doexpt(observationpt)
cat("approx. mean wait = ",mean(waits),"\n")

Running the program yields

approx. mean wait = 9.653743

Note that µ is a population mean, where our “population” here is the set of all possible bus wait
times (some more frequent than others). Our simulation, then, drew a sample of size 1000 from
that population. The expression mean(waits) was our sample mean.

Now, was 1000 iterations enough? How close is this value 9.653743 to the true expected value of
waiting time?1

What we would like to do is something like what the pollsters do during presidential elections,
when they say “Ms. X is supported by 62% of the voters, with a margin of error of 4%.” In other
words, we want to be able to attach a margin of error to that ﬁgure of 9.653743 above. We do this
in the next section.

16.2 Conﬁdence Intervals for Means

We are now set to make use of the infrastructure that we’ve built up in the preceding sections of
this chapter. Everything will hinge on understanding that the sample mean is a random variable,
with a known approximate distribution.

The goal of this section (and several that follow) is to develop a notion of margin of
error, just as you see in the election campaign polls. This raises two questions:

(a) What do we mean by “margin of error”?

(b) How can we calculate it?

1Of course, continue to ignore the fact that we know that this value is 10.0. What we’re trying to do here is ﬁgure

out how to answer “how close is it” questions in general, when we don’t know the true mean.

16.2. CONFIDENCE INTERVALS FOR MEANS

273

16.2.1 Basic Formulation

So, suppose we have a random sample W1, ..., Wn from some population with mean µ and variance
σ2.

Recall that (15.20) has an approximate N(0,1) distribution. We will be interested in the central
95% of the distribution N(0,1). Due to symmetry, that distribution has 2.5% of its area in the left
tail and 2.5% in the right one. Through the R call qnorm(0.025), or by consulting a N(0,1) cdf
table in a book, we ﬁnd that the cutoﬀ points are at -1.96 and 1.96. In other words, if some random
variable T has a N(0,1) distribution, then P (−1.96 < T < 1.96) = 0.95.

Thus

(cid:18)

0.95 ≈ P

−1.96 <

W − µ
√
n
σ/

(cid:19)

< 1.96

(Note the approximation sign.) Doing a bit of algebra on the inequalities yields

(cid:18)

0.95 ≈ P

W − 1.96

σ
√
n

< µ < W + 1.96

(cid:19)

σ
√
n

(16.1)

(16.2)

Now remember, not only do we not know µ, we also don’t know σ. But we can estimate it, as we
saw, via (15.23). One can show (the details will be given in Section ??) that (16.2) is still valid if
we substitute s for σ, i.e.

(cid:18)

0.95 ≈ P

W − 1.96

s
√
n

< µ < W + 1.96

(cid:19)

s
√
n

In other words, we are about 95% sure that the interval

(W − 1.96

s
√
n

, W + 1.96

s
√
n

)

(16.3)

(16.4)

contains µ. This is called a 95% conﬁdence interval for µ. The quantity 1.96 s√
of error.

n is the margin

16.2.2 Example: Simulation Output

We could add this feature to our program in Section 16.1:

274

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

doexpt <- function(opt) {
lastarrival <- 0.0
while (lastarrival < opt)

lastarrival <- lastarrival + rexp(1,0.1)

return(lastarrival-opt)

}

observationpt <- 240
nreps <- 10000
waits <- vector(length=nreps)
for (rep in 1:nreps) waits[rep] <- doexpt(observationpt)
wbar <- mean(waits)
cat("approx. mean wait =",wbar,"\n")
s2 <- mean(waits^2) - wbar^2
s <- sqrt(s2)
radius <- 1.96*s/sqrt(nreps)
cat("approx. CI for EW =",wbar-radius,"to",wbar+radius,"\n")

When I ran this, I got 10.02565 for the estimate of EW, and got an interval of (9.382715, 10.66859).
Note that the margin of error is the radius of that interval, about 1.29/2. We would then say, “We
are about 95% conﬁdent that the true mean wait time is between 9.38 and 10.67.”

What does this really mean? This question is of the utmost importance. We will devote an
entire section to it, Section 16.3.

Note that our analysis here is approximate, based on the Central Limit Theorem, which was
applicable because W involves a sum. We are making no assumption about the density of the
population from which the Wi are drawn. However, if that population density itself is normal, then
an exact conﬁdence interval can be constructed. This will be discussed in Section 16.7.

16.3 Meaning of Conﬁdence Intervals

16.3.1 A Weight Survey in Davis

Consider the question of estimating the mean weight, denoted by µ, of all adults in the city of
Davis. Say we sample 1000 people at random, and record their weights, with Wi being the weight
of the ith person in our sample.2

Now remember, we don’t know the true value of that population mean, µ—again,
that’s why we are collecting the sample data, to estimate µ! Our estimate will be our
sample mean, W. But we don’t know how accurate that estimate might be. That’s

2Do you like our statistical pun here? Typically an example like this would concern people’s heights, not weights.
But it would be nice to use the same letter for random variables as in Section 16.2, i.e. the letter W, so we’ll have
our example involve people’s weights instead of heights. It works out neatly, because the word weight has the same
sound as wait.

16.3. MEANING OF CONFIDENCE INTERVALS

275

the reason we form the conﬁdence interval, as a gauge of the accuracy of W as an
estimate of µ.

Say our interval (16.4) turns out to be (142.6,158.8). We say that we are about 95% conﬁdent that
the mean weight µ of all adults in Davis is contained in this interval. What does this mean?

Say we were to perform this experiment many, many times, recording the results in a notebook:
We’d sample 1000 people at random, then record our interval (W − 1.96 s√
n ) on the
ﬁrst line of the notebook. Then we’d sample another 1000 people at random, and record what
interval we got that time on the second line of the notebook. This would be a diﬀerent set of 1000
people (though possibly with some overlap), so we would get a diﬀerent value of W and so, thus a
diﬀerent interval; it would have a diﬀerent center and a diﬀerent radius. Then we’d do this a third
time, a fourth, a ﬁfth and so on.

n , W + 1.96 s√

Again, each line of the notebook would contain the information for a diﬀerent random sample of
1000 people. There would be two columns for the interval, one each for the lower and upper bounds.
And though it’s not immediately important here, note that there would also be columns for W1
through W1000, the weights of our 1000 people, and columns for W and s.

Now here is the point: Approximately 95% of all those intervals would contain µ, the mean weight
in the entire adult population of Davis. The value of µ would be unknown to us—once again, that’s
why we’d be sampling 1000 people in the ﬁrst place—but it does exist, and it would be contained
in approximately 95% of the intervals.

As a variation on the notebook idea, think of what would happen if you and 99 friends each do
this experiment. Each of you would sample 1000 people and form a conﬁdence interval. Since each
of you would get a diﬀerent sample of people, you would each get a diﬀerent conﬁdence interval.
What we mean when we say the conﬁdence level is 95% is that of the 100 intervals formed—by
you and 99 friends—about 95 of them will contain the true population mean weight. Of course,
you hope you yourself will be one of the 95 lucky ones! But remember, you’ll never know whose
intervals are correct and whose aren’t.

Now remember, in practice we only take one sample of 1000 people. Our notebook
idea here is merely for the purpose of understanding what we mean when we say that
we are about 95% conﬁdent that one interval we form does contain the true value of
µ.

16.3.2 More About Interpretation

Some statistics instructors give students the odd warning, “You can’t say that the probability is
95% that µ is IN the interval; you can only say that the probability is 95% conﬁdent that the interval
CONTAINS µ.” This of course is nonsense. As any fool can see, the following two statements are

276

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

equivalent:

• “µ is in the interval”

• “the interval contains µ”

So it is ridiculous to say that the ﬁrst is incorrect. Yet many instructors of statistics say so.

Where did this craziness come from? Well, way back in the early days of statistics, some instructor
was afraid that a statement like “The probability is 95% that µ is in the interval” would make
it sound like µ is a random variable. Granted, that was a legitimate fear, because µ is not a
random variable, and without proper warning, some learners of statistics might think incorrectly.
The random entity is the interval (both its center and radius), not µ; W and s in (16.4) vary from
sample to sample, so the interval is indeed the random object here, not µ.

So, it was reasonable for teachers to warn students not to think µ is a random variable. But later on,
some misguided instructor must have then decided that it is incorrect to say “µ is in the interval,”
and others then followed suit. They continue to this day, sadly.

A variant on that silliness involves saying that one can’t say “The probability is 95% that µ is in
the interval,” because µ is either in the interval or not, so that “probability” is either 1 or 0! That
is equally mushy thinking.

Suppose, for example, that I go into the next room and toss a coin, letting it land on the ﬂoor. I
return to you, and tell you the coin is lying on the ﬂoor in the next room. I know the outcome but
you don’t. What is the probability that the coin came up heads? To me that is 1 or 0, yes, but to
you it is 50%, in any practical sense.

It is also true in the “notebook” sense. If I do this experiment many times—go to the next room,
toss the coin, come back to you, go to the next room, toss the coin, come back to you, etc., one
line of the notebook per toss—then in the long run 50% of the lines of the notebook have Heads in
the Outcome column.

The same is true for conﬁdence intervals. Say we conduct many, many samplings, one per line of
the notebook, with a column labeled Interval Contains Mu. Unfortunately, we ourselves don’t get
to see that column, but it exists, and in the long run 95% of the entries in the column will be Yes.

Finally, there are those who make a distinction between saying “There is a 95% probability that...”
and “We are 95% conﬁdent that...” That’s silly too. What else could “95% conﬁdent” mean if not
95% probability?

Consider the experiment of tossing two fair dice. The probability is 34/36, or about 94%, that
we get a total that is diﬀerent from 2 or 12. As we toss the dice, what possible distinction could
be made between saying, “The probability is 94% that we will get a total between 3 and 11”

16.4. CONFIDENCE INTERVALS FOR PROPORTIONS

277

and saying, “We are 94% conﬁdent that we will get a total between 3 and 11”? The notebook
interpretation supports both phrasings, really. The words probability and conﬁdent should not be
given much weight here; remember the quote at the beginning of our Chapter 1:

I learned very early the diﬀerence between knowing the name of something and knowing
something—Richard Feynman, Nobel laureate in physics

16.4 Conﬁdence Intervals for Proportions

So we know how to ﬁnd conﬁdence intervals for means. How about proportions?

16.4.1 Derivation

For example, in an election opinion poll, we might be interested in the proportion p of people in
the entire population who plan to vote for candidate A.

We will estimate p by taking a random sample of n voters, and ﬁnding the sample proportion of
voters who plan to vote for A. The latter is usually denoted (cid:98)p, pronounced “p-hat.” (The symbol(cid:98)
is often used in statistics to mean “estimate of.”)

Assign to each voter in the population a value of Y, 1 if he/she plans to vote for A, 0 otherwise.
Let Yi be the value of Y for the ith person in our sample. Then

(cid:98)p = Y

(16.5)

where Y is the sample mean among the Yi, and p is the population mean of Y.

So we are really working with means after all, and thus in order to get a conﬁdence interval for p
from (cid:98)p, we can use (16.4)! We have that an approximate 95% conﬁdence interval for p is

(cid:0)

(cid:98)p − 1.96s/

√

n, (cid:98)p + 1.96s/

√

n(cid:1)

(16.6)

where as before s2 is the sample variance among the Yi, deﬁned in 15.23.

But there’s more, because we can exploit the fact that in this special case, each Yi is either 1 or 0,
in order to save ourselves a bit of computation, as follows:

Recalling the convenient form of s2, (15.24), we have

278

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

s2 =

=

1
n

1
n

n
(cid:88)

i=1
n
(cid:88)

i=1

Y 2
i − Y

2

2

Yi − Y

2

= Y − Y
= (cid:98)p − (cid:98)p2

Then (16.6) simpliﬁes to

(cid:16)

(cid:98)p − 1.96(cid:112)

(cid:98)p(1 − (cid:98)p)/n, (cid:98)p + 1.96(cid:112)

(cid:98)p(1 − (cid:98)p)/n

(16.7)

(16.8)

(16.9)

(16.10)

(16.11)

(cid:17)

16.4.2 That n vs. n-1 Thing Again

Recall Section 15.3.1.3, in which it was noted that this book’s deﬁnition of the sample variance,
(15.23), is a little at odds with the the way most books deﬁne it, (15.26). The above derivation
sheds a bit more light on this topic.

In the way I’ve deﬁned things here, I was consistent: I divided by n both in (15.23) and in (16.7).
Yet most books divide by n-1 in the former case but by n in the latter case! Their version of (16.11)
is exactly the same as mine, yet they use a diﬀerent s in (16.4)—even though they too observe that
the proportions case is just a special case of estimating means (as in (16.5)). So, another reason to
divide by n in (15.23) is to be consistent.

Again, the diﬀerence is usually minuscule anyway, but conceptually it’s important to understand.
As noted earlier, the n-1 divisor is really just a historical accident.

16.4.3 Simulation Example Again

In our bus example above, suppose we also want our simulation to print out the (estimated)
probability that one must wait longer than 6.4 minutes. As before, we’d also like a margin of error
for the output.

We incorporate (16.11) into our program:

1

2

3

doexpt <- function(opt) {
lastarrival <- 0.0
while (lastarrival < opt)

16.4. CONFIDENCE INTERVALS FOR PROPORTIONS

279

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

lastarrival <- lastarrival + rexp(1,0.1)

return(lastarrival-opt)

}

observationpt <- 240
nreps <- 1000
waits <- vector(length=nreps)
for (rep in 1:nreps) waits[rep] <- doexpt(observationpt)
wbar <- mean(waits)
cat("approx. mean wait =",wbar,"\n")
s2 <- (mean(waits^2) - mean(wbar)^2)
s <- sqrt(s2)
radius <- 1.96*s/sqrt(nreps)
cat("approx. CI for EW =",wbar-radius,"to",wbar+radius,"\n")
prop <- length(waits[waits > 6.4]) / nreps
s2 <- prop*(1-prop)
s <- sqrt(s2)
radius <- 1.96*s/sqrt(nreps)
cat("approx. P(W > 6.4) =",prop,", with a margin of error of",radius,"\n")

When I ran this, the value printed out for (cid:98)p was 0.54, with a margin of error of 0.03, thus an
interval of (0.51,0.57). We would say, “We don’t know the exact value of P (W > 6.4), so we ran a
simulation. The latter estimates this probability to be 0.54, with a 95% margin of error of 0.03.”

16.4.4 Example: Davis Weights

Note again that this uses the same principles as our Davis weights example. Suppose we were
interested in estimating the proportion of adults in Davis who weigh more than 150 pounds. Sup-
pose that proportion is 0.45 in our sample of 1000 people. This would be our estimate (cid:98)p for the
population proportion p, and an approximate 95% conﬁdence interval (16.11) for the population
proportion would be (0.42,0.48). We would then say, “We are 95% conﬁdent that the true popula-
tion proportion p of people who weigh over 150 pounds is between 0.42 and 0.48.”

Note also that although we’ve used the word proportion in the Davis weights example instead of
probability, they are the same. If I choose an adult at random from the population, the probability
that his/her weight is more than 150 is equal to the proportion of adults in the population who
have weights of more than 150.

And the same principles are used in opinion polls during presidential elections. Here p is the
population proportion of people who plan to vote for the given candidate. This is an unknown
quantity, which is exactly the point of polling a sample of people—to estimate that unknown
quantity p. Our estimate is (cid:98)p, the proportion of people in our sample who plan to vote for the
given candidate, and n is the number of people that we poll. We again use (16.11).

280

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

16.4.5 Interpretation

The same interpretation holds as before. Consider the examples in the last section:

• If each of you and 99 friends were to run the R program at the beginning of Section 16.4.4,
you 100 people would get 100 conﬁdence intervals for P (W > 6.4). About 95 of you would
have intervals that do contain that number.

• If each of you and 99 friends were to sample 1000 people in Davis and come up with conﬁdence
intervals for the true population proportion of people who weight more than 150 pounds, about
95 of you would have intervals that do contain that true population proportion.

• If each of you and 99 friends were to sample 1200 people in an election campaign, to estimate
the true population proportion of people who will vote for candidate X, about 95 of you will
have intervals that do contain this population proportion.

Of course, this is just a “thought experiment,” whose goal is to understand what the term “95%
conﬁdent” really means. In practice, we have just one sample and thus compute just one interval.
But we say that the interval we compute has a 95% chance of containing the population value,
since 95% of all intervals will contain it.

16.4.6 (Non-)Eﬀect of the Population Size

Note that in both the Davis and election examples, it doesn’t matter what the size of the population
is. The approximate distribution of (cid:98)p is N(p,p(1-p)/n), so the accuracy of (cid:98)p, depends only on p
and n. So when people ask, “How a presidential election poll can get by with sampling only 1200
people, when there are more than 100,000,000 voters in the U.S.?” now you know the answer.
(We’ll discuss the question “Why 1200?” below.)

Another way to see this is to think of a situation in which we wish to estimate the probability p
of heads for a certain coin. We toss the coin n times, and use (cid:98)p as our estimate of p. Here our
“population”—the population of all coin tosses—is inﬁnite, yet it is still the case that 1200 tosses
would be enough to get a good estimate of p.

16.4.7 Inferring the Number Polled

A news report tells us that in a poll, 54% of those polled supported Candidate A, with a 2.2%
margin of error. Assuming that the methods here were used, with a 95% level of conﬁdence, let’s

16.5. GENERAL FORMATION OF CONFIDENCE INTERVALS FROM APPROXIMATELY NORMAL ESTIMATORS281

ﬁnd the approximate number polled.

0.022 = 1.96 × (cid:112)0.54 · 0.46/n

(16.12)

Solving, we ﬁnd that n is approximately 1972.

16.4.8 Planning Ahead

Now, why do the pollsters often sample 1200 people?
First, note that the maximum possible value of (cid:98)p(1 − (cid:98)p) is 0.25.3 Then the pollsters know that
1200, or about 3%, even before
their margin of error with n = 1200 will be at most 1.96 × 0.5/
they poll anyone. They consider 3% to be suﬃciently accurate for their purposes, so 1200 is the n
they choose.

√

16.5 General Formation of Conﬁdence Intervals from Approxi-

mately Normal Estimators

We would now like to move on to constructing conﬁdence intervals for other settings than the case
handled so far, estimation of a single population mean or proportion.

16.5.1 The Notion of a Standard Error

Suppose we are estimating some population quantity θ based on sample data Y1, ..., Yn. So far, our
only examples have had θ as a population mean µ, a population proportion p. But we’ll see other
examples as things unfold in this and subsequent chapters.

Consider an estimate for θ, (cid:98)θ, and suppose that the estimator is composed of some sum for which
the Central Limit Theorem applies,4, so that the approximate distribution of (cid:98)θ is normal with mean
θ and some variance.

Ponder this sequence of points:

• (cid:98)θ is a random variable.

• Thus (cid:98)θ has a variance.
3Use calculus to ﬁnd the maximum value of f(x) = x(1-x).
4Using more advanced tools (Section ??), one can show approximate normality even in many nonlinear cases.

282

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

• Thus (cid:98)θ has a standard deviation η.

• Unfortunately, η is an unknown population quantity.

• But we may be able to estimate η from our sample data. Call that estimate (cid:98)η.
• We refer to (cid:98)η as the standard error of (cid:98)θ, or s.e.(cid:98)θ.

Back in Section 16.2.1, we found a standard error for W , the sample mean, using the following
train of thought:

• V ar(W ) = σ2
n
• (cid:100)V ar(W ) = s2
n
• s.e.(W ) = s√
n

In many cases, deriving a standard error is more involved than the above, But the point is this:

Suppose (cid:98)θ is a sample-based estimator of a population quantity θ, and that, due to
being composed of sums or some other reason, (cid:98)θ is approximately normally distributed
with mean θ, and some (possibly unknown) variance. Then the quantity

(cid:98)θ − θ
s.e.((cid:98)θ)

(16.13)

has an approximate N(0,1) distribution.

16.5.2 Forming General Conﬁdence Intervals

That means we can mimic the derivation that led to (16.4). As with (16.1), write

(cid:32)

0.95 ≈ P

−1.96 <

(cid:33)

< 1.96

(cid:98)θ − θ
s.e.((cid:98)θ)

(16.14)

After going through steps analogous to those following (16.1), we ﬁnd that an approximate 95%
conﬁdence interval for θ is

(cid:98)θ ± 1.96 · s.e.((cid:98)θ)

(16.15)

16.5. GENERAL FORMATION OF CONFIDENCE INTERVALS FROM APPROXIMATELY NORMAL ESTIMATORS283

In other words, the margin of error is 1.96 s.e.((cid:98)θ).

The standard error of the estimate is one of the most commonly-used quantities in
statistical applications. You will encounter it frequently in the output of R, for in-
stance, and in the subsequent portions of this book. Make sure you understand what
it means and how it is used.
And note again that (cid:112)

(cid:98)p(1 − (cid:98)p)/n is the standard error of (cid:98)p.

16.5.3 Standard Errors of Combined Estimators

Here is further chance to exercise your skills in the mailing tubes regarding variance.

Suppose we have two population values to estimate, ω and γ, and that we are also interested in the
quantity ω + 2γ. We’ll estimate the latter with ˆω + 2ˆγ. Suppose the standard errors of ˆω and ˆγ turn
out to be 3.2 and 8.8, respectively, and that the two estimators are independent and approximately
normal.5 Let’s ﬁnd the standard error of ˆω + 2ˆγ.

We know from the material surrounding (8.16) that ˆω+2ˆγ has an approximately normal distribution
with variance

Thus the standard error of ˆω + 2ˆγ is

V ar(ˆω) + 22V ar(ˆγ)

(cid:112)

·3.22 + 22 · 8.82

(16.16)

(16.17)

Now that we know the standard error of ˆω + 2ˆγ, we can use it in (16.15). We add and subtract
1.96 times (16.17) to ˆω + 2ˆγ, and that is our interval.

In general, for constants a and b, an approximate 95% conﬁdence interval for the population
quantity aω + bγ is

aˆω + bˆγ ± 1.96

(cid:112)

a2s.e.2(ˆω) + b2s.e.2(ˆγ)

(16.18)

We can go even further. If ˆω and ˆγ are not independent but have known covariance, we can use
the methods of Chapter 12 to obtain a standard error for any linear combination of these two
estimators.

5Technically, the term standard error is only used for approximately normal estimators anyway.

284

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

16.6 Conﬁdence Intervals for Diﬀerences of Means or Proportions

16.6.1 Independent Samples

Suppose in our sampling of people in Davis we are mainly interested in the diﬀerence in weights
between men and women. Let X and n1 denote the sample mean and sample size for men, and let
Y and n2 for the women. Denote the population means and variances by µi and σ2
i , i = 1,2. We
wish to ﬁnd a conﬁdence interval for µ1 − µ2. The natural estimator for that quantity is X − Y .

So, how can we form a conﬁdence interval for µ1 − µ2 using X − Y ? Since the latter quantity is
composed of sums, we can use (16.15) and (16.18). Here:

• a = 1, b = −1

• ω = µ1, γ = µ2

• ˆω = X, ˆγ = Y

But we know from before that s.e.(X = s1/

√

n, where s2

1 is the sample variance for the men,

s2
1 =

1
n1

n1(cid:88)

(Xi − X)2

i=1

and similarly for Y and the women. So, we have

s.e.(X − Y ) =

(cid:115)

s2
1
n1

+

s2
2
n2

Thus (16.15) tells us that an approximate 95% conﬁdence interval for µ1 − µ2 is



(cid:115)

X − Y − 1.96

s2
1
n1

+

s2
2
n2

(cid:115)

, X − Y + 1.96

s2
1
n1

+





s2
2
n2

(16.19)

(16.20)

(16.21)

What about conﬁdence intervals for the diﬀerence in two population proportions p1 − p2? Recalling
that in Section 16.4 we noted that proportions are special cases of means, we see that ﬁnding a
conﬁdence interval for the diﬀerence in two proportions is covered by (16.21). Here

• X reduces to (cid:98)p1

16.6. CONFIDENCE INTERVALS FOR DIFFERENCES OF MEANS OR PROPORTIONS 285

• Y reduces to (cid:98)p2

• s2

1 reduces to (cid:98)p1(1 − (cid:98)p1)

• s2

2 reduces to (cid:98)p2(1 − (cid:98)p2)

So, (16.21) reduces to

where the radius R is

(cid:98)p1 − (cid:98)p2 ± R

(cid:115)

1.96

(cid:98)p1(1 − (cid:98)p1)
n1

+ (cid:98)p2(1 − (cid:98)p2)
n2

(16.22)

(16.23)

16.6.2 Example: Network Security Application

In a network security application, C. Mano et al6 compare round-trip travel time for packets involved
in the same application in certain wired and wireless networks. The data was as follows:

sample
wired
wireless

sample mean sample s.d.
2.000
11.520

6.299
9.939

sample size
436
344

We had observed quite a diﬀerence, 11.52 versus 2.00, but could it be due to sampling variation?
Maybe we have unusual samples? This calls for a conﬁdence interval!

Then a 95% conﬁdence interval for the diﬀerence between wireless and wired networks is

11.520 − 2.000 ± 1.96

(cid:114)

9.9392
344

+

6.2992
436

= 9.52 ± 1.22

(16.24)

So you can see that there is a big diﬀerence between the two networks, even after allowing for
sampling variation.

6RIPPS: Rogue Identifying Packet Payload Slicer Detecting Unauthorized Wireless Hosts Through Network Traﬃc
Conditioning, C. Mano and a ton of other authors, ACM Transactions on Information Systems and Security,
May 2007.

286

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

16.6.3 Dependent Samples

Note carefully, though, that a key point above was the independence of the two samples. By
contrast, suppose we wish, for instance, to ﬁnd a conﬁdence interval for ν1 − ν2, the diﬀerence in
mean heights in Davis of 15-year-old and 10-year-old children, and suppose our data consist of pairs
of height measurements at the two ages on the same children. In other words, we have a sample of
n children, and for the ith child we have his/her height Ui at age 15 and Vi at age 10. Let U and
V denote the sample means.

The problem is that the two sample means are not independent. If a child is taller than his/her
peers at age 15, he/she was probably taller than them when they were all age 10. In other words,
for each i, Vi and Ui are positively correlated, and thus the same is true for V and U . Thus we
cannot use (16.21).

As always, it is instructive to consider this in “notebook” terms. Suppose on one particular sample
at age 10—one line of the notebook—we just happen to have a lot of big kids. Then V is large.
Well, if we look at the same kids later at age 15, they’re liable to be bigger than the average
15-year-old too. In other words, among the notebook lines in which V is large, many of them will
have U large too.

Since U is approximately normally distributed with mean ν1, about half of the notebook lines will
have U > ν1. Similarly, about half of the notebook lines will have V > ν2. But the nonindependence
will be reﬂected in MORE than one-fourth of the lines having both U > ν1 and V > ν2. (If the
two sample means were 100% correlated, that fraction would be 1.0.)

Contrast that with a sample scheme in which we sample some 10-year-olds and some 15-year-olds,
say at the same time. Now there are diﬀerent kids in each of the two samples. So, if by happenstance
we get some big kids in the ﬁrst sample, that has no impact on which kids we get in the second
sample. In other words, V and U will be independent. In this case, one-fourth of the lines will
have both U > ν1 and V > ν2.

So, we cannot get a conﬁdence interval for ν1 − ν2 from (16.21), since the latter assumes that the
two sample means are independent. What to do?

The key to the resolution of this problem is that the random variables Ti = Vi − Ui, i = 1,2,...,n are
still independent. Thus we can use (16.4) on these values, so that our approximate 95% conﬁdence
interval is

(T − 1.96

s
√
n

, T + 1.96

s
√
n

)

(16.25)

where T and s2 are the sample mean and sample variance of the Ti.

A common situation in which we have dependent samples is that in which we are comparing two

16.6. CONFIDENCE INTERVALS FOR DIFFERENCES OF MEANS OR PROPORTIONS 287

dependent proportions. Suppose for example that there are three candidates running for a political
oﬃce, A, B and C. We poll 1,000 voters and ask whom they plan to vote for. Let pA, pB and pC
be the three population proportions of people planning to vote for the various candidates, and let
(cid:98)pA, (cid:98)pB and (cid:98)pC be the corresponding sample proportions.
Suppose we wish to form a conﬁdence interval for pA − pB. Clearly, the two sample proportions are
not independent random variables, since for instance if (cid:98)pA = 1 then we know for sure that (cid:98)pB is 0.
Or to put it another way, deﬁne the indicator variables Ui and Vi as above, with for example Ui
being 1 or 0, according to whether the ith person in our sample plans to vote for A or not, with
Vi being deﬁned similarly for B. Since Ui and Vi are “measurements” on the same person, they are
not independent, and thus (cid:98)pA and (cid:98)pB are not independent either.
Note by the way that while the two sample means in our kids’ height example above were positively
correlated, in this voter poll example, the two sample proportions are negatively correlated.

So, we cannot form a conﬁdence interval for pA − pB by using (16.22). What can we do instead?

We’ll use the fact that the vector (NA, NB, NC)T has a multinomial distribution, where NA, NB and
NC denote the numbers of people in our sample who state they will vote for the various candidates
(so that for instance (cid:98)pA = NA/1000).
Now to compute V ar((cid:98)pA − (cid:98)pB), we make use of (12.10):

V ar((cid:98)pA − (cid:98)pB) = V ar((cid:98)pA) + V ar((cid:98)pB) − 2Cov((cid:98)pA, (cid:98)pB)

(16.26)

Or, we could have taken a matrix approach, using (12.54) with A equal to the row vector (1,-1,0).

So, using (13.102), the standard error of (cid:98)pA − (cid:98)pB is

(cid:112)0.001(cid:98)pA(1 − (cid:98)pA) + 0.001(cid:98)pB(1 − (cid:98)pB) + 0.002(cid:98)pA (cid:98)pB

(16.27)

16.6.4 Example: Machine Classiﬁcation of Forest Covers

Remote sensing is machine classiﬁcation of type from variables observed aerially, typically by satel-
lite. The application we’ll consider here involves forest cover type for a given location; there are
seven diﬀerent types. (See Blackard, Jock A. and Denis J. Dean, 2000, “Comparative Accuracies
of Artiﬁcial Neural Networks and Discriminant Analysis in Predicting Forest Cover Types from
Cartographic Variables,” Computers and Electronics in Agriculture, 24(3):131-151.) Direct obser-
vation of the cover type is either too expensive or may suﬀer from land access permission issues.
So, we wish to guess cover type from other variables that we can more easily obtain.

288

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

One of the variables was the amount of hillside shade at noon, which we’ll call HS12. Here’s
our goal: Let µ1 and µ2 be the population mean HS12 among sites having cover types 1 and 2,
respectively. If µ1 − µ2 is large, then HS12 would be a good predictor of whether the cover type is
1 or 2.

So, we wish to estimate µ1 − µ2 from our data, in which we do know cover type. There were over
50,000 observations, but for simplicity we’ll just use the ﬁrst 1,000 here. Let’s ﬁnd an approximate
95% conﬁdence interval for µ1 − µ2. The two sample means were 223.8 and 226.3, with s values of
15.3 and 14.3, and the sample sizes were 226 and 585.

Using (16.21), we have that the interval is

223.8 − 226.3 ± 1.96

(cid:114)

15.32
226

+

14.32
585

= −2.5 ± 2.3 = (−4.8, −0.3)

(16.28)

Given that HS12 values are in the 200 range (see the sample means), this diﬀerence between them
actually is not very large. This is a great illustration of an important principle, it will turn out in
Section 17.11.

As another illustration of conﬁdence intervals, let’s ﬁnd one for the diﬀerence in population pro-
portions of sites that have cover types 1 and 2. Our sample estimate is

(cid:98)p1 − (cid:98)p2 = 0.226 − 0.585 = −0.359

(16.29)

The standard error of this quantity, from (16.27), is

√

0.001 · 0.226 · 0.7740.001 · 0.585 · 0.415 + 002 · 0.226 · 0.585 = 0.019

(16.30)

That gives us a conﬁdence interval of

−0.359 ± 1.96 · 0.019 = (−0.397, −0.321)

(16.31)

16.7 And What About the Student-t Distribution?

Far better an approximate answer to the right question, which is often vague, than an exact answer
to the wrong question, which can always be made precise—John Tukey, pioneering statistician at
Bell Labs

16.7. AND WHAT ABOUT THE STUDENT-T DISTRIBUTION?

289

Another thing we are not doing here is to use the Student t-distribution. That is the name of
the distribution of the quantity

T =

W − µ
√

˜s/

n − 1

(16.32)

where ˜s2 is the version of the sample variance in which we divide by n-1 instead of by n, i.e. (15.24).

Note carefully that we are assuming that the Wi themselves—not just W —have a normal dis-
tribution.
In other words, if we are studying human weight, say, then the assumption is that
weight follows an exact bell-shaped curve. The exact distribution of T is called the Student t-
distribution with n-1 degrees of freedom. These distributions thus form a one-parameter
family, with the degrees of freedom being the parameter.
The general deﬁnition of the Student-t family is distribution of ratios U/(cid:112)V /k, where

• U has a N(0,1) distribution

• V has a chi-squared distribution with k degrees of freedom

• U and V are independent

It can be shown that in (16.32), if the sampled population has a normal distribution, then (W −µ)/σ
and ˜s2/σ2 actually do satisfy the above conditions on U and V, respectively, with k = n-1. (If we are
forming a conﬁdence interval for the diﬀerence of two means, the calculation of degrees of freedom
becomes more complicated, but it is not important here.)

This distribution has been tabulated. In R, for instance, the functions dt(), pt() and so on play
the same roles as dnorm(), pnorm() etc. do for the normal family. The call qt(0.975,9) returns
2.26. This enables us to get a conﬁdence interval for µ from a sample of size 10, at EXACTLY a
95% conﬁdence level, rather than being at an APPROXIMATE 95% level as we have had here, as
follows.

We start with (16.1), replacing 1.96 by 2.26, ( ¯W − µ)/(σ/
algebra, we ﬁnd the following conﬁdence interval for µ:

√

n) by T, and ≈ by =. Doing the same

(W − 2.26

˜s
√
10

, W + 2.26

˜s
√
10

)

(16.33)

Of course, for general n, replace 2.26 by t0.975,n−1, the 0.975 quantile of the t-distribution with n-1
degrees of freedom. The distribution is tabulated by the R functions dt(), pt() and so on.

I do not use the t-distribution here because:

290

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

• It depends on the parent population having an exact normal distribution, which is never really
true. In the Davis case, for instance, people’s weights are approximately normally distributed,
but deﬁnitely not exactly so. For that to be exactly the case, some people would have to have
weights of say, a billion pounds, or negative weights, since any normal distribution takes on
all values from −∞ to ∞.

• For large n, the diﬀerence between the t-distribution and N(0,1) is negligible anyway. That
wasn’t true in the case n = 10 above, where our conﬁdence interval multiplied the standard
error by 2.26 instead of 1.96 as we’d seen earlier. But for n = 50, the 2.26 already shrinks to
2.01, and for n = 100, it is 1.98.

16.8 R Computation

The R function t.test() forms conﬁdence intervals for a single mean or for the diﬀerence of two
means. In the latter case, the two samples must be independent; otherwise, do the single-mean CI
on diﬀerences, as in Section 16.6.3.

This function uses the Student-t distribution, rather than the normal, but as discussed in Section
16.7, the diﬀerence is negligible except in small samples.

Thus you can conveniently use t.test() to form a conﬁdence interval for a single mean, instead of
computing (16.4) yourself (or writing the R code yourself).

It’s slightly more complicated in the case of forming a conﬁdence interval for the diﬀerence of two
means. The t.test() function will do that for you too, but will make the assmption that we have
1 = σ2
σ2
2 in Section 16.6.1. Unless you believe there is a huge diﬀerence between the two population
variances, this approximation is not bad.

16.9 Example: Pro Baseball Data

The SOCR data repository at the UCLA Statistics Department includes a data set on major league
baseball players, at http://wiki.stat.ucla.edu/socr/index.php/SOCR_Data_MLB_HeightsWeights.
There are 1035 players in the sample, with the variables Name, Team, Position, Height, Weight
and Age. I downloaded it and placed it into a ﬁle Baseball.dat

16.9.1 R Code

First we read in the data:

16.9. EXAMPLE: PRO BASEBALL DATA

291

> p l a y e r s <− read . table ( ” B a s e b a l l . dat ” , h e a d e r=T)
E r r o r
sep , dec , quote ,
:

i n scan ( f i l e , what , nmax ,

l i n e 641 d i d not have 6 e l e m e n t s

s k i p , n l i n e s , na . s t r i n g s ,

Oops! The entry for one player, Kirk Saarloos, did not have a weight ﬁgure. So I edited the ﬁle
by hand, placing the string “NA” there for weight; this is R’s code for missing data. I then tried
again:

> p l a y e r s <− read . table ( ” B a s e b a l l . dat ” , h e a d e r=T)
> head ( p l a y e r s )

P o s i t i o n Height Weight

Name Team
Adam Donachie BAL
1
2
Paul Bako BAL
3 Ramon Hernandez BAL
4
5
6

Catcher
Catcher
Catcher
Kevin M i l l a r BAL F i r s t Baseman
C h r i s Gomez BAL F i r s t Baseman
Brian Roberts BAL Second Baseman

Age
180 2 2 . 9 9
215 3 4 . 6 9
210 3 0 . 7 8
210 3 5 . 4 3
188 3 5 . 7 1
176 2 9 . 3 9

74
74
72
72
73
69

I read in the ﬁle Baseball.dat, whose ﬁrst line consisted of a header giving the names of the
variables. I assigned the result to players, whose type will be that of an R data frame. I then
called R’s head() function, to take a look at the results to make sure things are OK.

We could then query various items in the object players, say the mean weight (not conditioned
on height), via players [,5] or players$Weight.

16.9.2 Analysis

Let’s ﬁnd an approximate 95% conﬁdence interval for the population mean weight of catchers.7

> c a t c h <− p l a y e r s [ p l a y e r s $ P o s i t i o n == ” Catcher ” , ]
> t . t e s t ( c a t c h $Weight )

One Sample t−t e s t

c a t c h $Weight

data :
t = 1 1 3 . 1 4 6 7 , df = 7 5 , p−v a l u e < 2 . 2 e −16
a l t e r n a t i v e h y p o t h e s i s :
95 p e r c e n t c o n f i d e n c e

i n t e r v a l :

t r u e mean i s not equal t o 0

7Note that this is an observational study, as discussed in Section 15.4, with attendant care needed for the conclu-

sions we derive from this data.

292

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

2 0 0 . 7 3 1 5 2 0 7 . 9 2 6 4

sample e s t i m a t e s :
mean o f x
2 0 4 . 3 2 8 9

Our CI is (200.7,207.9).

(There is material in the above output on signiﬁcance testing, which we will cover in the next
chapter.)

How about a comparison in population mean weights between catchers and ﬁrst basemen?

> f i r s t b <− p l a y e r s [ p l a y e r s $ P o s i t i o n == ” F i r s t Baseman” , ]
> t . t e s t ( c a t c h $Weight , f i r s t b $Weight )

Welch Two Sample t−t e s t

data :
c a t c h $Weight and f i r s t b $Weight
t = −2.7985 , df = 1 0 2 . 6 2 6 , p−v a l u e = 0 . 0 0 6 1 3 3
a l t e r n a t i v e h y p o t h e s i s :
95 p e r c e n t c o n f i d e n c e

i n t e r v a l :

t r u e d i f f e r e n c e i n means i s not equal t o 0

−15.002763 −2.557524

sample e s t i m a t e s :
mean o f x mean o f y

We might be interested in inference concerning the population proportion of catchers older than
32:

> old <− ( c a t c h $Age > 3 2 )
> head ( old )
[ 1 ] FALSE TRUE FALSE FALSE FALSE FALSE
> old <− as . integer ( old )
> head ( old )
[ 1 ] 0 1 0 0 0 0
> t . t e s t ( old )

One Sample t−t e s t

old

data :
t = 5 . 7 0 5 , df = 7 5 , p−v a l u e = 2 . 1 8 9 e −07
a l t e r n a t i v e h y p o t h e s i s :
95 p e r c e n t c o n f i d e n c e
0 . 1 9 6 9 5 7 3 0 . 4 0 8 3 0 5 8

i n t e r v a l :

t r u e mean i s not equal t o 0

16.10. EXAMPLE: UCI BANK MARKETING DATASET

293

sample e s t i m a t e s :
mean o f x
0 . 3 0 2 6 3 1 6

Note that the intervals, especially the last one, are rather wide. We just don’t have enough catchers
to get much accuracy. How many are there?8

> nrow( c a t c h )
[ 1 ] 76

16.10 Example: UCI Bank Marketing Dataset

This data set was obtained from the UC Irvine Machine Learning Data Repository, http://
archive.ics.uci.edu/ml/about.html. A bank in Portugal had developed a new type of ac-
count, and they were interested in studying what types of customers would be more likely to switch
to the new account.

> bank <− read . table ( ”bank− f u l l . c s v ” , h e a d e r=T, s e p=” ; ” )
> head ( bank )

management m a rr i ed
t e c h n i c i a n

age
t e r t i a r y
58
s i n g l e s e c o n d a r y
44
33 e n t r e p r e n e u r m ar r ie d s e c o n d a r y
unknown
47
unknown
33
t e r t i a r y
35

j o b m a r i t a l e d u c a t i o n default b a l a n c e h o u s i n g l o a n
no
no
y e s
no
no
no

blue−c o l l a r m a r ri e d
s i n g l e
management m a rr i ed

y e s
y e s
y e s
y e s
no
y e s

unknown

day
5
5
5
5
5
5

month d u r a t i o n campaign pdays p r e v i o u s poutcome

may
may
may
may
may
may

261
151
76
92
198
139

1
1
1
1
1
1

−1
−1
−1
−1
−1
−1

0
0
0
0
0
0

no
no
no
no
no
no

2143
29
2
1506
1
231
y
unknown no
unknown no
unknown no
unknown no
unknown no
unknown no

1
2
3
4
5
6

1
2
3
4
5
6

(The variable contact has been omitted here, to ﬁt the display on the page.)

There are many variables here, explained in more detail at the UCI site. We’ll come back to this
example, but let’s do one quick conﬁdence interval. Here we will compare the success rates of the

8In order to do any of this, we are tacitly assuming that our players are a sample of some general population of
players, say, past, present and future., even if we have all the present ones. This is very common in applied statistical
analysis.

294

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

marketing campaign for married and unmarried people:

> marrd <− bank [ bank$ m a r i t a l == ” m ar r ie d ” , ]
> unmarrd <− bank [ bank$ m a r i t a l
> t . t e s t ( marrd$ s u c c e s s , unmarrd$ s u c c e s s )

!= ” ma r ri e d ” , ]

Welch Two Sample t−t e s t

data : marrd$ s u c c e s s and unmarrd$ s u c c e s s
t = −12.471 , df = 3 4 6 7 6 . 2 6 , p−v a l u e < 2 . 2 e −16
a l t e r n a t i v e h y p o t h e s i s :
95 p e r c e n t c o n f i d e n c e

i n t e r v a l :

t r u e d i f f e r e n c e i n means i s not equal t o 0

−0.04578514 −0.03334804

sample e s t i m a t e s :
mean o f x mean o f y
0 . 1 0 1 2 3 4 7 0 . 1 4 0 8 0 1 2

So, we are 95% conﬁdent that the population success rate is between 3.3% and 4.6% less for married
people.

Note by the way that there are more than 46,000 people in this sample. So, the Student-t and
N(0,1) distributions are now indistinguishable.

16.11 Example: Amazon Links

This example involves the Amazon product co-purchasing network, March 2 2003. The data set
is large but simple. It stores a directed graph of what links to what: If a record show i then j, it
means that i is often co-purchased with j (though not necessarily vice versa). Let’s ﬁnd a conﬁdence
interval for the mean number of inlinks, i.e. links into a node.

Actually, even the R manipulations are not so trivial, so here is the complete code (http://snap.
stanford.edu/data/amazon0302.html):

f o r c o n v e n i e n c e

1 mzn <− read . table ( ” amazon0302 . t x t ” , h e a d e r=F)
2 # c u t down t h e d a t a s e t
3 mzn1000 <− mzn [ mzn [ 1 . ] <= 1000 & mzn [ , 2 ] <= 1 0 0 0 , ]
4 # make an R l i s t , one e l e m e n t p e r v a l u e o f
5 d e g r e e s 1 0 0 0 <− s p l i t ( mzn1000 , mzn1000 [ , 2 ] )
6 # by f i n d i n g t h e number o f rows i n each matrix , we g e t
7 # i n l i n k s
8

i n d e g r e e s 1 0 0 0 <− sapply ( d e g r e e s 1 0 0 0 , nrow)

j

t h e numbers o f

16.12. EXAMPLE: MASTER’S DEGREES IN CS/EE

295

Now run t.test():

> t . t e s t ( i n d e g r e e s 1 0 0 0 )

One Sample t−t e s t

i n d e g r e e s 1 0 0 0

data :
t = 3 5 . 0 2 7 9 , df = 1 0 0 0 , p−v a l u e < 2 . 2 e −16
a l t e r n a t i v e h y p o t h e s i s :
95 p e r c e n t c o n f i d e n c e

i n t e r v a l :

t r u e mean i s not equal t o 0

3 . 7 2 8 7 5 9 4 . 1 7 1 3 4 0

sample e s t i m a t e s :
mean o f x
3 . 9 5 0 0 5

So, in our sample data, the mean number of inlinks was 3.95, and we are 95% conﬁdent that the
true population mean is between 3.73 and 4.17.

16.12 Example: Master’s Degrees in CS/EE

In an analysis of the National Survey of College Graduates, I looked at workers in CS or EE, who
have CS or EE degrees.9 I had them in R data frames named cs and ee, each of which had an
indicator variable ms signifying that the worker has a Master’s degree (but not a PhD). Let’s see
the diﬀerence between CS and EE on this variable:

> t . t e s t ( c s $ms , e e $ms)

Welch Two Sample t−t e s t

c s $ms and e e $ms

data :
t = 2 . 4 8 9 5 , df = 1 8 7 8 . 1 0 8 , p−v a l u e = 0 . 0 1 2 8 8
a l t e r n a t i v e h y p o t h e s i s :
95 p e r c e n t c o n f i d e n c e

i n t e r v a l :

t r u e d i f f e r e n c e i n means i s not equal t o 0

0 . 0 1 0 7 3 5 8 0 0 . 0 9 0 4 5 6 8 9

sample e s t i m a t e s :
mean o f x mean o f y
0 . 3 5 6 0 5 5 1 0 . 3 0 5 4 5 8 8

9There were various other restrictions, which I will not go into here.

296

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

So, in our sample, 35.6% and 30.5% of the two groups had Master’s degrees, and we are 95%
conﬁdent that the true population diﬀerence in proportions of Master’s degrees in the two groups
is between 0.01 and 0.09.

16.13 Other Conﬁdence Levels

We have been using 95% as our conﬁdence level. This is common, but of course not unique. We
can for instance use 90%, which gives us a narrower interval (in (16.4), we multiply by 1.65 instead
of by 1.96. (The reader should check this, using the qnorm() function.) Narrower is better, of
course, but it comes at the expense of lower conﬁdence.

A conﬁdence interval’s error rate is usually denoted by 1−α, so a 95% conﬁdence level has α = 0.05.

16.14 One More Time: Why Do We Use Conﬁdence Intervals?

After all the variations on a theme in the very long Section 16.1, it is easy to lose sight of the goal,
so let’s review:

Almost everyone is familiar with the term “margin of error,” given in every TV news report during
elections. The report will say something like, “In our poll, 62% stated that they plan to vote for Ms.
X. The margin of error is 3%.” Those two numbers, 62% and 3%, form the essence of conﬁdence
intervals:

• The 62% ﬁgure is our estimate of p, the true population fraction of people who plan to vote

for Ms. X.

• Recognizing that that 62% ﬁgure is only a sample estimate of p, we wish to have a measure
of how accurate the ﬁgure is—our margin of error. Though the poll reports don’t say this,
what they are actually saying is that we are 95% sure that the true population value p is in
the range 0.62 ± 0.03.

So, a conﬁdence interval is nothing more than the concept of the a ± b range that we are so familiar
with.

Exercises

1. Consider Equation (16.3). In each of the entries in the table below, ﬁll in either R for random,
or NR for nonrandom:

16.14. ONE MORE TIME: WHY DO WE USE CONFIDENCE INTERVALS?

quantity R or NR?

297

W
s
µ
n

2. Consider ˆp, the estimator of a population proportion p, based on a sample of size n. Give the
expression for the standard error of ˆp.

3. Suppose we take a simple random sample of size 2 from a population consisting of just three
values, 66, 67 and 69. Let X denote the resulting sample mean. Find pX (67.5).

4. Suppose we have a random sample W1, ..., Wn, and we wish to estimate the population mean µ,
as usual. But we decide to place double weight on W1, so our estimator for µ is

U =

2W1 + W2 + ... + Wn
n + 1

(16.34)

Find E(U) and Var(U) in terms of µ and the population variance σ2.

5. Suppose a random sample of size n is drawn from a population in which, unknown to the
analyst, X actually has an exponential distribution with mean 10. Suppose the analyst forms an
approximate 95% conﬁdence interval for the mean, using (16.4). Use R simulation to estimate the
true conﬁdence level, for n = 10, 25, 100 and 500.

6. Suppose we draw a sample of size 2 from a population in which X has the values 10, 15 and 12.
Find pX , ﬁrst assuming sampling with replacement, then assuming sampling without replacement.

7. We ask 100 randomly sampled programmers whether C++ is their favorite language, and
12 answer yes. Give a numerical expression for an approximate 95% conﬁdence interval for the
population fraction of programmers who have C++ as their favorite language.

8.
In Equation (16.4), suppose 1.96 is replaced by 1.88 in both instances. Then of course the
conﬁdence level will be smaller than 95%. Give a call to an R function (not a simulation), that will
ﬁnd the new conﬁdence level.

9. Candidates A, B and C are vying for election. Let p1, p2 and p3 denote the fractions of people
planning to vote for them. We poll n people at random, yielding estimates (cid:98)p1, (cid:98)p2 and (cid:98)p3. Y claims
that she has more supporters than the other two candidates combined. Give a formula for an
approximate 95% conﬁdence interval for p2 − (p1 + p3).

10. Suppose Jack and Jill each collect random samples of size n from a population having unknown
mean µ but KNOWN variance σ2. They each form an approximate 95% conﬁdence interval for µ,
using (16.4) but with s replaced by σ. Find the approximate probability that their intervals do not
overlap. Express your answer in terms of Φ, the cdf of the N(0,1) distribution.

11. In the example of the population of three people, page 263, ﬁnd the following:

298

CHAPTER 16.

INTRODUCTION TO CONFIDENCE INTERVALS

(a) pX1(70)

(b) pX1,X2(69, 70)

(c) FX (69.5)

(d) probability that X overestimates the population mean µ

(e) pX (69) if our sample size is three rather than two (remember, we are sampling with replace-

ment)

12. In the derivation (15.8), suppose instead we have a simple random sample. Which one of the
following statements is correct?

(a) E(X) will still be equal to µ.

(b) E(X) will not exist.

(c) E(X) will exist, but may be less than µ.

(d) E(X) will exist, but may be greater than µ.

(e) None of the above is necessarily true.

13. Consider a toy example in which we take a random sample of size 2 (done with replacement)
from a population of size 2. The two values in the population (say heights in some measure system)
are 40 and 60. Find ps2(100).

Chapter 17

Introduction to Signiﬁcance Tests

Suppose (just for fun, but with the same pattern as in more serious examples) you have a coin
that will be ﬂipped at the Super Bowl to see who gets the ﬁrst kickoﬀ. (We’ll assume slightly
diﬀerent rules here. The coin is not “called.” Instead, it is agreed beforehand that if the coin
comes up heads, Team A will get the kickoﬀ, and otherwise it will be Team B.) You want to assess
for “fairness.” Let p be the probability of heads for the coin.

You could toss the coin, say, 100 times, and then form a conﬁdence interval for p using (16.11).
it tells you whether 100 tosses
The width of the interval would tell you the margin of error, i.e.
were enough for the accuracy you want, and the location of the interval would tell you whether the
coin is “fair” enough.

For instance, if your interval were (0.49,0.54), you might feel satisﬁed that this coin is reasonably
fair. In fact, note carefully that even if the interval were, say, (0.502,0.506), you would
still consider the coin to be reasonably fair; the fact that the interval did not contain 0.5 is
irrelevant, as the entire interval would be reasonably near 0.5.

However, this process would not be the way it’s traditionally done. Most users of statistics would
use the toss data to test the null hypothesis

against the alternate hypothesis

H0 : p = 0.5

HA : p (cid:54)= 0.5

(17.1)

(17.2)

For reasons that will be explained below, this procedure is called signiﬁcance testing. It forms

299

300

CHAPTER 17.

INTRODUCTION TO SIGNIFICANCE TESTS

the very core of statistical inference as practiced today. This, however, is unfortunate, as there
are some serious problems that have been recognized with this procedure. We will ﬁrst discuss the
mechanics of the procedure, and then look closely at the problems with it in Section 17.11.

17.1 The Basics

Here’s how signiﬁcance testing works.

The approach is to consider H0 “innocent until proven guilty,” meaning that we assume H0 is
true unless the data give strong evidence to the contrary. KEEP THIS IN MIND!—we are
continually asking, “What if...?”

The basic plan of attack is this:

We will toss the coin n times. Then we will believe that the coin is fair unless the
number of heads is “suspiciously” extreme, i.e. much less than n/2 or much more than
n/2.

Let p denote the true probability of heads for our coin. As in Section 16.4.1, let (cid:98)p denote the
proportion of heads in our sample of n tosses. We observed in that section that (cid:98)p is a special case
of a sample mean (it’s a mean of 1s and 0s). We also found that the standard deviation of (cid:98)p is
(cid:112)p(1 − p)/n, so that the standard error (Section 16.5.1) is (cid:112)

(cid:98)p(1 − (cid:98)p)/n.

In other words, the material surrounding (16.13) tells us that

(cid:113) 1

(cid:98)p − p
n · (cid:98)p(1 − (cid:98)p)

(17.3)

has an approximate N(0,1) distribution.

But remember, we are going to assume H0 for now, until and unless we ﬁnd strong evidence to the
contrary. So, in the numerator of (17.3), p = 0.5. And in the denominator, why use the standard
error when we “know” (under our provisional assumption) that p = 0.5? The exact standard
deviation of (cid:98)p is (cid:112)p(1 − p)/n. So, replace (17.3) by

Z =

(cid:113) 1

(cid:98)p − 0.5
n · 0.5(1 − 0.5)

(17.4)

then Z has an approximate N(0,1) distribution under the assumption that H0 is true.

17.2. GENERAL TESTING BASED ON NORMALLY DISTRIBUTED ESTIMATORS

301

Now recall from the derivation of (16.4) that -1.96 and 1.96 are the lower- and upper-2.5% points
of the N(0,1) distribution. Thus,

P (Z < −1.96 or Z > 1.96) ≈ 0.05

(17.5)

Now here is the point: After we collect our data, in this case by tossing the coin n times, we
compute (cid:98)p from that data, and then compute Z from (17.4). If Z is smaller than -1.96 or larger
than 1.96, we reason as follows:

Hmmm, Z would stray that far from 0 only 5% of the time. So, either I have to believe
that a rare event has occurred, or I must abandon my assumption that H0 is true.

For instance, say n = 100 and we get 62 heads in our sample. That gives us Z = 2.4, in that “rare”
range. We then reject H0, and announce to the world that this is an unfair coin. We say, “The
value of p is signiﬁcantly diﬀerent from 0.5.”

The 5% “suspicion criterion” used above is called the signiﬁcance level, typically denoted α. One
common statement is “We rejected H0 at the 5% level.”

On the other hand, suppose we get 47 heads in our sample. Then Z = -0.60. Again, taking 5% as
our signiﬁcance level, this value of Z would not be deemed suspicious, as it occurs frequently. We
would then say “We accept H0 at the 5% level,” or “We ﬁnd that p is not signiﬁcantly diﬀerent
from 0.5.”

The word signiﬁcant is misleading. It should NOT be confused with important. It simply is saying
we don’t believe the observed value of Z is a rare event, which it would be under H0; we have
instead decided to abandon our believe that H0 is true.

Note by the way that Z values of -1.96 and 1.96 correspond getting 50 − 1.96 · 0.5 ·
50 + 1.96 · 0.5 ·
rule to be “Reject if we get fewer than 40 or more than 60 heads, out of our 100 tosses.”

100 or
100 heads, i.e. roughly 40 or 60. In other words, we can describe our rejection

√

√

17.2 General Testing Based on Normally Distributed Estimators

In Section 16.5, we developed a method of constructing conﬁdence intervals for general approxi-
mately normally distributed estimators. Now we do the same for signiﬁcance testing.

Suppose (cid:98)θ is an approximately normally distributed estimator of some population value θ. Then

302

CHAPTER 17.

INTRODUCTION TO SIGNIFICANCE TESTS

to test H0 : θ = c, form the test statistic

Z =

(cid:98)θ − c
s.e.((cid:98)θ)

(17.6)

where s.e.((cid:98)θ) is the standard error of (cid:98)θ,1 and proceed as before:

Reject H0 : θ = c at the signiﬁcance level of α = 0.05 if |Z| ≥ 1.96.

17.3 Example: Network Security

Let’s look at the network security example in Section 16.6.2 again. Here (cid:98)θ = X − Y , and c is
presumably 0 (depending on the goals of Mano et al). From 16.20, the standard error works out to
0.61. So, our test statistic (17.6) is

Z =

X − Y − 0
0.61

=

11.52 − 2.00
0.61

= 15.61

(17.7)

This is deﬁnitely larger in absolute value than 1.96, so we reject H0, and conclude that the popu-
lation mean round-trip times are diﬀerent in the wired and wireless cases.

17.4 The Notion of “p-Values”

Recall the coin example in Section 17.1, in which we got 62 heads, i.e. Z = 2.4. Since 2.4 is
considerably larger than 1.96, our cutoﬀ for rejection, we might say that in some sense we not only
rejected H0, we actually strongly rejected it.

To quantify that notion, we compute something called the observed signiﬁcance level, more
often called the p-value.

We ask,

We rejected H0 at the 5% level. Clearly, we would have rejected it even at some small—
thus more stringent—levels. What is the smallest such level? Call this the p-value of
the test.

1See Section 16.5. Or, if we know the exact standard deviation of (cid:98)θ under H0, which was the case in our coin

example above, we could use that, for a better normal approximation.

17.5. EXAMPLE: BANK DATA

303

By checking a table of the N(0,1) distribution, or by calling pnorm(2.40) in R, we would ﬁnd
that the N(0,1) distribution has area 0.008 to the right of 2.40, and of course by symmetry there
is an equal area to the left of -2.40. That’s a total area of 0.016. In other words, we would have
been able to reject H0 even at the much more stringent signiﬁcance level of 0.016 (the 1.6% level)
instead of 0.05. So, Z = 2.40 would be considered even more signiﬁcant than Z = 1.96. In the
research community it is customary to say, “The p-value was 0.016.” 2 The smaller the p-value,
the more signiﬁcant the results are considered.

In our network security example above in which Z was 15.61, the value is literally “oﬀ the chart”;
pnorm(15.61) returns a value of 1. Of course, it’s a tiny bit less than 1, but it is so far out in the
right tail of the N(0,1) distribution that the area to the right is essentially 0. So the p-value would
be essentially 0, and the result would be treated as very, very highly signiﬁcant.

In computer output or research reports, we often see small p-values being denoted by asterisks.
There is generally one asterisk for p under 0.05, two for p less than 0.01, three for 0.001, etc. The
more asterisks, the more signiﬁcant the data is supposed to be. See for instance the R regression
output on page 408.

17.5 Example: Bank Data

Consider again the bank marketing data in Section 16.10. Our comparison was between marketing
campaign success rates for married and unmarried customers. The p-value was quite tiny, 2.2 ×
10−16, but be careful interpreting this.

First, don’t take that p-value as exact by any means. Though our sample sizes are certainly large
enough for the Central Limit Theorem to work well, that is in the heart of the distribution, not
the far tails. So, just take the p-value as “tiny,” and leave it at that.

Second, although the standard description for a test with such a small p-value is “very highly
signiﬁcant,” keep in mind that the diﬀerence between the two groups was not that large. The
conﬁdence interval we are 95% conﬁdent that the population success rate is between 3.3% and
4.6% less for married people. That is an interesting diﬀerence and possibly of some use to the
marketing people, but it is NOT large.

2The ‘p’ in “p-value” of course stands for “probability,” meaning the probably that a N(0,1) random variable
would stray as far, or further, from 0 as our observed Z here. By the way, be careful not to confuse this with the
quantity p in our coin example, the probability of heads.

304

CHAPTER 17.

INTRODUCTION TO SIGNIFICANCE TESTS

17.6 One-Sided HA

Suppose that—somehow—we are sure that our coin in the example above is either fair or it is more
heavily weighted towards heads. Then we would take our alternate hypothesis to be

HA : p > 0.5

(17.8)

A “rare event” which could make us abandon our belief in H0 would now be if Z in (17.4) is very
large in the positive direction. So, with α = 0.05, we call qnorm(0.95), and ﬁnd that our rule
would now be to reject H0 if Z > 1.65.

One-sided tests are not common, as their assumptions are often diﬃcult to justify.

17.7 Exact Tests

Remember, the tests we’ve seen so far are all approximate.
In (17.4), for instance, (cid:98)p had an
approximate normal distribution, so that the distribution of Z was approximately N(0,1). Thus the
signiﬁcance level α was approximate, as were the p-values and so on.3

But the only reason our tests were approximate is that we only had the approximate distribution
of our test statistic Z, or equivalently, we only had the approximate distribution of our estimator,
e.g. (cid:98)p. If we have an exact distribution to work with, then we can perform an exact test.

17.7.1 Example: Test for Biased Coin

Let’s consider the coin example again, with the one-sided alternative (17.8) . To keep things simple,
let’s suppose we toss the coin 10 times. We will make our decision based on X, the number of heads
out of 10 tosses. Suppose we set our threshhold for “strong evidence” again H0 to be 8 heads, i.e.
we will reject H0 if X ≥ 8. What will α be?

α =

10
(cid:88)

i=8

P (X = i) =

10
(cid:88)

i=8

(cid:19)10

(cid:18) 10
i

(cid:19) (cid:18) 1
2

= 0.055

(17.9)

That’s not the usual 0.05. Clearly we cannot get an exact signiﬁcance level of 0.05,4 but our α is

3Another class of probabilities which would be approximate would be the power values. These are the probabilities
of rejecting H0 if the latter is not true. We would speak, for instance, of the power of our test at p = 0.55, meaning
the chances that we would reject the null hypothesis if the true population value of p were 0.55.

4Actually, it could be done by introducing some randomization to our test.

17.7. EXACT TESTS

305

exactly 0.055, so this is an exact test.

So, we will believe that this coin is perfectly balanced, unless we get eight or more heads in our
10 tosses. The latter event would be very unlikely (probability only 5.5%) if H0 were true, so we
decide not to believe that H0 is true.

17.7.2 Example: Improved Light Bulbs

Suppose lifetimes of lightbulbs are exponentially distributed with mean µ. In the past, µ = 1000,
but there is a claim that the new light bulbs are improved and µ > 1000. To test that claim, we
will sample 10 lightbulbs, getting lifetimes X1, ..., X10, and compute the sample mean X. We will
then perform a signiﬁcance test of

vs.

H0 : µ = 1000

HA : µ > 1000

It is natural to have our test take the form in which we reject H0 if

for some constant w chosen so that

X > w

(17.10)

(17.11)

(17.12)

P (X > w) = 0.05

(17.13)

under H0. Suppose we want an exact test, not one based on a normal approximation.

Remember, we are making our calculations under the assumption that H0 is true. Now recall
(Section 7.6.4.1) that 10X, the sum of the Xi, has a gamma distribution, with r = 10 and λ = 0.001.
So, we can ﬁnd the w for which P (X > w) = 0.05 by using R’s qgamma():

> qgamma(0.95,10,0.001)
[1] 15705.22

So, we reject H0 if our sample mean is larger than 1570.5.

306

CHAPTER 17.

INTRODUCTION TO SIGNIFICANCE TESTS

Now suppose it turns out that X = 1624.2. Under H0 there was only a 0.05 chance that X would
exceed 1570.5, so we would reject H0 with α = 0.05. But what if we had set w to 1624.2? We
didn’t do so, of course, but what if? The computation

> 1 − pgamma( 1 6 2 4 2 , 1 0 , 0 . 0 0 1 )
[ 1 ] 0 . 0 3 8 4 0 6 2 9

shows that we would have rejected H0 even if we had originally set α to the more stringent criterion
of 0.038 instead of 0.05. So we report that the p-value was 0.038.

The idea of a p-value is to indicate in our report “how strongly” we rejected H0. Arguably there
is a bit of game-playing in p-values, as there is with signiﬁcance testing in general. This will be
pursued in Section 17.11.

17.7.3 Example: Test Based on Range Data

Suppose lifetimes of some electronic component formerly had an exponential distribution with mean
100.0. However, it’s claimed that now the mean has increased. (Suppose we are somehow sure it has
not decreased.) Someone has tested 50 of these new components, and has recorded their lifetimes,
X1, ..., X50. Unfortunately, they only reported to us the range of the data, R = maxi Xi − mini Xi,
not the individual Xi. We will need to do a signiﬁcance test with this limited data, at the 0.05
level.

Recall that the variance of an exponential random variable is the square of its mean. Intutively,
then, the larger this population mean of X, the larger the mean of the range R. In other words, the
form of the test should be to reject H0 if R is greater than some cutoﬀ value c. So, we need to ﬁnd
the value of c to make α equal to 0.05.

Unfortunately, we can’t do this analytically, i.e. mathematically, as the distribution of R is far too
complex. This we’ll have to resort to simulation.5 Here is code to do that:

f o r

s i g n i f i c a n c e

1 # code t o d e t e r m i n e t h e c u t o f f p o i n t
2 # a t 0 . 0 5 l e v e l
3
4 n r e p s <− 200000
5 n <− 50
6
7
8
9

r v e c <− vector ( length=n r e p s )
i n 1 : n r e p s ) {
fo r ( i
x <− rexp ( n , 0 . 0 1 )

5I am still referring to the following as an exact test, as we are not using any statistical approximation, such as

the Central Limit Theorm.

17.8. DON’T SPEAK OF “THE PROBABILITY THAT H0 IS TRUE”

307

rng <− range ( x )
r v e c [ i ] <− rng [ 2 ] − rng [ 1 ]

10
11
12 }
13
14
15
16 cat ( ” r e j e c t H0 i f R >” , r v e c [ c u t o f f ] , ”\n” )

r v e c <− sort ( r v e c )
c u t o f f <− r v e c [ c e i l i n g ( 0 . 9 5 ∗ n r e p s ) ]

Here we general nreps samples of size 50 from an exponential distribution having mean 100. Note
that since we are setting α, a probability deﬁned in the setting in which H0 is true, we assume the
mean is 100. For each of the nreps samples we ﬁnd the value of R, recording it in rvec. We then
take the 95th percentile of those values, which is the c for which P(R > c) = 0.05.6

The value of c output by the code was 220.4991. A second run yielded, 220.9304, and a third
220.7099. The fact that these values varied little among themselves indicates that our value of
nreps, 200000, was suﬃciently large.

17.7.4 Exact Tests under a Normal Distribution Assumption

If you are willing to assume that you are sampling from a normally-distributed population, then
the Student-t test is nominally exact. The R function t.test() performs this operation, with the
argument alternative set to be either ”less” or ”greater”.

17.8 Don’t Speak of “the Probability That H0 Is True”

It is very important to understand that throughout this chapter, we cannot speak of “the probability
that H0 is true,” because we have no probablistic structure on H0.

Consider the fair-coin problem at the beginning of this chapter. Suppose we hope to make a
statement like, say, “Given that we got 62 heads out of 100 tosses, we ﬁnd that the probability that
this is a fair coin is 0.04.” What kind of derivation would need to go into this? It would go along
the following lines:

6Of course, this is approximate. The greater the value of nreps, the better the approximation.

308

CHAPTER 17.

INTRODUCTION TO SIGNIFICANCE TESTS

P (H0 is true | our data) =

P (H0 is true and our data)
P (our data)

(17.14)

=

=

P (H0 is true and our data)
P (H0 is true and our data) + P (H0 is false and our data)

P (H0 true) P (our data | H0 true)
P (H0 true) P (our data | H0 true) + P (H0 false) P (our data | H0 false)

Through our modeling process, e.g. the discussion surrounding (17.4), we can calculate P (our data | H0 is true).
(The false case would be more complicated, since there are many diﬀerent kinds of false cases here,
for diﬀerent values of p, but could be handled similarly.) But what we don’t have is P (H0 is true).

We could certainly try to model that latter quantity, say by taking a sample of all possible pennies
(if our coin is a penny), doing very extensive testing of them;7 the proportion found to be fair would
then be P (H0 is true). But lacking that, we have no probablistic structure for P (H0 is true), and
thus cannot use language like “the probability that H0 is true,”

17.9 R Computation

The R function t.test(), discussed in Section 16.8, does both conﬁdence intervals and tests, in-
cluding p-values in the latter case.

17.10 The Power of a Test

In addition to the signiﬁcance level of a test, we may also be interested in its power (or its many
power values, as will be seen).

17.10.1 Example: Coin Fairness

For example, consider our ﬁrst example in this chapter, in which we were testing a coin for fairness
(Section 17.1). Our rule for a test at a 0.05 signiﬁcance level turned out to be that we reject H0 if
we get fewer than 40 or more than 60 heads out of our 100 tosses. We might ask the question, say:

Suppose the true heads probability is 0.65. We don’t know, of course, but what if that
were the case. That’s a pretty substantial departure from H0, so hopefully we would
reject. Well, what is the probability that we would indeed reject?

7Say, 100,000 tosses per coin.

17.11. WHAT’S WRONG WITH SIGNIFICANCE TESTING—AND WHAT TO DO INSTEAD309

We could calculate this. Let N denote the number of heads. Then the desired probability is
P (N < 40 or N > 60) = P (N < 40) + P (N > 60). Let’s ﬁnd the latter.8

Once again, since N has a binomial distribution, it is approximately normal, in this case with mean
np = 100 × 0.65 = 65 and variance np(1 − p) = 100 × 0.65 × 0.35 = 22.75. Then P (N > 60) is
about

1 − pnorm( 6 0 , 6 5 , sqrt ( 2 2 . 7 5 ) )

or about 0.85. So we would be quite likely to decide this is an unfair coin if (unknown to us) the
true probability of heads is 0.65.

We say that the power of this test at p = 0.65 is 0.85. There is a diﬀerent power for each p.

17.10.2 Example: Improved Light Bulbs

Let’s ﬁnd the power of the test in Section 17.7.2, at µ = 1250. Recall that we reject H0 if
X > 1570.522. Thus our power is

1 − pgamma( 1 5 7 0 5 . 2 2 , 1 0 , 1 / 1 2 5 0 )

This turns out to be about 0.197. So, if (remember, this is just a “what if?”) the true new mean
were 1250, we’d only have about a 20% chance of discovering that the new bulbs are improved.

17.11 What’s Wrong with Signiﬁcance Testing—and What to Do

Instead

The ﬁrst principle is that you must not fool yourself—and you are the easiest person to fool. So
you have to be very careful about that. After you’ve not fooled yourself, it’s easy not to fool other
scientists.—Richard Feynman, Nobel laureate in physics

“Sir Ronald [Fisher] has befuddled us, mesmerized us, and led us down the primrose path”—Paul
Meehl, professor of psychology and the philosophy of science

Signiﬁcance testing is a time-honored approach, used by tens of thousands of people
every day. But it is “wrong.” I use the quotation marks here because, although signiﬁcance
testing is mathematically correct, it is at best noninformative and at worst seriously misleading.

8The former would be found similarly, but would come out quite small.

310

CHAPTER 17.

INTRODUCTION TO SIGNIFICANCE TESTS

17.11.1 History of Signiﬁcance Testing, and Where We Are Today

We’ll see why signiﬁcance testing has serious problems shortly, but ﬁrst a bit of history.

When the concept of signiﬁcance testing, especially the 5% value for α, was developed in the 1920s
by Sir Ronald Fisher, many prominent statisticians opposed the idea—for good reason, as we’ll see
below. But Fisher was so inﬂuential that he prevailed, and thus signiﬁcance testing became the
core operation of statistics.

So, signiﬁcance testing became entrenched in the ﬁeld, in spite of being widely recognized as faulty,
to this day. Most modern statisticians understand this, even if many continue to engage in the
practice.9 Here are a few places you can read criticism of testing:

• There is an entire book on the subject, The Cult of Statistical Signiﬁcance, by S. Ziliak and
D. McCloskey. Interestingly, on page 2, they note the prominent people who have criticized
testing. Their list is a virtual “who’s who” of statistics, as well as physics Nobel laureate
Richard Feynman and economics Nobelists Kenneth Arrow and Milton Friedman.

• See http://www.indiana.edu/~stigtsts/quotsagn.html for a nice collection of quotes

from famous statisticians on this point.

• There is an entire chapter devoted to this issue in one of the best-selling elementary statistics

textbooks in the nation.10

• The Federal Judicial Center, which is the educational and research arm of the federal court
system, commissioned two prominent academics, one a statistics professor and the other a
law professor, to write a guide to statistics for judges: Reference Guide on Statistics. David
H. Kaye. David A. Freedman, at

http://www.fjc.gov/public/pdf.nsf/lookup/sciman02.pdf/$file/sciman02.pdf

There is quite a bit here on the problems of signiﬁcance testing, and especially p.129.

17.11.2 The Basic Fallacy

To begin with, it’s absurd to test H0 in the ﬁrst place, because we know a priori that H0 is
false.

9Many are forced to do so, e.g. to comply with government standards in pharmaceutical testing. My own approach
in such situations is to quote the test results but then point out the problems, and present conﬁdence intervals as
well.

10Statistics, third edition, by David Freedman, Robert Pisani, Roger Purves, pub. by W.W. Norton, 1997.

17.11. WHAT’S WRONG WITH SIGNIFICANCE TESTING—AND WHAT TO DO INSTEAD311

Consider the coin example, for instance. No coin is absolutely perfectly balanced, and yet that is
the question that signiﬁcance testing is asking:

H0 : p = 0.5000000000000000000000000000...

(17.15)

We know before even collecting any data that the hypothesis we are testing is false, and thus it’s
nonsense to test it.

But much worse is this word “signiﬁcant.” Say our coin actually has p = 0.502. From
anyone’s point of view, that’s a fair coin! But look what happens in (17.4) as the sample size n
grows. If we have a large enough sample, eventually the denominator in (17.4) will be small enough,
and (cid:98)p will be close enough to 0.502, that Z will be larger than 1.96 and we will declare that p is
“signiﬁcantly” diﬀerent from 0.5. But it isn’t! Yes, 0.502 is diﬀerent from 0.5, but NOT in any
signiﬁcant sense in terms of our deciding whether to use this coin in the Super Bowl.

The same is true for government testing of new pharmaceuticals. We might be comparing a new
drug to an old drug. Suppose the new drug works only, say, 0.4% (i.e. 0.004) better than the old
one. Do we want to say that the new one is “signﬁcantly” better? This wouldn’t be right, especially
if the new drug has much worse side eﬀects and costs a lot more (a given, for a new drug).

Note that in our analysis above, in which we considered what would happen in (17.4) as the
sample size increases, we found that eventually everything becomes “signﬁciant”—even if there is
no practical diﬀerence. This is especially a problem in computer science applications of statistics,
because they often use very large data sets. A data mining application, for instance, may consist
of hundreds of thousands of retail purchases. The same is true for data on visits to a Web site,
network traﬃc data and so on. In all of these, the standard use of signiﬁcance testing can result
in our pouncing on very small diﬀerences that are quite insigniﬁcant to us, yet will be declared
“signiﬁcant” by the test.

Conversely, if our sample is too small, we can miss a diﬀerence that actually is signiﬁcant—i.e.
In the
important to us—and we would declare that p is NOT signiﬁcantly diﬀerent from 0.5.
example of the new drug, this would mean that it would be declared as “not signiﬁcantly better”
than the old drug, even if the new one is much better but our sample size wasn’t large enough to
show it.

In summary, the basic problems with signiﬁcance testing are

• H0 is improperly speciﬁed. What we are really interested in here is whether p is near 0.5, not

whether it is exactly 0.5 (which we know is not the case anyway).

• Use of the word signiﬁcant is grossly improper (or, if you wish, grossly misinterpreted).

Signiﬁcance testing forms the very core usage of statistics, yet you can now see that it is, as I said

312

CHAPTER 17.

INTRODUCTION TO SIGNIFICANCE TESTS

above, “at best noninformative and at worst seriously misleading.” This is widely recognized by
thinking statisticians and prominent scientists, as noted above. But the practice of signiﬁcance
testing is too deeply entrenched for things to have any prospect of changing.

17.11.3 You Be the Judge!

This book has been written from the point of view that every educated person should understand
statistics. It impacts many vital aspects of our daily lives, and many people with technical degrees
ﬁnd a need for it at some point in their careers.

In other words, statistics is something to be used, not just learned for a course. You should think
about it critically, especially this material here on the problems of signiﬁcance testing. You yourself
should decide whether the latter’s widespread usage is justiﬁed.

17.11.4 What to Do Instead

Note carefully that I am not saying that we should not make a decision. We do have to decide, e.g.
decide whether a new hypertension drug is safe or in this case decide whether this coin is “fair”
enough for practical purposes, say for determining which team gets the kickoﬀ in the Super Bowl.
But it should be an informed decision, and even testing the modiﬁed H0 above would be much less
informative than a conﬁdence interval.

In fact, the real problem with signiﬁcance tests is that they take the decision out of our hands.
They make our decision mechanically for us, not allowing us to interject issues of importance to
us, such possible side eﬀects in the drug case.

So, what can we do instead?

In the coin example, we could set limits of fairness, say require that p be no more than 0.01 from
0.5 in order to consider it fair. We could then test the hypothesis

H0 : 0.49 ≤ p ≤ 0.51

(17.16)

Such an approach is almost never used in practice, as it is somewhat diﬃcult to use and explain.
But even more importantly, what if the true value of p were, say, 0.51001? Would we still really
want to reject the coin in such a scenario?

Forming a conﬁdence interval is the far superior approach. The width of the interval shows
us whether n is large enough for (cid:98)p to be reasonably accurate, and the location of the interval tells
us whether the coin is fair enough for our purposes.

17.11. WHAT’S WRONG WITH SIGNIFICANCE TESTING—AND WHAT TO DO INSTEAD313

Note that in making such a decision, we do NOT simply check whether 0.5 is in the
interval. That would make the conﬁdence interval reduce to a signiﬁcance test, which is what
we are trying to avoid. If for example the interval is (0.502,0.505), we would probably be quite
satisﬁed that the coin is fair enough for our purposes, even though 0.5 is not in the interval.

On the other hand, say the interval comparing the new drug to the old one is quite wide and more
or less equal positive and negative territory. Then the interval is telling us that the sample size
just isn’t large enough to say much at all.

Signiﬁcance testing is also used for model building, such as for predictor variable selection in
regression analysis (a method to be covered in Chapter 22). The problem is even worse there,
because there is no reason to use α = 0.05 as the cutoﬀ point for selecting a variable. In fact, even
if one uses signiﬁcance testing for this purpose—again, very questionable—some studies have found
that the best values of α for this kind of application are in the range 0.25 to 0.40, far outside the
range people use in testing.

In model building, we still can and should use conﬁdence intervals. However, it does take more
work to do so. We will return to this point in our unit on modeling, Chapter 21.

17.11.5 Decide on the Basis of “the Preponderance of Evidence”

I was in search of a one-armed economist, so that the guy could never make a statement and then
say: “on the other hand”—President Harry S Truman

If all economists were laid end to end, they would not reach a conclusion—Irish writer George
Bernard Shaw

In the movies, you see stories of murder trials in which the accused must be “proven guilty beyond
the shadow of a doubt.” But in most noncriminal trials, the standard of proof is considerably
lighter, preponderance of evidence. This is the standard you must use when making decisions
based on statistical data. Such data cannot “prove” anything in a mathematical sense. Instead, it
should be taken merely as evidence. The width of the conﬁdence interval tells us the likely accuracy
of that evidence. We must then weigh that evidence against other information we have about the
subject being studied, and then ultimately make a decision on the basis of the preponderance of
all the evidence.

Yes, juries must make a decision. But they don’t base their verdict on some formula. Similarly,
you the data analyst should not base your decision on the blind application of a method that is
usually of little relevance to the problem at hand—signiﬁcance testing.

314

CHAPTER 17.

INTRODUCTION TO SIGNIFICANCE TESTS

17.11.6 Example: the Forest Cover Data

In Section 16.6.4, we found that an approximate 95% conﬁdence interval for µ1 − µ2 was

223.8 − 226.3 ± 2.3 = (−4.8, −0.3)

(17.17)

Clearly, the diﬀerence in HS12 between cover types 1 and 2 is tiny when compared to the general
size of HS12, in the 200s. Thus HS12 is not going to help us guess which cover type exists at a
given location. Yet with the same data, we would reject the hypothesis

and say that the two means are “signiﬁcantly” diﬀerent, which sounds like there is an important
diﬀerence—which there is not.

H0 : µ1 = µ2

(17.18)

17.11.7 Example: Assessing Your Candidate’s Chances for Election

Imagine an election between Ms. Smith and Mr. Jones, with you serving as campaign manager
for Smith. You’ve just gotten the results of a very small voter poll, and the conﬁdence interval for
p, the fraction of voters who say they’ll vote for Smith, is (0.45,0.85). Most of the points in this
interval are greater than 0.5, so you would be highly encouraged! You are certainly not sure of the
ﬁnal election result, as a small part of the interval is below 0.5, and anyway voters might change
their minds between now and the election. But the results would be highly encouraging.

Yet a signiﬁcance test would say “There is no signiﬁcant diﬀerence between the two candidates.
It’s a dead heat.” Clearly that is not telling the whole story. The point, once again, is that the
conﬁdence interval is giving you much more information than is the signiﬁcance test.

Exercises

1. In the light bulb example on page 305, suppose the actual observed value of X turns out to be
15.88. Find the p-value.

Chapter 18

General Statistical Estimation and
Inference

Earlier, we often referred to certain estimators as being “natural.” For example, if we are estimating
a population mean, an obvious choice of estimator would be the sample mean. But in many
applications, it is less clear what a “natural” estimate for a population quantity of interest would
be. We will present general methods for estimation in this section.

We will also discuss advanced methods of inference.

18.1 General Methods of Parametric Estimation

Let’s begin with a simple motivating example.

18.1.1 Example: Guessing the Number of Raﬄe Tickets Sold

You’ve just bought a raﬄe ticket, and ﬁnd that you have ticket number 68. You check with a
couple of friends, and ﬁnd that their numbers are 46 and 79. Let c be the total number of tickets.
How should we estimate c, using our data 68, 46 and 79?

Let X1, X2, ..., Xn denote the ticket numbers we are aware of.
In the above case, n = 3, with
X1 = 68 etc. We will treat the Xi as a random sample from 1,2,...,c. This assumption should be
carefully considered. Recall that this means that X1, X2, ..., Xn are i.i.d. with a (discrete) uniform
distribution on 1,2....,c.

If we know, say, that the three friends bought their tickets right after they became available.

315

316

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

This might mean the Xi are nearer to 1 than to c, in which case the assumption of the uniform
distribution may not be very good. Or, suppose we know that the three friends were standing close
to each other in line. Then the independence assumption might be dubious.

And even if not, and even if the Xi are considered a sample with the uniformity assumption satisﬁed,
it would technically be what is called a simple random sample (Section 15.1.1), since the “sampling”
is done without replacement; no two people get the same ticket.

So, in practice one would need to consider how good our assumption is that we have a random
sample. If n << c, the various probabilities are virtually identical for sampling with and without
replacement, so that may not be an issue, so we would just worry about the uniformity.

But let’s suppose that has been resolved, and then look at how we can estimate c.

18.1.2 Method of Moments

One approach, a very intuitive one, is the Method of Momentsi (MM). It is less commonly used
than the other method we’ll cover, Maximum Likelihood, but is much easier to explain. Also, in
recent years, MM has become more popular in certain ﬁelds, such as random graph models (Section
2.13).

18.1.2.1 Example: Lottery Model

Let X be distributed the same as the Xi, i.e. it is uniformly distributed on 1,2,...,c.

Note ﬁrst that

Let’s solve for c:

We know that we can use

E(X) =

c + 1
2

c = 2EX − 1

X =

1
n

n
(cid:88)

i=1

Xi

(18.1)

(18.2)

(18.3)

18.1. GENERAL METHODS OF PARAMETRIC ESTIMATION

317

to estimate EX, so by (18.2), 2X − 1 is an intuitive estimate of c. Thus we take our estimator for
c to be

(cid:98)c = 2X − 1

(18.4)

This estimator is called the Method of Moments estimator of c.

18.1.2.2 General Method

Say our sample is X1, ..., Xn; a, and we have k parameters to estimate, θ1, ..., θk. Our goal is to
come up with estimators (cid:98)θ1, ..., (cid:98)θk.

Here is the procedure:

• For each i = 1, ..., k, write E(X k) as a function of the θj,

E(X i) = gi(θ1, ..., θk)

• On the left side of (18.5), replace E(X i) by the corresponding sample moment

1
n

n
(cid:88)

r=1

X i
r

• On the right side of (18.5), replace each θs by (cid:98)θs.

• Solve for the (cid:98)θs

In the raﬄe example, we had k = 1, θ1 = c, g1(c) = (c + 1)/2 and so on.

18.1.3 Method of Maximum Likelihood

(18.5)

(18.6)

Another method, much more commonly used, is called the Method of Maximum Likelihood.

18.1.3.1 Example: Raﬄe Model

In our example above, it means asking the question, “What value of c would have made our
data—68, 46, 79—most likely to happen?” Well, let’s ﬁnd what is called the likelihood, i.e. the

318

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

probability of our particular data values occurring:

L = P (X1 = 68, X2 = 46, X3 = 79) =

(cid:40)

( 1
c )3,
0,

if c ≥ 79
otherwise

(18.7)

Now keep in mind that c is a ﬁxed, though unknown constant. It is not a random variable. What
we are doing here is just asking “What if” questions, e.g. “If c were 85, how likely would our data
be? What about c = 91?”

Well then, what value of c maximizes (18.7)? Clearly, it is c = 79. Any smaller value of c gives us
a likelihood of 0. And for c larger than 79, the larger c is, the smaller (18.7) is. So, our maximum
likelihood estimator (MLE) is 79. In general, if our sample size in this problem were n, our MLE
for c would be

ˇc = max

i

Xi

(18.8)

Note that in our earlier discussion of whether our data can be treated as a random sample, we
raised the issue of the independence assumption. Note that this assumption in fact was used for ˇc,
while it was not for (cid:98)c.

18.1.3.2 General Procedure

Say again our random sample is X1, ..., Xn and we have k parameters to estimate, θ1, ..., θk. Our
goal is to come up with estimators (cid:98)θ1, ..., (cid:98)θk.

Denote the pmf or density of our Xi as f (t; θ1, ..., θk). Then our estimators (cid:98)θi are the values that
maximize the likelihood

Πn

i=1f (Xi; θ1, ..., θk)

(18.9)

with respect to the θj.

For “smooth” problems, i.e. ones in which the likelihood is a diﬀerentiable function of the param-
eters, aaximization involves taking the derivatives with respect to the θj, setting them to 0, then
solving for the θj; the solutions are the (cid:98)θj.

Since it is easier to take derivatives of sums than products, typically one maximizes the log likelihood

n
(cid:88)

i=1

ln[f (Xi; θ1, ..., θk)]

(18.10)

18.1. GENERAL METHODS OF PARAMETRIC ESTIMATION

319

18.1.4 Example: Estimation of the Parameters of a Gamma Distribution

As another example, suppose we have a random sample X1, ..., Xn from a gamma distribution.

fX (t) =

1
Γ(c)

λctc−1e−λt, t > 0

(18.11)

for some unknown c and λ. How do we estimate c and λ from the Xi?

18.1.4.1 Method of Moments

Let’s try the Method of Moments, as follows. We have two population parameters to estimate, c
and λ, so we need to involve two moments of X. That could be EX and E(X 2), but here it would
more conveniently be EX and Var(X). We know from our previous unit on continuous random
variables, Chapter 7, that

EX =

c
λ

V ar(X) =

c
λ2

(18.12)

(18.13)

In our earlier notation, this would be r = 2, θ1 = c , θ2 = λ and g1(c, λ) = c/λ and g2(c, λ) = c/λ2.

Switching to sample analogs and estimates, we have

Dividing the two quantities yields

which then gives

(cid:98)c
(cid:98)λ

(cid:98)c
(cid:98)λ2

= X

= s2

(cid:98)λ =

X
s2

2

X
s2

(cid:98)c =

(18.14)

(18.15)

(18.16)

(18.17)

320

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

18.1.4.2 MLEs

What about the MLEs of c and λ? Remember, the Xi are continuous random variables, so the
likelihood function, i.e. the analog of (18.7), is the product of the density values:

L = Πn

i=1

(cid:20) 1
Γ(c)

λcXi

c−1e−λXi

(cid:21)

= [λc/Γ(c)]n (Πn

i=1Xi)c−1 e−λ (cid:80)n

i=1 Xi

(18.18)

(18.19)

In general, it is usually easier to maximize the log likelihood (and maximizing this is the same as
maximizing the original likelihood):

l = (c − 1)

n
(cid:88)

i=1

ln(Xi) − λ

n
(cid:88)

i=1

Xi + nc ln(λ) − n ln(Γ(c))

(18.20)

One then takes the partial derivatives of (18.20) with respect to c and λ, and sets the derivatives
to zero. The solution values, ˇc and ˇλ, are then the MLEs of c and λ. Unfortunately, in this case,
these equations do not have closed-form solutions. So the equations must be solved numerically.
(In fact, numerical methods are needed even more in this case, because ﬁnding the derivative of
Γ(c) is not easy.)

18.1.5 R’s mle() Function

R provides a function, mle(), for ﬁnding MLEs in mathematically intractable situations such as
the one in the last section.

Note: The function is in the stats4 library, so run

> l i b r a r y ( s t a t s 4 )

ﬁrst.

Here’s an example in that context. We’ll simulate some data from a gamma distribution with given
parameter values, then pretend we don’t know those, and ﬁnd the MLEs from the data:

> n <- 1000
> x <- rgamma(n,shape=2,rate=1) # Erlang, r = 2, lambda is 1

# function to compute negative log likelihood
ll <- function(c,lambda) {

18.1. GENERAL METHODS OF PARAMETRIC ESTIMATION

321

loglik <- (c-1) * sum(log(x)) - sum(x)*lambda + n*c*log(lambda) -

n*log(gamma(c))

-loglik

}

summary(mle(minuslogl=ll,start=list(c=1.5,lambda=2)))
Maximum likelihood estimation

Call:
mle(minuslogl = ll, start = list(c = 1, lambda = 1))

Coefficients:

Estimate Std. Error
c
2.147605 0.08958823
lambda 1.095344 0.05144393

-2 log L: 3072.181

How did this work? The main task we have is to write a function that calculates the negative log
likelihood, with that function’s arguments will be the parameters to be estimated. We deﬁned our
function and named it ll().1

Fortunately for us, mle() calculates the derivatives numerically too, so we didn’t need to specify
them in the log likelihood function. (Needless to say, this function thus cannot be used in a problem
in which derivatives cannot be used, such as the lottery example above.)

We also need to supply mle() with initial guesses for the parameters. That’s done in the start
argument. I more or less arbitrarily chose these values. You may have to experiment, though, as
some sets of initial values may not result in convergence.

The standard errors of the estimated parameters are also printed out, enabling the formation of
conﬁdence intervals and signiﬁcance tests. (See Section 16.5 for the case of conﬁdence intervals.)
In fact, you can get the estimated covariance matrix for the vector of estimated parameters. In our
case here:

> mleout <- mle(minuslogl=ll,start=list(c=1.5,lambda=2))
> solve(mleout@details$hessian)

lambda
c
0.008026052 0.004093526
lambda 0.004093526 0.002646478

c

By the way, there were also some warning messages, due to the fact that during the iterative
maximization process, some iterations generated guesses for ˇλ were 0 or near it, causing problems
with log().

Maximizing the likelihood meant minimizing the negative log likelihood. Why did the authors of R

1Note that in R, log() calculates the natural logarithm by default.

322

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

write it this way, and in fact report -2 times the log likelihood? The answer is that this is related
to a signiﬁcance testing procedure, the likelihood ratio test, a matter beyond the scope of this book.

Note that if you are using mle() with a parametric family for which R supplies a density function,
it’s quite convenient to simply use that in our likelihood function code:

> l l <− function ( c , lambda ) −sum(dgamma( x , shape=c , r a t e=lambda , log=TRUE) )
> summary( mle ( m i n u s l o g l=l l , s t a r t=l i s t ( c =1.5 , lambda =2)))
Maximum l i k e l i h o o d e s t i m a t i o n

Call :
mle ( m i n u s l o g l = l l , s t a r t = l i s t ( c = 1 . 5 ,

lambda = 2 ) )

C o e f f i c i e n t s :

Estimate Std . E r r o r
c
2 . 1 4 7 6 0 5 0 . 0 8 9 5 8 8 2 3
lambda 1 . 0 9 5 3 4 4 0 . 0 5 1 4 4 3 9 3

−2 log L : 3 0 7 2 . 1 8 1

R made things so convenient that we didn’t even need to take the logs ourselves.

18.1.6 R’s gmm() Function

A function to do Method of Moments estimation is also available, in R’s gmm package on CRAN.
The package actually does more, something called Generalized Method of Moments, but we’ll see
how to use it in elementary form here.

The work is done by the eponymous function gmm(). In our usage here, the call form is

gmm( data , momentftn { , s t a r t ) }

where data is our data in matrix or vector form, momentftn speciﬁes the moments, and start is
our initial guess for the iterative solution process.

The function momentftn() speciﬁes how to compute the diﬀerences between the estimated values
of the population moments (in terms of the parameter estimates) and the corresponding sample
powers. During the computation, gmm() will set the means of these diﬀerences to 0, iterating
until convergence.

18.1. GENERAL METHODS OF PARAMETRIC ESTIMATION

323

18.1.6.1 Example: Bodyfat Data

Our data set will be bodyfat, which is included in the mfp package, with measurements on 252
men. The ﬁrst column of this data frame, brozek, is the percentage of body fat, which when
converted to a proportion is in (0,1). That makes it a candidate for ﬁtting a beta distribution
(Section 7.6.5).

Here is our momentftn():

g <− function ( th , x ) {

t 1 <− th [ 1 ]
t 2 <− th [ 2 ]
t 1 2 <− t 1 + t 2
meanb <− t 1 / t 1 2
m1 <− meanb − x
m2 <− t 1 ∗ t 2 / ( t 1 2 ˆ2 ∗ ( t 1 2 +1)) − ( x − meanb ) ˆ 2
f <− cbind (m1, m2)
return ( f )

}

Here the argument th (theta) will be the MM estimates (at any given iteration) of the population
parameters, in this case of α and β.

Now look at the line

m1 <− meanb − x

The value meanb is our estimate in the currently iteration of the mean of the beta distribution,
while x is our data. The gmm() function, in calling our g(), will calculate the average of m1 over
all our data, and then in setting that average to 0, we are equating our parametric estimate of the
population mean to our sample mean — exactly what MM is supposed to do.

Let’s try it:

> l i b r a r y ( mfp )
> data ( b o d y f a t )
> gmm( g , x , c ( a l p h a =0.1 , beta = 0 . 1 ) )
Method

twoStep

O b j e c t i v e function v a l u e : 2 . 5 5 9 6 4 5 e −10

a l p h a beta
4 . 6 7 1 4 1 9 . 9 9 6 9

324

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

Figure 18.1: Fitting a Beta Density

Convergence code = 0
> h i s t ( b o d y f a t $ b r o z e k / 1 0 0 , xlim=c ( 0 , 1 ) ,

p r o b a b i l i t y=TRUE)

> curve ( dbeta ( x , 4 . 6 7 , 2 0 . 0 0 ) , add=TRUE)

The result is seen in Figure 18.1. At least visually, the ﬁt seems pretty good.

18.1. GENERAL METHODS OF PARAMETRIC ESTIMATION

325

18.1.7 More Examples

Suppose fW (t) = ctc−1 for t in (0,1), with the density being 0 elsewhere, for some unknown c > 0.
We have a random sample W1, ..., Wn from this density.

Let’s ﬁnd the Method of Moments estimator.

So, set

yielding

What about the MLE?

so

Then set

and thus

EW =

(cid:90) 1

0

tctc−1 dt =

c
c + 1

W = (cid:98)c
(cid:98)c + 1

(cid:98)c =

W
1 − W

L = Πn

i=1cW c−1

i

l = n ln c + (c − 1)

n
(cid:88)

i=1

ln Wi

0 =

+

n
(cid:98)c

n
(cid:88)

i=1

ln Wi

(cid:98)c = −

1

1
n

(cid:80)n

i=1 ln Wi

(18.21)

(18.22)

(18.23)

(18.24)

(18.25)

(18.26)

(18.27)

326

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

As in Section 18.1.3, not every MLE can be determined by taking derivatives. Consider a continuous
analog of the example in that section, with fW (t) = 1

c on (0,c), 0 elsewhere, for some c > 0.

The likelihood is

as long as

and is 0 otherwise. So,

as before.

(cid:19)n

(cid:18) 1
c

c ≥ max

i

Wi

(cid:98)c = max

i

Wi

(18.28)

(18.29)

(18.30)

Now consider a diﬀerent problem. Suppose the random variable X is equal to 1, 2 and 3, with
probabilities c, c and 1-2c. The value c is thus a population parameter. We have a random sample
X1, ..., Xn from this population. Let’s ﬁnd the Method of Moments Estimator of c, and its bias.

First,

Thus

and so set

Next,

EX = c · 1 + c · 2 + (1 − 2c) · 3 = 3 − 3c

(18.31)

c = (3 − EX)/3

(18.32)

ˆc = (3 − X)/3

(18.33)

18.1. GENERAL METHODS OF PARAMETRIC ESTIMATION

327

Eˆc = E[(3 − X)/3)

· (3 − EX)

[3 − EX]

[3 − (3 − 3c)]

=

=

1
3
1
3
1
3
= c

=

(18.34)

(18.35)

(18.36)

(18.37)

(18.38)

On average, not too high and not too low; we say the bias is 0.

18.1.8 Asymptotic Properties

Most of the inference methods in this book use large-sample, i.e. asymptotic techniques, based on
the Central Limit Theorem. Let’s look at the asymptotic properties of Method of Moments and
Maximum Likelihood estimators.

18.1.8.1 Consistency

We say that an estimator (cid:98)ν of some parameter ν is consistent if, with probability 1,

n→∞ (cid:98)ν = ν
lim

(18.39)

where n is the sample size.
In other words, as the sample size grows, the estimator eventually
converges to the true population value. Clearly, in most situations consistency is a minimal property
we want our estimators to have.

Of course here X is a consistent estimator of EX, and (18.6) is a consistent estimator of E(X i).
So, as long as the gi() in (18.6) are continous etc., we see that the Method of Moments gives us
consistent estimators.

Showing consistency for MLEs is a little more involved. To start, recall (18.10). Its derivative with
respect to θj is

n
(cid:88)

i=1

f (cid:48)(Xi; θ1, ..., θk)
f (Xi; θ1, ..., θk)

(18.40)

328

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

For each value of θ = (θ1, ..., θk)(cid:48), this will converge to

E

(cid:19)

(cid:18) f (cid:48)(X; θ)
f (X; θ)

(18.41)

Let η denote the true population value of θ, with (cid:98)θ being the MLE. The latter is the root of (18.40),

0 =

n
(cid:88)

i=1

f (cid:48)(Xi; (cid:98)θ)
f (Xi; (cid:98)θ)

(18.42)

Moreover, in for instance the continuous case (we’ll be implicitly assuming some mathematical
conditions here, e.g. that roots are uniqe),

E

(cid:19)

(cid:18) f (cid:48)(X; η)
f (X; η)

(cid:90) ∞

=

f (cid:48)(t; η) dt

f (t; η) dt

(cid:90) ∞

−∞

=

−∞
d
dt
d
dt
= 0

=

1

(18.43)

(18.44)

(18.45)

(18.46)

Due to the uniqeness of the roots, we see that (cid:100)theta, the root of (18.40) must converge to η, the
root of (18.41).

18.1.8.2 Approximate Conﬁdence Intervals

Usually we are not satisﬁed with simply forming estimates (called point estimates). We also want
some indication of how accurate these estimates are, in the form of conﬁdence intervals (interval
estimates).

In many special cases, ﬁnding conﬁdence intervals can be done easily on an ad hoc basis. Look, for
instance, at the Method of Moments Estimator in Section 18.1.2. Our estimator (18.4) is a linear
function of X, so we easily obtain a conﬁdence interval for c from one for EX.

Another example is (18.27). Taking the limit as n → ∞ the equation shows us (and we could
verify) that

c =

1
E[ln W ]

(18.47)

18.2. BIAS AND VARIANCE

329

Deﬁning Xi = ln Wi and X = (X1 + ... + Xn)/, we can obtain a conﬁdence interval for EX in the
usual way. We then see from (18.47) that we can form a conﬁdence interval for c by simply taking
the reciprocal of each endpoint of the interval, and swapping the left and right endpoints.

What about in general? For the Method of Moments case, our estimators are functions of the
sample moments, and since the latter are formed from sums and thus are asymptotically normal,
the delta method (Section ??) can be used to show that our estimators are asymptotically normal
and to obtain asymptotic variances for them.

There is a well-developed asymptotic theory for MLEs, which under certain conditions shows asymp-
totic normality with a certain asymptotic variance, thus enabling conﬁdence intervals. The theory
also establishes that MLEs are in a certain sense optimal among all estimators. We will not pursue
this here, but will note that mle() does give standard errors for the estimates, thus enabling the
formation of conﬁdence intervals.

18.2 Bias and Variance

The notions of bias and variance play central roles in the evaluation of goodness of estimators.

18.2.1 Bias

This bowl of porridge is not too big, not too small, but just right—from the children’s story,
Goldilocks (paraphrased)

Deﬁnition 31 Suppose ˆθ is an estimator of θ. Then the bias of ˆθ is

bias = E(ˆθ) − θ

(18.48)

If the bias is 0, we say that the estimator is unbiased.

So, if ˆθ is an unbiased estimator of θ, then its average value over all possible samples is not too
high, not too low, but just right.

At ﬁrst that would seem to be a “must have” property for any estimator. But it’s very important
to note that, in spite of the pejorative-sounding name, bias is not an inherently bad property for
an estimator to have. Indeed, most good estimators are at least slightly biased.2 We’ll explore this

2Typically, though, the amount of bias will go to 0 as the sample size goes to inﬁnity. That is the case for
most consistent estimators (Sec. 18.1.2, though technically it is not implied by consistency; if a sequence of random
variables converges to a limit, their expected values do not necessarily converge to that limit, or converge at all).

330

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

in the next section.

18.2.2 Why Divide by n-1 in s2?

It should be noted that it is customary in (15.23) to divide by n-1 instead of n, for reasons that are
largely historical. Here’s the issue:

If we divide by n, as we have been doing, then it turns out that s2 is biased.

E(s2) =

n − 1
n

· σ2

(18.49)

Think about this in the Davis people example, once again in the notebook context. Remember,
here n is 1000, and each line of the notebook represents our taking a diﬀerent random sample of
1000 people. Within each line, there will be entries for W1 through W1000, the weights of our 1000
people, and for W and s. For convenience, let’s suppose we record that last column as s2 instead
of s.

Now, say we want to estimate the population variance σ2. As discussed earlier, the natural estimator
for it would be the sample variance, s2. What (18.49) says is that after looking at an inﬁnite number
of lines in the notebook, the average value of s2 would be just...a...little...bit...too...small. All the
s2 values would average out to 0.999σ2, rather than to σ2. We might say that s2 has a little bit
more tendency to underestimate σ2 than to overestimate it.

So, (18.49) implies that s2 is a biased estimator of the population variance σ2, with the amount of
bias being

n − 1
n

· σ2 − σ2 = −

1
n

· σ2

(18.50)

Let’s prove (18.49). As before, let W1, ..., Wn be a random sample from some population. So,
EWi = µ and V ar(Wi) = σ2, where again µ and σ2 are the population mean and variance.

It will be more convenient to work with ns2 than s2, since it will avoid a lot of dividing by n. So,
write

18.2. BIAS AND VARIANCE

ns2 =

=

=

n
(cid:88)

i=1
n
(cid:88)

i=1
n
(cid:88)

i=1

But that middle sum is

So,

331

(18.51)

(18.52)

(Wi − W )2

(def.)

(cid:2)(Wi − µ) + (µ − W )(cid:3)2

(alg.)

(Wi − µ)2 + 2(µ − W )

n
(cid:88)

i=1

(Wi − µ) + n(µ − W )2

(alg.)

(18.53)

n
(cid:88)

i=1

(Wi − µ) =

n
(cid:88)

i=1

Wi − nµ = nW − nµ

(18.54)

ns2 =

n
(cid:88)

i=1

(Wi − µ)2 − n(W − µ)2

(18.55)

Now let’s take the expected value of (18.55). First,

(cid:32) n
(cid:88)

(Wi − µ)2

(cid:33)

E

i=1

n
(cid:88)

i=1
n
(cid:88)

i=1
n
(cid:88)

i=1
n
(cid:88)

=

=

=

=

i=1
= nσ2

E[(Wi − µ)2]

(E is lin.)

(18.56)

E[(Wi − EWi)2]

(Wi distr. as pop.)

(18.57)

V ar(Wi)

(def. of Var())

σ2

(Wi distr. as pop.)

(18.58)

(18.59)

(18.60)

Also,

332

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

E[(W − µ)2] = E[(W − EW )2]

((15.8))

(def. of Var())

= V ar(W )
σ2
n

=

(15.13)

Applying these last two ﬁndings to (18.55), we get (18.49).

E(s2) =

n − 1
n

σ2

(18.61)

(18.62)

(18.63)

(18.64)

The earlier developers of statistics were bothered by this bias, so they introduced a “fudge factor”
by dividing by n-1 instead of n in (15.23). We will call that ˜s2:

˜s2 =

1
n − 1

n
(cid:88)

i=1

(Wi − W )2

(18.65)

This is the “classical” deﬁnition of sample variance, in which we divide by n-1 instead of n.

The R functions var() and sd() calculate the versions of s2 and s, respectively, that have a divisor
of n-1. In other words, var() calculates (18.65), and sd() computes its square root.

18.2.2.1 But in This Book, We Divide by n, not n-1 Anyway

But we will use n. After all, when n is large—which is what we are assuming by using the Central
Limit Theorem in most of the inference machinery here—it doesn’t make any appreciable diﬀerence.
Clearly it is not important in our Davis example, or our bus simulation example.

Moreover, speaking generally now rather than necessarily for the case of s2 there is no particular
reason to insist that an estimator be unbiased anyway. An alternative estimator may have a little
bias but much smaller variance, and thus might be preferable.

And anyway, even though the classical version of s2, i.e. ˜s2, is an unbiased estimator for σ2, ˜s is
not an unbiased estimator for σ, the population standard deviation (see below). Since we typically
have use for ˜s rather than for ˜s2—in (16.4), for example—you can see that unbiasedness is not such
an important property after all.

Let’s show that ˜s is biased. Recalling the shortcut formula V ar(U ) = E(U 2) − (EU )2, we have

18.2. BIAS AND VARIANCE

0 < V ar(˜s)

= E[˜s2] − [E˜s]2
= σ2 − [E˜s]2

since ˜s2 is an unbiased estimator of σ2. So,

and ˜s is biased downward.3

E˜s < σ

333

(18.66)

(18.67)

(18.68)

(18.69)

So, ˜s, the standard estimator of σ, is indeed biased, as are many other standard estimators of
various quantities. It would be futile to insist on unbiasedness as a criterion of the goodness of an
estimator.

18.2.3 Example of Bias Calculation: Max from U(0,c)

Let’s ﬁnd the bias of the estimator (18.30).

The bias is E(cid:98)c − c. To get E(cid:98)c we need the density of that estimator, which we get as follows:

P ((cid:98)c ≤ t) = P (all Wi ≤ t) (deﬁnition)
(cid:19)n

(density of Wi)

(cid:18) t
c

=

So,

Integrating against t, we ﬁnd that

(cid:98)c(t) =
f

n
cn tn−1

E(cid:98)c =

n
n + 1

c

(18.70)

(18.71)

(18.72)

(18.73)

3The reader may wonder why we have strict inequality in (18.66). But although it tis rue that Var(U) can be 0,
you’ll recall that that occurs only when U is constant. Here it would mean that ˜s is constant. This in turn would
mean that all the Wi in (18.65) are identical, with probability 1.0—which would mean the population random variable
W is constant, e.g. everyone in Davis has the same weight. So, other that in that absurd situation, the inequality in
(18.66) will indeed be strict.

334

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

So the bias is c/(n+1), not bad at all.

18.2.4 Example of Bias Calculation: Gamma Family

Let us ﬁnd via simulation, the bias of the Method of Moments Estimator of the parameter λ for
the family of gamma distributions. (The estimator was derived in Section 18.1.4.)

l a m bb i as <− function ( r , lamb , n , n r e p s ) {
lambhat <− vector ( length=n r e p s )
unfudge <− ( n−1) / n
i n 1 : n r e p s ) {
for ( i

x <− rgamma( n , shape=r , r a t e=lamb )
xbar <− mean( x )
s 2 <− var ( x ) ∗ unfudge
lambhat [ i ] <− xbar / s 2

}
mean( lambhat ) − lamb

}

18.2.5 Tradeoﬀ Between Variance and Bias

Consider a general estimator Q of some population value b. Then a common measure of the quality
(of course there are many others) of the estimator Q is the mean squared error (MSE),

E[(Q − b)2]

(18.74)

Of course, the smaller the MSE, the better.

One can break (18.74) down into variance and (squared) bias components, as follows:4

M SE(Q) = E[(Q − b)2] (deﬁnition)

(18.75)

= E[{(Q − EQ) + (EQ − b)}2] (algebra)
(18.76)
= E[(Q − EQ)2] + 2E [(Q − EQ)(EQ − b)] + E[(EQ − b)2] (E props.) (18.77)
= E[(Q − EQ)2] + E[(EQ − b)2] (factor out constant EQ-b)
(18.78)
= V ar(Q) + (EQ − b)2 (def. of Var(), fact that EQ-b is const.)
= variance + squared bias

(18.79)

(18.80)

4In reading the following derivation, keep in mind that EQ and b are constants.

18.3. SIMULTANEOUS INFERENCE METHODS

335

In other words, in discussing the accuracy of an estimator—especially in comparing two or more
candidates to use for our estimator—the average squared error has two main components, one for
variance and one for bias. In building a model, these two components are often at odds with each
other; we may be able to ﬁnd an estimator with smaller bias but more variance, or vice versa.

We also see from (18.80) that a little bias in an estimator may be quite tolerable, as long as the
variance is low. This is good, because as mentioned earlier, most estimators are in fact biased.

These point will become central in Chapters 21 and 22.

18.3 Simultaneous Inference Methods

Events of small probability happen all the time, because there are so many of them—Jim Sutton,
old Cal Poly economics professor

Suppose in our study of heights, weights and so on of people in Davis, we are interested in estimating
a number of diﬀerent quantities, with our forming a conﬁdence interval for each one. Though our
conﬁdence level for each one of them will be 95%, our overall conﬁdence level will be less than that.
In other words, we cannot say we are 95% conﬁdent that all the intervals contain their respective
population values.

In some cases we may wish to construct conﬁdence intervals in such a way that we can say we are
95% conﬁdent that all the intervals are correct. This branch of statistics is known as simultaneous
inference or multiple inference.

(The same issues apply to signiﬁcance testing, but we will focus on conﬁdence intervals here.)

In this age of Big Data, simultaneous inference is a major issue. We may have hundreds of variables,
so the chances of getting spurious results are quite high.

Usually this kind of methodology is used in the comparison of several treatments. This term
originated in the life sciences, e.g. comparing the eﬀectiveness of several diﬀerent medications for
controlling hypertension, it can be applied in any context. For instance, we might be interested in
comparing how well programmers do in several diﬀerent programming languages, say Python, Ruby
and Perl. We’d form three groups of programmers, one for each language, with say 20 programmers
per group. Then we would have them write code for a given application. Our measurement could
be the length of time T that it takes for them to develop the program to the point at which it runs
correctly on a suite of test cases.

Let Tij be the value of T for the jth programmer in the ith group, i = 1,2,3, j = 1,2,...,20. We
would then wish to compare the three “treatments,” i.e. programming languages, by estimating
µi = ETi1, i = 1,2,3. Our estimators would be Ui = (cid:80)20
j=1 Tij/20, i = 1,2,3. Since we are comparing
the three population means, we may not be satisﬁed with simply forming ordinary 95% conﬁdence

336

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

intervals for each mean. We may wish to form conﬁdence intervals which jointly have conﬁdence
level 95%.5

Note very, very carefully what this means. As usual, think of our notebook idea. Each line of the
notebook would contain the 60 observations; diﬀerent lines would involve diﬀerent sets of 60 people.
So, there would be 60 columns for the raw data, three columns for the Ui. We would also have six
more columns for the conﬁdence intervals (lower and upper bounds) for the µi. Finally, imagine
three more columns, one for each conﬁdence interval, with the entry for each being either Right or
Wrong. A conﬁdence interval is labeled Right if it really does contain its target population value,
and otherwise is labeled Wrong.

Now, if we construct individual 95% conﬁdence intervals, that means that in a given Right/Wrong
column, in the long run 95% of the entries will say Right. But for simultaneous intervals, we hope
that within a line we see three Rights, and 95% of all lines will have that property.

In our context here, if we set up our three intervals to have individual conﬁdence levels of 95%,
their simultaneous level will be 0.953 = 0.86, since the three conﬁdence intervals are independent.
Conversely, if we want a simultaneous level of 0.95, we could take each one at a 98.3% level, since
0.95

1
3 ≈ 0.983.

However, in general the intervals we wish to form will not be independent, so the above “cube root
method” would not work. Here we will give a short introduction to more general procedures.

Note that “nothing in life is free.” If we want simultaneous conﬁdence intervals, they will be wider.

Another reason to form simultaneous conﬁdence intervals is that it gives you “license to browse,”
i.e. to rummage through the data looking for interesting nuggets.

18.3.1 The Bonferonni Method

One simple approach is Bonferonni’s Inequality:

Lemma 32 Suppose A1, ..., Ag are events. Then

P (A1 or ... orAg) ≤

g
(cid:88)

i=1

P (Ai)

(18.81)

5The word may is important here. It really is a matter of philosophy as to whether one uses simultaneous inference

procedures.

18.3. SIMULTANEOUS INFERENCE METHODS

337

You can easily see this for g = 2:

P (A1 or A2) = P (A1) + P (A2) − P (A1 and A2) ≤ P (A1) + P (A2)

(18.82)

One can then prove the general case by mathematical induction.

Now to apply this to forming simultaneous conﬁdence intervals, take Ai to be the event that the ith
conﬁdence interval is incorrect, i.e. fails to include the population quantity being estimated. Then
(18.81) says that if, say, we form two conﬁdence intervals, each having individual conﬁdence level
(100-5/2)%, i.e. 97.5%, then the overall collective conﬁdence level for those two intervals is at least
95%. Here’s why: Let A1 be the event that the ﬁrst interval is wrong, and A2 is the corresponding
event for the second interval. Then

overall conf. level = P (not A1 and not A2)

= 1 − P (A1 or A2)
≥ 1 − P (A1) − P (A2)
= 1 − 0.025 − 0.025

= 0.95

(18.83)

(18.84)

(18.85)

(18.86)

(18.87)

18.3.2 Scheﬀe’s Method

The Bonferonni method is unsuitable for more than a few intervals; each one would have to have
such a high individual conﬁdence level that the intervals would be very wide. Many alternatives
exist, a famous one being Scheﬀe’s method.6 The large-sample version we’ll use here is:

Theorem 33 Suppose R = (R1, ..., Rk)(cid:48) has an approximately multivariate normal distribution,
with mean vector µ = (µi) and covariance matrix Σ = (σij). Let (cid:98)Σ be a consistent estimator of
Σ, meaning that it converges in probability to Σ as the sample size goes to inﬁnity.

For any constants c1, ..., ck, consider linear combinations of the Ri,

k
(cid:88)

i=1

ciRi

(18.88)

6The name is pronounced “sheh-FAY.”

338

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

which estimate

Form the conﬁdence intervals

k
(cid:88)

i=1

ciµi

k
(cid:88)

i=1

ciRi ±

where

(cid:113)

kχ2

α;ks(c1, ..., ck)

(18.89)

(18.90)

[s(c1, ..., ck)]2 = (c1, ..., ck)T (cid:98)Σ(c1, ..., ck)

(18.91)

and where χ2

α;k is the upper-α percentile of a chi-square distribution with k degrees of freedom.7

Then all of these intervals (for inﬁnitely many values of the ci!) have simultaneous conﬁdence level
1 − α.

By the way, if we are interested in only constructing conﬁdence intervals for contrasts, i.e. ci
having the property that Σici = 0, we the number of degrees of freedom reduces to k-1, thus
producing narrower intervals.

Just as in Section 18.2.2 we avoided the t-distribution, here we have avoided the F distribution,
which is used instead of ch-square in the “exact” form of Scheﬀe’s method.

18.3.3 Example

For example, again consider the Davis heights example in Section 16.6. Suppose we want to ﬁnd
approximate 95% conﬁdence intervals for two population quantities, µ1 and µ2. These correspond
to values of c1, c2 of (1,0) and (0,1). Since the two samples are independent, σ12 = 0. The chi-square
value is 5.99,8 so the square root in (18.90) is 3.46. So, we would compute (16.4) for X and then
for Y , but would use 3.46 instead of 1.96.

This actually is not as good as Bonferonni in this case. For Bonferonni, we would ﬁnd two 97.5%
conﬁdence intervals, which would use 2.24 instead of 1.96.

7Recall that the distribution of the sum of squares of g independent N(0,1) random variables is called chi-square

with g degrees of freedom. It is tabulated in the R statistical package’s function qchisq().

8Obtained from R via qchisq(0.95,2).

18.4. BAYESIAN METHODS

339

Scheﬀe’s method is too conservative if we just are forming a small number of intervals, but it is
great if we form a lot of them. Moreover, it is very general, usable whenever we have a set of
approximately normal estimators.

18.3.4 Other Methods for Simultaneous Inference

There are many other methods for simultaneous inference. It should be noted, though, that many
of them are limited in scope, and quite a few of them are oriented only toward signiﬁcance testing,
rather than conﬁdence intervals. In any event, they are beyond the scope of this book.

18.4 Bayesian Methods

Everyone is entitled to his own opinion, but not his own facts—Daniel Patrick Moynihan, senator
from New York, 1976-2000

Black cat, white cat, it doesn’t matter as long as it catches mice—Deng Xiaoping, when asked about
his plans to give private industry a greater role in China’s economy

Whiskey’s for drinkin’ and water’s for ﬁghtin’ over—Mark Twain, on California water jurisdiction
battles

The most controversial topic in statistics by far is that of Bayesian methods, the “California
water” of the statistics world. In fact, it is so controversial that a strident Bayesian colleague of
mine even took issue with my calling it “controversial”!

The name stems from Bayes’ Rule (Section 2.6),

P (A|B) =

P (A)P (B|A)
P (A)P (B|A) + P (not A)P (B|not A)

(18.92)

No one questions the validity of Bayes’ Rule, and thus there is no controversy regarding statistical
procedures that make use of probability calculations based on that rule. But the key word is
probability. As long as the various terms in (18.92) are real probabilities—that is, based on actual
data—there is no controversy.

But instead, the debate stems from the cases in which Bayesians replace some of the probabilities
in the theorem with “feelings,” i.e. non-probabilities, arising from what they call subjective prior
distributions. The key word is then subjective. Our section here will concern the controversy over

340

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

the use of subjective priors.9

Say we wish to estimate a population mean. Here the Bayesian analyst, before even collecting data,
says, “Well, I think the population mean could be 1.2, with probability, oh, let’s say 0.28, but on
the other hand, it might also be 0.88, with probability, well, I’ll put it at 0.49...” etc. This is the
analyst’s subjective prior distribution for the population mean. The analyst does this before even
collecting any data. Note carefully that he is NOT claiming these are real probabilities; he’s just
trying to quantify his hunches. The analyst then collects the data, and uses some mathematical
procedure that combines these “feelings” with the actual data, and which then outputs an estimate
of the population mean or other quantity of interest.

The Bayesians justify this by saying one should use all available information, even if it is just a
hunch. “The analyst is typically an expert in the ﬁeld under study. You wouldn’t want to throw
away his/her expertise, would you?” Moreover, they cite theoretical analyses that show that Bayes
estimator doing very well in terms of criteria such as mean squared error, even if the priors are not
“valid.”

The non-Bayesians, known as frequentists, on the other hand dismiss this as unscientiﬁc and
lacking in impartiality. “In research on a controversial health issue, say, you wouldn’t want the
researcher to incorporate his/her personal political biases into the number crunching, would you?”
So, the frequentists’ view is reminiscent of the Moynihan quoted above.

On the other hand, in the computer science world Bayesian estimation seems to be much less of
a controversy. Computer scientists, being engineers, tend to be interested in whether a method
seems to work, with the reasons being less important. This is the “black cat, white cat” approach.

By the way, the frequentists also point out that in the real world one must typically perform
inference (conﬁdence intervals or signiﬁcance tests), not just compute point estimates; Bayesian
methods are not really suited for inference.

Note carefully the key role of data. One might ask, for instance, “Why this sharp distinction
between the Bayesians and the frequentists over the subjectivity issue? Don’t the frequentists
make subjective decisions too?” Consider an analysis of disk drive lifetime data, for instance.
Some frequentist statistician might use a normal model, instead of, say, a gamma model. Isn’t that
subjectivity? The answer is no, because the statistician can use the data to assess the validity of
her model, employing the methods of Section 21.2.

9By contrast, there is no controversy if the prior makes use of real data. I will explain this in Section 18.4.1.1

below, but in the mean time, note that my use of the term Bayesian refers only to subjective priors.

18.4. BAYESIAN METHODS

341

18.4.1 How It Works

To introduce the idea, consider again the example of estimating p, the probability of heads for
a certain penny. Suppose we were to say—before tossing the penny even once—“I think p could
be any number, but more likely near 0.5, something like a normal distribution with mean 0.5 and
standard deviation, oh, let’s say 0.1.”

Of course, the true value of p is between 0 and 1, while the normal distribution extends from −∞
to ∞. As noted in Section 8.16, the use of normal distributions is common for modeling bounded
quantities like this one. Actually, many Bayesians prefer to use a beta distribution for the prior in
this kind of setting, as the math works out more cleanly (derivations not shown here). But let’s
stick with the normal prior here for illustration.

The prior distribution is then N (0.5, 0.12). But again, note that the Bayesians do not consider it to
be a distribution in the sense of probability. It just quantiﬁes our “gut feeling” here, our “hunch.”

Nevertheless, in terms of the mathematics involved, it’s as if the Bayesians are treating p as random,
with p’s distribution being whatever the analyst speciﬁes as the prior. Under this “random p”
assumption, the Maximum Likelihood Estimate (MLE), for instance, would change. Just as in the
frequentist approach, the data here is X, the number of heads we get from n tosses of the penny.
But in contrast to the frequentist approach, in which the likelihood would be

L =

(cid:19)

(cid:18) n
X

pX (1 − p)n−X

(18.93)

it now becomes

L =

√

1
2π 0.1

exp −0.5[(p − 0.5)/0.1]2

(cid:19)

(cid:18) n
X

pX (1 − p)n−X

(18.94)

This is basically P(A and B) = P(A) P(B|A), though using a density rather than a probability mass
function. We would then ﬁnd the value of p which maximizes L, and take that as our estimate.

A Bayesian would use Bayes’ Rule to compute the “distribution” of p given X, called the posterior
distribution. The analog of (18.92) would be (18.94) divided by the integral of (18.94) as p ranges
from 0 to 1, with the resulting quotient then being treated as a density. The (conditional) MLE
would then be the mode, i.e. the point of maximal density of the posterior distribution.

But we could use any measure of central tendency, and in fact typically the mean is used, rather
than the mode. In other words:

To estimate a population value θ, the Bayesian constructs a prior “distribution” for
θ (again, the quotation marks indicate that it is just a quantiﬁed gut feeling, rather

342

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

than a real probability distribution). Then she uses the prior together with the actual
observed data to construct the posterior distribution. Finally, she takes her estimate ˆθ
to be the mean of the posterior distribution.

Note how this procedure achieves a kind of balance between what our hunch says and what our
data say. In (18.94), suppose the mean of p is 0.5 but n = 20 and X = 12 Then the frequentist
estimator would be X/n = 0.6, while the Bayes estimator would be about 0.56. (Computation not
shown here.) So our Bayesian approach “pulled” our estimate away from the frequentist estimate,
toward our hunch that p is at or very near 0.5. This pulling eﬀect would be stronger for smaller n
or for a smaller standard deviation of the prior “distribution.”

18.4.1.1 Empirical Bayes Methods

Note carefully that if the prior distribution in our model is not subjective, but is a real distribution
veriﬁable from data, the above analysis on p would not be controversial at all. Say p does vary a
substantial amount from one penny to another, so that there is a physical distribution involved.
Suppose we have a sample of many pennies, tossing each one n times. If n is very large, we’ll get
a pretty accurate estimate of the value of p for each coin, and we can then plot these values in a
histogram and compare it to the N (0.5, 0.12) density, to check whether our prior is reasonable. This
is called an empirical Bayes model, because we can empirically estimate our prior distribution,
and check its validity. In spite of the name, frequentists would not consider this to be “Bayesian”
analysis. Note that we could also assume that p has a general N (µ, σ2) distribution, and estimate
µ and σ from the data.

18.4.2 Extent of Usage of Subjective Priors

Though many statisticians, especially academics, are staunch, often militantly proselytizing, Bayesians,
only a small minority of statisticians use the Bayesian approach in practice.

One way to see that Bayesian methodology is not mainstream is through the R programming
language. For example, as of December 2010, only about 65 of the more than 3000 packages on
CRAN, the R repository, involve Bayesian techniques. (See http://cran.r-project.org/web/
packages/tgp/index.html.) There is actually a book on the topic, Bayesian Computation with
R, by Jim Albert, Springer, 2007, and among those who use Bayesian techniques, many use R for
that purpose. However, almost all general-purpose books on R do not cover Bayesian methodology
at all.

Signiﬁcantly, even among Bayesian academics, many use frequentist methods when they work on
real, practical problems. Choose a Bayesian academic statistician at random, and you’ll likely ﬁnd
on the Web that he/she does not use Bayesian methods when working on real applications.

18.4. BAYESIAN METHODS

343

On the other hand, use of subjective priors has become very common in the computer science
research community. Papers using Bayesian methods appear frequently (no pun intended) in the
CS research literature, and “seldom is heard a discouraging word.”

18.4.3 Arguments Against Use of Subjective Priors

As noted, most professional statisticians are frequentists. In this section we will look at the argu-
ments they have against the subjective Bayesian approach.

First, it’s vital to reiterate a point made earlier:

Frequentists have no objection at all to use of prior distributions based on actual data.
They only object to use of subjective priors.

So, what is it about subjective priors that frequentists don’t like?

The ﬁrst point is that ultimately, the use of any statistical analysis is to make a decision about
something. This could be a very formal decision, such as occurs when the Food and Drug Ad-
ministration (FDA) decides whether to approve a new drug, or it could be informal, for instance
when an ordinary citizen reads a newspaper article reporting on a study analyzing data on traﬃc
accidents, and she decides what to conclude from the study.

There is nothing wrong using one’s gut feelings to make a ﬁnal decision, but it should not be
part of the mathematical analysis of the data. One’s hunches can play a role in deciding the
“preponderance of evidence,” as discussed in Section 17.11.5, but that should be kept separate
from our data analysis.

If for example the FDA’s data shows the new drug to be eﬀective, but at the same time the FDA
scientists still have their doubts, they may decide to delay approval of the drug pending further
study. So they can certainly act on their hunch, or on non-data information they have, concerning
approval of the drug. But the FDA, as a public agency, has a responsibility to the citizenry to
state what the data say, i.e. to report the frequentist estimate, rather than merely reporting a
number—the Bayesian estimate—that mixes fact and hunch.

In many if not most applications of statistics, there is a need for impartial estimates. As noted
above, even if the FDA acts on a hunch to delay approval of a drug in spite of favorable data,
the FDA owes the public (and the pharmaceutal ﬁrm) an impartial report of what the data say.
Bayesian estimation is by deﬁnition not impartial. One Bayesian statistician friend put it very well,
saying “I believe my own subjective priors, but I don’t believe those of other people.”

Furthermore, in practice we are typically interested in inference, i.e.
conﬁdence intervals and
signiﬁcance tests, rather than just point estimation. We are sampling from populations, and want

344

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

to be able to legitimately make inferences about those populations. For instance, though one can
derive a Bayesian 95% conﬁdence interval for p for our coin, it really has very little meaning, and
again is certainly not impartial.

Some Bayesians justify their approach by pointing to the problems of p-values. Yet most frequentists
also dislike p-values, and certainly use conﬁdence intervals instead. The Bayesians who say one
should uses subjective priors instead of p-values are simply setting up a sraw man, the critics say.

A common Bayesian approach concerns lower and upper bounds. The analyt feels sure that the
true population value θ is, say, between r and s. He then chooses a prior distribution on (r,s), say
uniform. Putting aside the question of the meaning of the results that ensure from the particular
choice of prior, critics may ask, “What if the frequentist estimate turns out to be less than r? It
could be a sampling artiﬁcat, but wouldn’t you want to know about it, rather than automatically
preventing such a situation?” This leads to the next section.

18.4.4 What Would You Do? A Possible Resolution

Consider the following scenario. Steven is running for president. Leo, his campaign manager, has
commissioned Lynn to conduct a poll to assess Steven’s current support among the voters. Lynn
takes her poll, and ﬁnds that 57% of those polled support Steven. But her own gut feeling as an
expert in politics, is that Steven’s support is only 48%. She then combines these two numbers in
some Bayesian fashion, and comes up with 50.2% as her estimate of Steven’s support.

So, here the frequentist estimate is 57%, while Lynn’s Bayesian estimate is 50.2%.

Lynn then gives Steven only the 50.2% ﬁgure, not reporting the value 57% number to him. Leo asks
Lynn how she arrived at that number, and she explains that she combined her prior distribution
with the data.

If you were Leo, what would you do? Consider two choices as to instructions you might give Lynn:

(a) You could say, “Lynn, I trust your judgment, so as the election campaign progresses, always

give me only your Bayesian estimate.”

(b) You might say, “Lynn, I trust your judgment, but as the election campaign progresses, always

give me both your Bayesian estimate and what the impartial data actually say.”

I believe that choice (b) is something that both the Bayesian and frequentist camps would generally
agree upon.

18.4. BAYESIAN METHODS

345

18.4.5 The Markov Chain Monte Carlo Method

The computation of posterior distributions can involve complex multiple integrals. One could use
numerical integration techniques, but again this can get complicated.

The Markov Chain Monte Carlo method approaches this problem by deﬁning a certain Markov
chain through the integration space, and then simulating that chain. The details are beyond the
scope of this book, but here is yet another application of Markov chains.

18.4.6 Further Reading

Two UCD professors, the ﬁrst current and the second former, have written interesting books about
the Bayesian approach:

• A Comparison of the Bayesian and Frequentist Approaches to Estimation, Frank Samaniego,

Springer, 2010.

• Bayesian Ideas and Data Analysis: An Introduction for Scientists and Statisticians, Chapman

& Hall, 2010.

Exercises

1. Consider raﬄe ticket example in Section 18.1.1. Suppose 500 tickets are sold, and you have data
on 8 of them. Continue to assume sampling with replacement. Consider the Maximum Likelihood
and Methods of Moments estimators.

(a) Find the probability that the MLE is exactly equal to the true value of c.

(b) Find the exact probability that the MLE is within 50 of the true value.

(c) Find the approximate probability that the Method of Moments estimator is within 50 of the

true value.

2. Suppose I = 1 or 0, with probability p and 1-p, respectively. Given I, X has a Poisson distribution
with mean λI . Suppose we have X1, ..., Xn, a random sample of size n from the (unconditional)
distribution of X. (We do not know the associated values of I, i.e. I1, ..., In.) This kind of situation
occurs in various applications. The key point is the eﬀect of the unseen variable.
In terms of
estimation, note that there are three parameters to be estimated.

346

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

(a) Set up the likelihood function, which if maximized with respect to the three parameters would

yield the MLEs for them.

(b) The words if and would in that last sentence allude to the fact that MLEs cannot be derived in
closed form. However, R’s mle() function can be used to ﬁnd their values numerically. Write
R code to do this. In other words, write a function with a single argument x, representing
the Xi, and returning the MLEs for the three parameters.

3. Find the Method of Moments and Maximum Likelihood estimators of the following parameters
in famous distribution families:

• p in the binomial family (n known)

• p in the geometric family

• µ in the normal family (σ known)

• λ in the Poisson family

4. For each of the following quantities, state whether the given estimator is unbiased in the given
context:

• ˆp, as an estimator of p, (16.5)

• ˆp(1 − ˆp), as an estimator of p(1-p), (16.11)

• ¯X − ¯Y , as an estimator of µ1 − µ2, (16.6.1)

• 1
n

(cid:80)n

i=1(Xi − µ1)2 (assuming µ1 is known), as an estimator of σ2

1, page 284

• ¯X, as an estimator of µ1, page 284 but sampling (from the population of Davis) without

replacement

5. Consider the Method of Moments Estimator ˆc in the raﬄe example, Section 18.1.1. Find the
exact value of V ar(ˆc). Use the facts that 1 + 2 + ... + r = r(r + 1)/2 and 12 + 22 + ... + r2 =
r(r + 1)(2r + 1)/6.

6. Suppose W has a uniform distribution on (-c,c), and we draw a random sample of size n,
W1, ..., Wn. Find the Method of Moments and Maximum Likelihood estimators. (Note that in the
Method of Moments case, the ﬁrst moment won’t work.)

7. An urn contains ω marbles, one of which is black and the rest being white. We draw marbles
from the urn one at a time, without replacement, until we draw the black one; let N denote the
number of draws needed. Find the Method of Moments estimator of ω based on X.

18.4. BAYESIAN METHODS

347

8. Suppose X1, ..., Xn are uniformly distributed on (0,c). Find the Method of Moments and
Maximum Likelihood estimators of c, and compare their mean squared error.

Hint: You will need the density of M = maxi Xi. Derive this by noting that M ≤ t if and only if
Xi ≤ t for all i = 1,2,...,n.

9. Add a single line to the code on page 273 that will print out the estimated value of Var(W).

10. In the raﬄe example, Section 18.1.1, ﬁnd a (1 − α)% conﬁdence interval for c based on ˇc, the
Maximum Likelihood Estimate of c.

11. In many applications, observations come in correlated clusters. For instance, we may sample
r trees at random, then s leaves within each tree. Clearly, leaves from the same tree will be more
similar to each other than leaves on diﬀerent trees.

In this context, suppose we have a random sample X1, ..., Xn, n even, such that there is correlation
within pairs. Speciﬁcally, suppose the pair (X2i+1, X2i+2) has a bivariate normal distribution with
mean (µ, µ) and covariance matrix

(cid:19)

(cid:18) 1 ρ
ρ 1

(18.95)

i = 0,...,n/2-1, with the n/2 pairs being independent. Find the Method of Moments estimators of
µ and ρ.

12. Suppose we have a random sample X1, ..., Xn from some population in which EX = µ and
V ar(X) = σ2. Let X = (X1 + ... + Xn)/n be the sample mean. Suppose the data points Xi are
collected by a machine, and that due to a defect, the machine always records the last number as
0, i.e. Xn = 0. Each of the other Xi is distributed as the population, i.e. each has mean µ and
variance σ2. Find the mean squared error of X as an estimator of µ, separating the MSE into
variance and squared bias components as in Section 18.2.

13. Suppose we have a random sample X1, ..., Xn from a population in which X is uniformly
distributed on the region (0, 1) ∪ (2, c) for some unknown c > 2. Find closed-form expressions
for the Method of Moments and Maximum Likelihood Estimators, to be denoted by T1 and T2,
respectively.

348

CHAPTER 18. GENERAL STATISTICAL ESTIMATION AND INFERENCE

Chapter 19

Mixture Models

Mixture distributions pop up in lots of places in probabilistic modeling, so we devote a chapter to
them here.

19.1 The Old Trick Coin Example, Updated

Recall the trick coin example in Section 5.1. Let’s look at a somewhat diﬀerent version.

Suppose we have a large box of coins of two types. One type has probability p of heads, the other
probability q of heads. A proportion r of the coins is of the ﬁrst type. We reach into the box, choose
a coin at random, and toss that coin 10 times, resulting in N heads. What is the distribution of N?

Since N is discrete, the distribution is described by its pmf, which is

pN (k) = r

pk(1 − p)10−k + (1 − r)

(cid:19)

(cid:18)10
k

(cid:19)

qk(1 − q)10−k

(19.1)

(cid:18)10
k

You can see why this is called a mixture model. The above pmf is a mixture of two binomial pmfs,
with mixing proportions r and 1-r.

19.2 General Mixture Distributions

The general deﬁnition is rather abstract. Let’s ﬁrst restrict it to discrete outputs:

Deﬁnition 34 Let {gt}t∈A be a collection of pmfs, and let M be a random variable taking values

349

350

CHAPTER 19. MIXTURE MODELS

in A. Consider discrete and continous cases for M, as follows. The mixture distribution of the
gt with weights given by the distribution of M is deﬁned as follows:

• M is discrete: The mixture distribution is the pmf1

h =

(cid:88)

i∈A

pM (i) gi

• M is continuous: The mixture distribution has the pmf

h(u) =

(cid:90)

t∈A

fM (t) gt(u) dt

(19.2)

(19.3)

For continuous outcomes, replace “pmf ” by “density” everywhere above.

Actually, the deﬁnition isn’t so abstract after all. A random variable Y having pmf or density h
arises ﬁrst of the random variable M, and then if M = i, getting Y randomly from the distribution
gi. A “notebook” analysis would have columns for both M and Y.

In other words, the pmf of Y is a weighted average of the gi, with weights being the pmf of M. Note
that this implies that the weights most be nonnegative and sum to 1.2

As noted, the above deﬁnition is pretty abstract, but it can be readily grasped by considering the
above trick coin example. Here,

• Y = N

• A = {0,1}

• pM (0) = pM (1) = 0.5

• g0 = the pmf for B(i,0.1)

• g1 = the pmf for B(i,0.9)

1The reader should verify that H does qualify as a pmf, i.e. it consists of numbers in [0,1] that sum to 1.
2Some readers may recognize this as a convex combination.

19.3. GENERATING RANDOM VARIATES FROM A MIXTURE DISTRIBUTION

351

Mixture distributions occur in a surprisingly wide variety of applications, and thus this material
should be mastered. It is somewhat diﬃcult at ﬁrst, but if you always keep concrete examples such
as the trick coin problem in mind, it is not bad at all.

By the way, there is nothing in the above deﬁnition that limits the Gi and H to be for scalar random
variables. They could all be bivariate, for instance (though of course they all have to be of the
same dimension.)

19.3 Generating Random Variates from a Mixture Distribution

One way to get additional insight on mixture distributions is to think about how to simulate them.
For instance, consider the trick coin problem. The following R code simulates Xi:

r x i <− function ( i ) {
m <− sample ( 0 : 1 , 1 )
p <− i f
rbinom ( 1 , i , p )

(M ==0) 0 . 1 e l s e 0 . 9

}

In general:

• We ﬁrst generate M.

• Then we generate a random variable from the pmf GM .

Various examples of mixture distributions will be presented in this chapter. But ﬁrst, we need to
develop some machinery.

19.4 A Useful Tool: the Law of Total Expectation

Let’s put the mixture distribution issue aside for a moment, to discuss a tool that is useful both in
mixture models and elsewhere.

The notion of iterated expectation, which was introduced in Sections 4.15 and 7.77, is an extremely
important tool for the analysis of mixture distributions. We ﬁrst need to extend our earlier deﬁni-
tions somewhat.

Again, this will seem rather abstract at ﬁrst, but you’ll see that it is a great convenience.

352

CHAPTER 19. MIXTURE MODELS

19.4.1 Conditional Expected Value As a Random Variable

For random variables Y and W, and an event, say W = t, the quantity E(Y|W = t) is the long-run
average of Y, among the times when W = t occurs.

Note several things about the expression E(Y|W = t):

• The item to the left of the | symbol is a random variable (Y).

• The item on the right of the | symbol is an event (W = t).

• The overall expression evaluates to a constant.

By contrast, for the quantity E(Y|W) to be deﬁned shortly, it is the case that:

• The item to the left of the | symbol is a random variable (Y).

• The item to the right of the | symbol is a random variable (W).

• The overall expression itself is a random variable, not a constant.

It will be very important to keep these diﬀerences in mind.

Consider the function g(t) deﬁned as3

g(t) = E(Y |W = t)

(19.4)

In this case, the item to the right of the | is an event, and thus g(t) is a constant (for each value of
t), not a random variable.

Deﬁnition 35 Deﬁne g() as in (19.4). Form the new random variable Q = g(W). Then the
quantity E(Y|W) is deﬁned to be Q.

(Before reading any further, re-read the two sets of bulleted items above, and make sure you
understand the diﬀerence between E(Y|W=t) and E(Y|W).)

One can view E(Y|W) as a projection in an abstract vector space. This is very elegant, and actually
aids the intuition. If (and only if) you are mathematically adventurous, read the details in Section
19.10.2.

3Of course, the t is just a placeholder, and any other letter could be used.

19.4. A USEFUL TOOL: THE LAW OF TOTAL EXPECTATION

353

19.4.2 Famous Formula: Theorem of Total Expectation

An extremely useful formula, given only scant or no mention in most undergraduate probability
courses, is

E(Y ) = E[E(Y |W )]

(19.5)

for any random variables Y and W (for which the expectations are deﬁned).

The RHS of (19.5) looks odd at ﬁrst, but it’s merely E[g(W)]; since Q = E(Y|W) is a random
variable, we can certainly ask what its expected value is.

Equation (19.5) is a bit abstract. It’s a very useful abstraction, enabling streamlined writing and
thinking about the probabilistic structures at hand. Be sure to review the intuitive explanation
following (4.68) before continuing.

19.4.3 Properties of Conditional Expectation and Variance

Conditional expected value is still expected value, and thus still has the usual properties of expected
value. In particular,

E(aU + bV | W ) = aE(U | W ) + bE(V | W )

(19.6)

for any constants a and b. The same is true for variance, e.g.

V ar(aU |W ) = a2V ar(U | W )

(19.7)

for any constants a, and

V ar(U + V | W ) = V ar(U | W ) + V ar(V | W )

(19.8)

if U and V are conditionally independent, given W.

The reason for all this is simple. Consider the discrete case, for convenience. As seen in (3.13),
E(Z) is a weighted average of the values of Z, with the weights being the values of pZ:

EZ =

(cid:88)

c

cpZ(c)

(19.9)

354

CHAPTER 19. MIXTURE MODELS

But E(Z|T ) is simply another weighted average, with the weights being the conditional distribution
of Z given T:

E(Z|T ) =

(cid:88)

c

cpZ|T (c)

Note that (continuing the discrete case)

pZ|T =r(c) =

P (Z = c, T = r)
P (T = r)

(19.10)

(19.11)

All this extends to variance, since it too is deﬁned in terms of expected value, in (3.38). So,

V ar(Z|T ) = E{[Z − E(Z|T )]2}

(19.12)

And of course, if the variables above are continuous, just replace pmfs for densities, and sums by
integrals.

19.4.4 Example: More on Flipping Coins with Bonuses

Recall the situation of Section 4.12: A game involves ﬂipping a coin k times. Each time you get
a head, you get a bonus ﬂip, not counted among the k. (But if you get a head from a bonus ﬂip,
that does not give you its own bonus ﬂip.) Let Y denote the number of heads you get among all
ﬂips, bonus or not. We’ll compute EY.

Let

W = number of heads from nonbonus ﬂips

= number of bonus ﬂips

(19.13)

(19.14)

This is a natural situation in which to try the Theorem of Total Expectation, conditioning on Y.
Reason as follows:

Y-W is the number of heads obtained from the bonus ﬂips. Given W, the random variable Y-W
has a binomial distribution with parameters W and 0.5, so

E(Y − W | W ) = 0.5W

(19.15)

19.4. A USEFUL TOOL: THE LAW OF TOTAL EXPECTATION

355

Then from (19.5),

EY = E[E(Y | W )]

= E [E({Y − W } + W | W )]

= E [E(Y − W |W ) + E(W | W )]

= E [0.5W + W ]

= 1.5EW

= 0.75k,

(19.16)

(19.17)

(19.18)

(19.19)

(19.20)

(19.21)

that last result coming from the fact that W itself has a binomial distribution with k trials and
success probability 0.5.

Now here is the point: We could have used (4.68) above. But our use of (19.5) really streamlined
things. If we had invoked (4.68), we would have had a messy sum, with binomial pmf expressions
and so on. By using (19.5), we avoided that, a big win.

19.4.5 Example: Trapped Miner

This rather whimsical example is adapted from Stochastic Processes, by Sheldon Ross, Wiley, 1996.

A miner is trapped in a mine, and has a choice of three doors. Though he doesn’t realize it, if he
chooses to exit the ﬁrst door, it will take him to safety after 2 hours of travel. If he chooses the
second one, it will lead back to the mine after 3 hours of travel. The third one leads back to the
mine after 5 hours of travel. Suppose the doors look identical, and if he returns to the mine he does
not remember which door(s) he tried earlier. What is the expected time until he reaches safety?

The answer turns out to be 10. This is no accident, as 2+3+5 = 10. This was discovered on an
intuitive basis (shown below) by UCD grad student Ahmed Ahmedin. Here is how we can derive
the answer, using (19.5):

Let Y be the time needed to reach safety, let N denote the total number of attempts the miner
makes before escaping (including the successful attempt at the end), and let Ui denote the time
spent traveling during the ith attempt, i = 1,...,N. Then

EY = E(U1 + ..., +UN )

= E [E(U1 + ... + UN |N )]

(19.22)

(19.23)

Note carefully that the expression U1 + ... + UN not only has random terms, but also the number of

356

CHAPTER 19. MIXTURE MODELS

terms, N, is random. So, technically, we cannot use (3.21) or its extension to multiple terms. But
by conditioning on N, we are back in the situation of a ﬁxed number of terms, and can proceed.

Given N, each of U1, ..., UN −1 takes on the values 3 and 5, with probability 0.5 each, while UN is
the constant 2. Thus

E(U1 + ..., +UN |N ) = (N − 1)

3 + 5
2

+ 2 = 4N − 2

(19.24)

N has a geometric distribution with success probability p = 1/3, so EN = 3. Putting all this
together, we have

EY = E(U1 + ..., +UN )

= E [E(U1 + ..., +UN |N )]
= E(4N − 2) = 10

(19.25)

(19.26)

(19.27)

If 2, 3 and 5 were replaced by a, b and c, the result would turn out to be a+b+c. Intuitively: It
takes an average of 3 attempts to escape, with each attempt having mean time of (a+b+c)/3, so
the mean time overall is a+b+c.

Here is a diﬀerent derivation (the one in Ross’ book):

Let W denote the number of the door chosen (1, 2 or 3) on the ﬁrst try. Then let us consider what
values E(Y|W) can have. If W = 1, then Y = 2, so

E(Y |W = 1) = 2

(19.28)

If W = 2, things are a bit more complicated. The miner will go on a 3-hour excursion, and then
be back in his original situation, and thus have a further expected wait of EY, since “time starts
over.” In other words,

E(Y |W = 2) = 3 + EY

(19.29)

Similarly,

E(Y |W = 3) = 5 + EY

(19.30)

19.4. A USEFUL TOOL: THE LAW OF TOTAL EXPECTATION

357

In summary, now considering the random variable E(Y|W), we have

Q = E(Y |W ) =






2, w.p. 1
3
3 + EY, w.p. 1
3
5 + EY, w.p. 1
3

(19.31)

where “w.p.” means “with probability.” So, using (19.5) we have

EY = EQ = 2 ×

1
3

+ (3 + EY ) ×

1
3

+ (5 + EY ) ×

1
3

=

10
3

+

2
3

EY

(19.32)

Equating the extreme left and extreme right ends of this series of equations, we can solve for EY,
which we ﬁnd to be 10.

It is left to the reader to see how this would change if we assume that the miner remembers which
doors he has already hit.

19.4.6 Example: Analysis of Hash Tables

(Famous example, adapted from various sources.)

Consider a database table consisting of m cells, only some of which are currently occupied. Each
time a new key must be inserted, it is used in a hash function to ﬁnd an unoccupied cell. Since
multiple keys can map to the same table cell, we may have to probe multiple times before ﬁnding
an unoccupied cell.

We wish to ﬁnd E(Y), where Y is the number of probes needed to insert a new key. One approach
to doing so would be to condition on W, the number of currently occupied cells at the time we do
a search. After ﬁnding E(Y|W), we can use the Theorem of Total Expectation to ﬁnd EY. We will
make two assumptions (to be discussed later):

(a) Given that W = k, each probe will collide with an existing cell with probability k/m, with

successive probes being independent.

(b) W is uniformly distributed on the set 1,2,...,m, i.e. P(W = k) = 1/m for each k.

To calculate E(Y|W=k), we note that given W = k, then Y is the number of independent trials
until a “success” is reached, where “success” means that our probe turns out to be to an unoccupied
cell. This is a geometric distribution, i.e.

P (Y = r|W = k) = (

r−1
)

k
m

(1 −

k
m

)

(19.33)

358

CHAPTER 19. MIXTURE MODELS

The mean of this geometric distribution is, from (4.4),

1
1 − k
m

Then

EY = E[E(Y |W )]
m−1
(cid:88)

=

E(Y |W = k)

1
m

k=1
m−1
(cid:88)

k=1

=

1
m − k

+ ... +

1
m − 1

+

1
3

du

(cid:90) m

= 1 +

1
2
1
u
1
= ln(m)

≈

(19.34)

(19.35)

(19.36)

(19.37)

(19.38)

(19.39)

(19.40)

where the approximation is something you might remember from calculus (you can picture it by
drawing rectangles to approximate the area under the curve.).

Now, what about our assumptions, (a) and (b)? The assumption in (a) of each cell having prob-
ability k/m should be reasonably accurate if k is much smaller than m, because hash functions
tend to distribute probes uniformly, and the assumption of independence of successive probes is all
right too, since it is very unlikely that we would hit the same cell twice. However, if k is not much
smaller than m, the accuracy will suﬀer.

Assumption (b) is more subtle, with diﬀering interpretations. For example, the model may concern
one speciﬁc database, in which case the assumption may be questionable. Presumably W grows
over time, in which case the assumption would make no sense—it doesn’t even have a distribution.
We could instead think of a database which grows and shrinks as time progresses. However, even
here, it would seem that W would probably oscillate around some value like m/2, rather than being
uniformly distributed as assumed here. Thus, this model is probably not very realistic. However,
even idealized models can sometimes provide important insights.

19.5. THE EM ALGORITHM

359

19.4.7 What About the Variance?

By the way, one might guess that the analog of the Theorem of Total Expectation for variance is

V ar(Y ) = E[V ar(Y |W )]

(19.41)

But this is false. Think for example of the extreme case in which Y = W. Then Var(Y|W) would
be 0, but Var(Y) should be nonzero.

The correct formula, called the Law of Total Variance, is

V ar(Y ) = E[V ar(Y |W )] + V ar[E(Y |W )]

(19.42)

Deriving this formula is easy, by simply evaluating both sides of (19.42), and using the relation
V ar(X) = E(X 2) − (EX)2. This exercise is left to the reader. See also Section 19.10.2.

19.5 The EM Algorithm

Now returning to our main topic of mixture models, let’s discuss a very common tool for ﬁtting
such models.

Consider again the example in Section 19.1. Now suppose p, q and r are all unknown, and we
wish to estimate them from data. Say we will perform the above experiment 50 times, resulting
in our data N1, ..., N50 We will estimate p, q and r by applying some method, say the Method of
Moments, Maximum Likelihood or some ad hoc method of our own, to our data. Each Ni has the
distribution seen in pN above. This is a parametric family, with 3 parameters. (If, say, r is known,
we only have a two-parameter family, and things are easier.)

So, how can we estimate those three parameters? In Chapter 18, we found some general methods
for parameter estimation. Let’s look at one of them, Maximum Likelihood Estimators (MLEs).
In (19.1), the MLE vector ((cid:98)p, (cid:98)q, (cid:98)r)(cid:48) would be found by maximizing

Πn

i=1

(cid:19)

(cid:20)

(cid:18)10
Ni

r

pNi(1 − p)10−Ni + (1 − r)

(cid:19)

qNi(1 − q)10−Ni

(cid:21)

(cid:18)10
Ni

(19.43)

After taking derivatives, etc., one would end up with a messy, nonlinear set of equations to solve.
One could try mle(), but there is a better way to get the MLE: the Expection/Maximization (EM)
algorithm. The derivation and ultimate formulas can get quite complex. Fortunately, R libraries

360

CHAPTER 19. MIXTURE MODELS

exist, such as mixtools, so you can avoid knowing all the details, as long as you understand the
basic notion of a mixture model.

19.5.1 Overall Idea

In something like (19.2), for instance, one would make initial guesses for the pM (i) and then estimate
the parameters of the gi. In the next step, we’d do the opposite—take our current guesses for the
latter parameters as known, and estimate the pM (i). Keep going until convergence.

To make things concrete, recall the trick coin example Section 19.1. But change it a little, so that
the probabilities of heads for the two coins are unknown; call them p0 (heads-light coin) and p1
(heads-heavy coin). And also suppose that the two coins are not equally likely to be chosen, so
that pM () is not known; denote P(M = 1) by q.

Suppose we have sample data, consisting of doing this experiment multiple times, say by reaching
into the box n times and then doing m ﬂips each time. We then wish to estimate 3 quantities—q
and the two pi—using our sample data.

We do so using the following iterative process. (The account here is not exactly EM, but captures
the spirit of it.) We set up initial guesses, and iterate until convergence:

• E step: Update guess for q (complicated Bayes Rule equations).

• M step: Using the new guess for q, update the gueses for the two pi.

The details are beyond the scope of this book.4

19.5.2 The mixtools Package

R’s CRAN repository of contributed software includes the mixtools library. Let’s see how to use
one of the functions in that library, normalmixEM(), which assumes that the densities in (19.3)
are from the normal distribution family.

Let’s suppose we are modeling the data as a mixture of 2 normals. So, with our observed data set,
our goal is to estimate 5 parameters from our data:

• µ1, the mean of the ﬁrst normal distribution

• µ2, the mean of the second normal distribution

4“M” in the M step refers to the Maximum Likelihood method, a special case of the material in Section 18.1.3.

19.5. THE EM ALGORITHM

361

• σ1, the mean of the second normal distribution

• σ2, the mean of the second normal distribution

• q = P(M = 1) = 1 - P(M = 2)

The basic form of the call is

normalmixEM ( x , lambda=NULL,mu=NULL, sigma=NULL, k=2)

where

• x is the data, a vector

• lambda is a vector of our initial guesses for the quantities P(M = i), of length k (recycling

will be used if it is shorter); note that these must be probabilities summing to 1

• mu is a vector of our initial guesses for the µi

• sigma is a vector of our initial guesses for the σi

• k is the number of values M can take on, e.g. 2 for a mixture of 2 normals

One can leave the initial guesses as the default NULL values, but reasonable guesses may help
convergence.

The return value will be an R list, whose components include lambda, mu and sigma, the ﬁnal
estimates of those values.

19.5.3 Example: Old Faithful Geyser

This example is taken from the online documentation in mixtools. The data concern the Old
Faithful Geyser, a built-in data set in R.

The data here consist of waiting times between eruptions. A histogram, obtained via

> h i s t ( f a i t h f u l $ w a i t i n g )

and shown in Figure 19.1, seems to suggest that the waiting time distribution is a mixture of 2
normals.

I tried initial guesses of means and standard deviations from the appearance of the histogram, and
used equal weights for my initial guesses in that aspect:

362

CHAPTER 19. MIXTURE MODELS

Figure 19.1: Old Faithful waiting times

i t e r a t i o n s= 9

> mixout <− normalmixEM ( f a i t h f u l $ w a i t i n g , lambda =0.5 ,mu=c ( 5 5 , 8 0 ) , sigma =10 ,k=2)
number o f
> s t r ( mixout )
L i s t o f 9
$ x
$ lambda
$ mu
$ sigma
$ l o g l i k
$ p o s t e r i o r
. . .

: num [ 1 : 2 7 2 ] 79 54 74 62 85 55 88 85 51 85 . . .
: num [ 1 : 2 ] 0 . 3 6 1 0 . 6 3 9
: num [ 1 : 2 ] 5 4 . 6 8 0 . 1
: num [ 1 : 2 ] 5 . 8 7 5 . 8 7
: num −1034
: num [ 1 : 2 7 2 , 1 : 2 ] 1 . 0 2 e −04 1 . 0 0 4 . 1 2 e −03 9 . 6 7 e −01 1 . 2 1 e −06

.. − attr ( ∗ , ”dimnames”)= L i s t o f 2
. .
. .

. . $ : NULL
. . $ : c h r

[ 1 : 2 ] ”comp . 1 ” ”comp . 2 ”

$ a l l . l o g l i k : num [ 1 : 1 0 ] −1085 −1051 −1037 −1034 −1034 . . .
: num 0
$ r e s t a r t s
$ f t
: c h r ”normalmixEM”
− attr ( ∗ , ” c l a s s ”)= c h r ”mixEM”

So, the estimate from EM is that about 36% of the eruptions are of Type 1, etc. Interesting, when
I tried it without my own initial guesses,

19.6. MEAN AND VARIANCE OF RANDOM VARIABLES HAVING MIXTURE DISTRIBUTIONS363

mixout <− normalmixEM ( f a i t h f u l $ w a i t i n g , k=2)

the results were the same, so it seems to be a fairly stable model here.

By the way, since we are working with a hidden variable M here—in fact, we are merely postulating
that it exists—how do we check this assumption? We’ll return to this general idea of model ﬁtting
in Chapter 21.

19.6 Mean and Variance of Random Variables Having Mixture

Distributions

Think of the random variables M and Y in the discussion following (19.3). Then EY is easy to ﬁnd
using the Law of Total Expectation:

EY = E[E(Y |M )]

(19.44)

Of course, evaluating this would require being able to compute E(Y | M), which is easy in some
cases, not so easy in others.

Also, using the Law of Total Variance, we have that

V ar(Y ) = E[V ar(Y |M )] + V ar[E(Y |M )]

(19.45)

19.7 Example: Two Kinds of Batteries

Say a factory produces two kinds of batteries. One kind has lifetime that is exponentially distributed
with mean 200 and the other’s distribution is exponential with mean 500. Suppose 60% of the
factory’s production is of the former type, with 40% being of the latter type. Let’s ﬁnd the mean
and variance of the lifetime Y of a randomly chosen battery.

Note that the distribution of Y is a mixture, in the sense of Section 19.2, in particular (19.3). Y
here is a continuous random variable, referred to in that section as the “continuous outcome” case.
In the notation there, let M be the battery type, with M being 0 or 1, for the 200-hour and 500-hour
batteries, respectively. The conditional density of Y given M = 0 is exponential with mean 200,
while given M = 1 it is exponential with mean 500. The unconditional density of Y is the mixture
of these two exponential densities, as (19.3).

We want to ﬁnd the unconditional mean and variance of Y.

364

Then

and

CHAPTER 19. MIXTURE MODELS

E(Y |M ) =

(cid:26) 200, w.p. 0.60
500, w.p. 0.40

V ar(Y |M ) =

(cid:26) 2002, w.p. 0.60
5002, w.p. 0.40

(19.46)

(19.47)

(recalling that in the exponential family, variance is the square of the mean).

We can now use the formulas in Section 19.6. Let Q1 = E(Y |M ) and Q2 = V ar(Y |M ). Then

EY = EQ1 = 0.60 × 200 + 0.40 × 500

(19.48)

and

V ar(Y ) = E(Q2) + V ar(Q1)

= (0.60 × 2002 + 0.40 × 5002) + V ar(Q1)
= (0.60 × 2002 + 0.40 × 5002) + E(Q2
= (0.60 × 2002 + 0.40 × 5002) + (0.60 × 2002 + 0.40 × 5002)

1) − (EQ1)2

−(0.60 × 200 + 0.40 × 500)2

(19.49)

(19.50)

(19.51)

(19.52)

(19.53)

(19.54)

19.8 Example: Overdispersion Models

A common model used in practice is that of overdispersion, in connection with Poisson models.

Recall the following about the Poisson distribution family:

(a) This family is often used to model counts.

(b) For any Poisson distribution, the variance equals the mean.

In some cases in which we are modeling count data, condition (b) is too constraining. We want a
“Poisson-ish” distribution in which the variance is greater than the mean, called overdispersion.

19.8. EXAMPLE: OVERDISPERSION MODELS

365

One may then try to ﬁt a mixture of several Poisson distributions, instead of a single one. This
does induce overdispersion, as we will now see.

Suppose M can equal 1,2,...,k, with probabilities p1, ..., pk that sum to 1. Say the distribution of Y
given M = i is Poisson with parameter λi. Then Y has a mixture distribution. Our goal here will
be to show that Y is overdispersed, i.e. has a large variance than mean.

By the Law of Total Expectation,

EY = E[E(Y |M )]
= E(λM )
k
(cid:88)

=

piλi

(19.55)

(19.56)

(19.57)

i=1

Note that in the above, the expression λM is a random variable, since its subscript M is random.
Indeed, it is a function of M, so Equation (3.36) then applies, yielding the ﬁnal equation. The
random variable λM takes on the values λ1, ..., λk with probabilities p1, ..., pk, hence that ﬁnal sum.

The corresponding formula for variance, (19.42), can be used to derive Var(Y).

V ar(Y ) = E[V ar(Y |M )] + V ar[E(Y |M )]
= E(λM ) + V ar(λM )
= EY + V ar(λM )

Did you notice that this last equation achieves our goal of showing overdispersed? Since

we have that

by (19.56).

V ar(λM ) > 0

V ar(Y ) > E(λM ) = EY

(19.58)

(19.59)

(19.60)

(19.61)

(19.62)

(19.63)

But let’s see just how much greater the variance is than the mean. The second term in (19.60) is
evaluated the same way as in (19.56): This is the variance of a random variable that takes on the

366

CHAPTER 19. MIXTURE MODELS

values λ1, ..., λk with probabilities p1, ..., pk, which is

k
(cid:88)

i=1

pi(λi − λ)2

λ = EλM =

k
(cid:88)

i=1

piλi

EY = λ

where

Thus

and

V ar(Y ) = λ +

k
(cid:88)

i=1

pi(λi − λ)2

So, as long as the λi are not equal, we have

(19.64)

(19.65)

(19.66)

(19.67)

V ar(Y ) > EY

(19.68)

in this Poisson mixture model, in contrast to the single-Poisson case in which Var(Y) = EY. You
can now see why the Poisson mixture model is called an overdispersion model.

So, if one has count data in which the variance is greater than the mean, one might try using this
model.

In mixing the Poissons, there is no need to restrict to discrete M. In fact, it is not hard to derive
the fact that if X has a gamma distribution with parameters r and p/(1-p) for some 0 < p < 1, and
Y given X has a Poisson distribution with mean X, then the resulting Y neatly turns out to have
a negative binomial distribution.

19.9 Example: Hidden Markov Models

The Hidden Markov Model to be introduced in Section ?? ﬁts our deﬁnition of a mixture distribu-
tion, with M being the mixing variable.

19.10. VECTOR SPACE INTERPRETATIONS (FOR THE MATHEMATICALLY ADVENTUROUS ONLY)367

According, the machinery of mixing models applies. For instance, we can use the EM algorithm to
calculate various quantities.

19.10 Vector Space Interpretations (for the mathematically ad-

venturous only)

The abstract vector space notion in linear algebra has many applications to statistics. We develop
some of that material in this section.

Let V be the set of all such random variables having ﬁnite variance and mean 0. We can set up
V as a vector space. For that, we need to deﬁne a sum and a scalar product. Deﬁne the sum of
any two vectors X and Y to be the random variable X+Y. For any constant c, the vector cX is the
random variable cX. Note that V is closed under these operations, as it must be: If X and Y both
have mean 0, then X+Y does too, and so on.

Deﬁne an inner product on this space:

(X, Y ) = Cov(X, Y ) = E(XY )

(19.69)

(Recall that Cov(X,Y) = E(XY) - EX EY, and that we are working with random variables that
have mean 0.) Thus the norm of a vector X is

||X|| = (X, X)0.5 =

(cid:112)

E(X 2) = (cid:112)V ar(X)

(19.70)

again since E(X) = 0.

19.10.1 Properties of Correlation

The famous Cauchy-Schwarz Inequality for inner products says,

Applying that here, we have

|(X, Y )| ≤ ||X|| ||Y ||

(19.71)

So, vector space theory tells us that correlations are bounded between -1 and 1.

|ρ(X, Y )| ≤ 1

(19.72)

368

CHAPTER 19. MIXTURE MODELS

Also, the Cauchy-Schwarz Inequality yields equality if and only if one vector is a scalar multiple
of the other, i.e. Y = cX for some c. When we then translate this to random variables of nonzero
means, we get Y = cX + d.

In other words, the correlation between two random variables is between -1 and 1, with equality if
and only if one is an exact linear function of the other.

19.10.2 Conditional Expectation As a Projection

For a random variable X in V, let W denote the subspace of V consisting of all functions h(X) with
mean 0 and ﬁnite variance. (Again, note that this subspace is indeed closed under vector addition
and scalar multiplication.)

Now consider any Y in V. Recall that the projection of Y onto W is the closest vector T in W to
Y, i.e. T minimizes ||Y − T ||. That latter quantity is

(cid:16)

E[(Y − T )2]

(cid:17)0.5

To ﬁnd the minimizing T, consider ﬁrst the minimization of

E[(S − c)2]

(19.73)

(19.74)

with respect to constant c for some random variable S. We already solved this problem back in
Section 3.66. The minimizing value is c = ES.

Getting back to (19.73), use the Law of Total Expectation to write

E[(Y − T )2] = E

(cid:16)

(cid:17)
E[(Y − T )2|X]

(19.75)

From what we learned with (19.74), applied to the conditional (i.e. inner) expectation in (19.75),
we see that the T which minimizes (19.75) is T = E(Y|X).

In other words, the conditional mean is a projection! Nice, but is this useful in any way? The
answer is yes, in the sense that it guides the intuition. All this is related to issues of statistical
prediction—here we would be predicting Y from X—and the geometry here can really guide our
insight. This is not very evident without getting deeply into the prediction issue, but let’s explore
some of the implications of the geometry.

For example, a projection is perpendicular to the line connecting the projection to the original

19.10. VECTOR SPACE INTERPRETATIONS (FOR THE MATHEMATICALLY ADVENTUROUS ONLY)369

vector. So

0 = (E(Y |X), Y − E(Y |X)) = Cov[E(Y |X), Y − E(Y |X)]

(19.76)

This says that the prediction E(Y|X) is uncorrelated with the prediction error, Y-E(Y|X). This
in turn has statistical importance. Of course, (19.76) could have been derived directly, but the
geometry of the vector space intepretation is what suggested we look at the quantity in the ﬁrst
place. Again, the point is that the vector space view can guide our intuition.

Simlarly, the Pythagorean Theorem holds, so

||Y ||2 = ||E(Y |X)||2 + ||Y − E(Y |X)||2

(19.77)

which means that

V ar(Y ) = V ar[E(Y |X)] + V ar[Y − E(Y |X)]

(19.78)

Equation (19.78) is a common theme in linear models in statistics, the decomposition of variance.

There is an equivalent form that is useful as well, derived as follows from the second term in (19.78).
Since

E[Y − E(Y |X)] = EY − E[E(Y |X)] = EY − EY = 0

(19.79)

we have

V ar[Y − E(Y |X)] = E (cid:2)(Y − E(Y |X))2(cid:3)

= E (cid:2)Y 2 − 2Y E(Y |X) + (E(Y |X))2(cid:3)

(19.80)

(19.81)

Now consider the middle term, E[−2Y E(Y |X)]. Conditioning on X and using the Law of Total
Expectation, we have

E[−2Y E(Y |X)] = −2E (cid:2)(E(Y |X))2(cid:3)

(19.82)

Then (19.80) becomes

370

CHAPTER 19. MIXTURE MODELS

V ar[Y − E(Y |X)] = E(Y 2) − E (cid:2)(E(Y |X))2(cid:3)

= E (cid:2)E(Y 2|X)(cid:3) − E (cid:2)(E(Y |X))2(cid:3)
= E (cid:0)E(Y 2|X) − (E(Y |X))2(cid:1)
= E [V ar(Y |X)]

(19.83)

(19.84)

(19.85)

(19.86)

the latter coming from our old friend, V ar(U ) = E(U 2) − (EU )2, with U being Y here, under
conditioning by X.

In other words, we have just derived another famous formula:

V ar(Y ) = E[V ar(Y |X)] + V ar[E(Y |X)]

(19.87)

19.11 Proof of the Law of Total Expectation

Let’s prove (19.5) for the case in which W and Y take values only in the set {1,2,3,...}. Recall that
if T is an integer-value random variable and we have some function h(), then L = h(T) is another
random variable, and its expected value can be calculated as5

E(L) =

(cid:88)

k

h(k)P (T = k)

(19.88)

In our case here, Q is a function of W, so we ﬁnd its expectation from the distribution of W:

5This is sometimes called The Law of the Unconscious Statistician, by nasty probability theorists who look down
k kP (L = k), and that (19.88) must be proven, whereas the

on statisticians. Their point is that technically EL = (cid:80)
statisticians supposedly think it’s a deﬁnition.

19.11. PROOF OF THE LAW OF TOTAL EXPECTATION

371

E(Q) =

=

=

=

=

∞
(cid:88)

i=1
∞
(cid:88)

i=1

∞
(cid:88)

i=1

∞
(cid:88)

j=1
∞
(cid:88)

g(i)P (W = i)

E(Y |W = i)P (W = i)



jP (Y = j|W = i)

 P (W = i)

P (Y = j|W = i)P (W = i)





∞
(cid:88)

j=1

∞
(cid:88)

i=1

j

jP (Y = j)

j=1
= E(Y )

E(Y ) = E[E(Y |W )]

(19.89)

In other words,

Exercises

1. In the catchup game in Section 12.2.1, let V and W denote the winnings of the two players after
only one turn. Find P (V > 0.4).

2. Suppose one keeps rolling a die. Let Sn denote the total number of dots after n rolls, mod 8, and
let T be the number of rolls needed for the event Sn = 0 to occur. Find E(T ), using an approach
like that in the “trapped miner” example in Section 19.4.5.

3. In our ordinary coins which we use every day, each one has a slightly diﬀerent probability of
heads, which we’ll call H. Say H has the distribution N (0.5, 0.032). We choose a coin from a batch
at random, then toss it 10 times. Let N be the number of heads we get. Find V ar(N ).

4. Suppose the number N of bugs in a certain number of lines of code has a Poisson distribution,
with parameter L, where L varies from one programmer to another. Show that Var(N) = EL +
Var(L).

5. This problem arises from the analysis of random graphs, which for concreteness we will treat
here as social networks such as Facebook.

In the model here, each vertex in the graph has N friends, N being a random variable with the
same distribution at every vertex. One thinks of each vertex as generating its links, unterminated,

372

CHAPTER 19. MIXTURE MODELS

i.e. not tied yet to a second vertex. Then the unterminated links of a vertex pair oﬀ at random
with those of other vertices. (Those that fail will just pair in self loops, but we’ll ignore that.)

Let M denote the number of friends a friend of mine has. That is, start at a vertex A, and follow
a link from A to another vertex, say B. M is the number of friends B has (we’ll include A in this
number).

(a) Since an unterminated link from A is more likely to pair up with a vertex that has a lot of
links, a key assumption is that P(M = k) = ck P(N = k) for some constant c. Fill in the
blank: This is an example of the setting we studied called

.

(b) Show the following relation of generating functions: gM (s) = g(cid:48)

N (s)/EN .

6. Suppose Type 1 batteries have exponentially distributed lifetimes with mean 2.0 hours, while
Type 2 battery lifetimes are exponentially distributed with mean 1.5. We have a large box con-
taining a mixture of the two types of batteries, in proportions q and 1-q. We reach into the box,
choose a battery at random, then use it. Let Y be the lifetime of the battery we choose. Use the
Law of Total Variance, (19.42), to ﬁnd V ar(Y ).

7. In the backup battery example in Section 13.3.5, ﬁnd Var(W), using the Law of Total Expecta-
tion.

8. Let X denote the number we obtain when we roll a single die once. Let GX (s) denote the
generating function of X.

(a) Find GX (s).

(b) Suppose we roll the die 5 times, and let T denote the total number of dots we get from the 5

rolls. Find GT (s).

9. Consider this model of disk seeks. For simplicity, we’ll assume a very tiny number of tracks,
3. Let X1 and X2 denote the track numbers of two successive disk requests. Each has a uniform
distribution on {1,2,3}. But given X1 = i, then X2 = i with probability 0.4, with X2 being j with
probability 0.3, j (cid:54)= i. (Convince yourself that these last two sentences are consistent with each
other.) Find the following:

(a) P (|X1 − X2| ≤ 1)

(b) E(|X1 − X2|)

(c) FX1,X2(2, 2)

19.11. PROOF OF THE LAW OF TOTAL EXPECTATION

373

10. Consider the computer worm example in Section 9.3.1. Let R denote the time it takes to go
from state 1 to state 3. Find fR(v). (Leave your answer in integral form.)

11. Suppose (X,Y) has a bivariate normal distribution, with EX = EY = 0, Var(X) = Var(Y) =
1, and ρ(X, Y ) = 0.2. Find the following, in integral forms:

(a) E(X 2 + XY 0.5)

(b) P (Y > 0.5X)

(c) FX,Y (0.6, 0.2)

12. Suppose Xi, i = 1,2,3,4,5 are independent and each have mean 0 and variance 1. Let Yi =
Xi+1 − Xi, i = 1,2,3,4. Using the material in Section 12.4, ﬁnd the covariance matrix of Y =
(Y1, Y2, Y3, Y4).

374

CHAPTER 19. MIXTURE MODELS

Chapter 20

Histograms and Beyond:
Nonparametric Density Estimation

Here we will be concerned with estimating density functions in settings in which we do not assume
our distribution belongs to some parametric model.

Why is this important? Actually, you’ve been seeing density estimates for years—except that
they’ve been called histograms—and hopefully you are convinced that histograms are indeed useful
tools for data visualization. Simply reporting the (estimated) mean and variance of a distribution
may not capture the nuances.

Moreover, density estimates can be used in clasiﬁcation problems, in which we are trying to get
the class of something, based on other variables. In a medical context, for instance, we may be
trying predict whether a patient will develop a certain disease, knowing her current test results,
family history and so on. We’ll cover such problems in Chapter 23, but for now what is important
to know is that the clasiﬁcation problem can be expressed in terms of ratios of densities.

But guess what! Histograms are actually density estimates, as we will see. And we can do better
than histograms, with more sophisticated density estimates.

20.1 Example: Baseball Player Data

In the data in Section 16.9, suppose we are looking at player’s weights W , and we are interested
in fW (182), the population density for weights, evaluated at 182. Our data is considered to be a
sample from the populatin of all major league players, past, present and future,1 and we will use

1Recall Section 15.4.

375

376CHAPTER 20. HISTOGRAMS AND BEYOND: NONPARAMETRIC DENSITY ESTIMATION

this data to develop an estimate,

for the population quantity

But how can we do this?

(cid:98)fW (142)

fW (182)

(20.1)

(20.2)

20.2 Basic Ideas in Density Estimation

Suppose we have a random sample R1, ..., Rn from a distribution FR. How can we estimate fR from
the Ri?

Recall from the ﬁrst day of your calculus course that for a function g()

and thus for small h,

g(cid:48)(x) = lim
h→0

g(x + h) − g(x)
h

g(cid:48)(x) ≈

g(x + h) − g(x)
h

Now take g() to be FR() for our random variable R above, so that

fR(t) ≈

FR(t + h) − FR(t)
h

(20.3)

(20.4)

(20.5)

We can alter this a little: Instead of looking at the interval (t, t + h), we can use (t − h, t + h):

fR(t) ≈

FR(t + h) − FR(t − h)
2h

(20.6)

That means that

20.3. HISTOGRAMS

fR(t) ≈

FR(t + h) − FR(t − h)
2h

=

=

P (R ≤ t + h) − P (R ≤ t − h)
2h

P (t − h < R ≤ t + h)
2h

377

(20.7)

(20.8)

(20.9)

if h is small. We can then form an estimate (cid:98)fR(t) by plugging in sample analogs in the right-hand
side of (20.7):

(cid:98)fR(t) =

=

#(t − h, t + h)/n
2h

#(t − h, t + h)
2hn

(20.10)

(20.11)

where the notation #(a, b) means the number of Ri in the interval (a,b).

There is an important issue of how to choose the value of h here, but let’s postpone that for now.
For the moment, let’s take

h =

maxi Ri − mini Ri
100

(20.12)

i.e. take h to be 0.01 of the range of our data.

At this point, we’d then compute (20.11) at lots of diﬀerent points t. Although it would seem that
theoretically we must compute (20.11) at inﬁnitely many such points, the graph of the function is
actually a step function. Imagine t moving to the right, starting at mini Ri. The interval (t−h, t+h)
moves along with it. Whenever the interval moves enough to the right to either pick up a new Ri
or lose one that it had had, (20.11) will change value, but not at any other time. So, we only need
to evaluate the function at about 2n values of t.

20.3 Histograms

If for some reason we really want to save on computation, let’s again say that we ﬁrst break the
interval (mini Ri, maxi Ri) into 100 subintervals of size h given by (20.12). We then compute (20.11)
only at the midpoints of those intervals, and assume that the graph of (cid:98)fR(t) is approximately
constant within each subinterval (true for small h). Do you know what we get from that? A

378CHAPTER 20. HISTOGRAMS AND BEYOND: NONPARAMETRIC DENSITY ESTIMATION

histogram! Yes, a histogram is a form of density estimation. (Usually a histogram merely displays
counts. We do so here too, but we have scaled things so that the total area under the curve is 1, a
property of densities.)

20.4 Kernel-Based Density Estimation

No matter what the interval width is, the histogram will consist of a bunch of rectangles, rather
than a curve. We can get a smoother result if we used more sophisticated methods, one of which
is called kernel-based density estimation. In base R, this is handled by the function density().

For any particular value of t, (cid:100)fR(t) above depends only on the Ri that fall into that interval. If for
instance some Ri is just barely outside the interval, it won’t count. We’ll now change that.

We need a set of weights, more precisely a weight function k, called the kernel. Any nonnegative
function which integrates to 1—i.e. a density function in its own right—will work. Our estimator
is then

(cid:99)fR(t) =

1
nh

n
(cid:88)

i=1

(cid:19)

(cid:18) t − Ri
h

k

(20.13)

To make this idea concrete, take k to be the uniform density on (-1,1), which has the value 0.5
on (-1,1) and 0 elsewhere. Then (20.13) reduces to (20.11). Note how the parameter h, called the
bandwidth, continues to control how far away from to t we wish to go for data points.

But as mentioned, what we really want is to include all data points, so we typically use a kernel
with support on all of (−∞, ∞). In R’s density() function, the default kernel is that of the N(0,1)
density.

The bandwidth h controls how much smoothing we do; smaller values of h place heavier weights
on data points near t and much lighter weights on the distant points.

There is no sureﬁre way to choose a good bandwidth. A commonly used rule of thumb is

h = 1.06 s n−1/5

(20.14)

where s is the sample standard deviation.

The default bandwidth in R is taken to the the standard deviation of k.

20.5. EXAMPLE: BASEBALL PLAYER DATA

379

20.5 Example: Baseball Player Data

Some ﬁgures are plotted below for the baseball data, introduced in Section 16.9, for player weights,
using functions in ggplot2:

• Figure 20.1 shows a histogram using the default number of bins, 30, programmed as follows

p <− g g p l o t ( b a s e b a l l )
p + geom h i s t o g r a m ( data=b a s e b a l l , a e s ( x=Weight , y = . . density . . ) )

As conceded in the documentation for geom histogram(), the default tends to be not very
good. This was the case here, with a very choppy ﬁgure.

• I then tried a binwidth of 10 pounds,

p + geom h i s t o g r a m ( data=b a s e b a l l , a e s ( x=Weight , y = . . density . . ) , b i n w i d t h =10)

This gave the much smoother plot in Figure 20.2.

• I then tried a kernel density estimate with the default bandwidth:

p + geom density ( a e s ( x=Weight ) )

The result was similar to the histogram, but smoother, which is the goal.

• Finally, I superimposed on that last plot a plot for the catchers only (the latter in red):

p + geom density ( a e s ( x=Weight ) ) +

geom density ( data=catch , a e s ( x=Weight , c o l o u r=” r e d ” ) )

As seen in Figure 20.4, the catchers tend to be a bit heavier, and have less variation than the
players in general.

20.6 More on Density Estimation in ggplot2

See Section C.6.

20.7 Bias, Variance and Aliasing

Nonparametric density estimation gives us an opportunity to apply the principles of bias from
Chapter 18.

380CHAPTER 20. HISTOGRAMS AND BEYOND: NONPARAMETRIC DENSITY ESTIMATION

Figure 20.1: Histogram estimate, default binwidth

20.7.1 Bias vs. Variance

Recall from Section 18.2.5 that for an estimator (cid:98)θ of a population quantity θ we have that an overall
measure of the accuracy of the estimator is

E[((cid:98)θ − θ)2] = bias((cid:98)θ)2 + V ar((cid:98)θ)

(20.15)

In many cases there is a tradeoﬀ between the bias and variance components here. We can have a
smaller bias, for instance, but at the expense of increased variance. This is certainly the case with
nonparametric density estimation.

As an illustration, suppose the true population density is fR(t) = 4t3 for t in (0,1), 0 elsewhere.
Let’s use (20.11):

(cid:98)fR(t) =

#(t − h, t + h))
2hn

(20.16)

20.7. BIAS, VARIANCE AND ALIASING

381

Figure 20.2: Histogram estimate, binwidth = 10

What is the bias? The numerator has a binomial distribution with n trials and success probability2

p = P (t − h < R < t + h) =

(cid:90) t+h

t−h

4u3 du = (t + h)4 − (t − h)4 = 8t3h + 8th3

(20.17)

By the binomial property, the numerator of (20.16) has expected value np, and thus

Subtracting fR(t), we have

E[(cid:99)fR(t)] =

np
2nh

= 4t3 + 4th2

bias[(cid:99)fR(t)] = 4th2

(20.18)

(20.19)

2Note in the calculation here that it doesn’t matter whether we write ≤ t + h or < t + h, since R is a continuous

random variable.

382CHAPTER 20. HISTOGRAMS AND BEYOND: NONPARAMETRIC DENSITY ESTIMATION

Figure 20.3: Kernel estimate, all players, default bandwidth

So, the smaller we set h, the smaller the bias, consistent with intuition.

How about the variance? Again using the binomial property, the variance of the numerator of
(20.16) is np(1-p), so that

V ar[[(cid:99)fR(t)] =

np(1 − p)
(2nh)2 =

np
2nh

·

1 − p
2nh

= (4t3 + 4th2) ·

1 − p
2nh

(20.20)

This matches intuition too: On the one hand, for ﬁxed h, the larger n is, the smaller the variance
of our estimator—i.e.
larger samples are better, as expected. On the other hand, the smaller we
set h, the larger the variance, because with small h there just won’t be many Ri falling into our
interval (t-h,t+h).

So, you can really see the bias-variance tradeoﬀ here, in terms of what value we choose for h.3

3You might ask about ﬁnding the h to minimize (20.15). This would not make sense in our present context, in
which we are simply assuming a known density in order to explore the bias and variance issues here. In practice, of

20.7. BIAS, VARIANCE AND ALIASING

383

Figure 20.4: Kernel estimate, all players plus catchers (red), default bandwidth

20.7.2 Aliasing

There is another issue here to recognize: The integration in (20.17) tacitly assumed that t − h > 0
and t + h < 1. But suppose we are interested in fR(1). Then the upper limit in the integral in
(20.17) will be 1, not t+h, which will approximately reduce the value of the integral by a factor of
2.

This results in strong bias near the endpoints of the support.4 Let’s illustrate this with the same
density explored in our last section.

Using the general method in Section 7.11 for generating random numbers from a speciﬁed dis-
tribution, we have that this function will generate n random numbers from the density 4t3 on
(0,1):

course, we don’t know the density—that’s why we are estimating it! However, some schemes (“plug-in” methods)
for selecting h ﬁnd a rough estimate of the density ﬁrst, and then ﬁnd the best h under the assumption that that
estimate is correct.

4Recall that this term was deﬁned in Section 7.5.

384CHAPTER 20. HISTOGRAMS AND BEYOND: NONPARAMETRIC DENSITY ESTIMATION

Figure 20.5: Example of Aliasing

f <− function ( n ) runif ( n ) ˆ 0 . 2 5

So, let’s generate some numbers and plot the density estimate:

> plot ( density ( f ( 1 0 0 0 ) ) )

The result is shown in Figure 20.5. Sure enough, the estimated density drops after about t = 0.9,
instead of continuing to rise.

20.8 Nearest-Neighbor Methods

Consider (20.11) again. We count data points that are within a ﬁxed distance from t; the number
of such points will be random. With the nearest-neighbor approach, it’s just the opposite: Now
the number will be ﬁxed, while the maximum distance from t will be random.

Speciﬁcally, at any point t we ﬁnd the k nearest Ri to t, where k is chosen by the analyst just like h
is selected in the kernel case. (And the method is are usually referred to as the k-Nearest Neighbor
method, kNN.) The estimate is now

20.9. ESTIMATING A CDF

385

Figure 20.6: Empirical cdf, toy example

(cid:98)fR(t) =

=

k/n
2 maxi |Ri − t|
k
2n maxi |Ri − t|

(20.21)

(20.22)

(20.23)

20.9 Estimating a cdf

Let’s introduce the notion of the empirical distribution function (ecdf), based on a sample
X1, ..., Xn. It is a sample estimate of a cdf, deﬁned to be the proportion of Xi that are below t in
the sample. Graphically, (cid:98)FX is a step function, with jumps at the values of the Xi.

As a small example, say n = 4 and our data are 4.8, 1.2, 2.2 and 6.1. We can plot the empirical
cdf by calling R’s ecdf() function:

> plot(ecdf(x))

The graph is in Figure 20.6. (In ggplot2, the function stat ecdf() is similar.)

02460.00.20.40.60.81.0ecdf(x)xFn(x)llll386CHAPTER 20. HISTOGRAMS AND BEYOND: NONPARAMETRIC DENSITY ESTIMATION

20.10 Hazard Function Estimation

In principle, estimating a hazard function from data should be a direct application of nonparametric
density function methods. In (??) we would estimate the numerator with a kernel-based method,
say, and the cdf in the denominator using the ecdf (Section 20.9).

However, the situation is complicated in that in many applications we have censored data, mean-
ing that not all the data is available, due to an event not yet happening.

Say we measure lifetimes of batteries, and that at the time we collect our data, some of the batteries
have died but some are still working. The problem is that we don’t know the lifetimes of the latter.
If one has run, say, for 212 hours so far, we know that it’s lifetime will be at least 212, but we don’t
know the actual value yet.

This is an advanced topic, but a good starting point would be R’s muhaz library in the CRAN
repository. See the references in the documentation.

20.11 For Further Reading

To see an example of nonparametric density estimation applied to biology, see this paper by a UCD
professor:

Kernel Methods in Line and Point Transect Sampling. Biometrics, Mack, Y. P. and P. X. Quang
(1998). 54, 609-619.

Also see All of Nonparametric Statistics, Larry Wasserman Springer, 2007.

Chapter 21

Introduction to Model Building

All models are wrong, but some are useful.—George Box1

[Mathematical models] should be made as simple as possible, but not simpler.—Albert Einstein2

Beware of geeks bearing formulas.—Warrent Buﬀett, 2009, on the role of “quants” (Wall Street
analysts who form probabilistic models for currency, bonds etc.) in the 2008 ﬁnancial collapse.

The above quote by Box says it all. Consider for example the family of normal distributions. In real
life, random variables are bounded—no person’s height is negative or greater than 500 inches—and
are inherently discrete, due to the ﬁnite precision of our measuring instruments. Thus, technically,
no random variable in practice can have an exact normal distribution. Yet the assumption of
normality pervades statistics, and has been enormously successful, provided one understands its
approximate nature.

The situation is similar to that of physics. Paraphrasing Box, we might say “Tthe physical models
used when engineers design an airplane wing are all wrong—but they are useful.” We know that
in many analyses of bodies in motion, we can neglect the eﬀect of air resistance. But we also know
that in some situations one must include that factor in our model.

So, the ﬁeld of probability and statistics is fundamentally about modeling. The ﬁeld is extremely
useful, provided the user understands the modeling issues well. For this reason, this book contains
this separate chapter on modeling issues.

1George Box (1919-) is a famous statistician, with several statistical procedures named after him.
2The reader is undoubtedly aware of Einstein’s (1879-1955) famous theories of relativity, but may not know his
connections to probability theory. His work on Brownian motion, which describes the path of a molecule as it
is bombarded by others, is probabilistic in nature, and later developed into a major branch of probability theory.
Einstein was also a pioneer in quantum mechanics, which is probabilistic as well. At one point, he doubted the
validity of quantum theory, and made his famous remark, “God does not play dice with the universe.”

387

388

CHAPTER 21.

INTRODUCTION TO MODEL BUILDING

21.1 “Desperate for Data”

Suppose we have the samples of men’s and women’s heights, X1, ..., Xn and Y1, ..., Yn. Assume
for simplicity that the variance of height is the same for each gender, σ2. The means of the two
populations are designated by µ1 and µ2.

Say we wish to guess the height of a new person who we know to be a man but for whom we know
nothing else. We do not see him, etc.

21.1.1 Known Distribution

Suppose for just a moment that we actually know the distribution of X, i.e. the population distri-
bution of male heights. What would be the best constant g to use as our guess for a person about
whom we know nothing other than gender?

Well, we might borrow from Section 18.2 and use mean squared error,

E[(g − X)2]

(21.1)

as our criterion of goodness of guessing. But we already know what the best g is, from Section
3.66: The best g is µ1. Our best guess for this unseen man’s height is the mean height of all men
in the population.

21.1.2 Estimated Mean

Of course, we don’t know µ1, but we can do the next-best thing, i.e. use an estimate of it from our
sample.

The natural choice for that estimator would be

T1 = X,

(21.2)

the mean height of men in our sample.

But what if n is really small, say n = 5? That’s awfully small. We may wish to consider adding
the women’s heights to our estimate, in order to get a larger sample. Then we would estimate µ1
by

T2 =

X + Y
2

,

(21.3)

21.1. “DESPERATE FOR DATA”

389

It may at ﬁrst seem obvious that T1 is the better estimator. Women tend to be shorter, after all,
so pooling the data from the two genders would induce a bias. On the other hand, we found in
Section 18.2 that for any estimator,

MSE = variance of the estimator + bias of the estimator2

(21.4)

In other words, some amount of bias may be tolerable, if it will buy us a subtantial reduction in
variance. After all, women are not that much shorter than men, so the bias might not be too
bad. Meanwhile, the pooled estimate should have lower variance, as it is based on 2n observations
instead of n; (15.8) indicates that.

Before continuing, note ﬁrst that T2 is based on a simpler model than is T1, as T2 ignores gender.
We thus refer to T1 as being based on the more complex model.

Which one is better? The answer will need a criterion for goodness of estimation, which we will
take to be mean squared error, MSE. So, the question becomes, which has the smaller MSE, T1 or
T2? In other words:

Which is smaller, E[(T1 − µ1)2] or E[(T2 − µ1)2]?

21.1.3 The Bias/Variance Tradeoﬀ

We could calculate MSE from scratch, but it would probably be better to make use of the work
we already went through, producing (18.80). This is especially true in that we know a lot about
variance of sample means, and we will take this route.

So, let’s ﬁnd the biases of the two estimators.

• T1

T1 is unbiased, from (15.8). So,

bias of T1 = 0

• T2

So,

E(T2) = E(0.5X + 0.5Y ) (deﬁnition)

= 0.5EX + 0.5EY (linearity of E())
= 0.5µ1 + 0.5µ2 [from (15.8)]

(21.5)

(21.6)

(21.7)

390

CHAPTER 21.

INTRODUCTION TO MODEL BUILDING

bias of T2 = (0.5µ1 + 0.5µ2) − µ1

On the other hand, T2 has a smaller variance than T1:

• T1

Recalling (15.13), we have

• T2

V ar(T1) =

σ2
n

V ar(T2) = V ar(0.5X + 0.5Y )

= 0.52V ar(X) + 0.52V ar(Y ) (properties of Var())

= 2 · 0.25 ·

σ2
n

=

σ2
2n

[from 15.13]

(21.8)

(21.9)

(21.10)

(21.11)

(21.12)

These ﬁndings are highly instructive. You might at ﬁrst think that “of course” T1 would be the
better predictor than T2. But for a small sample size, the smaller (actually 0) bias of T1 is not
enough to counteract its larger variance. T2 is biased, yes, but it is based on double the sample
size and thus has half the variance.

In light of (18.80), we see that T1, the “true” predictor, may not necessarily be the better of the
two predictors. Granted, it has no bias whereas T2 does have a bias, but the latter has a smaller
variance.

So, under what circumstances will T1 be better than T2? Let’s answer this by using (18.79):

M SE(T1) =

σ2
n

+ 02 =

σ2
n

M SE(T2) =

σ2
2n

+

(cid:18) µ1 + µ2
2

(cid:19)2

− µ1

=

σ2
2n

+

(cid:18) µ2 − µ1
2

(cid:19)2

T1 is a better predictor than T2 if (21.13) is smaller than (21.14), which is true if

(cid:19)2

(cid:18) µ2 − µ1
2

>

σ2
2n

(21.13)

(21.14)

(21.15)

21.1. “DESPERATE FOR DATA”

391

Granted, we don’t know the values of the µ1 and σ2, so in a real situation, we won’t really know
whether to use T1 or T2. But the above analysis makes the point that under some circumstances,
it really is better to pool the data in spite of bias.

21.1.4

Implications

So you can see that T1 is better only if either

• n is large enough, or

• the diﬀerence in population mean heights between men and women is large enough, or

• there is not much variation within each population, e.g. most men have very similar heights

Since that third item, small within-population variance, is rarely seen, let’s concentrate on the ﬁrst
two items. The big revelation here is that:

A more complex model is more accurate than a simpler one only if either

• we have enough data to support it, or

• the complex model is suﬃciently diﬀerent from the simpler one

In height/gender example above, if n is too small, we are “desperate for data,” and
thus make use of the female data to augment our male data. Though women tend to
be shorter than men, the bias that results from that augmentation is oﬀset by the reduction in
estimator variance that we get. But if n is large enough, the variance will be small in either model,
so when we go to the more complex model, the advantage gained by reducing the bias will more
than compensate for the increase in variance.

THIS IS AN ABSOLUTELY FUNDAMENTAL NOTION IN STATISTICS.

This was a very simple example, but you can see that in complex settings, ﬁtting too rich a model
can result in very high MSEs for the estimates. In essence, everything becomes noise. (Some people
have cleverly coined the term noise mining, a play on the term data mining.) This is the famous
overﬁtting problem.

In our unit on statistical relations, Chapter 22, we will show the results of a scary experiment
done at the Wharton School, the University of Pennsylvania’s business school. The researchers
deliberately added fake data to a prediction equation, and standard statistical software identiﬁed
it as “signiﬁcant”! This is partly a problem with the word itself, as we saw in Section 17.11, but
also a problem of using far too complex a model, as will be seen in that future unit.

392

CHAPTER 21.

INTRODUCTION TO MODEL BUILDING

Note that of course (21.15) contains several unknown population quantities. I derived it here merely
to establish a principle, namely that a more complex model may perform more poorly under some
circumstances.

It would be possible, though, to make (21.15) into a practical decision tool, by estimating the
unknown quantities, e.g. replacing µ1 by X. This then creates possible problems with conﬁdence
intervals, whose derivation did not include this extra decision step. Such estimators, termed adap-
tive, are beyond the scope of this book.

21.2 Assessing “Goodness of Fit” of a Model

Our example in Section 18.1.4 concerned how to estimate the parameters of a gamma distribution,
given a sample from the distribution. But that assumed that we had already decided that the
gamma model was reasonable in our application. Here we will be concerned with how we might
come to such decisions.

Assume we have a random sample X1, ..., Xn from a distribution having density fX . To keep things
simple, let’s suppose we are considering an exponential model.

21.2.1 The Chi-Square Goodness of Fit Test

The classic way to do this would be the Chi-Square Goodness of Fit Test. We would set

H0 : fX is a member of the exponential parametric family

(21.16)

This would involve partitioning (0, ∞) into k intervals (si−1, si) of our choice, and setting

Ni = number of Xi in (si−1, si)

(21.17)

We would then ﬁnd the Maximum Likelihood Estimate (MLE) of λ, on the assumption that the
distribution of X really is exponential. The MLE turns out to be the reciprocal of the sample mean,
i.e.

This would be considered the parameter of the “best-ﬁtting” exponential density for our data. We

(cid:98)λ = 1/X

(21.18)

21.2. ASSESSING “GOODNESS OF FIT” OF A MODEL

393

would then estimate the probabilities

pi = P [X(cid:15)(si−1, si)] = e−λsi−1 − e−λsi, i = 1, ..., k.

(21.19)

by

(cid:98)pi = e−(cid:98)λsi−1 − e−(cid:98)λsi, i = 1, ..., k.

(21.20)

Note that Ni has a binomial distribution, with n trials and success probability pi. Using this, the
expected value of ENi is estimated to be

νi = n(e−(cid:98)λsi−1 − e−(cid:98)λsi), i = 1, ..., k.

(21.21)

Our test statistic would then be

Q =

k
(cid:88)

i=1

(Ni − vi)2
vi

(21.22)

where vi is the expected value of Ni under the assumption of “exponentialness.” It can be shown
that Q is approximately chi-square distributed with k-2 degrees of freedom.3 Note that only large
values of Q should be suspicious, i.e. should lead us to reject H0; if Q is small, it indicates a good
ﬁt. If Q were large enough to be a “rare event,” say larger than χ0.95,k−2, we would decide NOT
to use the exponential model; otherwise, we would use it.

Hopefully the reader has immediately recognized the problem here. If we have a large
sample, this procedure will pounce on tiny deviations from the exponential distribution, and we
would decide not to use the exponential model—even if those deviations were quite minor. Again,
no model is 100% correct, and thus a goodness of ﬁt test will eventually tell us not to use any
model at all.

21.2.2 Kolmogorov-Smirnov Conﬁdence Bands

Again consider the problem above, in which we were assessing the ﬁt of a exponential model. In
line with our major point that conﬁdence intervals are far superior to hypothesis tests, we now
present Kolmogorov-Smirnov conﬁdence bands, which work as follows.

3We have k intervals, but the Ni must sum to n, so there are only k-1 free values. We then subtract one more

degree of freedom, having estimated the parameter λ.

394

CHAPTER 21.

INTRODUCTION TO MODEL BUILDING

Since this method relies on cdfs, recall notion of the empirical distribution function (ecdf),
Section 20.9. It is a sample estimate of a cdf, deﬁned to be the proportion of Xi that are below t
in the sample. Graphically, (cid:98)FX is a step function, with jumps at the values of the Xi.

What Kolmogorov-Smirnov does is form a conﬁdence band around the empirical cdf of a sample.
The basis for this is that the distribution of

M = max

−∞<t∞

| (cid:98)FX (t) − FX (t)|

(21.23)

is the same for all distributions having a density. This fact (whose proof is related to the
general method for simulating random variables having a given density, in Section 7.11) tells us
that, without knowing anything about the distribution of X, we can be sure that M has the same
distribution. And it turns out that

FM (1.358n−1/2) ≈ 0.95

(21.24)

Deﬁne “upper” and “lower” functions

U (t) = (cid:98)FX (t) + 1.358n−1/2, L(t) = (cid:98)FX (t) − 1.358n−1/2

(21.25)

So, what (21.23) and (21.24) tell us is

0.95 ≈ P (the curve FX is entirely between U and L)

(21.26)

So, the pair of curves, (L(t), U(t)) is called a a 95% conﬁdence band for FX .

Now suppose we wish to see how well, say, the gamma distribution family ﬁts our application.
If the band is very wide, we know we really don’t have enough data to decide much about the
distribution of X. But if the band is narrow but some member of the family is in the band or is
close to it, we would probably decide that the model is a good one. Once again, we should NOT
pounce on tiny deviations from the model.

Warning: The Kolmogorov-Smirnov procedure available in the R language performs only a hypoth-
esis test, rather than forming a conﬁdence band. In other words, it simply checks to see whether a
member of the family falls within the band. This is not what we want, because we may be perfectly
happy if a member is only near the band.

21.3. ROBUSTNESS

395

21.2.3 Less Formal Methods

Of course, another way, this one less formal, of assessing data for suitability for some model is to
plot the data in a histogram or something of that nature. In this section, let’s explore using ecdfs
for this. We can plot the ecdf against a ﬁtted model.

Let’s try this with the baseball player data (Section 16.9). Here is the code:

> l i b r a r y ( g g p l o t 2 )
> p <− g g p l o t ( bb , a e s ( Age ) )
> p + stat e c d f ( ) # p l o t e c d f
# d e f i n e a f u n c t i o n f o r
> n c d f <− function ( t ) pnorm( t , mean=mean( bb$Age ) , sd=sd ( bb$Age ) )
> p + stat e c d f ( ) + stat function ( fun=n c d f )

t h e c d f o f a f i t t e d normal d i s t r i b u t i o n

The resulting plot is in Figure 21.1. The ﬁtted curve, in red, is higher than the ecdf on the low
end, i.e. age in the early 20s. In other words, modeling age as normally distributed in the player
population overestimated the number of 20-somethings. It also overestimates the number in the
late 30s. But overall, not a bad approximation.4

Of course, we could do the same with densities. But that would meaning choose bin width, band-
width, number of nearest neighbors or something like that. BUT THERE IS NO GOOD WAY
TO CHOOSE THE BIN WIDTH OR h. Even though there is a lot of theory to suggest how
to choose the bin width or h, no method is foolproof. This is made even worse by the fact that the
theory generally has a goal of minimizing integrated mean squared error,

(cid:90) ∞

(cid:20)(cid:16)

E

−∞

(cid:98)fR(t) − fR(t)

(cid:17)2(cid:21)

dt

rather than, say, the mean squared error at a particular point of interest, v:

(cid:20)(cid:16)

E

(cid:98)fR(t) − fR(t)

(cid:17)2(cid:21)

(21.27)

(21.28)

21.3 Robustness

Traditionally, the term robust in statistics has meant resilience to violations in assumptions. For
example, in Section 16.7, we presented Student-t, a method for ﬁnding exact conﬁdence intervals for

4Don’t confuse this with the Central Limit Theorem. Here we are modeling the population itself has having a

normal distribution, not sample means from it.

396

CHAPTER 21.

INTRODUCTION TO MODEL BUILDING

Figure 21.1: Ecdf and ﬁtted cdf

means, assuming normally-distributed populations. But as noted at the outset of this chapter, no
population in the real world has an exact normal distribution. The question at hand (which we will
address below) is, does the Student-t method still give approximately correct results if the sample
population is not normal? If so, we say that Student-t is robust to the normality assumption.

Later, there was quite a lot of interest among statisticians in estimation procedures that do well
even if there are outliers in the data, i.e. erroneous observations that are in the fringes of the
sample. Such procedures are said to be robust to outliers.

Our interest here is on robustness to assumptions. Let us ﬁrst consider the Student-t example. As

21.4. REAL POPULATIONS AND CONCEPTUAL POPULATIONS

397

discussed in Section 16.7, the main statistic here is

T =

¯X − µ
√
n
s/

where µ is the population mean and s is the unbiased version of the sample variance:

(cid:115) (cid:80)n

i=1(Xi − ¯X)2
n − 1

s =

(21.29)

(21.30)

The distribution of T , under the assumption of a normal population, has been tabulated, and tables
for it appear in virtually every textbook on statistics. But what if the population is not normal,
as is inevitably the case?

The answer is that it doesn’t matter. For large n, even for samples having, say, n = 20, the distri-
bution of T is close to N(0,1) by the Central Limit Theorem regardless of whether the population
is normal.

By contrast, consider the classic procedure for performing hypothesis tests and forming conﬁdence
intervals for a population variance σ2, which relies on the statistic

K =

(n − 1)s2
σ2

(21.31)

where again s2 is the unbiased version of the sample variance. If the sampled population is normal,
then K can be shown to have a chi-square distribution with n-1 degrees of freedom. This then
sets up the tests or intervals. However, it has been shown that these procedures are not robust to
the assumption of a normal population. See The Analysis of Variance: Fixed, Random, and Mixed
Models, by Hardeo Sahai and Mohammed I. Ageel, Springer, 2000, and the earlier references they
cite, especially the pioneering work of Scheﬀe’.

21.4 Real Populations and Conceptual Populations

In our example in Section 16.3.1, we were sampling from a real population. However, in many,
probably most applications of statistics, either the population or the sampling is more conceptual.

Consider an experiment we will discuss in Section 22.2, in which we compare the programmability
of three scripting languages. (You need not read ahead.) We divide our programmers into three
groups, and assign each group to program in one of the languages. We then compare how long it
took the three groups to ﬁnish writing and debugging the code, and so on.

398

CHAPTER 21.

INTRODUCTION TO MODEL BUILDING

We think of our programmers as being a random sample from the population of all programmers,
but that is probably an idealization. We probably did NOT choose our programmers randomly;
we just used whoever we had available. But we can think of them as a “random sample” from the
rather conceptual “population” of all programmers who might work at this company.5

You can see from this that if one chooses to apply statistics carefully—which you absolutely should
do—there sometimes are some knotty problems of interpretation to think about.

Exercises

1. In our example in Section 21.1, assume µ1 = 70, µ2 = 66, σ = 4 and the distribution of height is
normal in the two populations. Suppose we are predicting the height of a man who, unknown to
us, has height 68. We hope to guess within two inches. Find P (|T1 − 68|) < 2 and P (|T2 − 68|) < 2
for various values of n.

2. In Chapter ?? we discuss simultaneous inference, the forming of conﬁdence intervals whose joint
conﬁdence level was 95% or some other target value. The Kolmogorov-Smirnov conﬁdence band
in Section 21.2.2 allows us to computer inﬁnitely many conﬁdence intervals for FX (t) at diﬀerent
values of t, at a “price” of only 1.358. Still, if we are just estimating FX (t) at a single value
of t, an individual conﬁdence interval using (16.11) would be narrower than that given to us by
Kolmogorov-Smirnov. Compare the widths of these two intervals in a situation in which the true
value of FX (t) = 0.4.

3. Say we have a random sample X1, ..., Xn from a population with mean µ and variance σ2.
The usual estimator of µ is the sample mean ¯X, but here we will use what is called a shrinkage
estimator: Our estimate of µ will be 0.9 ¯X. Find the mean squared error of this estimator, and give
an inequality (you don’t have to algebraically simplify it) that shows under what circumstances
0.9 ¯X is better than ¯X. (Strong advice: Do NOT “reinvent the wheel.” Make use of what we have
already derived.)

5You’re probably wondering why we haven’t discussed other factors, such as diﬀering levels of experience among

the programmers. This will be dealt with in our unit on regression analysis, Chapter 22.

Chapter 22

Linear Regression

In many senses, this chapter and several of the following ones form the real core of statistics,
especially from a computer science point of view.

In this chapter and the next, we are interested in relations between variables, in two main senses:

• In regression analysis, we are interested in the relation of one variable with one or more

others.

• In other kinds of analyses, such as principal components analysis, we are interested in
relations among several variables, symmetrically, i.e. not having one variable play a special
role.

Note carefully that many types of methods that go by another name are actually regression meth-
ods. Examples are the classiﬁcation problem, discriminant analysis, pattern recognition,
machine learning and so on. We’ll return to this point in Chapter 23.

22.1 The Goals: Prediction and Description

Prediction is diﬃcult, especially when it’s about the future.—Yogi Berra1

Before beginning, it is important to understand the typical goals in regression analysis.

1Yogi Berra (1925-2015) is a former baseball player and manager, famous for his malapropisms, such as “When
you reach a fork in the road, take it”; “That restaurant is so crowded that no one goes there anymore”; and “I never
said half the things I really said.”

399

400

CHAPTER 22. LINEAR REGRESSION

• Prediction: Here we are trying to predict one variable from one or more others.

• Description: Here we wish to determine which of several variables have a greater eﬀect on
(or relation to) a given variable. An important special case is that in which we are interested in
determining the eﬀect of one predictor variable, after the eﬀects of the other predictors
are removed.

Denote the predictor variables by, X (1), ..., X (r), alluding to the Prediction goal. They are
also called independent variables or explanatory variables (the latter term highlighting the
Description goal) The variable to be predicted, Y, is often called the response variable, or the
dependent variable. Note that one or more of the variables—whether the predictors or the
response variable—may be indicator variables (Section 3.9). Another name for response variables
of that type is dummy variables.

Methodology for this kind of setting is called regression analysis. If the response variable Y is an
indicator variable, the values 1 and 0 to indicate class membership, we call this the classiﬁcation
problem. (If we have more than two classes, we need several Ys.)

In the above context, we are interested in the relation of a single variable Y with other variables
X (i). But in some applications, we are interested in the more symmetric problem of relations among
variables X (i) (with there being no Y). A typical tool for the case of continuous random variables
is principal components analysis, and a popular one for the discrete case is log-linear model;
both will be discussed later in this chapter.

22.2 Example Applications: Software Engineering, Networks, Text

Mining

Example: As an aid in deciding which applicants to admit to a graduate program in computer
science, we might try to predict Y, a faculty rating of a student after completion of his/her ﬁrst
year in the program, from X (1) = the student’s CS GRE score, X (2) = the student’s undergraduate
GPA and various other variables. Here our goal would be Prediction, but educational researchers
might do the same thing with the goal of Description. For an example of the latter, see Predict-
ing Academic Performance in the School of Computing & Information Technology (SCIT), 35th
ASEE/IEEE Frontiers in Education Conference, by Paul Golding and Sophia McNamarah, 2005.

Example: In a paper, Estimation of Network Distances Using Oﬀ-line Measurements, Computer
Communications, by Prasun Sinha, Danny Raz and Nidhan Choudhuri, 2006, the authors wanted
to predict Y, the round-trip time (RTT) for packets in a network, using the predictor variables
X (1) = geographical distance between the two nodes, X (2) = number of router-to-router hops, and
other oﬄine variables. The goal here was primarily Prediction.

22.3. ADJUSTING FOR COVARIATES

401

Example: In a paper, Productivity Analysis of Object-Oriented Software Developed in a Com-
mercial Environment, Software—Practice and Experience, by Thomas E. Potok, Mladen Vouk and
Andy Rindos, 1999, the authors mainly had an Description goal: What impact, positive or nega-
tive, does the use of object-oriented programming have on programmer productivity? Here they
predicted Y = number of person-months needed to complete the project, from X (1) = size of the
project as measured in lines of code, X (2) = 1 or 0 depending on whether an object-oriented or
procedural approach was used, and other variables.

Example: Most text mining applications are classiﬁcation problems. For example, the paper
Untangling Text Data Mining, Proceedings of ACL’99, by Marti Hearst, 1999 cites, inter alia, an
application in which the analysts wished to know what proportion of patents come from publicly
funded research. They were using a patent database, which of course is far too huge to feasibly
search by hand. That meant that they needed to be able to (reasonably reliably) predict Y = 1 or
0, according to whether the patent was publicly funded from a number of X (i), each of which was
an indicator variable for a given key word, such as “NSF.” They would then treat the predicted Y
values as the real ones, and estimate their proportion from them.

Example: A major health insurance company wanted to have a tool to predict which of its
members would be likely to need hospitalization in the next year. Here Y = 1 or 0, according to
whether the patient turns out to be hospitalized, and the predictor variables were the members’
demographics, previous medical history and so on. (Interestingly, rather hiring its own data scientist
to do the analysis, the company put the problem on Kaggle, a site that holds predictive analytics
competitions, www.kaggle.com.)

22.3 Adjusting for Covariates

The ﬁrst statistical consulting engagement I ever worked involved something called adjusting for
covariates. I was retained by the Kaiser hospital chain to investigate how heart attack patients
fared at the various hospitals—did patients have a better chance to survive in some hospitals than
in others? There were four hospitals of particular interest.

I could have simply computed raw survival rates, say the proportion of patients who survive for a
month following a heart attack, and then used the methods of Section 16.4, for instance. This could
have been misleading, though, because one of the four hospitals served a largely elderly population.
A straight comparison of survival rates might then unfairly paint that particular hospital as giving
lower quality of care than the others.

So, we want to somehow adjust for the eﬀects of age. I did this by setting Y to 1 or 0, for survival,
X (1) to age, and X (2+i) to be an indicator random variable for whether the patient was at hospital
i, i = 1,2,3.2

2Note that there is no i = 4 case, since if the ﬁrst three hospital variables are all 0, that already tells us that this

402

CHAPTER 22. LINEAR REGRESSION

22.4 What Does “Relationship” Really Mean?

Consider the Davis city population example again.
In addition to the random variable W for
weight, let H denote the person’s height. Suppose we are interested in exploring the relationship
between height and weight.

As usual, we must ﬁrst ask, what does that really mean? What do we mean by “relationship”?
Clearly, there is no exact relationship; for instance, a person’s weight is not an exact function of
his/her height.

Eﬀective use of the methods to be presented here requires an understanding of what exactly is
meant by the term relationship in this context.

22.4.1 Precise Deﬁnition

Intuitively, we would guess that mean weight increases with height. To state this precisely, the key
word in the previous sentence is mean.

Take Y to be the weight W and X (1) to be the height H, and deﬁne

mW ;H (t) = E(W |H = t)

(22.1)

This looks abstract, but it is just common-sense stuﬀ. For example, mW ;H (68) would be the mean
weight of all people in the population of height 68 inches. The value of mW ;H (t) varies with t, and
we would expect that a graph of it would show an increasing trend with t, reﬂecting that taller
people tend to be heavier.

We call mW ;H the regression function of W on H. In general, mY ;X (t) means the mean of Y
among all units in the population for which X = t.3

Note the word population in that last sentence. The function m() is a population function.

So we have:

Major Point 1: When we talk about the relationship of one variable to one or more
others, we are referring to the regression function, which expresses the mean of the ﬁrst
variable as a function of the others. The key word here is mean!

patient was at the fourth hospital.

3The word “regression” is an allusion to the famous comment of Sir Francis Galton in the late 1800s regarding
“regression toward the mean.” This referred to the fact that tall parents tend to have children who are less tall—
closer to the mean—with a similar statement for short parents. The predictor variable here might be, say, the father’s
height F, with the response variable being, say, the son’s height S. Galton was saying that E(S | F) < F.

22.4. WHAT DOES “RELATIONSHIP” REALLY MEAN?

403

i ↓, j →
0
1
2

0
0.0079
0.0635
0.0476

1
0.0952
0.2857
0.0952

2
0.1429
0.1905
0.0238

3
0.0317
0.1587
0.000

Table 22.1: Bivariate pmf for the Marble Problem

22.4.2 (Rather Artiﬁcial) Example: Marble Problem

Recall the marble selection example in Section 13.1: Suppose we have a bag containing two yellow
marbles, three blue ones and four green ones. We choose four marbles from the bag at random,
without replacement. Let Y and B denote the number of yellow and blue marbles that we get.
Let’s ﬁnd mY ;B(2).

For convenience, Table 22.1 shows what we found before for P(Y = i and B = j).

Now keep in mind that since mY ;B(t) is the conditional mean of Y given B, we need to use con-
ditional probabilities to compute it. For our example here of mY ;B(2), we need the probabilities
P (Y = k|B = 2). For instance,

P (Y = 1|B = 2) =

pY,B(1, 2)
pB(2)

=

0.1905
0.1492 + 0.1905 + 0.0238

= 0.5333

(22.2)

(22.3)

(22.4)

The other conditional P (Y = k|B = 2) are then found to be 0.1429/0.3572 = 0.4001 for k = 0 and
0.0238/0.3572 = 0.0667 for k = 2.

mY ;B(2) = 0.4001 · 0 + 0.5333 · 1 + 0.0667 · 2 = 0.667

(22.5)

404

CHAPTER 22. LINEAR REGRESSION

22.5 Estimating That Relationship from Sample Data

The marble example in the last section was rather artiﬁcial, in that the exact distribution of the
variables was known (Table 22.1). In real applications, we don’t know this distribution, and must
estimate it from sample data.

As noted, mW ;H (t) is a population function, dependent on population distributions. How can we
estimate this function from sample data?

Toward that end, let’s again suppose we have a random sample of 1000 people from Davis, with

(H1, W1), ..., (H1000, W1000)

(22.6)

being their heights and weights. We again wish to use this data to estimate population values,
meaning the population regression function of W on H, mW ;H (t). But the diﬀerence here is that
we are estimating a whole function now, the whole curve mW ;H (t). That means we are estimating
inﬁnitely many values, with one mW ;H (t) value for each t.4 How do we do this?

One approach would be as follows. Say we wish to ﬁnd (cid:98)mW ;H (t) (note the hat, for “estimate of”!)
at t = 70.2. In other words, we wish to estimate the mean weight—in the population—among all
people of height 70.2. What we could do is look at all the people in our sample who are within, say,
1.0 inch of 70.2, and calculate the average of all their weights. This would then be our (cid:98)mW ;H (t).

22.5.1 Parametric Models for the Regression Function m()

There are many methods like the above (Chapter 24), but the traditional method is to choose
a parametric model for the regression function. That way we estimate only a ﬁnite number of
quantities instead of an inﬁnite number. This would be good in light of Section 21.1.

Typically the parametric model chosen is linear, i.e. we assume that mW ;H (t) is a linear function
of t:

mW ;H (t) = ct + d

(22.7)

for some constants c and d. If this assumption is reasonable—meaning that though it may not
be exactly true it is reasonably close—then it is a huge gain for us over a nonparametric model.
Do you see why? Again, the answer is that instead of having to estimate an inﬁnite number of
quantities, we now must estimate only two quantities—the parameters c and d.

4Of course, the population of Davis is ﬁnite, but there is the conceptual population of all people who could live in

Davis.

22.5. ESTIMATING THAT RELATIONSHIP FROM SAMPLE DATA

405

Equation (22.7) is thus called a parametric model of mW ;H (). The set of straight lines indexed
by c and d is a two-parameter family, analogous to parametric families of distributions, such as
the two-parametric gamma family; the diﬀerence, of course, is that in the gamma case we were
modeling a density function, and here we are modeling a regression function.

Note that c and d are indeed population parameters in the same sense that, for instance, r and λ
are parameters in the gamma distribution family. We must estimate c and d from our sample data.

So we have:

Major Point 2: The function mW ;H (t) is a population entity, so we must estimate it
from our sample data. To do this, we have a choice of either assuming that mW ;H (t)
takes on some parametric form, or making no such assumption.

If we opt for a parametric approach, the most common model is linear, i.e.
(22.7).
Again, the quantities c and d in (22.7) are population values, and as such, we must
estimate them from the data.

22.5.2 Estimation in Parametric Regression Models

So, how can we estimate these population values c and d? We’ll go into details in Section 22.10,
but here is a preview:

Using the result on page 56, together with the principle of iterated expectation, (4.68) and (7.77),
we can show that the minimum value of the quantity

(cid:104)

(W − g(H))2(cid:105)

E

overall all possible functions g(H), is attained by setting

g(H) = mW ;H (H)

(22.8)

(22.9)

In other words, mW ;H (H) is the optimal predictor of W among all possible functions of H, in the
sense of minimizing mean squared prediction error.5

Since we are assuming the model (22.7), this in turn means that:

5But if we wish to minimize the mean absolute prediction error, E (|W − g(H)|), the best function turns out to

be is g(H) = median(W |H).

406

CHAPTER 22. LINEAR REGRESSION

The quantity

(W − (uH + v))2(cid:105)
(cid:104)

E

(22.10)

is minimized by setting u = c and v = d.

This then gives us a clue as to how to estimate c and d from our data, as follows.

If you recall, in earlier chapters we’ve often chosen estimators by using sample analogs, e.g. s2 as
an estimator of σ2. Well, the sample analog of (22.10) is

1
n

n
(cid:88)

i=1

[Wi − (uHi + v)]2

(22.11)

Here (22.10) is the mean squared prediction error using u and v in the population, and (22.11) is
the mean squared prediction error using u and v in our sample. Since u = c and v = d minimize
(22.10), it is natural to estimate c and d by the u and v that minimize (22.11).

Using the “hat” notation common for estimators, we’ll denote the u and v that minimize (22.11)
by ˆc and ˆd, respectively. These numbers are then the classical least-squares estimators of the
population values c and d.

Major Point 3: In statistical regression analysis, one uses a linear model as in (22.7),
estimating the coeﬃcients by minimizing (22.11).

We will elaborate on this in Section 22.10.

22.5.3 More on Parametric vs. Nonparametric Models

Suppose we’re interested in the distribution of battery lifetimes, and we have a sample of them,
say B1, ..., B100. We wish to estimate the density of lifetimes in the population of all batteries of
this kind, fB(t).

We have two choices:

(a) We can simply plot a histogram of our data, which we found in Chapter 20 is actually a density
estimator. We are estimating inﬁnitely many population quantities, namely the heights of
the curve fB(t) at inﬁnitely many values of t.

22.6. EXAMPLE: BASEBALL DATA

407

(b) We could postulate a model for the distribution of battery lifetime, say using the gamma

family (Section 7.6.4). Then we would estimate just two parameters, λ and r.

What are the pros and cons of (a) versus (b)? The approach (a) is nice, because we don’t have
to make any assumptions about the form of the curve fB(t); we just estimate it directly, with the
histogram or other method from Chapter 20. But we are, in essence, using a ﬁnite amount of data
to estimate an inﬁnite values.

As to (b), it requires us to estimate only two parameters, which is nice. Also, having a nice,
compact parametric form for our estimate is appealing. But we have the problem of having to
make an assumption about the form of the model. We then have to see how well the model ﬁts the
data, say using the methods in Chapter 21. If it turns out not to ﬁt well, we may try other models
(e.g. from the Weibull family, not presented in this book).

The above situation is exactly parallel to what we are studying in the present chapter. The
analogy here of estimating a density function is estimating a regression function. The analog of the
histogram in (a) is the “average the people near a given height” method. The analog here of using
a parametric family of densities, such as the gamma, is using a parametric family of straight lines.
And the analog of comparing several candidate parametric density models is to compare several
regression models, e.g. adding quadratic or cubic terms (t2, t3) for height in (22.7). (See Section
22.18.3 for reading on model assessment methods.)

Most statistical analysts prefer parameteric models, but nonparametric approaches are becoming
increasingly popular.

22.6 Example: Baseball Data

Let’s do a regression analysis of weight against height in the baseball player data introduced in
Section 16.9.

22.6.1 R Code

I ran R’s lm() (“linear model”) function to perform the regression analysis:

> summary(lm( p l a y e r s $Weight ˜ p l a y e r s $ Height ) )

Call :
lm( formula = p l a y e r s $Weight ˜ p l a y e r s $ Height )

R e s i d u a l s :

408

Min

−51.988 −13.147

1Q Median
1 . 2 1 8

CHAPTER 22. LINEAR REGRESSION

3Q
1 1 . 6 9 4

Max
7 0 . 0 1 2

C o e f f i c i e n t s :

( I n t e r c e p t )
p l a y e r s $ Height
−−−
S i g n i f . codes :

Estimate Std . E r r o r t v a l u e Pr ( >| t | )
−155.092
4 . 8 4 1

1 7 . 6 9 9 −8.763
2 0 . 1 6 8

<2e −16 ∗∗∗
<2e −16 ∗∗∗

0 . 2 4 0

0

∗∗∗

0 . 0 0 1

∗∗

0 . 0 1

∗

0 . 0 5

.

0 . 1

1

R e s i d u a l

s t a n d a r d e r r o r : 1 7 . 7 8 on 1031 d e g r e e s o f

freedom

( 1 o b s e r v a t i o n d e l e t e d due t o m i s s i n g n e s s )

M u l t i p l e R−s q u a r e d : 0 . 2 8 2 9 ,
F− s t a t i s t i c : 4 0 6 . 7 on 1 and 1031 DF,

Adjusted R−s q u a r e d : 0 . 2 8 2 2

p−v a l u e : < 2 . 2 e −16

This illustrates the polymorphic nature of R: The same function can be applied to diﬀerent classes
of objects. Here summary() is such a function; another common one is plot(). So, we can call
summary() on an object of any class, at least, any one for which a summary() function has
been written. In the above R output, we called summary() on an object of type ”lm”; the R
interpreter checked the class of our object, and then accordingly called summary.lm(). But it’s
convenient for us, since we ignore all that and simply call summary() no matter what our object
is.

The call lm(players$Weight ˜players$Height) speciﬁed that my response and predictor variables
were the Weight and Height columns in the players data frame.

Note: The variables here are speciﬁed in an R data frame. One can also specify via a matrix,
which gives more ﬂexibility. For example,

lm( y ˜ x [ , c ( 2 , 3 , 7 ) ] )

to predict y from columns 2, 3 and 7 of x.

22.6.2 A Look through the Output

Next, note that lm() returns a lot of information (even more than shown above), all packed into
an object of type ”lm”.6 By calling summary() on that object, I got some of the information. It
gave me more than we’ll cover for now, but the key is that it told me that the sample estimates of

6R class names are quoted.

22.6. EXAMPLE: BASEBALL DATA

c and d are

ˆd = −155.092

ˆc = 4.841

409

(22.12)

(22.13)

In other words, our estimate for the function giving mean weight in terms of height is

mean w e i g h t = −155.092 + 4 . 8 4 1 h e i g h t

Do keep in mind that this is just an estimate, based on the sample data; it is not the population
mean-weight-versus-height function. So for example, our sample estimate is that an extra inch in
height corresponds on average to about 4.8 more pounds in weight.

We can form a conﬁdence interval to make that point clear, and get an idea of how accurate our
estimate is. The R output tells us that the standard error of ˆd is 0.240. Making use of Section
16.5, we add and subtract 1.96 times this number to ˆd to get our interval: (4.351,5.331). So, we
are about 95% conﬁdent that the true slope, c, is in that interval.

Note the column of output labled “t value.” This is again a Student-t test, with the p-value given
in the last column, labeled “P r(> |t|).” Let’s discuss this.
In the row of the summary above
regarding the Height variable, for example, we are testing

H0 : c = 0

(22.14)

R is using a Student-t distribution for this, while we have been using the the N(0,1) distribution,
based on the Central Limit Theorem approximation. For all but the smallest samples, the diﬀerence
is negligible. Consider:

Using (17.6), we would test (22.14) by forming the quotient

4.841 − 0
0.240

= 20.17

(22.15)

This is essentially the same as the 20.168 we see in the above summary. In other words, don’t
worry that R uses the Student-t distribution while we use (17.6).

At any rate, 20.17 is way larger than 1.96, thus resulting in rejection of H0. The p-value is then
the area to the left of -20.17 and to the right of 20.17, which we could compute using pnorm().
But R has already done this for us, reporting that the p-value is 2 × 10−16.

410

CHAPTER 22. LINEAR REGRESSION

What about the residuals? Here we go back to the original (Hi, Wi) data with our slope and
intercept estimates, and “predict” each Wi from the corresponding Hi. The residuals are the
resulting prediction errors. In other words, the ith residual is

Wi − ( ˆd + ˆcHi)

(22.16)

You might wonder why we would try to predict the data that we already know! But the reason for
doing this is to try to assess how well we can predict future cases, in which we know height but not
weight. If we can “predict” well in our known data, maybe we’ll do well later with unknown data.
This will turn out to be somewhat overoptimistic, we’ll see, but again, the residuals should be of
at least some value in assessing the predictive ability of our model. So, the R output reports to us
what the smallest and largest residual values were.

The R2 values will be explained in Section 22.15.4.

Finally, the F-test is a signiﬁcance test that c = d = 0. Since this book does not regard testing as
very useful, this aspect will not be pursued here.

22.7 Multiple Regression: More Than One Predictor Variable

Note that X and t could be vector-valued. For instance, we could have Y be weight and have X
be the pair

X =

(cid:16)

X (1), X (2)(cid:17)

= (H, A) = (height, age)

(22.17)

so as to study the relationship of weight with height and age. If we used a linear model, we would
write for t = (t1, t2),

mW ;H,A(t) = β0 + β1t1 + β2t2

(22.18)

In other words

mean weight = β0 + β1 height + β2 age

(22.19)

Once again, keep in mind that (22.18) and (22.19) are models for the population. We assume
that (22.18), (22.19) or whichever model we use is an exact representation of the relation in the
population. And of course, our derivations below assume our model is correct.

22.8. EXAMPLE: BASEBALL DATA (CONT’D.)

411

(It is traditional to use the Greek letter β to name the coeﬃcients in a linear regression model.)

So for instance mW ;H,A(68, 37.2) would be the mean weight in the population of all people having
height 68 and age 37.2.

In analogy with (22.11), we would estimate the βi by minimizing

1
n

n
(cid:88)

i=1

[Wi − (u + vHi + wAi)]2

(22.20)

with respect to u, v and w. The minimizing values would be denoted (cid:98)β0, (cid:98)β1 and (cid:98)β2.

We might consider adding a third predictor, gender:

mean weight = β0 + β1 height + β2 age + β3 gender

(22.21)

where gender is an indicator variable, 1 for male, 0 for female. Note that we would not have two
gender variables, since knowledge of the value of one such variable would tell us for sure what the
other one is. (It would also make a certain matrix noninvertible, as we’ll discuss later.)

22.8 Example: Baseball Data (cont’d.)

So, let’s regress weight against height and age:

> summary(lm( p l a y e r s $Weight ˜ p l a y e r s $ Height + p l a y e r s $Age ) )

Call :
lm( formula = p l a y e r s $Weight ˜ p l a y e r s $ Height + p l a y e r s $Age )

R e s i d u a l s :
Min

1Q Median
−50.794 −12.141 −0.304

3Q
1 0 . 7 3 7

Max
7 4 . 2 0 6

C o e f f i c i e n t s :

( I n t e r c e p t )
p l a y e r s $ Height
p l a y e r s $Age
−−−
S i g n i f . codes :

Estimate Std . E r r o r t v a l u e Pr ( >| t | )

−192.6564
4 . 9 7 4 6
0 . 9 6 4 7

1 7 . 8 9 0 5 −10.769 < 2 e −16 ∗∗∗
2 1 . 2 4 7 < 2 e −16 ∗∗∗
2 . 7 e −14 ∗∗∗

0 . 2 3 4 1
0 . 1 2 4 9

7 . 7 2 2

0

∗∗∗

0 . 0 0 1

∗∗

0 . 0 1

∗

0 . 0 5

.

0 . 1

1

412

CHAPTER 22. LINEAR REGRESSION

R e s i d u a l

s t a n d a r d e r r o r : 1 7 . 3 on 1030 d e g r e e s o f

freedom

( 1 o b s e r v a t i o n d e l e t e d due t o m i s s i n g n e s s )

M u l t i p l e R−s q u a r e d : 0 . 3 2 2 1 ,
F− s t a t i s t i c : 2 4 4 . 8 on 2 and 1030 DF,

Adjusted R−s q u a r e d : 0 . 3 2 0 8

p−v a l u e : < 2 . 2 e −16

So, our regression function coeﬃcient estimates are ˆβ0 = −192.6564, ˆβ1 = 4.9746 and ˆβ2 = 0.9647.
For instance, we estimate from our sample data that 10 years’ extra age results, on average, of
a weight gain about about 9.6 pounds—for people of a given height. This last condition is very
important.

22.9 Interaction Terms

Equation (22.18) implicitly says that, for instance, the eﬀect of age on weight is the same at all
height levels. In other words, the diﬀerence in mean weight between 30-year-olds and 40-year-olds
is the same regardless of whether we are looking at tall people or short people. To see that, just
plug 40 and 30 for age in (22.18), with the same number for height in both, and subtract; you get
10β2, an expression that has no height term.

That assumption is not a good one, since the weight gain in aging tends to be larger for tall people
than for short ones. If we don’t like this assumption, we can add an interaction term to (22.18),
consisting of the product of the two original predictors. Our new predictor variable X (3) is equal
to X (1)X (2), and thus our regression function is

mW ;H (t) = β0 + β1t1 + β2t2 + β3t1t2

(22.22)

If you perform the same subtraction described above, you’ll see that this more complex model does
not assume, as the old did, that the diﬀerence in mean weight between 30-year-olds and 40-year-olds
is the same regardless of we are looking at tall people or short people.

Recall the study of object-oriented programming in Section 22.1. The authors there set X (3) =
X (1)X (2). The reader should make sure to understand that without this term, we are basically
saying that the eﬀect (whether positive or negative) of using object-oriented programming is the
same for any code size.

Though the idea of adding interaction terms to a regression model is tempting, it can easily get out

of hand. If we have k basic predictor variables, then there are

potential two-way interaction

(cid:19)

(cid:18) k
2

three-way terms and so on. Unless we have a very large amount of data, we run a

terms,

(cid:19)

(cid:18) k
3

22.10. PARAMETRIC ESTIMATION OF LINEAR REGRESSION FUNCTIONS

413

big risk of overﬁtting (Section 22.15.1). And with so many interaction terms, the model would be
diﬃcult to interpret.

We can add even more interaction terms by introducing powers of variables, say the square of height
in addition to height. Then (22.22) would become

mW ;H (t) = β0 + β1t1 + β2t2 + β3t1t2 + β4t2
1

(22.23)

This square is essentially the “interaction” of height with itself. If we believe the relation between
weight and height is quadratic, this might be worthwhile, but again, this means more and more
predictors.

So, we may have a decision to make here, as to whether to introduce interaction terms. For that
matter, it may be the case that age is actually not that important, so we even might consider
dropping that variable altogether. These questions will be pursued in Section 22.15.

22.10 Parametric Estimation of Linear Regression Functions

So, how did R compute those estimated regression coeﬃcients? Let’s take a look.

22.10.1 Meaning of “Linear”

Here we model mY ;X as a linear function of X (1), ..., X (r):

mY ;X (t) = β0 + β1t(1) + ... + βrt(r)

(22.24)

Note that the term linear regression does NOT necessarily mean that the graph of the regression
function is a straight line or a plane. We could, for instance, have one predictor variable set equal
to the square of another, as in (22.23).

Instead, the word linear refers to the regression function being linear in the parameters. So, for
instance, (22.23) is a linear model; if for example we multiple β0, β1 and β2 by 8, then mA;b(s) is
multiplied by 8.

A more literal look at the meaning of “linear” comes from the matrix formulation (22.34) below.

414

CHAPTER 22. LINEAR REGRESSION

22.10.2 Random-X and Fixed-X Regression

Consider our earlier example of estimating the regression function of weight on height. To make
things, simple, say we sample only 5 people, so our data is (H1, W1), ..., (H5, W5). and we measure
height to the nearest inch.

In our “notebook” view, each line of our notebook would have 5 heights and 5 weights. Since we
would have a diﬀerent set of 5 people on each line, in the H1 column will generally have diﬀerent
values from line to line, though occasionally two consecutive lines will have the same value. H1 is
a random variable. We can regression analysis in this setting random-X regression.

We could, on the other hand, set up our sampling plan so that we sample one person each of heights
65, 67, 69, 71 and 73. These values would then stay the same from line to line. The H1 column,
for instance, would consist entirely of 65s. This is called ﬁxed-X regression.

So, the probabilistic structure of the two settings is diﬀerent. However, it turns out not to matter
much, for the following reason.

Recall that the deﬁnition of the regression function, concerns the conditional distribution of W
given H. So, our analysis below will revolve around that conditional distribution, in which case H
becomes nonrandom anyway.

22.10.3 Point Estimates and Matrix Formulation

So, how do we estimate the βi? Keep in mind that the βi are population values, which we need
to estimate them from our data. How do we do that? For instance, how did R compute the (cid:98)βi in
Section 22.6? As previewed in Section 22.5, the usual method is least-squares. Here we will go into
the details.

For concreteness, think of the baseball data, and let Hi, Ai and Wi denote the height, age and
weight of the ith player in our sample, i = 1,2,...,1033. As in (22.11), the estimation methodology
involves ﬁnding the values of ui which minimize the sum of squared diﬀerences between the actual
W values and their predicted values using the ui:

1033
(cid:88)

[Wi − (u0 + u1Hi + u2Ai)]2

i=1

(22.25)

When we ﬁnd the minimizing ui, we will set our estimates for the population regression coeﬃcients
βi in (22.24):

(cid:98)β0 = u0

(22.26)

22.10. PARAMETRIC ESTIMATION OF LINEAR REGRESSION FUNCTIONS

415

(cid:98)β1 = u1

(cid:98)β2 = u2

(22.27)

(22.28)

Obviously, this is a calculus problem. We set the partial derivatives of (22.25) with respect to the
ui to 0, giving use three linear equations in three unknowns, and then solve.

In linear algebra terms, we can write (22.25) as

(V − Qu)(cid:48)(V − Qu)

(22.29)

where

and

V =







W1
W2
...
W1033







,

u =









u0
u1
u2

Q =







A1
A2

1 H1
1 H2
...
1 H1033 A1033







(22.30)

(22.31)

(22.32)

Note the need for the 1s column. To see this, do the multiplication Qu, say for n = 3, and note
the that u0 term does emerge, as we see it must in (22.25).

Whatever vector u minimizes (22.29), we set our estimated β vector (cid:98)β = ( (cid:98)β0, (cid:98)β1, (cid:98)β2)(cid:48) to that u.

Then it can be shown that, after all the partial derivatives are taken and set to 0, the solution is

ˆβ = (Q(cid:48)Q)−1Q(cid:48)V

(22.33)

For the general case (22.24) with n observations (n = 1033 in the baseball data), the matrix Q has
n rows and r+1 columns. Column i+1 has the sample data on predictor variable i.

416

CHAPTER 22. LINEAR REGRESSION

Keep in mind that all of this is conditional on the X (i)
j
in (22.1), our assumption is that

, i.e. conditional on Q. As seen for example

E(V |Q) = Qβ

(22.34)

This is the standard approach, especially since there is the case of nonrandom X. Thus we will later
get conditional conﬁdence intervals, which is ﬁne. To avoid clutter, I will sometimes not show the
conditioning explicitly, and thus for instance will write, for example, Cov(V) instead of Cov(V|Q).

It turns out that ˆβ is an unbiased estimate of β:7

E ˆβ = E[(Q(cid:48)Q)−1Q(cid:48)V ] (22.33)

= (Q(cid:48)Q)−1Q(cid:48)EV (linearity of E())
= (Q(cid:48)Q)−1Q(cid:48) · Qβ (22.34)
= β

(22.35)

(22.36)

(22.37)

(22.38)

In some applications, we assume there is no constant term β0 in (22.24). This means that our Q
matrix no longer has the column of 1s on the left end, but everything else above is valid.

22.10.4 Approximate Conﬁdence Intervals

As noted, R gives you standard errors for the estimated coeﬃcients. Where do they come from?

As usual, we should not be satisﬁed with just point estimates, in this case the (cid:98)βi. We need an
indication of how accurate they are, so we need conﬁdence intervals. In other words, we need to
use the (cid:98)βi to form conﬁdence intervals for the βi.

For instance, recall the study on object-oriented programming in Section 22.1. The goal there was
primarily Description, speciﬁcally assessing the impact of OOP. That impact is measured by β2.
Thus, we want to ﬁnd a conﬁdence interval for β2.

Equation (22.33) shows that the (cid:98)βi are sums of the components of V, i.e. the Wj. So, the Central
Limit Theorem implies that the (cid:98)βi are approximately normally distributed. That in turn means
that, in order to form conﬁdence intervals, we need standard errors for the βi. How will we get
them?

Note carefully that so far we have made NO assumptions other than (22.24). Now, though, we

7Note that here we are taking the expected value of a vector, as in Chapter 12.

22.10. PARAMETRIC ESTIMATION OF LINEAR REGRESSION FUNCTIONS

417

need to add an assumption:8

V ar(Y |X = t) = σ2

(22.39)

for all t. Note that this and the independence of the sample observations (e.g. the various people
sampled in the Davis height/weight example are independent of each other) implies that

Cov(V |Q) = σ2I

(22.40)

where I is the usual identiy matrix (1s on the diagonal, 0s oﬀ diagonal).

Be sure you understand what this means. In the Davis weights example, for instance, it means
that the variance of weight among 72-inch tall people is the same as that for 65-inch-tall people.
That is not quite true—the taller group has larger variance—but research into this has found that
as long as the discrepancy is not too bad, violations of this assumption won’t aﬀect things much.

We can derive the covariance matrix of ˆβ as follows. Again to avoid clutter, let B = (Q(cid:48)Q)−1. A
theorem from linear algebra says that Q’Q is symmetric and thus B is too. Another theorem says
that for any conformable matrices U and V, then (UV)’ = V’U’. Armed with that knowledge, here
we go:

Cov( ˆβ) = Cov(BQ(cid:48)V ) ((22.33))

= BQ(cid:48)Cov(V )(BQ(cid:48))(cid:48) (12.54)
= BQ(cid:48)σ2I(BQ(cid:48))(cid:48) (22.40)
= σ2BQ(cid:48)QB (lin. alg.)
= σ2(Q(cid:48)Q)−1 (def. of B)

(22.41)

(22.42)

(22.43)

(22.44)

(22.45)

Whew! That’s a lot of work for you, if your linear algebra is rusty. But it’s worth it, because
(22.45) now gives us what we need for conﬁdence intervals. Here’s how:

First, we need to estimate σ2. Recall ﬁrst that for any random variable U, V ar(U ) = E[(U −EU )2],
we have

8Actually, we could derive some usable, though messy,standard errors without this assumption.

418

CHAPTER 22. LINEAR REGRESSION

σ2 = V ar(Y |X = t)

= V ar(Y |X (1) = t1, ..., X (r) = tr)
= E (cid:2){Y − mY ;X (t)}2(cid:3)
= E (cid:2)(Y − β0 − β1t1 − ... − βrtr)2(cid:3)

(22.46)

(22.47)

(22.48)

(22.49)

Thus, a natural estimate for σ2 would be the sample analog, where we replace E() by averaging
over our sample, and replace population quantities by sample estimates:

s2 =

1
n

n
(cid:88)

i=1

(Yi − ˆβ0 − ˆβ1X (1)

i − ... − ˆβrX (r)

i

)2

(22.50)

As in Chapter 18, this estimate of σ2 is biased, and classicly one divides by n-(r+1) instead of n.
But again, it’s not an issue unless r+1 is a substantial fraction of n, in which case you are overﬁtting
and shouldn’t be using a model with so large a value of r.

So, the estimated covariance matrix for ˆβ is

(cid:100)Cov( ˆβ) = s2(Q(cid:48)Q)−1

(22.51)

The diagonal elements here are the squared standard errors (recall that the standard error of an
estimator is its estimated standard deviation) of the βi. (And the oﬀ-diagonal elements are the
estimated covariances between the βi.) Since the ﬁrst standard errors you ever saw, in Section 16.5,
included factors like 1/

n, you might wonder why you don’t see such a factor in (22.51).

√

The answer is that such a factor is essentially there, in the following sense. Q’Q consists of various
sums of products of the X values, and the larger n is, then the larger the elements of Q’Q are. So,
(Q(cid:48)Q)−1 already has something like a “1/n” factor in it.

R’s vcov() function, applied to the output of lm() will give you (22.51) (subject to a bias correction
factor that we’ll discuss in Section 22.18.1, but that we’ll dismiss as unimportant.

22.11 Example: Baseball Data (cont’d.)

Let us use vcov() to obtain the estimated covariance matrix of the vector (cid:98)β for our baseball data.

> lmout <− lm( p l a y e r s $Weight ˜ p l a y e r s $ Height + p l a y e r s $Age )
> vcov ( lmout )

22.12. DUMMY VARIABLES

419

( I n t e r c e p t ) p l a y e r s $ Height
( I n t e r c e p t )
3 2 0 . 0 7 0 6 2 2 3
p l a y e r s $ Height −4.1020471
−0.6077188
p l a y e r s $Age

p l a y e r s $Age
−4.102047105 −0.607718793
0 . 0 0 2 1 6 0 1 2 8
0 . 0 1 5 6 0 7 3 9 0

0 . 0 5 4 8 1 7 2 1 1
0 . 0 0 2 1 6 0 1 2 8

The ﬁrst command saved the output of lm() in a variable that we chose to name lmout; we then
called vcov() on that object.

For instance, the estimated variance of (cid:98)β1 is 0.054817211. Actually, we already knew this, because
the standard error of (cid:98)β1 was reported earlier to be 0.2341, and 0.23412 = 0.054817211.

But now we can ﬁnd more. Say we wish to compute a conﬁdence interval for the population mean
weight of players who are 72 inches tall and age 30. That quantity is equal to

β0 + 72β1 + 30β2 = (1, 72, 30)β

(22.52)

which we will estimate by

Thus, using (12.56), we have

(1, 72, 30) (cid:98)β

(cid:100)V ar( (cid:98)β0 + 72 (cid:98)β1 + 30 (cid:98)β2) = (1, 72, 30)A









1
72
30

(22.53)

(22.54)

where A is the matrix in the R output above.

The square root of this quantity is the standard error of (cid:98)β0 + 72 (cid:98)β1 + 30 (cid:98)β2. We add and subtract
1.96 times that square root to (cid:98)β0 + 72 (cid:98)β1 + 30 (cid:98)β2, and then have an approximate 95% conﬁdence
interval for the population mean weight of players who are 72 inches tall and age 30.

22.12 Dummy Variables

Recall our example in Section 22.2 concerning a study of software engineer productivity. To review,
the authors of the study predicted Y = number of person-months needed to complete the project,
from X (1) = size of the project as measured in lines of code, X (2) = 1 or 0 depending on whether
an object-oriented or procedural approach was used, and other variables.

420

CHAPTER 22. LINEAR REGRESSION

As mentioned at the time, X (2) is an indicator variable, often called a “dummy” variable in the
regression context.

Let’s generalize that a bit. Suppose we are comparing two diﬀerent object-oriented languages, C++
and Java, as well as the procedural language C. Then we could change the deﬁnition of X (2) to
have the value 1 for C++ and 0 for non-C++, and we could add another variable, X (3), which has
the value 1 for Java and 0 for non-Java. Use of the C language would be implied by the situation
X (2) = X (3) = 0.

Note that we do NOT want to represent Language by a single value having the values 0, 1 and 2,
which would imply that C has, for instance, double the impact of Java.

22.13 Example: Baseball Data (cont’d.)

Let’s now bring the Position variable into play. First, what is recorded for that variable?

> l e v e l s ( p l a y e r s $ P o s i t i o n )
[ 1 ] ” Catcher ”
[ 4 ] ” O u t f i e l d e r ”
[ 7 ] ” S h o r t s t o p ”

” D e s i g n a t e d H i t t e r ” ” F i r s t Baseman”
” R e l i e f P i t c h e r ”
” S t a r t i n g P i t c h e r ”

” Second Baseman”
” Third Baseman”

So, all the outﬁeld positions have been simply labeled “Outﬁelder,” though pitchers have been
separated into starters and relievers.

Technically, this variable, players$Position, is an R factor. This is a fancy name for an integer
vector with labels, such that the labels are normally displayed rather than the codes. So actually
catchers are coded 1, designated hitters 2, ﬁrst basemen 3 and so on, but in displaying the data
frame, the labels are shown rather than the codes.

The designated hitters are rather problematic, as they only exist in the American League, not the
National League. Let’s restrict our analysis to the other players:

> nondh <− p l a y e r s [ p l a y e r s $ P o s i t i o n != ” D e s i g n a t e d H i t t e r ” , ]
> nrow( p l a y e r s )
[ 1 ] 1034
> nrow( nondh )
[ 1 ] 1016

This requires some deconstruction. The expression players$Position !=”Designated Hitter” gives
us a vector of True and False values. Then players [ players$Position !=”Designated Hitter”,]
consists of all rows of players corresponding to a True value. Result: We’ve deleted the designated
hitters, assigning the result to nondh. A comparison of numbers of rows show that there were only
18 designated hitters in the data set anyway.

22.13. EXAMPLE: BASEBALL DATA (CONT’D.)

421

Let’s consolidate into four kinds of positions: inﬁelders, outﬁelders, catchers and pitchers. First,
switch to numeric codes, in a vector we’ll name poscodes:

> p o s c o d e s <− as . integer ( nondh$ P o s i t i o n )
> head ( p o s c o d e s )
[ 1 ] 1 1 1 3 3 6
> head ( nondh$ P o s i t i o n )
[ 1 ] Catcher
[ 6 ] Second Baseman
9 L e v e l s : Catcher D e s i g n a t e d H i t t e r F i r s t Baseman . . . Third Baseman

F i r s t Baseman

Catcher

Catcher

F i r s t Baseman

Now consolidate into three dummy variables:

> i n f l d <− as . integer ( p o s c o d e s==3 | p o s c o d e s==6 | p o s c o d e s==7 | p o s c o d e s ==9)
> o u t f l d <− as . integer ( p o s c o d e s ==4)
> p i t c h e r <− as . integer ( p o s c o d e s==5 | p o s c o d e s ==8)

Again, remember that catchers are designated via the other three dummies being 0.

So, let’s run the regression:

> summary(lm( nondh$Weight ˜ nondh$ Height + nondh$Age + i n f l d + o u t f l d + p i t c h e r ) )

Call :
lm( formula = nondh$Weight ˜ nondh$ Height + nondh$Age + i n f l d +

o u t f l d + p i t c h e r )

R e s i d u a l s :
Min

1Q Median
−49.669 −12.083 −0.386

3Q
1 0 . 4 1 0

Max
7 5 . 0 8 1

C o e f f i c i e n t s :

Estimate Std . E r r o r t v a l u e Pr ( >| t | )

( I n t e r c e p t ) −193.2557
5 . 1 0 7 5
nondh$ Height
0 . 8 8 4 4
nondh$Age
−7.7727
i n f l d
−6.1398
o u t f l d
−8.3017
p i t c h e r
−−−
S i g n i f . codes :

0

∗∗∗

1 9 . 0 1 2 7 −10.165 < 2 e −16 ∗∗∗
0 . 2 5 2 0
2 0 . 2 7 0 < 2 e −16 ∗∗∗
7 . 0 6 8 2 . 9 3 e −12 ∗∗∗
0 . 1 2 5 1
2 . 2 9 1 7 −3.392 0 . 0 0 0 7 2 2 ∗∗∗
2 . 3 1 6 9 −2.650 0 . 0 0 8 1 7 5 ∗∗
2 . 1 4 8 1 −3.865 0 . 0 0 0 1 1 8 ∗∗∗

0 . 0 0 1

∗∗

0 . 0 1

∗

0 . 0 5

.

0 . 1

1

R e s i d u a l

s t a n d a r d e r r o r : 1 7 . 1 on 1009 d e g r e e s o f

freedom

422

CHAPTER 22. LINEAR REGRESSION

( 1 o b s e r v a t i o n d e l e t e d due t o m i s s i n g n e s s )

M u l t i p l e R−s q u a r e d : 0 . 3 2 8 6 ,
F− s t a t i s t i c : 9 8 . 7 6 on 5 and 1009 DF,

Adjusted R−s q u a r e d : 0 . 3 2 5 3

p−v a l u e : < 2 . 2 e −16

The estimated coeﬃcients for the position variables are all negative. For example, for a given height
and age, pitchers are on average about 8.3 pounds lighter than catchers, while outﬁelders are about
6.1 pounds lighter than catchers.

What if we want to compare inﬁelders and outﬁelders, say form a conﬁdence interval for β3 − β4?
Then we’d do a computation like (22.54), with a vector (0,0,0,1,-1,0) instead of (1,72,30).

22.14 What Does It All Mean?—Eﬀects of Adding Predictors

Keep in mind the twin goals of regression analysis, Prediction and Description. In applications
in which Description is the goal, we are keenly interested in the signs and magnitudes of the
βi,9 especially their signs. We do need to be careful, just as we saw in Section 17.11; the sign
of a coeﬃcient usually won’t be of much interest if the magnitude is near 0. Subject to that
caution, discussion of regression results often centers on the sign of a coeﬃcient: Is there a positive
relationship between the response variable and a predictor, holding the other predictors constant?

That latter phrase, holding the other predictors constant, is key. Recall for example our example
at the start of this chapter on a study of the eﬀects of using the object-oriented programming
paradigm. Does OOP help or hurt productivity? Since longer programs often take longer to
write, the researchers wanted to correct for program length, so they used that as a predictor, in
addition to a dummy variable for OOP. In other words, they wanted to know the impact of OOP
on productivity, holding program length constant.

So, in studying a predictor variable, it may matter greatly which other predictors one is using.
Let’s examine the baseball data in this regard.

In Section 22.8, we added the age variable as our second predictor, height being the ﬁrst. This
resulted in the coeﬃcient of height increasing from 4.84 to 4.97. This is not a large change, but
what does it tell us? It suggests that older players tend to be shorter. No, this doesn’t mean the
players shrink with age—shrinkage does occur among the elderly, but likely not here—but rather
that other phenomena are at work. It could be, for instance, that shorter players tend to have
longer careers. This in turn might be due to a situation in which certain positions whose players
tend to be tall have shorter careers. All of this could be explored, say starting with calculating the
correlation between height and age.10

9As estimated from the (cid:98)βi.
10The R function cor() computes the correlation between its ﬁrst two arguments if they are vectors.

22.14. WHAT DOES IT ALL MEAN?—EFFECTS OF ADDING PREDICTORS

423

To develop some intuition on this, consider the following artiﬁcial population of eight people:

gender height weight
150
165
175
185
120
130
140
155

male
male
male
male
female
female
female
female

66
70
70
70
66
66
66
70

Here is the weight-height relationship for men, i.e. the mean weight for each height group:

men:

height mean weight
150
175

66
70

women:

height mean weight
130
155

66
70

βheight = (175 − 150)/4 = 6.25

(22.55)

βheight = (155 − 130)/4 = 6.25

(22.56)

The coeﬃcient of height is the same for both gender subpopulations.

But look what happens when we remove gender from the analysis:

all:

height mean weight
135
170

66
70

βheight = (170 − 135)/4 = 8.75

(22.57)

424

CHAPTER 22. LINEAR REGRESSION

In other words, the beta coeﬃcient for height is 8.75 if gender is not in the equation, but is only
6.25 if we add in gender. For a given height, men in this population tend to be heavier, and since
the men tend to be taller, that inﬂated the height coeﬃcient in the genderless analysis.

Returning to the baseball example, recall that in in Section 22.13, we added the position variables
to height and age as predictors. The coeﬃcient for height, which had increased when we added in
the age variable, now increased further, while the coeﬃcient for age decreased, compared to the
results in Section 22.8. Those heavy catchers weren’t separated out from the other players in our
previous analysis, and now that we are separating them from the rest, the relationship of weight
versus height and age is now clariﬁed.

Such thinking was central to another baseball example, in Mere Mortals: Retract This Article,
Gregory Matthews blog,

http://statsinthewild.wordpress.com/2012/08/23/mere-mortals-retract-this-article/.

There the author took exception to someone else’s analysis that purported to show that professional
baseball players have a higher mortality rate than do pro football players. This was counterintuitive,
since football is more of a contact sport. It turned out that the original analysis had been misleading,
as it did not use age as a predictor.

Clearly, the above considerations are absolutely crucial to eﬀective use of regression analysis for the
Description goal. This insight is key—don’t do regression without it! And for the same reasons,
whenever you read someone else’s study, do so with a skeptical eye.

22.15 Model Selection

The issues raised in Chapter 21 become crucial in regression and classiﬁcation problems. In the
context of this chapter, we typically deal with models having large numbers of parameters.
In
regression analysis, we often have many predictor variables, and of course the number of parameters
can become even larger if we add in interaction and polynomial terms.

The model selection problem concerns simplifying a given model to one with fewer parameters.
There are two motivations for this:

• A central principle will be that simpler models are preferable, provided of course they ﬁt the
data well. Hence the Einstein quote that opens Chapter 21! Simpler models are often called
parsimonious.

• A simpler model may actually predict new cases better than a complex one, due to the

overﬁtting problem discussed below.

22.15. MODEL SELECTION

425

So, in this section we discuss methods of selecting which predictor variables (including powers and
interactions) we will use.

22.15.1 The Overﬁtting Problem in Regression

Recall that in Section 22.9 we mentioned that we could add polynomial terms to a regression
model. But you can see that if we carry this notion to its extreme, we get absurd results. If we ﬁt a
polynomial of degree 99 to our 100 points, we can make our ﬁtted curve exactly pass through every
point! This clearly would give us a meaningless, useless curve. We are simply ﬁtting the noise.

Recall that we analyzed this problem in Section 21.1.4 in our chapter on modeling. There we noted
an absolutely fundamental principle in statistics:

In choosing between a simpler model and a more complex one, the latter is more accurate
only if either

• we have enough data to support it, or

• the complex model is suﬃciently diﬀerent from the simpler one

This is extremely important in regression analysis, because we often have so many
variables we can use, thus often can make highly complex models.

In the regression context, the phrase “we have enough data to support the model” means (in the
parametric model case) we have enough data so that the conﬁdence intervals for the βi will be
reasonably narrow. For ﬁxed n, the more complex the model, the wider the resulting conﬁdence
intervals will tend to be.

If we use too many predictor variables,11, our data is “diluted,” by being “shared” by so many βi.
As a result, V ar( (cid:98)βi) will tend to be large, with big implications: Whether our goal is Prediction or
Description, our estimates will be so poor that neither goal is achieved.

On the other hand, if some predictor variable is really important (i.e. its βi is far from 0), then it
may pay to include it, even though the conﬁdence intervals might get somewhat wider.

The questions raised in turn by the above considerations, i.e. How much data is enough data?,
and How diﬀerent from 0 is “quite diﬀerent”?, are addressed below in Section 22.15.4.

A detailed mathematical example of overﬁtting in regression is presented in my paper A Careful
Look at the Use of Statistical Methodology in Data Mining (book chapter), by N. Matloﬀ, in
Foundations of Data Mining and Granular Computing, edited by T.Y. Lin, Wesley Chu and L.
Matzlack, Springer-Verlag Lecture Notes in Computer Science, 2005.

11In the ALOHA example above, b, b2, b3 and b4 are separate predictors, even though they are of course correlated.

426

CHAPTER 22. LINEAR REGRESSION

22.15.2 Relation to the Bias-vs.-Variance Tradefoﬀ

Above we mentioned that the overﬁtting issue can be viewed as due to the bias-vs.-variance tradeoﬀ.
The variance portion of this was explained above: As the number of predictors increases, V ar( (cid:98)βi)
will also tend to increase.

But the bias will decrease. To see this, suppose we have data on two predictors. Let Model I be
the result of using just X (1), and Model II be the corresponding result using both X (1) and X (2).
Then from the point of view of Model II, our Model I will be biased.

Speciﬁcally, omitting a predictor makes the conditional expected response, given all the other
predictors AND this additional one, is not modeled correctly

22.15.3 Multicollinearity

In typical applications, the X (i) are correlated with each other, to various degrees. If the correlation
is high—a condition termed multicollinearity—problems may occur.

Consider (22.33). Suppose one predictor variable were to be fully correlated with another. That
would mean that the ﬁrst is exactly equal to a linear function of the other, which would mean that
in Q one column is an exact linear combination of the ﬁrst column and another column. Then
(Q(cid:48)Q)−1 would not exist.

Well, if one predictor is strongly (but not fully) correlated with another, (Q(cid:48)Q)−1 will exist, but it
will be numerically unstable. Moreover, even without numeric roundoﬀ errors, (Q(cid:48)Q)−1 would be
very large, and thus (22.45) would be large, giving us large standard errors—not good!

Thus we have yet another reason to limit our set of predictor variables.

22.15.4 Methods for Predictor Variable Selection

So, we typically must discard some, maybe many, of our predictor variables. In the weight/height/age
example, we may need to discard the age variable. In the ALOHA example, we might need to dis-
card b4 and even b3. How do we make these decisions?

Note carefully that this is an unsolved problem. If anyone claims they have a foolproof way
to do this, then they do not understand the problem in the ﬁrst place. Entire books have been
written on this subject, e.g. Subset Selection in Regression, by Alan Miller, pub. by Chapman and
Hall, second ediiton 2002. In his preface to the second edition of the book, Miller laments that
almost no progress had been made in the ﬁeld since the ﬁrst edition had been published, a dozen
years earlier! The same statement could be made today.

22.15. MODEL SELECTION

427

Myriad diﬀerent methods have been developed. but again, none of them is foolproof.

22.15.4.1 Hypothesis Testing

The most commonly used methods for variable selection use hypothesis testing in one form or
another. Typically this takes the form

In the context of (22.19), for instance, a decision as to whether to include age as one of our predictor
variables would mean testing

H0 : βi = 0

(22.58)

H0 : β2 = 0

(22.59)

If we reject H0, then we use the age variable; otherwise we discard it.

This approach is extended in a method called stepwise regression (which actually should be called
“stepwise variable selection.” It comes in forward and backward varieties. In the former, one keeps
adding more and more predictors to the model, until there are no remaining “signiﬁcant” ones. At
each step, one enters the variable that is most “signiﬁcant,” meaning the one with the smallest
p-value. In the backward variation, one starts with all predictors, and removes one at each step.

I hope I’ve convinced the reader, in Sections 17.11 and 21.2.1, that using signiﬁcance
testing for variable selection is not a good idea. As usual, the hypothesis test is asking the
wrong question. For instance, in the weight/height/age example, the test is asking whether β2 is
zero or not—yet we know it is not zero, before even looking at our data. What we want to know
is whether β2 is far enough from 0 for age to give us better predictions of weight. Those are two
very, very diﬀerent questions.

A very interesting example of overﬁtting using real data may be found in the paper, Honest Con-
ﬁdence Intervals for the Error Variance in Stepwise Regression, by Foster and Stine, www-stat.
wharton.upenn.edu/~stine/research/honests2.pdf. The authors, of the University of Pennsyl-
vania Wharton School, took real ﬁnancial data and deliberately added a number of extra “predic-
tors” that were in fact random noise, independent of the real data. They then tested the hypothesis
(22.58). They found that each of the fake predictors was “signiﬁcantly” related to Y! This illustrates
both the dangers of hypothesis testing and the possible need for multiple inference procedures.12
This problem has always been known by thinking statisticians, but the Wharton study certainly
dramatized it.

12They added so many predictors that r became greater than n. However, the problems they found would have

been there to a large degree even if r were less than n but r/n was substantial.

428

CHAPTER 22. LINEAR REGRESSION

22.15.4.2 Conﬁdence Intervals

Well, then, what can be done instead? First, there is the same alternative to hypothesis testing
that we discussed before—conﬁdence intervals. If the interval is very wide, telling us that it would
be nice to have more data. But if the lower bound of that interval is far from zero, say, it would
look like the corresponding variable is worth using as a predictor.

On the other hand, suppose in the weight/height/age example our conﬁdence interval for β2 is
(0.04,0.06). In other words, we estimate β2 to be 0.05, with a margin of error of 0.01. The 0.01
is telling us that our sample size is good enough for an accurate assessment of the situation, but
the interval’s location—centered at 0.05–says, for instance, a 10-year diﬀerence in age only makes
about half a pound diﬀerence in mean weight. In that situation age would be of almost no value
in predicting weight.

An example of this using real data is given in Section 23.3.

22.15.4.3 Predictive Ability Indicators

Suppose you have several competing models, some using more predictors, some using fewer. If we
had some measure of predictive power, we could decide to use whichever model has the maximum
value of that measure. Here are some of the more commonly used methods of this type:

• One such measure is called adjusted R-squared. To explain it, we must discuss ordinary R2

ﬁrst.

Let ρ denote the population correlation between actual Y and predicted Y, i.e. the correlation
between Y and mY ;X (X), where X is the vector of predictor variables in our model. Then |ρ|
is a measure of the power of X to predict Y, but it is traditional to use ρ2 instead.13
R is then the sample correlation between the Yi and the vectors Xi. The sample R2 is then an
estimate of ρ2. However, the former is a biased estimate—over inﬁnitely many samples, the
long-run average value of R2 is higher than ρ2. And the worse the overﬁtting, the greater the
bias. Indeed, if we have n-1 predictors and n observations, we get a perfect ﬁt, with R2 = 1,
yet obviously that “perfection” is meaningless.
Adjusted R2 is a tweaked version of R2 with less bias. So, in deciding which of several models
to use, we might choose the one with maximal adjusted R2. Both measures are reported
when one calls summary() on the output of lm().

• The most popular alternative to hypothesis testing for variable selection today is probably
cross validation. Here we split our data into a training set, which we use to estimate the

13That quantity can be shown to be the proportion of variance of Y attributable to X.

22.15. MODEL SELECTION

429

βi, and a validation set, in which we see how well our ﬁtted model predicts new data, say
in terms of average squared prediction error. We do this for several models, i.e. several sets
of predictors, and choose the one which does best in the validation set. I like this method
very much, though I often simply stick with conﬁdence intervals.

• A method that enjoys some popularity in certain circles is the Akaike Information Crite-
rion (AIC). It uses a formula, backed by some theoretical analysis, which creates a tradeoﬀ
between richness of the model and size of the standard errors of the ˆβi. Here we choose the
model with minimal AIC.

The R statistical package includes a function AIC() for this, which is used by step() in the
regression case.

22.15.4.4 The LASSO

Consider again Equation (22.25). Ordinarily, we ﬁnding the ui that minimize this quantity. But
the LASSO (Least Absolute Shrinkage and Selection Operator) minimizes

1033
(cid:88)

[Wi − (u0 + u1Hi + u2Ai)]2 + s

i=1

(cid:88)

|ui|

i=11033

(22.60)

for some value of s (generally chosen by cross-validation). The importance of this in our context
here is that this method turns out to force some of the ˆui to 0—in eﬀect, becoming a variable
selection procedure. The LASSO has many fans, though again see the Miller book why you ought
to be more careful with it.

22.15.5 Rough Rules of Thumb

A rough rule of thumb is that one should have r <
is the sample size.14 This result is general, not just restricted to regression models.

n, where r is the number of predictors and n

√

Also, if the adjusted R2 is close to the unadjusted value, this is some indication that you are not
overﬁtting.

14Asymptotic Behavior of Likelihood Methods for Exponential Families When the Number of Parameters Tends

to Inﬁnity, Stephen Portnoy, Annals of Statistics, 1988.

430

CHAPTER 22. LINEAR REGRESSION

22.16 Prediction

As noted, regression analysis is motivated by prediction. This is true even if ones goal is Description.
We pursue this point further here.

22.16.1 Height/Weight Age Example

Let’s return to our weight/height/age example. We are informed of a certain person, of height 70.4
and age 24.8, but weight unknown. What should we predict his weight to be?

The intuitive answer (justiﬁed formally on page 405) is that we predict his weight to be the mean
weight for his height/age group,

mW ;H,A(70.4, 24.8)

(22.61)

But that is a population value. Say we estimate the function mW ;H using that data, yielding (cid:98)mW ;H .
Then we could take as our prediction for the new person’s weight

If our model is (22.18), then (22.62) is

(cid:98)mW ;H,A(70.4, 24.8)

where the (cid:98)βi are estimated from our data by least-squares.

(cid:98)mW ;H (t) = (cid:98)β0 + (cid:98)β170.4 + (cid:98)β224.8

22.16.2 R’s predict() Function

(22.62)

(22.63)

We can automate the prediction process in (22.63), which is handy when we are doing a lot of
predictions. An important example of such a situation was seen in Section 22.15.4.3, with the idea
of breaking our data into training and validation sets.

R’s predict() function makes this much easier. It is actually a collection of functions, with the
one corresponding to lm() being predict.lm(). We just call predict(), and R will sense which
version to call.15

With the arguments used here, the call form is

15R’s object orientation includes the notion of generic functions, where a single function, say plot(), actually

transfers control to the proper class-speciﬁc version.

22.17. EXAMPLE: TURKISH TEACHING EVALUATION DATA

431

predict ( lmobj , newxmatrix )

where lmobj is an object returned from a call to lm(), and newmatrix is the matrix of predictor
values from which we wish to predict Y. The return value will be the vector of predicted Y values.

22.17 Example: Turkish Teaching Evaluation Data

This data, again from the UCI Machine Learning Repository, consists of 5820 student evaluations
of professors in Turkey.

22.17.1 The Data

There are 28 questions of the type agree/disagree, scale of 1 to 5. Here are the ﬁrst few:

Q1: The semester course content, teaching method and evaluation system were provided at the
start.

Q2: The course aims and objectives were clearly stated at the beginning of the period.

Q3: The course was worth the amount of credit assigned to it.

Q4: The course was taught according to the syllabus announced on the ﬁrst day of class.

Q5: The class discussions, homework assignments, applications and studies were satisfactory.

Q6: The textbook and other courses resources were suﬃcient and up to date.

Q7: The course allowed ﬁeld work, applications, laboratory, discussion and other studies.

There are also several “miscellaneous” questions, e.g. concerning the diﬃculty of the class. I chose
to use just this one.

22.17.2 Data Prep

I used a text editor to remove quotation marks in the original ﬁle, making it easier to read in. I
then did

> t u r k <−
read . csv ( ”˜/ Montreal /Data/TurkEvals/ t u r k i y e −s t u d e n t −e v a l u a t i o n . c s v ” , h e a d e r=T)
> names( t u r k )
[ 1 ] ” i n s t r ”
[ 6 ] ”Q1”

” a t t e n d a n c e ” ” d i f f i c u l t y ”
”Q4”

”nb . r e p e a t ”
”Q3”

” c l a s s ”
”Q2”

”Q5”

432

CHAPTER 22. LINEAR REGRESSION

[ 1 1 ] ”Q6”
[ 1 6 ] ”Q11”
[ 2 1 ] ”Q16”
[ 2 6 ] ”Q21”
[ 3 1 ] ”Q26”
> t u r k <− t u r k [ , c ( 6 : 3 3 , 5 ) ]

”Q7”
”Q12”
”Q17”
”Q22”
”Q27”

”Q8”
”Q13”
”Q18”
”Q23”
”Q28”

”Q9”
”Q14”
”Q19”
”Q24”

”Q10”
”Q15”
”Q20”
”Q25”

In that last operation, I reordered the data, so that the new column numbers would reﬂect the
question numbers:

> head ( t u r k )

Q1 Q2 Q3 Q4 Q5 Q6 Q7 Q8 Q9 Q10 Q11 Q12 Q13 Q14 Q15 Q16 Q17 Q18 Q19 Q20

Q21
1
3
2
3
3
5
4
3
5
1
6
4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

3

3

5

3

1

4

Q22 Q23 Q24 Q25 Q26 Q27 Q28 d i f f i c u l t y
4
3
3
3
4
5
3
3
1
1
3
4

3
3
5
3
1
4

3
3
5
3
1
4

3
3
5
3
1
4

3
3
5
3
1
4

3
3
5
3
1
4

3
3
5
3
1
4

1
2
3
4
5
6

Let’s also split the rows of the data into training and validation sets, as in Section 22.15.4.3, and
ﬁt the model to the training set:

nr <− nrow( t u r k )
t r a i n <− sample ( 1 : nr , f l o o r ( 0 . 8 ∗nr ) , replace=F)
v a l <− s e t d i f f ( 1 : nr , t r a i n )
lmout <− lm( t u r k [ t r a i n , 9 ] ˜ . , data=t u r k [ t r a i n , c ( 1 : 8 , 1 0 : 2 9 ) ] )

22.17. EXAMPLE: TURKISH TEACHING EVALUATION DATA

433

22.17.3 Analysis

So, let’s run a regression on the training set. Question 9, “Q9: I greatly enjoyed the class and was
eager to actively participate during the lectures,” is the closest one to an overall evaluation of an
instructor. Let’s predict the outcome of Q9 from the other variables, in order to understand what
makes a popular teacher in Turkey. Here is part of the output:

> lmout <− lm( t u r k [ t r a i n , 9 ] ˜ . , data=t u r k [ t r a i n , c ( 1 : 8 , 1 0 : 2 9 ) ] )
> summary( lmout )
. . .
C o e f f i c i e n t s :

Estimate Std . E r r o r t v a l u e Pr ( >| t | )

1 . 6 3 9 e −01
( I n t e r c e p t )
−9.078 e −05
Q1
5 . 1 4 8 e −02
Q2
6 . 1 3 4 e −02
Q3
7 . 9 4 1 e −03
Q4
−3.858 e −02
Q5
−2.991 e −02
Q6
8 . 5 4 3 e −02
Q7
1 . 1 7 2 e −01
Q8
3 . 3 8 6 e −01
Q10
1 . 7 4 4 e −01
Q11
4 . 2 0 6 e −02
Q12
−2.283 e −02
Q13
2 . 8 7 1 e −02
Q14
−6.692 e −02
Q15
7 . 6 7 0 e −02
Q16
1 . 0 0 5 e −01
Q17
−3.766 e −03
Q18
2 . 2 6 8 e −02
Q19
−4.538 e −02
Q20
1 . 0 2 2 e −01
Q21
5 . 2 4 8 e −02
Q22
−8.160 e −03
Q23
−1.228 e −01
Q24
7 . 2 4 8 e −02
Q25
−6.819 e −03
Q26
−6.771 e −03
Q27
Q28
−2.506 e −02
d i f f i c u l t y −5.367 e −03

5 . 7 4 8 9 . 6 1 e −09 ∗∗∗

2 . 9 2 3 0 . 0 0 3 4 8 9 ∗∗
3 . 9 8 0 7 . 0 0 e −05 ∗∗∗
0 . 4 8 8 0 . 6 2 5 3 4 3

4 . 5 0 1 6 . 9 2 e −06 ∗∗∗
6 . 6 3 1 3 . 7 3 e −11 ∗∗∗
1 7 . 1 6 2 < 2 e −16 ∗∗∗
1 1 . 4 1 4 < 2 e −16 ∗∗∗
2 . 7 6 0 0 . 0 0 5 7 9 5 ∗∗

2 . 8 5 2 e −02
1 . 3 7 3 e −02 −0.007 0 . 9 9 4 7 2 6
1 . 7 6 2 e −02
1 . 5 4 1 e −02
1 . 6 2 6 e −02
1 . 8 5 1 e −02 −2.084 0 . 0 3 7 1 7 9 ∗
1 . 6 9 0 e −02 −1.770 0 . 0 7 6 8 7 4 .
1 . 8 9 8 e −02
1 . 7 6 7 e −02
1 . 9 7 3 e −02
1 . 5 2 8 e −02
1 . 5 2 4 e −02
2 . 0 9 0 e −02 −1.092 0 . 2 7 4 8 7 9
1 . 2 3 3 0 . 2 1 7 6 6 4
2 . 3 2 9 e −02
2 . 1 6 4 e −02 −3.093 0 . 0 0 1 9 9 3 ∗∗
2 . 0 0 7 e −02
1 . 7 1 6 e −02
1 . 9 4 0 e −02 −0.194 0 . 8 4 6 0 7 2
1 . 9 8 3 e −02
1 . 1 4 3 0 . 2 5 2 9 9 0
2 . 0 7 4 e −02 −2.189 0 . 0 2 8 6 7 6 ∗
2 . 2 8 0 e −02
2 . 2 8 8 e −02
2 . 1 6 0 e −02 −0.378 0 . 7 0 5 6 6 8
1 . 9 2 4 e −02 −6.380 1 . 9 5 e −10 ∗∗∗
3 . 5 2 3 0 . 0 0 0 4 3 1 ∗∗∗
2 . 0 5 7 e −02
1 . 7 7 5 e −02 −0.384 0 . 7 0 0 8 2 0
1 . 5 8 4 e −02 −0.428 0 . 6 6 8 9 5 8
1 . 7 8 2 e −02 −1.407 0 . 1 5 9 6 1 5
6 . 1 5 1 e −03 −0.873 0 . 3 8 2 9 2 5

3 . 8 2 1 0 . 0 0 0 1 3 5 ∗∗∗
5 . 8 5 7 5 . 0 4 e −09 ∗∗∗

4 . 4 8 4 7 . 5 2 e −06 ∗∗∗
2 . 2 9 4 0 . 0 2 1 8 6 0 ∗

434

CHAPTER 22. LINEAR REGRESSION

−−−
S i g n i f . codes :

0

∗∗∗

0 . 0 0 1

∗∗

0 . 0 1

∗

0 . 0 5

.

0 . 1

1

s t a n d a r d e r r o r : 0 . 5 5 6 on 4627 d e g r e e s o f

freedom

R e s i d u a l
M u l t i p l e R−s q u a r e d :
F− s t a t i s t i c : 6 8 6 . 8 on 28 and 4627 DF,

0 . 8 0 6 1 ,

Adjusted R−s q u a r e d :

0 . 8 0 4 9

p−v a l u e : < 2 . 2 e −16

Questions 10 (“Q10: My initial expectations about the course were met at the end of the period
or year”) had the largest coeﬃcient by far, 0.3386.

In terms of signiﬁcance testing, it was very highly signiﬁcant, but since we have a large sample size,
5802, we should be careful not to automatically conclude that this is an important predictor. Let’s
take a closer look.

The intercept term, (cid:98)β0, is 0.1639. This is considerably smaller than the coeﬃcient for Q10; in-
crements in Q10 are of size 1, and we see that this increment will make a sizeable impact on our
overall estimated regression function.

So, let’s try predict() on the validation set:

newy <− predict ( lmout , newdata=t u r k [ val , c ( 1 : 8 , 1 0 : 2 9 ) ] )

Just to make sure that predict() works as advertised, let’s do a check. Here’s is the predicted Y
for the ﬁrst observation in our validation set, calculated “by hand”:

> newx1 <− t u r k [ v a l [ 1 ] , c ( 1 : 8 , 1 0 : 2 9 ) ]
> newx1 <− as . numeric ( newx1 ) # was a d a t a frame
> newx1 <− c ( 1 , newx1 ) # need t o h a n d l e t h e i n t e r c e p t
> b e t a s <− lmout$coef
> b e t a s %∗% newx1

term

[ , 1 ]
[ 1 , ] 3 . 9 7 6 5 7 4

Here is what predict() tells us:

> newy [ 1 ]
6

3 . 9 7 6 5 7 4 # i t c h e c k s o u t !

Now, let’s see how accurate our predictions are on the new data:

> t r u e y <− t u r k [ val , 9 ]
> mean( abs ( newy − t r u e y ) )
[ 1 ] 0 . 2 8 2 0 8 3 4

22.18. WHAT ABOUT THE ASSUMPTIONS?

435

Not bad at all—on average, our preduction is oﬀ by about 0.3 point, on a scale of 5.

The basic point is to ﬁt a model to one data set and then try it out on new, “fresh” data, which
we’ve done above. But just for fun, let’s go back and “predict” the original data set:

p r e d o l d <− predict ( lmout , newdata=t u r k [ t r a i n , c ( 1 : 8 , 1 0 : 2 9 ) ] )

> mean( abs ( p r e d o l d − t u r k [ t r a i n , 9 ] ) )
[ 1 ] 0 . 2 9 0 8 4 4

Given the concern about overﬁtting brought up in Section 22.15.1, one might expect the mean
absolute error to be smaller on the original data, but it turns out to actually be a bit larger. The is
presumably due to sampling error, but the real issue here is that the mean absolute error did not
decrease a lot. This is because our sampling size, 5802, is large enough to support the 28 predictor
variables we are using. This is seen above, where the adjusted R2, 0.8049, was almost the same as
the unadjusted version, 0.8061.

22.18 What About the Assumptions?

We have made two assumptions in this chapter:

• Linearity of our model: (22.24). (But recall that this doesn’t prevent us from including powers

of variables etc.)

• Homogeneity of variance (termed homoscedasticity) of Y given the X (i): (22.39).16

The classical analysis makes one more assumption:

• The conditional distribution of Y given the X (i) is normal.

We discuss these points further in this section.

22.18.1 Exact Conﬁdence Intervals and Tests

Note carefully that we have not assumed that Y, given X, is normally distributed. In
the height/weight context, for example, such an assumption would mean that weights in a speciﬁc
height subpopulation, say all people of height 70 inches, have a normal distribution. We have not
needed this assumption, as wse have relied on the Central Limit Theorem to give us approximate

16We also assume that the observations are independent.

436

CHAPTER 22. LINEAR REGRESSION

normal distributions for the (cid:98)βi, enabling conﬁdence intervals and signiﬁcance tests. This issue is
similar to that of Section 16.7.

If we do make such a normality assumption, then we can get exact conﬁdence intervals (which of
course, only hold if we really do have an exact normal distribution in the population). This again
uses Student-t distributions. In that analysis, s2 has n-(r+1) in its denominator instead of our n,
just as there was n-1 in the denominator for s2 when we estimated a single population variance.
The number of degrees of freedom in the Student-t distribution is likewise n-(r+1).

But as before, for even moderately large n, it doesn’t matter. And for small n, the normal population
assumption almost never holds, or literally never. Thus exact methods are overrated, in this author’s
opinion.

22.18.2

Is the Homoscedasticity Assumption Important?

What about the assumption (22.39), which we made and which the “exact” methods assume too?
This assumption is seldom if ever exactly true in practice, but studies have shown that the anal-
ysis is robust to that assumption. This means that even with fairly substantial violation of the
assumption, the conﬁdence intervals work fairly well.

22.18.3 Regression Diagnostics

Researchers in regression analysis have devised some diagnostic methods, meaning methods to
check the ﬁt of a model, the validity of assumptions [e.g. (22.39)], search for data points that may
have an undue inﬂuence (and may actually be in error), and so on. The residuals tend to play a
central role here.

For instance, to check a model such as (22.19), we could plot our residuals against our age values.
Suppose the pattern is that the residuals tend to be negative for the very young or very old people
in our sample (i.e. overpredicting), and positive for the ones in between (underpredicting). This
may suggest trying a model quadratic in age.

The R package has tons of diagnostic methods. See for example Linear Models with R, Julian
Faraway, Chapman and Hall, 2005, and An R and S-Plus Companion to Applied Regression, John
Fox, Sage, 2002.

22.19. CASE STUDIES

22.19 Case Studies

437

22.19.1 Example: Prediction of Network RTT

Recall the paper by Raz et al, introduced in Section 22.2. They wished to predict network round-
trip travel time (RTT) from oﬄine variables. Now that we know how regression analysis works,
let’s look at some details of that paper.

First, they checked for multicollinearity. one measure of that is the ratio of largest to smallest
eigenvalue of the matrix of correlations among the predictors. A rule of thumb is that there are
problems if this value is greater than 15, but they found it was only 2.44, so they did not worry
about multicollinearity.

They took a backwards stepwise approach to predictor variable selection, meaning that they started
with all the variables, and removed them one-by-one while monitoring a goodness-of-ﬁt criterion.
They chose AIC for the latter.

Their initial predictors were DIST, the geographic distance between source and destination node,
HOPS, the number of network hops (router processing) and an online variable, AS, the number of
autonomous systems—large network routing regions—a message goes through. They measured
the latter using the network tool traceroute.

But AS was the ﬁrst variable they ended up eliminating. They found that removing it increased
AIC only slightly, from about 12.6 million to 12.9 million, and reduced R2 only a bit, from 0.785 to
0.778. They decided that AS was expendable, especially since they were hoping to use only oﬄine
variables.

Based on a scatter plot of RTT versus DIST, they then decided to try adding a quadratic term
in that variable. This increased R2 substantially, to 0.877. So, the ﬁnal prediction equation they
settled on predicts RTT from a quadratic function of DIST and a linear term for HOPS.

22.19.2 Transformations

It is common in some ﬁelds, especially economics, to apply logarithm transformations to regression
variables.17

One of the motivations for this is to deal with the homoscedasticity assumption: Say we have
just one predictor variable, for simplicity.
If V ar(Y |X = t) is increasing in t, it is hoped that
V ar[ln(Y )|X = t) is more stable.

17I personally do not take this approach.

438

CHAPTER 22. LINEAR REGRESSION

22.19.3 Example: OOP Study

Consider again the OOP study cited in Section 22.2.
It was actually a bit diﬀerent from our
description above. Among other things, they took natural logarithms of the variables. The model
was

mean Y = β0 + β1X (1) + β2X (2) + β3X (1)X (2)

(22.64)

where now: Y is the log of Person Months (PM); X (1) is the log of KLOC, the number of thousands
of lines of code; and X (2) is a dummy variable for OOP. The results were:

coef. betahat
4.37
0.49
0.56
-0.13

β0
β1
β2
β3

std.err.
0.23
0.07
1.57
-1.34

Let’s ﬁnd the estimated diﬀerence in mean log completion time under OOP and using procedural
language (former minus the latter), for 1000-line programs:

(4.37 + 0.49 · 1 + 0.56 · 1 − 0.13 · 1 · 1) − (4.37 + 0.49 · 1 + 0.5 · 0 − 0.13 · 0 · 0) = 0.92

(22.65)

While it is not the case that the mean of the log is the log of the mean, those who use log
transformations treat this as an approximation. The above computation would then be viewed as
the diﬀerence between two logs, thus the log of a quotient. That quotient would then be exp(0.92)
= 2.51. In other words, OOP takes much longer to write. However, the authors note that neither
of the beta coeﬃcients for OOP and KLOC × OOP was signiﬁcantly diﬀerent from 0 at the 0.05
level, and thus consider the whole thing a wash.

Exercises

In the quartic model in ALOHA simulation example, ﬁnd an approximate 95% conﬁdence

1.
interval for the true population mean wait if our backoﬀ parameter b is set to 0.6.

Hint: You will need to use the fact that a linear combination of the components of a multivariate
normal random vector has a univariate normal distributions as discussed in Section 13.5.2.1.

2. Consider the linear regression model with one predictor, i.e. r = 1. Let Yi and Xi represent the
values of the response and predictor variables for the ith observation in our sample.

22.19. CASE STUDIES

439

(a) Assume as in Section 22.10.4 that V ar(Y |X = t) is a constant in t, σ2. Find the exact value
of Cov( ˆβ0, ˆβ1), as a function of the Xi and σ2. Your ﬁnal answer should be in scalar, i.e.
non-matrix form.

(b) Suppose we wish to ﬁt the model mY ;X (t) = β1t, i.e. the usual linear model but without the

constant term, β0. Derive a formula for the least-squares estimate of β1.

3. Suppose the random pair (X, Y ) has density 8st on 0 < t < s < 1. Find mY ;X (s) and
V ar(Y |X = t), 0 < s < 1.

4. The code below reads in a ﬁle, data.txt, with the header record

"age", "weight", "systolic blood pressure", "height"

and then does the regression analysis.

Suppose we wish to estimate β in the model

mean weight = β0 + β1height + β2age

Fill in the blanks in the code:

dt <- ____________(____________________________________)
regr <- lm(___________________________________________________)
cvmat <- _______________(regr)
print("the estimated value of beta2-beta0 is",

____________________________________________________)

print("the estimated variance of beta2 - beta0 is",

_______________________ %*% cvmat %*%

_________________________)

# calculate the matrix Q
q <- cbind(______________________________________________)

5. In this problem, you will conduct an R simulation experiment similar to that of Foster and Stine
on overﬁtting, discussed in Section 22.15.4.

Generate data X (j)
N(0,4). Set Yi = X (1)
from an (r+1)-variate population.

i

, i = 1, ..., n, j = 1, ..., r from a N(0,1) distribution, and (cid:15)i, i = 1, ..., n from
i + (cid:15)i, i = 1, ..., n. This simulates drawing a random sample of n observations

Now suppose the analyst, unaware that Y is related to only X (1), ﬁts the model

mY ;X (1),...,X (r)(t1, ..., tr) = β0 + β1t(1) + ... + βrt(r)

(22.66)

440

CHAPTER 22. LINEAR REGRESSION

In actuality, βj = 0 for j > 1 (and for i = 0). But the analyst wouldn’t know this. Suppose
the analyst selects predictors by testing the hypotheses H0 : βi = 0, as in Section 22.15.4, with
α = 0.05.

Do this for various values of r and n. You should ﬁnd that, for ﬁxed n and increasing r. You begin
to ﬁnd that some of the predictors are declared to be “signiﬁcantly” related to Y (complete with
asterisks) when in fact they are not (while X (1), which really is related to Y , may be declared NOT
“signiﬁcant.” This illustrates the folly of using hypothesis testing to do variable selection.

6. Suppose given X = t, the distribution of Y has mean γt and variance σ2, for all t in (0,1). This
is a ﬁxed-X regression setting, i.e. X is nonrandom: For each i = 1,...,n we observe Yi drawn at
random from the distribution of Y given X = i/n. The quantities γ and σ2 are unknown.

Our goal is to estimate mY ;X (0.75). We have two choices for our estimator:

• We can estimate in the usual least-squares manner, denoting our estimate by G, and then

use as our estimator T1 = 0.75G.

• We can take our estimator T2 to be (Y1 + ... + Yn)/n,

Perform a tradeoﬀ analysis similar to that of Section 18.2, determining under what conditions T1
is superior to T2 and vice versa. Our criterion is mean squared error (MSE), E[(Ti − mY ;X (0.75)].
Make your expressions as closed-form as possible.

Advice: This is a linear model, albeit one without an intercept term. The quantity G here is simply
ˆσ. G will turn out to be a linear combination of the Xs (which are constants), so its variance is
easy to ﬁnd.

7. Suppose X has an N (µ, µ2) distribution, i.e. with the standard deviation equal to the mean. (A
common assumption in regression contexts.) Show that h(X) = ln(X) will be a variance-stabilizing
transformation, a concept discussed in Section ??.

8. Consider a random pair (X, Y ) for which the linear model E(Y |X) = β0 + β1X holds, and think
about predicting Y , ﬁrst without X and then with X, minimizing mean squared prediction error
(MSPE) in each case. As discussed on page 405 without X, the best predictor is EY , while with
X it is E(Y |X), which under our assumption here is β0 + β1X. Show that the reduction in MSPE
accrued by using X, i.e.

E (cid:2)(Y − EY )2(cid:3) − E (cid:2){Y − E(Y |X)}2(cid:3)
E [(Y − EY )2]

(22.67)

is equal to ρ2(X, Y ).

22.19. CASE STUDIES

441

9. In an analysis published on the Web (Sparks et al, Disease Progress over Time, The Plant Health
Instructor, 2008, the following R output is presented:

> severity.lm <- lm(diseasesev~temperature,data=severity)
> summary(severity.lm)
Coefficients:

Estimate Std. Error t value Pr(>|t|)

(Intercept) 2.66233
temperature 0.24168
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1

1.10082
0.06346

2.418
3.808

0.04195 *
0.00518 **

1

Fill in the blanks:

(a) The model here is

mean

= β0 + β1

(b) The two null hypotheses being tested here are H0 :

and H0 :

.

10. In the notation of this chapter, give matrix and/or vector expressions for each of the following
in the linear regression model:

(a) s2, our estimator of σ2

(b) the standard error of the estimated value of the regression function mY ;X (t) at t = c, where

c = (c0, c1, ..., cr)

442

CHAPTER 22. LINEAR REGRESSION

Chapter 23

Classiﬁcation

In prediction problems, in the special case in which Y is an indicator variable, with the value 1 if
the object is in a class and 0 if not, the regression problem is called the classiﬁcation problem.1

We’ll formalize this idea in Section 23.1, but ﬁrst, here are some examples:

• A forest ﬁre is now in progress. Will the ﬁre reach a certain populated neighborhood? Here
Y would be 1 if the ﬁre reaches the neighborhood, 0 otherwise. The predictors might be wind
direction, distance of the ﬁre from the neighborhood, air temperature and humidity, and so
on.

• Is a patient likely to develop diabetes? This problem has been studied by many researchers,
e.g. Using Neural Networks To Predict the Onset of Diabetes Mellitus, Murali S. Shanker J.
Chem. Inf. Comput. Sci., 1996, 36 (1), pp 3541. A famous data set involves Pima Indian
women, with Y being 1 or 0, depending on whether the patient does ultimately develop dia-
betes, and the predictors being the number of times pregnant, plasma glucose concentration,
diastolic blood pressure, triceps skin fold thickness, serum insulin level, body mass index,
diabetes pedigree function and age.

• Is a disk drive likely to fail soon? This has been studied for example in Machine Learning
Methods for Predicting Failures in Hard Drives: A Multiple-Instance Application, by Joseph
F. Murray, Gordon F. Hughes, and Kenneth Kreutz-Delgado, Journal of Machine Learning
Research 6 (2005) 783-816. Y was 1 or 0, depending on whether the drive failed, and the
predictors were temperature, number of read errors, and so on.

• An online service has many customers come and go. It would like to predict who is about to

leave, so as to oﬀer them a special deal for staying with this ﬁrm.

1The case of c > 2 classes will be treated in Section 23.4.

443

444

CHAPTER 23. CLASSIFICATION

• Of course, a big application is character recognition, based on pixel data. This is diﬀerent
from the above examples, as there are more than two classes, many more. We’ll return to
this point soon.

In electrical engineering the classiﬁcation is called pattern recognition, and the predictors are
called features. In computer science the term machine learning usually refers to classiﬁcation
problems. Diﬀerent terms, same concept.

23.1 Classiﬁcation = Regression

All of the many machine learning algorithms, despite their complexity, really boil down to regression
at their core. Here’s why:

23.1.1 What Happens with Regression in the Case Y = 0,1?

As we have frequently noted the mean of any indicator random variable is the probability that the
variable is equal to 1 (Section 3.9). Thus in the case in which our response variable Y takes on
only the values 0 and 1, i.e. classiﬁcation problems, the regression function reduces to

mY ;X (t) = P (Y = 1|X = t)

(23.1)

(Remember that X and t are vector-valued.)

As a simple but handy example, suppose Y is gender (1 for male, 0 for female), X (1) is height
and X (2) is weight, i.e. we are predicting a person’s gender from the person’s height and weight.
Then for example, mY ;X (70, 150) is the probability that a person of height 70 inches and weight
150 pounds is a man. Note again that this probability is a population fraction, the fraction of men
among all people of height 70 and weight 150 in our population.

Make a mental note of the optimal prediction rule, if we know the population regression function:

Given X = t, the optimal prediction rule is to predict that Y = 1 if and only if mY ;X (t) >
0.5.

So, if we known a certain person is of height 70 and weight 150, our best guess for the person’s
gender is to predict the person is male if and only if mY ;X (70, 150) > 0.5.2

The optimality makes intuitive sense, and is shown in Section 23.6.

2Things change in the multiclass case, though, as will be seen in Section 23.4.

23.2. LOGISTIC REGRESSION: A COMMON PARAMETRIC MODEL FOR THE REGRESSION FUNCTION IN CLASSIFICATION PROBLEMS445

23.2 Logistic Regression: a Common Parametric Model for the

Regression Function in Classiﬁcation Problems

Remember, we often try a parametric model for our regression function ﬁrst, as it means we
are estimating a ﬁnite number of quantities, instead of an inﬁnite number. Probably the most
commonly-used model is that of the logistic function (often called “logit”). Its r-predictor form
is

mY ;X (t) = P (Y = 1|X = t) =

1
1 + e−(β0+β1t1+...+βrtr)

(23.2)

For instance, consider the patent example in Section 22.2. Under the logistic model, the population
proportion of all patents that are publicly funded, among those that contain the word “NSF,” do
not contain “NIH,” and make ﬁve claims would have the value

1
1 + e−(β0+β1+5β3)

23.2.1 The Logistic Model: Motivations

The logistic function itself,

1
1 + e−u

(23.3)

(23.4)

has values between 0 and 1, and is thus a good candidate for modeling a probability. Also, it is
monotonic in u, making it further attractive, as in many classiﬁcation problems we believe that
mY ;X (t) should be monotonic in the predictor variables.

But there are additional reasons to use the logit model, as it includes many common parametric
models for X. To see this, note that we can write, for vector-valued discrete X and t,

446

CHAPTER 23. CLASSIFICATION

P (Y = 1|X = t) =

=

=

=

P (Y = 1 and X = t)
P (X = t)

P (Y = 1)P (X = t|Y = 1)
P (X = t)

P (Y = 1)P (X = t|Y = 1)
P (Y = 1)P (X = t|Y = 1) + P (Y = 0)P (X = t|Y = 0)

1
1 + (1−q)P (X=t|Y =0)

qP (X=t|Y =1)

(23.5)

(23.6)

(23.7)

(23.8)

where q = P (Y = 1) is the proportion of members of the population which have Y = 1. (Keep in
mind that this probability is unconditional!!!! In the patent example, for instance, if say q = 0.12,
then 12% of all patents in the patent population—without regard to words used, numbers of claims,
etc.—are publicly funded.)

If X is a continuous random vector, then the analog of (23.8) is

P (Y = 1|X = t) =

1

1 +

(1−q)fX|Y =0(t)
qfX|Y =1(t)

(23.9)

Now for simplicity, suppose X is scalar, i.e. r = 1. And suppose that, given Y, X has a normal
distribution. In other words, within each class, Y is normally distributed. Suppose also that the
two within-class variances of X are equal, with common value σ2, but with means µ0 and µ1. Then

fX|Y =i(t) =

√

(cid:34)

exp

−0.5

(cid:19)2(cid:35)

(cid:18) t − µi
σ

1
2πσ

(23.10)

After doing some elementary but rather tedious algebra, (23.9) reduces to the logistic form

where

1
1 + e−(β0+β1t)

β0 = − ln

(cid:19)

(cid:18) 1 − q
q

+

0 − µ2
µ2
1
2σ2

,

(23.11)

(23.12)

23.2. LOGISTIC REGRESSION: A COMMON PARAMETRIC MODEL FOR THE REGRESSION FUNCTION IN CLASSIFICATION PROBLEMS447

and

β1 =

µ1 − µ0
σ2

,

(23.13)

In other words, if X is normally distributed in both classes, with the same variance
but diﬀerent means, then mY ;X () has the logistic form! And the same is true if X is
multivariate normal in each class, with diﬀerent mean vectors but equal covariance matrices. (The
algebra is even more tedious here, but it does work out.) Given the central importance of the
multivariate normal family—the word central here is a pun, alluding to the (multivariate) Central
Limit Theorem—this makes the logit model even more useful.

If you retrace the derivation above, you will see that the logit model will hold for any within-class
distributions for which

ln

(cid:19)

(cid:18) fX|Y =0(t)
fX|Y =1(t)

(23.14)

(or its discrete analog) is linear in t. Well guess what—this condition is true for exponential
distributions too! Work it out for yourself.

In fact, a number of famous distributions imply the logit model. So, logit is not only a good intuitive
model, as discussed above, but in addition there are some good theoretical recommendations for it.

23.2.2 Esimation and Inference for Logit Coeﬃcients

We ﬁt a logit model in R using the glm() function, with the argument family=binomial. The
function ﬁnds Maximum Likelihood Estimates (Section 18.1.3) of the βi.3

The output gives standard errors for the (cid:98)βi as in the linear model case. This enables the formation
of conﬁdence intervals and signiﬁcance tests on individual (cid:98)βi. For inference on linear combinations
of the (cid:98)βi, use the vcov() function as in the linear model case.

3As in the case of linear regression, estimation and inference are done conditionally on the values of the predictor

variables Xi.

448

CHAPTER 23. CLASSIFICATION

23.3 Example: Forest Cover Data

Let’s look again at the forest cover data we saw in Section 16.6.4.4 Recall that this application has
the Prediction goal, rather than the Description goal;5 we wish to predict the type of forest cover.
There were seven classes of forest cover.

23.3.0.1 R Code

For simplicity, I restricted my analysis to classes 1 and 2.6 In my R analysis I had the class 1 and
2 data in objects cov1 and cov2, respectively. I combined them,

> cov1and2 <- rbind(cov1,cov2)

and created a new variable to serve as Y, recoding the 1,2 class names to 1,0:

cov1and2[,56] <- ifelse(cov1and2[,55] == 1,1,0)

Let’s see how well we can predict a site’s class from the variable HS12 (hillside shade at noon) that
we investigated in Chapter 17, using a logistic model.

As noted earlier, in R we ﬁt logistic models via the glm() function, for generalized linear models.
The word generalized here refers to models in which some function of mY ;X (t) is linear in parameters
βi. For the classiﬁcation model,

ln (mY ;X (t)/[1 − mY ;X (t)]) = β0 + β1t(1) + ... + βrt(r)

(23.15)

(Recall the discussion surrounding (23.14).)

This kind of generalized linear model is speciﬁed in R by setting the named argument family to
binomial. Here is the call:

> g <- glm(cov1and2[,56] ~ cov1and2[,8],family=binomial)

The result was:

4There is a problem here, to be discussed in Section 23.4.2, but which will not aﬀect the contents of this section.
5Recall these concepts from Section 22.1.
6This will be generalized in Section 23.4.

23.3. EXAMPLE: FOREST COVER DATA

449

> summary(g)

Call:
glm(formula = cov1and2[, 56] ~ cov1and2[, 8], family = binomial)

Deviance Residuals:

Min

1Q
-1.165 -0.820

Median
-0.775

3Q
1.504

Max
1.741

Coefficients:

Estimate Std. Error z value Pr(>|z|)
0.1870
(Intercept)
1.515820
cov1and2[, 8] -0.010960
0.0317 *
---
Signif. codes: 0 *** 0.001 ** 0.01 * 0.05 . 0.1

1.148665
0.005103

1.320
-2.148

1

(Dispersion parameter for binomial family taken to be 1)

Null deviance: 959.72 on 810
Residual deviance: 955.14 on 809
AIC: 959.14

degrees of freedom
degrees of freedom

Number of Fisher Scoring iterations: 4

23.3.1 Analysis of the Results

You’ll immediately notice the similarity to the output of lm(). In particular, note the Coeﬃcients
section. There we have the estimates of the population coeﬃcients βi, their standard errors, and
p-values for the tests of H0 : βi = 0.

One diﬀerence from the linear case is that in that case, the tests of

H0 : βi = 0

(23.16)

were “exact,” based on the Student-t distribution, rather than being approximate tests based on
the Central Limit Theorem. The assumption is that the conditional distribution of the response
given the predictors is exactly normal. As noted before, those tests can’t possibly be exact, since
the assumption cannot exactly hold.

But in the logit case, no “exact” test is available anyway, so R does indeed do approximate tests
based on the Central Limit Theorem. Accordingly, the test column in the output is labeled “z
value,” rather than “t value” as before.

At any rate, we see that for example (cid:98)β1 = −0.01. This is tiny, reﬂecting our analysis of this data
in Chapter 17. There we found that the estimated mean values of HS12 for cover types 1 and 2
were 223.8 and 226.3, a diﬀerence of only 2.5, minuscule in comparison to the estimated means
themselves. That diﬀerence in essence now gets multiplied by 0.01. Let’s see the eﬀect on the

450

CHAPTER 23. CLASSIFICATION

regression function, i.e. the probability of cover type 1 given HS12.

Note ﬁrst, though, that things are a little more complicated than they were in the linear case.
Recall our ﬁrst baseball data analysis, in Section 22.6. Our model for mY ;X () was

mean weight = β0 + β1 height

(23.17)

After estimating the βi from the data, we had the estimated regression equation,

mean w e i g h t = −155.092 + 4 . 8 4 1 h e i g h t

In presenting these results to others, we can illustrate the above equation by noting, for instance,
that a 3-inch diﬀerence in height corresponds to an estimated 3 × 4.841 = 14.523 diﬀerence in
mean weight. The key point, though, is that this diﬀerence is the same whether we are comparing
70-inch-tall players with 73-inch-tall ones, or comparing 72-inch-tall players with 75-inch-tall ones,
etc.

In our nonlinear case, the logit, the diﬀerent in regression value will NOT depend only on the
diﬀerence between the predictor values, in this case HS12. We cannot simply say something like
“If two forest sites diﬀer in HS12 by 15, then the diﬀerence in probability of cover type 1 diﬀers by
such-and-such an amount.”

Instead we’ll have to choose two speciﬁc HS11 values for our illustration. Let’s use the estimated
mean HS12 values for the two cover types, 223.8 and then 226.3, found earlier.

So, in (23.2), let’s plug in our estimates 1.52 and -0.01 from our R output above, twice, ﬁrst with
HS12 = 223.8 and then with HS12 = 226.3

In other words, let’s imagine two forest sites, with unknown cover type, but known HS12 values
223.8 and 226.8 that are right in the center of the HS12 distribution for the two cover types. What
would we predict for the cover types to be for those two sites?

Plugging in to (23.2), the results are 0.328 and 0.322, respectively. Remember, these numbers
are the estimated probabilities that we have cover type 1, given HS12. So, our guess—predicting
whether we have cover type 1 or 2—isn’t being helped much by knowing HS12.

In other words, HS12 isn’t having much eﬀect on the probability of cover type 1, and so it cannot
be a good predictor of cover type.

And yet... the R output says that β1 is “signiﬁcantly” diﬀerent from 0, with a p-value of 0.03.
Thus, we see once again that signiﬁcance testing does not achieve our goal.

23.4. THE MULTICLASS CASE

451

23.4 The Multiclass Case

In classiﬁcation problems, we often have more than two classes. In the forest cover example above,
we simpliﬁed to having just two types of forest cover, but there were actually seven. In an op-
tical character recognition application, there may be dozens of classes, maybe even hundreds or
thousands for some written languages.

We will use m to denote the number of classes, and code the classes as 0, 1, ..., m − 1. Y will the
class number, and let

qi = P (Y = i) = P (class i), i = 0, 1, ..., m − 1

(23.18)

Note that the qi are unconditional probabilities.

23.4.1 One vs. All Approach

The methods remain basically the same. Consider the forest cover example, for instance. There
are seven cover types, so we could run seven logistic models, predicting a dummy Y for each type.
In the ﬁrst run, Y would be 1 for cover type 1, 0 for everything else. In predicting a new case, we
plug the predictor values into each of the seven models, and guess Y to be the one with maximal
probability among the seven.

The above approach is called One versus All (OVA). To see where the name comes from, again
consider the forest cover example, with seven logistic regression models. In the ith model, we are
predicting class i, against the collective alternative of all the other classes.

There is also the All versus All method (AVA). Here we run a regression analysis for each possible
pair of classes (7 × 6/2 = 21 of them for the forest cover data). For exach pair, we restrict our
analysis only to data for the two classes. In predicting a new case, we use a “voting” procedure:
For each pair of classes, we decide which of the two classes is more likely for this new case, and
then take our guess for the class to be whichever class “won” the most times.

AVA has the disadvantage of using much less data for each pair. But if for example we are using
a logistic model, which implies a linear boundary between each class and all the others,7, an
assumption that may not be very accurate. AVA essentially makes the weaker assumption that the
boundary is linear between each pair of classes.

Note that under either OVA or AVA, we are always predicting one class at a time. Let’s call this
the Two Classes Suﬃce Principle, which will have implications below.

7The boundary is implied by setting (23.11) to 0.5.

452

CHAPTER 23. CLASSIFICATION

There are multivariate versions of the logit model, but they make certain restrictive assumptions.

23.4.2 Issues of Data Balance

Here will discuss a topic that is usually glossed over in treatments of the classiﬁcation problem,
and indeed is often misunderstood in the machine learning (ML) research literature, where one sees
much questionable handwringing over “the problem of unbalanced data.” On the contrary, the real
problem is often that the data are balanced.

23.4.2.1 Statement of the Problem

Consider the UCI Letters Recognition data set (https://archive.ics.uci.edu/ml/datasets/
Letter+Recognition). There are between 700 and 800 cases for each English capital letter, which
does not reﬂect that wide variation in letter frequencies. The letter ’E’, for example, is more than
100 times as frequent as the letter ’Z’, according to published data (see below). This would be
considered “balanced” data, but it would be artiﬁcially so.

Now think of a diﬀerent version of that data, in which the letter frequencies reﬂect their true nature
in general English. There would be rather few instances of ’Z’ here, but lots of cases of ’A’. This
would be “unbalanced” but natural. And our predictions for data as a whole would be optimal in
the sense of minimizing overall misclassiﬁcation error (Section 23.6).

From here on, we will use the term unbalanced to mean that our data form a sample from our target
population, in this case the population of all English text. Then we can estimate the qi from the
data:

(cid:98)qi =

Ni
n

(23.19)

where Ni is the number of our data points in class i, and n is the total number of observations
in the data set. For balanced data, our data arose from sampling independently from the various
classes, without attempting to have our data’s class frequencies be representative of the population.

In the balanced case, due to a paucity of data for ’Z’, our misclassiﬁcation rate for that particular
letter (guessing another letter when the letter is actually ’Z’) might not be good. The latter issue
is the one of concern in the machine learning community.

In other words, for general prediction accuracy, unbalanced data is the best. If we are concerned
with misclassiﬁcation rates for speciﬁc classes, things are more complex.

23.4. THE MULTICLASS CASE

453

23.4.2.2 Solutions

There are really two problems to be solved:

(a) Our data is balanced, when it shouldn’t be, as with the Letter Recognition data.

(b) Our data is unbalanced, and we are concerned about misclassiﬁcation with the rarer classes.

We will assume just two classes here, per the Two Classes Suﬃce Principle we set up earlier. Let
q and 1 − q denote the class probabilities for Y = 1 and Y = 0.

Let’s ﬁrst treat the context of Problem (a), and consider the quantity q in (23.9). If our data were
balanced, we actually has an easy solution, if we know the correct value of q from some external
source. For instance, in the English letters example above, there is much published data, such as
at the Web page Practical Cryptography (http://practicalcryptography.com/cryptanalysis/
letter-frequencies-various-languages/english-letter-frequencies/). They ﬁnd that πA =
0.08, πB = 0.0160, πC = 0.0316 and so on. So, if we use logistic regression (26 times), then when
we run the one for ’C’, we take q = 0.0316.

Now recall from Section 23.2.1 that one of the motivations for the logistic model is that it holds if
the density of our predictor vector X is multivariate normal within each class (with equal covariance
matrices in each class). It turns out that Equation (23.12) holds for a general number of predictors,
not just 1 as in that equation.

If we then run glm() for our logistic regression, that function will act as if our data is unbalanced
(the situation it was designed for). That means that our (cid:98)β0 term will be incorrect, essentially using
(23.19) . This suggests the following adjustment procedure.

(i) Add ln(N i0/n).

(ii) Subtract ln[(1 − π)/π)], where π is the true class probability.

If we use a nonparametric approach (Chapter 24), Equation (23.9) shows us how to make the
necessary adjustment. When we have a new case to predict, we do the following:

(a) Our software has given us an estimate of the left-hand side of that equation, for t equal to

our new data point.

(b) We know the value that our software has used for its estimate of (1 − q)/q, which is N0/N1.

(c) Using (a) and (b), we can solve for the estimate of fo(t)/f1(t).

454

CHAPTER 23. CLASSIFICATION

(d) Now plug the correct estimate of (1 − q)/q, and the result of (c), back into (23.9) to get the

proper estimate of the desired conditional probability.

Now what about Problem (b) above, the unbalanced situation? The easiest solution is to deliber-
ately use the wrong value of q in the above adjustment procedure.

23.5 Model Selection in Classiﬁcation

Since, as has been emphasized here, classiﬁcation is merely a special case of regression, the same
issues arise for model selection as in Section 22.15. The only new issue is what to do with nonpara-
metric models. For instance, with k-Nearest Neighbor estimation, how do we choose k?

Here the standard technique is again, as in Section 22.15, to split the data into training and
validation sets. We could ﬁt models with various values of k to the training set, and then see how
well each does on the validation set.

The same points apply to deciding which predictors to use, and which to discard, as well as (in the
parametric model case) whether to add polynomial terms and so on.

23.6 Optimality of the Regression Function for 0-1-Valued Y (op-

tional section)

Remember, our context is that we want to guess Y, knowing X. Since Y is 0-1 valued, our guess
for Y based on X, g(X), should be 0-1 valued too. What is the best function g()?

Again, since Y and g are 0-1 valued, our criterion should be what will I call Probability of Correct
Classiﬁcation (PCC):8

We’ll show intuitively that the best rule, i.e. the g() that maximizes (23.20), is given by the function

PCC = P [Y = g(X)]

(23.20)

g(t) =

(cid:40)

0, if g(t) ≤ 0.5
1, if g(t) > 0.5

(23.21)

8This assumes that our goal is to minimize the overall misclassiﬁcation error rates, which in terms assumes equal
costs for the two kinds of classiﬁcation errors, i.e. that guessing Y = 1 when Y = 0 is no more or no less serious than
the opposite error.

23.6. OPTIMALITY OF THE REGRESSION FUNCTION FOR 0-1-VALUED Y (OPTIONAL SECTION)455

Think of this simple situation: There is a biased coin, with known probability of heads p. The coin
will be tossed once, and you are supposed to guess the outcome.

Let’s name your guess q, and let C denote the as-yet-unknown outcome of the toss (1 for heads, 0
for tails). Then the probability that you guess correctly is

P (C = q) = P (C = 1)q + P (C = 0)(1 − q)

= P (C = 1)q + [1 − P (C = 1)](1 − q)

= [2P (C = 1) − 1]q + 1 − P (C = 1)

= [2p − 1]q + 1 − p

(23.22)

(23.23)

(23.24)

(23.25)

(That ﬁrst equation merely accounts for the two cases, q = 1 and q = 0. For example, if you choose
q = 0, then the right-hand side reduces to P(C = 0), as it should.)

Inspecting the last of the three equations above, we see that if we set q = 1, then P(C = q), i.e.
the probability that we correctly predict the coin toss, is p. If we set q to 0, then P(C = q) is 1-p.
That in turn says that if p > 0.5 (remember, p is known), we should set q to 1; otherwise we should
set q to 0.

The above reasoning gives us very intuitive—actually trivial—result:

If the coin is biased toward heads, we should guess heads. If the coin is biased toward
tails, we should guess tails.

Now returning to (23.21), would take P(C = q) above as the conditional probability P(Y = g(X) |
X). The above coin example says we should predict Y to be 1 or 0, depending on whether g(X) is
larger or smaller than 0.5. Then use (4.68) to complete the proof.

Exercises

1. Suppose we are interested in documents of a certain type, which we’ll call Type 1. Everything
that is not Type 1 we’ll call Type 2, with a proportion q of all documents being Type 1. Our goal
will be to try to guess document type by the presence of absence of a certain word; we will guess
Type 1 if the word is present, and otherwise will guess Type 2.

Let T denote document type, and let W denote the event that the word is in the document. Also,
let pi be the proportion of documents that contain the word, among all documents of Type i, i =
1,2. The event C will denote our guessing correctly.

Find the overall probability of correct classiﬁcation, P (C), and also P (C|W ).

456

CHAPTER 23. CLASSIFICATION

Hint: Be careful of your conditional and unconditional probabilities here.

2. We showed that (23.9) reduces to the logistic model in the case in which the distribution of X
given Y is normal. Show that this is also true in the case in which that distribution is exponential,
i.e.

fX|Y (t, i) = λie−λit, t > 0

(23.26)

Chapter 24

Nonparametric Estimation of
Regression and Classiﬁcation
Functions

In some applications, there may be no good parametric model, say linear or logistic, for mY ;X . Or,
we may have a parametric model that we are considering, but we would like to have some kind of
nonparametric estimation method available as a means of checking the validity of our parametric
model. So, how do we estimate a regression function nonparametrically?

Many, many methods have been developed. We introduce a few here.

24.1 Methods Based on Estimating mY ;X(t)

To guide our intuition on this, let’s turn again to the example of estimating the relationship between
height and weight. Consider estimation of the quantity mW ;H (68.2), the population mean weight
of all people of height 68.2.

We could take our estimate of mW ;H (68.2), (cid:98)mW ;H (68.2), to be the average weight of all the people
in our sample who have that height. But we may have very few people of that height (or even
none), so that our estimate may have a high variance, i.e. may not be very accurate.

What we could do instead is to take the mean weight of all the people in our sample whose heights
are near 68.2, say between 67.7 and 68.7. That would bias things a bit, but we’d get a lower
variance. This is again an illustration of the variance/bias tradeoﬀ introduced in Section 21.1.3.

All nonparametric regression/classiﬁcation (or “machine learning”) methods work like this. There

457

458CHAPTER 24. NONPARAMETRIC ESTIMATION OF REGRESSION AND CLASSIFICATION FUNCTIONS

are many variations, but at their core they all have this same theme. (Again, note the Hillel quote
at the beginning of Section 23.1.)

As noted earlier, the classiﬁcation problem is a special case of regression, so in the following material
we will usually not distinguish between the two.

24.1.1 Nearest-Neighbor Methods

In Chapter??, we presented both kernel and nearest-neighbors for density estimation. The same
two approaches can be used to estimate regression functions.

In the nearest-neighbor approach, we for instance estimating mY ;X (68.2) to be the mean weight
of the k people in our sample with heights nearest 68.2. Here k controls bias/variance tradeoﬀ.

Note that if we have more than one predictor variable, the distance used to determine “nearest” is
multivariate, e.g. the distance in the plane in the case of two predictors.

In spite of the apparently simple notion here, nearest-neighbor regression and classiﬁcation methods
are quite eﬀective and popular. Several contributed packages on the CRAN site for R implement
this idea.

Here is simple (nonoptimized) code to do all this:

t h e u s e r has a

f i t t i n g t o t h e x , y d a t a s e t or u s i n g t h a t d a t a t o

r e g r e s s i o n ;

o b s e r v a t i o n

1 # t h e f u n c t i o n knn ( ) d o e s k−n e a r e s t n e i g h b o r
2 # c h o i c e o f e i t h e r
j u s t
3 # p r e d i c t new o b s e r v a t i o n s newobs f o r which o n l y t h e p r e d i c t o r s a r e
4 # known
5
6 # arguments :
7
8 # x : m a t r i x or d a t a frame o f
9 #
10 #
11 # y :
12 #
13 #
14 # k :
15 #
16 #
17 # newobs :
18 #
19 #
20 # r e g t y p e :

t h e number o f n e a r e s t n e i g h b o r s
or p r e d i c t i n g t h e new d a t a

” r e g ” f o r p r e d i c t i o n o f c o n t i n u o u s v a r i a b l e s , ” c l s ” f o r

a m a t r i x o f v a l u e s o f
on which t o p r e d i c t

v e c t o r o f
c l a s s i f i c a t i o n case ,

t h e r e s p o n s e v a r i a b l e s c o r r e s p o n d i n g t o x ;

t h e s e a r e assumed t o be 1 s and 0 s

t h e r e s p o n s e s ; d e f a u l t v a l u e i s NULL

i n t h e

t h e p r e d i c t o r v a r i a b l e data , one row p e r

t h e p r e d i c t o r s , one row p e r o b s e r v a t i o n ,

t o use f o r e s t i m a t i n g t h e r e g r e s s i o n

24.1. METHODS BASED ON ESTIMATING MY ;X (T )

459

t h e r e g r e s s i o n f u n c t i o n a t x

f o r y from newobs

t h e n Rˆ2 i n t h e ” r e g ” case , p r o p o r t i o n

i n t h e ” c l s ” c a s e ;

i s NULL,

r e g v a l s :

p r e d v a l s :

p r e d s u c c e s s :

f i n d e r on CRAN

e s t i m a t e d v a l u e s o f

i s not NULL, p r e d i c t e d v a l u e s

i f newobs
o t h e r w i s e NULL

( i s . vector ( x ) ) x <− matrix ( x , ncol =1)

l i b r a r y (RANN) # f a s t n e a r e s t −n e i g h b o r

c l a s s i f i c a t i o n p r o b l e m s ; d e f a u l t v a l u e ” r e g ”

i f newobs
o f c o r r e c t l y c l a s s i f i e d o b s e r v a t i o n s
o t h e r w i s e NULL

# make s u r e x i s a m a t r i x or d a t a frame f o r use w i t h RANN
i f
r e t v a l <− l i s t ( )
# j u s t
i f

21 #
22 #
23
24 # r e t u r n v a l u e : an R l i s t w i t h t h e f o l l o w i n g components
25 #
26 #
27 #
28 #
29 #
30 #
31 #
32 #
33 #
34
35
36
37 knn <− function ( x , y , k , newobs=NULL, r e g t y p e=” r e g ” ) {
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

}
# row i o f nea ron es now c o n s i s t s o f
# o b s e r v a t i o n s
#
# now f i n d t h e e s t i m a t e d r e g r e s s i o n f u n c t i o n a t each row
r e g v a l s <− apply ( n e a r o n e s , 1 , pred1y , y )
i f

( i s . null ( newobs ) ) {
n e a r o n e s <− nn2 ( data=x , k=k , query=x ) $nn . i d x

p r e d v a l s <− as . integer ( r e g v a l s > 0 . 5 )
p r e d s u c c e s s <− mean( p r e d v a l s == y )

( r e g t y p e==” r e g ” ) {
tmp <− cor ( r e g v a l s , y )
p r e d s u c c e s s <− tmpˆ2

n e a r o n e s <− nn2 ( data=x , k=k , query=newobs ) $nn . i d x

( i s . null ( newobs ) ) {
i f

i n x t o row i o f x or row i o f newobs

}
p r e d v a l s <− NULL

t r y i n g o u t on c u r r e n t d a t a s e t ?

t h e i n d i c e s

} e l s e {

} e l s e {

i n x o f

t h e k c l o s e s t

460CHAPTER 24. NONPARAMETRIC ESTIMATION OF REGRESSION AND CLASSIFICATION FUNCTIONS

}

} e l s e {

( r e g t y p e == ” r e g ” ) p r e d v a l s <− n e w r e g v a l s e l s e {
p r e d v a l s <− as . integer ( r e g v a l s > 0 . 5 )

p r e d s u c c e s s <− NULL
n e w r e g v a l s <− apply ( n e a r o n e s , 1 , pred1y , y )
i f

61
62
63
64
65
66
67
68
69
70
71
72 }
73
74 # f o r a s i n g l e o b s e r v a t i o n , c a l c u l a t e t h e v a l u e o f
75 # f u n c t i o n t h e r e , knowing t h e i n d i c e s x i d x s o f
76 # o r i g i n a l d a t a x t h a t a r e c l o s e s t
77 pred1y <− function ( x i d x s , y ) p r e d v a l <− mean( y [ x i d x s ] )

}
r e t v a l $ r e g v a l s <− r e g v a l s
r e t v a l $ p r e d v a l s <− p r e d v a l s
r e t v a l $ p r e d s u c c e s s <− p r e d s u c c e s s
r e t v a l

t h e v a l u e s

t o t h e g i v e n o b s e r v a t i o n

t h e r e g r e s s i o n

i n t h e

24.1.2 Kernel-Based Methods

As our deﬁnition of “near,” we could take all people in our sample whose heights are within h
amount of 68.2. This should remind you of our density estimators in Chapter 20. A generalization
would be to use a kernel method. For instance, for univariate X and t:

(cid:98)mY ;X (t) =

(cid:80)n

i=1 Yik
(cid:80)n

i=1 k

(cid:17)

(cid:16) t−Xi
h
(cid:16) t−Xi
h

(cid:17)

(24.1)

Again note that if we have more than one predictor variable, the function k() has a multivariate
argument.

Here k() is a density, i.e. a nonnegative function that integrates to 1. Also, it is almost always
chosen so that k() is symmetric around 0, with a peak at 0 and then tapering oﬀ as one moves
away from 0 in either direction.

This looks imposing! But it is simply a weighted average of the Y values in our sample, with the
larger weights being placed on observations for which X is close to t.

Note the word chosen. The analyst makes this choice (or takes a default value, say in an R library),
simply from considerations of weighting: Choosing k() to be a “tall, narrow” function will make
the weights drop oﬀ more rapidly to 0.

24.1. METHODS BASED ON ESTIMATING MY ;X (T )

461

In fact, the choice of kernel is not very important (often it is taken to be the N(0,1) density.) What
does matter is the parameter h. The smaller h is, the more our weighting will concentrate on nearby
observations.

In other words, setting a smaller value of h is quite analogous to choosing a smaller value of k (the
number of nearest neighbors, not our kernel function here) in nearest-neighbor regression.

As before, the choice of h here involves a bias/variance tradeoﬀ. We might try choosing h via cross
validation, as discussed in Section 22.15.4.

There is an R package that includes a function nkreg() for kernel regression. The R base has a
similar method, called LOESS. Note: That is the class name, but the R function is called lowess().

24.1.3 The Naive Bayes Method

This method is for the classiﬁcation problem only.

The Naive Bayes method is not “Bayesian” in the sense of Section 18.4. Instead, its name comes
It basically makes the same
simply from its usage of Bayes’ Rule for conditional probability.
computations as in Section 23.2.1, for the case in which the predictors are indicator variables and
are independent of each other, given the class.

The term naive is an allusion to analysts who naively assume independent predictors, without
realizing that they are making a serious restriction.

Under that assumption, the numerator in (23.8) becomes

P (Y = 1) P [X (1) = t1|Y = 1]

... P [X (r) = tr|Y = 1]

(24.2)

All of those quantities (and similarly, those in the denominator of (23.8) can be estimated directly
as sample proportions. For example, (cid:98)P [X (1) = t1|Y = 1] would be the fraction of X (1)
that are
equal to t1, among those observations for which Yj = 1.

j

A common example of the use of Naive Bayes is text mining, as in Section 13.5.1.4. Our indepen-
dence assumption in this case means that the probability that, for instance, a document of a certain
class contains both of the words baseball and strike is the product of the individual probabilities of
those words.

Clearly the independence assumption is not justiﬁed in this application. But if our vocabulary
is large, that assumption limits the complexity of our model, which may be necessary from a
bias/variance tradeoﬀ point of view (Section 21.1.3).

462CHAPTER 24. NONPARAMETRIC ESTIMATION OF REGRESSION AND CLASSIFICATION FUNCTIONS

24.2 Methods Based on Estimating Classiﬁcation Boundaries

In the methods presented above, we are estimating the function mY ;X (t). But with support vector
machines and CART below, we are in a way working backwards. In the classiﬁcation case (which is
what we will focus on), for instance, our goal is to estimate the values of t for which the regression
function equals 0.5:

B = {t : mY ;X (t) = 0.5}

(24.3)

Recall that r is the number of predictor variables we have. Then note the geometric form that the
set B in (24.3) will take on: discrete points if r = 1; a curve if r = 2; a surface if r = 3; and a
hypersurface if r > 3.

The motivation for using (24.3) stems from the fact, noted in Section 23.1, that if we know mY ;X (t),
we will predict Y to be 1 if and only if mY ;X (t) > 0.5. Since (24.3) represents the boundary between
the portions of the X space for which mY ;X (t) is either larger or smaller than 0.5, it is the boundary
for our prediction rule, i.e. the boundary separating the regions in X space in which we predict Y
to be 1 or 0.

Lest this become too abstract, again consider the simple example of predicting gender from height
and weight. Consider the (u,v) plane, with u and v representing height and weight, respectively.
Then (24.3) is some curve in that plane. If a person’s (height,weight) pair is on one side of the
curve, we guess that the person is male, and otherwise guess female.

If the logistic model (23.2) holds, then that curve is actually a straight line. To see this, note that
in (23.2), the equation (24.3) boils down to

β0 + β1u + β2v = 0

(24.4)

whose geometric form is a straight line.

24.2.1 Support Vector Machines (SVMs)

This method has been getting a lot of publicity in computer science circles (maybe too much; see
below). It is better explained for the classiﬁcation case.

In the form of dot product (or inner product) from linear algebra, (24.4) is

(β1, β2)(cid:48)(u, v) = −β0

(24.5)

24.2. METHODS BASED ON ESTIMATING CLASSIFICATION BOUNDARIES

463

What SVM does is to generalize this, for instance changing the criterion to, say

β0u2 + β1uv + β2v2 + β3u + β4v = 1

(24.6)

Now our (u, v) plane is divided by a curve instead of by a straight line (though it includes straight
lines as special cases), thus providing more ﬂexibility and thus potentially better accuracy.

In SVM terminology, (24.6) uses a diﬀerent kernel than regular dot product. (This of course should
not be confused our the term kernel in kernel-based regression above.) The actual method is more
complicated than this, involving transforming the original predictor variables and then using an
ordinary inner product in the transformed space. In the above example, the transformation consists
of squaring and multiplying our variables. That takes us from two-dimensional space (just u and
v) to ﬁve dimensions (u, v, u2, v2 and uv).

There are various other details that we’ve omitted here, but the essence of the method is as shown
above.

Of course, a good choice of the kernel is crucial to the successful usage of this method. It is the
analog of h and k in the nearness-based methods above.

A former UCD professor, Nello Cristiani, is one of the world leaders in SVM research. See An
Introduction to Support Vector Machines, N. Cristianini and J. Shawe-Taylor, Cambridge University
Press, 2000.

24.2.2 CART

Another nonparametric method is that of Classiﬁcation and Regression Trees (CART). It’s
again easiest explained in the classiﬁcation context, say the diabetes example above.

In the diabetes example, we might try to use glucose variable as our ﬁrst predictor. The data may
show that a high glucose value implies a high likelihood of developing diabetes, while a low value
does the opposite. We would then ﬁnd a split on this variable, meaning a a cutoﬀ value that deﬁnes
“high” and “low.” Pictorially, we draw this as the root of a tree, with the left branch indicating a
tentative guess of no diabetes and the right branch corresponding to a guess of diabetes.

Actually, we could do this for all our predictor variables, and ﬁnd which one produces the best split
at the root stage. But let’s assume that we ﬁnd that glucose is that variable.

Now we repeat the process. For the left branch—all the subset of our data corresponding to “low”
glucose—we ﬁnd the variable that best splits that branch, say body mass index. We do the same
for the right branch, say ﬁnding that age gives the best split. We keep going until the resulting
cells are too small for a reasonable split.

464CHAPTER 24. NONPARAMETRIC ESTIMATION OF REGRESSION AND CLASSIFICATION FUNCTIONS

An example with real data is given in a tutorial on the use of rpart, an R package that does
analysis of the CART type, An Introduction to Recursive Partitioning Using the RPART Routines,
by Terry Therneau and Elizabeth Atkinson. The data was on treatment of cardiac arrest patients
by emergency medical technicians.

The response variable here is whether the technicians were able to revive the patient, with predictors
X (1) = initial heart rhythm, X (2) = initial response to defrillabration, and X (3) = initial response
to drugs. The resulting tree was

So, if for example a patient has X (1) = 1 and X (2) = 3, we would guess him to be revivable.

CART is a boundary method, as SVM is. Say for instance we have two variables, represented
graphically by s and t, and our root node rule is s > 0.62. In the left branch, the rule is t > 0.8
and in the right branch it’s t > 0.58. This boils down to a boundary line as follows:

24.3. COMPARISON OF METHODS

465

CART obviously has an intuitive appeal, easily explained to nonstatisticians, and easy quite easy
to implement. It also has the virtue of working equally well with discrete or continuous predictor
variables.

The analogs here of the h in the kernel method and k in nearest-neighbor regression are the choice
of where to deﬁne the splits, and when to stop splitting. Cross validation is often used for making
such decisions.

24.3 Comparison of Methods

Beware! There are no “magic” solutions to statistical problems. The statements one
sees by some computer science researchers to the eﬀect that SVMs are generally superior to other
prediction methods are, unfortunately, unfounded; there just is no generally superior method.

First, note that every one of the above methods involves some choice of tuning parameter, such as

0.00.20.40.60.81.00.00.20.40.60.81.0st466CHAPTER 24. NONPARAMETRIC ESTIMATION OF REGRESSION AND CLASSIFICATION FUNCTIONS

h in the kernel method, k in the nearest-neighbor method, the split points in CART, and in the
case of SVM, the form of kernel to use. For SVM the choice of kernel is crucial, yet diﬃcult.

Second, the comparisons are often unfair, notably comparisons of the logit model to SVM. Such
comparisons usually limit the logit experiments to ﬁrst-degree terms without interactions. But the
kernel in SVM is essentially analogous to throwing in second-degree and interaction terms, and so
oni, (23.2) for the logit case, thus producing a curved partitioning line just like SVM does.

I highly recommend the site www.dtreg.com/benchmarks.htm, which compares six diﬀerent types
of classiﬁcation function estimators—including logistic regression and SVM—on several dozen real
data sets. The overall percent misclassiﬁcation rates, averaged over all the data sets, was fairly
close, ranging from a high of 25.3% to a low of 19.2%. The much-vaunted SVM came in at an
overall score across all data sets of 20.3%. That’s nice, but it was only a tad better than logit’s
20.9%—and remember, that’s with logit running under the handicap of having only ﬁrst-degree
terms.

Or consider the annual KDDCup competition, in which teams from around the world compete to
solve a given classiﬁcation problem with the lowest misclassiﬁcation rate. In KDDCup2009, for
instance, none of the top teams used SVM. See SIGKDD Explorations, December 2009 issue.

Considering that logit has a big advantage in that one gets an actual equation for the classiﬁcation
function, complete with parameters which we can estimate and make conﬁdence intervals for, it
is not clear just what role SVM and the other nonparametric estimators should play, in general,
though in speciﬁc applications they may be appropriate.

Chapter 25

Relations Among Variables

It is a very sad thing that nowadays there is so little useless information—Oscar Wilde, famous
19th century writer

Unlike the case of regression analysis, where the response/dependent variable plays a central role,
we are now interested in symmetric relations among several variables. Often our goal is dimension
reduction, meaning compressing our data into just a few important variables.

Dimension reduction ties in to the Oscar Wilde quote above, which is a complaint that there is
too much information of the useful variety. We are concerned here with reducing the complexity of
that information to a more manageable, simple set of variables.

Here we cover two of the most widely-used methods, principal components analysis for contin-
uous variables, and the log-linear model for the discrete case. We also introduce clustering.

25.1 Principal Components Analysis (PCA)

Consider a random vector X = (X1, X2)(cid:48). Suppose the two components of X are highly correlated
with each other. Then for some constants c and d,

X2 ≈ c + dX1

(25.1)

Then in a sense there is really just one random variable here, as the second is nearly equal to some
linear combination of the ﬁrst. The second provides us with almost no new information, once we
have the ﬁrst.

In other words, even though the vector X roams in two-dimensional space, it usually sticks close to

467

468

CHAPTER 25. RELATIONS AMONG VARIABLES

a one-dimensional object, namely the line (25.1). We saw a graph illustrating this in our chapter
on multivariate distributions, page 244.

In general, consider a k-component random vector

X = (X1, ..., Xk)(cid:48)

(25.2)

We again wish to investigate whether just a few, say w, of the Xi tell almost the whole story, i.e.
whether most Xj can be expressed approximately as linear combinations of these few Xi. In other
words, even though X is k-dimensional, it tends to stick close to some w-dimensional subspace.

Note that although (25.1) is phrased in prediction terms, we are not (or more accurately, not
necessarily) interested in prediction here. We have not designated one of the X (i) to be a response
variable and the rest to be predictors.

Once again, the Principle of Parsimony is key. If we have, say, 20 or 30 variables, it would be nice
if we could reduce that to, for example, three or four. This may be easier to understand and work
with, albeit with the complication that our new variables would be linear combinations of the old
ones.

25.1.1 How to Calculate Them

Here’s how it works. Let Σ denote the covariance matrix of X. The theory of linear algebra says
that since Σ is a symmetric matrix, it is diagonalizable, i.e. there is a real matrix Q for which

Q(cid:48)ΣQ = D

(25.3)

(A related approach is singular value decomposition.) The
where D is a diagonal matrix.
columns Ci of Q are the eigenvectors of Σ, and it turns out that they are orthogonal to each other,
i.e. their dot product is 0.

Let

Wi = C(cid:48)

iX, i = 1, ..., k

so that the Wi are scalar random variables, and set

W = (W1, ..., Wk)(cid:48)

(25.4)

(25.5)

25.1. PRINCIPAL COMPONENTS ANALYSIS (PCA)

Then

W = Q(cid:48)X

469

(25.6)

Now, use the material on covariance matrices from our chapter on random vectors, page 213,

Cov(W ) = Cov(Q(cid:48)X) = Q(cid:48)Cov(X)Q = D (from (25.3))

(25.7)

Note too that if X has a multivariate normal distribution (which we are not assuming), then W
does too.

Let’s recap:

• We have created new random variables Wi as linear combinations of our original Xj.

• The Wi are uncorrelated. Thus if in addition X has a multivariate normal distribution, so

that W does too, then the Wi will be independent.

• The variance of Wi is given by the ith diagonal element of D.

The Wi are called the principal components of the distribution of X.

It is customary to relabel the Wi so that W1 has the largest variance, W2 has the second-largest,
and so on. We then choose those Wi that have the larger variances, and discard the others, because
the latter, having small variances, are close to constant and thus carry no information.

Note that an alternate deﬁnition of the ﬁrst principal component is a value of u that maximizes
u(cid:48)X subject to u having length 1. The second principal component maximizes u(cid:48)X subject to u
having length 1 and subject to the second component being orthogonal to the ﬁrst, and so on.

All this will become clearer in the example below.

25.1.2 Example: Forest Cover Data

Let’s try using principal component analysis on the forest cover data set we’ve looked at before.
There are 10 continuous variables.1

In my R run, the data set (not restricted to just two forest cover types, but consisting only of the
ﬁrst 1000 observations) was in the object f. Here are the call and the results:

1There are also many discrete ones.

470

CHAPTER 25. RELATIONS AMONG VARIABLES

> prc <- prcomp(f[,1:10])
> summary(prc)
Importance of components:

Standard deviation
Proportion of Variance
Cumulative Proportion

PC1

PC2

PC3

PC4

PC5 PC6

1812.394 1613.287 1.89e+02 1.10e+02 96.93455 30.16789

0.438 6.01e-03 2.04e-03
0.990 9.96e-01 9.98e-01

0.00158 0.00015
0.99968 0.99984

0.552
0.552
PC7

PC10
Standard deviation
25.95478 16.78595 4.2 0.783
Proportion of Variance 0.00011 0.00005 0.0 0.000
0.99995 1.00000 1.0 1.000
Cumulative Proportion

PC8 PC9

You can see from the variance values here that R has scaled the Wi so that their variances sum to
1.0. (It has not done so for the standard deviations, which are for the nonscaled variables.) This
is ﬁne, as we are only interested in the variances relative to each other, i.e. saving the principal
components with the larger variances.

What we see here is that eight of the 10 principal components have very small variances, i.e. are
close to constant. In other words, though we have 10 variables X1, ..., X10, there is really only two
variables’ worth of information carried in them.

So for example if we wish to predict forest cover type from these 10 variables, we should only use
two of them. We could use W1 and W2, but for the sake of interpretability we stick to the original X
vector. We can use any two of the Xi, though typically it would be two that have large coeﬃcients
in the top two principal components..

The coeﬃcients of the linear combinations which produce W from X, i.e. the Q matrix, are available
via prc$rotation.

25.1.3 Scaling

If your original variables range quite a bit in variance, you should have prcomp() scale them ﬁrst,
so they all have standard deviation 1.2 The argument name is scale, of course.

Without scaling, the proportion-of-total-variance type of analysis discussed above may be mislead-
ing, as large-variance variables may dominate.

25.1.4 Scope of Application

PCA makes no assumptions about the data. It is strictly an exploratory/descriptive tool.

However, it should be noted that the motivation we presented for PCA at the beginning of this
chapter involved correlations among our original variables. This is further highlighted by the fact

2And mean 0, though this is irrelevant, as Σ is all that matter.

25.1. PRINCIPAL COMPONENTS ANALYSIS (PCA)

471

that the PCs are calculated based on the covariance matrix of the data, which except for scale is
the same as the correlation matrix.

This in turn implies that each variable is at least ordinal in nature, i.e. that it makes sense to speak
of the impact of larger or smaller values of a variable.

Note, though, that an indicator random variable is inherently ordinal! So, if you have a categorical
variable, i.e. one that simply codes what category an individual falls into (such as Democratic,
Republican, independent), then you can convert it to a set of indicator variables, and potentially
get some insight into the relation between this variable and others.

This can be especially valuable if, as is often the case, your data consists of a mixture of ordinal
and categorical variables.

25.1.5 Example: Turkish Teaching Evaluation Data

This data, again from the UCI Machine Learning Repository, was introduced in Section 22.17.
Let’s try PCA on it:

> t p c a <− prcomp ( turk , s c a l e=T)
> summary( t p c a )
Importance o f components :

PC1

PC2

PC3

PC4

PC5

PC6

PC7
Standard d e v i a t i o n
4 . 8 0 0 8 1 . 1 2 9 6 0 . 9 8 8 2 7 0 . 6 2 7 2 5 0 . 5 9 8 3 7 0 . 5 3 8 2 8 0 . 5 0 5 8 7
P r o p o r t i o n o f V a r i a n c e 0 . 7 9 4 7 0 . 0 4 4 0 0 . 0 3 3 6 8 0 . 0 1 3 5 7 0 . 0 1 2 3 5 0 . 0 0 9 9 9 0 . 0 0 8 8 2
0 . 7 9 4 7 0 . 8 3 8 7 0 . 8 7 2 4 2 0 . 8 8 5 9 8 0 . 8 9 8 3 3 0 . 9 0 8 3 2 0 . 9 1 7 1 4
Cumulative P r o p o r t i o n

PC8

PC9

PC10

PC11

PC12

PC13

PC14
Standard d e v i a t i o n
0 . 4 5 1 8 2 0 . 4 2 7 8 4 0 . 4 1 5 1 7 0 . 3 7 7 3 6 0 . 3 7 1 6 1 0 . 3 6 9 5 7 0 . 3 4 5 0
P r o p o r t i o n o f V a r i a n c e 0 . 0 0 7 0 4 0 . 0 0 6 3 1 0 . 0 0 5 9 4 0 . 0 0 4 9 1 0 . 0 0 4 7 6 0 . 0 0 4 7 1 0 . 0 0 4 1
0 . 9 2 4 1 8 0 . 9 3 0 5 0 0 . 9 3 6 4 4 0 . 9 4 1 3 5 0 . 9 4 6 1 1 0 . 9 5 0 8 2 0 . 9 5 4 9
Cumulative P r o p o r t i o n

PC15

PC16

PC17

PC18

PC19

PC20

PC21
0 . 3 4 1 1 4 0 . 3 3 7 9 2 0 . 3 3 1 1 0 0 . 3 2 5 0 7 0 . 3 1 6 8 7 0 . 3 0 8 6 7 0 . 3 0 4 6
Standard d e v i a t i o n
P r o p o r t i o n o f V a r i a n c e 0 . 0 0 4 0 1 0 . 0 0 3 9 4 0 . 0 0 3 7 8 0 . 0 0 3 6 4 0 . 0 0 3 4 6 0 . 0 0 3 2 9 0 . 0 0 3 2
0 . 9 5 8 9 4 0 . 9 6 2 8 8 0 . 9 6 6 6 6 0 . 9 7 0 3 0 0 . 9 7 3 7 6 0 . 9 7 7 0 5 0 . 9 8 0 2
Cumulative P r o p o r t i o n

PC22

PC23

PC24

PC25

PC26

PC27

PC28
Standard d e v i a t i o n
0 . 2 9 0 8 3 0 . 2 9 0 3 5 0 . 2 8 3 6 3 0 . 2 7 8 1 5 0 . 2 6 6 0 2 0 . 2 6 0 2 3 0 . 2 3 6 2 1
P r o p o r t i o n o f V a r i a n c e 0 . 0 0 2 9 2 0 . 0 0 2 9 1 0 . 0 0 2 7 7 0 . 0 0 2 6 7 0 . 0 0 2 4 4 0 . 0 0 2 3 4 0 . 0 0 1 9 2
0 . 9 8 3 1 6 0 . 9 8 6 0 7 0 . 9 8 8 8 4 0 . 9 9 1 5 1 0 . 9 9 3 9 5 0 . 9 9 6 2 9 0 . 9 9 8 2 1
Cumulative P r o p o r t i o n

472

CHAPTER 25. RELATIONS AMONG VARIABLES

PC29
Standard d e v i a t i o n
0 . 2 2 7 7 3
P r o p o r t i o n o f V a r i a n c e 0 . 0 0 1 7 9
1 . 0 0 0 0 0
Cumulative P r o p o r t i o n

This is remarkable—the ﬁrst PC accounts for 79% of the variance of the set of 29 variables. In
other words, in spite of the survey asking supposedly 29 diﬀerent aspects of the course, they can
be summarized largely in just one variable. Let’s see what that variable is:

> t p c a $ r o t a t i o n [ , 1 ]
Q1

Q3

Q2

Q7

Q6
−0.16974120 −0.18551431 −0.18553930 −0.18283025 −0.18973563 −0.18635256
Q12
−0.18730028 −0.18559928 −0.18344211 −0.19241585 −0.18388873 −0.18184118
Q18
−0.19430111 −0.19462822 −0.19401115 −0.19457451 −0.18249389 −0.19320936
Q24
−0.19412781 −0.19335127 −0.19232101 −0.19232914 −0.19554282 −0.19328500

Q22

Q20

Q14

Q16

Q10

Q19

Q13

Q15

Q23

Q21

Q17

Q11

Q8

Q9

Q4

Q5

d i f f i c u l t y
−0.19203359 −0.19186433 −0.18751777 −0.18855570 −0.01712709

Q26

Q28

Q25

Q27

This is even more remarkable. Except for the “diﬃculty” variable, all the Qi have about the same
coeﬃcients (the same loadings). In other words, just one question would have been enough, and
it wouldn’t matter mcuh which one were used.

The second PC, though only accounting for 4% of the total variation, is still worth a look:

> t p c a $ r o t a t i o n [ , 2 ]
Q1
0 . 3 2 0 0 9 8 5 0
Q7
0 . 2 4 0 2 5 0 4 6
Q13

Q2
0 . 2 2 0 4 6 4 6 8
Q8
0 . 2 4 4 7 7 5 4 3
Q14

Q6
0 . 1 9 8 9 0 4 7 1
Q12
0 . 2 0 8 8 1 7 7 3
Q18
−0.09943140 −0.15193169 −0.15089563 −0.03494282 −0.26163096 −0.11646066
Q24
−0.14424468 −0.18729978 −0.21208705 −0.21650494 −0.09349599 −0.05372049

Q5
0 . 2 0 2 3 6 3 7 2
Q11
0 . 1 1 0 6 4 5 2 3
Q17

Q3
0 . 1 1 4 3 2 0 2 8
Q9
0 . 1 3 1 9 8 0 6 0
Q15

Q4
0 . 2 3 3 4 0 3 4 7
Q10
0 . 1 9 2 3 9 2 0 7
Q16

Q22

Q20

Q19

Q21

Q23

d i f f i c u l t y
−0.20342350 −0.10790888 −0.05928032 −0.20370705 −0.27672177

Q27

Q26

Q25

Q28

Here the “diﬃculty” variable now shows up, and some of the Qi become unimportant.

25.2. LOG-LINEAR MODELS

473

25.2 Log-Linear Models

Here we discuss a procedure which is something of an analog of principal components for discrete
variables, especially nominal ones, i.e. variables we would convert to dummies in a regression
setting.

25.2.1 The Setting

Let’s consider a variation on the software engineering example in Section 22.2. Assume we have
the factors, IDE, Language and Education. One change—of extreme importance—is that we
will now assume that these factors are random. What does this mean?

In the original example described in Section 22.2, programmers were assigned to languages, and
in our extensions of that example, we continued to assume this. Thus for example the number of
programmers who use an IDE and program in Java was ﬁxed; if we repeated the experiment, that
number would stay the same. If we were sampling from some programmer population, our new
sample would have new programmers, but the number using and IDE and Java would be the same
as before, as our study procedure speciﬁes this.

By contrast, let’s now assume that we simply sample programmers at random, and ask them
whether they prefer to use an IDE or not, and which language they prefer.3 Then for example the
number of programmers who prefer to use an IDE and program in Java will be random, not ﬁxed;
if we repeat the experiment, we will get a diﬀerent count.

Suppose we now wish to investigate relations between the factors. Are choice of platform and
language related to education, for instance?

25.2.2 The Data

Denote our three factors by X (s), s = 1,2,3.4 Here X (1), IDE, will take on the values 1 and 2
instead of 1 and 0 as before, 1 meaning that the programmer prefers to use an IDE, and 2 meaning
not so. X (3), Education, changes this way too, and X (2) will take on the values 1 for C++, 2 for
Java and 3 for C.

Note that we no longer use indicator variables.
Indeed, the log-linear model is in contrast to
PCA, which as noted above tacitly assumes ordinal variables. Here we are working with strictly
categorical variables, whose values are merely labels. We could, for example, relabel X (2) to have
the value 1 for Java, 2 for C++ and 3 for C, and yet we would still have the same results (though

3Other sampling schemes are possible too.
4All the examples here will have three factors, but there can be any number.

474

CHAPTER 25. RELATIONS AMONG VARIABLES

the results would be relabeled too).

r denote the value of X (s) for the rth programmer in our sample, r = 1,2,...,n. Our data are

Let X (s)
the counts

Nijk = number of r such that X (1)

r = i, X (2)

r = j and X (3)

r = k

(25.8)

For instance, if we sample 100 programmers, our data might look like this:

prefers to use IDE:

C++
Java
C

Bachelor’s or less
18
22
6

Master’s or more
15
10
4

prefers not to use IDE:

C++
Java
C

Bachelor’s or less
7
6
3

Master’s or more
4
2
3

So for example N122 = 10 and N212 = 4.

Here we have a three-dimensional contingency table. Each Nijk value is a cell in the table.

25.2.3 The Models

Let pijk be the population probability of a randomly-chosen programmer falling into cell ijk, i.e.

(cid:16)

pijk = P

X (1) = i and X (2) = j and X (3) = k

(cid:17)

= E(Nijk)/n

(25.9)

As mentioned, we are interested in relations among the factors, in the form of independence, both
full and partial. Indeed, it is common for an analyst to ﬁt successively more reﬁned models to the
data, each assuming a more complex dependence structure than the last. This will unfold in detail
below.

Consider ﬁrst the model that assumes full independence:

pijk = P

= P

(cid:16)

(cid:16)

X (1) = i and X (2) = j and X (3) = k

(cid:17)

X (1) = i

(cid:17)

· P

(cid:16)

X (2) = j

(cid:17)

· P

(cid:16)

X (3) = k

(cid:17)

(25.10)

(25.11)

25.2. LOG-LINEAR MODELS

475

Taking logs of both sides in (25.11), we see that independence of the three factors is equivalent to
saying

for some numbers ai, bj and cj; e.g.

log(pijk) = ai + bj + ck

b2 = log[P (X (2) = 2)]

(25.12)

(25.13)

The point is that (25.12) looks like our no-interaction linear regression models, whose analog of
noninteraction here is independence of our variables. On the other hand, if we assume instead that
Education is independent of IDE and Language but that IDE and Language are not independent
of each other, our model would include an i-j interaction term, as follows.5

We would have

so we would set

pijk = P

(cid:16)

X (1) = i and X (2) = j

(cid:17)

· P

(cid:16)

X (3) = k

(cid:17)

(25.14)

Most formal models rewrite this as

log(pijk) = aij + bk

aij = u + vi + wj + rij

(25.15)

(25.16)

Here we have written P (cid:0)X (1) = i and X (2) = j(cid:1) as a sum of an “overall eﬀect” u, “main eﬀects”
vi and wj, and “interaction eﬀects,” rij, again analogous to linear regression.

Note, though, that this actually gives us too many parameters. For the i-j part of the model, we
have 2 × 3 = 6 actual probabilities, but 1 + 2 + 3 + 2 × 3 = 12 parameters (1 for u, 2 for the vi
and so on). So the model needs constraints, e.g.

5In order to simplify the discussion below, we will often write i as a shorthand for X (i) and so on.

(cid:88)

vi = 0

(25.17)

476

CHAPTER 25. RELATIONS AMONG VARIABLES

This is similar to classical Analysis of Variance (ANOVA), not covered in this book. We will not
state the constraints below, but they are used in most if not all software packages for the log-linear
model..

Another possible model would have IDE and Language conditionally independent, given Education,
meaning that at any level of education, a programmer’s preference to use IDE or not, and his choice
of programming language, are not related. We’d write the model this way:

pijk = P

= P

= P

(cid:16)

(cid:16)

(cid:16)

X (1) = i and X (2) = j and X (3) = k

(cid:17)

X (1) = i and X (2) = j | X (3) = k

(cid:17)

· P

(cid:16)

X (3) = k

(cid:17)

X (1) = i | X (3) = k

(cid:17)

· P

(cid:16)

X (2) = j | X (3) = k

(cid:17)

· P

(cid:16)

X (3) = k

(cid:17)

(25.18)

(25.19)

(25.20)

and thus set

log(pijk) == u + ai + fik + bj + hjk + ck

(25.21)

25.2.4 Interpretation of Parameters

Note carefully that the type of independence in (25.21) has a quite diﬀerent interpretation than
that in (25.15). Actually, our old trick coin example, Section 19.1, was like (25.20); given the
choice of coin, the Bi were independent. On the other hand, among people, height and weight are
correlated but they are presumably independent of preference for ﬂavor of ice cream, a situation
like (25.15).

This is an excellent example of what can go wrong with mindless use of packaged software. Log-
linear analysis is all about distinguishing between various kinds of dependence. Without an under-
standing of how the models reﬂect this, the software can produce highly misleading results.

So, pay close attention to which interactions are in the model, and which are not. In (25.15) we
see a two-factor interaction between the i and j factors, but no interaction with k. So i and j are
being modeled as completely independent of k, though not with each other. On the other hand, in
(25.21), the i and j factors have interactions with k, but not with each other.

Now consider the model

log(pijk) = u + ai + fik + bj + hjk + lij + ck

(25.22)

Here things are a little harder to interpret. Given k, i and j are no longer modeled as independent.

25.2. LOG-LINEAR MODELS

477

However, the degree of “correlation” between i and j, given k, is modeled as being independent of
k. In other words, the strength of the relation between i and j is the same, for all levels of k.

If we had included an mijk term—which would now make the model full or saturated—then it
may be possible that i and j are highly related for some values of k, and less related for others.

Clearly, the more variables we have, and the higher the order of interactions we include, the harder
it is to interpret the model

25.2.5 Parameter Estimation

Remember, whenever we have parametric models, the statistician’s “Swiss army knife” is Maximum
Likelihood Estimation (MLE, Section 18.1.3). That is what is most often used in the case of log-
linear models.

How, then, do we compute the likelihood of our data, the Nijk? It’s actually quite straightforward,
because the Nijk have a multinomial distribution (Section 13.5.1.1). Then the likelihood function
is

L =

n!
Πi,j,kNijk!

pNijk
ijk

(25.23)

We then write the pijk in terms of our model parameters. Take for example (25.21), where we write

pijk = eu+vi+wj +rij +ck

(25.24)

We then substitute (25.24) in (25.23), and maximize the latter with respect to the ai, bj, dij and
ck, subject to constraints as mentioned earlier.

The maximization may be messy. But certain cases have been worked out in closed form, and in
any case today one would typically do the computation by computer. In R, for example, there is
the loglin() function for this purpose, illustrated below.

Unfortunately, most books and software packages for the log-linear model put almost all of their
focus on signiﬁcance testing, rather than point estimation and conﬁdence intervals. In the popular
loglin() package, for instance, the parameter estimates, e.g. ˆvi above, are not even reported
unless the user requests them. Even then, no standard errors are reported. This is counter to the
universally recognized—though unfortunately widely ignored—point that signiﬁcance testing can
be quite misleading, especially in large samples (Section 17.11).

The tests themselves assess ﬁt. For instance, say we ﬁt the model (25.21). The program estimates
parameters using maximum likelihood, and then tests whether the model is “correct.” The actual

478

CHAPTER 25. RELATIONS AMONG VARIABLES

test will either be chi-squared (Section 21.2.1) or the Likelihood Ratio Test (related to MLEs); both
test statistics have chi-square distributions under H0. We can call pchisq() to ﬁnd the p-value.
Again, note that H0 is that the speciﬁed model is correct.

25.2.6 Example: Hair, Eye Color

Here we will look at R’s built-in data set HairEyeColor. There are variables Hair, Eye and Sex,
with 4 levels for hair color and 4 for eye color, over 592 people. Type ?HairEyeColor to see the
details.

This is actually an example from the online help for the function loglin(), though we’ll look at a
diﬀerent model in terms of interactions.

25.2.6.1 The loglin() Function

We’ll use the built-in R function loglin(), whose input data must be of class ”table”. Let’s see
ﬁrst how the latter works.

Say we have two variables, the ﬁrst having levels 1 and 2, and the second having levels 1, 2 and 3.
Suppose our data frame d is

> d

V1 V2
3
3
2
1
2

1 1
2 2
3 2
4 1
5 1

The ﬁrst person (or other entity) in our dataset has X (1) = 1, X (2) = 3 and so on. The function
table() does what its name implies: It tabulates the counts in each cell:

> table ( d )
V2

V1

1 2 3
1 1 1 1
2 0 1 1

This says there was one instance in which X (1) = 1, X (2) = 3 etc., but no instances of X (1) =
2, X (2) = 1.

So, our data is input as an object of type ”table”, speciﬁed in the argument table.

25.2. LOG-LINEAR MODELS

479

Our model is input via the argument margin, which is an R list of vectors. For instance c(1,3)
speciﬁes an interaction between variables 1 and 3, and c(1,2,3) means a three-way interaction.
Once a higher-order interaction is speciﬁed, we need not specify its lower-order “subset.” If, say,
we specify c(2,5,6), we need not specify c(2,6). On the other hand, if a variable m is involved in
no interactions, we need to specify it as c(m), in order to keep its individual (i.e. non-interaction)
eﬀect in the model.

The model used is actually for the cell counts, not the cell probabilities. Thus the constant term,
e.g. u in (25.22) is smaller by an amount equal to the log of the total number of observations in
the table.

25.2.7 Hair/Eye Color Analysis

Let’s get an overview of the data ﬁrst:

> HairEyeColor
, Sex = Male
,

Eye

Hair

Black
Brown
Red
Blond

Brown Blue Hazel Green
3
15
7
8

32
53
10
3

10
25
7
5

11
50
10
30

,

, Sex = Female

Eye

Hair

Black
Brown
Red
Blond

Brown Blue Hazel Green
2
14
7
8

9
34
7
64

36
66
16
4

5
29
7
5

Note that this is a 3-dimensional array, with Hair being rows, Eye being columns, and Sex being
layers. The data above show, for instance, that there are 25 men with brown hair and hazel eyes.
Let’s check this:

> HairEyeColor [ 2 , 3 , 1 ]
[ 1 ] 25

480

CHAPTER 25. RELATIONS AMONG VARIABLES

Let’s ﬁt a model (as noted, for the Nijk rather than the pijk) in which hair and eye color are
independent of gender, but not with each other, i.e. the model (25.16):

> fm <− l o g l i n ( HairEyeColor ,
2 i t e r a t i o n s : d e v i a t i o n 5 . 6 8 4 3 4 2 e −14
> fm
$ l r t
[ 1 ] 1 9 . 8 5 6 5 6

l i s t ( c ( 1 , 2 ) , 3 ) , param=T, f i t =T)

$ p e a r s o n
[ 1 ] 1 9 . 5 6 7 1 2

$df
[ 1 ] 15

$margin
$margin [ [ 1 ] ]
[ 1 ] ” Hair ” ”Eye”

$margin [ [ 2 ] ]
[ 1 ] ” Sex ”

$ f i t
,

, Sex = Female

Eye

Hair

Green
Blue
Brown
Black 3 5 . 9 5 2 7 0 3 1 0 . 5 7 4 3 2 4
2 . 6 4 3 5 8 1
Brown 6 2 . 9 1 7 2 3 0 4 4 . 4 1 2 1 6 2 2 8 . 5 5 0 6 7 6 1 5 . 3 3 2 7 7 0
7 . 4 0 2 0 2 7
Red
8 . 4 5 9 4 5 9
Blond

8 . 9 8 8 1 7 6
3 . 7 0 1 0 1 4 4 9 . 6 9 9 3 2 4

Hazel
7 . 9 3 0 7 4 3

7 . 4 0 2 0 2 7
5 . 2 8 7 1 6 2

1 3 . 7 4 6 6 2 2

$param
$param$ ‘ ( I n t e r c e p t ) ‘
[ 1 ] 2 . 4 9 4 7 4 8

$param$ Hair

Black
−0.3063649

Brown

Blond
0 . 9 5 2 0 0 8 1 −0.3471908 −0.2984523

Red

25.2. LOG-LINEAR MODELS

481

$param$Eye
Brown
0 . 3 6 1 1 1 2 5

$param$Sex
Male
−0.0574957

Blue

Green
0 . 5 1 1 2 1 7 3 −0.2798778 −0.5924520

Hazel

Female
0 . 0 5 7 4 9 5 7

$param$ Hair . Eye

Eye

Hair

Brown

Black
Brown
Red
Blond −1.3062970

Blue
0 . 9 7 5 2 1 3 2 −0.3986671
0 . 2 7 6 4 5 6 0 −0.2219556
0 . 0 5 4 6 2 7 9 −0.5203601

Green
0 . 1 0 4 7 4 6 0 −0.6812921
0 . 1 2 7 3 0 6 8 −0.1818072
0 . 3 8 9 1 5 3 2
0 . 0 7 6 5 7 9 0
0 . 4 7 3 9 4 6 1
1 . 1 4 0 9 8 2 8 −0.3086318

Hazel

The Likelihood Ratio Test, which has a chi-square distribution with 16 degrees of freedom under
H0 here, is not rejected:

> 1 − pchisq ( 1 9 . 8 5 6 5 6 , 1 6 )
[ 1 ] 0 . 2 2 6 7 5 0 7

The p-value here is about 0.23. So, if you allow your analyses to be dictated by tests, you would
accept the above model (which is plausible anyway). But even if you accept the hypothesis, you
should still be interested in the magnitudes of the eﬀects, shown above. Of course, for them to
make sense, you need to exponentiate them back into probabilities, a fair amount of work.

At any rate, one generally entertains several diﬀerent models for a data set, in much the same way
as one considers several diﬀerent sets of predictor variables in a regression setting. If one does not
use signiﬁcance testing for this, one goes through a less structured, but hopefully more fruitful,
process of comparing numerical model results.

Let’s again consider the case of brown-haired, hazel-eyed men. The model ﬁt is

exp (ˆu + ˆv2 + ˆw3 + ˆr23) = exp(2.494748+0.9520081−0.279877−0.0574957+0.1273068) = 25.44934
(25.25)

Actually, we could have gotten this directly, from

> fm$ f i t [ 2 , 3 , 1 ]
[ 1 ] 2 5 . 4 4 9 3 2

482

CHAPTER 25. RELATIONS AMONG VARIABLES

(there is a bit of roundoﬀ error above), but you should look at (25.25) to make sure you understand
what this number 25.44932 means. It is the estimate of E(N231) under our hypothesized model,
and (25.25) shows how this breaks down with respect to main eﬀects and interactions.

In particular, the interaction term, 0.1273068, is rather small compared to most of the other
numbers in the sum. This suggests that although there is some relation between hair and eye color,
the relation is not strong. Of course, we’d have to browse through the other interaction values to
make a stronger statement.

25.2.8 Obtaining Standard Errors

It was mentioned above that the output of loglin(), being signiﬁcance test-oriented, does not report
standard errors for the coeﬃcients. However, there is a “trick” one can use to get them, as follows.6

As noted earlier, our cell counts follow a multinomial distribution. But there is another model, in
which the cell counts are independent, each one following a Poisson distribution. (Note carefully
that this is not saying that the factors are independent.) The “trick” is that it turns out that both
models, multinomial and Poisson, yield the same MLEs. Thus even if we assume the multinomial
model, we can run our analyses as if it follows the Poisson model.

The remaining point is to note that R’s glm() function, used before for logistic regression by setting
family=binomial, can be used here with family=poisson. Since glm() output reports standard
errors, this workaround enables us to obtain standard errors for log-linear analysis.

25.3 Clustering

The clustering problem is somewhat like the reverse of the classiﬁcation problem. In the latter,
we our original data to enable ourselves to later input new data points and output class identities.
With clustering, we use our original data to hunt for classes themselves.

Clustering is treated as merely a technique for exploration/description, for browsing through the
data. It is not generally regarded as “estimating” anything (though we will return to this issue
later).

25.3.1 K-Means Clustering

This is probably the oldest clustering method, and it is still in wide use.

6At the expense of computation, one can generally use the bootstrap for obtaining standard errors. See Section

??.

25.3. CLUSTERING

25.3.1.1 The Algorithm

483

The method itself is quite simple, using an iterative algorithm. The user speciﬁes k, the number
of clusters he hopes to ﬁnd, and at any step during the iteration process, the current k clusters
are summarized by their centroids. (If we have m variables, then the centroid of a group is the
m-element vector of means of those variables within this group.) We iterate the following:

1. For each data point, i.e. each row of our data matrix, determine which centroid this point is

closest to.

2. Add this data point to the group corresponding to that centroid.

3. After all data points are processed in this manner, update the centroids to reﬂect the current

group memberships.

4. Next iteration.

• ]5.] Iterate until the centroids don’t change much from one iteration to the next.

25.3.1.2 Example: the Baseball Player Data

We use the R function kmeans() here. Its simplest call form is

kmeans ( x , k )

where x is our data matrix and k is as above.

Let’s try it on our baseball player data Height, Weight and Age, which are in columns 4-6 of our
data frame:

> head ( b a s e b a l l )

P o s i t i o n Height Weight

Name Team
Adam Donachie BAL
1
Paul Bako BAL
2
3 Ramon Hernandez BAL
4
5
6
kmb <− kmeans ( b a s e b a l l [ , 4 : 6 ] , 4 )

Catcher
Catcher
Catcher
Kevin M i l l a r BAL F i r s t Baseman
C h r i s Gomez BAL F i r s t Baseman
Brian Roberts BAL Second Baseman

Age PosCategory
Catcher
Catcher
Catcher
I n f i e l d e r
I n f i e l d e r
I n f i e l d e r

180 2 2 . 9 9
215 3 4 . 6 9
210 3 0 . 7 8
210 3 5 . 4 3
188 3 5 . 7 1
176 2 9 . 3 9

74
74
72
72
73
69

The return value, which we’ve stored here in kmb, is of class (of course) ”kmeans”. One of its
components is the ﬁnal centroids:

484

CHAPTER 25. RELATIONS AMONG VARIABLES

> kmb$ c e n t e r s

Height Weight

Age
1 7 3 . 8 1 2 6 7 2 0 2 . 6 5 8 4 2 9 . 0 0 8 8 2
2 7 2 . 4 5 0 1 3 1 8 0 . 3 1 8 1 2 7 . 9 0 7 7 6
3 7 4 . 7 9 1 4 7 2 2 1 . 7 7 2 5 2 9 . 4 8 8 6 7
4 7 6 . 3 0 0 0 0 2 4 4 . 4 5 7 1 2 9 . 0 4 1 2 9

Cluster 2 seems interesting, consisting of shorter, lighter and younger players, while Cluster 4 seems
the opposite. Clusters 1 and 3 are similar except in weight.

Let’s see if these clusters correlate to player position. Note that we’ve consolidated the original
Position variable into PosCategory, which consists of catchers, pitchers, inﬁelders and outﬁelders.
Let’s take a look at the relation to the clusters. The cluster component of the object returned by
the function shows which cluster each observations fell into. Here’s are table of the results:

> kmb$ s i z e # number o f p o i n t s
[ 1 ] 363 371 211
> b a s e b a l l $ c l s <− kmb$ c l u s t e r
> table ( b a s e b a l l $ c l s , b a s e b a l l $ PosCategory )

70

i n each c l u s t e r

Catcher
31
32
9
4

1
2
3
4

I n f i e l d e r O u t f i e l d e r P i t c h e r
201
143
107
84

74
51
57
12

69
42
84
15

Clusters 1 and 2 were the largest overall, but there is an interesting pattern here. Catchers were
almost entirely in Clusters 1 and 2, while the other position categories, especially inﬁelders, were
more evenly dispersed.

25.3.2 Mixture Models

Even though k-means and other clustering methods don’t claim to be estimating anything—again,
they are just intended as exploratory tools—the basic motivation is that each cluster represents
some multivariate distribution, e.g. trivariate normal in the baseball example above. Let qi be the
probability that an individual is in cluster i. Then the overall distribution has the form (19.2)—a
mixture!

So, we really are modeling the population by a mixture of subpopulations, and the EM algorithm
for mixtures (Section 19.5) can be used to estimate the centroids and the mixture weights qi.
in
particular, the mixtools library includes a function mvnormalmixEM() which does exactly this.

25.4. SIMPSON’S (NON-)PARADOX

485

25.3.3 Spectral Models

The word spectral here refers in turn to spectrum, which in linear algebra terms is the set of
eigenvalues of a matrix. It turns out that this is useful for clustering.

The details are complex, and many variant algorithms exist, but here is an overview:

Start with a similarity matrix S, calculated from your original data matrix X. Sij is deﬁned in
terms of some distance between rows i and j in X. One then computes

L = I − D−1/2SD−1/2

(25.26)

where D is a diagonal matrix whose (i,i) element is (cid:80)

j Sij.

One then ﬁnds an eigenvector v corresponding to the second-smallest eigenvalue of L. One then
partitions the rows of X according to some criterion involving v.

Then one partitions those partitions! The same method is used, and in this way, the data points are
chopped up into groups, our clusters. This is an example of what is called hierarchical clustering.

25.3.4 Other R Functions

The CRAN code repository includes a number of clustering packages. See the CRAN Task View:
Cluster Analysis & Finite Mixture Models,7 http://cran.r-project.org/web/views/Cluster.
html.

25.3.5 Further Reading

UCD professor Catherine Yang is a specialist in clustering. See for instance Segmenting Customer
Transactions Using a Pattern-Based Clustering Approach, Proceedings of the Thrid IEEE Interna-
tional Conference on Data Mining.

25.4 Simpson’s (Non-)Paradox

Every serious user of statistics must keep in mind Simpson’s Paradox at all times. It’s an example
of “what can go wrong” in multivariate studies. Its central importance is reﬂected, for instance, in
an extensive entry in Wikipedia.

7CRAN has a number of these “task views” on various topics.

486

CHAPTER 25. RELATIONS AMONG VARIABLES

And yet...a very simple precaution will enable you to avoid the problem.

So, what is this paradox? In short, the relation between variables X and Y can be positive,
conditional on every level of a third variable Z, and yet be negative overall. (Or vice versa, i.e.
change from negative to positive.)

As you will see below, it arguably is not really a paradox at all. Instead, the real problem is a
failure to examine the more important variables before the less important ones.

25.4.1 Example: UC Berkeley Graduate Admission Data

This is a very famous data set, much analyzed over the years, well known partly because a lawsuit
was involved.

25.4.1.1 Overview

The suit claimed that UC Berkeley graduate admissions practices discriminated against women.
The plaintiﬀs pointed out that male applicants had a 44% acceptance rate, compared to only 35%
for women.

On the surface, the claims of discrimination seemed quite plausible. Here X, called Admit, was
being admitted to graduate school, and Y, called Gender, was an indicator variable for being male.
X and Y appeared to be positively related.

However, things changed a lot when a third variable Z, called Dept, was brought in. This coded
which department the applicant applied to. Upon closer inspection by UCB statistics professors, it
was found that conditional on Z, X and Y were actually negatively related for (almost) every value
of Z. In other words, it turned out that in (almost) every department, the women applicants were
actually being slightly fared better than the men.8

25.4.1.2 Log-Linear Analysis

Let’s analyze this with a log-linear model. As it happens, the data is one of R’s built-in data sets,
UCBAdmissions (which we will copy to a new variable ucb, for brevity):

> ucb <− UCBAdmissions

As noted, our “Z,” is Dept; six departments were represented in the data, i.e. six diﬀerent graduate
programs.

8One department was an exception, but the diﬀerence was small and possibly due to sampling variation. Thus

most accounts treat this as an instance of Simpson’s Paradox, rather than “almost” an example.

25.4. SIMPSON’S (NON-)PARADOX

487

We can easily determine how many applicants there were:

> sum( ucb )
[ 1 ] 4526

Since the Admit and Gender variables had two levels each, there were 2 × 2 × 6 = 24 diﬀerent
cells in the table. In other words, our log-linear analysis is modeling 24 diﬀerent cell probabilities
(actually 23, since they must sum to 1.0).

So, even a saturated model would easily satisfy our rule of thumb (Section 22.15.5) regarding
overﬁtting, which recommended keeping the number of parameters below the square root of the
sample size.

Let’s ﬁt a model that includes all two-way interactions.9

> l l o u t <− l o g l i n ( ucb , l i s t ( 1 : 2 , c ( 1 , 3 ) , 2 : 3 ) , param=T)
2 i t e r a t i o n s : d e v i a t i o n 3 . 5 5 2 7 1 4 e −15
> l l o u t $param
$ ‘ ( I n t e r c e p t ) ‘
[ 1 ] 4 . 8 0 2 4 5 7

$Admit

Admitted
−0.3212111

R e j e c t e d
0 . 3 2 1 2 1 1 1

$Gender

Male

Female
0 . 3 2 8 7 5 6 9 −0.3287569

$Dept

B
0 . 1 5 3 7 6 2 5 8 −0.76516841

A

C
0 . 5 3 9 7 2 0 5 4

$Admit . Gender

Gender

Admit

Male
Admitted −0.02493703
R e j e c t e d

Female
0 . 0 2 4 9 3 7 0 3
0 . 0 2 4 9 3 7 0 3 −0.02493703

$Admit . Dept

Dept

9A full model was ﬁt, with similar patterns.

F
0 . 4 3 0 2 1 5 3 4 −0.02881353 −0.32971651

D

E

488

Admit

CHAPTER 25. RELATIONS AMONG VARIABLES

A
0 . 6 3 7 1 8 0 4

B
0 . 6 1 5 4 7 7 2

Admitted
R e j e c t e d −0.6371804 −0.6154772 −0.005914624

C
0 . 0 0 5 9 1 4 6 2 4 −0.01010004 −0.2324371 −1.016035
1 . 0 1 6 0 3 5

0 . 0 1 0 1 0 0 0 4

0 . 2 3 2 4 3 7 1

D

E

F

$Gender . Dept
Dept

Gender
Male
Female −0.6954949 −1.232906

F
1 . 2 3 2 9 0 6 −0.6370521 −0.2836477 −0.7020726 −0.3056282
0 . 3 0 5 6 2 8 2
0 . 2 8 3 6 4 7 7

A
0 . 6 9 5 4 9 4 9

0 . 7 0 2 0 7 2 6

0 . 6 3 7 0 5 2 1

D

C

B

E

Again, there is a lot here, but it’s not hard to spot the salient numbesr. Let’s look at the main
eﬀects ﬁrst, labeled $Admit. $Gender and $Dept, near the top of the output, reproduced here
for convenience:

$Admit

Admitted
−0.3212111

R e j e c t e d
0 . 3 2 1 2 1 1 1

$Gender

Male

Female
0 . 3 2 8 7 5 6 9 −0.3287569

$Dept

B
0 . 1 5 3 7 6 2 5 8 −0.76516841

A

C
0 . 5 3 9 7 2 0 5 4

F
0 . 4 3 0 2 1 5 3 4 −0.02881353 −0.32971651

D

E

One sees that the main eﬀect for Gender, ±0.3287569, is small compared to the main eﬀects for some
of the departments. So some departments attract a lot of applicants, notably C, while others such
as F attract fewer than average. If this varies by gender, then it’s important to take department
into account in our analysis.

In view of that latter fact, in viewing the two-way interactions, consider the interaction between
Admit and Dept. The Admit values were strongly positive for Departments A and B, small for C
and D, and moderately to strongly negative for E and F. In other words, A and B were especially
easy to get into, while E and F were particularly diﬃcult.

But now turn to the Gender.Dept interaction, showing the pattern of which departments were
applied to by which genders. The men were extra likely to apply to Departments A and B, while
for the women it was C, D, E and F. In other words, men were more likely to apply to the easier
departments, while the women were more likely to apply to the diﬃcult ones! No wonder the
women seemed to be faring poorly overall in the data presented in the lawsuit, which did not break

25.4. SIMPSON’S (NON-)PARADOX

489

things down by department.

The Admit.Gender interaction says it all: Everything else equal, the women were slightly more
likely to be admitted than men.

25.4.2 Toward Making It Simpson’s NON-Paradox

The above example shows that Simpson’s Paradox really isn’t a paradox at all. The so-called
“anomaly” occurred simply because an omitted variable (department) was a stronger factor than
was one of the included variables (gender).

Indeed, it can be argued that the “paradox,” i.e. the reversal of signs, wouldn’t occur if one used
the correct ordering when bringing factors into consideration, as follows. Let’s use the UCB data
above to make this concrete.

Recall the method of forward stepwise regression (Section 22.15.4.1). The analog here might be
start by considering just two of the three variables at a time, before looking at three-variable
models. For the UCB data, we would ﬁrst look at Admit and Gender, then look at Admit and
Dept. For example, we would ﬁt the latter model by running

> l o g l i n ( ucb , l i s t ( c ( 1 , 3 ) ) , param=TRUE)

The results are not shown here, but they reveal that indeed Dept has a much stronger association
with Admit than does Gender. We would thus take Admit-Dept as our ”ﬁrst-step” model, and
then add Gender to get our second-step model (again, ”step” as in the spirit of stepwise regression).
That would produce the model we analyzed above, WITH NO PARADOXICAL RESULT.

Exercises

490

CHAPTER 25. RELATIONS AMONG VARIABLES

Appendix A

R Quick Start

Here we present a quick introduction to the R data/statistical programming language. Further
learning resources are listed at http://heather.cs.ucdavis.edu//r.html.

R syntax is similar to that of C. It is object-oriented (in the sense of encapsulation, polymorphism
and everything being an object) and is a functional language (i.e. almost no side eﬀects, every
action is a function call, etc.).

A.1 Correspondences

aspect
assignment
array terminology
subscripts
array notation
2-D array storage
mixed container
return mechanism
primitive types
logical values
mechanism for combining modules
run method

C/C++
=
array
start at 0
m[2][3]
row-major order
struct, members accessed by .
return
int, ﬂoat, double, char, bool
true, false
include, link
batch

R
<- (or =)
vector, matrix, array
start at 1
m[2,3]
column-major order
list, members acessed by $ or [[ ]]
return() or last value computed
integer, ﬂoat, double, character, logical
TRUE, FALSE (abbreviated T, F)
library()
interactive, batch

491

492

APPENDIX A. R QUICK START

A.2 Starting R

To invoke R, just type “R” into a terminal window. On a Windows machine, you probably have
an R icon to click.

If you prefer to run from an IDE, you may wish to consider ESS for Emacs, StatET for Eclipse or
RStudio, all open source. ESS is the favorite among the “hard core coder” types, while the colorful,
easy-to-use, RStudio is a big general crowd pleaser. If you are already an Eclipse user, StatET will
be just what you need.

R is normally run in interactive mode, with > as the prompt. Among other things, that makes it
easy to try little experiments to learn from; remember my slogan, “When in doubt, try it out!”

A.3 First Sample Programming Session

Below is a commented R session, to introduce the concepts. I had a text editor open in another
window, constantly changing my code, then loading it via R’s source() command. The original
contents of the ﬁle odd.R were:

{
k <− 0 # a s s i g n 0 t o k
for ( n i n x )

1 oddcount <− function ( x )
2
3
4
5
6
7 }

}
return ( k )

i f

{

( n %% 2 == 1 ) k <− k+1 # %% i s

t h e modulo o p e r a t o r

By the way, we could have written that last statement as simply

1

k

because the last computed value of an R function is returned automatically.

The R session is shown below. You may wish to type it yourself as you go along, trying little
experiments of your own along the way.1

1 > source ( ”odd .R” ) # l o a d code from t h e g i v e n f i l e
2 > l s ( ) # what o b j e c t s do we have ?
3
4 > # what k i n d o f o b j e c t

[ 1 ] ” oddcount ”

i s oddcount ( w e l l , we a l r e a d y know )?

1The source code for this ﬁle is at http://heather.cs.ucdavis.edu/~matloff/MiscPLN/R5MinIntro.tex. You

can download the ﬁle, and copy/paste the text from there.

A.3. FIRST SAMPLE PROGRAMMING SESSION

493

5 > c l a s s ( oddcount )
6
[ 1 ] ” f u n c t i o n ”
7 > # w h i l e i n i n t e r a c t i v e mode , and not
8 > # any o b j e c t by t y p i n g
9 > oddcount # a f u n c t i o n i s an o b j e c t ,

i t s name ; o t h e r w i s e use p r i n t ( ) , e . g . p r i n t ( x+y )

i n s i d e a f u n c t i o n , can p r i n t

so can p r i n t

i t

{

{

8

i f

i s

8 88

f i r s t

[ 1 ] 12

5 12 13

[ 1 ] 12 13

t h e modulo o p e r a t o r

t e s t oddcount ( ) , b u t

}
return ( k )

t h e c o n c a t e n a t e f u n c t i o n

( n %% 2 == 1 ) k <− k+1 # %% i s

l o o k a t some p r o p e r t i e s o f v e c t o r s

k <− 0 # a s s i g n 0 t o k
fo r ( n i n x )

10 function ( x )
11
12
13
14
15
16 }
17
18 > # l e t ’ s
19 > y <− c ( 5 , 1 2 , 1 3 , 8 , 8 8 ) # c ( )
20 > y
21
[ 1 ]
22 > y [ 2 ] # R s u b s c r i p t s b e g i n a t 1 , not 0
23
24 > y [ 2 : 4 ] # e x t r a c t e l e m e n t s 2 , 3 and 4 o f y
25
26 > y [ c ( 1 , 3 : 5 ) ] # e l e m e n t s 1 , 3 , 4 and 5
27
28 > oddcount ( y ) # s h o u l d r e p o r t 2 odd numbers
29
30
31 > # change code ( i n t h e o t h e r window ) t o v e c t o r i z e t h e count o p e r a t i o n ,
32 > # f o r much f a s t e r e x e c u t i o n
33 > source ( ”odd .R” )
34 > oddcount
35 function ( x )
36
37
38
39 }
40
41 > # t r y i t on s u b s e t o f y , e l e m e n t s 2 t h r o u g h 3
42 > oddcount ( y [ 2 : 3 ] )
43
44 > # t r y i t on s u b s e t o f y , e l e m e n t s 2 , 4 and 5

x1 <− ( x %% 2 == 1 ) # x1 now a v e c t o r o f TRUEs and FALSEs
x2 <− x [ x1 ] # x2 now has t h e e l e m e n t s o f x t h a t were TRUE i n x1
return ( length ( x2 ) )

[ 1 ] 2

[ 1 ] 1

8 88

5 13

[ 1 ]

{

494

APPENDIX A. R QUICK START

{

i t

t h a t

t h a t

[ 1 ] 0

[ 1 ] 2

t h e f a c t

t h a t TRUE and

length ( x [ x %% 2 == 1 ] ) # l a s t v a l u e computed i s a u t o r e t u r n e d

i n v o l v e s :
t h e r e s u l t o f a p p l y i n g t h e mod 2
t h e n x %% 2 == 1 a p p l i e s

45 > oddcount ( y [ c ( 2 , 4 , 5 ) ] )
46
47
48 > # f u r t h e r c o m p a c t i f y t h e code
49 > source ( ”odd .R” )
50 > oddcount
51 function ( x )
52
53 }
54 > oddcount ( y ) # t e s t
55
56
57 # and even more c o m p a c t i f i c a t i o n , making use o f
58 # FALSE a r e t r e a t e d as 1 and 0
59 > oddcount <− function ( x ) sum( x %% 2 == 1 )
60 # make s u r e you u n d e r s t a n d t h e s t e p s
61 # and t h u s x %% 2 i s a new v e c t o r ,
62 # o p e r a t i o n t o e v e r y e l e m e n t o f x ;
63 # o p e r a t i o n t o each e l e m e n t o f
64 # and FALSE v a l u e s ; sum ( )
65
66 # we can a l s o d e t e r m i n e which e l e m e n t s a r e odd
67 > which ( y %% 2 == 1 )
68
69
70 > # now have f t n r e t u r n odd coun t AND t h e odd numbers t h e m s e l v e s , u s i n g
71 > # t h e R l i s t
72 > source ( ”odd .R” )
73 > oddcount
74 function ( x )
75
76
77 }
78 > # R’ s
79 > oddcount ( y )
80 $odds
[ 1 ]
81
82
83 $numodds
[ 1 ] 2
84

{
x1 <− x [ x %% 2 == 1 ]
return ( l i s t ( odds=x1 , numodds=length ( x1 ) ) )

t y p e can c o n t a i n any t y p e ; components d e l i n e a t e d by $

t h e n adds them ( as 1 s and 0 s )

t h e == 1

[ 1 ] 1 3

5 13

t y p e

t h a t

l i s t

x i s a v e c t o r ,

r e s u l t , y i e l d i n g a new v e c t o r o f TRUE

A.3. FIRST SAMPLE PROGRAMMING SESSION

495

i n ocy , which w i l l be a l i s t

5 13

85
86 > ocy <− oddcount ( y ) # s a v e t h e o u t p u t
87 > ocy
88 $odds
89
[ 1 ]
90
91 $numodds
[ 1 ] 2
92
93
94 > ocy$odds
95
96 > ocy [ [ 1 ] ] # can g e t
97
5 13
98 > ocy [ [ 2 ] ]
[ 1 ] 2
99

5 13

l i s t

[ 1 ]

[ 1 ]

e l e m e n t s u s i n g [ [

] ]

i n s t e a d o f $

Note that the function of the R function function() is to produce functions! Thus assignment is
used. For example, here is what odd.R looked like at the end of the above session:

1 oddcount <− function ( x )
x1 <− x [ x %% 2 == 1 ]
2
return ( l i s t ( odds=x1 , numodds=length ( x1 ) ) )
3
4 }

{

We created some code, and then used function() to create a function object, which we assigned
to oddcount.

Note that we eventually vectorized our function oddcount(). This means taking advantage of
the vector-based, functional language nature of R, exploiting R’s built-in functions instead of loops.
This changes the venue from interpreted R to C level, with a potentially large increase in speed.
For example:

u s e r
0 . 0 0 8

1 > x <− runif ( 1 0 0 0 0 0 0 ) # 1000000 random numbers from t h e i n t e r v a l
2 > system . time (sum( x ) )
3
4
5 > system . time ( { s <− 0 ;
6
7

system e l a p s e d
2 . 8 5 9

system e l a p s e d
0 . 0 0 6

s <− s + x [ i ] } )

i n 1 : 1 0 0 0 0 0 0 )

u s e r
2 . 7 7 6

for ( i

0 . 0 0 0

0 . 0 0 4

( 0 , 1 )

496

APPENDIX A. R QUICK START

A.4 Second Sample Programming Session

A matrix is a special case of a vector, with added class attributes, the numbers of rows and columns.

f u n c t i o n combines rows o f m a t r i c e s ;

t h e r e ’ s a c b i n d ( )

t o o

[ 1 , ]
[ 2 , ]

[ 1 , ]
[ 2 , ]

[ 1 , ]
[ 2 , ]
[ 3 , ]

[ , 2 ]
2
8

[ , 2 ]
3
4

[ , 1 ]
1
2

[ , 3 ]
5
6

[ , 1 ]
1
5
6

[ , 2 ]
2
8
−1

1 > # ” rowbind ( )
2 > m1 <− rbind ( 1 : 2 , c ( 5 , 8 ) )
3 > m1
[ , 1 ]
4
1
5
6
5
7 > rbind (m1, c (6 , −1))
8
9
10
11
12
13 > # form m a t r i x from 1 , 2 , 3 , 4 , 5 , 6 ,
14 > m2 <− matrix ( 1 : 6 , nrow=2)
15 > m2
16
17
18
19 > ncol (m2)
20
[ 1 ] 3
21 > nrow(m2)
[ 1 ] 2
22
23 > m2 [ 2 , 3 ] # e x t r a c t e l e m e n t
24
[ 1 ] 6
25 # g e t
26 > m3 <− m2 [ , 2 : 3 ]
27 > m3
28
29
30
31
32 > m1 ∗ m3 # e l e m e n t w i s e m u l t i p l i c a t i o n
33
34
35
36 > 2 . 5 ∗ m3 # s c a l a r m u l t i p l i c a t i o n ( b u t
37

[ , 1 ]
3
4

[ , 1 ]
3
20

[ , 2 ]
5
6

[ , 2 ]
10
48

[ 1 , ]
[ 2 , ]

[ 1 , ]
[ 2 , ]

[ , 1 ]

[ , 2 ]

i n row 2 , c o l 3

s u b m a t r i x o f m2, c o l s 2 and 3 , any row

s e e b e l o w )

i n 2 rows ; R u s e s column−major s t o r a g e

A.4. SECOND SAMPLE PROGRAMMING SESSION

497

[ 1 , ]
[ 2 , ]

[ , 2 ]
17
73

[ , 1 ]
11
47

[ 1 , ]
7 . 5 1 2 . 5
[ 2 , ] 1 0 . 0 1 5 . 0

38
39
40 > m1 %∗% m3 # l i n e a r a l g e b r a m a t r i x m u l t i p l i c a t i o n
41
42
43
44
45 > # m a t r i c e s a r e s p e c i a l c a s e s o f v e c t o r s ,
46 > sum(m1)
47
[ 1 ] 16
48 > i f e l s e (m2 %%3 == 1 , 0 ,m2) # ( s e e b e l o w )
49
50
51

[ , 3 ]
5
6

[ , 1 ]
0
2

[ , 2 ]
3
0

[ 1 , ]
[ 2 , ]

so can t r e a t

them as v e c t o r s

The “scalar multiplication” above is not quite what you may think, even though the result may
be. Here’s why:

In R, scalars don’t really exist; they are just one-element vectors. However, R usually uses recy-
cling, i.e. replication, to make vector sizes match. In the example above in which we evaluated
the express 2.5 * m3, the number 2.5 was recycled to the matrix

(cid:18) 2.5 2.5
2.5 2.5

(cid:19)

(A.1)

in order to conform with m3 for (elementwise) multiplication.

The ifelse() function is another example of vectorization. Its call has the form

i f e l s e ( b o o l e a n v e c t o r e x p r e s s i o n 1 , v e c t o r e x p r e s s i o n 2 , v e c t o r e x p r e s s i o n 3 )

All three vector expressions must be the same length, though R will lengthen some via recycling.
The action will be to return a vector of the same length (and if matrices are involved, then the
result also has the same shape). Each element of the result will be set to its corresponding element
in vectorexpression2 or vectorexpression3, depending on whether the corresponding element
in vectorexpression1 is TRUE or FALSE.

In our example above,

> i f e l s e (m2 %%3 == 1 , 0 ,m2) # ( s e e b e l o w )

498

APPENDIX A. R QUICK START

the expression m2 %%3 == 1 evaluated to the boolean matrix

(cid:18) T F F
F T F

(cid:19)

(TRUE and FALSE may be abbreviated to T and F.)

The 0 was recycled to the matrix

(cid:18) 0 0 0
0 0 0

(cid:19)

while vectorexpression3, m2, evaluated to itself.

A.5 Third Sample Programming Session

This time, we focus on vectors and matrices.

> m <− rbind ( 1 : 3 , c ( 5 , 1 2 , 1 3 ) ) # ”row bind , ” combine rows
> m

(A.2)

(A.3)

[ , 3 ]
[ , 1 ]
3
1
[ 1 , ]
[ 2 , ]
13
5
> t (m) # t r a n s p o s e

[ , 2 ]
2
12

[ , 1 ]
1
2
3

[ , 2 ]
5
[ 1 , ]
12
[ 2 , ]
[ 3 , ]
13
> ma <− m[ , 1 : 2 ]
> ma

[ , 1 ]
1
5

[ , 2 ]
2
12

[ 1 , ]
[ 2 , ]
> rep ( 1 , 2 ) # ” r e p e a t , ” make m u l t i p l e c o p i e s
[ 1 ] 1 1
> ma %∗% rep ( 1 , 2 ) # m a t r i x m u l t i p l y

[ , 1 ]
3
17

[ 1 , ]
[ 2 , ]
> solve (ma, c ( 3 , 1 7 ) ) # s o l v e l i n e a r

system

A.6. DEFAULT ARGUMENT VALUES

499

[ 1 ] 1 1
> solve (ma) # m a t r i x i n v e r s e

[ , 1 ]

[ , 2 ]
6 . 0 −1.0
0 . 5

[ 1 , ]
[ 2 , ] −2.5

A.6 Default Argument Values

Consider the sort() function, which is built-in to R, though the following points hold for any
function, including ones you write yourself.

The online help for this function, invoked by

> ? sort

shows that the call form (the simplest version) is

sort ( x , d e c r e a s i n g = FALSE,

. . . )

Here is an example:

> x <− c ( 1 2 , 5 , 1 3 )
> sort ( x )
[ 1 ]
> sort ( x , d e c r e a s i n g=FALSE)
[ 1 ] 13 12

5 12 13

5

So, the default is to sort in ascending order, i.e. the argument decreasing has TRUE as its default
value. If we want the default, we need not specify this argument. If we want a descending-order
sort, we must say so.

A.7 The R List Type

The R list type is, after vectors, the most important R construct. A list is like a vector, except
that the components are generally of mixed types.

A.7.1 The Basics

Here is example usage:

500

APPENDIX A. R QUICK START

> g <− l i s t ( x = 4 : 6 ,
> g
$x
[ 1 ] 4 5 6

s = ” abc ” )

$ s
[ 1 ] ” abc ”

> g$x # can r e f e r e n c e by component name
[ 1 ] 4 5 6
> g$ s
[ 1 ] ” abc ”
> g [ [ 1 ] ] # can r e f e r e n c e by index , b u t n o t e d o u b l e b r a c k e t s
[ 1 ] 4 5 6
> g [ [ 2 ] ]
[ 1 ] ” abc ”
> for ( i
[ 1 ] 4 5 6
[ 1 ] ” abc ”

i n 1 : length ( g ) ) print ( g [ [ i ] ] )

A.7.2 The Reduce() Function

One often needs to combine elements of a list in some way. One approach to this is to use Reduce():

> x <− l i s t ( 4 : 6 , c ( 1 , 6 , 8 ) )
> x
[ [ 1 ] ]
[ 1 ] 4 5 6

[ [ 2 ] ]
[ 1 ] 1 6 8

> sum( x )
E r r o r
i n sum( x )
> Reduce (sum, x )
[ 1 ] 30

:

i n v a l i d ’ type ’

( l i s t ) o f argument

Here Reduce() cumulatively applied R’s sum() to x. Of course, you can use it with functions you
write yourself too.

Continuing the above example:

A.7. THE R LIST TYPE

501

> Reduce ( c , x )
[ 1 ] 4 5 6 1 6 8

A.7.3 S3 Classes

R is an object-oriented (and functional) language. It features two types of classes, S3 and S4. I’ll
introduce S3 here.

An S3 object is simply a list, with a class name added as an attribute:

> j <− l i s t ( name=” Joe ” ,
> c l a s s ( j ) <− ” employee ”
> m <− l i s t ( name=” Joe ” ,
> c l a s s (m) <− ” employee ”

s a l a r y =55000 , union=T)

s a l a r y =55000 , union=F)

So now we have two objects of a class we’ve chosen to name ”employee”. Note the quotation
marks.

We can write class generic functions:

cat ( wrkr$name , ”\n” )
cat ( ” s a l a r y ” , wrkr$ s a l a r y , ” \n” )
cat ( ” union member” , wrkr$union , ” \n” )

> print . employee <− function ( wrkr ) {
+
+
+
+ }
> print ( j )
Joe
s a l a r y 55000
union member TRUE
> j
Joe
s a l a r y 55000
union member TRUE

What just happened? Well, print() in R is a generic function, meaning that it is just a placeholder
for a function speciﬁc to a given class. When we printed j above, the R interpreter searched for a
function print.employee(), which we had indeed created, and that is what was executed. Lacking
this, R would have used the print function for R lists, as before:

> rm( print . employee ) # remove t h e f u n c t i o n ,
> j
$name

t o s e e what happens w i t h p r i n t

APPENDIX A. R QUICK START

502

[ 1 ] ” Joe ”

$ s a l a r y
[ 1 ] 55000

$union
[ 1 ] TRUE

attr ( , ” c l a s s ” )
[ 1 ] ” employee ”

A.7.4 Handy Utilities

R functions written by others, e.g. in base R or in the CRAN repository for user-contributed code,
often return values which are class objects. It is common, for instance, to have lists within lists. In
many cases these objects are quite intricate, and not thoroughly documented. In order to explore
the contents of an object—even one you write yourself—here are some handy utilities:

• names(): Returns the names of a list.

• str(): Shows the ﬁrst few elements of each component.

• summary(): General function. The author of a class x can write a version speciﬁc to x,
i.e. summary.x(), to print out the important parts; otherwise the default will print some
bare-bones information.

For example:

> z <− l i s t ( a = runif ( 5 0 ) , b = l i s t ( u=sample ( 1 : 1 0 0 , 2 5 ) , v=” b l u e sky ” ) )
> z
$a

[ 1 ] 0 . 3 0 1 6 7 6 2 2 9 0 . 6 7 9 9 1 8 5 1 8 0 . 2 0 8 7 1 3 5 2 2 0 . 5 1 0 0 3 2 8 9 3 0 . 4 0 5 0 2 7 0 4 2

0 . 4 1 2 3 8 8 0 3 8

[ 7 ] 0 . 9 0 0 4 9 8 0 6 2 0 . 1 1 9 9 3 6 2 2 2 0 . 1 5 4 9 9 6 4 5 7 0 . 2 5 1 1 2 6 2 1 8 0 . 9 2 8 3 0 4 1 6 4

0 . 9 7 9 9 4 5 9 3 7
[ 1 3 ] 0 . 9 0 2 3 7 7 3 6 3 0 . 9 4 1 8 1 3 8 9 8 0 . 0 2 7 9 6 4 1 3 7 0 . 9 9 2 1 3 7 9 0 8 0 . 2 0 7 5 7 1 1 3 4
0 . 0 4 9 5 0 4 9 8 6
[ 1 9 ] 0 . 0 9 2 0 1 1 8 9 9 0 . 5 6 4 0 2 4 4 2 4 0 . 2 4 7 1 6 2 0 0 4 0 . 7 3 0 0 8 6 7 8 6 0 . 5 3 0 2 5 1 7 7 9
0 . 5 6 2 1 6 3 9 8 6
[ 2 5 ] 0 . 3 6 0 7 1 8 9 8 8 0 . 3 9 2 5 2 2 2 4 2 0 . 8 3 0 4 6 8 4 2 7 0 . 8 8 3 0 8 6 7 5 2 0 . 0 0 9 8 5 3 1 0 7

A.8. DATA FRAMES

503

0 . 1 4 8 8 1 9 1 2 5
[ 3 1 ] 0 . 3 8 1 1 4 3 8 7 0 0 . 0 2 7 7 4 0 9 5 9 0 . 1 7 3 7 9 8 9 2 6 0 . 3 3 8 8 1 3 0 4 2 0 . 3 7 1 0 2 5 8 8 5
0 . 4 1 7 9 8 4 3 3 1
[ 3 7 ] 0 . 7 7 7 2 1 9 0 8 4 0 . 5 8 8 6 5 0 4 1 3 0 . 9 1 6 2 1 2 0 1 1 0 . 1 8 1 1 0 4 5 1 0 0 . 3 7 7 6 1 7 3 9 9
0 . 8 5 6 1 9 8 8 9 3
[ 4 3 ] 0 . 6 2 9 2 6 9 1 4 6 0 . 9 2 1 6 9 8 3 9 4 0 . 8 7 8 4 1 2 3 9 8 0 . 7 7 1 6 6 2 4 0 8 0 . 5 9 5 4 8 3 4 7 7
0 . 9 4 0 4 5 7 3 7 6
[ 4 9 ] 0 . 2 2 8 8 2 9 8 5 8 0 . 7 0 0 5 0 0 3 5 9

$b
$b$u

[ 1 ] 33 67 32 76 29

3 42 54 97 41 57 87 36 92 81 31 78 12 85 73 26 44

86 40 43

$b$v
[ 1 ] ” b l u e sky ”
> names( z )
[ 1 ] ”a” ”b”
> s t r ( z )
L i s t o f 2
$ a : num [ 1 : 5 0 ] 0 . 3 0 2 0 . 6 8 0 . 2 0 9 0 . 5 1 0 . 4 0 5 . . .
$ b : L i s t o f 2

. . $ u :
. . $ v : c h r ” b l u e sky ”

i n t

[ 1 : 2 5 ] 33 67 32 76 29 3 42 54 97 41 . . .

> names( z$b )
[ 1 ] ”u” ”v”
> summary( z )

Length C l a s s Mode

a 50
2
b

−none− numeric
−none− l i s t

A.8 Data Frames

Another workhorse in R is the data frame. A data frame works in many ways like a matrix, but
diﬀers from a matrix in that it can mix data of diﬀerent modes. One column may consist of integers,
while another can consist of character strings and so on. Within a column, though, all elements
must be of the same mode, and all columns must have the same length.

We might have a 4-column data frame on people, for instance, with columns for height, weight, age

504

APPENDIX A. R QUICK START

and name—3 numeric columns and 1 character string column.

Technically, a data frame is an R list, with one list element per column; each column is a vector.
Thus columns can be referred to by name, using the $ symbol as with all lists, or by column number,
as with matrices. The matrix a[i,j] notation for the element of a in row i, column j, applies to
data frames. So do the rbind() and cbind() functions, and various other matrix operations, such
as ﬁltering.

Here is an example using the dataset airquality, built in to R for illustration purposes. You can
learn about the data through R’s online help, i.e.

> ? a i r q u a l i t y

Let’s try a few operations:

> names( a i r q u a l i t y )
[ 1 ] ”Ozone”
> head ( a i r q u a l i t y ) # l o o k a t

” S o l a r .R” ”Wind”

”Temp”
f i r s t

t h e

”Month”

”Day”

few rows

5
5
5
5
5
5

67
72
74
62
56
66

41
36
12
18
NA
28

Ozone S o l a r .R Wind Temp Month Day
1
190
7 . 4
2
8 . 0
118
3
149 1 2 . 6
4
313 1 1 . 5
5
NA 1 4 . 3
6
NA 1 4 . 9

1
2
3
4
5
6
> a i r q u a l i t y [ 5 , 3 ] # wind on t h e 5 t h day
[ 1 ] 1 4 . 3
> a i r q u a l i t y $Wind [ 3 ] # same
[ 1 ] 1 2 . 6
> nrow( a i r q u a l i t y ) # number o f days o b s e r v e d
[ 1 ] 153
> ncol ( a i r q u a l i t y ) # number o f v a r i a b l e s
[ 1 ] 6
> a i r q u a l i t y $ C e l s i u s <− ( 5 / 9 ) ∗ ( a i r q u a l i t y [ , 4 ] − 3 2 ) # new v a r i a b l e
> names( a i r q u a l i t y )
[ 1 ] ”Ozone”
> ncol ( a i r q u a l i t y )
[ 1 ] 7
> a i r q u a l i t y [ 1 : 3 , ]

” S o l a r .R” ”Wind”

”Month”

”Temp”

”Day”

” C e l s i u s ”

Ozone S o l a r .R Wind Temp Month Day C e l s i u s
1 1 9 . 4 4 4 4 4
2 2 2 . 2 2 2 2 2
3 2 3 . 3 3 3 3 3

7 . 4
190
118
8 . 0
149 1 2 . 6

67
72
74

41
36
12

5
5
5

1
2
3

A.9. GRAPHICS

505

> a q j u n e <− a i r q u a l i t y [ a i r q u a l i t y $Month == 6 , ] # f i l t e r op
> nrow( a q j u n e )
[ 1 ] 30
> mean( a q j u n e $Temp)
[ 1 ] 7 9 . 1
> write . table ( aqjune , ”AQJune” ) # w r i t e d a t a frame t o f i l e
> a q j <− read . table ( ”AQJune” , h e a d e r=T) # read i t

i n

A.9 Graphics

R excels at graphics, oﬀering a rich set of capabilities, from beginning to advanced. In addition to
the functions in base R, extensive graphics packages are available, such as lattice and ggplot2.

One point of confusion for beginners involves saving an R graph that is currently displayed on the
screen to a ﬁle. Here is a function for this, which I include in my R startup ﬁle, .Rproﬁle, in my
home directory:

p r 2 f i l e
function ( f i l e n a m e )
{

o r i g d e v <− dev . cur ( )
p a r t s <− s t r s p l i t ( f i l e n a m e , ” . ” ,
n p a r t s <− length ( p a r t s [ [ 1 ] ] )
s u f f <− p a r t s [ [ 1 ] ] [ n p a r t s ]
i f

( s u f f == ” pdf ” ) {

f i x e d = TRUE)

pdf ( f i l e n a m e )

}
e l s e i f

( s u f f == ”png” ) {

png ( f i l e n a m e )

}
e l s e j p e g ( f i l e n a m e )
devnum <− dev . cur ( )
dev . set ( o r i g d e v )
dev . copy ( which = devnum )
dev . set ( devnum )
dev . o f f ( )
dev . set ( o r i g d e v )

}

The code, which I won’t go into here, mostly involves manipulation of various R graphics devices.

506

APPENDIX A. R QUICK START

I’ve set it up so that you can save to a ﬁle of type either PDF, PNG or JPEG, implied by the ﬁle
name you give.

A.10 Packages

The analog of a library in C/C++ in R is called a package (and often loosely referred to as a
library). Some are already included in base R, while others can be downloaded, or written by
yourself.

> l i b r a r y ( p a r a l l e l ) # l o a d t h e p a c k a g e named ’ p a r a l l e l ’
> l s ( package : p a r a l l e l ) # l e t ’ s s e e what

f u n c t i o n s

[ 1 ] ” c l u s t e r A p p l y ”
[ 4 ] ” c l u s t e r E v a l Q ”
[ 7 ] ” clusterSetRNGStream ” ” c l u s t e r S p l i t ”

” c lus t er Appl yLB ”
” c l u s t e r E x p o r t ”

[ 1 0 ] ” makeCluster ”
[ 1 3 ] ”mc . r e s e t . stream ”
[ 1 6 ] ” mclapply ”
[ 1 9 ] ” m c p a r a l l e l ”
[ 2 2 ] ” parApply ”
[ 2 5 ] ” parLapplyLB ”
[ 2 8 ] ” parSapplyLB ”
[ 3 1 ] ” s p l i t I n d i c e s ”
> ? pvec # l e t ’ s s e e how one o f

” makeForkCluster ”
” m c a f f i n i t y ”
”mcMap”
”nextRNGStream”
” parCapply ”
” parRapply ”
” pvec ”
” s t o p C l u s t e r ”

them works

i t g a v e us
” c l u s t e r C a l l ”
” c l u s t e r M a p ”
” d e t e c t C o r e s ”
” makePSOCKcluster ”
” m c c o l l e c t ”
”mcmapply”
”nextRNGSubStream”
” parLapply ”
” parSapply ”
” s e t D e f a u l t C l u s t e r ”

The CRAN repository of contributed R code has thousands of R packages available. It also includes
a number of “tables of contents” for speciﬁc areas, say time series, in the form of CRAN Task Views.
See the R home page, or simply Google “CRAN Task View.”

> i n s t a l l . packages ( ” c t s ” , ”˜/myr” ) # download i n t o d e s i r e d d i r e c t o r y
−−− P l e a s e
. . .
downloaded 533 Kb

s e l e c t a CRAN m i r r o r

for u s e i n t h i s

s e s s i o n −−−

The downloaded b i n a r y packages a r e i n

/var/ f o l d e r s / j k / d h 9 z k d s 9 7 s j 2 3 k j c f k r 5 v 6 q 0 0 0 0 0 g n /T//RtmplkKzOU/downloaded packages

> ? l i b r a r y
> l i b r a r y ( c t s , l i b . l o c=”˜/myr” )

A t t a c h i n g package :
. . .

c t s

A.11. OTHER SOURCES FOR LEARNING R

507

A.11 Other Sources for Learning R

There are tons of resources for R on the Web. You may wish to start with the links at http:
//heather.cs.ucdavis.edu/~matloff/r.html.

A.12 Online Help

R’s help() function, which can be invoked also with a question mark, gives short descriptions of
the R functions. For example, typing

> ?rep

will give you a description of R’s rep() function.

An especially nice feature of R is its example() function, which gives nice examples of whatever
function you wish to query. For instance, typing

> example ( w i r e f r a m e ( ) )

will show examples—R code and resulting pictures—of wireframe(), one of R’s 3-dimensional
graphics functions.

A.13 Debugging in R

The internal debugging tool in R, debug(), is usable but rather primitive. Here are some alterna-
tives:

• The RStudio IDE has a built-in debugging tool.

• For Emacs users, there is ess-tracebug.

• The StatET IDE for R on Eclipse has a nice debugging tool. Works on all major platforms,

but can be tricky to install.

• My own debugging tool, debugR, is extensive and easy to install, but for the time being is lim-

ited to Linux, Mac and other Unix-family systems. See http://heather.cs.ucdavis.edu/debugR.html.

508

APPENDIX A. R QUICK START

A.14 Complex Numbers

If you have need for complex numbers, R does handle them. Here is a sample of use of the main
functions of interest:

> za <− complex ( r e a l =2, i m a g i n a r y =3.5)
> za
[ 1 ] 2+3.5 i
> zb <− complex ( r e a l =1, i m a g i n a r y=−5)
> zb
[ 1 ] 1−5 i
> za ∗ zb
[ 1 ] 19.5 −6.5 i
> Re( za )
[ 1 ] 2
> Im( za )
[ 1 ] 3 . 5
> za ˆ2
[ 1 ] −8.25+14 i
> abs ( za )
[ 1 ] 4 . 0 3 1 1 2 9
> exp ( complex ( r e a l =0, i m a g i n a r y=p i / 4 ) )
[ 1 ] 0 . 7 0 7 1 0 6 8 + 0 . 7 0 7 1 0 6 8 i
> cos ( p i / 4 )
[ 1 ] 0 . 7 0 7 1 0 6 8
> sin ( p i / 4 )
[ 1 ] 0 . 7 0 7 1 0 6 8

Note that operations with complex-valued vectors and matrices work as usual; there are no special
complex functions.

A.15 Further Reading

For further information about R as a programming language, there is my book, The Art of R
Programming: a Tour of Statistical Software Design, NSP, 2011, as well as Hadley Wickham’s
Advanced R, Chapman and Hall, 2014.

For R’s statistical functions, a plethora of excellent books is available. such as The R Book (2nd
Ed.), Michael Crowley, Wiley, 2012. I also very much like R in a Nutshell (2nd Ed.), Joseph Adler,
O’Reilly, 2012, and even Andrie de Vries’ R for Dummies, 2012.

Appendix B

Review of Matrix Algebra

This book assumes the reader has had a course in linear algebra (or has self-studied it, always
the better approach). This appendix is intended as a review of basic matrix algebra, or a quick
treatment for those lacking this background.

B.1 Terminology and Notation

A matrix is a rectangular array of numbers. A vector is a matrix with only one row (a row
vector or only one column (a column vector).

The expression, “the (i,j) element of a matrix,” will mean its element in row i, column j.

Please note the following conventions:

• Capital letters, e.g. A and X, will be used to denote matrices and vectors.

• Lower-case letters with subscripts, e.g. a2,15 and x8, will be used to denote their elements.

• Capital letters with subscripts, e.g. A13, will be used to denote submatrices and subvectors.

If A is a square matrix, i.e., one with equal numbers n of rows and columns, then its diagonal
elements are aii, i = 1,...,n.

A square matrix is called upper-triangular if aij = 0 whenever i > j, with a corresponding
deﬁnition for lower-triangular matrices.

509

510

APPENDIX B. MATRIX REVIEW

The norm (or length) of an n-element vector X is

(cid:107) X (cid:107)=

(cid:118)
(cid:117)
(cid:117)
(cid:116)

n
(cid:88)

i=1

x2
i

(B.1)

B.1.1 Matrix Addition and Multiplication

• For two matrices have the same numbers of rows and same numbers of columns, addition is

deﬁned elementwise, e.g.





1 5
0 3
4 8





 +



6 2
0 1
4 0





 =







7 7
0 4
8 8

• Multiplication of a matrix by a scalar, i.e., a number, is also deﬁned elementwise, e.g.



0.4



7 7
0 4
8 8





 =







2.8 2.8
0
1.6
3.2 3.2

• The inner product or dot product of equal-length vectors X and Y is deﬁned to be

n
(cid:88)

k=1

xkyk

(B.2)

(B.3)

(B.4)

• The product of matrices A and B is deﬁned if the number of rows of B equals the number of
columns of A (A and B are said to be conformable). In that case, the (i,j) element of the
product C is deﬁned to be

For instance,

cij =

n
(cid:88)

k=1

aikbkj





7 6
0 4
8 8





(cid:18) 1 6
2 4

(cid:19)



=







19 66
8
16
24 80

(B.5)

(B.6)

B.2. MATRIX TRANSPOSE

511

It is helpful to visualize cij as the inner product of row i of A and column j of B, e.g. as
shown in bold face here:





7 6
0 4
8 8





(cid:18) 1 6
2 4

(cid:19)



=







19 66
8
16
24 80

• Matrix multiplication is associative and distributive, but in general not commutative:

A(BC) = (AB)C

A(B + C) = AB + AC

AB (cid:54)= BA

(B.7)

(B.8)

(B.9)

(B.10)

B.2 Matrix Transpose

• The transpose of a matrix A, denoted A(cid:48) or AT , is obtained by exchanging the rows and

columns of A, e.g.





7 70
8 16
8 80

(cid:48)




=

(cid:18) 7

8
8
70 16 80

(cid:19)

• If A + B is deﬁned, then

• If A and B are conformable, then

(A + B)(cid:48) = A(cid:48) + B(cid:48)

(AB)(cid:48) = B(cid:48)A(cid:48)

(B.11)

(B.12)

(B.13)

512

APPENDIX B. MATRIX REVIEW

B.3 Linear Independence

Equal-length vectors X1,...,Xk are said to be linearly independent if it is impossible for

a1X1 + ... + akXk = 0

(B.14)

unless all the ai are 0.

B.4 Determinants

Let A be an n × n matrix. The deﬁnition of the determinant of A, det(A), involves an abstract
formula featuring permutations. It will be omitted here, in favor of the following computational
method.

Let A−(i,j) denote the submatrix of A obtained by deleting its ith row and jth column. Then the
determinant can be computed recursively across the kth row of A as

where

n
(cid:88)

det(A) =

(−1)k+mdet(A−(k,m))

m=1

det

(cid:19)

(cid:18) s

t
u v

= sv − tu

(B.15)

(B.16)

Generally, determinants are mainly of theoretical importance, but they often can clarify one’s
understanding of concepts.

B.5 Matrix Inverse

• The identity matrix I of size n has 1s in all of its diagonal elements but 0s in all oﬀ-diagonal
elements. It has the property that AI = A and IA = A whenever those products are deﬁned.

• The A is a square matrix and AB = I, then B is said to be the inverse of A, denoted A−1.

Then BA = I will hold as well.

• A−1 exists if and only if its rows (or columns) are linearly independent.

B.6. EIGENVALUES AND EIGENVECTORS

513

• A−1 exists if and only if det(A) (cid:54)= 0.

• If A and B are square, conformable and invertible, then AB is also invertible, and

(AB)−1 = B−1A−1

(B.17)

A matrix U is said to be orthogonal if its rows each have norm 1 and are orthogonal to each other,
i.e., their inner product is 0. U thus has the property that U U (cid:48) = I i.e., U −1 = U .

The inverse of a triangular matrix is easily obtained by something called back substitution.

Typically one does not compute matrix inverses directly. A common alternative is the QR de-
composition: For a matrix A, matrices Q and R are calculated so that A = QR, where Q is an
orthogonal matrix and R is upper-triangular.

If A is square and invertible, A−1 is easily found:

A−1 = (QR)−1 = R−1Q(cid:48)

(B.18)

Again, though, in some cases A is part of a more complex system, and the inverse is not explicitly
computed.

B.6 Eigenvalues and Eigenvectors

Let A be a square matrix.1

• A scalar λ and a nonzero vector X that satisfy

AX = λX

(B.19)

are called an eigenvalue and eigenvector of A, respectively.

• If A is symmetric and real, then it is diagonalizable, i.e., there exists an orthogonal matrix

U such that

U (cid:48)AU = D

(B.20)

for a diagonal matrix D. The elements of D are the eigenvalues of A, and the columns of U
are the eigenvectors of A.

1For nonsquare matrices, the discussion here would generalize to the topic of singular value decomposition.

514

APPENDIX B. MATRIX REVIEW

A diﬀerent suﬃcient condition for B.20 is that the eigenvalues of A are distinct. In this case,
U will not necessarily be orthogonal.

By the way, this latter suﬃcient condition shows that “most” square matrices are diagonaliz-
able, if we treat their entries as continous random variables. Under such a circumstance, the
probability of having repeated eigenvalues would be 0.

B.7 Matrix Algebra in R

The R programming language has extensive facilities for matrix algebra, introduced here. Note by
the way that R uses column-major order.

A linear algebra vector can be formed as an R vector, or as a one-row or one-column matrix.

> # c o n s t r u c t i n g m a t r i c e s
> a <− rbind ( 1 : 3 , 1 0 : 1 2 )
> a

[ , 1 ]
1
10

[ , 2 ]
2
[ 1 , ]
[ 2 , ]
11
> b <− matrix ( 1 : 9 , ncol =3)
> b

[ , 3 ]
3
12

[ , 1 ]
1
2
3

[ , 2 ]
4
5
6

[ , 3 ]
7
8
9

[ 1 , ]
[ 2 , ]
[ 3 , ]
# m u l t i p l i c a t i o n , e t c .
> c <− a %∗% b ; c + matrix ( c ( 1 , − 1 , 0 , 0 , 3 , 8 ) ,nrow=2)

[ , 1 ]
15
67

[ , 2 ]
32
167

[ , 3 ]
53
274

[ 1 , ]
[ 2 , ]
> c %∗% c ( 1 , 5 , 6 ) # n o t e 2 d i f f e r e n t c ’ s

[ , 1 ]
474
[ 1 , ]
> # t r a n s p o s e ,
> t ( a ) # t r a n s p o s e

i n v e r s e

[ , 1 ]
1
2
3

[ , 2 ]
10
11
12

[ 1 , ]
[ 2 , ]
[ 3 , ]
> u <− matrix ( runif ( 9 ) , nrow=3)

B.7. MATRIX ALGEBRA IN R

515

> u

[ , 1 ]

[ , 2 ]

[ , 3 ]
[ 1 , ] 0 . 0 8 4 4 6 1 5 4 0 . 8 6 3 3 5 2 7 0 0 . 6 9 6 2 0 9 2
[ 2 , ] 0 . 3 1 1 7 4 3 2 4 0 . 3 5 3 5 2 1 3 8 0 . 7 3 1 0 3 5 5
[ 3 , ] 0 . 5 6 1 8 2 2 2 6 0 . 0 2 3 7 5 4 8 7 0 . 2 9 5 0 2 2 7
> uinv

[ , 1 ]

[ , 2 ]
0 . 5 8 1 8 4 8 2 −1.594123
2 . 1 3 3 3 9 6 5 −2.451237

[ , 3 ]
2 . 5 7 6 9 9 5
[ 1 , ]
1 . 0 3 9 4 1 5
[ 2 , ]
[ 3 , ] −1.2798127
3 . 2 3 3 1 1 5 −1.601586
> u %∗% uinv # n o t e r o u n d o f f e r r o r

[ , 1 ]

[ , 2 ]

[ , 3 ]
[ 1 , ] 1 . 0 0 0 0 0 0 e+00 −1.680513 e −16 −2.283330 e −16
4 . 4 1 2 7 0 3 e −17
[ 2 , ] 6 . 6 5 1 5 8 0 e −17
1 . 0 0 0 0 0 0 e+00
1 . 0 0 0 0 0 0 e+00
[ 3 , ] 2 . 2 8 7 6 6 7 e −17 −3.539920 e −17
> # e i g e n v a l u e s and e i g e n v e c t o r s
> eigen ( u )
$ v a l u e s
[ 1 ]
−0.2563082 −0.2329172 i

1 . 2 4 5 6 2 2 0 + 0 . 0 0 0 0 0 0 0 i −0.2563082+0.2329172 i

[ , 3 ]

$ v e c t o r s

[ , 1 ]

[ , 2 ]
[ 1 , ] −0.6901599+0 i −0.6537478+0.0000000 i
−0.6537478+0.0000000 i
[ 2 , ] −0.5874584+0 i −0.1989163 −0.3827132 i
−0.1989163+0.3827132 i
[ 3 , ] −0.4225778+0 i
0.5666579 −0.2558820 i
> # d i a g o n a l m a t r i c e s ( o f f −d i a g o n a l s 0)
> diag ( 3 )
[ , 1 ]
1
0
0

[ 1 , ]
[ 2 , ]
[ 3 , ]
> diag ( ( c ( 5 , 1 2 , 1 3 ) ) )

0 . 5 6 6 6 5 7 9 + 0 . 2 5 5 8 8 2 0 i

[ , 3 ]
0
0
1

[ , 2 ]
0
1
0

[ , 1 ]
5
0
0

[ , 2 ]
0
12
0

[ , 3 ]
0
0
13

[ 1 , ]
[ 2 , ]
[ 3 , ]

516

APPENDIX B. MATRIX REVIEW

Appendix C

Introduction to the ggplot2 Graphics
Package

C.1 Introduction

Hadley Wickham’s ggplot2 package is a hugely popular alternative to R’s base graphics package.
(Others include lattice, ggobi and so on.)

The ggplot2 pacakge is an implementation of the ideas in the book, The Grammar of Graphics, by
Leland Wilkison, whose goal was to set out a set of general unifying principles for the visualization
of data. For this reason, ggplot2 oﬀers a more elegant and arguably more natural approach than
does the base R graphics package.

The package has a relatively small number of primitive functions, making it relatively easy to
master. But through combining these functions in various ways, a very large number of types
of graphs may be produced. It is considered especially good in setting reasonable default values
of parameters, and much is done without the user’s asking. Legends are automatically added to
graphs, for instance.

The package is quite extensive (only a few functions, but lots of options), and thus this document
is merely a brief introduction.

C.2 Installation and Use

Download and install ggplot2 with the usual install.packages() function, and then at each usage,
load via library(). Here’s what I did on my netbook:

517

518

APPENDIX C. INTRODUCTION TO THE GGPLOT2 GRAPHICS PACKAGE

# d i d once :
> i n s t a l l . packages ( ” g g p l o t 2 ” , ”/home/nm/R” )
# do each time I use t h e p a c k a g e ( or s e t
> . l i b P a t h s ( ”/home/nm/R” )
> l i b r a r y ( g g p l o t 2 )

i n . R p r o f i l e )

C.3 Basic Structures

One operates in the following pattern:

• One begins with a call to ggplot():

> p <− g g p l o t ( yo urd at afr am e )

or

> p <− g g p l o t ( yourdataframe , a e s ( y o u r a r g s ) )

Here yourdataframe could have been read from a ﬁle, say using read.table(), or generated
within the program. If your data is in the form of an R matrix, use as.data.frame() to
convert it.

The result p is an R S3 object of class ”ggplot”, consisting of a component named data,
and other components containing information about the plot.

Note that at this point, though, there is nothing to plot (if we didn’t call aes()).

• One adds features to—or even changes—the plot via the + operator, which of course is an

overloaded version of R’s built-in +, the function ”+.ggplot”().

Each invocation of + adds a new layer to the graph, adding to the contents of the previous
layer. Typically, each new layer adds new features to the graph, or changes old features. One
might, for instance, superimpose several curves on the same graph, by adding one new layer
per curve.1

The idea of layering is partly motivated by reusability. One can save a lower layer in a
variable (or on disk, using the R save() function), so that we can make a diﬀerent graph,
with diﬀerent features, starting with the same layer.

To actually display a plot, we print it, i.e. print p. Recall that in R, print() is a generic
function, i.e. a stub for a class-speciﬁc one. In this case the latter does a plot. At this stage,
we don’t have anything to display yet, if we didn’t call aes() above.

1There are ways to do this in a single layer, but let’s not get too complex in this introductory document.

C.4. EXAMPLE: SIMPLE LINE GRAPHS

519

• The function aes() (“aesthetics”) is used to specify graph attributes. For instance, in a
scatter plot, which variable will be on the horizontal axis, and which on the vertical? What
colors do we want for the points? Etc.

We can call aes() at various layers, depending on how general (reusable, as noted above) we
want each layer to be.

So for instance we could use aes() to specify our data variables either when we call ggplot(),
so these variables will be used in all operations, or when we later add a layer calling, say,
geom point(), to indicate data variables for this speciﬁc operation.

There are various types of objects that can be used as the second operand for the + operator.
Examples are:

• geoms (“geometrics”): Geometric objects to be drawn, such as points, lines, bars, polygons

and text.

• position adjustments: For instance, in a bar graph, this controls whether bars should be

side by side, or stacked on top of each other.

• facets: Speciﬁcations to draw many graphs together, as panels in a large graph. You can

have rows of panels, columns of panels, and rows and columns of panels.

• themes: Don’t like the gray background in a graph? Want nicer labeling, etc.? You can set
each of these individually, but one of the built-in themes, or a user-contributed one, can save
you the trouble, or you can write one that you anticipate using a lot.

C.4 Example: Simple Line Graphs

> d f 1
u v
1 0 3
2 2 4
3 5 5
> g g p l o t ( d f 1 ) + geom l i n e ( a e s ( x=u , y=v ) )

Here aes() was called from geom line() rather than from ggplot(), so as to apply just to this
line. The result is

520

APPENDIX C. INTRODUCTION TO THE GGPLOT2 GRAPHICS PACKAGE

Now let’s add a second line, from a diﬀerent data frame:

> d f 2
w z
1 1 4
2 2 5
3 3 9
g g p l o t ( d f 1 ) + geom l i n e ( a e s ( x=u , y=v ) ) + geom l i n e ( data=df2 , a e s ( x=w, y=z ) )

Here is the result:

3.03.54.04.55.0012345uvC.5. EXAMPLE: CENSUS DATA

521

It worked as long as we speciﬁed data for the second line.

Note that ggplot2 automatically adjusted that second graph, to make room for the “taller” second
line.

C.5 Example: Census Data

The data set here consists of programmers (software engineers, etc.) and electrical engineers in
Silicon Valley, in the 2000 Census. I’ve removed those with less than a Bachelor’s degree. The R
object was a data frame named pm.

I ﬁrst ran

p <− g g p l o t (pm)

to set up the ggplot object. Next, I typed

p + geom h i s t o g r a m ( a e s ( HiDeg ) )

which produced a histogram of a particular variable in the data (i.e. a particular column in the
data frame), which was the highest-degree values of the workers:

468012345uv522

APPENDIX C. INTRODUCTION TO THE GGPLOT2 GRAPHICS PACKAGE

Note that the + operation yields a new object of class ”ggplot”. Since the generic print function
for that class actually plots the graph, the graph did appear on the screen. I could have saved the
new object in a variable if needed.

I then decided to do a scatter plot of salary versus age:

> p + geom p o i n t ( a e s ( x=Age , y=WgInc ) )

So, here is an example of the reusability mentioned earlier. For this small data set, it wasn’t an issue,
but some larger data sets can take a while to render, so you deﬁnitely want to save intermediate
results for reuse.

Note the roles of aes() both here and in the previous example. I used it to specify for the geom
what I wanted to do in that layer. Each geom has its own set of aesthetics one can specify. In the
case of geom point(), I need to specify which variable to use for the X- and Y-axes. There are
other aesthetics for this geom that could be speciﬁed, as you’ll see below.

This gave me this graph:

HiDegcount0200040006000800010000BSMSPhDC.5. EXAMPLE: CENSUS DATA

523

(As is often the case with large data sets, the points tend to “ﬁll in” entire regions. One solution
is to graph a random subset of the data, not done here. Data smoothing techniques can also be
used. Similar comments apply to some of the graphs below.)

However, I wanted to separate the points according to highest degree level:

> p + geom p o i n t ( a e s ( x=Age , y=WgInc , c o l o r=HiDeg ) )

Here I have three data variables informing aes(): Age, wage income and highest degree. The
argument color here means that I want the degree to be used for color coding the points:

AgeWgInc050000100000150000llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll2030405060708090524

APPENDIX C. INTRODUCTION TO THE GGPLOT2 GRAPHICS PACKAGE

So the orange points are for Bachelor’s people, green for Master’s and blue for PhDs. Note the
color legend that was automatically included on the right.

Since some people might be viewing a black-and-white version of this document, I ran the command
again, specifying coding the highest degree by point shape instead of point color:

p + geom p o i n t ( a e s ( x=Age , y=WgInc , shape=HiDeg ) )

Here ggplot2 decided to use a circle, a triangle and a square to represent Bachelor’s, Master’s and
PhD workers:

AgeWgInc050000100000150000llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll2030405060708090HiDeglBSlMSlPhDC.5. EXAMPLE: CENSUS DATA

525

Since I’m interested in age discrimination in the industry, I decided to restrict my graph to those
over age 40. The ggplot2 package cleverly exploits the R subset() function, allowing me to write

p %+% subset (pm, Age > 4 0 ) + geom p o i n t ( a e s ( x=Age , y=WgInc , c o l o r=HiDeg ) )

The new operator %+% is again mapped to ”+.ggplot”(). The result was

AgeWgInc050000100000150000lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll2030405060708090HiDeglBSMSPhD526

APPENDIX C. INTRODUCTION TO THE GGPLOT2 GRAPHICS PACKAGE

Look at all those 0-income PhDs! (There was also a business income variable, which I did not
pursue here, so maybe some had high incomes after all.)

Even with color, it’s a little hard to compare the three degree levels, so let’s try faceting:

> pt <− p + geom p o i n t ( a e s ( x=Age , y=WgInc ) )
> pt + f a c e t grid ( HiDeg ˜ . )

Here I saved an overall graph of wage income versus age to pt, and then added faceting by degree.
This instructed ggplot2 to draw three graphs, one for each degree level, as three panels in the
same large graph. The HiDeg ˜. argument (which you may recognize as similar notation to R’s
lm() function for linear models) states that I want HiDeg to deﬁne the rows of panels, without any
variable deﬁning columns. The result was:

AgeWgInc050000100000150000lllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll5060708090HiDeglBSlMSlPhDC.5. EXAMPLE: CENSUS DATA

527

I tried a horizontal presentation too:

AgeWgInc050000100000150000050000100000150000050000100000150000llllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll2030405060708090BSMSPhD528

APPENDIX C. INTRODUCTION TO THE GGPLOT2 GRAPHICS PACKAGE

Note that there doesn’t seem to be much relation between degree in salary, after age 35 or so.

Note that I could have combined operations, e.g.

> p + geom p o i n t ( a e s ( x=Age , y=WgInc ) ) + f a c e t grid ( HiDeg ˜ . )

to get the vertical set of panels, without directly going through the intermediate step of drawing
the nonfaceted graph.

If there had been a Gender variable in the data, I could have deﬁned rows and columns of panels:

> p + geom p o i n t ( a e s ( x=Age , y=WgInc ) ) + f a c e t grid ( Gender ˜ HiDeg )

C.6 Function Plots, Density Estimates and Smoothing

The ggplot2 package has lots of ways to plot known functions and to do smoothing. Here are some
examples:

# p l o t a f u n c t i o n

AgeWgInc050000100000150000BSlllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll2030405060708090MSlllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll2030405060708090PhDllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllllll2030405060708090C.7. WHAT’S GOING ON INSIDE

529

s q f u n <− function ( t ) t ˆ2
# p l o t o v e r t h e i n t e r v a l
p <− g g p l o t ( data . frame ( x=c ( 0 , 2 ) ) , a e s ( x ) )
p + stat function ( fun=s q f u n )

( 0 , 2 )

# compute and p l o t a d e n s i t y e s t i m a t e
w <− rnorm( 2 5 0 0 )
p <− g g p l o t ( data . frame (w) )
p + geom density ( a e s ( x=w) ) # many o t h e r c h o i c e s
y <− rnorm( 2 5 0 0 , sd=2)
p + geom density ( a e s ( x=w) ) + geom density ( a e s ( x=y ) )

# g e n e r a t e d a t a from a q u a d r a t i c f u n c t i o n w i t h added n o i s e ;
# p r e t e n d we don ’ t know i t ’ s a q u a d r a t i c f u n c t i o n , and
# smooth t h e d a t a t o g e t an i d e a o f what
t <− seq ( 0 . 0 1 , 2 , 0 . 0 1 )
u <− t ˆ2 + rnorm( n=100 ,sd =0.2)
d <− data . frame ( t , u )
p <− g g p l o t ( d )
p + geom smooth ( a e s ( x=t , y=u ) , method=” l o e s s ” )

t h e f u n c t i o n i s

l i k e

C.7 What’s Going on Inside

In order to use ggplot2 most eﬀectively, it helps to understand what happens one level down. Let’s
do so via the ﬁrst example in this document:

> l i b r a r y ( g g p l o t 2 )
> d f 1 <− data . frame ( u = c ( 0 , 2 , 5 ) , v = c ( 3 : 5 ) )
> d f 1
u v
1 0 3
2 2 4
3 5 5
> p <− g g p l o t ( d f 1 )
> p
E r r o r : No l a y e r s

i n plot

By just typing p, we meant to print it, but there is nothing to print for now. Yet, even at this
early stage, p has a lot in it:

530

APPENDIX C. INTRODUCTION TO THE GGPLOT2 GRAPHICS PACKAGE

> s t r ( p )
L i s t o f 9
$ data

: ’ data . frame ’ :

3 obs . o f

2 v a r i a b l e s :

. . $ u : num [ 1 : 3 ] 0 2 5
[ 1 : 3 ] 3 4 5
. . $ v :
$ l a y e r s
l i s t ( )
$ s c a l e s

:
: R e f e r e n c e c l a s s

i n t

’ S c a l e s ’

[ package ” g g p l o t 2 ” ] with 1 f i e l d s

. . $ s c a l e s : NULL
. . and 21 methods , o f which 9 a r e p o s s i b l y r e l e v a n t :
. .
. .

add , c l o n e ,
non p o s i t i o n s c a l e s

find , get s c a l e s , has scale ,

i n i t i a l i z e ,

input , n ,

:
:

l i s t ( )
l i s t ( )

$ mapping
$ theme
$ c o o r d i n a t e s : L i s t o f 1
. . $ l i m i t s : L i s t o f 2
. .
. .
.. − attr ( ∗ , ” c l a s s ”)= c h r

. . $ x : NULL
. . $ y : NULL

$ f a c e t

: L i s t o f 1
l o g i TRUE

. . $ s h r i n k :
.. − attr ( ∗ , ” c l a s s ”)= c h r

[ 1 : 2 ] ” c a r t e s i a n ” ” c o o r d ”

[ 1 : 2 ] ” n u l l ” ” f a c e t ”

$ plot env
$ l a b e l s
− attr ( ∗ , ” c l a s s ”)= c h r

:<environment : R GlobalEnv>
:

l i s t ( )

[ 1 : 2 ] ” gg ” ” g g p l o t ”

You can see that p is indeed a class object, consisting of a list of various components, some of
which themselves are lists. Note the data component, which sure enough does consist of the data
frame we had speciﬁed.

Note by the way that the layer component, i.e. p$layers, is empty, resulting in our failure to plot
when we tried to do so above.

Now, let’s add a geom:

> p1 <− p + geom l i n e ( a e s ( x=u , y=v ) )
> s t r ( p1 )
L i s t o f 9
$ data

: ’ data . frame ’ :

3 obs . o f

2 v a r i a b l e s :

. . $ u : num [ 1 : 3 ] 0 2 5
[ 1 : 3 ] 3 4 5
. . $ v :
: L i s t o f 1
$ l a y e r s
’ p r o t o ’ ,

. . $ : C l a s s e s

i n t

’ environment ’ <environment : 0 x96d8264>

C.8. FOR FURTHER INFORMATION

531

$ s c a l e s

: R e f e r e n c e c l a s s
l i s t ( )

. . $ s c a l e s :
. . and 21 methods , o f which 9 a r e p o s s i b l y r e l e v a n t :
. .
. .

add , c l o n e ,
non p o s i t i o n s c a l e s

find , get s c a l e s , has scale ,

i n i t i a l i z e ,

input , n ,

’ S c a l e s ’

[ package ” g g p l o t 2 ” ] with 1 f i e l d s

:
:

l i s t ( )
l i s t ( )

$ mapping
$ theme
$ c o o r d i n a t e s : L i s t o f 1
. . $ l i m i t s : L i s t o f 2
. .
. .
.. − attr ( ∗ , ” c l a s s ”)= c h r

. . $ x : NULL
. . $ y : NULL

$ f a c e t

: L i s t o f 1
l o g i TRUE

. . $ s h r i n k :
.. − attr ( ∗ , ” c l a s s ”)= c h r

[ 1 : 2 ] ” c a r t e s i a n ” ” c o o r d ”

[ 1 : 2 ] ” n u l l ” ” f a c e t ”

$ plot env
$ l a b e l s

:<environment : R GlobalEnv>
: L i s t o f 2

. . $ x : c h r ”u”
. . $ y : c h r ”v”

− attr ( ∗ , ” c l a s s ”)= c h r

[ 1 : 2 ] ” gg ” ” g g p l o t ”

Now the layers component is nonempty, as is the labels component.

Obviously the rest is complicated, but at least now you have some understanding of what happens
to these class objects when we do “+”.

Among other things, this insight can help you in debugging, if your ggplot2 code doesn’t produce
what you had expected.

C.8 For Further Information

Just plugging “ggplot2 tutorial,” “ggplot2 introduction,” “ggplot2 examples” and so on into your
favorite search engine will give you tons of information.

Hadley’s book, ggplot2: Elegant Graphics for Data Analysis, is of course the deﬁnitive source, but
also try his pictorial reference manual, at http://had.co.nz/ggplot2/. Winston Chang’s O’Reilly
series book, the R Graphics Cookbook, is chock full of examples, almost all of them using ggplot2.
Paul Murrell’s book, R Graphics, gives a more general treatment of graphics in R.

The ggobi packated, whose lead author is UCD professor Duncan Temple Lang, takes an interactive
approach to graphics.

