Академический Документы
Профессиональный Документы
Культура Документы
1 Some Preliminaries
1.1 Notation and conventions . . . . . . . . . . . . . . .
1.1.1 Background Information . . . . . . . . . . . .
1.2 Some Useful Mathematical Facts . . . . . . . . . . .
1.3 Acknowledgements . . . . . . . . . . . . . . . . . . .
1.4 The Curse of Dimension . . . . . . . . . . . . . . . .
1.4.1 The Curse: Data isnt Where You Think it is
1.4.2 Minor Banes of Dimension . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
2 Learning to Classify
2.1 Classification, Error, and Loss . . . . . . . . . . . . . . . .
2.1.1 Loss and the Cost of Misclassification . . . . . . .
2.1.2 Building a Classifier from Probabilities . . . . . . .
2.1.3 Building a Classifier using Decision Boundaries . .
2.1.4 What will happen on Test Data? . . . . . . . . . .
2.1.5 The Class Confusion Matrix . . . . . . . . . . . . .
2.1.6 Statistical Learning Theory and Generalization . .
2.2 Classifying with Naive Bayes . . . . . . . . . . . . . . . .
2.3 The Support Vector Machine . . . . . . . . . . . . . . . .
2.3.1 Choosing a Classifier with the Hinge Loss . . . . .
2.3.2 Finding a Minimum: General Points . . . . . . . .
2.3.3 Finding a Minimum: Stochastic Gradient Descent
2.3.4 Example: Training a Support Vector Machine with
2.3.5 Multi-Class Classifiers . . . . . . . . . . . . . . . .
2.4 Classifying with Random Forests . . . . . . . . . . . . . .
2.4.1 Building a Decision Tree . . . . . . . . . . . . . . .
2.4.2 Entropy and Information Gain . . . . . . . . . . .
2.4.3 Entropy and Splits . . . . . . . . . . . . . . . . . .
2.4.4 Choosing a Split with Information Gain . . . . . .
2.4.5 Forests . . . . . . . . . . . . . . . . . . . . . . . . .
2.4.6 Building and Evaluating a Decision Forest . . . . .
2.4.7 Classifying Data Items with a Decision Forest . . .
2.5 Classifying with Nearest Neighbors . . . . . . . . . . . . .
2.6 You should . . . . . . . . . . . . . . . . . . . . . . . . . .
2.6.1 be able to: . . . . . . . . . . . . . . . . . . . . . . .
2.6.2 remember: . . . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
Stochastic
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
. . . . . .
2
2
3
4
4
4
4
6
7
7
8
8
9
9
11
12
13
17
18
20
21
Gradient Descent 23
25
26
26
29
31
32
34
34
35
37
40
40
40
3.3
3.4
3.5
3.6
3.7
3.8
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
56
56
60
60
61
64
66
67
69
71
73
75
76
78
78
79
81
82
83
83
86
87
91
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
93
93
95
100
102
104
105
107
108
109
111
112
116
116
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
118
118
119
119
120
121
122
124
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
125
128
129
129
131
131
6 Regression
6.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . .
6.2 Linear Regression and Least Squares . . . . . . . . . .
6.2.1 Linear Regression . . . . . . . . . . . . . . . . .
6.2.2 Residuals and R-squared . . . . . . . . . . . . .
6.2.3 Transforming Variables . . . . . . . . . . . . .
6.3 Finding Problem Data Points . . . . . . . . . . . . . .
6.3.1 The Hat Matrix and Leverage . . . . . . . . . .
6.3.2 Cooks Distance . . . . . . . . . . . . . . . . .
6.3.3 Standardized Residuals . . . . . . . . . . . . .
6.4 Many Explanatory Variables . . . . . . . . . . . . . .
6.4.1 Functions of One Explanatory Variable . . . .
6.4.2 Regularizing Linear Regressions . . . . . . . . .
6.4.3 Example: Weight against Body Measurements
6.5 You should . . . . . . . . . . . . . . . . . . . . . . . .
6.5.1 remember: . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
133
133
134
134
139
143
145
148
148
149
150
151
153
155
157
157
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
161
161
161
162
164
164
165
168
170
172
172
175
177
177
182
183
185
188
188
192
192
194
194
5.4
.
.
.
.
.
.
8 Classification II
8.1 Logistic Regression . . . . . . . . . .
8.2 Neural Nets . . . . . . . . . . . . . .
8.3 Convolution and orientation features
8.4 Convolutional neural networks . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
198
198
198
198
198
9 Boosting
199
9.1 GradientBoost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
9.2 ADAboost . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
10 Some Important Models
200
10.1 HMMs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
10.2 CRFs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
10.3 Fitting and inference with MCMC? . . . . . . . . . . . . . . . . . . . 200
11 Background: First Tools for Looking at Data
11.1 Datasets . . . . . . . . . . . . . . . . . . . . . . .
11.2 Whats Happening? - Plotting Data . . . . . . .
11.2.1 Bar Charts . . . . . . . . . . . . . . . . .
11.2.2 Histograms . . . . . . . . . . . . . . . . .
11.2.3 How to Make Histograms . . . . . . . . .
11.2.4 Conditional Histograms . . . . . . . . . .
11.3 Summarizing 1D Data . . . . . . . . . . . . . . .
11.3.1 The Mean . . . . . . . . . . . . . . . . . .
11.3.2 Standard Deviation and Variance . . . . .
11.3.3 Variance . . . . . . . . . . . . . . . . . . .
11.3.4 The Median . . . . . . . . . . . . . . . . .
11.3.5 Interquartile Range . . . . . . . . . . . . .
11.3.6 Using Summaries Sensibly . . . . . . . . .
11.4 Plots and Summaries . . . . . . . . . . . . . . . .
11.4.1 Some Properties of Histograms . . . . . .
11.4.2 Standard Coordinates and Normal Data .
11.4.3 Boxplots . . . . . . . . . . . . . . . . . . .
11.5 Whose is bigger? Investigating Australian Pizzas
11.6 You should . . . . . . . . . . . . . . . . . . . . .
11.6.1 be able to: . . . . . . . . . . . . . . . . . .
11.6.2 remember: . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
201
201
203
204
204
205
207
207
207
210
214
215
217
218
219
219
222
225
226
231
231
231
12 Background:Looking at Relationships
12.1 Plotting 2D Data . . . . . . . . . . . . . . . . . .
12.1.1 Categorical Data, Counts, and Charts . .
12.1.2 Series . . . . . . . . . . . . . . . . . . . .
12.1.3 Scatter Plots for Spatial Data . . . . . . .
12.1.4 Exposing Relationships with Scatter Plots
12.2 Correlation . . . . . . . . . . . . . . . . . . . . .
12.2.1 The Correlation Coefficient . . . . . . . .
12.2.2 Using Correlation to Predict . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
232
232
232
236
238
241
244
246
251
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
255
256
259
259
259
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
260
261
261
261
262
263
265
266
268
268
268
270
270
271
271
272
273
275
276
278
280
281
281
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
282
283
284
292
293
294
298
301
302
305
305
306
307
311
311
313
313
C H A P T E R
Some Preliminaries
1.1 NOTATION AND CONVENTIONS
A dataset as a collection of d-tuples (a d-tuple is an ordered list of d elements).
Tuples differ from vectors, because we can always add and subtract vectors, but
we cannot necessarily add or subtract tuples. There are always N items in any
dataset. There are always d elements in each tuple in a dataset. The number of
elements will be the same for every tuple in any given tuple. Sometimes we may
not know the value of some elements in some tuples.
We use the same notation for a tuple and for a vector. Most of our data will
be vectors. We write a vector in bold, so x could represent a vector or a tuple (the
context will make it obvious which is intended).
The entire data set is {x}. When we need to refer to the ith data item, we
write xi . Assume we have N data items, and we wish to make a new dataset out of
them; we write the dataset made out of these items as {xi } (the i is to suggest you
are taking a set of items and making a dataset out of them). If we need to refer
(j)
to the jth component of a vector xi , we will write xi (notice this isnt in bold,
because it is a component not a vector, and the j is in parentheses because it isnt
a power). Vectors are always column vectors.
When I write {kx}, I mean the dataset created by taking each element of the
dataset {x} and multiplying by k; and when I write {x + c}, I mean the dataset
created by taking each element of the dataset {x} and adding c.
Terms:
mean ({x}) is the mean of the dataset {x} (definition 11.1, page 207).
std ({x}) is the standard deviation of the dataset {x} (definition 11.2, page 210).
var ({x}) is the variance of the dataset {x} (definition 11.3, page 214).
median ({x}) is the standard deviation of the dataset {x} (definition 11.4,
page 215).
percentile({x}, k) is the k% percentile of the dataset {x} (definition 11.5,
page 217).
iqr{x} is the interquartile range of the dataset {x} (definition 11.7, page 218).
{
x} is the dataset {x}, transformed to standard coordinates (definition 11.8,
page 222).
Standard normal data is defined in definition 11.9, (page 223).
Normal data is defined in definition 11.10, (page 223).
7
Section 1.1
corr ({(x, y)}) is the correlation between two components x and y of a dataset
(definition 12.1, page 246).
is the empty set.
is the set of all possible outcomes of an experiment.
Sets are written as A.
argmax
f (x) means the value of x that maximises f (x).
x
argmin
f (x) means the value of x that minimises f (x).
x
maxi (f (xi )) means the largest value that f takes on the different elements of
the dataset {xi }.
is an estimated value of a parameter .
Section 1.2
By doing this, we get a function on positive real numbers that is a smooth interpolate of the factorial function. We wont do any real work with this function, so
wont expand on this definition. In practice, well either look up a value in tables
or require a software environment to produce it.
1.3 ACKNOWLEDGEMENTS
Typos spotted by: Han Chen (numerous!), Henry Lin (numerous!), Paris Smaragdis
(numerous!), Johnny Chang, Eric Huber, Brian Lunt, Yusuf Sobh, Scott Walters,
Your Name Here TAs for this course have helped improve the notes. Thanks
to Zicheng Liao, Michael Sittig, Nikita Spirin, Saurabh Singh, Daphne Tsatsoulis,
Henry Lin, Karthik Ramaswamy.
1.4 THE CURSE OF DIMENSION
High dimensional models display uninituitive behavior (or, rather, it can take years
to make your intuition see the true behavior of high-dimensional models as natural).
In these models, most data lies in places you dont expect. We will do several simple
calculations with an easy high-dimensional distribution to build some intuition.
1.4.1 The Curse: Data isnt Where You Think it is
Assume our data lies within a cube, with edge length two, centered on the origin.
This means that each component of xi lies in the range [1, 1]. One simple model
for such data is to assume that each dimension has uniform probability density in
this range. In turn, this means that P (x) = 21d . The mean of this model is at the
origin, which we write as 0.
The first surprising fact about high dimensional data is that most of the data
can lie quite far away from the mean. For example, we can divide our dataset into
two pieces. A() consists of all data items where every component of the data has
a value in the range [(1 ), (1 )]. B() consists of all the rest of the data. If
you think of the data set as forming a cubical orange, then B() is the rind (which
has thickness ) and A() is the fruit.
Your intuition will tell you that there is more fruit than rind. This is true,
for three dimensional oranges, but not true in high dimensions. The fact that the
orange is cubical just simplifies the calculations, but has nothing to do with the
real problem.
We can compute P ({x A()}) and P ({x A()}). These probabilities tell
us the probability a data item lies in the fruit (resp. rind). P ({x A()}) is easy
Section 1.4
10
to compute as
P ({x A()}) = (2(1 )))
1
2d
= (1 )d
and
P ({x B()}) = 1 P ({x A()}) = 1 (1 )d .
But notice that, as d ,
P ({x A()}) 0.
This means that, for large d, we expect most of the data to be in B(). Equivalently,
for large d, we expect that at least one component of each data item is close to
either 1 or 1.
This suggests (correctly) that much data is quite far from the origin. It is
easy to compute the average of the squared distance of data from the origin. We
want
!
Z
X
T
x2i P (x)dx
E x x =
box
i
but we can rearrange, so that
X 2 X
E xi =
E xT x =
i
box
x2i P (x)dx.
This means that, for large d, we expect our data points to be quite far apart.
Section 1.4
11
C H A P T E R
Learning to Classify
A classifier is a procedure that accepts a set of features and produces a class
label for them. There could be two, or many, classes. Classifiers are immensely
useful, and find wide application, because many problems are naturally classification
problems. For example, if you wish to determine whether to place an advert on a
web-page or not, you would use a classifier (i.e. look at the page, and say yes or
no according to some rule). As another example, if you have a program that you
found for free on the web, you would use a classifier to decide whether it was safe
to run it (i.e. look at the program, and say yes or no according to some rule). As
yet another example, credit card companies must decide whether a transaction is
good or fraudulent.
All these examples are two class classifiers, but in many cases it is natural
to have more classes. You can think of sorting laundry as applying a multi-class
classifier. You can think of doctors as complex multi-class classifiers. In this (crude)
model, the doctor accepts a set of features, which might be your complaints, answers
to questions, and so on, and then produces a response which we can describe as a
class. The grading procedure for any class is a multi-class classifier: it accepts a
set of features performance in tests, homeworks, and so on and produces a
class label (the letter grade).
Classifiers are built by taking a set of labeled examples and using them to
come up with a procedure that assigns a label to any new example. In the general
problem, we have a training dataset (xi , yi ); each of the feature vectors xi consists
of measurements of the properties of different types of object, and the yi are labels
giving the type of the object that generated the example. We will then use the
training dataset to find a procedure that will predict an accurate label (y) for any
new object (x).
Section 2.1
13
First, we need to be clear on what a good procedure is. Second, we really want the
procedure to be good on test data, which we havent seen and wont see; we only
get to see the training data. These two considerations shape much of what we do.
2.1.1 Loss and the Cost of Misclassification
The choice of procedure must depend on the cost of making a mistake. This cost
can be represented with a loss function, which specifies the cost of making each
type of mistake. I will write L(j k) for the loss incurred when classifying an
example of class j as having class k.
A two-class classifier can make two kinds of mistake. Because two-class classifiers are so common, there is a special name for each kind of mistake. A false
positive occurs when a negative example is classified positive (which we can write
L( +) and avoid having to remember which index refers to which class); a
false negative occurs when a positive example is classified negative (similarly
L(+ )). By convention, the loss of getting the right answer is zero, and the
loss for any wrong answer is non-negative.
The choice of procedure should depend quite strongly on the cost of each
mistake. For example, pretend there is only one disease; then doctors would be
classifiers, deciding whether a patient had it or not. If this disease is dangerous, but
is safely and easily treated, false negatives are expensive errors, but false positives
are cheap. In this case, procedures that tend to make more false positives than false
negatives are better. Similarly, if the disease is not dangerous, but the treatment is
difficult and unpleasant, then false positives are expensive errors and false negatives
are cheap, and so we prefer false negatives to false positives.
You might argue that the best choice of classifier makes no mistake. But for
most practical cases, the best choice of classifier is guaranteed to make mistakes.
As an example, consider an alien who tries to classify humans into male and female,
using only height as a feature. However the aliens classifier uses that feature, it
will make mistakes. This is because the classifier must choose, for each value of
height, whether to label the humans with that height male or female. But for the
vast majority of heights, there are some males and some females with that height,
and so the aliens classifier must make some mistakes whatever gender it chooses
for that height.
For many practical problems, it is difficult to know what loss function to use.
There is seldom an obvious choice. One common choice is to assume that all errors
are equally bad. This gives the 0-1 loss every error has loss 1, and all right
answers have loss zero.
2.1.2 Building a Classifier from Probabilities
Assume that we have a reliable model of p(y|x). This case occurs less often than you
might think for practical data, because building such a model is often very difficult.
However, when we do have a model and a loss function, it is easy to determine the
best classifier. We should choose the rule that gives minimum expected loss.
We start with a two-class classifier. At x, the expected loss of saying
is L(+ )p(+|x) (remember, L( ) = 0); similarly, the expected loss
of saying + is L( +)p(|x). At most points, one of L( +)p(|x) and
Section 2.1
14
L(+ )p(+|x) is larger than the other, and so the choice is clear. The remaining
set of points (where L( +)p(|x) = L(+ )p(+|x)) is small (formally, it
has zero measure) for most models and problems, and so it doesnt matter what we
choose at these points. This means that the rule
random choice
otherwise
is the best available. Because it doesnt matter what we do when L(+ )p(+|x) =
L( +)p(|x), it is fine to use
+ if L(+ )p(+|x) > L( +)p(|x)
say
otherwise
The same reasoning applies in the multi-class case. We choose the class where the
expected loss from that choice is smallest. In the case of 0-1 loss, this boils down
to:
choose k such that p(k|x) is largest.
2.1.3 Building a Classifier using Decision Boundaries
Building a classifier out of posterior probabilities is less common than you might
think, for two reasons. First, its often very difficult to get a good posterior probability model. Second, most of the model doesnt matter to the choice of classifier.
What is important is knowing which class has the lowest expected loss, not the
exact values of the expected losses, so we should be able to get away without an
exact posterior model.
Look at the rules in section 2.1.2. Each of them carves up the domain of x into
pieces, and then attaches a class the one with the lowest expected loss to each
piece. There isnt necessarily one piece per class, (though theres always one class
per piece). The important factor here is the boundaries between the pieces, which
are known as decision boundaries. A powerful strategy for building classifiers
is to choose some way of building decision boundaries, then adjust it to perform
well on the data one has. This involves modelling considerably less detail than
modelling the whole posterior.
For example, in the two-class case, we will spend some time discussing the
decision boundary given by
if xT a + b < 0
choose
+
otherwise
often written as signxT a + b (section 14.5). In this case we choose a and b to obtain
low loss.
2.1.4 What will happen on Test Data?
What we really want from a classifier is to have small loss on test data. But this
is difficult to measure or achieve directly. For example, think about the case of
classifying credit-card transactions as good or bad. We could certainly obtain
Section 2.1
15
a set of examples that have been labelled for training, because the card owner often
complains some time after a fraudulent use of their card. But what is important
here is to see a new transaction and label it without holding it up for a few months
to see what the card owner says. The classifier may never know if the label is right
or not.
Generally, we will assume that the training data is like the test data, and
so we will try to make the classifier perform well on the training data. Classifiers
that have small training error might not have small test error. One example of
this problem is the (silly) classifier that takes any data point and, if it is the same
as a point in the training set, emits the class of that point and otherwise chooses
randomly between the classes. This classifier has been learned from data, and has
a zero error rate on the training dataset; it is likely to be unhelpful on any other
dataset, however.
Test error is usually worse than training error, because of an effect that is
sometimes called overfitting, so called because the classification procedure fits
the training data better than it fits the test data. Other names include selection
bias, because the training data has been selected and so isnt exactly like the
test data, and generalizing badly, because the classifier fails to generalize. The
effect occurs because the classifier has been trained to perform well on the training
dataset, and the training dataset is not the same as the test dataset. First, it is
quite likely smaller. Second, it might be biased through a variety of accidents. This
means that small training error may have to do with quirks of the training dataset
that dont occur in other sets of examples. One consequence of overfitting is that
classifiers should always be evaluated on data that was not used in training.
Remember this:
Classifiers should always be evaluated on data that
was not used in training.
Now assume that we are using the 0-1 loss, so that the loss of using a classifier
is the same as the error rate, that is, the percentage of classification attempts on
a test set that result in the wrong answer. We could also use the accuracy, which
is the percentage of classification attempts that result in the right answer. We
cannot estimate the error rate of the classifier using training data, because the
classifier has been trained to do well on that data, which will mean our error rate
estimate will be too low. An alternative is to separate out some training data to
form a validation set (confusingly, this is often called a test set), then train the
classifier on the rest of the data, and evaluate on the validation set. This has the
difficulty that the classifier will not be the best estimate possible, because we have
left out some training data when we trained it. This issue can become a significant
nuisance when we are trying to tell which of a set of classifiers to usedid the
classifier perform poorly on validation data because it is not suited to the problem
representation or because it was trained on too little data?
We can resolve this problem with cross-validation, which involves repeatedly: splitting data into training and validation sets uniformly and at random,
Section 2.1
16
training a classifier on the training set, evaluating it on the validation set, and
then averaging the error over all splits. This allows an estimate of the likely future performance of a classifier, at the expense of substantial computation. You
should notice that cross-validation, in some sense, looks at the sensitivity of the
classifier to a change in the training set. The most usual form of this algorithm
involves omitting single items from the dataset and is known as leave-one-out
cross-validation.
You should usually compare the error rate of a classifier to two important
references. The first is the error rate if you assign classes to examples uniformly at
random, which for a two class classifier is 50%. A two class classifier should never
have an error rate higher than 50%. If you have one that does, all you need to do
is swap its class assignment, and the resulting error rate would be lower than 50%.
The second is the error rate if you assign all data to the most common class. If one
class is uncommon and the other is common, this error rate can be hard to beat.
Data where some classes occur very seldom requires careful, and quite specialized,
handling.
2.1.5 The Class Confusion Matrix
Evaluating a multi-class classifier is more complex than evaluating a binary classifier. The error rate if you assign classes to examples uniformly at random can
be rather high. If each class has about the same frequency, then this error rate is
(1 100/number of classes)%. A multi-class classifier can make many more kinds
of mistake than a binary classifier can. It is useful to know the total error rate of
the classifier (percentage of classification attempts that produce the wrong answer)
or the accuracy, (the percentage of classification attempts that produce the right
answer). If the error rate is low enough, or the accuracy is high enough, theres not
much to worry about. But if its not, you can look at the class confusion matrix
to see whats going on.
True
True
True
True
True
0
1
2
3
4
Predict
0
151
32
10
6
2
Predict
1
7
5
9
13
3
Predict
2
2
9
7
9
2
Predict
3
3
9
9
5
6
Predict
4
1
0
1
2
0
Class
error
7.9%
91%
81%
86%
100%
TABLE 2.1: The class confusion matrix for a multiclass classifier. Further details
about the dataset and this example appear in worked example 2.3.
Table 2.1 gives an example. This is a class confusion matrix from a classifier
built on a dataset where one tries to predict the degree of heart disease from a collection of physiological and physical measurements. There are five classes (0 . . . 4).
The i, jth cell of the table shows the number of data points of true class i that
were classified to have class j. As I find it hard to recall whether rows or columns
represent true or predicted classes, I have marked this on the table. For each row,
there is a class error rate, which is the percentage of data points of that class that
Section 2.1
17
were misclassified. The first thing to look at in a table like this is the diagonal; if
the largest values appear there, then the classifier is working well. This clearly isnt
what is happening for table 2.1. Instead, you can see that the method is very good
at telling whether a data point is in class 0 or not (the class error rate is rather
small), but cannot distinguish between the other classes. This is a strong hint that
the data cant be used to draw the distinctions that we want. It might be a lot
better to work with a different set of classes.
2.1.6 Statistical Learning Theory and Generalization
What is required in a classifier is an ability to predictwe should like to be confident
that the classifier chosen on a particular data set has a low risk on future data items.
The family of decision boundaries from which a classifier is chosen is an important
component of the problem. Some decision boundaries are more flexible than others
(in a sense we dont intend to make precise). This has nothing to do with the
number of parameters in the decision boundary. For example, if we were to use
a point to separate points on the line, there are very small sets of points that
are not linearly separable (the smallest set has three points in it). This means
that relatively few sets of points on the line are linearly separable, so that if our
dataset is sufficiently large and linearly separable, the resulting classifier is likely
to behave well in future. However, using the sign of sin x to separate points on
the line produces a completely different qualitative phenomenon; for any labeling
of distinct points on the line into two classes, we can choose a value of to achieve
this labeling. This flexibility means that the classifier is wholly unreliableit can
be made to fit any set of examples, meaning the fact that it fits the examples is
uninformative.
There is a body of theory that treats this question, which rests on two important points.
A large enough dataset yields a good representation of the source
of the data: this means that if the dataset used to train the classifier is very
large, there is a reasonable prospect that the performance on the training set
will represent the future performance. However, for this to be helpful, we
need the data set to be large with respect to the flexibility of the family
The flexibility of a family of decision boundaries can be formalized: yielding the Vapnik-Chervonenkis dimension (or V-C dimension) of the family. This dimension is independent of the number of parameters of the family. Families with finite V-C dimension can yield classifiers
whose future performance can be bounded using the number of training elements; families with infinite V-C dimension (like the sin x example above)
cannot be used to produce reliable classifiers.
The essence of the theory is as follows: if one chooses a decision boundary from an
inflexible family, and the resulting classifier performs well on a large data set, there
is strong reason to believe that it will perform well on future items drawn from the
same source. This statement can be expressed precisely in terms of bounds on total
risk to be expected for particular classifiers as a function of the size of the data
set used to train the classifier. These bounds hold in probability. These bounds
Section 2.2
18
p(x|y)p(y)
p(x)
(again, this isnt usually the case; it just turns out to be fruitful to assume that it
is true). This assumption means that
p(y|x)
=
=
p(x|y)p(y)
p(x)
Q
i p(xi |y)p(y)
p(x)
Y
p(xi |y)p(y).
i
Now because we need only to know the posterior values up to scale at x to make
a decision (check the rules above if youre unsure), we dont need to estimate p(x).
In the case of 0-1 loss, this yields the rule
Q
choose y such that i p(xi |y)p(y) is largest.
Naive bayes is particularly good when there are a large number of features, but there
are some things to be careful about. You cant actually multiply a large number
of probabilities and expect to get an answer that a floating point system thinks is
different from zero. Instead, you should add the log probabilities. A model with
many different features is likely to have many strongly negative log probabilities,
so you should not just add up all the log probabilities then exponentiate, or else
you will find that each class has a posterior probability of zero. Instead, subtract
the largest log from all the others, then exponentiate; you will obtain a vector
proportional to the class probabilities, where the largest element has the value 1.
Section 2.2
19
We still need models for p(xi |y) for each xi . It turns out that simple parametric models work really well here. For example, one could fit a normal distribution
to each xi in turn, for each possible value of y, using the training data. The logic
of the measurements might suggest other distributions, too. If one of the xi s was
a count, we might fit a Poisson distribution. If it was a 0-1 variable, we might fit a
Bernoulli distribution. If it was a numeric variable that took one of several values,
then we might use either a multinomial model.
Many effects cause missing values: measuring equipment might fail; a record
could be damaged; it might be too hard to get information in some cases; survey
respondents might not want to answer a question; and so on. As a result, missing values are quite common in practical datasets. A nice feature of naive bayes
classifiers is that they can handle missing values for particular features rather well.
Dealing with missing data during learning is easy. For example, assume for
some i, we wish to fit p(xi |y) with a normal distribution. We need to estimate
the mean and standard deviation of that normal distribution (which we do with
maximum likelihood, as one should). If not every example has a known value of xi ,
this really doesnt matter; we simply omit the unknown number from the estimate.
Write xi,j for the value of xi for the jth example. To estimate the mean, we form
P
jcases with known values xi,j
number of cases with known values
and so on.
Dealing with missing data during classification
is easy, too. We need to look
P
for the y that produces the largest value of i log p(xi |y). We cant evaluate p(xi |y)
if the value of that feature is missing - but it is missing for each class. We can just
leave that term out of the sum, and proceed. This procedure is fine if data is
missing as a result of noise (meaning that the missing terms are independent of
class). If the missing terms depend on the class, there is much more we could do
for example, we might build a model of the class-conditional density of missing
terms.
Notice that if some values of a discrete feature xi dont appear for some class,
you could end up with a model of p(xi |y) that had zeros for some values. This
almost inevitably leads to serious trouble, because it means your model states you
cannot ever observe that value for a data item of that class. This isnt a safe
property: it is hardly ever the case that not observing something means you cannot
observe it. A simple, but useful, fix is to add one to all small counts.
The usual way to find a model of p(y) is to count the number of training
examples in each class, then divide by the number of classes. If there are some
classes with very little data, then the classifier is likely to work poorly, because you
will have trouble getting reasonable estimates of the parameters for the p(xi |y).
Section 2.2
20
adi
car
con
fad
gla
mas
adi
car
con
fad
gla
mas
2
0
2
0
0
0
0
3
0
0
0
1
0
0
2
0
0
0
0
0
0
0
0
3
0
0
0
1
2
0
0
1
0
0
1
1
which is fairly good. The accuracy is 52%. In the training data, the classes
are nearly balanced and there are six classes, meaning that chance is about
16%. The is 4.34. These numbers, and the class-confusion matrix, will vary
with test-train split. I have not averaged over splits, which would be the next
thing.
Section 2.2
21
Build a naive bayes classifier to classify the mouse protein dataset from the
UC Irvine machine learning repository. The dataset is at http://archive.ics.uci.
edu/ml/datasets/Mice+Protein+Expression.
Solution: Theres only one significant difficulty here; many of the data items
are incomplete. I dropped all incomplete data items, which is about half of the
dataset. One can do somewhat more sophisticated things, but we dont have the
tools yet. I used the R package caret to do train-test splits, cross-validation,
etc. on the naive bayes classifier in the R package klaR. I separated out a test
set, then trained with cross-validation on the remainder. The class-confusion
matrix on the test set was:
Predn
c-CS-m
c-CS-s
c-SC-m
c-SC-s
t-CS-m
t-CS-s
t-SC-m
t-SC-s
c-CS-m
9
0
0
0
0
0
0
0
c-CS-s
0
15
0
0
0
0
0
0
c-SC-m
0
0
12
0
0
0
0
0
c-SC-s
0
0
0
15
0
0
0
0
t-CS-m
0
0
0
0
18
0
0
0
t-CS-s
0
0
0
0
0
15
0
0
t-SC-m
0
0
0
0
0
0
12
0
t-SC-s
0
0
0
0
0
0
0
14
which is as accurate as you can get. Again, I have not averaged over splits,
which would be the next thing.
Naive bayes with normal class-conditional distributions takes an interesting
and suggestive form. Assume we have two classes. Recall our decision rule is
+ if L(+ )p(+|x) > L( +)p(|x)
say
otherwise
Now as p gets larger, so does log p (logarithm is a monotonically increasing function), and the rule isnt affected by adding the same constant to both sides, so we
can rewrite as:
+ if log L(+ ) + log p(x|+) + log p(+) > log L( +) log p(x|) + log p()
say
otherwise
+
Write +
j , j respectively for the mean and standard deviation for the classconditional density for the jth component of x for class + (and so on); the comP (xj + )2 P
parison becomes log L(+ ) j 2(+j)2 j log j+ + log p(+) > log L(
j
P (xj
P
)2
j
+) j 2( )2 j log j + log p() Now we can expand and collect terms
j
cj x2j dj xj e > 0
j
Section 2.3
22
(where cj , dj , e are functions of the means and standard deviations and losses and
priors). Rather than forming these by estimating the means, etc., we could directly
search for good values of cj , dj and e.
2.3 THE SUPPORT VECTOR MACHINE
Assume we have a set of N example points xi that belong to two classes, which we
indicate by 1 and 1. These points come with their class labels, which we write as
yi ; thus, our dataset can be written as
{(x1 , y1 ), . . . , (xN , yN )} .
We wish to predict the sign of y for any point x. We will use a linear classifier, so
that for a new data item x, we will predict
sign ((a x + b))
and the particular classifier we use is given by our choice of a and b.
You should think of a and b as representing a hyperplane, given by the points
where a x + b = 0. This hyperplane separates the positive data from the negative
data, and is known as the decision boundary. Notice that the magnitude of
a x + b grows as the point x moves further away from the hyperplane.
Example: 2.1 A linear model with a single feature
Assume we use a linear model with one feature. Then the model has
(p)
the form yi = sign(axi + b). For any particular example which has
the feature value x , this means we will test whether x is larger than,
or smaller than, b/a.
Section 2.3
23
classifiers have a long history of working very well in practice on real data. Third,
linear classifiers are fast to evaluate.
In fact, examples that are classified badly by the linear rule usually are classified badly because there are two few features. Remember the case of the alien
who classified humans into male and female by looking at their heights; if that alien
had looked at their chromosomes as well, the error rate would be extremely small.
In practical examples, experience shows that the error rate of a poorly performing
linear classifier can usually be improved by adding features to the vector x.
Recall that using naive bayes with
for the class conditional
P a normal model
2
distributions boiled down to testing
c
x
d
x
j j e > 0 for some values of
j j j
cj , dj , and e. This may not look to you like a linear classifier, but it is. Imagine
that, for an example ui , you form the feature vector
x = u2i,1 , ui,1 , u2i,2 , ui,2 , . . . , ui,d
T
Then we can interpret testing aT x + b > 0 as testing a1 u2i,1 (a2 )ui,1 + a3 u2i,2
(a4 )ui,2 + ... (b) > 0, and pattern matching to the expression for naive bayes
suggests that the two cases are equivalent (i.e. for any choice of a, b, there is a
corresponding naive bayes case and vice versa; exercises).
2.3.1 Choosing a Classifier with the Hinge Loss
We will choose a and b by choosing values that minimize a cost function. We will
adopt a cost function of the form:
Training error cost + penalty term.
For the moment, we will ignore the penalty term and focus on the training error
cost. Write
i = aT xi + b
for the value that the linear function takes on example i. Write C(i , yi ) for a
function that compares i with yi . The training error cost will be of the form
(1/N )
N
X
C(i , yi ).
i=1
Section 2.3
24
5
4
Loss
3
2
1
0
4
FIGURE 2.1: The hinge loss, plotted for the case yi = 1. The horizontal variable is
the i = a xi + b of the text. Notice that giving a strong negative response to this
positive example causes a loss that grows linearly as the magnitude of the response
grows. Notice also that giving an insufficiently positive response also causes a loss.
Giving a strongly positive response is free.
signs and i has large magnitude, then C can be zero because xi is on the right
side of the decision boundary and so are all the points near to xi .
The choice
C(yi , i ) = max(0, 1 yi i )
has these properties. If yi i > 1 (so the classifier predicts the sign correctly and
xi is far from the boundary) there is no cost. But in any other case, there is a
cost. The cost rises if xi moves toward the decision boundary from the correct side,
and grows linearly as xi moves further away from the boundary on the wrong side
(Figure 2.1). This means that minimizing the loss will encourage the classifier to (a)
make strong positive (or negative) predictions for positive (or negative) examples
and (b) for examples it gets wrong, make the most positive (negative) prediction
that it can. This choice is known as the hinge loss.
Now we think about future examples. We dont know what their feature
values will be, and we dont know their labels. But we do know that an example
Section 2.3
25
with feature vector x will be classified with the rule sign (()a x + b). If we classify
this example wrongly, we should like | a x + b | to be small. Achieving this would
mean that at least some nearby examples will have the right sign. The way to
achieve this is to ensure that || a || is small. By this argument, we would like to
achieve a small value of the hinge loss using a small value of || a ||. Thus, we add a
penalty term to the loss so that pairs (a, b) that have small values of the hinge loss
and large values of || a || are expensive. We minimize
#
"
N
X
T
a a
max(0, 1 yi (a xi + b))
+
S(a, b; ) = (1/N )
2
i=1
(hinge loss)
(penalty)
where is some weight that balances the importance of a small hinge loss against
the importance of a small || a ||. There are now two problems to solve. First, assume
we know ; we will need to find a and b that minimize S(a, b; ). Second, we will
need to estimate .
2.3.2 Finding a Minimum: General Points
I will first summarize general recipes for finding a minimum. Write u = [a, b] for the
vector obtained by stacking the vector a together with b. We have a function g(u),
and we wish to obtain a value of u that achieves the minimum for that function.
Sometimes we can solve this problem in closed form by constructing the gradient
and finding a value of u the makes the gradient zero. This happens mainly for
specially chosen problems that occur in textbooks. For practical problems, we tend
to need a numerical method.
Typical methods take a point u(i) , update it to u(i+1) , then check to see
whether the result is a minimum. This process is started from a start point. The
choice of start point may or may not matter for general problems, but for our
problem it wont matter. The update is usually obtained by computing a direction
p(i) such that for small values of h, g(u(i) + hp(i) ) is smaller than g(u(i) ). Such a
direction is known as a descent direction. We must then determine how far to
go along the descent direction, a process known as line search.
One method to choose a descent direction is gradient descent, which uses
the negative gradient of the function. Recall our notation that
u1
u2
u=
...
ud
and that
g
u1
g
u2
g =
...
g
ud
Section 2.3
26
We can write a Taylor series expansion for the function g(u(i) + hp(i) ). We have
that
g(u(i) + hp(i) ) = g(u(i) ) + h(g)T p(i) + O(h2 )
This means that we can expect that if
p(i) = g(u(i) ),
we expect that, at least for small values of h, g(u(i) +hp(i) ) will be less than g(u(i) ).
This works (as long as g is differentiable, and quite often when it isnt) because g
must go down for at least small steps in this direction.
2.3.3 Finding a Minimum: Stochastic Gradient Descent
Assume we wish to minimize some function g(u) = g0 (u) + (1/N )
function of u. Gradient descent would require us to form
!
N
X
gi (u)
g(u) = g0 (u) + (1/N )
PN
i=1 gi (u),
as a
i=1
and then take a small step in this direction. But if N is large, this is unattractive,
as we might have to sum a lot of terms. This happens a lot in building classifiers,
where you might quite reasonably expect to deal with millions of examples. For
some cases, there might be trillions of examples. Touching each example at each
step really is impractical.
Instead, assume that, at each step, we choose a number k in the range 1 . . . N
uniformly and at random, and form
pk = (g0 (u) + gk (u))
and then take a small step along pk . Our new point becomes
(i)
a(i+1) = a(i) + pk ,
where is called the steplength (even though it very often isnt the length of the
step we take!). It is easy to show that
E[pk ] = g(u)
(where the expectation is over the random choice of k). This implies that if we take
many small steps along pk , they should average out to a step backwards along the
gradient. This approach is known as stochastic gradient descent (because were
not going along the gradient, but along a random vector which is the gradient only
in expectation). It isnt obvious that stochastic gradient descent is a good idea.
Although each step is easy to take, we may need to take more steps. The question
is then whether we gain in the increased speed of the step what we lose by having
to take more steps. Not much is known theoretically, but in practice the approach
is hugely successful for training classifiers.
Choosing a steplength takes some work. We cant search for the step that
gives us the best value of g, because we dont want to evaluate the function g (doing
Section 2.3
27
Section 2.3
28
2.3.4 Example: Training a Support Vector Machine with Stochastic Gradient Descent
I have summarized stochastic gradient descent in algorithm 2.1, but here is an
example in more detail. We need to choose a and b to minimize
C(a, b) = (1/N )
N
X
i=1
max(0, 1 yi (a xi + b)) +
T
a a.
2
This is a support vector machine, because it uses hinge loss. For a support vector
machine, stochastic gradient descent is particularly easy. We have estimates a(n)
and b(n) of the classifier parameters, and we want to improve the estimates. We
pick the kth example at random. We must now compute
T
max(0, 1 yk (a xk + b)) + a a .
2
Assume that yk (a xk + b) > 1. In this case, the classifier predicts a score with
the right sign, and a magnitude that is greater than one. Then the first term is
zero, and the gradient of the second term is easy. Now if yk (a xk + b) < 1, we can
ignore the max, and the first term is 1 yk (a xk + b); the gradient is again easy.
But what if yk (a xk + b) = 1? there are two distinct values we could choose for
the gradient, because the max term isnt differentiable. It turns out not to matter
which term we choose (Figure ??), so we can write the gradient as
a
if yk (a xk + b) 1
pk =
a yk x
otherwise
yk
We choose a steplength , and update our estimates using this gradient. This yields:
a
if yk (a xk + b) 1
(n+1)
(n)
a
=a
a yk x otherwise
and
b(n+1) = b(n)
0
yk
if yk (a xk + b) 1
.
otherwise
Section 2.3
1e7
1e5
1e3
1e1
1
Size of w
5
4
3
2
0.8
0.6
0.4
0.2
1
0
0
29
50
Epoch
100
0
0
20
40
60
Epoch
80
100
FIGURE 2.2: On the left, the magnitude of the weight vector a at the end of each
epoch for the first training regime described in the text. On the right, the accuracy on held out data at the end of each epoch. Notice how different choices of
regularization parameter lead to different magnitudes of a; how the method isnt
particularly sensitive to choice of regularization parameter (they change by factors
of 100); how the accuracy settles down fairly quickly; and how overlarge values of
the regularization parameter do lead to a loss of accuracy.
In the first training regime, there were 100 epochs. In each epoch, I applied
426 steps. For each step, I selected one data item uniformly at random (sampling
with replacement), then stepped down the gradient. This means the method sees
a total of 42, 600 data items. This means that there is a high probability it has
touched each data item once (27, 000 isnt enough, because we are sampling with
replacement, so some items get seen more than once). I chose 5 different values
for the regularization parameter and trained with a steplength of 1/(0.01 e + 50),
where e is the epoch. At the end of each epoch, I computed aT a and the accuracy
(fraction of examples correctly classified) of the current classifier on the held out
test examples. Figure 2.2 shows the results. You should notice that the accuracy
changes slightly each epoch; that for larger regularizer values aT a is smaller; and
that the accuracy settles down to about 0.8 very quickly.
In the second training regime, there were 100 epochs. In each epoch, I applied
50 steps. For each step, I selected one data item uniformly at random (sampling
with replacement), then stepped down the gradient. This means the method sees
a total of 5,000 data items, and about 3, 216 unique data items it hasnt seen
the whole training set. I chose 5 different values for the regularization parameter
and trained with a steplength of 1/(0.01 e + 50), where e is the epoch. At the end
of each epoch, I computed aT a and the accuracy (fraction of examples correctly
classified) of the current classifier on the held out test examples. Figure 2.3 shows
the results. You should notice that the accuracy changes slightly each epoch; that
for larger regularizer values aT a is smaller; and that the accuracy settles down
to about 0.8 very quickly; and that there isnt much difference between the two
training regimes. All of these points are relatively typical of stochastic gradient
Section 2.3
1e7
1e5
1e3
1e1
1
Size of w
5
4
3
2
30
1
0.8
Held out error
0.6
0.4
0.2
1
0
0
50
Epoch
100
0
0
20
40
60
Epoch
80
100
FIGURE 2.3: On the left, the magnitude of the weight vector a at the end of each
epoch for the second training regime described in the text. On the right, the accuracy on held out data at the end of each epoch. Notice how different choices of
regularization parameter lead to different magnitudes of a; how the method isnt
particularly sensitive to choice of regularization parameter (they change by factors
of 100); how the accuracy settles down fairly quickly; and how overlarge values of
the regularization parameter do lead to a loss of accuracy.
Remember this:
Linear SVMs are a go-to classifier. When you have
a binary classification problem, the first step should be to try a linear SVM.
There is an immense quantity of good software available.
Section 2.4
31
with the largest classifier score. One can think up quite good reasons this approach
shouldnt work. For one thing, the classifier isnt told that you intend to use the
score to tell similarity between classes. In practice, the approach works rather well
and is quite widely used. This approach scales a bit better with the number of
classes (O(N )).
Remember this:
It is straightforward to build a multi-class classifier
out of binary classifiers. Any decent SVM package will do this for you.
Section 2.4
32
moves
bites
big
furry
cat
toddler
dog
chair leg
cardboard
sofa
box
FIGURE 2.4: This the household robots guide to obstacles is a typical decision
tree. I have labelled only one of the outgoing branches, because the other is the
negation. So if the obstacle moves, bites, but isnt furry, then its a toddler. In
general, an item is passed down the tree until it hits a leaf. It is then labelled with
the leaf s label.
use a binary tree, because its easier to describe and because thats usual (it doesnt
change anything important, though). Each node has a decision function, which
takes data items and returns either 1 or -1.
We train the tree by thinking about its effect on the training data. We pass
the whole pool of training data into the root. Any node splits its incoming data
into two pools, left (all the data that the decision function labels 1) and right (ditto,
-1). Finally, each leaf contains a pool of data, which it cant split because it is a
leaf.
Training the tree uses a straightforward algorithm. First, we choose a class of
decision functions to use at each node. It turns out that a very effective algorithm
is to choose a single feature at random, then test whether its value is larger than, or
smaller than a threshold. For this approach to work, one needs to be quite careful
about the choice of threshold, which is what we describe in the next section. Some
minor adjustments, described below, are required if the feature chosen isnt ordinal.
Surprisingly, being clever about the choice of feature doesnt seem add a great deal
of value. We wont spend more time on other kinds of decision function, though
there are lots.
Now assume we use a decision function as described, and we know how to
choose a threshold. We start with the root node, then recursively either split the
pool of data at that node, passing the left pool left and the right pool right, or stop
splitting and return. Splitting involves choosing a decision function from the class
to give the best split for a leaf. The main questions are how to choose the best
split (next section), and when to stop.
Stopping is relatively straightforward. Quite simple strategies for stopping
are very good. It is hard to choose a decision function with very little data, so we
must stop splitting when there is too little data at a node. We can tell this is the
Section 2.4
33
5
y>.32
x>-0.58
+
x>1.06
5
5
FIGURE 2.5: A straightforward decision tree, illustrated in two ways. On the left,
I have given the rules at each split; on the right, I have shown the data points in
two dimensions, and the structure that the tree produces in the feature space.
case by testing the amount of data against a threshold, chosen by experiment. If all
the data at a node belongs to a single class, there is no point in splitting. Finally,
constructing a tree that is too deep tends to result in generalization problems, so
we usually allow no more than a fixed depth D of splits. Choosing the best splitting
threshold is more complicated.
Figure 2.6 shows two possible splits of a pool of training data. One is quite
obviously a lot better than the other. In the good case, the split separates the pool
into positives and negatives. In the bad case, each side of the split has the same
number of positives and negatives. We cannot usually produce splits as good as
the good case here. What we are looking for is a split that will make the proper
label more certain.
Figure 2.7 shows a more subtle case to illustrate this. The splits in this figure
are obtained by testing the horizontal feature against a threshold. In one case,
the left and the right pools contain about the same fraction of positive (x) and
negative (o) examples. In the other, the left pool is all positive, and the right pool
is mostly negative. This is the better choice of threshold. If we were to label any
item on the left side positive and any item on the right side negative, the error rate
would be fairly small. If you count, the best error rate for the informative split is
20% on the training data, and for the uninformative split it is 40% on the training
data.
But we need some way to score the splits, so we can tell which threshold is
best. Notice that, in the uninformative case, knowing that a data item is on the
left (or the right) does not tell me much more about the data than I already knew.
We have that p(1|left pool, uninformative) = 2/3 3/5 = p(1|parent pool) and
p(1|right pool, uninformative) = 1/2 3/5 = p(1|parent pool). For the informative pool, knowing a data item is on the left classifies it completely, and knowing
that it is on the right allows us to classify it an error rate of 1/3. The informative
34
x
x
o
o
Informative split
x
x x
x x
o
o
o
o
o
o
o
o
o
o
o
Section 2.4
FIGURE 2.6: Two possible splits of a pool of training data. Positive data is repre-
sented with an x, negative data with a o. Notice that if we split this pool with
the informative line, all the points on the left are os, and all the points on the
right are xs. This is an excellent choice of split once we have arrived in a leaf,
everything has the same label. Compare this with the less informative split. We
started with a node that was half x and half o, and now have two nodes each of
which is half x and half o this isnt an improvement, because we do not know
more about the label as a result of the split.
split means that my uncertainty about what class the data item belongs to is significantly reduced if I know whether it goes left or right. To choose a good threshold,
we need to keep track of how informative the split is.
2.4.2 Entropy and Information Gain
It turns out to be straightforward to keep track of information, in simple cases. We
will start with an example. Assume I have 4 classes. There are 8 examples in class
1, 4 in class 2, 2 in class 3, and 2 in class 4. How much information on average will
you need to send me to tell me the class of a given example? Clearly, this depends
on how you communicate the information. You could send me the complete works
of Edward Gibbon to communicate class 1; the Encyclopaedia for class 2; and so
on. But this would be redundant. The question is how little can you send me.
Keeping track of the amount of information is easier if we encode it with bits (i.e.
you can send me sequences of 0s and 1s).
Imagine the following scheme. If an example is in class 1, you send me a 1.
If it is in class 2, you send me 01; if it is in class 3, you send me 001; and in class
4, you send me 101. Then the expected number of bits you will send me is
p(class = 1)1 + p(2)2 + p(3)3 + p(4)3 =
1
1
1
1
1+ 2+ 3+ 3
2
4
8
8
which is 1.75 bits. This number doesnt have to be an integer, because its an
expectation.
Notice that for the ith class, you have sent me log2 p(i) bits. We can write
o
o
o
x
x
x x
x
x
x x
x x
o
o
o
o
o
x
x
x
x
x x
x
x x
o
o
x
x
x x
o
o
x
x
x
x x
x x
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
x
x
x
x x
x
x x
o
o
o
o
o
o
o
o
o
x
x
x
x x
x
x x
35
Section 2.4
Informative split
FIGURE 2.7: Two possible splits of a pool of training data. Positive data is repre-
sented with an x, negative data with a o. Notice that if we split this pool with
the informative line, all the points on the left are xs, and two-thirds of the points
on the right are os. This means that knowing which side of the split a point lies
would give us a good basis for estimating the label. In the less informative case,
about two-thirds of the points on the left are xs and about half on the right are xs
knowing which side of the split a point lies is much less useful in deciding what
the label is.
the expected number of bits you need to send me as
X
p(i) log2 p(i).
This expression handles other simple cases correctly, too. You should notice that it
isnt really important how many objects appear in each class. Instead, the fraction
of all examples that appear in the class is what matters. This fraction is the prior
probability that an item will belong to the class. You should try what happens if
you have two classes, with an even number of examples in each; 256 classes, with
an even number of examples in each; and 5 classes, with p(1) = 1/2, p(2) = 1/4,
p(3) = 1/8, p(4) = 1/16 and p(5) = 1/16. If you try other examples, you may find
it hard to construct a scheme where you can send as few bits on average as this
expression predicts. It turns out that, in general, the smallest number of bits you
will need to send me is given by the expression
X
p(i) log2 p(i)
under all conditions, though it may be hard or impossible to determine what representation is required to achieve this number.
The entropy of a probability distribution is a number that scores how many
bits, on average, would need to be known to identify an item sampled from that
probability distribution. For a discrete probability distribution, the entropy is
computed as
X
p(i) log2 p(i)
Section 2.4
36
where i ranges over all the numbers where p(i) is not zero. For example, if we
have two classes and p(1) = 0.99, then the entropy is 0.0808, meaning you need
very little information to tell which class an object belongs to. This makes sense,
because there is a very high probability it belongs to class 1; you need very little
information to tell you when it is in class 2. If you are worried by the prospect of
having to send 0.0808 bits, remember this is an average, so you can interpret the
number as meaning that, if you want to tell which class each of 104 independent
objects belong to, you could do so in principle with only 808 bits.
Generally, the entropy is larger if the class of an item is more uncertain.
Imagine we have two classes and p(1) = 0.5, then the entropy is 1, and this is the
largest possible value for a probability distribution on two classes. You can always
tell which of two classes an object belongs to with just one bit (though you might
be able to tell with even less than one bit).
2.4.3 Entropy and Splits
Now we return to the splits. Write P for the set of all data at the node. Write Pl
for the left pool, and Pr for the right pool. The entropy of a pool C that scores
how many bits would be required to represent the class of an item in that pool, on
average. Write n(i; C) for the number of items of class i in the pool, and N (C) for
the number of items in the pool. Then the entropy is H(C) of the pool C is
X n(i; C)
n(i; C)
log2
.
N (C)
N (C
i
It is straightforward that H(P) bits are required to classify an item in the parent
pool P. For an item in the left pool, we need H(Pl ) bits; for an item in the right
pool, we need H(Pr ) bits. If we split the parent pool, we expect to encounter items
in the left pool with probability
N (Pl )
N (P)
and items in the right pool with probability
N (Pr )
.
N (P)
This means that, on average, we must supply
N (Pl )
N (Pr )
H(Pl ) +
H(Pr )
N (P)
N (P)
bits to classify data items if we split the parent pool. Now a good split is one that
results in left and right pools that are informative. In turn, we should need fewer
bits to classify once we have split than we need before the split. You can see the
difference
N (Pr )
N (Pl )
H(Pl ) +
H(Pr )
I(Pl , Pr ; P) = H(P)
N (P)
N (P)
as the information gain caused by the split. This is the average number of bits
that you dont have to supply if you know which side of the split an example lies.
Better splits have larger information gain.
x
x
x
x
o
o
o
o
o
o
o
o
o
o
o
o
37
Section 2.4
FIGURE 2.8: We search for a good splitting threshold by looking at values of the
chosen component that yield different splits. On the left, I show a small dataset
and its projection onto the chosen splitting component (the horizontal axis). For the
8 data points here, there are only 7 threshold values that produce interesting splits,
and these are shown as ts on the axis. On the right, I show a larger dataset; in
this case, I have projected only a subset of the data, which results in a small set of
thresholds to search.
Section 2.4
38
is as follows. We can split such a feature into two pools by flipping an unbiased
coin for each value if the coin comes up H, any data point with that value goes
left, and if it comes up T , any data point with that value goes right. We chose this
split at random, so it might not be any good. We can come up with a good split by
repeating this procedure F times, computing the information gain for each split,
then keeping the one that has the best information gain. We choose F in advance,
and it usually depends on the number of values the categorical variable can take.
We now have a relatively straightforward blueprint for an algorithm, which I
have put in a box. Its a blueprint, because there are a variety of ways in which it
can be revised and changed.
Section 2.4
39
2.4.5 Forests
A single decision tree tends to yield poor classifications. One reason is because the
tree is not chosen to give the best classification of its training data. We used a
random selection of splitting variables at each node, so the tree cant be the best
possible. Obtaining the best possible tree presents significant technical difficulties.
It turns out that the tree that gives the best possible results on the training data
can perform rather poorly on test data. The training data is a small subset of
possible examples, and so must differ from the test data. The best possible tree on
the training data might have a large number of small leaves, built using carefully
chosen splits. But the choices that are best for training data might not be best for
test data.
Rather than build the best possible tree, we have built a tree efficiently, but
with number of random choices. If we were to rebuild the tree, we would obtain
a different result. This suggests the following extremely effective strategy: build
many trees, and classify by merging their results.
2.4.6 Building and Evaluating a Decision Forest
There are two important strategies for building and evaluating decision forests. I
am not aware of evidence strongly favoring one over the other, but different software
packages use different strategies, and you should be aware of the options. In one
strategy, we separate labelled data into a training and a test set. We then build
multiple decision trees, training each using the whole training set. Finally, we
evaluate the forest on the test set. In this approach, the forest has not seen some
fraction of the available labelled data, because we used it to test. However, each
tree has seen every training data item.
In the other strategy, sometimes called bagging, each time we train a tree we
randomly subsample the labelled data with replacement, to yield a training set the
same size as the original set of labelled data. Notice that there will be duplicates
in this training set, which is like a bootstrap replicate. This training set is often
called a bag. We keep a record of the examples that do not appear in the bag (the
out of bag examples). Now to evaluate the forest, we evaluate each tree on its
out of bag examples, and average these error terms. In this approach, the entire
forest has seen all labelled data, and we also get an estimate of error, but no tree
has seen all the training data.
Section 2.4
40
An alternative strategy that takes this observation into account is to pass the
test data item down each tree. When it arrives at a leaf, we record one vote for each
of the training data items in that leaf. The vote goes to the class of the training
data item. Finally, we take the class with the most votes. This approach allows
big, accurate leaves to dominate the voting process. Both strategies are in use, and
I am not aware of compelling evidence that one is always better than the other.
This may be because the randomness in the training process makes big, accurate
leaves uncommon in practice.
Section 2.4
41
Build a random forest classifier to classify the heart dataset from the UC
Irvine machine learning repository. The dataset is at http://archive.ics.uci.edu/
ml/datasets/Heart+Disease. There are several versions. You should look at the
processed Cleveland data, which is in the file processed.cleveland.data.txt.
Solution: I used the R random forest package. This uses a bagging strategy.
There is sample code in listing ??. This package makes it quite simple to fit
a random forest, as you can see. In this dataset, variable 14 (V14) takes the
value 0, 1, 2, 3 or 4 depending on the severity of the narrowing of the arteries.
Other variables are physiological and physical measurements pertaining to the
patient (read the details on the website). I tried to predict all five levels of
variable 14, using the random forest as a multivariate classifier. This works
rather poorly, as the out-of-bag class confusion matrix below shows. The total
out-of-bag error rate was 45%.
Predict Predict Predict Predict Predict Class
0
1
2
3
4
error
True 0
151
7
2
3
1
7.9%
True 1
32
5
9
9
0
91%
True 2
10
9
7
9
1
81%
True 3
6
13
9
5
2
86%
True 4
2
3
2
6
0
100%
This is the example of a class confusion matrix from table 2.1. Fairly clearly,
one can predict narrowing or no narrowing from the features, but not the
degree of narrowing (at least, not with a random forest). So it is natural to
quantize variable 14 to two levels, 0 (meaning no narrowing), and 1 (meaning
any narrowing, so the original value could have been 1, 2, or 3). I then built
a random forest to predict this from the other variables. The total out-of-bag
error rate was 19%, and I obtained the following out-of-bag class confusion
matrix
Predict Predict Class
0
1
error
True 0
138
26
16%
True 1
31
108
22%
Notice that the false positive rate (16%, from 26/164) is rather better than the
false negative rate (22%). Looking at these class confusion matrices, you might
wonder whether it is better to predict 0, . . . , 4, then quantize. But this is not a
particularly good idea. While the false positive rate is 7.9%, the false negative
rate is much higher (36%, from 50/139). In this application, a false negative is
likely more of a problem than a false positive, so the tradeoff is unattractive.
Section 2.5
42
Listing 2.1: R code used for the random forests of worked example 2.3
setwd ( / u s e r s / d a f / C u r r e n t/ c o u r s e s / P r o b c o u r s e / T r e e s /RCode ) ;
i n s t a l l . packages ( randomForest )
l i b r a r y ( randomForest )
h e a r t<read . csv ( p r o c e s s e d . c l e v e l a n d . data . t x t , h e a d e r=FALSE)
h e a r t $ l e v e l s<as . f a c t o r ( h e a r t $V14 )
h e a r t f o r e s t . a l l v a l s<
randomForest ( formula=l e v e l s V1+V2+V3+V4+V5+V6
+V7+V8+V9+V10+V11+V12+V13 ,
data=h e a r t , ty p e= c l a s s i f i c a t i o n , mtry=5)
# t h i s f i t s to a l l l e v e l s
# I g o t t h e CCM by t y p i n g
heartforest . allvals
h e a r t $ y e s n o<cut ( h e a r t $V14 , c( I n f , 0 . 1 , I n f ) )
h e a r t f o r e s t<
randomForest ( formula=y e s n o V1+V2+V3+V4+V5+V6
+V7+V8+V9+V10+V11+V12+V13 ,
data=h e a r t , ty p e= c l a s s i f i c a t i o n , mtry=5)
# t h i s f i t s to the quantized case
# I g o t t h e CCM by t y p i n g
heartforest
Section 2.5
43
point. If there are enough training examples, then the closest point should be inside
the same cell as the query point.
You may be worried that, if the query point is close to a decision boundary,
the closest point might be on the other side of that boundary. But if it were,
we could improve things by simply having more training points. All this suggests
that, with enough training points, our classifier should work about as well as the
best possible classifier. This intuition turns out to be correct, though the number
of training points required is wholly impractical, particularly for high-dimensional
feature vectors.
One important generalization is to find the k nearest neighbors, then choose
a label from those. A (k, l) nearest neighbor classifier finds the k example points
closest to the point being considered, and classifies this point with the class that has
the highest number of votes, as long as this class has more than l votes (otherwise,
the point is classified as unknown). A (k, 0)-nearest neighbor classifier is usually
known as a k-nearest neighbor classifier, and a (1, 0)-nearest neighbor classifier
is usually known as a nearest neighbor classifier. In practice, one seldom uses
more than three nearest neighbors. Finding the k nearest points for a particular
query can be difficult, and Section ?? reviews this point.
There are three practical difficulties in building nearest neighbor classifiers.
You need a lot of labelled examples. You need to be able to find the nearest
neighbors for your query point. And you need to use a sensible choice of distance.
For features that are obviously of the same type, such as lengths, the usual metric
may be good enough. But what if one feature is a length, one is a color, and one is
an angle? One possibility is to whiten the features (section 3.1). This may be hard
if the dimension is so large that the covariance matrix is hard to estimate. It is
almost always a good idea to scale each feature independently so that the variance
of each feature is the same, or at least consistent; this prevents features with very
large scales dominating those with very small scales. Notice that nearest neighbors
(fairly obviously) doesnt like categorical data. If you cant give a clear account
of how far apart two things are, you shouldnt be doing nearest neighbors. It is
possible to fudge this point a little, by (say) putting together a distance between
the levels of each factor, but its probably unwise.
Nearest neighbors is wonderfully flexible about the labels the classifier predicts. Nothing changes when you go from a two-class classifier to a multi-class
classifier.
Section 2.5
44
Build a nearest neighbor classifier to classify the digit data originally constructed by Yann Lecun. You can find it at several places. The original dataset
is at http://yann.lecun.com/exdb/mnist/. The version I used was used for a
Kaggle competition (so I didnt have to decompress Lecuns original format).
I found it at http://www.kaggle.com/c/digit-recognizer.
Solution: As youd expect, R has nearest neighbor code that seems quite
good (I havent had any real problems with it, at least). There isnt really all
that much to say about the code. I used the R FNN package. This uses a
bagging strategy. There is sample code in listing ??. I trained on 1000 of the
42000 examples, so you could see how in the code. I tested on the next 200
examples. For this (rather small) case, I found the following class confusion
matrix
P
0
1
2
3
4
5
6
7
8
9
0 12 0
0
0
0
0
0
0
0
0
1 0 20 4
1
0
1
0
2
2
1
2 0
0 20 1
0
0
0
0
0
0
3 0
0
0 12 0
0
0
0
4
0
4 0
0
0
0 18 0
0
0
1
1
5 0
0
0
0
0 19 0
0
1
0
6 1
0
0
0
0
0 18 0
0
0
7 0
0
1
0
0
0
0 19 0
2
8 0
0
1
0
0
0
0
0 16 0
9 0
0
0
2
3
1
0
1
1 14
There are no class error rates here, because I was in a rush and couldnt recall
the magic line of R to get them. However, you can see the classifier works
rather well for this case.
Remember this:
Nearest neighbor classifiers are often very effective.
They can predict any kind of label. You do need to be careful to have enough
data, and to have a meaningful distance function.
Section 2.6
You should
45
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
7
7
7
9
10
10
10
10
10
10
11
12
12
13
13
13
13
17
20
20
20
21
21
25
26
26
26
27
30
31
31
34
34
37
39
Section 2.6
You should
46
PROBLEMS
PROGRAMMING EXERCISES
2.1. The UC Irvine machine learning data repository hosts a famous collection of
data on whether a patient has diabetes (the Pima Indians dataset), originally
owned by the National Institute of Diabetes and Digestive and Kidney Diseases
and donated by Vincent Sigillito. This can be found at http://archive.ics.uci.
edu/ml/datasets/Pima+Indians+Diabetes. This data has a set of attributes of
patients, and a categorical variable telling whether the patient is diabetic or
not. For several attributes in this data set, a value of 0 may indicate a missing
value of the variable.
(a) Build a simple naive Bayes classifier to classify this data set. You should
hold out 20% of the data for evaluation, and use the other 80% for training.
You should use a normal distribution to model each of the class-conditional
distributions. You should write this classifier yourself (its quite straightforward), but you may find the function createDataPartition in the R
package caret helpful to get the random partition.
(b) Now adjust your code so that, for attribute 3 (Diastolic blood pressure),
attribute 4 (Triceps skin fold thickness), attribute 6 (Body mass index),
and attribute 8 (Age), it regards a value of 0 as a missing value when
estimating the class-conditional distributions, and the posterior. R uses
a special number NA to flag a missing value. Most functions handle this
number in special, but sensible, ways; but youll need to do a bit of looking
at manuals to check. Does this affect the accuracy of your classifier?
(c) Now use the caret and klaR packages to build a naive bayes classifier
for this data, assuming that no attribute has a missing value. The caret
package does cross-validation (look at train) and can be used to hold out
data. The klaR package can estimate class-conditional densities using a
density estimation procedure that I will describe much later in the course.
Use the cross-validation mechanisms in caret to estimate the accuracy of
your classifier. I have not been able to persuade the combination of caret
and klaR to handle missing values the way Id like them to, but that may
be ignorance (look at the na.action argument).
(d) Now install SVMLight, which you can find at http://svmlight.joachims.
org, via the interface in klaR (look for svmlight in the manual) to train
and evaluate an SVM to classify this data. You dont need to understand
much about SVMs to do this well do that in following exercises. You
should hold out 20% of the data for evaluation, and use the other 80% for
training. You should NOT substitute NA values for zeros for attributes 3,
4, 6, and 8.
2.2. The UC Irvine machine learning data repository hosts a collection of data
on student performance in Portugal, donated by Paulo Cortez, University of
Minho, in Portugal. You can find this data at https://archive.ics.uci.edu/ml/
datasets/Student+Performance. It is described in P. Cortez and A. Silva. Using
Data Mining to Predict Secondary School Student Performance. In A. Brito
and J. Teixeira Eds., Proceedings of 5th FUture BUsiness TEChnology Conference (FUBUTEC 2008) pp. 5-12, Porto, Portugal, April, 2008, EUROSIS,
ISBN 978-9077381-39-7.
There are two datasets (for grades in mathematics and for grades in Portugese). There are 30 attributes each for 649 students, and 3 values that can
Section 2.6
You should
47
Section 2.6
You should
48
Section 2.6
You should
49
census of mutation sequence spaces: the example of p53 cancer rescue mutants, IEEE/ACM transactions on computational biology and bioinformatics
/ IEEE, ACM, 3, 114-125.
You can find this data at https://archive.ics.uci.edu/ml/datasets/p53+Mutants.
There are a total of 16772 instances, with 5409 attributes per instance. Attribute 5409 is the class attribute, which is either active or inactive. There are
several versions of this dataset. You should use the version K8.data.
(a) Train an SVM to classify this data, using stochastic gradient descent. You
will need to drop data items with missing values. You should estimate
a regularization constant using cross-validation, trying at least 3 values.
Your training method should touch at least 50% of the training set data.
You should produce an estimate of the accuracy of this classifier on held
out data consisting of 10% of the dataset, chosen at random.
(b) Now train a naive bayes classifier to classify this data. You should produce
an estimate of the accuracy of this classifier on held out data consisting
of 10% of the dataset, chosen at random.
(c) Compare your classifiers. Which one is better? why?
2.7. The UC Irvine machine learning data repository hosts a collection of data on
whether a mushroom is edible, donated by Jeff Schlimmer and to be found at
http://archive.ics.uci.edu/ml/datasets/Mushroom. This data has a set of categorical attributes of the mushroom, together with two labels (poisonous or
edible). Use the R random forest package (as in the example in the chapter)
to build a random forest to classify a mushroom as edible or poisonous based
on its attributes.
(a) Produce a class-confusion matrix for this problem. If you eat a mushroom
based on your classifiers prediction it is edible, what is the probability of
being poisoned?
Section 2.6
You should
50
CODE SNIPPETS
Listing 2.2: R code used for the naive bayes example of worked example 2.1
setwd ( / u s e r s / d a f / C u r r e n t/ c o u r s e s / P r o b c o u r s e / C l a s s i f i c a t i o n /RCode/ B r e a s t T i s s u e )
wdat<read . csv ( c l e a n e d b r e a s t . c s v )
l i b r a r y ( klaR )
library ( c a r e t )
b i g x<wdat [ , c ( 1 : 2 ) ]
b i g y<wdat [ , 2 ]
wtd<c r e a t e D a t a P a r t i t i o n ( y=bigy , p = . 8 , l i s t=FALSE)
t r a x<b i g x [ wtd , ]
t r a y<b i g y [ wtd ]
model<t r a i n ( tr a x , tr a y , nb , t r C o n t r o l=t r a i n C o n t r o l ( method= cv , number =10))
t e c l a s s e s<predict ( model , newdata=b i g x [wtd , ] )
c o n f u s i o n M a t r i x ( data=t e c l a s s e s , b i g y [wtd ] )
Listing 2.3: R code used for the naive bayes example of worked example 2.2
setwd ( / u s e r s / d a f / C u r r e n t/ c o u r s e s / P r o b c o u r s e / C l a s s i f i c a t i o n /RCode/ MouseProtein )
wdat<read . csv ( Data Cortex N u c l e a r . c s v )
#i n s t a l l . p a c k a g e s ( klaR )
#i n s t a l l . p a c k a g e s ( c a r e t )
l i b r a r y ( klaR )
library ( c a r e t )
c c i<complete . c a s e s ( wdat )
b i g x<wdat [ c c i ,c ( 8 2 ) ]
b i g y<wdat [ c c i , 8 2 ]
wtd<c r e a t e D a t a P a r t i t i o n ( y=bigy , p = . 8 , l i s t=FALSE)
t r a x<b i g x [ wtd , ]
t r a y<b i g y [ wtd ]
model<t r a i n ( tr a x , tr a y , nb , t r C o n t r o l=t r a i n C o n t r o l ( method= cv , number =10))
t e c l a s s e s<predict ( model , newdata=b i g x [wtd , ] )
c o n f u s i o n M a t r i x ( data=t e c l a s s e s , b i g y [wtd ] )
C H A P T E R
Section 3.1
52
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
BODYFAT
DENSITY AGE WEIGHT HEIGHT
ADIPOSITYNECK
CHESTABDOMEN HIP
THIGH
FIGURE 3.1: A parallel plot of the bodyfat dataset, including all data points. I have
named the components on the horizontal axis. It is easy to see that large values
of bodyfat correspond to small values of density, and vice versa. Notice that one
datapoint has height very different from all others; similarly, one datapoint has
weight very different from all others.
items have ankle values that are very different from the others, for example).
3.1.3 Scatterplot Matrices
One strategy that is very useful when there arent too many dimensions is to use a
scatterplot matrix. To build one, you lay out scatterplots for each pair of variables
in a matrix. On the diagonal, you name the variable that is the vertical axis for
each plot in the row, and the horizontal axis in the column. This sounds more
complicated than it is; look at the example of figure 3.3, which shows a scatterplot
matrix for four of the variables in the height weight dataset of http://www2.stetson.
edu/jrasp/data.htm; look for bodyfat.xls at that URL). This is originally a 16dimensional dataset, but a 16 by 16 scatterplot matrix is squashed and hard to
interpret.
What is nice about this kind of plot is that its quite easy to spot correlations
between pairs of variables, though you do need to take into account the coordinates
have not been normalized. For figure 3.3, you can see that weight and adiposity
Section 3.1
53
0.9
0.8
0.7
0.6
0.5
0.4
0.3
0.2
0.1
0
BODYFAT
DENSITY AGE WEIGHT HEIGHT
ADIPOSITYNECK
CHESTABDOMEN HIP
THIGH
FIGURE 3.2: A plot with those data items removed, so that those components are
renormalized. Two datapoints have rather distinct ankle measurements. Generally,
you can see that large knees go with large ankles and large biceps (the v structure).
appear to show quite strong correlations, but weight and age are pretty weakly
correlated. Height and age seem to have a low correlation. It is also easy to
visualize unusual data points. Usually one has an interactive process to do so
you can move a brush over the plot to change the color of data points under the
brush. To show what might happen, figure 3.4 shows a scatter plot matrix with
some points shown as circles. Notice how they lie inside the blob of data in some
views, and outside in others. This is an effect of projection.
UC Irvine keeps a large repository of datasets that are important in machine
learning. You can find the repository at http://archive.ics.uci.edu/ml/index.html.
Figures 3.5 and 3.6 show visualizations of a famous dataset to do with the botanical
classification of irises.
Figures ??, ?? and 3.9 show visualizations of another dataset to do with forest
fires in Portugal, also from the UC Irvine repository (look at http://archive.ics.uci.
edu/ml/datasets/Forest+Fires). In this dataset, there are a variety of measurements
of location, time, temperature, etc. together with the area burned by a wildfire.
It would be nice to know what leads to large fires, and a visualization is the place
to start. Many fires are tiny (or perhaps there was no area measurement?) and so
Section 3.1
100
100
100
50
50
50
50
400
400
300
300
50
100
54
Age
200
400
400
300
Weight
200
100
200
0
50
100
200
0
50
100
100
80
60
80
80
60
60
40
40
50
100
50
100
50
100
Height
40
20
20
50
200
400
20
60
60
60
40
40
40
20
20
20
Adiposity
50
100
200
400
FIGURE 3.3: This is a scatterplot matrix for four of the variables in the height weight
Section 3.1
100
100
100
50
50
50
50
400
400
300
300
50
100
55
Age
200
400
400
300
Weight
200
100
200
0
50
100
200
0
50
100
100
80
60
80
80
60
60
40
40
50
100
50
100
50
100
Height
40
20
20
50
200
400
20
60
60
60
40
40
40
20
20
20
Adiposity
50
100
200
400
You should compare this figure with figure 3.3. I have marked two
data points with circles in this figure; notice that in some panels these are far from
the rest of the data, in others close by. A brush in an interactive application can
be used to mark data like this to allow a user to explore a dataset.
FIGURE 3.4:
Section 3.1
setosa
versicolor
56
virginica
2.5
2.0
1.5
Petal
Width
0.5
0.0
7
4
6
5
Petal
Length
1.0
4
3
2
1
4.5
3.5 4.0 4.5
4.0
3.5
Sepal
Width
3.0
2.5
2.0
8
7
Sepal
Length
6
5
This is a scatterplot matrix for the famous Iris data, originally due
to ***. There are four variables, measured for each of three species of iris. I have
plotted each species with a different marker. You can see from the plot that the
species cluster quite tightly, and are different from one another. R code for this plot
is on the website.
FIGURE 3.5:
Section 3.1
setosa
versicolor
57
virginica
Petal.Length
Petal.Width
Sepal.Length
FIGURE 3.6:
Section 3.1
T1
T2
T3
T4
T5
T6
58
T7
50
30 40 50
40
30
ISI
20
0 10 20
800
10
0
600
400
DC
400
200
200 400
0
300
250
150
250
200
150
DMC 150
100
0 50 100
60
50
0
80
80
60
FFMC
40
20
40
20
This is a scatterplot matrix for the fire dataset from the UC Irvine
repository. The smallest area fire is T1, and the largest is T7; each is plotted with
a different marker. These plots show severity of the fire, plotted against variables
5-8 of the dataset. You should notice that there isnt much separation between the
markers. It might be very hard to predict the severity of a fire from these variables.
R code for this plot is on the website.
FIGURE 3.7:
Section 3.1
T1
T2
T3
T4
T5
T6
59
T7
6
5
4
3
3 4 5 6
rain
0 1 2 3
4
3
2
1
0
wind
4
0
4
2
0
100
60
80 100
RH
60
80
60
40
20
30
25
20
40
60
20
20 25 30
temp
5 10 15
15
10
5
This is a scatterplot matrix for the fire dataset from the UC Irvine
repository. The smallest area fire is T1, and the largest is T7; each is plotted with
a different marker. These plots show severity of the fire, plotted against variables
9-12 of the dataset. You should notice that there isnt much separation between the
markers. It might be very hard to predict the severity of a fire from these variables.
R code for this plot is on the website.
FIGURE 3.8:
Section 3.1
T1
T2
T3
T4
T5
T6
60
T7
V9
V11
V10
Section 3.2
61
i xi
.
N
This expression is meaningful for vectors, too, because we can add vectors and
divide by scalars. We write
P
xi
mean ({x}) = i
N
and call this the mean of the data. Notice that each component of mean ({x}) is the
mean of that component of the data. There is not an easy analogue of the median,
however (how do you order high dimensional data?) and this is a nuisance. Notice
that, just as for the one-dimensional mean, we have
mean ({x mean ({x})}) = 0
(i.e. if you subtract the mean from a data set, the resulting data set has zero mean).
3.2.2 Using Covariance to encode Variance and Correlation
Variance, standard deviation and correlation can each be seen as an instance of a
more general operation on data. Assume that we have two one dimensional data
sets {x} and {y}. Then we can define the covariance of {x} and {y}.
Section 3.2
62
This is occasionally a useful way to think about correlation. It says that the correlation measures the tendency of {x} and {y} to be larger (resp. smaller) than their
means for the same data points, compared to how much they change on their own.
Working with covariance (rather than correlation) allows us to unify some
ideas. In particular, for data items which are d dimensional vectors, it is straightforward to compute a single matrix that captures all covariances between all pairs
of components this is the covariance matrix.
Section 3.2
63
o
o n
n
x(j) , x(k)
Proof: Recall
Covmat ({x}) =
i (xi
mean
Section 3.2
Proposition:
Covmat ({xi })jj = jj = var
n
x(j)
o
Proof:
Covmat ({x})jj
Proposition:
o
o n
n
x(j) , x(j)
o
n
= var x(j)
= cov
Proof: We have
Covmat ({x})jk
o
o n
n
x(j) , x(k)
n
o n
o
= cov x(k) , x(j)
= cov
= Covmat ({x})kj
64
Section 3.3
65
Proof: We have
uT u
=
=
1 X T
u (xi mean ({x})) (xi mean ({x}))T u
N i
2
1 X T
u (xi mean ({x})) .
N i
Section 3.3
66
FIGURE 3.10: On the left, a blob in two dimensions. This is a set of data points
that lie somewhat clustered around a single center, given by the mean. I have
plotted the mean of these data points with a +. On the right, a data set that is
best thought of as a collection of five blobs. I have plotted the mean of each with a
+. We could compute the mean and covariance of this data, but it would be less
revealing than the mean and covariance of a single blob. In chapter 14.5, I will
describe automatic methods to describe this dataset as a series of blobs.
square, or symmetric, or anything else; it just has to have second dimension d). It
is easy to compute the mean and covariance of {u}. We have
mean ({u}) =
=
so you get the new mean by multiplying the original mean by A and adding b.
The new covariance matrix is easy to compute as well. We have:
Covmat ({u}) = Covmat ({Ax + b})
P
T
i (ui mean ({u}))(ui mean ({u}))
=
N
P
T
i (Axi + b Amean ({x}) b)(Axi + b Amean ({x}) b)
=
N
P
A i (xi mean ({x}))(xi mean ({x}))T AT
=
N
= ACovmat ({x})AT .
3.3.2 Transforming Blobs
The trick to interpreting high dimensional data is to use the mean and covariance
to understand the blob. Figure 3.11 shows a two-dimensional data set. Notice that
there is obviously some correlation between the x and y coordinates (its a diagonal
blob), and that neither x nor y has zero mean. We can easily compute the mean
and subtract it from the data points, and this translates the blob so that the origin
is at the center (Figure 3.11). In coordinates, this means we compute the new
Section 3.3
67
FIGURE 3.11: On the left, a blob in two dimensions. This is a set of data points
that lie somewhat clustered around a single center, given by the mean. I have plotted
the mean of these data points with a hollow square (its easier to see when there is
a lot of data). To translate the blob to the origin, we just subtract the mean from
each datapoint, yielding the blob on the right.
dataset {u} from the old dataset {x} by the rule ui = xi mean ({x}). This new
dataset has been translated so that the mean is zero.
Once this blob is translated (Figure 3.12, left), we can rotate it as well. It
is natural to try to rotate the blob so that there is no correlation between distinct
pairs of dimensions. We can do so by diagonalizing the covariance matrix. In
particular, let U be the matrix formed by stacking the eigenvectors of Covmat ({x})
into a matrix (i.e. U = [v1 , . . . , vd ], where vj are eigenvectors of the covariance
matrix). We now form the dataset {n}, using the rule
ni = U T ui = U T (xi mean ({x})).
The mean of this new dataset is clearly 0. The covariance of this dataset is
Covmat ({n}) = Covmat U T x
= U T Covmat ({x})U
= ,
Section 3.3
68
Rotate to diagonalize
covariance
FIGURE 3.12: On the left, the translated blob of figure 3.11. This blob lies somewhat
diagonally, because the vertical and horizontal components are correlated. On the
right, that blob of data rotated so that there is no correlation between these components. We can now describe the blob by the vertical and horizontal variances alone,
as long as we do so in the new coordinate system. In this coordinate system, the
vertical variance is significantly larger than the horizontal variance the blob is
short and wide.
can describe the blob simply by giving the variances of each component the
covariances are zero.
Translating a blob of data doesnt change the scatterplot matrix in any interesting way (the axes change, but the picture doesnt). Rotating a blob produces
really interesting results, however. Figure 3.14 shows the dataset of figure 3.3,
translated to the origin and rotated to diagonalize it. Now we do not have names
for each component of the data (theyre linear combinations of the original components), but each pair is now not correlated. This blob has some interesting shape
features. Figure 3.14 shows the gross shape of the blob best. Each panel of this
figure has the same scale in each direction. You can see the blob extends about 80
units in direction 1, but only about 15 units in direction 2, and much less in the
other two directions. You should think of this blob as being rather cigar-shaped;
its long in one direction, but there isnt much in the others. The cigar metaphor
isnt perfect because there arent any 4 dimensional cigars, but its helpful. You
can think of each panel of this figure as showing views down each of the four axes
of the cigar.
Now look at figure ??. This shows the same rotation of the same blob of
data, but now the scales on the axis have changed to get the best look at the
detailed shape of the blob. First, you can see that blob is a little curved (look at
the projection onto direction 2 and direction 4). There might be some effect here
worth studying. Second, you can see that some points seem to lie away from the
main blob. I have plotted each data point with a dot, and the interesting points
Section 3.3
69
FIGURE 3.13: On the left, the translated and rotated blob of figure 3.12. This blob is
stretched one direction has more variance than another. Because all covariances
are zero, it is easy to scale the blob so that all variances are one (the blob on the
right). You can think of this as a standard blob. All blobs can be reduced to a
standard blob, by relatively straightforward linear algebra.
Section 3.3
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
Direction 1
100
Direction 3
100
100
100
Direction 2
Direction 4
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
70
100
100
100
100
100
100
100
100
100
FIGURE 3.14: A panel plot of the bodyfat dataset of figure 3.3, now rotated so that the
covariance between all pairs of distinct dimensions is zero. Now we do not know
names for the directions theyre linear combinations of the original variables.
Each scatterplot is on the same set of axes, so you can see that the dataset extends
more in some directions than in others.
Section 3.3
5
5
1
2
3
5
100
5
1
100
10
1
5
5
50
2
3
50
Direction 4
5
20
20
10
4
3 1
0
10
0
2
20
100
100
10
3
1
5
10
20
50
0
10
4
2
Direction 2
100
2
Direction 1
100
50
100
5
1
3
0
20
32
50
5
100
50
100
20
5
1
2
4
25 3
1
50
20
100
50
50
100
20
5
50
50
Direction 3
50
3
71
2
1
34
0
20
100
5
FIGURE 3.15: A panel plot of the bodyfat dataset of figure 3.3, now rotated so that
the covariance between all pairs of distinct dimensions is zero. Now we do not know
names for the directions theyre linear combinations of the original variables. I
have scaled the axes so you can see details; notice that the blob is a little curved,
and there are several data points that seem to lie some way away from the blob,
which I have numbered.
Section 3.4
72
Blob coordinates
Translation
FIGURE 3.16: A 2D blob, with its natural blob coordinate system. The origin of this
coordinate system is at the mean of the data. The coordinate axes are (a) at right
angles to one another and (b) are directions that have no covariance.
Section 3.4
10
5
10
0
12
5
5
0
10
5
73
10
5
10
0
12
5
5
10
5
FIGURE 3.17: On the left, a blob of 3D data that has very low variance in two
directions in the blob coordinates. As a result, all the data points are very close to
a 1D blob. Experience shows that this is a common phenomenon. Although there
might be many components in the data items, all data points are very close to a
much lower dimensional object in the high dimensional space. When this is the
case, we could obtain a lower dimensional representation of the data by working in
blob coordinates, or we could smooth the data (as on the right), by projecting each
data point onto the lower dimensional space.
some directions in the blob coordinate system, the blob will be spread out ie
have large variance but in others, it might not be.
Equivalently, imagine we choose to represent each data item in blob coordinates. Then the mean over the dataset will be zero. Each pair of distinct coordinates will be uncorrelated. Some coordinates corresponding to directions where
the blob is spread out will have a large range of values. Other coordinates
directions in which the blob is small will have a small range of values. We could
choose to replace these coordinates with zeros, with little significant loss in accuracy. The advantage of doing so is that we would have lower dimensional data to
deal with.
However, it isnt particularly natural to work in blob coordinates. Each component of a data item may have a distinct meaning and scale (i.e. feet, pounds,
and so on), but this is not preserved in any easy way in blob coordinates. Instead,
we should like to (a) compute a lower dimensional representation in blob coordinates then (b) transform that representation into the original coordinate system of
the data item. Doing so is a form of smoothing suppressing small, irrelevant
variations by exploiting multiple data items.
For example, look at Figure 3.17. Imagine we transform the blob on the left
to blob coordinates. The covariance matrix in these coordinates is a 3 3 diagonal
matrix. One of the values on the diagonal is large, because the blob is extended on
Section 3.4
74
one direction; but the other two are small. This means that, in blob coordinates, the
data varies significantly in one direction, but very little in the other two directions.
Now imagine we project the data points onto the high-variation direction;
equivalently, we set the other two directions to zero for each data point. Each
of the new data points is very close to the corresponding old data point, because
by setting the small directions to zero we havent moved the point very much.
In blob coordinates, the covariance matrix of this new dataset has changed very
little. It is again a 3 3 diagonal matrix, but now two of the diagonal values are
zero, because there isnt any variance in those directions. The third value is large,
because the blob is extended in that direction. We take the new dataset, and rotate
and translate it into the original coordinate system. Each point must lie close to the
corresponding point in the original dataset. However, the new dataset lies along a
straight line (because it lay on a straight line in the blob coordinates). This process
gets us the blob on the right in Figure 3.17. This blob is a smoothed version of the
original blob.
Smoothing works because when two data items are strongly correlated, the
value of one is a good guide to the value of the other. This principle works for
more than two data items. Section 14.5 describes an example where the data
items have dimension 101, but all values are extremely tightly correlated. In a
case like this, there may be very few dimensions in blob coordinates that have
any significant variation (3-6 for this case, depending on some details of what one
believes is a small number, and so on). The components are so strongly correlated
in this case that the 101-dimensional blob really looks like a slightly thickened 3
(or slightly more) dimensional blob that has been inserted into a 101-dimensional
space (Figure 3.17). If we project the 101-dimensional data onto that structure
in the original, 101-dimensional space, we may get much better estimates of the
components of each data item than the original measuring device can supply. This
occurs because each component is now estimated using correlations between all the
measurements.
3.4.2 The Low-Dimensional Representation of a Blob
We wish to construct an r dimensional representation of a blob, where we have
chosen r in advance. First, we compute {v} by translating the blob so its mean is
at the origin, so that vi = xi mean ({x}). Now write V = [v1 , v2 , . . . , vN ]. The
covariance matrix of {v} is then
Covmat ({v}) =
1
VV T = Covmat ({x}).
N
Now write for the diagonal matrix of eigenvalues of Covmat ({x}) and U for the
matrix of eigenvectors, so that Covmat ({x})U = U. We assume that the elements
of are sorted in decreasing order along the diagonal. The covariance matrix for
the dataset transformed into blob coordinates will be . Notice that
=
=
=
U T Covmat ({x})U
U T VV T U
(U T V)(U T V)T .
Section 3.4
75
This means we can interpret (U T V) as a new dataset {b}. This is our data, rotated
into blob coordinates.
Now write r for the d d matrix
Ir 0
0 0
which projects a d dimensional vector onto its first r components, and replaces the
others with zeros. Then we have that
r = r Tr
is the covariance matrix for the reduced dimensional data in blob coordinates.
Notice that r keeps the r largest eigenvalues on the diagonal of , and replaces
all others with zero.
We have
r
= r Tr
= r U T Covmat ({x})UTr
= (r U T V)(V T UTr )
= PP T
where P = (r U T V). This represents our data, rotated into blob coordinates,
and then projected down to r dimensions, with remaining terms replaced by zeros.
Write {br } for this new dataset.
Occasionally, we need to refer to this representation, and we give it a special
name. Write
pcaproj (xi , r, {x}) = r U T (xi mean ({x}))
where the notation seeks to explicitly keep track of the fact that the low dimensional
representation of a particular data item depends on the whole dataset (because you
have to be able to compute the mean, and the eigenvectors of the covariance).
Notice that pcaproj (xi , r, {x}) is a representation of the dataset with important
properties:
The representation is r-dimensional (i.e. the last d r components are zero).
Each pair of distinct components of {pcaproj (xi , r, {x})} has zero covariance.
The first component of {pcaproj (xi , r, {x})} has largest variance; the second
component has second largest variance; and so on.
3.4.3 Smoothing Data with a Low-Dimensional Representation
We would now like to construct a low dimensional representation of the blob, in
the original coordinates. We do so by rotating the low-dimensional representation
back to the original coordinate system, then adding back the mean to translate the
origin back to where it started. We can write this as
pcasmooth (xi , r, {x}) = UTr r U T (xi mean ({x})) + mean ({x})
= UTr pcaproj (xi , r, {x}) + mean ({x})
Section 3.4
76
Original blob
Translate to
origin by
subtracting the
mean
Rotate to line
up with axes
analysis.
we have a new representation of the ith data item in the original space (Figure 14.5). Now consider the dataset obtained by smoothing each of our data items.
We write this dataset as {pcasmooth (xi , r, {x})}.
You should think of {pcasmooth (xi , r, {x})} as a smoothed version of the
original data. One way to think of this process is that we have chosen a lowdimensional basis that represents the main variance in the data rather well. It is
quite usual to think of a data item as being given by a the mean plus a weighted
sum of these basis elements. In this view, the first weight has larger variance than
the second, and so on. By construction, this dataset lies in an r dimensional affine
subspace of the original space. We constructed this r-dimensional space to preserve
the largest variance directions of the data. Each column of this matrix is known as
a principal component. In particular, we have constructed this dataset so that
Section 3.4
77
1D Representation
Rotated to line up
with blob coordinates
Section 3.4
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
Direction 1
Direction 4
100
Direction 3
100
100
100
Direction 2
100
100
100
100
100
100
100
100
100
100
100
100
100
100
100
78
100
100
100
100
100
100
100
100
100
100
100
100
100
FIGURE 3.20: A panel plot of the bodyfat dataset of figure 3.3, with the dimension
reduced to two using principal components analysis. Compare this figure to figure
3.14, which is on the same set of axes. You can see that the blob has been squashed
in direction 3 and direction 4. But not much has really happened, because there
wasnt very much variation in those directions in the first place.
Section 3.4
0.25
3
Value
0.2
0.15
2
1
0.1
0.05
300
400
500
600
700
Wavelength (nm)
800
0
0
50
100
Number of eigenvalue
0.05
0.1
0.15
400
600
Wavelength (nm)
800
0.1
0.1
0
0.1
0.2
300
150
0.2
Reflectance value
Reflectance value
Reflectance value
0.3
Reflectance value
79
0.2
200
400
500
600
700
Wavelength (nm)
800
0
0.1
0.2
0.3
300
400
500
600
700
Wavelength (nm)
800
FIGURE 3.21: On the top left, the mean spectral reflectance of a dataset of 1995
spectral reflectances, collected by Kobus Barnard (at http:// www.cs.sfu.ca/ colour/
data/ ). On the top right, eigenvalues of the covariance matrix of spectral reflectance data, from a dataset of 1995 spectral reflectances, collected by Kobus
Barnard (at http:// www.cs.sfu.ca/ colour/ data/ ). Notice how the first few eigenvalues are large, but most are very small; this suggests that a good representation
using few principal components is available. The bottom row shows the first three
principal components. A linear combination of these, with appropriate weights,
added to the mean of figure ??, gives a good representation of the dataset.
Section 3.4
80
0.8
0.4
0.3
0.6
0.2
0.4
0.1
0
0.2
0.1
400
500
600
700
0.2
400
500
600
700
FIGURE 3.22: On the left, a spectral reflectance curve (dashed) and approximations
using the mean, the mean and 3 principal components, the mean and 5 principal
components, and the mean and 7 principal components. Notice the mean is a relatively poor approximation, but as the number of principal components goes up, the
error falls rather quickly. On the right is the error for these approximations. Figure plotted from a dataset of 1995 spectral reflectances, collected by Kobus Barnard
(at http:// www.cs.sfu.ca/ colour/ data/ ).
ponents. The remaining d r components of br,i are zero. So we can write
2
d
X
(u)
(bi )2 .
u=r+1
Now a natural measure of error is the average over the dataset of this term. We
have that
d
d
n
o
X
1 X
(u)
var b(u)
(bi )2 =
N u=r+1
u=r+1
which is easy to evaluate, because we know these variances they are the values
of the d r eigenvalues that we decided to ignore. So the mean error can be written
as
1T ( r )1.
Now we could choose r by identifying how much error we can tolerate. More usual
is to plot the eigenvalues of the covariance matrix, and look for a knee, like that
in Figure 14.5. You can see that the sum of remaining eigenvalues is small.
3.4.5 Example: Representing Spectral Reflectances
Diffuse surfaces reflect light uniformly in all directions. Examples of diffuse surfaces
include matte paint, many styles of cloth, many rough materials (bark, cement,
stone, etc.). One way to tell a diffuse surface is that it does not look brighter
(or darker) when you look at it along different directions. Diffuse surfaces can
Section 3.4
20
15
15
Value
Value
81
10
10
5
5
0
0
1000
2000
3000
Number of eigenvalue
4000
0
0
5
10
15
Number of eigenvalue
20
FIGURE 3.23: On the left,the eigenvalues of the covariance of the Japanese facial
expression dataset; there are 4096, so its hard to see the curve (which is packed
to the left). On the right, a zoomed version of the curve, showing how quickly the
values of the eigenvalues get small.
be colored, because the surface reflects different fractions of the light falling on it
at different wavelengths. This effect can be represented by measuring the spectral
reflectance of a surface, which is the fraction of light the surface reflects as a function
of wavelength. This is usually measured in the visual range of wavelengths (about
380nm to about 770 nm). Typical measurements are every few nm, depending on
the measurement device. I obtained data for 1995 different surfaces from http://
www.cs.sfu.ca/colour/data/ (there are a variety of great datasets here, from Kobus
Barnard).
Each spectrum has 101 measurements, which are spaced 4nm apart. This
represents surface properties to far greater precision than is really useful. Physical
properties of surfaces suggest that the reflectance cant change too fast from wavelength to wavelength. It turns out that very few principal components are sufficient
to describe almost any spectral reflectance function. Figure 3.21 shows the mean
spectral reflectance of this dataset, and Figure 3.21 shows the eigenvalues of the
covariance matrix.
This is tremendously useful in practice. One should think of a spectral reflectance as a function, usually written (). What the principal components analysis tells us is that we can represent this function rather accurately on a (really
small) finite dimensional basis. This basis is shown in figure 3.21. This means that
there is a mean function r() and k functions m () such that, for any (),
() = r() +
k
X
ci i () + e()
i=1
where e() is the error of the representation, which we know is small (because it
consists of all the other principal components, which have tiny variance). In the
case of spectral reflectances, using a value of k around 3-5 works fine for most
applications (Figure 3.22). This is useful, because when we want to predict what
Section 3.4
82
FIGURE 3.24: The mean and first 16 principal components of the Japanese facial
expression dataset.
a particular object will look like under a particular light, we dont need to use a
detailed spectral reflectance model; instead, its enough to know the ci for that
object. This comes in useful in a variety of rendering applications in computer
graphics. It is also the key step in an important computer vision problem, called
color constancy. In this problem, we see a picture of a world of colored objects under unknown colored lights, and must determine what color the objects
are. Modern color constancy systems are quite accurate, even though the problem
sounds underconstrained. This is because they are able to exploit the fact that
relatively few ci are enough to accurately describe a surface reflectance.
3.4.6 Example: Representing Faces with Principal Components
An image is usually represented as an array of values. We will consider intensity
images, so there is a single intensity value in each cell. You can turn the image
into a vector by rearranging it, for example stacking the columns onto one another
Section 3.4
83
10
20
50
100
FIGURE 3.25: Approximating a face image by the mean and some principal compo-
nents; notice how good the approximation becomes with relatively few components.
(use reshape in Matlab). This means you can take the principal components of a
set of images. Doing so was something of a fashionable pastime in computer vision
for a while, though there are some reasons that this is not a great representation of
pictures. However, the representation yields pictures that can give great intuition
into a dataset.
Figure ?? shows the mean of a set of face images encoding facial expressions of
Japanese women (available at http://www.kasrl.org/jaffe.html; there are tons of face
datasets at http://www.face-rec.org/databases/). I reduced the images to 64x64,
which gives a 4096 dimensional vector. The eigenvalues of the covariance of this
dataset are shown in figure 3.23; there are 4096 of them, so its hard to see a
trend, but the zoomed figure suggests that the first couple of hundred contain
most of the variance. Once we have constructed the principal components, they
can be rearranged into images; these images are shown in figure 3.24. Principal
components give quite good approximations to real images (figure 3.25).
The principal components sketch out the main kinds of variation in facial
expression. Notice how the mean face in Figure 3.24 looks like a relaxed face, but
with fuzzy boundaries. This is because the faces cant be precisely aligned, because
each face has a slightly different shape. The way to interpret the components is to
remember one adjusts the mean towards a data point by adding (or subtracting)
some scale times the component. So the first few principal components have to
do with the shape of the haircut; by the fourth, we are dealing with taller/shorter
faces; then several components have to do with the height of the eyebrows, the
shape of the chin, and the position of the mouth; and so on. These are all images of
women who are not wearing spectacles. In face pictures taken from a wider set of
models, moustaches, beards and spectacles all typically appear in the first couple
of dozen principal components.
Section 3.5
84
xT1
X = xT2
. . . xTN
where each row of the matrix is a data vector. Now notice that the covariance
matrix for this dataset can be formed by constructing X T X , so that
Covmat ({X}) = X T X
and if we form the SVD (see the math notes at the end if you dont remember this)
of X , we have X = UV T . But we have X T X = VT V T so that
V T X T X V = T
and T is diagonal. So we can recover the principal components of the dataset
without actually forming the covariance matrix - we just form the SVD of X .
3.5.2 Just a few Principal Components with NIPALS
For really big datasets, even taking the SVD is hard. Usually, we dont really want
to recover all the principal components, because we want to recover a reasonably
accurate low dimensional representation of the data. We continue to work with a
data matrix X , whose rows are data items. Now assume we wish to recover the first
principal component. This means we are seeking a vector u and a set of N numbers
wi such that wi u is a good approximation to xi . In particular, we would like the
dataset made of wi u to encode as much of the variance of the original dataset as
possible. Now we can stack the wi into a column vector w. The Frobenius norm
is a term for the matrix norm obtained by summing squared entries of the matrix.
We write
X
a2ij .
|| A ||F =
i,j
In the exercises, you will show that the choice of w and u minimizes the cost
|| X wuT ||F
Section 3.5
X
ij
85
(xij wi uj ) .
Now we need to find the relevant w and u. Notice there is not a unique choice,
because the pair (sw, (1/s)u) works as well as the pair (w, u). We will choose u
such that || u || = 1. There is still not a unique choice, because you can flip the
signs in u and w, but this doesnt matter. The gradient of the cost function is a
set of partial derivatives with respect to components of w and u. The partial with
respect to wk is
X
C
(xkj wk uj ) uj
=
wk
j
which can be written in matrix vector form as
w C = (X wuT )u.
Similarly, the partial with respect to ul is
X
C
(xil wi ul ) wi
=
ul
i
which can be written in matrix vector form as
u C = (X T uwT )w.
At the solution, these partial derivatives are zero. This suggests an algorithm.
First, assume we have an estimate of u, say u(n) . Then we could choose the w that
makes the partial wrt w zero, so
1
w(n+ 2 ) =
X u(n)
(u(n) )T u(n)
Now we can update the estimate of u by choosing a value that makes the partial
1
wrt u zero, using our estimate w(n+ 2 ) , to get
1
u(n+ 2 ) =
X T w(n+ 2 )
1
(w(n+ 2 ) )T w
We need to rescale to ensure that our estimate of u has unit length. Write s =
1
1
((w(n+ 2 ) )T w) 2 We get
1
u(n+ 2 )
(n+1)
u
=
s
and
1
w(n+1) = sw(n+ 2 ) .
This iteration can be started by choosing some row of X as u(0) . You can test for
convergence by checking || u(n+1) u(n) ||. If this is small enough, then the algorithm
has converged.
Section 3.5
86
To obtain a second principal component, you form X (1) = X wuT and apply
the algorithm to that. You can get many principal components like this, but its not
a good way to get all of them (eventually numerical issues mean the estimates are
poor). The algorithm is widely known as NIPALS (for Non-linear Iterative Partial
Least Squares).
NIPALS is quite forgiving of missing values, though missing values make
it
hard
to use matrix notation. Recall I wrote the cost function as C(w, u) =
P
(x
wi uj )2 . We change the sum so that it ranges over only the known values,
ij
ij
to get
X
C(w, u) =
(xij wi uj )2
ijknown values
then write
and
C
=
wk
C
=
ul
jknown
(xkj wk uj )uj
iknown
(xil wi ul )wi .
values for k
values for l
(n+ 1 )
uk 2
=P
iknown
iknown
values for l wl
(n+ 21 )
wl
Section 3.5
87
differences between classes. One very important construction for such directions
can be used for classification and for regression.
Assume we have a dataset of N items, each with two parts xi and yi . We
assume that at least xi has high dimension. We also assume that mean ({x}) = 0
and mean ({y}) = 0. This simplifies notation, and is easy to achieve (subtract the
mean). This is a mild generalization of what occurred in classification, where we
had a feature vector xi and a label yi for each data item; but now instead of having
just a label, we have a vector. This situation arises in practice quite often. For
example, xi might be a vector that describes an image and yi might be a vector
that describes a caption for that image. If we have a classification problem with C
classes, we might choose yi to be a vector with zero in all components except the
one corresponding to the class (this is sometimes known as a one-hot vector). We
wish to choose projections of x and y to a shared low dimensional space, so that
the projections in this space are strongly correlated to one another.
For the moment, assume the low dimensional space is one dimensional. Then
there is some unit vector a so that projecting xi to that space is given by aT xi ;
similarly, there is some unit vector b so that projecting yi to the space is given by
bT yi . Now we stack the vectors into data matrices whose rows are data items, as
above, so the ith row of X is xTi and the ith row of Y is yiT . Then px = matxXa
is a vector containing all the projections of the x part, and py = Yb is a vector
containing all the projections of the y part. We want these projections to be like
one another to the extent possible.
One criterion we can use is to maximize pTx py by choice of a and b. We must
maximise
aT max X T Yb subject to aT a = 1 and bT b = 1.
Write a and b for Lagrange multipliers. To solve this problem, we must find a
and b such that
X T Yb = a a and Y T X a = b b.
We can substitute using the second equation to get
(X T YY T X )a = a b a
which means that a is an eigenvector of (X T YY T X ). It turns out we seek the
eigenvector corresponding to the largest eigenvalue; equivalently, we must solve
aT (X T YY T X )a subject to aT a = 1
There is a straightforward way to obtain a second, third, etc. dimension. We
take the dataset, and subtract the a component from each xi and the b component
from each yi ; we now have a new dataset, and seek another a, b using our procedure.
These new directions must (a) maximise the covariance we are interested in and (b)
are orthogonal to the original directions. As Figure ?? suggests, these projections
of the data separate blobs of data with different labels.
3.5.4 Just a few Discriminative Directions with PLS1
Notice that we have, again, formed a (kind of) covariance matrix here, meaning
that this method might not apply if the dimension of the data is big. But we
Section 3.6
88
1
0
Second direction
0.5
0.0
1.0
0.5
1.0
Multi-Dimensional Scaling
10
11
First direction
FIGURE 3.26:
Section 3.6
89
20
Second direction
20
20
0
20
40
40
80
60
60
40
40
60
60
Multi-Dimensional Scaling
100
100
200
300
550
500
450
400
350
300
250
First direction
FIGURE 3.27:
(where the superscript is to remind you that this is a squared distance). We could
build an N N matrix of squared distances, which we write D(2) (x). The i, jth
(2)
entry in this matrix is Dij (x), and the x argument means that the distances are
between points in the high-dimensional space. Now we could choose the vi to make
2
X (2)
(2)
Di j(x) Dij (v)
ij
as small as possible. Doing so should mean that points that are far apart in the
high dimensional space are far apart in the plot, and that points that are close in
the high dimensional space are close in the plot.
In its current form, the expression is difficult to deal with, but we can refine
it. Because translation does not change the distances between points, it cannot
change either of the D(2) matrices. So it is enoughP
to solve the case when the mean
of the points xi is zero. We can assume that N1
i xi = 0. Now write 1 for the
n-dimensional vector containing all ones, and I for the identity matrix. Notice that
(2)
1 T
A = I 11 .
N
Using this expression, you can show that the matrix M, defined below,
1
M(x) = AD(2) (x)AT
2
has i, jth entry xi xj (exercises). I now argue that, to make D(2) (v) is close to
D(2) (x), it is enough to make M(v) close to M(x). Proving this will take us out
of our way unnecessarily, so I omit a proof.
Section 3.6
Multi-Dimensional Scaling
90
We can choose a set of vi that makes D(2) (v) close to D(2) (x) quite easily,
using the method of the previous section. Take the dataset of N d-dimensional
column vectors xi , and form a matrix X by stacking the vectors, so
X = [x1 , x2 , . . . , xN ] .
In this notation, we have
M(x) = X T X .
This matrix is symmetric, and it is positive semidefinite. It cant be positive definite, because the data is zero mean, so M(x)1 = 0. The M(v) we seek must (a)
be as close as possible to M(x) and (b) have rank 2. It must have rank 2 because
there must be some V which is 2 N so that M(v) = V T V. The columns of this
V are our vi .
We can use the method of section ?? to construct M(v) and V. As usual,
we write U for the matrix of eigenvectors of M(x), for the diagonal matrix of
eigenvalues sorted in descending order, 2 for the 2 2 upper left hand block of
(1/2)
, and 2
for the matrix of positive square roots of the eigenvalues. Then our
methods yield
(1/2) (1/2)
M(v) = U2 2 2 U2T
and
(1/2)
V = 2
U2T
and we can plot these vi (example in section 14.5). This method for constructing
a plot is known as principal coordinate analysis.
This plot might not be perfect, because reducing the dimension of the data
points should cause some distortions. In many cases, the distortions are tolerable.
In other cases, we might need to use a more sophisticated scoring system that
penalizes some kinds of distortion more strongly than others. There are many ways
to do this; the general problem is known as multidimensional scaling.
Section 3.6
Multi-Dimensional Scaling
91
400
Polokwane
200
Nelspruit
Mahikeng
Johannesburg
200
Kimberley
Bloemfontein
Pietermaritzburg
400
600
Bhisho
800
Cape Town
1000
800
600
400
200
200
400
FIGURE 3.28: On the left, a public domain map of South Africa, obtained from
http:// commons.wikimedia.org/ wiki/ File:Map of South Africa.svg , and edited to remove surrounding countries. On the right, the locations of the cities inferred by
multidimensional scaling, rotated, translated and scaled to allow a comparison to
the map by eye. The map doesnt have all the provincial capitals on it, but its easy
to see that MDS has placed the ones that are there in the right places (use a piece
of ruled tracing paper to check).
0.2
0.2
0.1
0.15
0.1
0.05
0.1
0
0.2
0.4
0.05
0.1
0.2
0.15
0
0.2
0.4
0.2
0.1
0.1
0.2
0.2
0.3 0.4
0.2
0.2
0.4
0.2
0.1
0.1
0.2
0.3
FIGURE 3.29: Two views of the spectral data of section 3.4.5, plotted as a scatter
Section 3.6
Multi-Dimensional Scaling
92
capital, and rotated, translated and scaled the resulting plot to check it against a
real map (Figure 3.28).
One natural use of principal coordinate analysis is to see if one can spot
any structure in a dataset. Does the dataset form a blob, or is it clumpy? This
isnt a perfect test, but its a good way to look and see if anything interesting is
happening. In figure 3.29, I show a 3D plot of the spectral data, reduced to three
dimensions using principal coordinate analysis. The plot is quite interesting. You
should notice that the data points are spread out in 3D, but actually seem to lie on
a complicated curved surface they very clearly dont form a uniform blob. To
me, the structure looks somewhat like a butterfly. I dont know why this occurs, but
it certainly suggests that something worth investigating is going on. Perhaps the
choice of samples that were measured is funny; perhaps the measuring instrument
doesnt make certain kinds of measurement; or perhaps there are physical processes
that prevent the data from spreading out over the space.
0.15
0.1
USSR
China
Japan
Yugoslavia
0.05
USA
Israel
France
Egypt
0.05
India
Cuba
0.1
Congo
Brazil
0.15
0.1
0.05
0.05
0.1
0.15
0.2
FIGURE 3.30: A map of country similarity, prepared from the data of figure ??. The
map is often interpreted as showing a variation in development or wealth (poorest
at bottom left to richest at top right); and freedom (most repressed at top left and
freeest at bottom right). I havent plotted these axes, because the interpretation
wouldnt be consistent with current intuition (the similarity data is forty years old,
and quite a lot has happened in that time).
Our algorithm has one really interesting property. In some cases, we do not
actually know the datapoints as vectors. Instead, we just know distances between
the datapoints. This happens often in the social sciences, but there are important
cases in computer science as well. As a rather contrived example, one could survey
people about breakfast foods (say, eggs, bacon, cereal, oatmeal, pancakes, toast,
muffins, kippers and sausages for a total of 9 items). We ask each person to rate the
similarity of each pair of distinct items on some scale. We advise people that similar
items are ones where, if they were offered both, they would have no particular
preference; but, for dissimilar items, they would have a strong preference for one
over the other. The scale might be very similar, quite similar, similar, quite
dissimilar, and very dissimilar (scales like this are often called Likert scales).
We collect these similarities from many people for each pair of distinct items, and
Section 3.7
93
50
40
20
0
0
20
40
50
50
200
400
100 600
50
100
0
100 500
400
300
200
then average the similarity over all respondents. We compute distances from the
similarities in a way that makes very similar items close and very dissimilar items
distant. Now we have a table of distances between items, and can compute a V
and produce a scatter plot. This plot is quite revealing, because items that most
people think are easily substituted appear close together, and items that are hard
to substitute are far apart. The neat trick here is that we did not start with a X ,
but with just a set of distances; but we were able to associate a vector with eggs,
and produce a meaningful plot.
Table ?? shows data from one such example. Students were interviewed (in
1971! things may have changed since then) about their perceptions of the similarity
of countries. The averaged perceived similarity is shown in table ??. Large numbers
reflect high similarity, so we cant use these numbers directly. It is reasonable to
turn these numbers into distances by (a) using 0 as the distance between a country
and itself and (b) using esij as the distance between countries i and j (where sij is
the similarity between them). Once we have distances, we can apply the procedure
of section 3.6.1 to get points, then plot a scatter plot (Figure 3.30).
3.7 EXAMPLE: UNDERSTANDING HEIGHT AND WEIGHT
Recall the height-weight data set of section ?? (from http://www2.stetson.edu/
jrasp/data.htm; look for bodyfat.xls at that URL). This is, in fact, a 16-dimensional
dataset. The entries are (in this order): bodyfat; density; age; weight; height; adiposity; neck; chest; abdomen; hip; thigh; knee; ankle; biceps; forearm; wrist. We
know already that many of these entries are correlated, but its hard to grasp a 16
dimensional dataset in one go. The first step is to investigate with a multidimensional scaling.
Figure ?? shows a multidimensional scaling of this dataset down to three
dimensions. The dataset seems to lie on a (fairly) flat structure in 3D, meaning
that inter-point distances are relatively well explained by a 2D representation. Two
points seem to be special, and lie far away from the flat structure. The structure
Section 3.7
94
HeightWeight 2D MDS
50
50
100
500
400
300
200
dataset. One data point is clearly special, and another looks pretty special. The
data seems to form a blob, with one axis quite a lot more important than another.
isnt perfectly flat, so there will be small errors in a 2D representation; but its clear
that a lot of dimensions are redundant. Figure 3.32 shows a 2D representation of
these points. They form a blob that is stretched along one axis, and there is no sign
of multiple blobs. Theres still at least one special point, which we shall ignore but
might be worth investigating further. The distortions involved in squashing this
dataset down to 2D seem to have made the second special point less obvious than
it was in figure ??.
HeightWeight mean
weight
180
160
140
120
chest hip
abdomen
100
80
height
thigh
60
age
40
20
0
bodyfat
knee
neck
adiposity
biceps
forearm
ankle
wrist
density
2
10
12
14
16
18
FIGURE 3.33: The mean of the bodyfat.xls dataset. Each component is likely in a
different unit (though I dont know the units), making it difficult to plot the data
without being misleading. Ive adopted one solution here, by plotting each component
as a vertical bar, and labelling the bar. You shouldnt try to compare the values to
one another. Instead, think of this plot as a compact version of a table.
The next step is to try a principal component analysis. Figure 3.33 shows
the mean of the dataset. The components of the dataset have different units, and
Section 3.7
95
0.9
1200
0.8
1000
0.7
0.6
800
0.5
600
0.4
0.3
400
0.2
200
age
0
bodyfat
0.1
10
12
14
16
density
2
4
abdomen
chest
hip
thigh
adiposity
knee biceps
neck
ankle forearm
height
wrist
10
12
14
16
18
FIGURE 3.34: On the left, the eigenvalues of the covariance matrix for the bodyfat
data set. Notice how fast the eigenvalues fall off; this means that most principal
components have very small variance, so that data can be represented well with a
small number of principal components. On the right, the first principal component
for this dataset, plotted using the same convention as for figure 3.33.
Figure 3.34 also shows the first principal component. The eigenvalues justify
thinking of each data item as (roughly) the mean plus some weight times this
principal component. From this plot you can see that data items with a larger
value of weight will also have larger values of most other measurements, except age
and density. You can also see how much larger; if the weight goes up by 8.5 units,
then the abdomen will go up by 3 units, and so on. This explains the main variation
in the dataset.
In the rotated coordinate system, the components are not correlated, and they
have different variances (which are the eigenvalues of the covariance matrix). You
can get some sense of the data by adding these variances; in this case, we get 1404.
This means that, in the
translated and rotated coordinate system, the average data
point is about 37 = 1404 units away from the center (the origin). Translations
and rotations do not change distances, so the average data point is about 37 units
from the center in the original dataset, too. If we represent a datapoint by using
the mean and the first three principal components, there will be some error. We
can estimate the average error from the component variances. In this case, the sum
of the first three eigenvalues is 1357, so the mean square
error in representing a
p
datapoint by the first three principal components is (1404 1357), or 6.8. The
relative error is 6.8/37 = 0.18. Another way to represent this information, which is
more widely used, is to say that the first three principal components explain all but
Section 3.8
age
0.8
0.6
0.6
0.4
0.4
0.2
0.2
0
0.2
0
bodyfat
abdomen
chest
adiposity
neck
density
2
height
weight
4
6
hip
8
10
0
wrist
14
16
abdomen
chest
adiposity
density
hip thigh
biceps
forearm
knee
ankle
wrist
neck
0.2
knee
biceps
forearm
ankle
thigh
12
96
18
0.4
0
height
ageweight
4
6
10
12
14
16
18
FIGURE 3.35: On the left, the second principal component, and on the right the
third principal component of the height-weight dataset.
(1404 1357)/1404 = 0.034, or 3.4% of the variance; notice that this is the square
of the relative error, which will be a much smaller number.
All this means that explaining a data point as the mean and the first three
principal components produces relatively small errors. Figure 3.36 shows the second
and third principal component of the data. These two principal components suggest
some further conclusions. As age gets larger, height and weight get slightly smaller,
but the weight is redistributed; abdomen gets larger, whereas thigh gets smaller.
A smaller effect (the third principal component) links bodyfat and abdomen. As
bodyfat goes up, so does abdomen.
3.8 WHAT YOU SHOULD REMEMBER - NEED SOMETHING
PROBLEMS
Summaries
3.1. You have a dataset {x} of N vectors, xi , each of which is d-dimensional. We
will consider a linear function of this dataset. Write a for a constant vector;
then the value of this linear function evaluated on the ith data item is aT xi .
Write fi = aT xi . We can make a new dataset {f } out of the values of this
linear function.
(a) Show that mean ({f }) = aT mean ({x}) (easy).
(b) Show that var ({f }) = aT Covmat ({x})a (harder, but just push it through
the definition).
(c) Assume the dataset has the special property that there exists some a so
that aT Covmat ({x})a. Show that this means that the dataset lies on a
hyperplane.
3.2. On Figure 3.36, mark the mean of the dataset, the first principal component,
and the second principal component.
3.3. You have a dataset {x} of N vectors, xi , each of which is d-dimensional.
Assume that Covmat ({x}) has one non-zero eigenvalue. Assume that x1 and
Section 3.8
97
10
5
0
5
10
10
10
PROGRAMMING EXERCISES
3.4. Obtain the iris dataset from the UC Irvine machine learning data repository at
http://https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data.
(a) Plot a scatterplot matrix of this dataset, showing each species with a
different marker. The fragment of R code in listing 3.1 should take you
most of the way.
(b) Now obtain the first two principal components of the data. Plot the
data on those two principal components alone, again showing each species
with a different marker. Has this plot introduced significant distortions?
Explain
3.5. Take the wine dataset from the UC Irvine machine learning data repository at
http://archive.ics.uci.edu/ml/datasets/seeds.
(a) Plot the eigenvalues of the covariance matrix in sorted order. How many
principal components should be used to represent this dataset? Why?
(b) Construct a stem plot of each of the first 3 principal components (i.e. the
eigenvectors of the covariance matrix with largest eigenvalues). What do
you see?
(c) Compute the first two principal components of this dataset, and project
it onto those components. Now produce a scatter plot of this two dimensional dataset, where data items of class 1 are plotted as a 1, class 2 as
a 2, and so on.
3.6. Take the wheat kernel dataset from the UC Irvine machine learning data repository at http://archive.ics.uci.edu/ml/datasets/seeds. Compute the first two
principal components of this dataset, and project it onto those components.
(a) Produce a scatterplot of this projection. Do you see any interesting phenomena?
Section 3.8
98
(b) Plot the eigenvalues of the covariance matrix in sorted order. How many
principal components should be used to represent this dataset? why?
C H A P T E R
99
Section 4.1
100
Section 4.1
101
5
4
1
2
distance
2 3 4 5
FIGURE 4.1: Left, a data set; right, a dendrogram obtained by agglomerative clus-
tering using single-link clustering. If one selects a particular value of distance, then
a horizontal line at that distance splits the dendrogram into clusters. This representation makes it possible to guess how many clusters there are and to get some
insight into how good the clusters are.
4.1.1 Clustering and Distance
In the algorithms above, and in what follows, we assume that the features are scaled
so that distances (measured in the usual way) between data points are a good
representation of their similarity. This is quite an important point. For example,
imagine we are clustering data representing brick walls. The features might contain
several distances: the spacing between the bricks, the length of the wall, the height
of the wall, and so on. If these distances are given in the same set of units, we could
have real trouble. For example, assume that the units are centimeters. Then the
spacing between bricks is of the order of one or two centimeters, but the heights
of the walls will be in the hundreds of centimeters. In turn, this means that the
distance between two datapoints is likely to be completely dominated by the height
and length data. This could be what we want, but it might also not be a good
thing.
There are some ways to manage this issue. One is to know what the features
measure, and know how they should be scaled. Usually, this happens because you
have a deep understanding of your data. If you dont (which happens!), then it is
often a good idea to try and normalize the scale of the data set. There are two good
strategies. The simplest is to translate the data so that it has zero mean (this is
just for neatness - translation doesnt change distances), then scale each direction
so that it has unit variance. More sophisticated is to translate the data so that
it has zero mean, then transform it so that each direction is independent and has
unit variance. Doing so is sometimes referred to as decorrelation or whitening
Section 4.1
102
100
80
60
40
20
60
232
64
57
225
62
13
21
7
130
46
209
93
210
184
51
116
124
99
197
218
69
89
90
102
91
131
92
215
117
213
115
201
103
132
111
142
110
139
135
170
173
174
160
114
198
195
137
185
105
196
127
141
136
190
122
113
126
112
123
143
220
68
70
138
202
73
200
118
77
227
230
229
233
217
125
223
98
100
38
101
94
104
214
133
107
219
228
108
56
179
129
15
193
186
199
167
211
53
224
75
106
76
119
67
204
95
97
189
49
203
37
188
158
11
145
4
163
221
9120
206
72
8
208
191
171
48
128
176
183
177
146
19
154
151
125
144
26
30
32
161
24
231
23
27
47
52
109
55
121
71
236
78
80
84
85
81
16
147
63
14
6
155
18
166
20
168
162
29
234
66
235
134
172
45
156
82
33
237
83
148
2
157
31
164
165
181
34
194
240
50
149
44
153
150
88
54
159
17
246
28
239
87
251
22
207
386
245
58
65
238
242
61
247
249
252
243
244
40
212
59
140
43
182
10
192
178
35
180
187
222
175
12
226
79
241
74
248
250
205
152
96
169
216
536
41
42
39
FIGURE 4.2: A dendrogram obtained from the body fat dataset, using single link
clustering. Recall that the data points are on the horizontal axis, and that the
vertical axis is distance; there is a horizontal line linking two clusters that get
merged, established at the height at which theyre merged. I have plotted the entire
dendrogram, despite the fact its a bit crowded at the bottom, because it shows that
most data points are relatively close (i.e. there are lots of horizontal branches at
about the same height).
(because you make the data more like white noise); I described how to do this in
section ??.
Section 4.1
103
250
200
150
100
50
50
100
30
20
10
10
20
30
40
FIGURE 4.3: A clustering of the body fat dataset, using agglomerative clustering,
single link distance, and requiring a maximum of 30 clusters. I have plotted each
cluster with a distinct marker (though some markers differ only by color; you might
need to look at the PDF version to see this figure at its best). Notice that one
cluster contains much of the data, and that there are a set of small isolated clusters.
The original data is 16 dimensional, which presents plotting problems; I show a
scatter plot on the first two principal components (though I computed distances for
clustering in the original 16 dimensional space).
Section 4.1
104
2
1.8
1.6
1.4
1.2
1
0.8
0.6
0.4
0.2
173
207
169
155
192
203
188
186
145
159
162
195
205
193
209
206
146
156
174
177
179
160
151
184
187
150
175
191
176
178
194
190
153
158
167
182
163
183
196
181
154
170
157
144
201
210
197
148
164
168
165
171
172
149
199
161
198
141
143
185
152
147
166
180
200
202
189
142
204
208
59
1
35
50
56
25
18
47
23
5
263
29
8
22
57
39
645
48
49
21
54
14
15
33
51
753
12
4
46
34
68
16
41
42
30
55
67
69
27
70
32
20
64
58
2
28
43
66
13
19
10
9
36
37
38
11
44
63
24
60
31
65
52
71
108
77
137
75
122
96
101
123
134
140
72
76
73
81
132
74
118
107
92
105
93
104
97
112
119
106
85
116
98
100
113
111
124
131
86
99
88
110
87
102
128
117
126
127
109
120
103
79
95
82
94
130
133
80
135
84
129
125
91
136
114
138
139
61
62
115
78
90
121
83
89
17
40
FIGURE 4.4: A dendrogram obtained from the seed dataset, using single link cluster-
ing. Recall that the data points are on the horizontal axis, and that the vertical axis
is distance; there is a horizontal line linking two clusters that get merged, established
at the height at which theyre merged. I have plotted the entire dendrogram, despite
the fact its a bit crowded at the bottom, because you can now see how clearly the
data set clusters into a small set of clusters there are a small number of vertical
runs.
Cluster the seed dataset from the UC Irvine Machine Learning Dataset Repository (you can find it at http://archive.ics.uci.edu/ml/datasets/seeds).
Solution: Each item consists of seven measurements of a wheat kernel; there
are three types of wheat represented in this dataset. As you can see in figures 4.4
and 4.5, this data clusters rather well.
Section 4.1
105
8
4
FIGURE 4.5: A clustering of the seed dataset, using agglomerative clustering, single
link distance, and requiring a maximum of 30 clusters. I have plotted each cluster
with a distinct marker (though some markers differ only by color; you might need to
look at the PDF version to see this figure at its best). Notice that there are a set of
fairly natural isolated clusters. The original data is 8 dimensional, which presents
plotting problems; I show a scatter plot on the first two principal components (though
I computed distances for clustering in the original 8 dimensional space).
Agglomerative clustering in R
Cluster the questions in the student evaluation dataset from the UC Irvine
Machine Learning Dataset Repository (you can find it at https://archive.ics.
uci.edu/ml/datasets/Turkiye+Student+Evaluation; this dataset was donated by
G. Gunduz and E. Fokoue), and display it on the first two principal components.
You should use 5 clusters, but investigate the results of choosing others.
Solution: R provides tools for agglomerative clustering, too. I used the block
of code shown in listing 4.1 to produce figure 4.6. The clustering shown in
that figure is to 5 clusters. To get some idea of what the clusters are like,
you can compute the per-cluster means, using a line in that listing. I found
means where: all students in the cluster gave moderately low, low, moderately
high and high answers to all questions, respectively; and one where all students
answered 1 to all questions. There are 5820 students in this collection. The
clusters suggest that answers tend to be quite strongly correlated a student
who gives a low answer to a question will likely give low answers to others, too.
Choosing other numbers of clusters wasnt particularly revealing, though there
were more levels to the answers.
Section 4.2
106
CLUSPLOT( wdm )
(0.996,1.8]
(3.4,4.2]
(4.2,5]
(2.6,3.4]
Component 2
(1.8,2.6]
Component 1
These two components explain 86.76 % of the point variability.
FIGURE 4.6:
X
X
(clusters, data) =
(xj ci )T (xj ci ) .
iclusters
jith
cluster
Notice that if we know the center for each cluster, it is easy to determine which
cluster is the best choice for each point. Similarly, if the allocation of points to
clusters is known, it is easy to compute the best center for each cluster. However,
there are far too many possible allocations of points to clusters to search this space
for a minimum. Instead, we define an algorithm that iterates through two activities:
Section 4.2
107
Assume the cluster centers are known and, allocate each point to the closest
cluster center.
Assume the allocation is known, and choose a new set of cluster centers. Each
center is the mean of the points allocated to that cluster.
We then choose a start point by randomly choosing cluster centers, and then iterate
these stages alternately. This process eventually converges to a local minimum of
the objective function (the value either goes down or is fixed at each step, and
it is bounded below). It is not guaranteed to converge to the global minimum of
the objective function, however. It is also not guaranteed to produce k clusters,
unless we modify the allocation phase to ensure that each cluster has some nonzero
number of points. This algorithm is usually referred to as k-means (summarized
in Algorithm 4.3).
Usually, we are clustering high dimensional data, so that visualizing clusters
can present a challenge. If the dimension isnt too high, then we can use panel
plots. An alternative is to project the data onto two principal components, and
plot the clusters there. A natural dataset to use to explore k-means is the iris
data, where we know that the data should form three clusters (because there are
three species). Recall this dataset from section ??. I reproduce figure 3.5 from
that section as figure 4.11, for comparison. Figures 4.8, ?? and ?? show different
k-means clusterings of that data.
Section 4.2
setosa
versicolor
108
virginica
2.5
2.0
1.5
Petal
Width 1.0
h
Petal.Lengt
Petal
Length
0.5
0.0
4
3
2
1
4.5
3.5 4.0 4.5
4.0
3.5
Sepal
Width 3.0
2.5
Petal.Width
2.0
8
7
Sepal.Length
Sepal
Length
6
5
FIGURE 4.7: Left: a 3D scatterplot for the famous Iris data, originally due to ***.
I have chosen three variables from the four, and have plotted each species with a
different marker. You can see from the plot that the species cluster quite tightly,
and are different from one another. Right: a scatterplot matrix for the famous Iris
data, originally due to ***. There are four variables, measured for each of three
species of iris. I have plotted each species with a different marker. You can see from
the plot that the species cluster quite tightly, and are different from one another.
K-means clustering in R
Cluster the iris dataset into two clusters using k-means, then plot the results
on the first two principal components
Solution: I used the code fragment in listing 4.2, which produced figure ??
Section 4.2
109
looking for the k that gives the smallest value of the cost function is not helpful,
because that k is always the same as the number of data points (and the value is
then zero). However, it can be very helpful to plot the value as a function of k, then
look at the knee of the curve. Figure 4.11 shows this plot for the iris data. Notice
that k = 3 the true answer doesnt look particularly special, but k = 2,
k = 3, or k = 4 all seem like reasonable choices. It is possible to come up with
a procedure that makes a more precise recommendation by penalizing clusterings
that use a large k, because they may represent inefficient encodings of the data.
However, this is often not worth the bother.
In some special cases (like the iris example), we might know the right answer
to check our clustering against. In such cases, one can evaluate the clustering by
looking at the number of different labels in a cluster (sometimes called the purity),
and the number of clusters. A good solution will have few clusters, all of which
have high purity.
Mostly, we dont have a right answer to check against. An alternative strategy,
which might seem crude to you, for choosing k is extremely important in practice.
Usually, one clusters data to use the clusters in an application (one of the most
Section 4.2
1.5
110
CLUSPLOT( scalediris )
1.0
0.5
0.0
Petal
Width
0.0
0.5
0.5
1.0
1.5
1.5
1.5
0.5
2
1
Sepal
Width
0.0
0.5
1.0
1.5
1.5
3
Petal
Length
Component 2
0.0
0.5
1.0
0
1
2
2 1 0
Sepal
Length
0
1
0
2
Component 1
These two components explain 95.81 % of the point variability.
FIGURE 4.8: On the left, a panel plot of the iris data clustered using k-means with
k = 2. By comparison with figure 4.11, notice how the versicolor and verginica
clusters appear to have been merged. On the right, this data set projected onto the
first two principal components, with one blob drawn over each cluster.
d2
i,j
22
Section 4.2
1.5
111
CLUSPLOT( scalediris )
1.0
0.5
0.0
Petal
Width
0.0
0.5
0.5
1.0
1.5
1.5
1.5
0.5
0.0
0.5
1.0
1.5
2
1
Sepal
Width
1.5
3
Petal
Length
Component 2
0.0
0.5
1.0
1
2
2 1 0
Sepal
Length
0
1
0
2
Component 1
These two components explain 95.81 % of the point variability.
FIGURE 4.9: On the left, a panel plot of the iris data clustered using k-means with
k = 3. By comparison with figure 4.11, notice how the clusters appear to follow
the species labels. On the right, this data set projected onto the first two principal
components, with one blob drawn over each cluster.
where > 0 is a choice of scaling parameter. This is often called the affinity
between the point i and the center j. Now a natural choice of weights is
si,j
wi,j = Pk
l=1 si,l
All these weights are non-negative, they sum to one, and the weight is large if the
point is much closer to one center than to any other. The scaling parameter sets
the meaning of much closer we measure distance in units of .
Once we have weights, re-estimating the cluster centers is easy. We use a
weights to compute a weighted average of the points. In particular, we re-estimate
the jth cluster center by
P
w x
Pi i,j i .
i wi,j
Notice that k-means is a special case of this algorithm where limits to zero. In
this case, each point has a weight of one for some cluster, and zero for all others,
and the weighted mean becomes an ordinary mean. I have collected the description
into Algorithm 4.4 for convenience.
4.2.3 General Comments on K-Means
If you experiment with k-means, you will notice one irritating habit of the algorithm.
It almost always produces either some rather spread out clusters, or some single
element clusters. Most clusters are usually rather tight and blobby clusters, but
there is usually one or more bad cluster. This is fairly easily explained. Because
every data point must belong to some cluster, data points that are far from all
Section 4.2
1.5
112
CLUSPLOT( scalediris )
1.0
0.5
0.0
Petal
Width
1
0.0
0.5
0.5
1.0
1.5
1.5
1.5
1.0
0.5
2
1
Sepal
Width
1
0.0
0.5
1.0
1.5
4
0
1.5
5
Petal
Length
Component 2
0.0
0.5
Sepal
Length
2 1 0
0
0
1
0
2
Component 1
These two components explain 95.81 % of the point variability.
FIGURE 4.10: On the left, a panel plot of the iris data clustered using k-means with
k = 5. By comparison with figure 4.11, notice how setosa seems to have been broken
in two groups, and versicolor and verginica into a total of three . On the right,
this data set projected onto the first two principal components, with one blob drawn
over each cluster.
others (a) belong to some cluster and (b) very likely drag the cluster center into
a poor location. This applies even if you use soft assignment, because every point
must have total weight one. If the point is far from all others, then it will be
assigned to the closest with a weight very close to one, and so may drag it into a
poor location, or it will be in a cluster on its own.
There are ways to deal with this. If k is very big, the problem is often not
significant, because then you simply have many single element clusters that you
can ignore. It isnt always a good idea to have too large a k, because then some
larger clusters might break up. An alternative is to have a junk cluster. Any point
that is too far from the closest true cluster center is assigned to the junk cluster,
and the center of the junk cluster is not estimated. Notice that points should not
be assigned to the junk cluster permanently; they should be able to move in and
out of the junk cluster as the cluster centers move.
In some cases, we want to cluster objects that cant be averaged. For example,
you can compute distances between two trees but you cant meaningfully average
them. In some cases, you might have a table of distances between objects, but
not know vectors representing the objects. For example, one could collect data on
the similarities between countries (as in Section 3.6.2, particularly Figure 3.30),
then try and cluster using this data (similarities can be turned into distances by,
for example, taking the negative logarithm). A variant of k-means, known as kmedoids, applies to this case.
In k-medoids, the cluster centers are data items rather than averages, but the
rest of the algorithm has a familiar form. We assume the number of medoids is
known, and initialize these randomly. We then iterate two procedures. In the first,
Section 4.3
virginica
2.5
Petal
Width
Petal
Length
4.5
3.5 4.0 4.5
3.5
Sepal
Width
3.0
4
3
2
1
400
4.0
0.5
0.0
1.0
500
1.5
600
2.0
300
versicolor
113
200
setosa
2.5
2.0 2.5 3.0
2.0
100
8
8
Sepal
Length
6
5
10
12
14
Number of Clusters
FIGURE 4.11: On the left, the scatterplot matrix for the Iris data, for reference. On
the right, a plot of the value of the cost function for each of several different values
of k. Notice how there is a sharp drop in cost going from k = 1 to k = 2, and again
at k = 4; after that, the cost falls off slowly. This suggests using k = 2, k = 3, or
k = 4, depending on the precise application.
we allocate data points to medoids. In the second, we choose the best medoid for
each cluster by finding the medoid that minimizes the sum of distances of points in
the cluster to that medoid (blank search is fine).
4.3 DESCRIBING REPETITION WITH VECTOR QUANTIZATION
Repetition is an important feature of many interesting signals. For example, images contain textures, which are orderly patterns that look like large numbers of
small structures that are repeated. Examples include the spots of animals such as
leopards or cheetahs; the stripes of animals such as tigers or zebras; the patterns on
bark, wood, and skin. Similarly, speech signals contain phonemes characteristic,
stylised sounds that people assemble together to produce speech (for example, the
ka sound followed by the tuh sound leading to cat). Another example comes
from accelerometers. If a subject wears an accelerometer while moving around, the
signals record the accelerations during their movements. So, for example, brushing
ones teeth involves a lot of repeated twisting movements at the wrist, and walking
involves swinging the hand back and forth.
Repetition occurs in subtle forms. The essence is that a small number of
local patterns can be used to represent a large number of examples. You see this
effect in pictures of scenes. If you collect many pictures of, say, a beach scene, you
will expect most to contain some waves, some sky, and some sand. The individual
patches of wave, sky or sand can be surprisingly similar, and different images are
made by selecting some patches from a vocabulary of patches, then placing them
down to form an image. Similarly, pictures of living rooms contain chair patches,
TV patches, and carpet patches. Many different living rooms can be made from
Section 4.3
114
||xi cj||
22
end
i,j
Section 4.3
115
vectors overlap or not. We then build a set of clusters out of these vectors; this set
of clusters is often thought of as a dictionary. We can now now describe any new
vector with the cluster center closest to that vector. This means that a vector in
a continuous space is described with a number in the range [1, . . . , k] (where you
get to choose k), and two vectors that are close should be described by the same
number. This strategy is known as vector quantization.
We can now build features that represent important repeated structure in signals. We take a signal, and cut it up into vectors of length d. These might overlap,
or be disjoint; we follow whatever strategy we used in building the dictionary. We
then take each vector, and compute the number that describes it (i.e. the number of
the closest cluster center, as above). We then compute a histogram of the numbers
we obtained for all the vectors in the signal. This histogram describes the signal.
Notice several nice features to this construction. First, it can be applied to
anything that can be thought of in terms of vectors, so it will work for speech
signals, sound signals, accelerometer signals, images, and so on. You might need to
adjust some indices. For example, you cut the image into patches, then rearrange
the patch to form a vector. As another example, accelerometer signals are three
dimensional vectors that depend on time, so you cut out windows of a fixed number
of time samples (say t), then rearrange to get a 3t dimensional vector.
Another nice feature is the construction can accept signals of different length,
and produce a description of fixed length. One accelerometer signal might cover 100
time intervals; another might cover 200; but the description is always a histogram
with k buckets, so its always a vector of length k.
Yet another nice feature is that we dont need to be all that careful how we
cut the signal into fixed length vectors. This is because it is hard to hide repetition.
This point is easier to make with a figure than in text, so look at figure ??.
4.3.2 Example: Groceries in Portugal
At http://archive.ics.uci.edu/ml/datasets/Wholesale+customers, you will find a dataset
giving sums of money spent annually on different commodities by customers in Portugal. The commodities are divided into a set of categories (fresh; milk; grocery;
frozen; detergents and paper; and delicatessen) relevant for the study. These customers are divided by channel (two channels) and by region (three regions). You
can think of the data as being divided into six groups (one for each channel-region
pair). There are 440 records, and so there are many customers per group. Figure 4.12 shows a panel plot of the customer data; the data has been clustered, and
I gave each of 20 clusters its own marker. Relatively little structure is apparent in
this scatter plot. You cant, for example, see evidence of six groups that are cleanly
separated.
Its unlikely that all the customers in a group are the same. Instead, we
expect that there might be different types of customer. For example, customers
who prepare food at home might spend more money on fresh or on grocery, and
those who mainly buy prepared food might spend more money on delicatessan;
similarly, coffee drinkers with cats or children might spend more on milk than the
lactose-intolerant, and so on. Because some of these effects are driven by things
like wealth and the tendency of people to like to have neighbors who are similar to
Section 4.3
116
Delicatessen
DetPaper
Frozen
Grocery
Milk
Fresh
FIGURE 4.12: A panel plot of the wholesale customer data of http:// archive.ics.uci.
edu/ ml/ datasets/ Wholesale+customers, which records sums of money spent annually on different commodities by customers in Portugal. This data is recorded for six
different groups (two channels each within three regions). I have plotted each group
with a different marker, but you cant really see much structure here, for reasons
explained in the text.
them, you could expect that different groups contain different numbers of each type
of customer. There might be more deli-spenders in wealthier regions; more milkspenders and detergent-spenders in regions where it is customary to have many
children; and so on.
An effect like this is hard to see on a panel plot (Figure 4.12). The plot
for this dataset is hard to read, because the dimension is fairly high for a panel
plot and the data is squashed together in the bottom left corner. There is another
effect. If customers are grouped in the way I suggested above, then each group
might look the same in a panel plot. A group of some milk-spenders and more
detergent-spenders will have many data points with high milk expenditure values
(and low other values) and also many data points with high detergent expenditure
values (and low other values). In a panel plot, this will look like two blobs; but
another group with more milk-spenders and some detergent-spenders will also look
like two blobs, in about the same place. It will be hard to spot the difference. A
histogram of the types within each group will make this difference obvious.
I used k-means clustering to cluster the customer data to 20 different clusters
(Figure 4.14). I chose 20 rather arbitrarily, but with the plot of error against k
in mind. Then I described the each group of data by the histogram of customer
types that appeared in that group (Figure ??). Notice how the distinction between
the groups is now apparent the groups do appear to contain quite different
distributions of customer type. It looks as though the channels (rows in this figure)
are more different than the regions (columns in this figure). To be more confident
in this analysis, we would need to be sure that different types of customer really
Section 4.3
117
1.5e+11
Delicatessen
1.0e+11
Frozen
Grocery
5.0e+10
DetPaper
Milk
Fresh
0
10
15
20
25
30
35
Number of Clusters
FIGURE 4.13: On the left, the sum of squared error for clusterings of the customer
data with k-means for k running from 2 to 35. This suggests using a k somewhere in
the range 10-30; I chose 20. On the right, I have clustered this data to 20 cluster
centers with k-means. The clusters do seem to be squashed together, but the plot on
the left suggests that clusters do capture some important information. Using too few
clusters will clearly lead to problems. Notice that I did not scale the data, because
each of the measurements is in a comparable unit. For example, it wouldnt make
sense to scale expenditures on fresh and expenditures on grocery with a different
scale.
are different. We could do this by repeating the analysis for fewer clusters, or by
looking at the similarity of customer types.
4.3.3 Efficient Clustering and Hierarchical K Means
One important difficulty occurs in applications. We might need to have an enormous
dataset (millions of image patches are a real possibility), and so a very large k. In
this case, k means clustering becomes difficult because identifying which cluster
center is closest to a particular data point scales linearly with k (and we have to
do this for every data point at every iteration). There are two useful strategies for
dealing with this problem.
The first is to notice that, if we can be reasonably confident that each cluster
contains many data points, some of the data is redundant. We could randomly
subsample the data, cluster that, then keep the cluster centers. This works, but
doesnt scale particularly well.
A more effective strategy is to build a hierarchy of k-means clusters. We
randomly subsample the data (typically, quite aggressively), then cluster this with
a small value of k. Each data item is then allocated to the closest cluster center, and
the data in each cluster is clustered again with k-means. We now have something
that looks like a two-level tree of clusters. Of course, this process can be repeated to
produce a multi-level tree of clusters. It is easy to use this tree to vector quantize a
Section 4.3
0.2
0.1
0.0
0.3
count/sum(count)
0.3
count/sum(count)
count/sum(count)
0.3
0.2
0.1
0.0
1
10
15
20
10
15
20
0.1
0.0
15
20
10
15
20
0.3
0.2
0.1
0.0
10
case21
case13
count/sum(count)
count/sum(count)
count/sum(count)
0.2
0.1
case12
0.3
0.2
0.0
1
case11
0.3
118
0.2
0.1
0.0
1
10
15
case22
20
10
15
20
case23
FIGURE 4.14: The histogram of different types of customer, by group, for the cus-
tomer data. Notice how the distinction between the groups is now apparent the
groups do appear to contain quite different distributions of customer type. It looks
as though the channels (rows in this figure) are more different than the regions
(columns in this figure).
query data item. We vector quantize at the first level. Doing so chooses a branch of
the tree, and we pass the data item to this branch. It is either a leaf, in which case
we report the number of the leaf, or it is a set of clusters, in which case we vector
quantize, and pass the data item down. This procedure is efficient both when one
clusters and at run time.
4.3.4 Example: Activity from Accelerometer Data
Section 4.3
30
1000
2000 3000
Time
4000
5000
35
30
25
0
25
1000
2000 3000
Time
4000
5000
20
0
20
500
1000 1500
Time
2000
2500
40
20
0
0
200
400
600
Time
800
2000
4000
Time
6000
40
35
30
25
0
8000
X Acceleration
X Acceleration
X Acceleration
30
60
40
0
0
35
60
45
40
X Acceleration
35
40
1000
80
60
60
40
20
500
1000 1500
Time
2000
2000
Time
4000
6000
80
0
0
119
45
X Acceleration
40
25
0
45
45
X Acceleration
X Acceleration
50
X Acceleration
2500
40
20
0
0
500
1000 1500
Time
2000
2500
4.15:
Some
examples
from
the
accelerometer
dataset
at
https:// archive.ics.uci.edu/ ml/ datasets/
Dataset+for+ADL+Recognition+with+Wrist-worn+Accelerometer.
I have labelled each signal by the activity. These show acceleration in the X direction (Y
and Z are in the dataset, too). There are four examples for brushing teeth and
four for eat meat. You should notice that the examples dont have the same length
in time (some are slower and some faster eaters, etc.), but that there seem to be
characteristic features that are shared within a category (brushing teeth seems to
involve faster movements than eating meet).
FIGURE
with this by warping time and resampling the signal. For example, doing so will
make a thorough toothbrusher look as though they are moving their hands very
fast (or a careless toothbrusher look ludicrously slow: think speeding up or slowing
down a movie). So we need a representation that can cope with signals that are a
bit longer or shorter than other signals.
Another important property of these signals is that all examples of a particular
activity should contain repeated patterns. For example, brushing teeth should show
fast accelerations up and down; walking should show a strong signal at somewhere
around 2 Hz; and so on. These two points should suggest vector quantization to
you. Representing the signal in terms of stylized, repeated structures is probably a
good idea because the signals probably contain these structures. And if we represent
the signal in terms of the relative frequency with which these structures occur, the
representation will have a fixed length, even if the signal doesnt. To do so, we need
to consider (a) over what time scale we will see these repeated structures and (b)
how to ensure we segment the signal into pieces so that we see these structures.
Generally, repetition in activity signals is so obvious that we dont need to be
smart about segment boundaries. I broke these signals into 32 sample segments,
one following the other. Each segment represents one second of activity. This
is long enough for the body to do something interesting, but not so long that our
representation will suffer if we put the segment boundaries in the wrong place. This
resulted in about 40, 000 segments. I then used hierarchical k-means to cluster these
segments. I used two levels, with 40 cluster centers at the first level, and 12 at the
second. Figure 4.16 shows some cluster centers at the second level.
Section 4.3
120
X Acceleration
60
40
20
0
0
10
20
Time
30
40
FIGURE 4.16: Some cluster centers from the accelerometer dataset. Each cluster
Brush teeth
Brush teeth
Brush teeth
0.2
0.2
0.2
0.2
0.15
0.15
0.15
0.15
0.1
0.1
0.1
0.1
0.05
0.05
0.05
0.05
100
200
300
Comb hair
400
100
200
300
Comb hair
400
100
200
300
Comb hair
400
0.2
0.2
0.2
0.2
0.15
0.15
0.15
0.15
0.1
0.1
0.1
0.1
0.05
0.05
0.05
0.05
100
200
300
Climb stairs
400
100
200
300
Climb stairs
400
100
200
300
Climb stairs
400
0.2
0.2
0.2
0.2
0.15
0.15
0.15
0.15
0.1
0.1
0.1
0.1
0.05
0.05
0.05
0.05
100
200
300
400
100
200
300
400
100
200
300
400
100
200
300
Comb hair
400
100
200
300
Climb stairs
400
100
200
300
400
FIGURE 4.17: Histograms of cluster centers for the accelerometer dataset, for different activities. You should notice that (a) these histograms look somewhat similar for
different actors performing the same activity and (b) these histograms look somewhat different for different activities.
Section 4.3
121
I then computed histogram representations for different example signals (Figure 4.17). You should notice that when the activity label is different, the histogram
looks different, too.
Another useful way to check this representation is to compare the average
within class chi-squared distance with the average between class chi-squared distance. I computed the histogram for each example. Then, for each pair of examples,
I computed the chi-squared distance between the pair. Finally, for each pair of activity labels, I computed the average distance between pairs of examples where one
example has one of the activity labels and the other example has the other activity
label. In the ideal case, all the examples with the same label would be very close
to one another, and all examples with different labels would be rather different.
Table 4.1 shows what happens with the real data. You should notice that for some
pairs of activity label, the mean distance between examples is smaller than one
would hope for (perhaps some pairs of examples are quite close?). But generally,
examples of activities with different labels tend to be further apart than examples
of activities with the same label.
0.9
2.0
1.6
1.9
2.0
1.5
2.0
1.8
2.0
1.4
2.0
2.0
1.9
2.0
1.5
2.0
2.0
1.9
2.0
1.8
0.9
1.9
2.0
1.9
2.0
1.7
1.7
0.3
2.0
1.9
1.9
2.0
1.9
1.9
1.9
1.8
1.9
1.9
1.9
2.0
1.9
1.9
1.9
1.8
1.7
1.9
2.0
1.9
2.0
1.8
1.8
1.5
1.9
1.9
1.6
2.0
1.9
1.9
2.0
1.9
1.9
1.9
1.9
1.9
1.9
1.8
2.0
1.9
1.9
2.0
1.9
1.9
1.9
1.9
1.9
1.9
1.9
1.8
2.0
2.0
1.9
2.0
1.8
1.9
1.9
1.9
1.9
1.9
1.9
2.0
1.5
2.0
1.7
2.0
1.8
2.0
2.0
2.0
1.9
1.9
2.0
1.9
1.9
2.0
1.5
4.1:
Each column of the table represents an activity
for
the
activity
dataset
https:// archive.ics.uci.edu/ ml/ datasets/
Dataset+for+ADL+Recognition+with+Wrist-worn+Accelerometer, as does each
row. In each of the upper diagonal cells, I have placed the average chi-squared
distance between histograms of examples from that pair of classes (I dropped the
lower diagonal for clarity). Notice that in general the diagonal terms (average
within class distance) are rather smaller than the off diagonal terms. This quite
strongly suggests we can use these histograms to classify examples successfully.
TABLE
Yet another way to check the representation is to try classification with nearest
neighbors, using the chi-squared distance to compute distances. I split the dataset
into 80 test pairs and 360 training pairs; using 1-nearest neighbors, I was able to
get a held-out error rate of 0.79. This suggests that the representation is fairly
good at exposing what is important.
Section 4.4
You should
122
term:
term:
term:
term:
term:
clustering . . . . . .
decorrelation . . . .
whitening . . . . . .
k-means . . . . . . .
vector quantization
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
. 93
. 95
. 95
. 101
. 109
Section 4.4
You should
123
PROGRAMMING EXERCISES
4.1. Obtain the activities of daily life dataset from the UC Irvine machine learning
website (https://archive.ics.uci.edu/ml/datasets/Dataset+for+ADL+Recognition+with+Wrist-worn+Accelerom
data provided by Barbara Bruno, Fulvio Mastrogiovanni and Antonio Sgorbissa).
(a) Build a classifier that classifies sequences into one of the 14 activities provided. To make features, you should vector quantize, then use a histogram
of cluster centers (as described in the subsection; this gives a pretty explicit set of steps to follow). You will find it helpful to use hierarchical
k-means to vector quantize. You may use whatever multi-class classifier
you wish, though Id start with Rs decision forest, because its easy to
use and effective. You should report (a) the total error rate and (b) the
class confusion matrix of your classifier.
(b) Now see if you can improve your classifier by (a) modifying the number
of cluster centers in your hierarchical k-means and (b) modifying the size
of the fixed length samples that you use.
C H A P T E R
where is a positive definite matrix. Notice that if is not positive definite, then
we cannot have a probability distribution,
because there are some directions d such
that exp 21 (td )T 1 (td ) does not fall off to zero as t limits to infinity.
In turn, this means we cant compute the integral, and so cant normalize.
The following facts explain the names of the parameters:
= i
N
124
Section 5.1
125
(which is quite easy to show). The maximum likelihood estimate of the covariance,
is
,
P
)(xi
)T
= i (xi
N
(which is rather a nuisance to show, because you need to know how to differentiate
a determinant). You should be aware that this estimate is not guaranteed to be
positive definite, even though the covariance matrix of a gaussian must be positive
definite. We deal with this problem by checking the estimate. If its smallest
eigenvalue is too close to zero, then we add some small positive constant times the
identity to get a positive definite matrix.
5.1.1 Affine Transformations and Gaussians
Gaussians behave very well under affine transformations. In fact, weve already
worked out all the math. Assume I have a dataset {x}. The mean of the maximum
likelihood gaussian model is mean ({x}), and the covariance is Covmat ({x}). We
assume that this is positive definite, or adjust it as above.
I can now transform the data with an affine transformation, to get yi =
Axi + b. We assume that A is a square matrix with full rank, so that this transformation is 1-1. The mean of the maximum likelihood gaussian model for the
transformed dataset is mean ({y}) = Amean ({x}) + b. Similarly, the covariance is
Covmat ({y}) = ACovmat ({x})AT .
A very important point follows in an obvious way. I can apply an affine
transformation to any multivariate gaussian to obtain one with (a) zero mean and
(b) independent components. In turn, this means that, in the right coordinate
system, any gaussian is a product of zero mean, unit standard deviation, onedimensional normal distributions. This fact is quite useful. For example, it means
that simulating multivariate normal distributions is quite straightforward you
could simulate a standard normal distribution for each component, then apply an
affine transformation.
5.1.2 Plotting a 2D Gaussian: Covariance Ellipses
There are some useful tricks for plotting a 2D Gaussian, which are worth knowing
both because theyre useful, and they help to understand Gaussians. Assume we
are working in 2D; we have a Gaussian with mean (which is a 2D vector), and
covariance (which is a 2x2 matrix). We could plot the collection of points x that
has some fixed value of p(x|, ). This set of points is given by:
1
(x )T 1 (x ) = c2
2
where c is some constant. I will choose c2 = 12 , because the choice doesnt matter,
and this choice simplifies some algebra. You might recall that a set of points x that
satisfies a quadratic like this is a conic section. Because (and so 1 ) is positive
definite, the curve is an ellipse. There is a useful relationship between the geometry
of this ellipse and the Gaussian.
This ellipse like all ellipses has a major axis and a minor axis. These
are at right angles, and meet at the center of the ellipse. We can determine the
Section 5.2
126
properties of the ellipse in terms of the Gaussian quite easily. The geometry of the
ellipse isnt affected by rotation or translation, so we will translate the ellipse so
that = 0 (i.e. the mean is at the origin) and rotate it so that 1 is diagonal.
Writing x = [x, y] we get that the set of points on the ellipse satisfies
1
1
1 1 2
( x + 2 y2) =
2 k12
k2
2
where k12 and k12 are the diagonal elements of 1 . We will assume that the ellipse
1
2
has been rotated so that k1 < k2 . The points (k1 , 0) and (k1 , 0) lie on the ellipse,
as do the points (0, k2 ) and (0, k2 ). The major axis of the ellipse, in this coordinate
system, is the x-axis, and the minor axis is the y-axis. In this coordinate system,
x and y are independent. If you do a little algebra, you will see that the standard
deviation of x is abs (k1 ) and the standard deviation of y is abs (k2 ). So the ellipse
is longer in the direction of largest standard deviation and shorter in the direction
of smallest standard deviation.
Now rotating the ellipse is means we will pre- and post-multiply the covariance
matrix with some rotation matrix. Translating it will move the origin to the mean.
As a result, the ellipse has its center at the mean, its major axis is in the direction
of the eigenvector of the covariance with largest eigenvalue, and its minor axis is
in the direction of the eigenvector with smallest eigenvalue. A plot of this ellipse,
which can be coaxed out of most programming environments with relatively little
effort, gives us a great deal of information about the underlying Gaussian. These
ellipses are known as covariance ellipses.
5.2 MIXTURE MODELS AND CLUSTERING
It is natural to think of clustering in the following way. The data was created by
a collection of distinct models (one per cluster). For each data item, something
(nature?) chose which model was to produce a point, and then the model produced
a point. We see the results: crucially, wed like to know what the models were,
but we dont know which model produced which point. If we knew the models, it
would be easy to decide which model produced which point. Similarly, if we knew
which point went to which model, we could determine what the models were.
One encounters this situation or problems that can be mapped to this situation again and again. It is very deeply embedded in clustering problems. It is
pretty clear that a natural algorithm is to iterate between estimating which model
gets which point, and the model parameters. We have seen this approach before,
in the case of k-means.
A particularly interesting case occurs when the models are probabilistic. There
is a standard, and very important, algorithm for estimation here, called EM (or
expectation maximization, if you want the long version). I will develop this
algorithm in two simple cases, and we will see it in a more general form later.
Notation: This topic lends itself to a glorious festival of indices, limits of
sums and products, etc. I will do one example in quite gory detail; the other
follows the same form, and for that well proceed more expeditiously. Writing the
limits of sums or products explicitlyP
is usually
Q even more confusing than adopting
a compact notation. When I write i or i , I mean a sum (or product) over all
Section 5.2
127
Q
P
values of i. When I write i,j or i,j , I mean a sum (or product) over all values
of i except for the jth item. I will write vectors, as usual, as x; the ith such vector
in a collection is xi , and the kth component of the ith vector in a collection is xik .
In what follows, I will construct a vector i corresponding to the ith data item xi
(it will tell us what cluster that item belongs to). I will write to mean all the i
(one
When I write
P for each data item). The jth component of this vector is ij . P
,
I
mean
a
sum
over
all
values
that
can
take.
When
I
write
u
, I mean a
u
P
sum over all values that each can take. When I write ,v , I mean a sum over
all values that all can take, omitting all cases for the vth vector v .
5.2.1 A Finite Mixture of Blobs
A blob of data points is quite easily modelled with a single normal distribution.
Obtaining the parameters is straightforward (estimate the mean and covariance
matrix with the usual expressions). Now imagine I have t blobs of data, and I know
t. A normal distribution is likely a poor model, but I could think of the data as being
produced by t normal distributions. I will assume that each normal distribution has
a fixed, known covariance matrix , but the mean of each is unknown. Because the
covariance matrix is fixed, and known, we can compute a factorization = AAT .
The factors must have full rank, because the covariance matrix must be positive
definite. This means that we can apply A1 to all the data, so that each blob
covariance matrix (and so each normal distribution) is the identity.
Write j for the mean of the jth normal distribution. We can model a
distribution that consists of t distinct blobs by forming aPweighted sum of the
blobs, where the jth blob gets weight j . We ensure that j j = 1, so that we
can think of the overall model as a probability distribution. We can then model
the data as samples from the probability distribution
"
#
X
1
1
T
exp (x j ) (x j ) .
j p
p(x|1 , . . . , k , 1 , . . . , k ) =
2
(2)d
j
The way to think about this probability distribution is that a point is generated by
first choosing one of the normal distributions (the ith is chosen with probability
j ), then generating a point from that distribution. This is a pretty natural model
of clustered data. Each mean is the center of a blob. Blobs with many points in
them have a high value of j , and blobs with few points have a low value of j .
We must now use the data points to estimate the values of j and j (again, I am
assuming that the blobs and the normal distribution modelling each have the
identity as a covariance matrix). A distribution of this form is known as a mixture
of normal distributions.
Writing out the likelihood will reveal a problem: we have a product of many
sums. The usual trick of taking the log will not work, because then you have a sum
of logs of sums, which is hard to differentiate and hard to work with. A much more
productive approach is to think about a set of hidden variables which tell us which
blob each data item comes from. For the ith data item, we construct a vector i . I
will write to mean all the i (one for each data item). The jth component of this
vector is ij , where ij = 1 if xi comes from blob (equivalently, normal distribution)
j and zero otherwise. Notice there is exactly one 1 in i , because each data item
Section 5.2
128
comes from one blob. Assume we know the values of these terms. I will write
= (1 , . . . , k , 1 , . . . , k ) for the unknown parameters. Then we can write
p(xi |i , ) =
Y
u
"
#iu
1
T
p
exp (x u ) (x u )
2
(2)d
1
(because ij = 1 means that xi comes from blob j, so the terms in the product are
a collection of 1s and the probability we want). We also have
p(ij = 1|) = j
allowing us to write
p(i |) =
[u ]
iu
(because this is the probability that we select blob j to produce a data item; again,
the terms in the product are a collection of 1s and the probability we want). This
means that
("
# )iu
Y
1
1
p
p(xi , i |) =
exp (x u )T (x u ) u
2
(2)d
u
and we can write a log-likelihood. The data are the observed values of x and
(remember, we pretend we know these; Ill fix this in a moment), and the parameters
are the unknown values of 1 , . . . , k and 1 , . . . , k . We have
L(1 , . . . , k , 1 , . . . , k ; x, )
= L(; x, )
X 1
T
(xi j ) (xi j ) + log j ij + K
=
2
ij
where K is a constant that absorbs the normalizing constants for the normal distributions. You should check this expression gives the right answer. I have used
the ij as a switch for one term, ij = 1 and the term in curly brackets is on,
and for all others that term is multiplied by zero. The problem with all this is that
we dont know . I will deal with this when we have another example.
5.2.2 Topics and Topic Models
A real attraction of probabilistic clustering methods is that we can cluster data
where there isnt a clear distance function. One example occurs in document processing. For many kinds of document, we obtain a good representation by (a)
choosing a list of different words then (b) representing the document by a vector of
word counts, where we simply ignore every word outside the list. This is a viable
representation for many applications because quite often, most of the words people
actually use come from a relatively short list (typically 100s to 1000s, depending
on the particular application). The vector has one component for each word in the
list, and that component contains the number of times that particular word is used.
The problem is to cluster the documents.
Section 5.2
129
It isnt a particularly good idea to cluster on the distance between word vectors. This is because quite small changes in word use might lead to large differences
between count vectors. For example, some authors might write car when others
write auto. In turn, two documents might have a large (resp. small) count for
car and a small (resp. large) count for auto. Just looking at the counts would
significantly overstate the difference between the vectors. However, the counts are
informative: a document that uses the word car often, and the word lipstick
seldom, is likely quite different from a document that uses lipstick often and car
seldom.
We get a useful notion of the differences between documents by pretending
that the count vector for each document comes from one of a small set of underlying
topics. Each topic generates words as independent, identically distributed samples
from a multinomial distribution, with one probability per word in the vocabulary.
You should think of each topic as being like a cluster center. If two documents come
from the same topic, they should have similar word distributions. Topics are one
way to deal with changes in word use. For example, one topic might have quite
high probability of generating the word car and a high probability of generating
the word auto; another might have low probability of generating those words,
but a high probability of generating lipstick.
We cluster documents together if they come from the same topic. Imagine
we know which document comes from which topic. Then we could estimate the
word probabilities using the documents in each topic. Now imagine we know the
word probabilities for each topic. Then we could tell (at least in principle) which
topic a document comes from by looking at the probability each topic generates
the document, and choosing the topic with the highest probability. This should
strike you as being a circular argument. It has a form you should recognize from
k-means, though the details of the distance have changed.
To construct a probabilistic model, we will assume that a document is generated in two steps. We will have t topics. First, we choose a topic, choosing the
jth topic with probability j . Then we will obtain a set of words by repeatedly
drawing IID samples from that topic, and record the count of each word in a count
vector. Assume we have N vectors of word counts, and write xi for the ith such
vector. Each topic is a multinomial probability distribution. Write pj for the vector of word probabilities for the jth topic. We assume that words are generated
independently, conditioned on the topic. Write xik for the kth component of xi ,
and so on. Then the probability of observing the counts in xi when the document
was generated by topic j is
Y
p(xi |pj ) =
pxjuiu .
u
X Y
l
pxluu
l .
Section 5.3
The EM Algorithm
130
This model is widely called a topic model; be aware that there are many kinds
of topic model, and this is a simple one. The expression should look unpromising,
in a familiar way. If you write out a likelihood, you will see a product of sums;
and if you write out a log-likelihood, you will see a sum of logs of sums. Neither
is enticing. We could use the same trick we used for a mixture of normals. Write
ij = 1 if xi comes from topic j, and ij = 0 otherwise. Then we have
"
#
Y
p(xi |ij = 1, ) =
pxjuiu
u
(because ij = 1 means that xi comes from topic j). This means we can write
p(xi |i , ) =
Y
l
("
pxluiu
#)il
(because ij = 1 means that xi comes from topic j, so the terms in the product are
a collection of 1s and the probability we want). We also have
p(ij = 1|) = j
(because this is the probability that we select topic j to produce a data item),
allowing us to write
Y
p(i |) =
[u ]iu
u
(again, the terms in the product are a collection of 1s and the probability we want).
This means that
#il
"
Y Y
xiu
p(xi , i |) =
(plu ) l
u
and we can write a log-likelihood. The data are the observed values of x and
(remember, we pretend we know these for the moment), and the parameters are
the unknown values collected in . We have
(
"
# )
X X X
L(; x, ) =
xiu log plu + log l il
i
Again, you should check this expression gives the right answer. Again, I have used
the ij as a switch for one term, ij = 1 and the term in curly brackets is on,
and for all others that term is multiplied by zero. The problem with all this, as
before, is that we dont know ij . But there is a recipe.
5.3 THE EM ALGORITHM
There is a straightforward, natural, and very powerful recipe. In essence, we will
average out the things we dont know. But this average will depend on our estimate
of the parameters, so we will average, then re-estimate parameters, then re-average,
and so on. If you lose track of whats going on here, think of the example of k-means
Section 5.3
The EM Algorithm
131
with soft weights (section 14.5; this is what the equations for the case of a mixture
of normals will boil down to). In this analogy, the tell us which cluster center a
data item came from. Because we dont know the values of the , we assume we
have a set of cluster centers; these allow us to make a soft estimate of the ; then
we use this estimate to re-estimate the centers; and so on.
This is an instance of a general recipe. Recall we wrote for a vector of
parameters. In the mixture of normals case, contained the means and the mixing
weights; in the topic model case, it contained the topic distributions and the mixing
weights. Assume we have an estimate of the value of this vector, say (n) . We could
then compute p(|(n) , x). In the mixture of normals case, this is a guide to which
example goes to which cluster. In the topic case, it is a guide to which example
goes to which topic.
We could use this to compute the expected value of the likelihood with respect
to . We compute
X
Q(; (n) ) =
L(; x, )p(|(n) , x)
(where the sum is over all values of ). Notice that Q(; (n) ) is a function of
(because L was), but now does not have any unknown terms in it. This Q(; (n) )
encodes what we know about .
For example, assume that p(|(n) , x) has a single, narrow peak in it, at (say)
0
= . In the mixture of normals case, this would mean that there is one allocation
of points to clusters that is significantly better than all others, given (n) . For this
example, Q(; (n) ) will be approximately L(; x, 0 ).
Now assume that p(|(n) , x) is about uniform. In the mixture of normals
case, this would mean that any particular allocation of points to clusters is about
as good as any other. For this example, Q(; (n) ) will average L over all possible
values with about the same weight for each.
We obtain the next estimate of by computing
(n+1) =
argmax
Q(; (n) )
and iterate this procedure until it converges (which it does, though I shall not prove
that). The algorithm I have described is extremely general and powerful, and is
known as expectation maximization or (more usually) EM. The step where
we compute Q(; (n) ) is called the E step; the step wehre we compute the new
estimate of is known as the M step.
5.3.1 Example: Mixture of Normals: The E-step
Now let us do the actual calculations for a mixture of normal distributions. The E
step requires a little work. We have
X
Q(; (n) ) =
L(; x, )p(|(n) , x)
If you look at this expression, it should strike you as deeply worrying. There are
a very large number of different possible values of . In this case, there are N t
Section 5.3
The EM Algorithm
132
cases (there is one i for each data item, and each of these can have a one in each
of t locations). It isnt obvious how we could compute this average.
But notice
p(, x|(n) )
p(|(n) , x) =
p(x|(n) )
and let us deal with numerator and denominator separately. For the numerator,
notice that the xi and the i are independent, identically distributed samples, so
that
Y
p(i , xi |(n) ).
p(, x|(n) ) =
i
#
"
X Y
(n)
p(i , xi | )
=
i
Y X
p(i , xi |(n) ) .
=
i
You should check the last step; one natural thing to do is check with N = 2 and
t = 2. This means that we can write
p(|(n) , x)
=
=
p(, x|(n) )
p(x|(n) )
Q
(n)
)
i p(i , xi |
i
h
Q P
(n)
i
i p(i , xi | )
Y
i
Y
i
p( , x |(n) )
P i i
(n)
i p(i , xi | )
Section 5.3
so we can write
L(; x, ) =
The EM Algorithm
133
cTi i + K.
!
X X
T
ci i + K p(|(n) , x)
=
i
!
X X
Y
T
ci i + K
=
p(u |(n) , x)
u
i
!
Y
Y
X
T
(n)
T
(n)
c1 1
p(u | , x) + . . . cN N
=
p(u | , x) .
u
u
P
We can simplify further. We have that i p(i |xi , (n) ) = 1, because this is a
probability distribution. Notice that, for any index v,
!
Y
X
X
X Y
, v u,v
X
cTv v p(v |(n) , x)
=
v
So we can write
Q(; (n) ) = L(; x, )p(|(n) , x)
X X
X 1
X
where
wij
Section 5.3
The EM Algorithm
134
Now
p(ij = 1|(n) , x)
=
=
=
=
p(x, ij = 1|(n) )
p(x|(n) )
p(x, ij = 1|(n) )
P
(n) )
l p(x, il = 1|
Q
p(xi , ij = 1|(n) ) u,i p(xu , u |)
Q
P
(n) )
l p(x, il = 1|
u,i p(xu , u |)
p(x , = 1|(n) )
P i ij
(n) )
l p(x, il = 1|
Q
If the last couple of steps puzzle you, remember we obtained p(x, |) = i p(xi , i |).
Also, look closely at the denominator; it expresses the fact that the data must have
come from somewhere. So the main question is to obtain p(x, ij = 1|(n) ). But
p(x, ij = 1|(n) )
Substituting yields
p(ij = 1|
= p(xi , ij |(n) )
exp 21 (xi j )T (xi j ) j
.
, x) = P
1
T
k exp 2 (xi k ) (xi k ) k
X 1
(xi j )T (xi j ) + log j wij + K
Q(; (n) ) =
2
ij
and we have to maximise this with respect to and , and the terms wij are known.
This maximization is easy. We compute
P
xi wij
(n+1)
= Pi
j
i wij
and
i wij
.
N
You should check these expressions by differentiating and setting
P to zero. When you
do so, remember that, because is a probability distribution, j j = 1 (otherwise
youll get the wrong answer).
(n+1)
Section 5.3
The EM Algorithm
135
X
ij
X
ij
("
("
ij p(|(n) , x)
wij
Here the last two steps follow from the same considerations as in the mixture of
normals. The xi and i are IID samples, and so the expectation simplifies as in
that case. If youre uncertain, rewrite the steps of section 5.3.1. The form of this Q
function is the same as that (a sum of cTi i terms, but using a different expression
for ci ). In this case, as above,
wij
Again, we have
p(ij = 1|(n) , x) =
=
p(xi , ij = 1|(n) )
p(xi |(n) )
p(x , = 1|(n) )
P i ij
(n) )
l p(xi , il = 1|
Substituting yields
hQ
i
k
pxj,k
j
i
p(ij = 1|(n) , x) = P hQ
xk
p
l
k l,k l
k
)
("
#
X
X
Q(; (n) ) =
xi,k log pj,k + log j wij
ij
Section 5.3
The EM Algorithm
136
and we have to maximise this with respect to and , and the terms wij are known.
This maximization is easy. We compute
P
xi wij
(n+1)
= Pi
pj
i wij
and
i wij
.
N
You should check these expressions by differentiating and setting to zero.
(n+1)
j
Section 5.4
You should
137
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
118
120
120
120
121
124
125
125
125
125
Section 5.4
You should
138
PROGRAMMING EXERCISES
5.1. Obtain the activities of daily life dataset from the UC Irvine machine learning
website (https://archive.ics.uci.edu/ml/datasets/Dataset+for+ADL+Recognition+with+Wrist-worn+Accelerom
data provided by Barbara Bruno, Fulvio Mastrogiovanni and Antonio Sgorbissa).
(a) Build a classifier that classifies sequences into one of the 14 activities provided. To make features, you should vector quantize, then use a histogram
of cluster centers (as described in the subsection; this gives a pretty explicit set of steps to follow). You will find it helpful to use hierarchical
k-means to vector quantize. You may use whatever multi-class classifier
you wish, though Id start with Rs decision forest, because its easy to
use and effective. You should report (a) the total error rate and (b) the
class confusion matrix of your classifier.
(b) Now see if you can improve your classifier by (a) modifying the number
of cluster centers in your hierarchical k-means and (b) modifying the size
of the fixed length samples that you use.
C H A P T E R
Regression
Classification tries to predict a class from a data item. Regression tries to
predict a value. For example, we know the zip code of a house, the square footage
of its lot, the number of rooms and the square footage of the house, and we wish to
predict its likely sale price. As another example, we know the cost and condition of
a trading card for sale, and we wish to predict a likely profit in buying it and then
reselling it. As yet another example, we have a picture with some missing pixels
perhaps there was text covering them, and we want to replace it and we want
to fill in the missing values. As a final example, you can think of classification as
a special case of regression, where we want to predict either +1 or 1; this isnt
usually the best way to proceed, however. Predicting values is very useful, and so
there are many examples like this.
6.1 OVERVIEW
Some formalities are helpful here. In the simplest case, we have a dataset consisting
of a set of N pairs (xi , yi ). We think of yi as the value of some function evaluated
at xi , but with some random component. This means there might be two data
items where the xi are the same, and the yi are different. We refer to the xi as
explanatory variables and the yi is a dependent variable. We want to use the
examples we have the training examples to build a model of the dependence
between y and x. This model will be used to predict values of y for new values of
x, which are usually called test examples.
We do not guarantee that different values of x produce different values of y.
Data just isnt like this (see the crickets example Figure 6.1). Traditionally, regression produces some representation of a probability distribution for y conditioned on
x, so that we would get (say) some representation of a distribution on the houses
likely sale value. The best prediction would then be the expected value of that
distribution. Usually the representation is in the form of variance estimates. One
common approach is to assume that the distribution on every prediction is normal,
with a constant variance that is estimated from data. Another common approach
is to estimate a variance at every prediction point.
It should be clear that none of this will work if there is not some relationship
between the training examples and the test examples. If I collect training data
on the height and weight of children, Im unlikely to get good predictions of the
weight of adults from their height. We can be more precise with a probabilistic
framework. We think of xi as IID samples from some (usually unknown) probability
distribution P (X). Then the test examples should also be IID samples from P (X).
A probabilistic formalism can help be precise about the yi , too. Assume another
random variable Y has joint distribution with X given by P (Y, X). We think of
each yi as a sample from P (Y | {X = xi }). Then our modelling problem would be:
given the training data, build a model that takes a test example x and yields a
139
Section 6.2
140
model of P (Y | {X = xi }).
To do anything useful with this formalism requires some aggressive simplifying assumptions. There are very few circumstances that require a comprehensive
representation of P (Y | {X = xi }). Usually, we are interested in E[Y | {X = xi }]
(the mean of P (Y | {X = xi })) and in var ({P (Y | {X = xi })}).
To recover this representation, we assume that, for any pair of examples (x, y),
the value of y is obtained by applying some (unknown) function f to x, then adding
some random variable with zero mean. We can write y(x) = f (x) + , though its
worth remembering that there can be many different values of y associated with a
single x. Now we must make some estimate of f which yields E[Y | {X = xi }]
and estimate the variance of . The variance of might be constant, or might
vary with x.
There is a very widespread collection of regression methods. We will see a
subset here. It can be extremely useful to have variances or confidence intervals
associated with predictions (for example, in the house price case it could offer a
guideline as to the spread of likely bids), but it isnt always essential. In this chapter,
I will discuss both methods that are engineered to produce predictive distributions
and methods that ignore these distributions. Cross-validation ideas can be used to
obtain estimates of predictive variance from almost anything.
Section 6.2
141
to different ys. One way that this could occur is that y is a measurement (and so
subject to some measurement noise). Another is that there is some randomness in
y. For example, we expect that two houses with the same set of features (the x)
might still sell for different prices (the ys).
A good, simple model is to assume that the dependent variable (i.e. y) is
obtained by evaluating a linear function of the explanatory variables (i.e. x), then
adding a zero-mean normal random variable. We can write this model as
y = xT +
where is a zero mean normal random variable with unknown variance. In this
expression, is a vector of weights, which we must estimate. When we use this
model to predict a value of y for a particular set of explanatory variables x , we
cannot predict the value that will take. Our best available prediction is the mean
value (which is zero). Notice that if x = 0, the model predicts y = 0. This may
seem like a problem to you you might be concerned that we can fit only lines
through the origin but remember that x contains explanatory variables, and we
can choose what appears in x. The two examples show how a sensible choice of x
allows us to fit a line with an arbitrary y-intercept.
Section 6.2
800
90
1000
142
R^2=0.68
80
Temperature
600
0
70
200
75
400
Weight (gr)
85
R^2=0.87
10
20
30
Length (cm)
40
14
15
16
17
18
19
20
Frequency
FIGURE 6.1: On the left, a regression of weight against length for perch from a
Finnish lake (you can find this dataset, and the back story at http:// www.amstat.
org/ publications/ jse/ jse data archive.htm; look for fishcatch on that page). Notice that the linear regression fits the data fairly well, meaning that you should be
able to predict the weight of a perch from its length fairly well. On the right, a
regression of air temperature against chirp frequency for crickets. The data is fairly
close to the line, meaning that you should be able to tell the temperature from the
pitch of crickets chirp fairly well. This data is from http:// mste.illinois.edu/ patel/
amar430/ keyprob1.html. The R2 you see on each figure is a measure of the goodness
of fit of the regression (section 6.2.2).
Section 6.2
80
85
90
100
143
R^2=0.06
75
70
60
20
60
65
40
Lifespan
80
R^2=0.41
0.65
0.70
0.75
0.80
0.85
0.90
0.95
97
98
99
100
Temperature (F)
FIGURE 6.2: Regressions do not necessarily yield good predictions or good model fits.
On the left, a regression of the lifespan of female fruitflies against the length of
their torso as adults (apparently, this doesnt change as a fruitfly ages; you can
find this dataset, and the back story at http:// www.amstat.org/ publications/ jse/
jse data archive.htm; look for fruitfly on that page). The figure suggests you can
make some prediction of how long your fruitfly will last by measuring its torso, but
not a particularly accurate one. On the right, a regression of heart rate against
body temperature for adults. You can find the data at http:// www.amstat.org/
publications/ jse/ jse data archive.htm as well; look for temperature on that page.
Notice that predicting heart rate from body temperature isnt going to work that well,
either.
, which we dont know, but will not need to worry about right now. We have that
X
log P (yi |xi , )
log L() =
i
1 X
(yi xTi )2 + term not depending on
2 2 i
Section 6.2
the vector
144
y1
y2
...
yn
xT1
xT2 .
. . . xTn
1
N
y X )T (y X
X T X X T y = 0.
For reasonable choices of features, we could expect that X T X which should
strike you as being a lot like a covariance matrix has full rank. If it does, which
is the usual case, this equation is easy to solve. If it does not, there is more to do,
which we will do in section 6.4.2.
Regress the hormone data against time for all the devices in the Efron example.
Solution: This example is mainly used to demonstrate how to regress in R.
There is sample code in listing 6.1. The summary in the listing produces a
great deal of information (try it). Most of it wont mean anything to you yet.
You can get a figure by doing plot(foo.lm), but these figures will not mean
anything yet, either. In the code, Ive shown how to plot the data and a line
on top of it.
Section 6.2
145
Listing 6.1: R code used for the linear regression example of worked example 6.1
e f d<read . table ( e f r o n t a b l e . t x t , h e a d e r=TRUE)
# t h e t a b l e has t h e form
#N1
Ah
Bh
Ch N2 At Bt Ct
# now we need t o c o n s t r u c t a new d a t a s e t
hor<s t a c k ( e f d , s e l e c t = 2 : 4 )
tim<s t a c k ( e f d , s e l e c t = 6 : 8 )
f o o<data . frame ( time=tim [ , c ( v a l u e s ) ] ,
hormone=hor [ , c ( v a l u e s ) ] )
f o o . lm<lm( hormonetime , data=f o o )
plot ( f o o )
abline ( f o o . lm)
eT e
N
and this gives the average of the squared error of prediction on the training examples.
Section 6.2
146
y1
y2
...
yn
and X for the matrix
xT1
xT2 .
. . . xTn
e = y X .
We have that eT 1 = 0. The mean square error is given by
m=
eT e
.
N
Notice that the mean squared error is not a great measure of how good the
regression is. This is because the value depends on the units in which the dependent
variable is measured. So, for example, if you measure y in meters you will get a
different mean squared error than if you measure y in kilometers.
There is an important quantitative measure of how good a regression is which
doesnt depend on units. Unless the dependent variable is a constant (which would
make prediction easy), it has some variance. If our model is of any use, it should
explain some aspects of the value of the dependent variable. This means that
the variance of the residual should be smaller than the variance of the dependent
variable. If the model made perfect predictions, then the variance of the residual
should be zero.
Section 6.2
147
and so on for var[ei ], etc. Notice from the facts that y = X . Now
T
var[y] = (1/N ) X X + [e e]
X X + [e e]
T
T
T
X X + 2 [e e] X X + [e e] [e e]
= (1/N ) X X
T
T
= (1/N ) X X
X X + [e e] [e e]
=
Section 6.2
148
You can check if the regression predicts a constant. This is usually a bad
sign. You can check this by looking at the predictions for each of the training data
Section 6.2
149
Remember this: Linear regressions can make bad predictions. You can
check for trouble by: evaluating R2 ; looking at a plot; looking to see if the
regression makes a constant prediction; or checking whether the residual is
random. Other strategies exist, but are beyond the scope of this book.
8
6
4
10000
8000
6000
4000
0
2000
Number of appearances
12000
14000
20
40
60
Rank
80
100
Log rank
FIGURE 6.3: On the left, word count plotted against rank for the 100 most common
words in Shakespeare, using a dataset that comes with R (called bard, and quite
likely originating in an unpublished report by J. Gani and I. Saunders). I show a
regression line too. This is a poor fit by eye, and the R2 is poor, too (R2 = 0.1). On
the right, log word count plotted against log rank for the 100 most common words
in Shakespeare, using a dataset that comes with R (called bard, and quite likely
originating in an unpublished report by J. Gani and I. Saunders). The regression
line is very close to the data.
Section 6.2
150
frequencies. Some words are used very often in text; most are used seldom. The
dataset for this figure consists of counts of the number of time a word occurred
for the 100 most common words in Shakespeares printed works. It was originally
collected from a concordance, and has been used to attack a variety of interesting
questions, including an attempt to assess how many words Shakespeare knew. This
is hard, because he likely knew many words that he didnt use in his works, so
one cant just count. If you look at the plot of Figure 6.3, you can see that a
linear regression of count (the number of times a word is used) against rank (how
common a word is, 1-100) is not really useful. The most common words are used
very often, and the number of times a word is used falls off very sharply as one
looks at less common words. You can see this effect in the scatter plot of residual
against dependent variable in Figure 6.3 the residual depends rather strongly
on the dependent variable. This is an extreme example that illustrates how poor
linear regressions can be.
However, if we regress log-count against log-rank, we get a very good fit
indeed. This suggests that Shakespeares word usage (at least for the 100 most
common words) is consistent with Zipf s law. This gives the relation between
frequency f and rank r for a word as
f
1s
r
Remember this:
The performance of a regression can be improved by
transforming variables. Transformations can follow from looking at plots,
or thinking about the logic of the problem
The Box-Cox transformation is a method that can search for a transformation of the dependent variable that improves the regression. The method uses a
one-parameter family of transformations, with parameter , then searches for the
best value of this parameter using maximum likelihood. A clever choice of transformation means that this search is relatively straightforward. We define the Box-Cox
transformation of the dependent variable to be
(
yi 1
(bc)
if 6= 0 .
yi =
log yi if = 0
Section 6.3
80
Temperature
600
0
70
200
75
400
Weight (gr)
85
800
90
1000
151
10
20
30
40
Length (cm)
14
15
16
17
18
19
20
Frequency
FIGURE 6.4: The Box-Cox transformation suggests a value of = 0.303 for the
regression of weight against height for the perch data of Figure 6.1. You can
find this dataset, and the back story at http:// www.amstat.org/ publications/ jse/
jse data archive.htm; look for fishcatch on that page). On the left, a plot of the
resulting curve overlaid on the data. For the cricket temperature data of that figure (from http:// mste.illinois.edu/ patel/ amar430/ keyprob1.html), the transformation suggests a value of = 4.75. On the right, a plot of the resulting curve
overlaid on the data.
It turns out to be straightforward to estimate a good value of using maximum
likelihood. One searches for a value of that makes residuals look most like a
normal distribution. Statistical software will do it for you; the exercises sketch
out the method. This transformation can produce significant improvements in a
regression. For example, the transformation suggests a value of = 0.303 for
the fish example of Figure 6.1. It isnt natural to plot weight0.303 against height,
because we dont really want to predict weight0.303 . Instead, we plot the predictions
of weight that come from this model, which will lie on a curve with the form
1
(ax + b) 0.303 , rather than on a straight line. Similarly, the transformation suggests
a value of = 0.475 for the cricket data. Figure 6.4 shows the result of these
transforms.
6.3 FINDING PROBLEM DATA POINTS
Outlying data points can significantly weaken the usefulness of a regression. For
some regression problems, we can identify data points that might be a problem, and
then resolve how to deal with them. One possibility is that they are true outliers
someone recorded a data item wrong, or they represent an effect that just doesnt
occur all that often. Another is that they are important data, and our linear model
may not be good enough. If the data points really are outliers, we can ignore them;
if they arent, we may be able to improve the regression by transforming features
or by finding a new explanatory variable.
152
nyv
20
40
40
20
0
40
20
yv
20
40
Section 6.3
40
20
0
xv
20
40
40
20
20
40
nxv
FIGURE 6.5: On the left, a synthetic dataset with one independent and one explanatory variable, with the regression line plotted. Notice the line is close to the data
points, and its predictions seem likely to be reliable. On the right, the result of
adding a single outlying datapoint to that dataset. The regression line has changed
significantly, because the regression line tries to minimize the sum of squared vertical distances between the data points and the line. Because the outlying datapoint is
far from the line, the squared vertical distance to this point is enormous. The line
has moved to reduce this distance, at the cost of making the other points further
from the line.
i (yi
Section 6.3
153
50
Residuals
250
100
50
150
200
Weight
100
300
150
350
40
50
60
70
80
100
150
200
Height
Fitted values
250
50
Residuals
250
100
50
150
200
Weight
100
300
150
350
30
30
40
50
Height
60
70
80
100
150
200
250
Fitted values
FIGURE 6.6: On the top left, weight regressed against height for the bodyfat dataset.
The line doesnt describe the data particularly well, because it has been strongly
affected by a few data points (filled-in markers). On the top right, a scatter plot
of the residual against the value predicted by the regression. This doesnt look like
noise, which is a sign of trouble. On the bottom left, weight regressed against
height for the bodyfat dataset. I have now removed the four suspicious looking data
points with filled-in markers; these seemed the most likely to be outliers. On the top
right, a scatter plot of the residual against the value predicted by the regression.
Notice that the residual looks like noise. The residual seems to be uncorrelated to the
predicted value; the mean of the residual seems to be zero; and the variance of the
residual doesnt depend on the predicted value. All these are good signs, consistent
with our model, and suggest the regression will yield good predictions.
Remember this:
Outliers can affect linear regressions significantly.
Usually, if you can plot the regression, you can look for outliers by eyeballing
the plot. Other methods exist, but are beyond the scope of this text.
Section 6.3
154
yp = (X X T X
1
X T )y.
What this means is that the values the model predicts at training points are a linear
1 T
function of the true values at the training points. The matrix (X X T X
X ) is
sometimes called the hat matrix. The hat matrix is written H, and I shall write
the i, jth component of the hat matrix hij .
The hat matrix has a variety of important properties. I wont prove any here,
but the proofs are in the exercises. It is a symmetric matrix. The eigenvalues can
be only 1 or 0. And the row sums have the important property that
X
h2ij 1.
j
This is important, because it can be used to find data points that have values that
are hard to predict. The leverage of the ith training point is the ith diagonal
element, hii , of the hat matrix
P H. Now we can write the prediction at the ith
training point yp,i = hii yi + j6=i hij yj . But if hii has large absolute value, then
all the other entries in that row of the hat matrix must have small absolute value.
This means that, if a data point has high leverage, the models value at that point
is predicted almost entirely by the observed value at that point. Alternatively, its
hard to use the other training data to predict a value at that point.
Here is another way to see this importance of hii . Imagine we change the
value of yi by adding ; then yp,i becomes yp,i + hii . In turn, a large value of hii
means that the predictions at the ith point are very sensitive to the value of yi .
6.3.2 Cooks Distance
There are two tools that are simple and effective. One method deletes the ith
point, computes the regression for the reduced data set, then compares the true
value of every other point to the predictions made by the dataset with the ith point
deleted. The score for the comparison is called Cooks distance. If a point has a
large value of Cooks distance, then it has a strong influence on the regression and
might well be an outlier. Typically, one computes Cooks distance for each point,
and takes a closer look at any point with a large value. This procedure is described
in more detail in procedure 6.2
Section 6.3
155
This means we can tell whether a residual is large by standardizing it that is,
dividing by its standard deviation. Write si for the standard residual at the ith
training point. Then we have that
si = r
(eT e)
N
ei
.
(1 hii )
When the regression is behaving, this standard residual should look like a sample
of a standard normal random variable. In turn, this means that if all is going well,
about 66% of the residuals should have values in the range [1, 1], and so on. Large
values of the standard residuals are a sign of trouble.
R produces a nice diagnostic plot that can be used to look for problem data
points (code and details in the appendix). The plot is a scatter plot of the standardized residuals against leverage, with level curves of Cooks distance superimposed.
Figure 6.8 shows an example. Some bad points that are likely to present problems
are identified with a number (you can control how many, and the number, with
arguments to plot; appendix). Problem points will have high leverage and/or high
Section 6.4
156
Frequency
10
20
0
2
Residuals
30
40
100
150
200
Fitted values
250
str
FIGURE 6.7: On the left, standardized residuals plotted against predicted value for
weight regressed against height for the bodyfat dataset. I removed the four suspicious
looking data points, identified in Figure 6.6 with filled-in markers ; these seemed the
most likely to be outliers. You should compare this plot with the residuals in figure
6.6, which are not standardized. On the right, a histogram of the residual values.
Notice this looks rather like a histogram of a standard normal random variable,
though there are slightly more large positive residuals than one would like. This
suggests the regression is working tolerably.
Cooks distance and/or high residual. The figure shows this plot for three different versions of the dataset (original; two problem points removed; and two further
problem points removed).
6.4 MANY EXPLANATORY VARIABLES
In earlier sections, I implied you could put anything into the explanatory variables.
This is correct, and makes it easy to do the math for the general case. However,
I have plotted only cases where there was one explanatory variable (together with
a constant, which hardly counts). In some cases (section 6.4.1), we can add explanatory variables and still have an easy plot. Other cases are much harder to
plot successfully, and one needs better ways to visualize the regression than just
plotting.
Assume there is more than one explanatory variable and its tough to plot
the regression nicely. The value of R2 is still a useful guide to the goodness of the
regression. A useful plot, which can offer a lot of insight, is to plot the value of
the residual against the value predicted by the model. We have already used this
plot to track down suspicious data points (Figures 6.6 and 6.6). Generally, we look
for a tendency of the residual not to look like noise. It turns out that the residual
at each example can have different variances in a predictable way, so to produce a
helpful plot, we need to standardize the residuals (section 6.3.1).
Section 6.4
157
Residuals vs Leverage
39
4
2
1
0.5
Standardized residuals
42
0.5
Cooks distance
0.0
0.1
0.2
0.3
0.4
0.5
Leverage
lm(WEIGHT ~ HEIGHT)
Residuals vs Leverage
3
Residuals vs Leverage
41
0.1
36
216
1
0
Standardized residuals
2
1
0.05
Standardized residuals
0.05
0.1
145
0.05
0.05
0.1
0.1
Cooks distance
0.00
0.01
Cooks distance
0.02
0.03
Leverage
lm(WEIGHT ~ HEIGHT)
0.00
0.01
0.02
0.03
Leverage
lm(WEIGHT ~ HEIGHT)
Section 6.4
158
model to incorporate other functions of the length. In fact, its quite suprising that
the weight of a fish should be predicted by its length. If the fish doubled in each
direction, say, its weight should go up by a factor of eight. The success of our
regression suggests that fish do not just scale in each direction as they grow. But
we might try the model yi = 2 x2i + 1 xi + 0 + i . This is easy to do. The ith
row of the matrix X currently looks like [xi , 1]. We build a new matrix X (b) , where
the ith row is [x2i , xi , 1], and proceed as before. This gets us a new model. The
nice thing about this model is that it is easy to plot our predicted weight is still
a function of the length, its just not a linear function of the length. Several such
models are plotted in Figure 6.9.
1200
1000
800
0
1, x
400
1, x, x^2
1, x, x^2, x^3
"Data"
Weight
Weight
"Data"
1, ..., x^10
Data
Data
1000
400
10
20
30
Length
40
10
20
30
40
Length
FIGURE 6.9: On the left, several different models predicting fish weight from length.
The line uses the explanatory variables 1 and xi ; and the curves use other monomials in xi as well, as shown by the legend. This allows the models to predict curves
that lie closer to the data. It is important to understand that, while you can make
a curve go closer to the data by inserting monomials, that doesnt mean you necessarily have a better model. On the right, I have used monomials up to x10
i . This
curve lies very much closer to the data points than any on the other side, at the
cost of some very odd looking wiggles inbetween data points. I cant think of any
reason that these structures would come from true properties of fish, and it would
be hard to trust predictions from this model.
You should notice that it can be quite easy to add a lot of functions like this
(in the case of the fish, I tried x3i as well). However, its hard to decide whether the
regression has actually gotten better. The least-squares error on the training data
will never go up when you add new explanatory variables, so the R2 will never get
worse. This is easy to see, because you could always use a coefficient of zero with the
new variables and get back the previous regression. However, the models that you
choose are likely to produce worse and worse predictions as you add explanatory
variables. Knowing when to stop can be tough (Section 7.1), though its sometimes
obvious that the model is untrustworthy (Figure 6.9).
Section 6.4
159
Remember this: If you have only one measurement, you can construct
a high dimensional x by using functions of that measurement. This produces
a regression that has many explanatory variables, but is still easy to plot.
Knowing when to stop is hard. An understanding of the problem is helpful.
Here > 0 is a constant that weights the two requirements (small error; small )
relative to one another. Notice also that dividing the total error by the number of
data points means that our choice of shouldnt be affected by changes in the size
of the data set.
We choose in the same way we used for classification; split the training set
into a training piece and a validation piece, train for different values of , and test
the resulting regressions on the validation piece. The error is a random variable,
random because of the random split. It is a fair model of the error that would occur
on a randomly chosen test example (assuming that the training set is like the
test set, in a way that I do not wish to make precise yet). We could use multiple
splits, and average over the splits. Doing so yields both an average error for a value
of and an estimate the standard deviation of error. Figure 6.10 shows the result
of doing so for the bodyfat dataset, both with and without outliers. Notice that
now there is now no that yields the smallest validation error, because the value
Section 6.4
160
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
15
all data
600
400
200
400
600
MeanSquared Error
800
800
200
MeanSquared Error
15
1000
15
1000
15
6
log(Lambda)
10
10
log(Lambda)
FIGURE 6.10: Plots of mean-squared error as a function of log regularization parameter (i.e. log ) for a regression of weight against all variables for the bodyfat
dataset. These plots show mean-squared error averaged over cross-validation folds
with a vertical one standard deviation bar. On the left, the plot for the dataset with
the six outliers identified in Figure 14.5 removed. On the right, the plot for the
whole dataset. Notice how the outliers increase the variability of the error, and the
best error.
Section 6.4
161
Remember this:
The performance of a regression can be improved by
regularizing, particularly if some explanatory variables are correlated. The
procedure is similar to that used for classification.
10
5
0
Standardized residuals
15
10
5
0
5
15
10
Standardized residuals
10
15
15
100
150
200
250
Fitted values
300
350
100
150
200
250
300
350
Fitted values
FIGURE 6.11: On the left, standardized residuals plotted against predicted value
for weight regressed against all variables for the bodyfat dataset. Four data points
appear suspicious, and I have marked these with a filled in marker. On the right,
standardized residuals plotted against predicted value for weight regressed against
all variables for the bodyfat dataset, but with the four suspicious looking data points
removed. Notice two other points stick out markedly.
Regressing weight against all variables is more interesting, because you cant
plot the function easily. In this case, you need to rely on the residual plots. Figure 6.11 shows the standardized residual plotted against predicted value for a regression of weight against all other variables. There is clearly a problem here; the
residual seems to depend quite strongly on the predicted value. Removing the four
Section 6.4
162
outliers we have already identified leads to a much improved plot, also shown in
Figure 6.11. This is banana-shaped, which is suspicious. There are two points that
seem to come from some other model (one above the center of the banana, one
below). This is likely because, for these points, some variables other than height
have an odd relationship with weight. Removing these points gives the residual
plot shown in Figure 6.12.
The banana shape of this plot is a suggestion that some non-linearity somewhere would improve the regression. One option is a non-linear transformation of
the independent variables. Finding the right one might require some work, so its
natural to try a Box-Cox transformation first. This gives
the best value of the
parameter as 0.5 (i.e. the dependent variable should be weight, which makes the
residuals look much better (Figure 6.12).
Standardized residuals against fitted values,
sqrt(weight) against all,
6 outliers removed
Standardized residuals
5
0
5
15
10
Standardized residuals
10
15
100
150
200
250
Fitted values
300
350
18
20
22
24
26
28
30
Fitted values
FIGURE 6.12: On the left, standardized residuals plotted against predicted value for
weight regressed against all variables for the bodyfat dataset. I removed the four
suspicious data points of Figure 6.11, and the two others identified in that figure.
Notice a suspicious banana shape the residuals are distinctly larger for small and
for large predicted values. This suggests a non-linear transformation of something
might be helpful.
I used a Box-Cox transformation, which suggested a value of 0.5
(i.e. regress 2( weight 1)) against all variables. On the right, the standardized
residuals for this regression. Notice that the banana has gone, though there is
a suspicious tendency for the residuals to be smaller rather than larger. Notice
also the plots are on different axes. Its fair to compare these plots by eye; but its
not fair to compare details, because the residual of a predicted square root means
something different than the residual of a predicted value.
Section 6.5
You should
163
Section 6.5
You should
164
APPENDIX: DATA
Batch A
Amount of Time in
Hormone
Service
25.8
99
20.5
152
14.3
293
23.2
155
20.6
196
31.1
53
20.9
184
20.9
171
30.4
52
Batch B
Amount of Time in
Hormone
Service
16.3
376
11.6
385
11.8
402
32.5
29
32.0
76
18.0
296
24.1
151
26.5
177
25.8
209
Batch C
Amount of Time in
Hormone
Service
28.8
119
22.0
188
29.7
115
28.9
88
32.8
58
32.5
49
25.4
150
31.7
107
28.5
125
TABLE 6.1: A table showing the amount of hormone remaining and the time in
service for devices from lot A, lot B and lot C. The numbering is arbitrary (i.e.
theres no relationship between device 3 in lot A and device 3 in lot B). We expect
that the amount of hormone goes down as the device spends more time in service,
so cannot compare batches just by comparing numbers.
Section 6.5
You should
165
PROBLEMS
Blood pressure against age
Systolic blood pressure
250
200
150
100
0
20
40
60
Age in years
80
FIGURE 6.13: A regression of blood pressure against age, for 30 data points.
6.1. Figure 7.18 shows a linear regression of systolic blood pressure against age.
There are 30 data points.
(a) Write ei = yi xT
i for the residual. What is the mean ({e}) for this
regression?
(b) For this regression, var ({y}) = 509 and the R2 is 0.4324. What is var ({e})
for this regression?
(c) How well does the regression explain the data?
(d) What could you do to produce better predictions of blood pressure (without actually measuring blood pressure)?
6.2. In this exercise, I will show that the prediction process of chapter 12(see
page 251) is a linear regression with two independent variables. Assume we
have N data items which are 2-vectors (x1 , y1 ), . . . , (xN , yN ), where N > 1.
These could be obtained, for example, by extracting components from larger
vectors. As usual, we will write x
i for xi in normalized coordinates, and so on.
The correlation coefficient is r (this is an important, traditional notation).
(a) Show that r = mean ({(x mean ({x}))(y mean ({y}))})/(std (x)std (y)).
std(y)
(b) Now write s = std(x) . Now assume that we have an xo , for which we wish
to predict a y value. Show that the value of the prediction obtained using
the method of page 252 is
sr(x0 mean ({x})) + mean ({y}).
(c) Show that sr = mean ({(xy)}) mean ({x})mean ({y}).
(d) Now write
y1
x1
1
1
y
x
and Y = 2 .
X = 2
... ...
...
xn
1
yn
where X T X = X T Y.
The coefficients of the linear regression will be ,
Show that
T
X X =N
mean x2
mean ({x})
mean ({x})
1
Section 6.5
(e) Now show that var ({x}) = mean
(x mean ({x}))2
You should
= mean
166
2
x
mean ({x})2 .
(f ) Now show that std (x)std (y)corr ({(x, y)}) = mean ({(x mean ({x}))(y mean ({y}))}).
C H A P T E R
where the expectation is taken over training sets and possible choices of x. This
expectation can be written in an extremely useful form. Recall var ({U }) = E U 2
167
Section 7.1
168
pectation is over choices of training data). This is because our training data is a
subset ofh all data,
iand our model is chosen to be good on the training data. The
term E (f f ) is referred to as bias. This term reflects the fact that even the
h i
best choice of model (E f ) may not be the same as the true source of data (E[f ]
Section 7.1
169
and so the model with the lowest training error will tend to be the most complex
model. Training error is a poor guide to test error, because lower training error is
evidence of lower bias on the models part; but with lower bias, we expect to see
greater variance, and the training error doesnt take that into account.
One strategy is to penalize the model for complexity. We add some penalty,
reflecting the complexity of the model, to the training error. We then expect to see
the general behavior of figure ??. The training error goes down, and the penalty
goes up as the model gets more complex, so we expect to see a point where the sum
is at a minimum.
There are a variety of ways of constructing penalties. AIC (short for An
Information Criterion) is a method due originally to Akaike, in ****. Rather than
using the training error, AIC uses the maximum value of the log-likelihood of the
model. Write L for this value. Write k for the number of parameters estimated to
fit the model. Then the AIC is
2k 2L
and a better model has a smaller value of AIC (remember this by remembering
that a larger log-likelihood corresponds to a better model). Estimating AIC is
straightforward for regression models if you assume that the noise is a zero mean
normal random variable. You estimate the mean-squared error, which gives the
variance of the noise, and so the log-likelihood of the model. You do have to keep
track of two points. First, k is the total number of parameters estimated to fit the
model. For example, in a linear regression model, where you model y as xT + ,
you need to estimate d parameters to estimate and the variance of (to get
the log-likelihood). So in this case k = d + 1. Second, log-likelihood is usually
only known up to a constant, so that different software implementations often use
different constants. This is wildly confusing when you dont know about it (why
would AIC and extractAIC produce different numbers on the same model?) but
of no real significance youre looking for the smallest value of the number, and
the actual value doesnt mean anything. Just be careful to compare only numbers
computed with the same routine.
An alternative is BIC (Bayes Information Criterion), given by
2k log N 2L
(where N is the size of the training data set). You will often see this written as
2L 2k log N ; I have given the form above so that one always wants the smaller
value as with AIC. There is a considerable literature comparing AIC and BIC. AIC
has a mild reputation for overestimating the number of parameters required, but is
often argued to have firmer theoretical foundations.
Section 7.1
170
Write
MN for the model that predicts weight from length for the perch dataset
Pj=n
as j=0 j lengthj . Choose an appropriate value of N [1, 10] using AIC and
BIC.
Solution: I used the R functions AIC and BIC, and got the table below.
1
2
3
4
5
6
7
8
9
10
AIC 677 617 617 613 615 617 617 612 613 614
BIC 683 625 627 625 629 633 635 633 635 638
The best model by AIC has (rather startlingly!) N = 8. One should not take
small differences in AIC too seriously, so models with N = 4 and N = 9 are
fairly plausible, too. BIC suggests N = 2.
7.1.3 Cross-Validation
AIC and BIC are estimates of error on future data. An alternative is to measure this
error on held out data, using a cross-validation strategy (as in section 2.1.4). One
splits the training data into F folds, where each data item lies in exactly one fold.
The case F = N is sometimes called leave-one-out cross-validation. One then
sets aside one fold in turn, fitting the model to the remaining data, and evaluating
the model error on the left-out fold. This error is then averaged. Numerous variants
are available, particularly when lots of computation and lots of data are available.
For example: one might not average over all folds; one might use fewer or more
folds; and so on.
Cross-validation
Write
MN for the model that predicts weight from length for the perch dataset
Pj=n
as j=0 j lengthj . Choose an appropriate value of N [1, 10] using leave-oneout cross validation.
Solution: I used the R functions CVlm, which takes a bit of getting used to.
There is sample code in the exercises section. I found:
1
2
3
4
5
6
7
8
1.94e04 4.03e03 7.18e03 4.46e03 5.97e03 5.64e04 1.23e06 4.03e06
where the best model is N = 2.
7.1.4 Forward and Backward Stagewise Regression
Assume we have a set of explanatory variables and we wish to build a model,
choosing some of those variables for our model. Our explanatory variables could
be many distinct measurements, or they could be different non-linear functions of
the same measurement, or a combination of both. We can evaluate models relative
9
3.86e06
10
1.87e08
Section 7.1
171
to one another fairly easily (AIC, BIC or cross-validation, your choice). However,
choosing which set of explanatory variables to use can be quite difficult, because
there are so many sets. Imagine you start with a set of F possible explanatory
variables (including the original measurement, and a constant). You dont know
how many to use, so you might have to try every different group, of each size, and
there are too many. There are two useful alternatives.
In forward stagewise regression, you start with an empty working set
of explanatory variables. You then iterate the following process, which is fairly
obviously a greedy algorithm. For each of the explanatory variables not in working
set, you construct a new model using the working set and that explanatory variable,
and compute the model evaluation score. If the best of these models has a better
score than the model based on the working set, you insert the appropriate variable
into the working set and iterate. If no variable improves the working set, you decide
you have the best model and stop. Backward stagewise regression is pretty
similar, but you start with a working set containing all the variables, and remove
variables one-by-one and greedily. As usual, greedy algorithms are very helpful but
not capable of exact optimization. Each of these strategies can produce rather good
models, but neither is guaranteed to produce the best model.
7.1.5 Dropping Variables with L1 Regularization
We have a large set of explanatory variables, and we would like to choose a small
set that explains most of the variance in the independent variable. We could do
this by encouraging to have many zero entries. In section 6.4.2, we saw we could
regularize a regression by adding a term to the cost function that P
discouraged large
values of . Instead of solving for the value of that minimized i (yi xTi )2 =
(y X )T (y X ) (which I shall call the error cost), we minimized
X
(yi xTi )2 + T = (y X )T (y X ) + T
i
(which I shall call the L2 regularized error). Here > 0 was a constant chosen
by cross-validation. Larger values of encourage entries of to be small, but do
not force them to be zero. The reason is worth understanding.
Write k for the kth component of , and write k for all the other components. Now we can write the L2 regularized error as a function of k :
(a + )k2 2b(k )k + c(k )
where a is a function of the data and b and c are functions of the data and of .
Now notice that
b(k )
.
k =
(a + )
Notice that doesnt appear in the numerator. This means that, to force k to
zero by increasing , we may have to make arbitrarily large. This is because the
improvement in the penalty obtained by going from a small k to k = 0 is tiny
the penalty is proportional to k2 .
Section 7.1
172
(y X )T (y X ) + || ||1
for an appropriate choice of . An equivalent problem is to solve a constrained
minimization problem, where one minimizes
(y X )T (y X ) subject to || ||1 t
where t is some value chosen to get a good result, typically by cross-validation.
There is a relationship between the choice of t and the choice of (with some
thought, a smaller t will correspond to a bigger ) but it isnt worth investigating
in any detail.
Actually solving this system is quite involved, because the cost function is not
differentiable. You should not attempt to use stochastic gradient descent, because
this will not compel zeros to appear in (exercises). There are several methods,
which are beyond our scope. As the value of increases, the number of zeros in
will increase too. We can choose in the same way we used for classification;
split the training set into a training piece and a validation piece, train for different
values of , and test the resulting regressions on the validation piece. However, one
consequence of modern methods is that we can generate a very good approximation
to the path ()
for all values of 0 about as easily as we can choose for a
particular value of .
One way to understand the models that result is to look at the behavior
of cross-validated error as changes. The error is a random variable, random
because of the random split. It is a fair model of the error that would occur on
a randomly chosen test example (assuming that the training set is like the test
set, in a way that I do not wish to make precise yet). We could use multiple splits,
and average over the splits. Doing so yields both an average error for each value
of and an estimate of the standard deviation of error. Figure 7.1 shows the
result of doing so for two datasets. Again, there is no that yields the smallest
validation error, because the value of error depends on the random split crossvalidation. A reasonable choice of lies between the one that yields the smallest
error encountered (one vertical line in the plot) and the largest value whose mean
error is within one standard deviation of the minimum (the other vertical line in
the plot). It is informative to keep track of the number of zeros in as a function
of , and this is shown in Figure 7.1.
Another way to understand the models is to look at how changes as
changes. We expect that, as gets smaller, more and more coefficients become
non-zero. Figure ?? shows plots of coefficient values as a function of log for a
regression of weight against all variables for the bodyfat dataset, penalised using
When
the L1 norm. For different values of , one gets different solutions for .
Section 7.1
14
14
14
13
13
13
10
10
9 7 5 5 4 4 2 1 0
14
14
14
13
13
13
10
10
9 7 5 5 4 4 2 1 0
600
0
200
400
MeanSquared Error
800
800
600
400
200
MeanSquared Error
14
173
1000
1000
14
0
log(Lambda)
log(Lambda)
FIGURE 7.1: Plots of mean-squared error as a function of log regularization parameter (i.e. log ) for a regression of weight against all variables for the bodyfat
dataset. These plots show mean-squared error averaged over cross-validation folds
with a vertical one standard deviation bar. On the left, the plot for the dataset with
the six outliers identified in Figure 14.5 removed. On the right, the plot for the
whole dataset. Notice how the outliers increase the variability of the error, and the
best error. The top row of numbers gives the number of non-zero components in .
Notice how as increases, this number falls. The penalty ensures that explanatory
variables with small coefficients are dropped as gets bigger.
is very large, the penalty dominates, and so the norm of must be small. In
falls
turn, most components of are zero. As gets smaller, the norm of beta
and some components of become non-zero. At first glance, the variable whose
coefficient grows very large seems important. Look more carefully; this is the last
component introduced into the model. But Figure 7.1 implies that the right model
has 7 components. This means that the right model has log 1.3, the vertical
line shown in the detailed figure. In the best model, that coefficient is in fact zero.
The L1 norm can sometimes produce an impressively small model from a
large number of variables. In the UC Irvine Machine Learning repository, there is
a dataset to do with the geographical origin of music (https://archive.ics.uci.edu/
ml/datasets/Geographical+Original+of+Music). The dataset was prepared by Fang
Zhou, and donors were Fang Zhou, Claire Q, and Ross D. King. Further details
appear on that webpage, and in the paper: Predicting the Geographical Origin
of Music by Fang Zhou, Claire Q and Ross. D. King, which appeared at ICDM
in 2014. There are two versions of the dataset. One has 116 explanatory variables
(which are various features representing music), and 2 independent variables (the
latitude and longitude of the location where the music was collected). Figure 7.3
shows the results of a regression of latitude against the independent variables using
L1 regularization. Notice that the model that achieves the lowest cross-validated
prediction error uses only 38 of the 116 variables.
Section 7.1
14
13
11
14
13
11
174
Coefficients
20
10
Coefficients
30
40
10
14
Log Lambda
Log Lambda
weight against all variables for the bodyfat dataset, penalised using the L1 norm. In
each case, the six outliers identified in Figure 14.5 were removed. On the left, the
plot of the whole path for each coefficient (each curve is one coefficient). On the
right, a detailed version of the plot. The vertical line shows the value of log the
produces the model with smallest cross-validated error (look at Figure 7.1). Notice
that the variable that appears to be important, because it would have a large weight
with = 0, does not appear in this model.
Regularizing a regression with the L1 norm is sometimes known as a lasso. A
nuisance feature of the lasso is that, if several explanatory variables are correlated,
it will tend to choose one for the model and omit the others (example in exercises).
This can lead to models that have worse predictive error than models chosen using
the L2 penalty. One nice feature of good minimization algorithms for the lasso is
that it is easy to use both an L1 penalty and an L2 penalty together. One can form
!
X
(1 )
1
2
T
2
+
(yi xi )
|| ||2 + || ||1
N
2
i
Error +
Regularizer
where one usually chooses 0 1 by hand. Doing so can both discourage large
values in and encourage zeros. Penalizing a regression with a mixed norm like this
is sometimes known as elastic net. It can be shown that regressions penalized with
elastic net tend to produce models with many zero coefficients, while not omitting
correlated explanatory variables. All the computation can be done by the glmnet
package in R (see exercises for details).
7.1.6 Using Regression to Compare Trends
Regression isnt only used to predict values. Another reason to build a regression
model is to compare trends in data. Doing so can make it clear what is really hap-
Section 7.1
88
77
57
15
Coefficients
320
10
280
300
MeanSquared Error
340
10
360
88 88 88 85 83 76 74 72 61 48 28 20 15 13 6 3 0
175
log(Lambda)
Log Lambda
Mean-squared error as a function of log regularization parameter (i.e. log ) for a regression of latitude against features describing music (details in text), using the dataset at https:// archive.ics.uci.edu/ ml/ datasets/
Geographical+Original+of+Music and penalized with the L1 norm. The plot on the
left shows mean-squared error averaged over cross-validation folds with a vertical
one standard deviation bar. The top row of numbers gives the number of non-zero
Notice how as increases, this number falls. The penalty ensures
components in .
that explanatory variables with small coefficients are dropped as gets bigger. On
the right, a plot of the coefficient values as a function of log for the same regression. The vertical line shows the value of log the produces the model with smallest
cross-validated error. Only 38 of 116 explanatory variables are used by this model.
FIGURE 7.3:
Section 7.1
40
35
B CC B
C
A
A
30
C CCC
A
25
20
15
50
B
C
BA
C
A A AA
100
150
Time in service
200
Residual
Amount of hormone
176
2
0 B
C
C B
C
A
A
C
A
B
B
C
CC
C
B
A
C
AA
A
50
100
150
Time in service
200
FIGURE 7.4: On the left, a scatter plot of hormone against time for devices from
tables 7.1 and 7.1. Notice that there is a pretty clear relationship between time and
amount of hormone (the longer the device has been in service the less hormone there
is). The issue now is to understand that relationship so that we can tell whether lots
A, B and C are the same or different. The best fit line to all the data is shown as
well, fitted using the methods of section 6.2. On the right, a scatter plot of residual
the distance between each data point and the best fit line against time for the
devices from tables 7.1 and 7.1. Now you should notice a clear difference; some
devices from lots B and C have positive and some negative residuals, but all lot
A devices have negative residuals. This means that, when we account for loss of
hormone over time, lot A devices still have less hormone in them. This is pretty
good evidence that there is a problem with this lot.
This plot allows us to ask whether any particular batch behaves differently from
the overall model in any interesting way.
However, it is hard to evaluate the distances between data points and the best
fitting line by eye. A sensible alternative is to subtract the amount of hormone
predicted by the model from the amount that was measured. Doing so yields a
residual the difference between a measurement and a prediction. We can then
plot those residuals (Figure 7.4). In this case, the plot suggests that lot A is special
all devices from this lot contain less hormone than our model predicts.
7.1.7 Significance: What Variables are Important?
Imagine you regress some measure of risk of death against blood pressure, whether
someone smokes or not, and the length of their thumb. Because high blood pressure
and smoking tend to increase risk of death, you would expect to see large coefficients for these explanatory variables. Since changes in the thumb length have no
effect, you would expect to see small coefficients for these explanatory variables.
This suggests a regression can be used to determine what effects are important in
building a model.
One difficulty is the result of sampling variance. Imagine that we have an
explanatory variable that has absolutely no relationship to the dependent variable.
If we had an arbitrarily large amount of data, and could exactly identify the correct
Section 7.1
177
model, wed find that, in the correct model, the coefficient of that variable was zero.
But we dont have an arbitrarily large amount of data. Instead, we have a sample
of data. Hopefully, our sample is random, so that the reasoning of section 14.5 can
be applied. Using that reasoning, our estimate of the coefficient is the value of a
random variable whose expected value is zero, but whose variance isnt. As a result,
we are very unlikely to see a zero. This reasoning applies to each coefficient of the
model. To be able to tell which ones are small, we would need to know the standard
deviation of each, so we can tell whether the value we observe is a small number of
standard deviations away from zero. This line of reasoning is very like hypothesis
testing. It turns out that the sampling variance of regression coefficients can be
estimated in a straightforward way. In turn, we have an estimate of the extent
to which their difference from zero could be a result of random sampling. R will
produce this information routinely; use summary on the output of lm.
A second difficulty has to do with practical significance, and is rather harder.
We could have explanatory variables that are genuinely linked to the independent
variable, but might not matter very much. This is a common phenomenon, particularly in medical statistics. It requires considerable care to disentangle some of these
issues. Here is an example. Bowel cancer is an unpleasant disease, which could kill
you. Being screened for bowel cancer is at best embarrassing and unpleasant, and
involves some startling risks. There is considerable doubt, from reasonable sources,
about whether screening has value and if so, how much (as a start point, you could
look at Ransohoff DF. How Much Does Colonoscopy Reduce Colon Cancer Mortality?. Ann Intern Med. 2009). There is some evidence linking eating red or
processed meat to incidence of bowel cancer. A good practical question is: should
one abstain from eating red or processed meat based on increased bowel cancer
risk?
Coming to an answer is tough; the coefficient in any regression is clearly
not zero, but its pretty small as these numbers indicate. The UK population in
2012 was 63.7 million (this is a summary figure from Google, using World Bank
data; theres no reason to believe that its significantly wrong). I obtained the
following figures from the UK cancer research institute website, at http://www.
cancerresearchuk.org/health-professional/cancer-statistics/statistics-by-cancer-type/bowel-cancer.
There were 41, 900 new cases of bowel cancer in the UK in 2012. Of these cases,
43% occurred in people aged 75 or over. 57% of people diagnosed with bowel cancer
survive for ten years or more after diagnosis. Of diagnosed cases, an estimated 21%
are linked to eating red or processed meat, and the best current estimate is that
the risk of incidence is between 17% and 30% higher per 100g of red meat eaten
per day (i.e. if you eat 100g of red meat per day, your risk increases by some number between 17% and 30%; 200g a day gets you twice that number; and rather
roughly so on). These numbers are enough to confirm that there is a non-zero
coefficient linking the amount of red or processed meat in your diet with your risk
of bowel cancer (though youd have a tough time estimating the exact value of that
coefficient from the information here). If you eat more red meat, your risk really
will go up. But that coefficient is clearly pretty small, because the incidence is
about 1 in 1500 per year. Does it matter? you get to choose, and your choice has
consequences.
Section 7.2
Robust Regression
178
Section 7.2
Robust Regression
179
For small values of u, this looks like u2 ; for large values of u, it looks like 1. The
parameter controls the point at which the function flattens out, and we have
plotted a variety of examples in Figure ??.
Generally, M-estimators are discussed in terms of their influence function.
This is
.
u
Our miniIts importance becomes evidence when we consider algorithms to fit .
mization criterion is
!
X
X
T
(yi xi ; )
=
(xi )
u
i
i
=
0.
yi xTi
(where the partial derivative is evaluated at the value of the residual). We can
write the minimization criterion as
X
[wi ()] (yi xTi )(xi ) = 0.
i
wi
= wi ((n) ) =
yi
u
xTi (n)
Section 7.2
Robust Regression
180
350
250
150
200
Weight
300
30
40
50
60
70
Height
FIGURE 7.5: Comparing three different linear regression strategies on the bodyfat
data, regressing weight against height. Notice that using a robust regression gives
an answer very like that obtained by rejecting outliers by hand. The answer may
well be better because it isnt certain that each of the four points rejected is an
outlier, and the robust method may benefit from some of the information in these
points. I tried a range of scales for the Huber loss (the k2 parameter), but found
no difference in the line resulting over scales varying by a factor of 1e4, which is
why I plot only one scale.
fit some to these points, then use the result as a start point. If we do this often
enough, one of the start points will be an estimate that is not contaminated by
outliers.
The estimators require a sensible estimate of , which is often referred to as
scale. Typically, the scale estimate is supplied at each iteration of the solution
method. One reasonable estimate is the MAD or median absolute deviation,
given by
(n)
(n) = 1.4826 mediani | ri (xi ; (n1) ) |.
Another a popular estimate of scale is obtained with Hubers proposal 2 (that
is what everyone calls it!). Choose some constant k1 > 0, and define (u) =
2
min (| u |, k1 ) . Now solve the following equation for :
X
i
(n)
ri (xi ; (n1)
) = N k2
where k2 is another constant, usually chosen so that the estimator gives the right answer
for a normal distribution (exercises). This equation needs to be solved with an iterative
method; the MAD estimate is the usual start point. R provides hubers, which will compute
this estimate of scale (and figures out k2 for itself). The choice of k1 depends somewhat on
how contaminated you expect your data to be. As k1 , this estimate becomes more
like the standard deviation of the data.
Section 7.2
181
60
Frequency
20
20
20
40
40
Residual
60
80
80
100
100
120
120
Robust Regression
100
150
200
250
300
Fitted value
350
50
100
Residual
FIGURE 7.6: A robust linear regression of weight against all variables for the bodyfat
dataset, using the Huber loss and all data points. On the left, residual plotted
against fitted value (the residual is not standardized). Notice that there are some
points with very large residual, but most have much smaller residual; this wouldnt
happen with a squared error. On the right, a histogram of the residual. If one
ignores the extreme residual values, this looks normal. The robust process has been
able to discount the effect of the outliers, without us needing to identify and reject
outliers by hand.
Section 7.2
Robust Regression
182
Determine:
nthe smallest number of points required (e.g., for lines, n = 2,
for circles, n = 3)
kthe number of iterations required
tthe threshold used to identify a point that fits well
dthe number of nearby points required
to assert a model fits well
Until k iterations have occurred
Draw a sample of n points from the data
uniformly and at random
Fit to that set of n points
For each data point outside the sample
Test the distance from the point to the structure
against t; if the distance from the point to the structure
is less than t, the point is close
end
If there are d or more points close to the structure
then there is a good fit. Refit the structure using all
these points. Add the result to a collection of good fits.
end
Use the best fit from this collection, using the
fitting error as a criterion
Algorithm 7.1: RANSAC: Fitting Structures Using Random Sample Consensus.
the dataset. Each sample contains the minimum number of points required to fit
the abstraction of interest. For example, if we wish to fit lines, we draw pairs of
points; if we wish to fit circles, we draw triples of points, and so on. We assume
that we need to draw n data points, and that w is the fraction of these points that
are good (we need only a reasonable estimate of this number). Now the expected
value of the number of draws k required to get one point is given by
E[k] = 1P (one good sample in one draw) +
2P (one good sample in two draws) + . . .
= wn + 2(1 wn )wn + 3(1 wn )2 wn + . . .
= wn
(where the last step takes a little manipulation of algebraic series). We would like
to be fairly confident that we have seen a good sample, so we wish to draw more
than wn samples; a natural thing to do is to add a few standard deviations to this
number. The standard deviation of k can be obtained as
1 wn
.
SD(k) =
wn
An alternative approach to this problem is to look at a number of samples that
Section 7.3
183
guarantees a low probability z of seeing only bad samples. In this case, we have
(1 wn )k = z,
which means that
log(z)
.
log(1 wn )
It is common to have to deal with data where w is unknown. However, each fitting
attempt contains information about w. In particular, if n data points are required,
then we can assume that the probability of a successful fit is wn . If we observe
a long sequence of fitting attempts, we can estimate w from this sequence. This
suggests that we start with a relatively low estimate of w, generate a sequence
of attempted fits, and then improve our estimate of w. If we have more fitting
attempts than the new estimate of w predicts, the process can stop. The problem
of updating the estimate of w reduces to estimating the probability that a coin
comes up heads or tails given a sequence of fits.
k=
Section 7.3
184
L
a
t
i
t
u
d
e
Catch score 1
3
2
1
0
-11.1
142.8
-1
-11
-11.5
-12
144
143.5
143
Longitude
143.9
142.5
FIGURE 7.7: A dataset recording scores of prawn trawls around the Great Barrier
Reef, from http:// www.statsci.org/ data/ oz/ reef.html. There are two scores; this is
score 1. On the left I have plotted the data as a 3D scatter plot. This form of plot
isnt usually very successful, though it helps to make it slightly easier to read if one
supplies vertical lines from each value to zero, and a zero surface. On the right, a
heat map of this data, made by constructing a fine grid, then computing the average
score for each grid box (relatively few boxes get one score, and even fewer get two).
The brightest point corresponds to the highest score; mid-grey is zero (most values),
and dark points are negative. Notice that the scale is symmetric; the reason there is
no very dark point is that the smallest value is considerably larger than the negative
of the largest value. The x and y dimensions are longitude and latitude, and I have
ignored the curvature of the earth, which is pretty small at this scale.
important subcases: interpolation, where we want a surface that passes through
each value; and smoothing, where our surface should be close to the values, but
need not pass through them. This case is easily generalised to a larger number of
dimensions. Particularly common is to have points in 3D, or in space and time.
Although this problem is very like regression, there is an important difference:
we are interested only in the predicted value at each point, rather than in the
conditional distribution. Typical methods for dealing with this problem are very
like regression methods, but typically the probabilistic infrastructure required to
predict variances, standard errors, and the like are not developed. Interpolation and
smoothing problems in one dimension have the remarkable property of being very
different to those in two and more dimensions (if youve been through a graphics
course, youll know that, for example, interpolating splines in 2D are very different
from those in 1D). We will concentrate on the multidimensional case.
Now think about the function y that we wish to interpolate, and assume that
Section 7.3
185
L
a
t
i
t
u
d
e
Catch score 1
5
0
-5
-11
-11.1
142.8
Longitude
143.9
142.5
-11.5
143
-12 144
143.5
FIGURE 7.8: The prawn data of figure 7.7, interpolated with radial basis functions
(in this case, a Gaussian kernel) with scale chosen by cross-validation. On the left,
a surface shown with the 3D scatter plot. On the right, a heat map. I ignored the
curvature of the earth, small at this scale, when computing distances between points.
This figure is a good example of why interpolation is usually not what one wants to
do (text).
x is reasonably scaled, meaning that distances between points are a good guide to
their similarity. There are several ways to achieve this. We could whiten the points
(section 14.5), or we could use our knowledge of the underlying problem to scale
the different features relative to one another. Once we have done this properly, we
expect the similarity between y(u) and y(v) to depend on the distance between
these points (i.e. || u v ||) rather than on the direction of the vector joining them
(i.e. u v). Furthermore, we expect that the dependency should decline with
increasing || x xi ||. In most problems, we dont know how quickly the weights
should decline with increasing distance, and it is usual to have a scaling parameter
to handle this. The scaling parameter will need to be selected.
All this suggests constructing an interpolate using kernel function. A kernel
function
K(u) is a non-negative function such that (a) K(u) = K(u) and (b)
R
K(u)du
= 1. Widely used kernel functions are:
1
2
1
exp u+exp u+2 .
Section 7.3
186
L
a
t
i
t
u
d
e
Catch score 1
4
2
0
-11.1
-2
-11
142.8
Longitude
143.9
-11.5
-12 144
143.5
143
142.5
FIGURE 7.9: The prawn data of figure 7.7, smoothed with radial basis functions (in
this case, a Gaussian kernel) with scale chosen by cross-validation. On the left,
a surface shown with the 3D scatter plot. On the right, a heat map. I ignored
the curvature of the earth, small at this scale, when computing distances between
points. I used 60 basepoints, constructed by choosing 60 of 155 training points at
random, then adding a small random offset.
support, either.
The Quartic kernel, K(u) =
15
16 (1
u2 )2 I[|u|1] .
You should notice that each is a bump function its large at u = 0, and falls
away as | u | increases. It follows from the two properties above that, for h > 0, if
K(u) is a kernel function, then K(u; h) = h1 K( hu ) is also a kernel function. This
means we can vary the width of the bump at the origin in a natural way by choice
of h; this is usually known as the scale of the function.
We choose a kernel function K, then build a function
R
X
|| x bj ||
aj K
y(x) =
h
j=1
where bj is a set of R points which dont have to be training points. These are
sometimes referred to as base points. You can think of this process as placing a
weighted bump at each base point. Consider the values that this function takes at
the training points xi . We have
y(xi ) =
R
X
j=1
aj K
|| xi bj ||
h
Section 7.3
187
P
2
and we would like to minimize
i (yi y(xi )) . We can rewrite this with the
aid of some linear
algebra. Write G for the Gram matrix, whose i, jth entry
||xi bj||
; write Y for the vector whose ith component is yi ; and a for the
is K
h
vector whose jth component is aj . Then we want to minimize
(Y Ga)T (Y Ga).
There are a variety of cases. For interpolation, we choose the base points to be the
same as the training points. A theorem of Micchelli guarantees that for a kernel
function that is (a) a function of distance and (b) monotonically decreasing with
distance, G will have full rank. Then we must solve
Y = Ga
(Interpolation)
for a and we will obtain a function that passes through each training point. For
smoothing, we may choose any set of R < N basepoints, though its a good idea to
choose points that are close to the training points. Cluster centers are one useful
choice. We then solve the least-squares problem by solving
G T Y = G T Ga
(Smoothing)
(Interpolation)
for a and we will obtain a function that passes through each training point. Similarly, for smoothing, we solve
G T Y = (G T G + I)a
(Smoothing)
for a. Usually, we choose lambda to be small enough to make the linear algebra
work (1e 9 works for me), and ignore it.
As Figure ?? suggests, interpolation isnt really as useful as you might think.
Most measurements arent exactly right, or exactly repeatable. If you look closely
at the figure, youll see one location where there are two scores; this is entirely to
be expected for the score of a prawn trawl at a particular spot in the ocean. This
creates problems for interpolation; G must be rank deficient, because two rows will
be the same. Another difficulty is that the scores look wiggly moving a short
distance can cause the score to change quite markedly. This is likely the effect of
Section 7.3
188
luck in trawling, rather than any real effect. The interpolating method chooses a
very short scale, because this causes the least error in cross-validation, caused by
predicting zero at the held out point (which is more accurate than any prediction
available at any longer scale). The result is an entirely implausible model.
Now look at Figure 7.9. The smoothed surface is a reasonable guide to the
scores; in the section of ocean where scores tend to be large and positive, so is the
smoothed surface; where they tend to be negative, the smoothed surface is negative,
too.
7.3.2 Density Estimation
One specialized application of kernel functions related to smoothing is density
estimation. Here we have a set of N data points xi which we believe to be IID
samples from some p(X), and we wish to estimate the probability density function
p(X). In the case that we have a parametric model for p(X), we could do so by
estimating the parameters with (say) maximum likelihood. But we may not have
such a model, or we may have data that does not comfortably conform to any
model.
A natural, and important, model is to place probability 1/N at each data
point, and zero elsewhere. This is sometimes called an empirical distribution.
However, this model implies that we can only ever see the values we have already
seen, which is often implausible or incovenient. We should like to smooth this
model. If the xi have low enough dimension, we can construct a density estimate
with kernels in a straightforward way.
R Recall that a kernel function is non-negative, and has the property that
K(u)du = 1. This means that if
y(x) =
R
X
j=1
aj K
|| x bj ||
h
we have
Z
y(x)dx =
R
X
aj .
j=1
Now imagine we choose a basepoint at each data point, and we choose aj = 1/N
for all j. The resulting function is non-negative, and integrates to one, so can be
seen as a probability density function. We are placing a bump function of scale h,
weighted by 1/N on top of each data point. If there are many data points close
together, these bump functions will tend to reinforce one another and the resulting
function will be large in such regions. The function will also be small in gaps
between data points that are large compared to h. The resulting model captures
the need to (a) represent the data and (b) allow values that have not been observed
to have non-zero probability.
Choosing h using cross-validation is straightforward. We choose the h that
maximises the log-likelihood of omitted test data, averaged over folds. There is
one nuisance effect here to be careful of. If you use a kernel that has finite support,
you could find that an omitted test item has zero probability; this leads to trouble
Section 7.3
189
L
a
t
i
t
u
d
e
-11
-11.2
-11.4
-11.6
-11.1
-11.8
-12
142.5
142.8
143
143.5
Longitude
143.9
144
FIGURE 7.10: The prawn data of figure 7.7, now shown on the left as a scatter
plot of locations from which scores were reported. The os correspond to negative
scores, and the +s to positive scores. On the right, a density estimate of the
probability distribution from which the fishing locations were drawn, where lighter
pixels correspond to larger density values. I ignored the curvature of the earth, small
at this scale, when computing distances between points.
with logarithms, etc. You could avoid this by obtaining an initial scale estimate
with a kernel that has infinite support, then refining this with a kernel with finite
support.
*** example *** careful about how kernel functions scale with dimension
7.3.3 Kernel Smoothing
We expect that, if x is near a training example xi , then y(x) will be similar to
yi . This suggests constructing an estimate of y(x) as a weighted sum of the values
at nearby examples. Write W (x, xi ) for the weight applied to yi when estimating
y(x). Then our estimate of y(x) is
N
X
yi W (x, xi ).
i=1
We need good choices of W (x, xi ). There are some simple, natural constraints we
can impose. We should like y to be a convex combination of the observed values.
This means we
P want W (x, xi ) to be non-negative (so we can think of them as
weights) and i W (x, xi ) = 1
Section 7.3
190
-11.9
L
a
t
i
t
u
d
e
L
a
t
i
t
u
d
e
-11.1
-11.1
142.8
Longitude
143.9
142.8
Longitude
143.9
FIGURE 7.11: Further plots of the prawn data of figure 7.7. On the left, a density estimate of the probability distribution from which the fishing locations which
achieved positive values of the first score were drawn, where lighter pixels correspond to larger density values. On the right, a density estimate of the probability
distribution from which the fishing locations which achieved negative values of the
first score were drawn, where lighter pixels correspond to larger density values. I
ignored the curvature of the earth, small at this scale, when computing distances
between points.
where I have expanded the notation for the weight function to keep track of the
scaling parameter, hi , which we will need to select. Notice that, at each data point,
we are using a kernel of different width to set weights. This should seem natural to
you. If there are few points near a given data point, then we should have weights
that vary slowly, so that we can for a weighted average of those points. But if
there are many points nearby, we can have weights that vary fast. The weights
are non-negative because we are using a non-negative kernel function. The weights
sum to 1, because we divide at each point by the sum of all kernels evaluated at
that point. For reference, this gives the expression
N
i||
X
K( ||xx
)
hi
y(x) =
yi Pk
||xxj||
i=1
j=1 K( hj )
Section 7.4
191
L
a
t
i
t
u
d
e
-11.1
142.8
Longitude
143.9
of the first catch score for prawns, as a function of position, from the prawn data
of figure 7.7, using the density estimate of figure ??. L ighter pixels correspond to
larger density values. I ignored the curvature of the earth, small at this scale, when
computing distances between points.
Changing the hi will change the radius of the bumps, and will cause more (or
fewer) points to have more (or less) influence on the shape of y(x). Selecting the
hi is easy in principle. We search for a set of values that minimizes cross-validation
error. In practice, this takes an extremely extensive search involving a great deal
of computation, particularly if there are lots of points or the dimension is high.
For the examples of this section, I used the R package np (see appendix for code
samples).
*** robustness isnt really an issue here - copes rather well with outliers ***
not completely correct - locfit will use an alternative *** for that, you need to
discuss biweight
*** distinction between (a) the standard error of prediction and (b) the sd of
predictive distribution
*** you could get away with poor scaling of x if you scale each dimension
separately
*** Curse of dimension and multiple here
*** appendix to each chapter - R CODE for each figure?
7.4 EXPLOITING YOUR NEIGHBORS FOR REGRESSION
TODO: work in local polynomial stuff
Nearest neighbors can clearly predict a number for a query example you find
Section 7.4
90
70
75
200
80
85
Temp
600
400
Weight
800
95
1000
192
10
20
30
Length
40
14
15
16
17
18
19
20
Freq
FIGURE 7.13: Non parametric regressions for the datasets of Figures 6.1 and 6.4.
The curve shows the expected value of the independent variable for each value of
the explanatory variable. The vertical bars show the standard error of the predicted
density for the independent variable, for each value of the explanatory variable.
Notice that, as one would expect, the standard deviation is smaller closer to data
points, and larger further away. On the left, the perch data. On the right, the
cricket data.
the closest training example, and report its number. This would be one way to
use nearest neighbors for regression, but it isnt terribly effective. One important
difficulty is that the regression prediction is piecewise constant (Figure 7.16). If
there is an immense amount of data, this may not present major problems, because
the steps in the prediction will be small and close together. But its not generally
an effective use of data.
A more effective strategy is to find several nearby training examples, and use
them to produce an estimate. This approach can produce very good regression
estimates, because every prediction is made by training examples that are near to
the query example. However, producing a regression estimate is expensive, because
for every query one must find the nearby training examples.
Write x for the query point, and assume that we have already collected the
N nearest neighbors, which we write xi . Write yi for the value of the dependent
variable for the ith of these points. Notice that some of these neighbors could be
quite far from the query point. We dont want distant points to make as much
contribution to the model as nearby points. This suggests forming a weighted
average of the predictions of each point. Write wi for the weight at the ith point.
Then the estimate is
P
wi yi
ypred = Pi
.
i wi
A variety of weightings are reasonable choices. Write di = || (x xi ) || for
the distance between the query point and the ith nearest neighbor. Then inverse
Section 7.4
15
193
Residual
Residual
10
100
150
200
250
300
350
400
100
150
Predicted value
200
250
300
Predicted value
FIGURE 7.14:
Remember this:
Nearest neighbors can be used for regression. In the
simplest approach, you find the nearest neighbor to your feature vector, and
take that neighbors number as your prediction. More complex approaches
smooth predictions over multiple neighbors.
Section 7.4
250
194
180
Weight
160
150
170
Weight
200
190
RMSE=25.5
100
RMSE=25.5
30
40
50
60
70
66
68
Height
70
72
74
Height
FIGURE 7.15:
(yi 0 )2 K(
|| x0 xi ||
).
hi
If you differentiate and set to zero, etc., you will find you have the familiar expression
for kernel regression
!
N
i||
X
K( ||x0hx
)
i
yi Pk
y0 =
.
||x0 xj||
)
i=1
j=1 K(
hi
This suggests something really fruitful. You can read the equation
X
i
(yi 0 )2 K(
|| x0 xi ||
)
hi
as choosing a local function at each xi such that the weighted errors are minimized.
The local function is constant (the value of 0 ), but doesnt have to be. Instead, it
could be polynomial. A polynomial function about the point xi
7.4.2 Using your Neighbors to Predict More than a Number
Linear regression takes some features and predicts a number. But in practice, one
often wants to predict something more complex than a number. For example, I
Section 7.4
195
Inverse Dist
Exp si=0.1
Exp si=0.5
Exp si=1
0.8
0.8
0.6
0.4
0.4
Dependent variable
Dependent variable
0.6
0.2
0
0.2
0.4
0.2
0
0.2
0.4
0.6
0.6
0.8
0.8
1
6
0
2
Explanatory variable
1
Inverse Dist
Exp si=0.1
Exp si=0.5
Exp si=1
0.8
0.6
Inverse Dist
Exp si=0.1
Exp si=0.5
Exp si=1
0.8
0.6
0.4
0.4
Dependent variable
Dependent variable
0
2
Explanatory variable
0.2
0
0.2
0.2
0
0.2
0.4
0.4
0.6
0.6
0.8
1
0.8
6
0
2
Explanatory variable
0
2
Explanatory variable
Section 7.4
196
notation.
In the simplest, and most general, approach, we obtain a prediction for a new
set of explanatory variables x by (a) finding the nearest neighbor and then (b)
producing the dependent variable for that neighbor. We might vary the strategy
slightly by using an approximate nearest neighbor. If the dependent variables
have enough structure that it is possible to summarize a collection of different
dependent variables, then we might recover the k nearest neighbors and summarize
their dependent variables. How we summarize rather depends on the dependent
variables. For example, it is a bit difficult to imagine the average of a set of
trees, but quite straightforward to average images. If the dependent variable was
a word, we might not be able to average words, but we can vote and choose the
most popular word. If the dependent variable is a vector, we can compute either
distance weighted averages or a distance weighted linear regression.
Matched
Images
Patch to be replaced
Final
composited
image
Initial image
FIGURE 7.17: We can fill large holes in images by matching the image to a collection,
choosing one element of the collection, then cutting out an appropriate block of pixels
and putting them into the hole in the query image. In this case, the hole has been
made by an artist, who wishes to remove the roofline from the view. Notice how
there are a range of objects (boats, water) that have been inserted into the hole.
These objects are a reasonable choice, because the overall structures of the query
and matched image are largely similar getting an image that matches most of
the query image supplies enough context to ensure that the rest makes sense.
Section 7.4
197
with embarrassing politics from publicity pictures (see the fascinating examples
in ?); and home users might wish to remove a relative they dislike from a family
picture. All these users must then find something to put in place of the pixels that
were removed.
If one has a large hole in a large image, we may not be able to just extend
a texture to fill the hole. Instead, entire objects might need to appear in the hole
(Figure 7.17). There is a straightforward, and extremely effective, way to achieve
this. We match the image to a large collection of images, to find the nearest
neighbors (the details of the distance function are below). This yields a set of
example images where all the pixels we didnt want to replace are close to those of
the query image. From these, we choose one, and fill in the pixels from that image.
There are several ways to choose. If we wish to do so automatically, we could
use the example with the smallest distance to the image. Very often, an artist is
involved, and then we could prepare a series of alternatives using, perhaps, the
k closest examples then show them to the artist, who will choose one. This
method, which is very simple to describe, is extremely effective in practice.
It is straightforward to get a useful distance between images. We have an
image with some missing pixels, and we wish to find nearby images. We will
assume that all images are the same size. If this isnt in fact the case, we could
either crop or resize the example images. A good measure of similarity between two
images A and B can be measured by forming the sum of squared differences (or
SSD) of corresponding pixel values. You should think of an image as an array of
pixels. If the images are grey-level images, then each pixel contains a single number,
encoding the grey-level. If the images are color images, then each pixel (usually!)
contains three values, one encoding the red-level, one encoding the green-level, and
one encoding the blue-level. The SSD is computed as
X
(Aij Bij )2
(i,j)
where i and j range over all pixels. If the images are grey-level images, then by
(Aij Bij )2 , I mean the squared difference between grey levels; if they are color
images, then this means the sum of squared differences between red, green and blue
values. This distance is small when the images are similar, and large when they are
different (it is essentially the length of the difference vector).
Now we dont know some of the pixels in the query image. Write K for the
set of pixels around a point whose values are known, and K for the size of this set.
We can now use
1 X
(Aij Bij )2 .
K
(i,j)K
Filling in the pixels requires some care. One does not usually get the best
results by just copying the missing pixels from the matched image into the hole.
Instead, it is better to look for a good seam. We search for a curve enclosing the
missing pixels which (a) is reasonably close to the boundary of the missing pixels
and (b) gives a good boundary between the two images. A good boundary is one
where the query image (on one side) is similar to the matched image (on the
Section 7.5
Bayesian Regression
198
other side). A good sense of similarity requires that pixels match well, and that
image gradients crossing the boundary tend to match too.
Remember this:
Nearest neighbors can be used to predict more than
numbers. Examples include parse trees, blocks of pixels, and so on.
dv
where D is all values of n, so R (you should check nothing interesting has happened with the change of variables). Now we have
2 log p(m, n) = (x )T 1 (x ) + K
Section 7.5
Bayesian Regression
199
(where you should check the last step, which I got by completing the square; you
could expand out the expression). Now write p(m, n) = f (m)g(n C 1 Bm) where
2 log g(u) = uT Cu
R
I claim g(n C 1 Bm)dn does not depend on m. Choose some particular value of
m, and compute the integral for that value, recalling that the domain of the integral
is over all values of n the domain does not change with m. Different choices
of m shift the peak of the integrand, but do not
R integral.
R change the value of the
Bm
)dn
=
Equivalently, for some fixed m = m0 , note that D g(nC 1
0
D g(w)dw,
R
where w = n C 1 Bm0 . This is the same for all m0 , so g(n C 1 Bm)dn does
not depend on m. As a result
2 log p(u) = (u u )T (A B T C 1 B)(u u ) + K
so p(u) is normal with mean u . Obtaining its covariance requires a very little
more work. Write
uu Tvu
=
vu vv
and multiply this by 1 , yielding
I
Auu + B T vu ATvu + B T vv
==
0
Buu + Cvu
BTvu + Cvv
0
I
Section 7.6
You should
200
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
162
162
162
163
163
165
165
165
165
168
168
170
172
173
174
174
174
178
178
179
180
181
182
182
187
191
191
192
Section 7.6
You should
201
APPENDIX: DATA
Batch A
Amount of Time in
Hormone
Service
25.8
99
20.5
152
14.3
293
23.2
155
20.6
196
31.1
53
20.9
184
20.9
171
30.4
52
Batch B
Amount of Time in
Hormone
Service
16.3
376
11.6
385
11.8
402
32.5
29
32.0
76
18.0
296
24.1
151
26.5
177
25.8
209
Batch C
Amount of Time in
Hormone
Service
28.8
119
22.0
188
29.7
115
28.9
88
32.8
58
32.5
49
25.4
150
31.7
107
28.5
125
TABLE 7.1: A table showing the amount of hormone remaining and the time in
service for devices from lot A, lot B and lot C. The numbering is arbitrary (i.e.
theres no relationship between device 3 in lot A and device 3 in lot B). We expect
that the amount of hormone goes down as the device spends more time in service,
so cannot compare batches just by comparing numbers.
Section 7.6
You should
202
PROBLEMS
Blood pressure against age
Systolic blood pressure
250
200
150
100
0
20
40
60
Age in years
80
FIGURE 7.18: A regression of blood pressure against age, for 30 data points.
7.1. Figure 7.18 shows a linear regression of systolic blood pressure against age.
There are 30 data points.
(a) Write ei = yi xT
i for the residual. What is the mean ({e}) for this
regression?
(b) For this regression, var ({y}) = 509 and the R2 is 0.4324. What is var ({e})
for this regression?
(c) How well does the regression explain the data?
(d) What could you do to produce better predictions of blood pressure (without actually measuring blood pressure)?
7.2. In this exercise, I will show that the prediction process of chapter 12(see
page 251) is a linear regression with two independent variables. Assume we
have N data items which are 2-vectors (x1 , y1 ), . . . , (xN , yN ), where N > 1.
These could be obtained, for example, by extracting components from larger
vectors. As usual, we will write x
i for xi in normalized coordinates, and so on.
The correlation coefficient is r (this is an important, traditional notation).
(a) Show that r = mean ({(x mean ({x}))(y mean ({y}))})/(std (x)std (y)).
std(y)
(b) Now write s = std(x) . Now assume that we have an xo , for which we wish
to predict a y value. Show that the value of the prediction obtained using
the method of page 252 is
sr(x0 mean ({x})) + mean ({y}).
(c) Show that sr = mean ({(xy)}) mean ({x})mean ({y}).
(d) Now write
y1
x1
1
1
y
x
and Y = 2 .
X = 2
... ...
...
xn
1
yn
where X T X = X T Y.
The coefficients of the linear regression will be ,
Show that
T
X X =N
mean x2
mean ({x})
mean ({x})
1
Section 7.6
(e) Now show that var ({x}) = mean
(x mean ({x}))2
You should
= mean
203
2
x
mean ({x})2 .
(f ) Now show that std (x)std (y)corr ({(x, y)}) = mean ({(x mean ({x}))(y mean ({y}))}).
C H A P T E R
Classification II
8.1 LOGISTIC REGRESSION
8.2 NEURAL NETS
8.3 CONVOLUTION AND ORIENTATION FEATURES
8.4 CONVOLUTIONAL NEURAL NETWORKS
204
C H A P T E R
Boosting
9.1 GRADIENTBOOST
*** for classification *** for regression *** in each case with trees?
9.2 ADABOOST
**?
205
C H A P T E R
10
206
C H A P T E R
11
Section 11.1
Datasets
208
height or weight or body temperature) when you could reasonably expect to encounter any value in a particular range. For example, we might have the heights of
all people in a particular room; or the rainfall at a particular place for each day of
the year; or the number of children in each family on a list.
You should think of a dataset as a collection of d-tuples (a d-tuple is an
ordered list of d elements). Tuples differ from vectors, because we can always add
and subtract vectors, but we cannot necessarily add or subtract tuples. We will
always write N for the number of tuples in the dataset, and d for the number of
elements in each tuple. The number of elements will be the same for every tuple,
though sometimes we may not know the value of some elements in some tuples
(which means we must figure out how to predict their values, which we will do
much later).
Index
1
2
3
4
5
6
7
8
9
10
net worth
100, 360
109, 770
96, 860
97, 860
108, 930
124, 330
101, 300
112, 710
106, 740
120, 170
Index
1
2
3
4
5
6
7
8
9
10
Taste score
12.3
20.9
39
47.9
5.6
25.9
37.3
21.9
18.1
21
Index
11
12
13
14
15
16
17
18
19
20
Taste score
34.9
57.2
0.7
25.9
54.9
40.9
15.9
6.4
18
38.9
TABLE 11.1: On the left, net worths of people you meet in a bar, in US $; I made
this data up, using some information from the US Census. The index column,
which tells you which data item is being referred to, is usually not displayed in a
table because you can usually assume that the first line is the first item, and so
on. On the right, the taste score (Im not making this up; higher is better) for 20
different cheeses. This data is real (i.e. not made up), and it comes from http://
lib.stat.cmu.edu/ DASL/ Datafiles/ Cheese.html.
Each element of a tuple has its own type. Some elements might be categorical.
For example, one dataset we shall see several times has entries for Gender; Grade;
Age; Race; Urban/Rural; School; Goals; Grades; Sports; Looks; and Money for
478 children, so d = 11 and N = 478. In this dataset, each entry is categorical
data. Clearly, these tuples are not vectors because one cannot add or subtract (say)
Gender, or add Age to Grades.
Most of our data will be vectors. We use the same notation for a tuple and
for a vector. We write a vector in bold, so x could represent a vector or a tuple
(the context will make it obvious which is intended).
The entire data set is {x}. When we need to refer to the ith data item, we
write xi . Assume we have N data items, and we wish to make a new dataset out
of them; we write the dataset made out of these items as {xi } (the i is to suggest
you are taking a set of items and making a dataset out of them).
In this chapter, we will work mainly with continuous data. We will see a
Section 11.2
209
variety of methods for plotting and summarizing 1-tuples. We can build these
plots from a dataset of d-tuples by extracting the rth element of each d-tuple.
All through the book, we will see many datasets downloaded from various web
sources, because people are so generous about publishing interesting datasets on
the web. In the next chapter, we will look at 2-dimensional data, and we look at
high dimensional data in chapter 3.
11.2 WHATS HAPPENING? - PLOTTING DATA
The very simplest way to present or visualize a dataset is to produce a table. Tables
can be helpful, but arent much use for large datasets, because it is difficult to get
any sense of what the data means from a table. As a continuous example, table 11.1
gives a table of the net worth of a set of people you might meet in a bar (I made
this data up). You can scan the table and have a rough sense of what is going on;
net worths are quite close to $ 100, 000, and there arent any very big or very small
numbers. This sort of information might be useful, for example, in choosing a bar.
People would like to measure, record, and reason about an extraordinary
variety of phenomena. Apparently, one can score the goodness of the flavor of
cheese with a number (bigger is better); table 11.1 gives a score for each of thirty
cheeses (I did not make up this data, but downloaded it from http://lib.stat.cmu.
edu/DASL/Datafiles/Cheese.html). You should notice that a few cheeses have very
high scores, and most have moderate scores. Its difficult to draw more significant
conclusions from the table, though.
Gender
boy
boy
girl
girl
girl
girl
girl
girl
girl
girl
Goal
Sports
Popular
Popular
Popular
Popular
Popular
Popular
Grades
Sports
Sports
Gender
girl
girl
boy
boy
boy
girl
girl
girl
girl
girl
Goal
Sports
Grades
Popular
Popular
Popular
Grades
Sports
Popular
Grades
Sports
TABLE 11.2: Chase and Dunner (?) collected data on what students thought made
other students popular. As part of this effort, they collected information on (a) the
gender and (b) the goal of students. This table gives the gender (boy or girl)
and the goal (to make good grades Grades; to be popular Popular; or to
be good at sports Sports). The table gives this information for the first 20
of 478 students; the rest can be found at http:// lib.stat.cmu.edu/ DASL/ Datafiles/
PopularKids.html. This data is clearly categorical, and not ordinal.
Table 11.2 shows a table for a set of categorical data. Psychologists collected
data from students in grades 4-6 in three school districts to understand what factors students thought made other students popular. This fascinating data set can
be found at http://lib.stat.cmu.edu/DASL/Datafiles/PopularKids.html, and was pre-
Section 11.2
210
pared by Chase and Dunner (?). Among other things, for each student they asked
whether the students goal was to make good grades (Grades, for short); to be
popular (Popular); or to be good at sports (Sports). They have this information for 478 students, so a table would be very hard to read. Table 11.2 shows the
gender and the goal for the first 20 students in this group. Its rather harder to
draw any serious conclusion from this data, because the full table would be so big.
We need a more effective tool than eyeballing the table.
Number of children of each gender
300
250
250
200
200
150
150
100
100
50
50
0
boy
girl
Sports
Grades
Popular
FIGURE 11.1: On the left, a bar chart of the number of children of each gender in
the Chase and Dunner study (). Notice that there are about the same number of
boys and girls (the bars are about the same height). On the right, a bar chart of
the number of children selecting each of three goals. You can tell, at a glance, that
different goals are more or less popular by looking at the height of the bars.
Section 11.2
4
3
2
1
0
0.95
1.25
211
12
10
8
6
4
2
0
0
10 20 30 40 50 60 70
Cheese goodness, in cheese goodness units
FIGURE 11.2: On the left, a histogram of net worths from the dataset described in
the text and shown in table 11.1. On the right, a histogram of cheese goodness
scores from the dataset described in the text and shown in table 11.1.
Section 11.2
212
12
12
10
10
0
96
98
100
102
0
96
98
100
102
FIGURE 11.3: On top, a histogram of body temperatures, from the dataset pub-
Section 11.3
Summarizing 1D Data
213
But a histogram with even intervals can have empty boxes (see figure 11.2). In
this case, it can be more informative to have some larger intervals to ensure that
each interval has some data items in it. But how high should we plot the box?
Imagine taking two consecutive intervals in a histogram with even intervals, and
fusing them. It is natural that the height of the fused box should be the average
height of the two boxes. This observation gives us a rule.
Write dx for the width of the intervals; n1 for the height of the box over the
first interval (which is the number of elements in the first box); and n2 for the
height of the box over the second interval. The height of the fused box will be
(n1 + n2 )/2. Now the area of the first box is n1 dx; of the second box is n2 dx; and
of the fused box is (n1 + n2 )dx. For each of these boxes, the area of the box is
proportional to the number of elements in the box. This gives the correct rule: plot
boxes such that the area of the box is proportional to the number of elements in
the box.
11.2.4 Conditional Histograms
Most people believe that normal body temperature is 98.4o in Fahrenheit. If you
take other peoples temperatures often (for example, you might have children), you
know that some individuals tend to run a little warmer or a little cooler than this
number. I found data giving the body temperature of a set of individuals at http://
www2.stetson.edu/jrasp/data.htm. As you can see from the histogram (figure 11.3),
the body temperatures cluster around a small set of numbers. But what causes the
variation?
One possibility is gender. We can investigate this possibility by comparing a histogram of temperatures for males with histogram of temperatures for females. Such histograms are sometimes called conditional histograms or classconditional histograms, because each histogram is conditioned on something (in
this case, the histogram uses only data that comes from gender).
The dataset gives genders (as 1 or 2 - I dont know which is male and which
female). Figure 11.3 gives the class conditional histograms. It does seem like
individuals of one gender run a little cooler than individuals of the other, although
we dont yet have mechanisms to test this possibility in detail (chapter 14.5).
11.3 SUMMARIZING 1D DATA
For the rest of this chapter, we will assume that data items take values that are
continuous real numbers. Furthermore, we will assume that values can be added,
subtracted, and multiplied by constants in a meaningful way. Human heights are
one example of such data; you can add two heights, and interpret the result as a
height (perhaps one person is standing on the head of the other). You can subtract
one height from another, and the result is meaningful. You can multiply a height
by a constant say, 1/2 and interpret the result (A is half as high as B).
11.3.1 The Mean
One simple and effective summary of a set of data is its mean. This is sometimes
known as the average of the data.
Section 11.3
Summarizing 1D Data
214
For example, assume youre in a bar, in a group of ten people who like to talk
about money. Theyre average people, and their net worth is given in table 11.1
(you can choose who you want to be in this story). The mean of this data is $107,
903.
Properties of the Mean
should remember:
Scaling data scales the mean: or mean ({kxi }) = kmean ({xi }).
Translating data translates the mean: or mean ({xi + c}) = mean ({xi }) + c.
The sum of signed differences from the mean is zero. This means that
N
X
i=1
Choose the number such that the sum of squared distances of data points
to is minimized. That number is the mean. In notation
arg min X
(xi )2 = mean ({xi })
These properties are easy to prove (and so easy to remember). I have broken
these out into a box of useful facts below, to emphasize them. All but one proof is
relegated to the exercises.
Section 11.3
Summarizing 1D Data
215
arg min P
2
i (xi ) = mean ({x})
Proof: Choose the number such that the sum of squared distances of data
points to is minimized. That number is the mean. In notation:
Proposition:
arg min X
(xi )2 = mean ({x})
We can show this by actually minimizing the expression. We must have that the
derivative of the expression we are minimizing is zero at the value of we are
seeking. So we have
N
d X
(xi )2
d i=1
N
X
i=1
= 2
2(xi )
N
X
i=1
= 0
(xi )
Section 11.3
Summarizing 1D Data
216
arg min X
(xi )2 = mean ({xi })
The mean is a location parameter; it tells you where the data lies along
a number line.
You should think of the standard deviation as a scale. It measures the size of
the average deviation from the mean for a dataset, or how wide the spread of data
is. For this reason, it is often referred to as a scale parameter. When the standard
deviation of a dataset is large, there are many items with values much larger than,
or much smaller than, the mean. When the standard deviation is small, most data
items have values close to the mean. This means it is helpful to talk about how
many standard devations away from the mean a particular data item is. Saying
that data item xj is within k standard deviations from the mean means that
abs (xj mean ({x})) kstd ({xi }).
Section 11.3
Summarizing 1D Data
217
Similarly, saying that data item xj is more than k standard deviations from the
mean means that
abs (xi mean ({x})) > kstd ({x}).
As I will show below, there must be some data at least one standard deviation
away from the mean, and there can be very few data items that are many standard
deviations away from the mean.
Properties of the Standard Deviation Standard deviation has very important properties:
Translating data does not change the standard deviation, i.e. std ({xi + c}) =
std ({xi }).
Scaling data scales the standard deviation, i.e. std ({kxi }) = kstd ({xi }).
For any dataset, there can be only a few items that are many standard deviations away from the mean. In particular, assume we have N data items, xi ,
whose standard deviation is . Then there are at most k12 data points lying
k or more standard deviations away from the mean.
For any dataset, there must be at least one data item that is at least one
standard deviation away from the mean.
The first two properties are easy to prove, and are relegated to the exercises. I
prove the others below. Again, for emphasis, I have broken these properties out in
a box below.
Section 11.3
Summarizing 1D Data
218
Proposition:
Assume we have a dataset {x} of N data items, x1 , . . . , xN .
Assume the standard deviation of this dataset is std ({x}) = . Then there are at
most k12 data points lying k or more standard deviations away from the mean.
Proof: Assume the mean is zero. There is no loss of generality here, because
translating data translates the mean, but doesnt change the standard deviation.
Now we must construct a dataset with the largest possible fraction r of data
points lying k or more standard deviations from the mean. To achieve this, our
data should have N (1 r) data points each with the value 0, because these
contribute 0 to the standard deviation. It should have N r data points with the
value k; if they are further from zero than this, each will contribute more to
the standard deviation, so the fraction of such points will be fewer. Because
rP
2
i xi
std ({x}) = =
N
we have that, for this rather specially constructed dataset,
r
N rk 2 2
=
N
so that
1
.
k2
We constructed the dataset so that r would be as large as possible, so
r=
1
k2
Section 11.3
Summarizing 1D Data
219
data that lies many standard deviations from the mean, because you cant.
Proposition:
(std ({x}))2 max(xi mean ({x}))2 .
i
Proof: You can see this by looking at the expression for standard deviation.
We have
v
u i=N
u1 X
std ({x}) = t
(xi mean ({x}))2 .
N i=1
N (std ({x}))2 =
i=N
X
i=1
But
i=N
X
i=1
so
Property 11.3: For any dataset, there must be at least one data item that is
at least one standard deviation away from the mean.
Boxes 11.2 and 11.3 mean that the standard deviation is quite informative.
Very little data is many standard deviations away from the mean; similarly, at least
some of the data should be one or more standard deviations away from the mean.
So the standard deviation tells us how data points are scattered about the mean.
Section 11.3
Summarizing 1D Data
220
There is an ambiguity that comes up often here because two (very slightly)
different numbers are called the standard deviation of a dataset. One the one
we use in this chapter is an estimate of the scale of the data, as we describe it.
The other differs from our expression very slightly; one computes
rP
2
i (xi mean ({x}))
N 1
(notice the N 1 for our N ). If N is large, this number is basically the same as the
number we compute, but for smaller N there is a difference that can be significant.
Irritatingly, this number is also called the standard deviation; even more irritatingly,
we will have to deal with it, but not yet. I mention it now because you may look
up terms I have used, find this definition, and wonder whether I know what Im
talking about. In this case, I do (although I would say that).
The confusion arises because sometimes the datasets we see are actually samples of larger datasets. For example, in some circumstances you could think of the
net worth dataset as a sample of all the net worths in the USA. In such cases, we
are often interested in the standard deviation of the dataset that was sampled. The
second number is a slightly better way to estimate this standard deviation than the
definition we have been working with. Dont worry - the N in our expressions is
the right thing to use for what were doing.
11.3.3 Variance
It turns out that thinking in terms of the square of the standard deviation, which
is known as the variance, will allow us to generalize our summaries to apply to
higher dimensional data.
One good way to think of the variance is as the mean-square error you would
incur if you replaced each data item with the mean. Another is that it is the square
of the standard deviation.
Properties of the Variance
Section 11.3
Summarizing 1D Data
221
the fact that it is the square of the standard deviation. I have broken these out in
a box, for emphasis.
While one could restate the other two properties of the standard deviation in
terms of the variance, it isnt really natural to do so. The standard deviation is in
the same units as the original data, and should be thought of as a scale. Because
the variance is the square of the standard deviation, it isnt a natural scale (unless
you take its square root!).
11.3.4 The Median
One problem with the mean is that it can be affected strongly by extreme values.
Go back to the bar example, of section 11.3.1. Now Warren Buffett (or Bill Gates,
or your favorite billionaire) walks in. What happened to the average net worth?
Assume your billionaire has net worth $ 1, 000, 000, 000. Then the mean net
worth suddenly has become
10 $107, 903 + $1, 000, 000, 000
= $91, 007, 184
11
But this mean isnt a very helpful summary of the people in the bar. It is probably more useful to think of the net worth data as ten people together with one
billionaire. The billionaire is known as an outlier.
One way to get outliers is that a small number of data items are very different, due to minor effects you dont want to model. Another is that the data
was misrecorded, or mistranscribed. Another possibility is that there is just too
much variation in the data to summarize it well. For example, a small number
of extremely wealthy people could change the average net worth of US residents
dramatically, as the example shows. An alternative to using a mean is to use a
median.
Section 11.3
Summarizing 1D Data
222
For example,
median ({3, 5, 7}) = 5,
median ({3, 4, 5, 6, 7}) = 5,
and
median ({3, 4, 5, 6}) = 4.5.
For much, but not all, data, you can expect that roughly half the data is smaller
than the median, and roughly half is larger than the median. Sometimes this
property fails. For example,
median ({1, 2, 2, 2, 2, 2, 2, 2, 3}) = 2.
With this definition, the median of our list of net worths is $107, 835. If we insert
the billionaire, the median becomes $108, 930. Notice by how little the number has
changed it remains an effective summary of the data.
Properties of the median You can think of the median of a dataset as
giving the middle or center value. It is another way of estimating where the
dataset lies on a number line (and so is another location parameter). This means
it is rather like the mean, which also gives a (slightly differently defined) middle
or center value. The mean has the important properties that if you translate the
dataset, the mean translates, and if you scale the dataset, the mean scales. The
median has these properties, too, which I have broken out in a box. Each is easily
proved, and proofs are relegated to the exercises.
Section 11.3
Summarizing 1D Data
223
Section 11.3
Summarizing 1D Data
224
Like the standard deviation, the interquartile range gives an estimate of how
widely the data is spread out. But it is quite well-behaved in the presence of
outliers. For our net worth data without the billionaire, the interquartile range is
$12350; with the billionaire, it is $17710.
Properties of the interquartile range You can think of the interquartile
range of a dataset as giving an estimate of the scale of the difference from the mean.
This means it is rather like the standard deviation, which also gives a (slightly
differently defined) scale. The standard deviation has the important properties
that if you translate the dataset, the standard deviation translates, and if you
scale the dataset, the standard deviation scales. The interquartile range has these
properties, too, which I have broken out into a box. Each is easily proved, and
proofs are relegated to the exercises.
For most datasets, interquartile ranges tend to be somewhat larger than standard deviations. This isnt really a problem. Each is a method for estimating the
scale of the data the range of values above and below the mean that you are likely
to see. It is neither here nor there if one method yields slightly larger estimates
than another, as long as you dont compare estimates across methods.
11.3.6 Using Summaries Sensibly
One should be careful how one summarizes data. For example, the statement
that the average US family has 2.6 children invites mockery (the example is from
Andrew Vickers book What is a p-value anyway?), because you cant have fractions
of a child no family has 2.6 children. A more accurate way to say things might
Section 11.4
225
be the average of the number of children in a US family is 2.6, but this is clumsy.
What is going wrong here is the 2.6 is a mean, but the number of children in a
family is a categorical variable. Reporting the mean of a categorical variable is
often a bad idea, because you may never encounter this value (the 2.6 children).
For a categorical variable, giving the median value and perhaps the interquartile
range often makes much more sense than reporting the mean.
For continuous variables, reporting the mean is reasonable because you could
expect to encounter a data item with this value, even if you havent seen one in
the particular data set you have. It is sensible to look at both mean and median;
if theyre significantly different, then there is probably something going on that is
worth understanding. Youd want to plot the data using the methods of the next
section before you decided what to report.
You should also be careful about how precisely numbers are reported (equivalently, the number of significant figures). Numerical and statistical software will
produce very large numbers of digits freely, but not all are always useful. This is a
particular nuisance in the case of the mean, because you might add many numbers,
then divide by a large number; in this case, you will get many digits, but some
might not be meaningful. For example, Vickers (ibid) describes a paper reporting
the mean length of pregnancy as 32.833 weeks. That fifth digit suggests we know
the mean length of pregnancy to about 0.001 weeks, or roughly 10 minutes. Neither
medical interviewing nor peoples memory for past events is that detailed. Furthermore, when you interview them about embarrassing topics, people quite often lie.
There is no prospect of knowing this number with this precision.
People regularly report silly numbers of digits because it is easy to miss the
harm caused by doing so. But the harm is there: you are implying to other people,
and to yourself, that you know something more accurately than you do. At some
point, someone will suffer for it.
11.4 PLOTS AND SUMMARIES
Knowing the mean, standard deviation, median and interquartile range of a dataset
gives us some information about what its histogram might look like. In fact, the
summaries give us a language in which to describe a variety of characteristic properties of histograms that are worth knowing about (Section 11.4.1). Quite remarkably, many different datasets have histograms that have about the same shape
(Section 11.4.2). For such data, we know roughly what percentage of data items
are how far from the mean.
Complex datasets can be difficult to interpret with histograms alone, because
it is hard to compare many histograms by eye. Section 11.4.3 describes a clever
plot of various summaries of datasets that makes it easier to compare many cases.
11.4.1 Some Properties of Histograms
The tails of a histogram are the relatively uncommon values that are significantly
larger (resp. smaller) than the value at the peak (which is sometimes called the
mode). A histogram is unimodal if there is only one peak; if there are more than
one, it is multimodal, with the special term bimodal sometimes being used for
the case where there are two peaks (Figure 11.4). The histograms we have seen
Section 11.4
226
mode
Bimodal
modes
Multimodal
modes
Population 1
Population 2
Population 1
Population 2
Population 3
FIGURE 11.4: Many histograms are unimodal, like the example on the top; there is
one peak, or mode. Some are bimodal (two peaks; bottom left) or even multimodal
(two or more peaks; bottom right). One common reason (but not the only reason)
is that there are actually two populations being conflated in the histograms. For
example, measuring adult heights might result in a bimodal histogram, if male and
female heights were slightly different. As another example, measuring the weight
of dogs might result in a multimodal histogram if you did not distinguish between
breeds (eg chihauhau, terrier, german shepherd, pyranean mountain dog, etc.).
have been relatively symmetric, where the left and right tails are about as long as
one another. Another way to think about this is that values a lot larger than the
mean are about as common as values a lot smaller than the mean. Not all data is
symmetric. In some datasets, one or another tail is longer (figure 11.5). This effect
is called skew.
Skew appears often in real data. SOCR (the Statistics Online Computational
Resource) publishes a number of datasets. Here we discuss a dataset of citations
to faculty publications. For each of five UCLA faculty members, SOCR collected
the number of times each of the papers they had authored had been cited by
other authors (data at http://wiki.stat.ucla.edu/socr/index.php/SOCR Data Dinov
072108 H Index Pubs). Generally, a small number of papers get many citations, and
many papers get few citations. We see this pattern in the histograms of citation
numbers (figure 11.6). These are very different from (say) the body temperature
pictures. In the citation histograms, there are many data items that have very few
citations, and few that have many citations. This means that the right tail of the
histogram is longer, so the histogram is skewed to the right.
One way to check for skewness is to look at the histogram; another is to
Section 11.4
227
Symmetric Histogram
mode, median, mean, all on top of
one another
right
tail
left
tail
Left Skew
Right Skew
mean
mode
median
left
tail
right
tail
left
tail
mean
mode
median
right
tail
FIGURE 11.5: On the top, an example of a symmetric histogram, showing its tails
(relatively uncommon values that are significantly larger or smaller than the peak
or mode). Lower left, a sketch of a left-skewed histogram. Here there are few
large values, but some very small values that occur with significant frequency. We
say the left tail is long, and that the histogram is left skewed. You may find
this confusing, because the main bump is to the right one way to remember this
is that the left tail has been stretched. Lower right, a sketch of a right-skewed
histogram. Here there are few small values, but some very large values that occur
with significant frequency. We say the right tail is long, and that the histogram
is right skewed.
compare mean and median (though this is not foolproof). For the first citation
histogram, the mean is 24.7 and the median is 7.5; for the second, the mean is 24.4,
and the median is 11. In each case, the mean is a lot bigger than the median. Recall
the definition of the median (form a ranked list of the data points, and find the
point halfway along the list). For much data, the result is larger than about half
of the data set and smaller than about half the dataset. So if the median is quite
small compared to the mean, then there are many small data items and a small
number of data items that are large the right tail is longer, so the histogram is
skewed to the right.
Left-skewed data also occurs; figure 11.6 shows a histogram of the birth
weights of 44 babies born in Brisbane, in 1997 (from http://www.amstat.org/publications/
jse/jse data archive.htm). This data appears to be somewhat left-skewed, as birth
weights can be a lot smaller than the mean, but tend not to be much larger than
the mean.
Skewed data is often, but not always, the result of constraints. For example,
good obstetrical practice tries to ensure that very large birth weights are rare (birth
Section 11.4
228
Histogram of citations for faculty member A Birth weights for 44 babies born in Brisbane
400
15
300
10
200
5
100
0
0
100
200
300
400
0
500 1000
2000
3000
4000
5000
FIGURE 11.6:
On the left, a histogram of citations for a faculty
member,
from
data
at
http:// wiki.stat.ucla.edu/ socr/ index.php/
Very few publications have many
SOCR Data Dinov 072108 H Index Pubs.
citations, and many publications have few. This means the histogram is strongly
right-skewed. On the right, a histogram of birth weights for 44 babies borne in
Brisbane in 1997. This histogram looks slightly left-skewed.
is typically induced before the baby gets too heavy), but it may be quite hard to
avoid some small birth weights. This could could skew birth weights to the left
(because large babies will get born, but will not be as heavy as they could be if
obstetricians had not interfered). Similarly, income data can be skewed to the right
by the fact that income is always positive. Test mark data is often skewed
whether to right or left depends on the circumstances by the fact that there is
a largest possible mark and a smallest possible mark.
11.4.2 Standard Coordinates and Normal Data
It is useful to look at lots of histograms, because it is often possible to get some
useful insights about data. However, in their current form, histograms are hard to
compare. This is because each is in a different set of units. A histogram for length
data will consist of boxes whose horizontal units are, say, metres; a histogram
for mass data will consist of boxes whose horizontal units are in, say, kilograms.
Furthermore, these histograms typically span different ranges.
We can make histograms comparable by (a) estimating the location of the
plot on the horizontal axis and (b) estimating the scale of the plot. The location
is given by the mean, and the scale by the standard deviation. We could then
normalize the data by subtracting the location (mean) and dividing by the standard
deviation (scale). The resulting values are unitless, and have zero mean. They are
often known as standard coordinates.
Section 11.4
229
We write {
x} for a dataset that happens to be in standard coordinates.
Section 11.4
230
0.5
0.4
0.3
0.2
0.1
0
0
4 3 2 1 0 1 2 3 4 6
4
2
0
2
4
6
Human heights, standard coordinates
Human weights, standard coordinates
60
60
50
50
40
40
30
30
20
20
10
10
0
6
0
6 6
FIGURE 11.7: Data is standard normal data when its histogram takes a stylized,
bell-shaped form, plotted above. One usually requires a lot of data and very small
histogram boxes for this form to be reproduced closely. Nonetheless, the histogram
for normal data is unimodal (has a single bump) and is symmetric; the tails fall
off fairly fast, and there are few data items that are many standard deviations from
the mean. Many quite different data sets have histograms that are similar to the
normal curve; I show three such datasets here.
It is not always easy to tell whether data is normal or not, and there are
a variety of tests one can use, which we discuss later. However, there are many
examples of normal data. Figure 11.7 shows a diverse variety of data sets, plotted
Section 11.4
231
Section 11.5
232
Outlier
31
30
Whisker
29
27
Interquartile range
28
q3
Box
Median
26
Dominos
EagleBoys
FIGURE 11.8: A boxplot showing the box, the median, the whiskers and two outliers.
Notice that we can compare the two datasets rather easily; the next section explains
the comparison.
Once we have identified outliers, we plot these with a special symbol (crosses
in the plots I show). We then plot whiskers, which show the range of non-outlier
data. We draw a whisker from q1 to the smallest data item that is not an outlier,
and from q3 to the largest data item that is not an outlier. While all this sounds
complicated, any reasonable programming environment will have a function that
will do it for you. Figure 11.8 shows an example boxplot. Notice that the rich
graphical structure means it is quite straightforward to compare two histograms.
11.5 WHOSE IS BIGGER? INVESTIGATING AUSTRALIAN PIZZAS
At http://www.amstat.org/publications/jse/jse data archive.htm), you will find a dataset
giving the diameter of pizzas, measured in Australia (search for the word pizza).
This website also gives the backstory for this dataset. Apparently, EagleBoys pizza
claims that their pizzas are always bigger than Dominos pizzas, and published a set
of measurements to support this claim (the measurements were available at http://
www.eagleboys.com.au/realsizepizza as of Feb 2012, but seem not to be there anymore).
Whose pizzas are bigger? and why? A histogram of all the pizza sizes appears
Section 11.5
233
26
28
30
32
FIGURE 11.9: A histogram of pizza diameters from the dataset described in the text.
30
30
25
25
20
20
15
15
10
10
26
28
30
32
26
28
30
32
FIGURE 11.10: On the left, the class-conditional histogram of Dominos pizza diameters from the pizza data set; on the right, the class-conditional histogram of
EagleBoys pizza diameters. Notice that EagleBoys pizzas seem to follow the pattern we expect the diameters are clustered tightly around a mean, and there is a
small standard deviation but Dominos pizzas do not seem to be like that. There
is more to understand about this data.
in figure 11.9. We would not expect every pizza produced by a restaurant to have
exactly the same diameter, but the diameters are probably pretty close to one
another, and pretty close to some standard value. This would suggest that wed
expect to see a histogram which looks like a single, rather narrow, bump about a
mean. This is not what we see in figure 11.9 instead, there are two bumps, which
suggests two populations of pizzas. This isnt particularly surprising, because we
know that some pizzas come from EagleBoys and some from Dominos.
If you look more closely at the data in the dataset, you will notice that each
data item is tagged with the company it comes from. We can now easily plot con-
Section 11.5
234
30
29
28
27
26
Dominos
EagleBoys
FIGURE 11.11: Boxplots of the pizza data, comparing EagleBoys and Dominos pizza.
There are several curiosities here: why is the range for Dominos so large (25.5-29)?
EagleBoys has a smaller range, but has several substantial outliers; why? One would
expect pizza manufacturers to try and control diameter fairly closely, because pizzas
that are too small present risks (annoying customers; publicity; hostile advertising)
and pizzas that are too large should affect profits.
ditional histograms, conditioning on the company that the pizza came from. These
appear in figure 11.10. Notice that EagleBoys pizzas seem to follow the pattern
we expect the diameters are clustered tightly around one value but Dominos
pizzas do not seem to be like that. This is reflected in a boxplot (figure 11.11),
which shows the range of Dominos pizza sizes is surprisingly large, and that EagleBoys pizza sizes have several large outliers. There is more to understand about
this data. The dataset contains labels for the type of crust and the type of topping
perhaps these properties affect the size of the pizza?
EagleBoys produces DeepPan, MidCrust and ThinCrust pizzas, and Dominos
produces DeepPan, ClassicCrust and ThinNCrispy pizzas. This may have something to do with the observed patterns, but comparing six histograms by eye is
unattractive. A boxplot is the right way to compare these cases (figure 11.12). The
boxplot gives some more insight into the data. Dominos thin crust appear to have a
narrow range of diameters (with several outliers), where the median pizza is rather
larger than either the deep pan or the classic crust pizza. EagleBoys pizzas all have
a range of diameters that is (a) rather similar across the types and (b) rather a lot
like the Dominos thin crust. There are outliers, but few for each type.
Another possibility is that the variation in size is explained by the topping.
We can compare types and toppings by producing a set of conditional boxplots (i.e.
the diameters for each type and each topping). This leads to rather a lot of boxes
(figure 11.13), but theyre still easy to compare by eye. The main difficulty is that
the labels on the plot have to be shortened. I made labels using the first letter
from the manufacturer (D or E); the first letter from the crust type (previous
paragraph); and the first and last letter of the topping. Toppings for Dominos are:
Hawaiian; Supreme; BBQMeatlovers. For EagleBoys, toppings are: Hawaiian; SuperSupremo; and BBQMeatlovers. This gives the labels: DCBs; (Dominos; Clas-
Section 11.5
235
30
29
28
27
26
DThinNCrispy
DDeepPan
DClassicCrust
EMidCrust
EDeepPan
EThinCrust
FIGURE 11.12: Boxplots for the pizza data, broken out by type (thin crust, etc.).
Section 11.5
236
Dominos
EagleBoys
ThinCrust
30
ClassicCrust
DeepPan
29
ThinCrust
28
DeepPan
27
MidCrust
26
DCBsDCHnDCSeDDBsDDHnDDSeDTBsDTHnDTSeEDBsEDHnEDSoEMBsEMHnEMSoETBsETHnETSo
FIGURE 11.13: The pizzas are now broken up by topping as well as crust type (look at
the source for the meaning of the names). I have separated Dominos from Eagleboys
with a vertical line, and grouped each crust type with a box. It looks as though the
issue is not the type of topping, but the crust. Eagleboys seems to have tighter
control over the size of the final pizza.
else next time). But making more regular pizzas might require more skilled (and so
more expensive) labor. The fact that Dominos and EagleBoys seem to be following
different strategies successfully suggests that more than one strategy might work.
But you cant choose if you dont know whats happening. As I said at the start,
whats going on here? is perhaps the single most useful question anyone can ask.
Section 11.6
You should
237
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
201
201
201
204
205
207
207
207
208
210
210
210
213
214
214
215
215
215
216
216
217
217
218
218
218
219
219
219
219
219
220
222
223
223
Section 11.6
New term:
Definition:
New term:
Definition:
New term:
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
You should
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
238
.
.
.
.
.
223
223
224
224
225
C H A P T E R
12
Background:Looking at
Relationships
We think of a dataset as a collection of d-tuples (a d-tuple is an ordered list of
d elements). For example, the Chase and Dunner dataset had entries for Gender;
Grade; Age; Race; Urban/Rural; School; Goals; Grades; Sports; Looks; and Money
(so it consisted of 11-tuples). The previous chapter explored methods to visualize
and summarize a set of values obtained by extracting a single element from each
tuple. For example, I could visualize the heights or the weights of a population (as
in Figure 11.7). But I could say nothing about the relationship between the height
and weight. In this chapter, we will look at methods to visualize and summarize
the relationships between pairs of elements of a dataset.
12.1 PLOTTING 2D DATA
We take a dataset, choose two different entries, and extract the corresponding
elements from each tuple. The result is a dataset consisting of 2-tuples, and we
think of this as a two dimensional dataset. The first step is to plot this dataset in a
way that reveals relationships. The topic of how best to plot data fills many books,
and we can only scratch the surface here. Categorical data can be particularly
tricky, because there are a variety of choices we can make, and the usefulness of
each tends to depend on the dataset and to some extent on ones cleverness in
graphic design (section 12.1.1).
For some continuous data, we can plot the one entry as a function of the other
(so, for example, our tuples might consist of the date and the number of robberies;
or the year and the price of lynx pelts; and so on, section 12.1.2).
Mostly, we use a simple device, called a scatter plot. Using and thinking about
scatter plots will reveal a great deal about the relationships between our data items
(section 12.1.3).
12.1.1 Categorical Data, Counts, and Charts
Categorical data is a bit special. Assume we have a dataset with several categorical descriptions of each data item. One way to plot this data is to think of
it as belonging to a richer set of categories. Assume the dataset has categorical
descriptions, which are not ordinal. Then we can construct a new set of categories
by looking at each of the cases for each of the descriptions. For example, in the
Chase and Dunner data of table 11.2, our new categories would be: boy-sports;
girl-sports; boy-popular; girl-popular; boy-grades; and girl-grades. A
large set of categories like this can result in a poor bar chart, though, because there
may be too many bars to group the bars successfully. Figure 12.1 shows such a bar
chart. Notice that it is hard to group categories by eye to compare; for example,
239
Section 12.1
Plotting 2D Data
240
150
100
boyGrades
50
girlGrades
0
bP bG bS gS gG gP
boySports
girlSports
FIGURE 12.1: I sorted the children in the Chase and Dunner study into six categories
(two genders by three goals), and counted the number of children that fell into each
cell. I then produced the bar chart on the left, which shows the number of children
of each gender, selecting each goal. On the right, a pie chart of this information.
I have organized the pie chart so it is easy to compare boys and girls by eye start
at the top; going down on the left side are boy goals, and on the right side are girl
goals. Comparing the size of the corresponding wedges allows you to tell what goals
boys (resp. girls) identify with more or less often.
you can see that slightly more girls think grades are important than boys do, but
to do so you need to compare two bars that are separated by two other bars. An
alternative is a pie chart, where a circle is divided into sections whose angle is
proportional to the size of the data item. You can think of the circle as a pie, and
each section as a slice of pie. Figure 12.1 shows a pie chart, where each section is
proportional to the number of students in its category. In this case, Ive used my
judgement to lay the categories out in a way that makes comparisons easy. Im not
aware of any tight algorithm for doing this, though.
Pie charts have problems, because it is hard to judge small differences in area
accurately by eye. For example, from the pie chart in figure 12.1, its hard to tell
that the boy-sports category is slightly bigger than the boy-popular category
(try it; check using the bar chart). For either kind of chart, it is quite important
to think about what you plot. For example, the plot of figure 12.1 shows the total
number of respondents, and if you refer to figure 11.1, you will notice that there
are slightly more girls in the study. Is the percentage of boys who think grades are
important smaller (or larger) than the percentage of girls who think so? you cant
tell from these plots, and youd have to plot the percentages instead.
An alternative is to use a stacked bar chart. You can (say) regard the data
as of two types, Boys and Girls. Within those types, there are subtypes (Popularity, Grades and Sport). The height of the bar is given by the number of
elements in the type, and the bar is divided into sections corresponding to the number of elements of that subtype. Alternatively, if you want the plot to show relative
frequencies, the bars could all be the same height, but the shading corresponds to
the fraction of elements of that subtype. This is all much harder to say than to see
or to do (Figure 12.2).
Section 12.1
Goals by gender
Plotting 2D Data
241
250
Gender by goals
Sports
Popular
Grades
50
50
100
100
150
150
200
200
girl
boy
Grades
Popular
Sports
boy
girl
Gender
1.0
1.0
Goals
Sports
Popular
Grades
0.0
0.0
0.2
0.2
0.4
0.4
0.6
0.6
0.8
0.8
girl
boy
Grades
Popular
Sports
Goals
boy
girl
Gender
FIGURE 12.2: These bar charts use stacked bars. In the top row, the overall height
of the bar is given by the number of elements of that type but each different subtype is
identified by shading, so you can tell by eye, for example, how many of the Grades
in the study were Boys. This layout makes it hard to tell what fraction of, say,
Boys aspire to Popularity. In the bottom row, all bars have the same height,
but the shading of the bar identifies the fraction of that type that has a corresponding
subtype. This means you can tell by eye what fraction of Girls aspire to Sports.
An alternative to a pie chart that is very useful for two dimensional data is
a heat map. This is a method of displaying a matrix as an image. Each entry of
the matrix is mapped to a color, and the matrix is represented as an image. For
the Chase and Dunner study, I constructed a matrix where each row corresponds
to a choice of sports, grades, or popular, and each column corresponds to a
choice of boy or girl. Each entry contains the count of data items of that type.
Zero values are represented as white; the largest values as red; and as the value
increases, we use an increasingly saturated pink. This plot is shown in figure 12.3
If the categorical data is ordinal, the ordering offers some hints for making
a good plot. For example, imagine we are building a user interface. We build an
initial version, and collect some users, asking each to rate the interface on scales for
Section 12.1
Plotting 2D Data
242
120
Sports
100
Grades
80
60
Popular
bo
gi
rl
40
FIGURE 12.3: A heat map of the Chase and Dunner data. The color of each cell
corresponds to the count of the number of elements of that type. The colorbar at
the side gives the correspondence between color and count. You can see at a glance
that the number of boys and girls who prefer grades is about the same; that about
the same number of boys prefer sports and popularity, with sports showing a mild
lead; and that more girls prefer popularity to sports.
-2
-1
0
1
2
-2
24
6
2
0
0
-1
5
12
4
0
0
0
0
3
13
3
0
1
0
0
6
13
1
2
1
0
0
2
5
Each cell in the table on the left contains the count of users rating ease of use
(horizontal, on a scale of -2 -very bad- to 2 -very good) vs. enjoyability (vertical,
same scale). Users who found the interface hard to use did not like using it either.
While this data is categorical, its also ordinal, so that the order of the cells is
determined. It wouldnt make sense, for example, to reorder the columns of the
table or the rows of the table.
ease of use (-2, -1, 0, 1, 2, running from bad to good) and enjoyability (again,
-2, -1, 0, 1, 2, running from bad to good). It is natural to build a 5x5 table, where
each cell represents a pair of ease of use and enjoyability values. We then count
the number of users in each cell, and build graphical representations of this table.
One natural representation is a 3D bar chart, where each bar sits on its cell in the
2D table, and the height of the bars is given by the number of elements in the cell.
Table 12.1 shows a table and figure 12.4 shows a 3D bar chart for some simulated
data. The main difficulty with a 3D bar chart is that some bars are hidden behind
others. This is a regular nuisance. You can improve things by using an interactive
tool to rotate the chart to get a nice view, but this doesnt always work. Heatmaps
dont suffer from this problem (Figure 12.4), another reason they are a good choice.
Section 12.1
Plotting 2D Data
243
20
Enjoyability
30
20
10
0
2
15
0
10
1
5
Enjoyability
2 1
2
0 1
Ease of use
2
2
0
1
Ease of use
FIGURE 12.4: On the left, a 3D bar chart of the data. The height of each bar is
given by the number of users in each cell. This figure immediately reveals that users
who found the interface hard to use did not like using it either. However, some of
the bars at the back are hidden, so some structure might be hard to infer. On the
right, a heat map of this data. Again, this figure immediately reveals that users
who found the interface hard to use did not like using it either. Its more apparent
that everyone disliked the interface, though, and its clear that there is no important
hidden structure.
Remember this:
There are a variety of tools for plotting categorical
data. Its difficult to give strict rules for which to use when, but usually
one tries to avoid pie charts (angles are hard to judge by eye) and 3D bar
charts (where occlusion can hide important effects).
12.1.2 Series
Sometimes one component of a dataset gives a natural ordering to the data. For
example, we might have a dataset giving the maximum rainfall for each day of
the year. We could record this either by using a two-dimensional representation,
where one dimension is the number of the day and the other is the temperature,
or with a convention where the ith data item is the rainfall on the ith day. For
example, at http://lib.stat.cmu.edu/DASL/Datafiles/timeseriesdat.html, you can find
four datasets indexed in this way. It is natural to plot data like this as a function
of time. From this dataset, I extracted data giving the number of burglaries each
month in a Chicago suburb, Hyde Park. I have plotted part this data in Figure 12.5
(I left out the data to do with treatment effects). It is natural to plot a graph of
the burglaries as a function of time (in this case, the number of the month). The
plot shows each data point explicitly. I also told the plotting software to draw
lines joining data points, because burglaries do not all happen on a specific day.
Section 12.1
100
Plotting 2D Data
244
80
1000
60
40
500
20
0
0
10
20
30
month
40
0
1840
1860
1880
year
1900
1920
FIGURE 12.5: Left, the number of burglaries in Hyde Park, by month. Right, a
plot of the number of lynx pelts traded at Hudson Bay and of the price paid per pelt,
as a function of the year. Notice the scale, and the legend box (the number of pelts
is scaled by 100).
The lines suggest, reasonably enough, the rate at which burglaries are happening
between data points.
FIGURE 12.6: Snows scatter plot of cholera deaths on the left. Each cholera death
is plotted as a small bar on the house in which the bar occurred (for example, the
black arrow points to one stack of these bars, indicating many deaths, in the detail
on the right). Notice the fairly clear pattern of many deaths close to the Broad
street pump (grey arrow in the detail), and fewer deaths further away (where it was
harder to get water from the pump).
As another example, at http://lib.stat.cmu.edu/datasets/Andrews/ you can
Section 12.1
Plotting 2D Data
245
find a dataset that records the number of lynx pelts traded to the Hudsons Bay
company and the price paid for each pelt. This version of the dataset appeared first
in table 3.2 of Data: a Collection of Problems from many Fields for the Student
and Research Worker by D.F. Andrews and A.M. Herzberg, published by Springer
in 1985. I have plotted it in figure 12.5. The dataset is famous, because it shows
a periodic behavior in the number of pelts (which is a good proxy for the number
of lynx), which is interpreted as a result of predator-prey interactions. Lynx eat
rabbits. When there are many rabbits, lynx kittens thrive, and soon there will
be many lynx; but then they eat most of the rabbits, and starve, at which point
the rabbit population rockets. You should also notice that after about 1900, prices
seem to have gone up rather quickly. I dont know why this is. There is also some
suggestion, as there should be, that prices are low when there are many pelts, and
high when there are few.
12.1.3 Scatter Plots for Spatial Data
It isnt always natural to plot data as a function. For example, in a dataset containing the temperature and blood pressure of a set of patients, there is no reason
to believe that temperature is a function of blood pressure, or the other way round.
Two people could have the same temperature, and different blood pressures, or
vice-versa. As another example, we could be interested in what causes people to
die of cholera. We have data indicating where each person died in a particular
outbreak. It isnt helpful to try and plot such data as a function.
The scatter plot is a powerful way to deal with this situation. In the first
Section 12.1
Heart rate
80
70
60
96
98
100
Body temperature
246
6
Normalized heart rate
2 2
212
2 2122122 1
1
222121211212
1
1 12221 2222112112122 22
211 111121 2 212 1
1 1111212112 212
1
11 2111112 1 1
12
2111 2211211 2 2
2 1 21 12 2
1 121 22 1
2
222
2
2 1 2
90
Plotting 2D Data
102
4
2
0
2
2 11
1
12
111
21
11 12 2212
22
22
1 22
2
2
1
1
1
2
1
2
2
2
2
2
2
1
1211 2 212212 1 2
2 11
1 112
221211211212
11 21 1
21 1 2
1112 2
12
2 11
1 212
2 1222121
1221 1
11
2 2
1
4
6
6
4
2
0
2
4
Normalized body temperature
FIGURE 12.8: A scatter plot of body temperature against heart rate, from the dataset
at http:// www2.stetson.edu/ jrasp/ data.htm; normtemp.xls. I have separated the
two genders by plotting a different symbol for each (though I dont know which
gender is indicated by which letter); if you view this in color, the differences in color
makes for a greater separation of the scatter. This picture suggests, but doesnt
conclusively establish, that there isnt much dependence between temperature and
heart rate, and any dependence between temperature and heart rate isnt affected by
gender.
instance, assume that our data points actually describe points on the a real map.
Then, to make a scatter plot, we make a mark on the map at a place indicated by
each data point. What the mark looks like, and how we place it, depends on the
particular dataset, what we are looking for, how much we are willing to work with
complex tools, and our sense of graphic design.
Figure 12.6 is an extremely famous scatter plot, due to John Snow. Snow
one of the founders of epidemiology used a scatter plot to reason about a cholera
outbreak centered on the Broad Street pump in London in 1854. At that time,
the mechanism that causes cholera was not known. Snow plotted cholera deaths as
little bars (more bars, more deaths) on the location of the house where the death
occurred. More bars means more deaths, fewer bars means fewer deaths. There
are more bars per block close to the pump, and few far away. This plot offers quite
strong evidence of an association between the pump and death from cholera. Snow
used this scatter plot as evidence that cholera was associated with water, and that
the Broad Street pump was the source of the tainted water.
Figure 12.7 shows a scatter plot of arsenic levels in groundwater for the United
States, prepared by the US Geological Survey. The data set was collected by Focazio
and others in 2000; by Welch and others in 2000; and then updated by Ryker 2001.
It can be found at http://water.usgs.gov/GIS/metadata/usgswrd/XML/arsenic map.
xml. One variant of a scatter plot that is particularly useful for geographic data
occurs when one fills regions on a map with different colors, following the data in
that region. Figure 12.7 shows the nitrogen usage by US county in 1991; again,
this figure was prepared by the US Geological Survey.
Section 12.1
Weights
350
300
250
200
150
100
20
247
300
Weights, outliers removed
400
Plotting 2D Data
40
Heights
60
250
200
150
100
60
80
65
70
75
Heights, outliers removed
80
300
400
250
300
Weights
FIGURE 12.9: A scatter plots of weight against height, from the dataset at http://
www2.stetson.edu/ jrasp/ data.htm. Left: Notice how two outliers dominate the
picture, and to show the outliers, the rest of the data has had to be bunched up.
Right shows the data with the outliers removed. The structure is now somewhat
clearer.
200
150
100
60
200
100
65
70
75
Heights, outliers removed
80
0
60
65
70
Heights
75
80
FIGURE 12.10: Scatter plots of weight against height, from the dataset at http://
www2.stetson.edu/ jrasp/ data.htm. Left: data with two outliers removed, as in
figure 12.9. Right: this data, rescaled slightly. Notice how the data looks less
spread out. But there is no difference between the datasets. Instead, your eye is
easily confused by a change of scale.
Remember this:
Scatter plots are a most effective tool for geographic
data and 2D data in general. A scatter plot should be your first step with a
new 2D dataset.
Section 12.1
Plotting 2D Data
248
1500
1000
500
0
0
2
4
6
number of pelts traded
8
x 10
FIGURE 12.11: A scatter plot of the price of lynx pelts against the number of pelts.
I have plotted data for 1901 to the end of the series as circles, and the rest of the
data as *s. It is quite hard to draw any conclusion from this data, because the scale
is confusing. Furthermore, the data from 1900 on behaves quite differently from the
other data.
12.1.4 Exposing Relationships with Scatter Plots
Scatter plots are natural for geographic data, but a scatter plot is a useful, simple
tool for ferreting out associations in other kinds of data as well. Now we need
some notation. Assume we have a dataset {x} of N data items, x1 , . . . , xN . Each
data item is a d dimensional vector (so its components are numbers). We wish to
investigate the relationship between two components of the dataset. For example,
we might be interested in the 7th and the 13th component of the dataset. We
will produce a two-dimensional plot, one dimension for each component. It does
not really matter which component is plotted on the x-coordinate and which on
the y-coordinate (though it will be some pages before this is clear). But it is very
difficult to write sensibly without talking about the x and y coordinates.
We will make a two-dimensional dataset out of the components that interest
us. We must choose which component goes first in the resulting 2-vector. We will
plot this component on the x-coordinate (and we refer to it as the x-coordinate),
and to the other component as the y-coordinate. This is just to make it easier to
describe what is going on; theres no important idea here. It really will not matter
which is x and which is y. The two components make a dataset {xi } = {(xi , yi )}.
To produce a scatter plot of this data, we plot a small shape at the location of each
data item.
Such scatter plots are very revealing. For example, figure 12.8 shows a scatter
plot of body temperature against heart rate for humans. In this dataset, the gender
of the subject was recorded (as 1 or 2 I dont know which is which), and
so I have plotted a 1 at each data point with gender 1, and so on. Looking
at the data suggests there isnt much difference between the blob of 1 labels and
the blob of 2 labels, which suggests that females and males are about the same
in this respect.
The scale used for a scatter plot matters. For example, plotting lengths in
Section 12.1
Plotting 2D Data
249
meters gives a very different scatter from plotting lengths in millimeters. Figure 12.9 shows two scatter plots of weight against height. Each plot is from the
same dataset, but one is scaled so as to show two outliers. Keeping these outliers
means that the rest of the data looks quite concentrated, just because the axes
are in large units. In the other plot, the axis scale has changed (so you cant see
the outliers), but the data looks more scattered. This may or may not be a misrepresentation. Figure 12.10 compares the data with outliers removed, with the
same plot on a somewhat different set of axes. One plot looks as though increasing
height corresponds to increasing weight; the other looks as though it doesnt. This
is purely due to deceptive scaling each plot shows the same dataset.
Dubious data can also contribute to scaling problems. Recall that, in figure 12.5, price data before and after 1900 appeared to behave differently. Figure 12.11 shows a scatter plot of the lynx data, where I have plotted number of
pelts against price. I plotted the post-1900 data as circles, and the rest as asterisks. Notice how the circles seem to form a quite different figure, which supports the
suggestion that something interesting happened around 1900. We can reasonably
choose to analyze data after 1900 separately from before 1900. A choice like this
should be made with care. If you exclude every data point that might disagree with
your hypothesis, you may miss the fact that you are wrong. Leaving out data is
an essential component of many kinds of fraud. You should always reveal whether
you have excluded data, and why, to allow the reader to judge the evidence.
When you look at Figure 12.11, you should notice the scatter plot does not
seem to support the idea that prices go up when supply goes down. This is puzzling
because its generally a pretty reliable idea. In fact, the plot is just hard to interpret
because it is poorly scaled. Scale is an important nuisance, and its easy to get
misled by scale effects.
4
2
0
2
4
4
2
0
2
4
Heights, outliers removed, normalized
FIGURE 12.12: A normalized scatter plot of weight against height, from the dataset
at http:// www2.stetson.edu/ jrasp/ data.htm. Now you can see that someone who
is a standard deviation taller than the mean will tend to be somewhat heavier than
the mean too.
The way to avoid the problem is to plot in standard coordinates. We can
normalize without worrying about the dimension of the data we normalize each
Section 12.1
Heart rate
80
70
60
96
250
6
Normalized heart rate
2 2
212
2 2122122 1
1
222121211212
1
1 12221 2222112112122 22
211 111121 2 212 1
1 1111212112 212
1
11 2111112 1 1
12
2111 2211211 2 2
2 1 21 12 2
1 121 22 1
2
222
2
2 1 2
90
Plotting 2D Data
98
100
Body temperature
102
4
2
0
2
2 11
1
12
111
21
11 12 2212
22
22
1 22
2
2
1
1
1
2
1
2
2
2
2
2
2
1
1211 2 212212 1 2
2 11
1 112
221211211212
11 21 1
21 1 2
1112 2
12
2 11
1 212
2 1222121
1221 1
11
2 2
1
4
6
6
4
2
0
2
4
Normalized body temperature
FIGURE 12.13: Left: A scatter plot of body temperature against heart rate, from the
dataset at http:// www2.stetson.edu/ jrasp/ data.htm; normtemp.xls. I have separated the two genders by plotting a different symbol for each (though I dont know
which gender is indicated by which letter); if you view this in color, the differences
in color makes for a greater separation of the scatter. This picture suggests, but
doesnt conclusively establish, that there isnt much dependence between temperature
and heart rate, and any dependence between temperature and heart rate isnt affected
by gender. The scatter plot of the normalized data, in standard coordinates, on the
right supports this view.
4
3
normalized price
1500
1000
500
2
1
0
1
0
0
2
4
6
number of pelts traded
8
4
x 10
2
2
1
0
1
2
normalized number of pelts
FIGURE 12.14: Left: A scatter plot of the price of lynx pelts against the number of
pelts (this is a repeat of figure 12.11 for reference). I have plotted data for 1901
to the end of the series as circles, and the rest of the data as *s. It is quite hard
to draw any conclusion from this data, because the scale is confusing. Right: A
scatter plot of the price of pelts against the number of pelts for lynx pelts. I excluded
data for 1901 to the end of the series, and then normalized both price and number
of pelts. Notice that there is now a distinct trend; when there are fewer pelts, they
are more expensive, and when there are more, they are cheaper.
Section 12.2
Correlation
251
Remember this:
The plot scale can mask effects in scatter plots, and
its usually a good idea to plot in standard coordinates.
4
2
0
2
4
4
2
0
2
4
Heights, outliers removed, normalized
12.2 CORRELATION
4
2
0
2
4
4
2
0
2
4
Weights, outliers removed, normalized
FIGURE 12.15: On the left, a normalized scatter plot of weight (y-coordinate) against
height (x-coordinate). On the right, a scatter plot of height (y-coordinate) against
weight (x-coordinate). Ive put these plots next to one another so you dont have to
mentally rotate (which is what you should usually do).
The simplest, and most important, relationship to look for in a scatter plot is
this: when x
increases, does y tend to increase, decrease, or stay the same? This is
straightforward to spot in a normalized scatter plot, because each case produces a
very clear shape on the scatter plot. Any relationship is called correlation (we will
see later how to measure this), and the three cases are: positive correlation, which
means that larger x
values tend to appear with larger y values; zero correlation,
which means no relationship; and negative correlation, which means that larger x
values tend to appear with smaller y values. I have shown these cases together
in one figure using a real data example (Figure 12.16), so you can compare the
appearance of the plots.
4
2
0
2
2 211
121 1
1
221111
11 12 2212
2222
222 22 2
212
222121
2
1
1
11
1212
2 11
1 122
2111211 11
2
2
2
1
1
2
1
1
1
12
21 2
2
2 11
1112 2
1
1
2
1
1
2
2
2 1222111
1 21
2 22 1
1
4
6
6
4
2
0
2
4
Normalized body temperature
Positive Correlation
Correlation
252
Negative Correlation
4
3
normalized price
No Correlation
6
Section 12.2
0
2
2
1
0
1
4
4
2
0
2
4
Heights, outliers removed, normalized
2
2
1
0
1
2
normalized number of pelts
FIGURE 12.16: The three kinds of scatter plot are less clean for real data than for
our idealized examples. Here I used the body temperature vs heart rate data for the
zero correlation; the height-weight data for positive correlation; and the lynx data
for negative correlation. The pictures arent idealized real data tends to be messy
but you can still see the basic structures.
Section 12.2
Correlation
253
be few data points in the strip, because there arent many big x
values. If there
is no relationship, we dont expect to see large or small y values in this strip,
because there are few data points in the strip and because large or small y values
are uncommon we see them only if there are many data points, and then seldom.
So for a strip with x
close to zero, we might see some y values that are far from
zero because we will see many y values. For a strip with x that is far from zero,
we expect to see few y values that are far from zero, because we see few points in
this strip. This reasoning means the data should form a round blob, centered at
the origin. In the temperature-heart rate plot of figure 12.13, it looks as though
nothing of much significance is happening. The average heart rate seems to be
about the same for people who run warm or who run cool. There is probably not
much relationship here.
The correlation is not affected by which variable is plotted on the x-axis and
which is plotted on the y-axis. Figure 12.15 compares a plot of height against
weight to one of weight against height. Usually, one just does this by rotating the
page, or by imagining the new picture. The left plot tells you that data points
with higher height value tend to have higher weight value; the right plot tells you
that data points with higher weight value tend to have higher height value i.e.
the plots tell you the same thing. It doesnt really matter which one you look at.
Again, the important word is tend the plot doesnt tell you anything about
why, it just tells you that when one variable is larger the other tends to be, too.
12.2.1 The Correlation Coefficient
Consider a normalized data set of N two-dimensional vectors. We can write the
ith data point in standard coordinates (
xi , yi ). We already know many important
summaries of this data, because it is in standard coordinates. We have mean ({
x}) =
0; mean ({
y }) = 0; std ({
x}) = 1; and std ({
y }) = 1. Each
of
these
summaries
is
2
2
2
itself the
mean
of
some
monomial.
So
std
({
x
})
=
mean
x
=
1;
std
({
y
})
=
mean y2 (the other two are easy). We can rewrite this information
in
terms
2
of means ofmonomials,
giving mean ({
x}) = 0; mean ({
y }) = 0; mean x
= 1;
and mean y2 = 1. There is one monomial missing here, which is x
y.
The term mean ({
xy}) captures correlation between x and y. The term is
known as the correlation coefficient or correlation.
Section 12.2
Correlation
254
Section 12.2
Height, normalized
2
0
2
2
0
2
2
0
2
Density, normalized
0
2
2
0
2
4
Adiposity, normalized
Density and Body Fat, correlation=0.98
4
Bodyfat, normalized
Adiposity, normalized
4
4
0
2
4
Age, normalized
Density and Adiposity, correlation=0.73
6
4
4
255
4
4
Correlation
2
0
2
4
4
2
0
2
Density, normalized
FIGURE 12.17: Scatter plots for various pairs of variables for the age-height-weight
dataset from http:// www2.stetson.edu/ jrasp/ data.htm; bodyfat.xls. In each case,
two outliers have been removed, and the plots are in standard coordinates (compare
to figure 12.18, which shows these data sets plotted in their original units). The
legend names the variables.
The correlation coefficient is symmetric (it doesnt depend on the order of its
arguments), so
corr ({(x, y)}) = corr ({(y, x)})
The value of the correlation coefficient is not changed by translating the data.
Scaling the data can change the sign, but not the absolute value. For constants
a 6= 0, b, c 6= 0, d we have
corr ({(ax + b, cx + d)}) = sign(ab)corr ({(x, y)})
If y tends to be large (resp. small) for large (resp. small) values of x
, then
the correlation coefficient will be positive.
If y tends to be small (resp. large) for large (resp. small) values of x
, then
the correlation coefficient will be negative.
Section 12.2
75
70
65
35
30
25
20
1.3
40
60
80
100
Age, NOT normalized
Density and Adiposity, correlation=0.73
40
300
250
200
150
100
15
60
20
1
1.1
1.2
Density, NOT normalized
256
15
0.9
Correlation
20
25
30
35
40
Adiposity, NOT normalized
Density and Body Fat, correlation=0.98
50
40
30
20
10
0
0.9
1
1.1
1.2
Density, NOT normalized
1.3
FIGURE 12.18: Scatter plots for various pairs of variables for the age-height-weight
dataset from http:// www2.stetson.edu/ jrasp/ data.htm; bodyfat.xls. In each case,
two outliers have been removed, and the plots are NOT in standard coordinates
(compare to figure 12.17, which shows these data sets plotted in normalized coordinates). The legend names the variables.
If y doesnt depend on x
, then the correlation coefficient is zero (or close to
zero).
The largest possible value is 1, which happens when x
= y.
The smallest possible value is -1, which happens when x
=
y.
The first property is easy, and we relegate that to the exercises. One way to
see that the correlation coefficient isnt changed by translation or scale is to notice
that it is defined in standard coordinates, and scaling or translating data doesnt
change those. Another way to see this is to scale and translate data, then write out
the equations; notice that taking standard coordinates removes the effects of the
scale and translation. In each case, notice that if the scale is negative, the sign of
the correlation coefficient changes.
The property that, if y tends to be large (resp. small) for large (resp. small)
values of x
, then the correlation coefficient will be positive, doesnt really admit
Section 12.2
Correlation
257
a formal statement. But its relatively straightforward to see whats going on.
Because mean ({
x}) = 0, small values of mean ({
x}) must be negative and large
P
x
i yi
i
; and for this sum to be
values must be positive. But corr ({(x, y)}) =
N
positive, it should contain mostly positive terms. It can contain few or no hugely
positive (or hugely negative) terms, because std (
x) = std (
y) = 1 so there arent
many large (or small) numbers. For the sum to contain mostly positive terms, then
the sign of x
i should be the same as the sign yi for most data items. Small changes
to this argument work to show that if if y tends to be small (resp. large) for large
(resp. small) values of x
, then the correlation coefficient will be negative.
Showing that no relationship means zero correlation requires slightly more
work. Divide the scatter plot of the dataset up into thin vertical strips. There
are S strips. Each strip is narrow, so the x
value does not change much for the
data points in a particular strip. For the sth strip, write N (s) for the number of
data points in the strip, x
(s) for the x value at the center of the strip, and y(s)
for the mean of the y values within that strip. Now the strips are narrow, so we
can approximate all data points within a strip as having the same value of x
. This
yields
1 X
y (s)
mean ({
xy})
x
(s) N (s)
S
sstrips
(where you could replace with = if the strips were narrow enough). Now assume
that y(s) does not change from strip to strip, meaning that there is no relationship between x
and y in this dataset (so the picture is like the left hand side in
figure 12.16). Then each value of y(s) is the same we write y and we can
rearrange to get
1 X
mean ({
xy}) y
x
(s).
S
sstrips
Now notice that
0 = mean ({
y })
1
S
y (s)
N (s)
sstrips
(where again you could replace with = if the strips were narrow enough). This
means that if every strip has the same value of y(s), then that value must be zero.
In turn, if there is no relationship between x
and y, we must have mean ({
xy}) = 0.
Section 12.2
Correlation
258
Proposition:
1 corr ({(x, y)}) 1
Proof: Writing x, y for the normalized coefficients, we have
P
x
i yi
corr ({(x, y)}) = i
N
and you can think of the value as the inner product of two vectors. We write
1
1
x1 , x
2 , . . . x
N ] and y = [
y1 , y2 , . . . yN ]
x = [
N
N
2
and we have corr ({(x, y)}) = xT y. Notice xT x = std (x) = 1, and similarly
for y. But the inner product of two vectors is at its maximum when the two
vectors are the same, and this maximum is 1. This argument is also sufficient to
show that smallest possible value of the correlation is 1, and this occurs when
x
i =
yi for all i.
Property 12.1: The largest possible value of the correlation is 1, and this occurs
when x
i = yi for all i. The smallest possible value of the correlation is 1, and
this occurs when x
i =
yi for all i.
12.2.2 Using Correlation to Predict
Assume we have N data items which are 2-vectors (x1 , y1 ), . . . , (xN , yN ), where
N > 1. These could be obtained, for example, by extracting components from
larger vectors. As usual, we will write xi for xi in normalized coordinates, and so
on. Now assume that we know the correlation coefficient is r (this is an important,
traditional notation). What does this mean?
One (very useful) interpretation is in terms of prediction. Assume we have a
data point (x0 , ?) where we know the x-coordinate, but not the y-coordinate. We
can use the correlation coefficient to predict the y-coordinate. First, we transform
to standard coordinates. Now we must obtain the best y0 value to predict, using
the x
0 value we have.
We want to construct a prediction function which gives a prediction for any
value of x
. This predictor should behave as well as possible on our existing data.
For each of the (
xi , yi ) pairs in our data set, the predictor should take x
i and
produce a result as close to yi as possible. We can choose the predictor by looking
at the errors it makes at each data point.
We write yip for the value of yi predicted at x
i . The simplest form of predictor
is linear. If we predict using a linear function, then we have, for some unknown
a, b, that yip = a
xi + b. Now think about ui = yi yip , which is the error in our
prediction. We would like to have mean ({u}) = 0 (otherwise, we could reduce the
Section 12.2
Correlation
259
mean ({
y yp })
mean ({
y }) mean ({a
xi + b})
mean ({
y }) amean ({
x}) + b
0 a0 + b
0.
var ({
y yp })
because mean ({u}) = 0
mean (
y a
x)2
2
mean (
y ) 2a
xy + a2 (
x)2
2
2
2amean ({
xy}) + a2 mean (
x)
mean (
y)
1 2ar + a2 ,
Section 12.2
Correlation
260
yi
x
0
1
(xi mean ({x}))
std (x)
1
(yi mean ({y}))
std (y)
1
(x0 mean ({x})).
std (x)
Section 12.2
Correlation
261
prediction becomes
(y p mean ({y}))
=r
std (y)
This gives a really useful rule of thumb, which I have broken out in the box below.
An even more compact version of the rule of thumb is in the following box.
We can compute the average root mean square error that this prediction
procedure will make. The square of this error must be
= mean y 2 2rmean ({xy}) + r2 mean x2
mean u2
=
1 2r2 + r2
1 r2
so the root mean square error will be 1 r2 . This is yet another interpretation of
correlation; if x and y have correlation close to one, then predictions could have very
small root mean square error, and so might be very accurate. In this case, knowing
one variable is about as good as knowing the other. If they have correlation close
to zero, then the root mean square error in a prediction might be as large as the
root mean square error in y which means the prediction is nearly a pure guess.
The prediction argument means that we can spot correlations for data in
other kinds of plots one doesnt have to make a scatter plot. For example, if
we were to observe a childs height from birth to their 10th year (you can often
find these observations in ballpen strokes, on kitchen walls), we could plot height
as a function of year. If we also had their weight (less easily found), we could plot
weight as a function of year, too. The prediction argument above say that, if you
can predict the weight from the height (or vice versa) then theyre correlated. One
way to spot this is to look and see if one curve goes up when the other does (or
Section 12.2
Correlation
262
FIGURE 12.19: This figure, from Vickers (ibid, p184) shows a plot of the stork
population as a function of time, and the human birth rate as a function of time, for
some years in Germany. The correlation is fairly clear; but this does not mean that
reducing the number of storks means there are fewer able to bring babies. Instead,
this is the impact of the first world war a hidden or latent variable.
goes down when the other goes up). You can see this effect in figure 12.5, where
(before 19h00), prices go down when the number of pelts goes up, and vice versa.
These two variables are negatively correlated.
12.2.3 Confusion caused by correlation
There is one very rich source of potential (often hilarious) mistakes in correlation.
When two variables are correlated, they change together. If the correlation is
positive, that means that, in typical data, if one is large then the other is large,
and if one is small the other is small. In turn, this means that one can make
a reasonable prediction of one from the other. However, correlation DOES NOT
mean that changing one variable causes the other to change (sometimes known as
causation).
Two variables in a dataset could be correlated for a variety of reasons. One
important reason is pure accident. If you look at enough pairs of variables, you
may well find a pair that appears to be correlated just because you have a small
set of observations. Imagine, for example, you have a dataset consisting of only
two vectors there is a pretty good chance that there is some correlation between
the coefficients. Such accidents can occur in large datasets, particularly if the
dimensions are high.
Another reason variables could be correlated is that there is some causal
relationship for example, pressing the accelerator tends to make the car go
faster, and so there will be some correlation between accelerator position and car
acceleration. As another example, adding fertilizer does tend to make a plant grow
bigger. Imagine you record the amount of fertilizer you add to each pot, and the
size of the resulting potplant. There should be some correlation.
Yet another reason variables could be correlated is that there is some other
background variable often called a latent variable linked causally to each of
Section 12.3
263
the observed variables. For example, in children (as Freedman, Pisani and Purves
note in their excellent Statistics), shoe size is correlated with reading skills. This
DOES NOT mean that making your feet grow will make you read faster, or that
you can make your feet shrink by forgetting how to read. The real issue here is
the age of the child. Young children tend to have small feet, and tend to have
weaker reading skills (because theyve had less practice). Older children tend to
have larger feet, and tend to have stronger reading skills (because theyve had more
practice). You can make a reasonable prediction of reading skills from foot size,
because theyre correlated, even though there is no direct connection.
This kind of effect can mask correlations, too. Imagine you want to study the
effect of fertilizer on potplants. You collect a set of pots, put one plant in each,
and add different amounts of fertilizer. After some time, you record the size of each
plant. You expect to see correlation between fertilizer amount and plant size. But
you might not if you had used a different species of plant in each pot. Different
species of plant can react quite differently to the same fertilizer (some plants just
die if over-fertilized), so the species could act as a latent variable. With an unlucky
choice of the different species, you might even conclude that there was a negative
correlation between fertilizer and plant size. This example illustrates why you need
to take great care in setting up experiments and interpreting their results.
This sort of thing happens often, and its an effect you should look for. Another nice example comes from Vickers (ibid). The graph, shown in Figure 12.19,
shows a plot of (a) a dataset of the stork population in Europe over a period of
years and (b) a dataset of the birth rate over those years. This isnt a scatter plot;
instead, the data has been plotted on a graph. You can see by eye that these two
datasets are quite strongly correlated . Even more disturbing, the stork population dropped somewhat before the birth rate dropped. Is this evidence that storks
brought babies in Europe during those years? No (the usual arrangement seems
to have applied). For a more sensible explanation, look at the dates. The war
disturbed both stork and human breeding arrangements. Storks were disturbed
immediately by bombs, etc., and the human birth rate dropped because men died
at the front.
12.3 STERILE MALES IN WILD HORSE HERDS
Large herds of wild horses are (apparently) a nuisance, but keeping down numbers
by simply shooting surplus animals would provoke outrage. One strategy that
has been adopted is to sterilize males in the herd; if a herd contains sufficient
sterile males, fewer foals should result. But catching stallions, sterilizing them, and
reinserting them into a herd is a performance does this strategy work?
We can get some insight by plotting data. At http://lib.stat.cmu.edu/DASL/
Datafiles/WildHorses.html, you can find a dataset covering herd management in wild
horses. I have plotted part of this dataset in figure 12.20. In this dataset, there
are counts of all horses, sterile males, and foals made on each of a small number
of days in 1986, 1987, and 1988 for each of two herds. I extracted data for one
herd. I have plotted this data as a function of the count of days since the first data
point, because this makes it clear that some measurements were taken at about
the same time, but there are big gaps in the measurements. In this plot, the data
Section 12.3
264
Number of horses
80
Adults
Sterile Males
Foals
60
40
20
0
0
200
400
Day
600
800
FIGURE 12.20: A plot of the number of adult horses, sterile males, and foals in horse
herds over a period of three years. The plot suggests that introducing sterile males
might cause the number of foals to go down. Data from http:// lib.stat.cmu.edu/
DASL/ Datafiles/ WildHorses.html.
points are shown with a marker. Joining them leads to a confusing plot because
the data points vary quite strongly. However, notice that the size of the herd drifts
down slowly (you could hold a ruler against the plot to see the trend), as does the
number of foals, when there is a (roughly) constant number of sterile males.
Does sterilizing males result in fewer foals? This is likely hard to answer for
this dataset, but we could ask whether herds with more sterile males have fewer
foals. A scatter plot is a natural tool to attack this question. However, the scatter
plots of figure 12.21 suggest, rather surprisingly, that when there are more sterile
males there are more adults (and vice versa), and when there are more sterile
males there are more foals (and vice versa). This is borne out by a correlation
analysis. The correlation coefficient between foals and sterile males is 0.74, and
the correlation coefficient between adults and sterile males is 0.68. You should find
this very surprising how do the horses know how many sterile males there are
in the herd? You might think that this is an effect of scaling the plot, but there is
a scatter plot in normalized coordinates in figure 12.21 that is entirely consistent
with the conclusions suggested by the unnormalized plot. What is going on here?
The answer is revealed by the scatter plots of figure 12.22. Here, rather than
plotting a * at each data point, I have plotted the day number of the observation.
This is in days from the first observation. You can see that the whole herd is
shrinking observations where there are many adults (resp. sterile adults, foals)
occur with small day numbers, and observations where there are few have large day
numbers. Because the whole herd is shrinking, it is true that when there are more
adults and more sterile males, there are also more foals. Alternatively, you can see
the plots of figure 12.20 as a scatter plot of herd size (resp. number of foals, number
of sterile males) against day number. Then it becomes clear that the whole herd is
shrinking, as is the size of each group. To drive this point home, we can look at the
correlation coefficient between adults and days (-0.24), between sterile adults and
days (-0.37), and between foals and days (-0.61). We can use the rule of thumb in
Section 12.3
Number of foals
20
15
10
5
5
10
15
Number of sterile adults
Foals vs. sterile adults (standard coordinates)
3
2
1
0
1
0
1
2
3
N sterile adults (standard coordinates)
10
0
20
N sterile adults (standard coordinates)
0
0
1
2
265
30
40
50
60
Number of adults
70
1
0
1
N adults (standard coordinates)
FIGURE 12.21: Scatter plots of the number of sterile males in a horse herd against
the number of adults, and the number of foals against the number of sterile males,
from data of http:// lib.stat.cmu.edu/ DASL/ Datafiles/ WildHorses.html. Top: unnormalized; bottom: standard coordinates.
box 12.3 to interpret this. This means that every 282 days, the herd loses about
three adults; about one sterile adult; and about three foals. For the herd to have
a stable size, it needs to gain by birth as many foals as it loses both to growing up
and to death. If the herd is losing three foals every 282 days, then if they all grow
up to replace the missing adults, the herd will be shrinking slightly (because it is
losing four adults in this time); but if it loses foals to natural accidents, etc., then
it is shrinking rather fast.
The message of this example is important. To understand a simple dataset,
you might need to plot it several ways. You should make a plot, look at it and ask
what it says, and then try to use another type of plot to confirm or refute what
you think might be going on.
40
67
1
0
360
66
336 0
361
772
39
738
696
710
404
742
700 374
335
375
2
2
1
0
1
2
N adults (standard coordinates)
Section 12.4
You should
266
67
40
0
404
772
700
742
361
374
336
335 375
66
696
39
710
738
360
2
1
0
1
2
N adults (standard coordinates)
FIGURE 12.22: Scatter plots of the number of foals vs. the number of adults and
the number of adults vs. the number of sterile adults for the wild horse herd,
from http:// lib.stat.cmu.edu/ DASL/ Datafiles/ WildHorses.html. Rather than plot
data points as dots, I have plotted the day on which the observation was made.
Notice how the herd starts large, and then shrinks.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
233
233
234
235
236
238
240
Section 12.4
standard coordinates.
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
You should
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
267
.
.
.
.
.
.
244
244
246
246
247
255
C H A P T E R
13
Section 13.1
Discrete Distributions
269
this model to tell whether the difference between sample average and 15 grams is
easily explained by random variations in the sample, or is significant. This allows
us to tell how forcefully the evidence contradicts your original belief.
Building a model requires understanding the specific problem you want to
solve, then choosing from a vocabulary of many different models that might apply
to the problem. Long experience shows that even if the model you choose does not
match the problem exactly, it can still be useful. In this chapter, I describe the
properties of some probability distributions that are used again and again in model
building.
13.1 DISCRETE DISTRIBUTIONS
13.1.1 The Discrete Uniform Distribution
If every value of a discrete random variable has the same probability, then the
probability distribution is the discrete uniform distribution. We have seen this
distribution before, numerous times. For example, I define a random variable by
the number that shows face-up on the throw of a fair die. This has a uniform
distribution. As another example, write the numbers 1-52 on the face of each card
of a standard deck of playing cards. The number on the face of the first card drawn
from a well-shuffled deck is a random variable with a uniform distribution.
One can construct expressions for the mean and variance of a discrete uniform
distribution, but theyre not usually much use (too many terms, not often used).
Keep in mind that if two random variables have a uniform distribution, their sum
and difference will not (recall example ??).
13.1.2 Bernoulli Random Variables
A Bernoulli random variable models a biased coin with probability p of coming up
heads in any one flip.
Section 13.1
Discrete Distributions
270
Notice that the geometric distribution is non-negative everywhere. It is straightforward to show that it sums to one, and so is a probability distribution (exercises).
1p
p2 .
The proof of these facts requires some work with series, and is relegated
to the exercises.
Section 13.1
Discrete Distributions
271
It should be clear that this model isnt really about coins, but about repeated
trials. The trial could be anything that has some probability of failing. Each
trial is independent, and the rule for repeating is that you keep trying until the
first success. Textbooks often set exercises involving missiles and aircraft; Ill omit
these on the grounds of taste.
13.1.4 The Binomial Probability Distribution
Assume we have a biased coin with probability p of coming up heads in any one
flip. The binomial probability distribution gives the probability that it comes up
heads h times in N flips.
Worked example ?? yields one way of deriving this distribution. In that
example, I showed that there are
N !/(h!(N h)!)
outcomes of N coin flips that have h heads. These outcomes are disjoint, and
each has probability ph (1 p)(N h) . As a result, we must have the probability
distribution below.
Section 13.1
Discrete Distributions
272
Pb (i; N, p) = 1
i=0
Section 13.1
Discrete Distributions
273
N
X
Yi
E[X] = E
j=1
N
X
E[Yi ]
j=1
=
=
N E[Y1 ]
N p.
The variance is easy, too. Each coin toss is independent, so the variance
of the sum of coin tosses is the sum of the variances. This gives
N
X
var[X] = var
Yi
j=1
=
=
N var[Y1 ]
N p(1 p)
Section 13.1
Discrete Distributions
274
N!
pn1 pn2 . . . pnk k .
n1 !n2 !...nk ! 1 2
Dice
I throw five fair dice. What is the probability of getting two 2s and three 3s?
Solution:
5! 1 2 1 3
2!3! ( 6 ) ( 6 )
Section 13.1
Discrete Distributions
275
k e
,
k!
X
i
= e
i!
i=0
so that
X
k e
k=0
k!
=1
Section 13.2
Continuous Distributions
276
number of road-killed animals in two miles of road should be twice the number in
one mile of road. This property means that no pieces of the road are special
each behaves the same as the other.
We can build a really useful model of spatial randomness by observing this
fact and generalizing very slightly. A Poisson point process with intensity is a
set of random points with the property that the number of points in an interval of
length s is a Poisson random variable with parameter s. Notice how this captures
our intuition that if points are very randomly distributed, there should be twice
as many of them in an interval that is twice as long.
This model is easily, and very usefully, extended to points on the plane, on
surfaces, and in 3D. In each case, the process is defined on a domain D (which has
to meet some very minor conditions that are of no interest to us). The number
of points in any subset s of D is a Poisson random variable, with intensity m(s),
where m(s) is the area (resp. volume) of s. These models are useful, because they
capture the property that (a) the points are random and (b) the probability you
find a point doesnt depend on where you are. You could reasonably believe models
like this apply to, say, dead flies on windscreens; the places where you find acorns
at the foot of an oak tree; the distribution of cowpats in a field; the distribution of
cherries in a fruitcake; and so on.
13.2 CONTINUOUS DISTRIBUTIONS
13.2.1 The Continuous Uniform Distribution
Some continuous random variables have a natural upper bound and a natural lower
bound but otherwise we know nothing about them. For example, imagine we are
given a coin of unknown properties by someone who is known to be a skillful maker
of unfair coins. The manufacturer makes no representations as to the behavior of
the coin. The probability that this coin will come up heads is a random variable,
about which we know nothing except that it has a lower bound of zero and an
upper bound of one.
If we know nothing about a random variable apart from the fact that it has
a lower and an upper bound, then a uniform distribution is a natural model.
Write l for the lower bound and u for the upper bound. The probability density
function for the uniform distribution is
x<l
0
1/(u l) l x u
p(x) =
0
x>u
A continuous random variable whose probability distribution is the uniform distribution is often called a uniform random variable.
Section 13.2
2
0.2
0.4
0.6
0.8
alpha: 1, beta: 10
alpha: 10, beta: 1
alpha: 3, beta: 15
alpha: 20, beta: 100
10
0
0
277
alpha: 1, beta: 1
alpha: 10, beta: 10
alpha: 50, beta: 50
10
Continuous Distributions
0
0
0.2
0.4
0.6
0.8
FIGURE 13.1: Probability density functions for the Beta distribution with a variety
( + ) (1)
x
(1 x)(1) .
()()
+ .
2. The variance is
(+)2 (++1) .
Section 13.2
Continuous Distributions
278
0.5
0.5
0
0
10
0
0
10
FIGURE 13.2: Probability density functions for the Gamma distribution with a variety
(1) x
x
e
.
()
Figure 13.2 shows plots of the probability density function of the Gamma distribution for a variety of different values of and .
2 .
Section 13.3
279
2. The variance is
1
.
1
.
2
Notice the relationship between this parameter and the parameter of the Poisson distribution. If (say) the phone calls are distributed with Poisson distribution
with intensity (per hour), then your expected number of calls per hour is . The
time between calls will be exponentially distributed with parameter , and the
expected time to the next call is 1/ (in hours).
13.3 THE NORMAL DISTRIBUTION
13.3.1 The Standard Normal Distribution
The first step is to plot this probability density function (Figure 13.3). You
should notice it is quite familiar from work on histograms, etc. in Chapter 14.5. It
has the shape of the histogram of standard normal data, or at least the shape that
the histogram of standard normal data aspires to.
Section 13.3
280
FIGURE 13.3: A plot of the probability density function of the standard normal dis-
tribution. Notice how probability is concentrated around zero, and how there is
relatively little probability density for numbers with large absolute values.
has a standard normal distribution, then p(x) is a normal distribution. We can work
out the form of the probability density function of a general normal distribution in
two steps: first, we notice that for any normal distribution, we must have
(x )2
p(x) exp
.
2 2
Section 13.3
281
p(x)dx = 1.
p(x) =
exp
.
2 2
2
is a normal distribution.
Section 13.3
282
It is a remarkable and deep fact, known as the central limit theorem, that
adding many independent random variables produces a normal distribution pretty
much whatever the distributions of those random variables. Ive not shown this
in detail because its a nuisance to prove. However, if you add together many
random variables, each of pretty much any distribution, then the answer has a
distribution close to the normal distribution. It turns out that many of the processes
we observe add up subsidiary random variables. This means that you will see normal
distributions very often in practice.
A normal random variable tends to take values that are quite close to the
mean, measured in standard deviation units. We can demonstrate this important
fact by computing the probability that a standard normal random variable lies
between u and v. We form
2
Z v
u
1
exp
du.
2
2
u
It turns out that this integral can be evaluated relatively easily using a special
function. The error function is defined by
Z x
2
exp t2 dt
erf(x) =
0
so that
Z x
2
u
1
x
1
exp
du.
erf ( ) =
2
2
2
2
0
Notice that erf(x) is an odd function (i.e. erf(x) = erf(x)). From this (and
tables for the error function, or Matlab) we get that, for a standard normal random
variable
2
Z 1
1
x
exp
dx 0.68
2
2 1
and
and
2
x
exp
dx 0.95
2
2
2
x
exp
dx 0.99.
2
2
These are very strong statements. They measure how often a standard normal
random variable has values that are in the range 1, 1, 2, 2, and 3, 3 respectively.
But these measurements apply to normal random variables if we recognize that they
now measure how often the normal random variable is some number of standard
deviations away from the mean. In particular, it is worth remembering that:
Section 13.4
283
N!
ph q (N h)
h!(N h)!
The
mean of this distribution is N p, the variance is N pq, and the standard deviation
is N pq.
Evaluating this probability distribution for large N is very difficult, because
factorials grow fast. We will construct an approximation to the binomial distribution for large N that allows us to evaluate the probability that h lies in some range.
This approximation will show that the probability that h is within one standard
deviation of the mean is approximately 68%.
This is important, because it shows that our model of probability as frequency
is consistent. Consider the probability that the number of heads you
see lies within
one standard deviation of the mean. The size of that interval is 2 N pq. As N gets
bigger, the size of that interval, relative to the total number of flips, gets smaller.
If I flip a coin N times, in principle I could see a number of heads h that ranges
from 0 to N . However, we will establish that about 68% of the time, h will lie in
the interval within one standard deviation of the mean. The size of this interval,
relative to the total number of flips is
r
N pq
pq
=2
.
2
N
N
As a result, as N ,
h
p
N
because h will tend to land in an interval around pN that gets narrower as N gets
larger.
Section 13.4
284
Probability
0.3
0.2
0.1
0
0
0.15
0.1
0.05
2
3
Number of heads
P(k heads) in 40 flips
0
0
4
6
8
Number of heads
P(k heads) in 80 flips
10
20
40
60
Number of heads
80
0.08
Probability
Probability
0.1
0.05
0.06
0.04
0.02
0
0
10
20
30
Number of heads
40
0
0
FIGURE 13.4: Plots of the binomial distribution for p = q = 0.5 for different values
of N . You should notice that the set of values of h (the number of heads) that have
substantial probability is quite narrow compared to the range of possible values. This
set gets narrower as the number
of flips increases. This is because the mean is pN
and the standard deviation is N pq so the fraction of values that is within one
standard deviation of the mean is O(1/ N ).
The main difficulty with Figure 13.4 (and with the argument above) is that
the mean and standard deviation of the binomial distribution tends to infinity as
the number of coin flips tends to infinity. This can confuse issues. For example, the
plots of Figure 13.4 show narrowing probability distributions but is this because
the scale is compacted, or is there a real effect? It turns out there is a real effect,
and a good way to see it is to consider the normalized number of heads.
13.4.1 Large N
Recall that to normalize a dataset, you subtract the mean and divide the result
by the standard deviation. We can do the same for a random variable. We now
consider
h Np
x=
.
N pq
Section 13.4
285
N!
Probability
0.3
0.2
0.1
0
20
0.15
0.1
0.05
0
20
10
0
10
20
Number of heads
P(k heads) in 40 flips, normalized
10
0
10
20
Number of heads
P(k heads) in 80 flips, normalized
0.08
Probability
Probability
0.1
0.05
0.06
0.04
0.02
0
20
10
0
10
Number of heads
20
0
20
10
0
10
Number of heads
20
FIGURE 13.5: Plots of the distribution for the normalized variable x, with P (x) given
in the text, obtained from the binomial distribution with p = q = 0.5 for different
values of N . These distributions are normalized (mean 0, variance 1. They look
increasingly like a standard normal distribution EXCEPT that the value at their
mode gets smaller as N gets bigger (there are more possible outcomes). In the text,
we will establish that the standard normal distribution is a limit, in a useful sense.
But it is hard to work with this distribution for very large N . The factorials
become very
difficult to evaluate. Second, it is a discrete distribution on N points,
spaced 1/ N pq apart. As N becomes very large, the number of points that have
non-zero probability becomes very large, and x can be very large, or very small. For
example, there is some probability, though there
may be very little indeed, on the
point where h = N , or, equivalently, x = N (p + N pq). For sufficiently large N ,
Section 13.4
286
N
e
N
This yields
P (h)
Np
h
h
Nq
N h
(N h) s
N
2h(N h)
h = N p + x
and
log
Np
(N p + x)
Nq
(N q x)
x
= log 1 +
Np
x
1 x 2
+ ( )(
)
Np
2 Np
x
1 x 2
+ ( )(
) .
Nq
2 Nq
Section 13.4
287
"
2 #
x
x
1
[N p + x]
+
+
Np
2
Np
"
2 #
x
x
1
[N q x]
+
Nq
2
Nq
1
x2 + O((x)3 )
=
2
(recall = N pq if youre having trouble with the last step). Now we look at the
square-root term. We have
s
1
N
= (log [N p + x] + log [N q x] log N + log 2)
log
2(N p + x)(N q x)
2
x
log N p + O
N p
1
x
=
Nq
2 + log N q O
log N + log 2
x
but, since N is very large compared to x, we can ignore the O( N
p ) terms. Then
this term is not a function of x. So we have
log P (x)
x2
+ constant.
2
Section 13.4
288
13.4.3 So What?
I have proven an extremely useful fact, which I shall now put in a box.
exp
2
2
in the sense that
P ({x [a, b]})
b
a
exp
u2
2
du
N pq
so that 68% of the time, h must take a value in the range [N p N pq, N p+ N pq].
Equivalently, the relative frequency h/N must take a value in the range
pq
pq
[p , p + ]
N
N
but as N this range gets smaller and smaller, and h/N limits to p. So our
view of probability as a frequency is consistent.
To obtain h, we added N independent Bernoulli random variables. So you can
interpret the box as saying that the sum of many independent Bernoulli random
variables has a probability distribution that limits to the normal distribution as
the number added together gets larger. Remember that I have stated, though not
precisely, but not proved the deep and useful fact that the sum of pretty much
any independent random variables has a distribution that gets closer to a normal
distribution as the number added together gets larger.
Section 13.5
You should
289
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
261
262
262
263
263
264
264
266
267
267
267
268
268
268
269
270
271
271
271
271
272
272
272
273
273
273
273
273
274
274
275
280
C H A P T E R
14
Section 14.1
291
case (section 14.5). However, there is some random error in the estimate, and we
can tell (on average) how large the error caused by random sampling could be.
14.1 ESTIMATING MODEL PARAMETERS WITH MAXIMUM LIKELIHOOD
Assume we have a dataset D = {x}, and a probability model we believe applies to
that dataset. Generally, application logic suggests the type of model (i.e. normal
probability density; Poisson probability; geometric probability; and so on). But
usually, we do not know the parameters of the model for example, the mean and
standard deviation of a normal distribution; the intensity of a poisson distribution;
and so on. Notice that this situation is unlike what we have seen to date. In
chapter 13, we assumed that we knew , and could then use the model to assign
a probability to a set of data items D. Here we know the value of D, but dont
know . Our model will be better or worse depending on how well we choose the
parameters. We need a strategy to estimate the parameters of a model from a
sample dataset. Notice how each of the following examples fits this pattern.
Section 14.1
292
Example: 14.4
data
Imagine we know for some reason that our data is well described by
a normal distribution. We could ask what is the mean and standard
deviation of the normal distribution that best represents the data?
Write for the parameters of our model. If we knew , then the probability
of observing the data D would be P (D|). We know D, and we dont know , so
the value of P (D|) is a function of .
Section 14.1
293
In words, this means: Choose the parameter such that the probability of
observing the data you actually see, is maximised. This should strike you as being
a reasonable choice. You should also be aware that this is not the only possible
choice (well see another one in section 14.2).
For the examples we work with, the data will be independent and identically distributed or IID. This means that each data item is an idependently
obtained sample from the same probability distribution (see section ??). In turn,
this means that the likelihood is a product of terms, one for each data item, which
we can write as
Y
L() = P (D|) =
P (di |).
idataset
It is traditional to write for any set of parameters that are unknown. There
are two, distinct, important concepts we must work with. One is the unknown
parameter(s), which we will write . The other is the estimate of the value of that
This estimate is the best we can do it may
parameter, which we will write .
not be the true value of the parameter.
Section 14.1
294
In N independent coin flips, you observe k heads. Use the maximum likelihood
principle to infer p(H).
Solution: The coin has = p(H), which is the unknown parameter. We know
that an appropriate probability model is the binomial model P (k; N, ). We
have that
N
L() = P (D|) = Pb (k; N, ) =
k (1 )(N k)
k
which is a function of the unknown probability that a coin comes up
heads; k and N are known. We must find the value of that maximizes this
expression. Now the maximum occurs when
L()
= 0.
We have
L()
=
N
k
kk1 (1 )(N k) k (N k)(1 )(nk1)
Section 14.1
295
You flip a coin N times, stopping when you see a head. Use the maximum
likelihood principle to infer p(H) for the coin.
Solution: The coin has = p(H), which is the unknown parameter. We know
that an appropriate probability model is the geometric model Pg (N ; ). We
have that
L() = P (D|) = Pg (N ; ) = (1 )(N 1)
which is a function of the unknown probability that a coin comes up heads;
N is known. We must find the value of that maximizes this expression. Now
the maximum occurs when
L()
= 0 = ((1 )(N 1) (N 1)(1 )(N 2) )
Section 14.1
296
You throw a die N times, and see n1 ones, . . . and n6 sixes. Write p1 , . . . , p6
for the probabilities that the die comes up one, . . ., six. Use the maximum
likelihood principle to estimate p1 , . . . , p6 .
Solution: The data are n, n1 , . . . , n6 . The parameters are = (p1 , . . . , p6 ).
P (D|) comes from the multinomial distribution. In particular,
L() = P (D|) =
n!
pn1 pn2 . . . pn6 6
n1 ! . . . n6 ! 1 2
ni pi
pi
ni
=
.
1 p1 p2 p3 p4 p5
n6
1
(n1 , n2 , n3 , n4 , n5 , n6 )
(n1 + n2 + n3 + n4 + n5 + n6 )
Section 14.1
297
Poisson distributions
You observe N intervals, each of the same, fixed length (in time, or space).
You know that, in these intervals, events occur with a Poisson distribution (for
example, you might be observing Prussian officers being kicked by horses, or
telemarketer calls...). You know also that the intensity of the Poisson distribution is the same for each observation. The number of events you observe in the
ith interval is ni . What is the intensity, ?
Solution: The likelihood is
L() =
iintervals
P ({ni events} |) =
iintervals
i
which yields a maximum likelihood estimate of
P
ni
= i
N
ni e
.
ni !
Section 14.1
298
A famously sweary politician gives a talk. You listen to the talk, and for each of
30 intervals 1 minute long, you record the number of swearwords. You record
this as a histogram (i.e. you count the number of intervals with zero swear
words, with one, etc.). For the first 10 intervals, you see
no. of swear words no. of intervals
0
5
1
2
2
2
3
1
4
0
and for the following 20 intervals, you see
no. of swear words no. of intervals
0
9
1
5
2
3
3
2
4
1
Assume that the politicians use of swearwords is Poisson. What is the intensity
using the first 10 intervals? the second 20 intervals? all the intervals? why are
they different?
Solution: Use the expression from worked example 14.4 to find
10
=
=
20
=
=
30
=
=
Section 14.1
299
Normal distributions
=
=
=
P (x1 , . . . xN |, )
P (x1 |, )P (x2 |, ) . . . P (xN |, )
N
Y
(xi )2
1
exp
2 2
2
i=1
and this expression is a moderate nuisance to work with. The log of the likelihood is
!
N
X
(xi )2
+
term not depending on .
log L() =
2 2
i=1
We can find the maximum by differentiating wrt and setting to zero, which
yields
log L()
N
X
2(xi )
i=1
=
=
2 2
1
2
N
X
i=1
xi N
= i=1
N
which probably isnt all that surprising. Notice we did not have to pay attention
to in this derivation we did not assume it was known, it just doesnt do
anything.
Section 14.1
300
Assume we have x1 , . . . , xN which are data that can be modelled with a normal
distribution. Use the maximum likelihood principle to estimate the standard
deviation of that normal distribution.
Solution: Now we have to write out the log of the likelihood in more detail.
Write for the mean of the normal distribution and for the unknown standard
deviation of the normal distribution. We get
!
N
X
(xi )2
N log + Term not depending on
log L() =
22
i=1
We can find the maximum by differentiating wrt and setting to zero, which
yields
N
log L()
2 X (xi )2
N
= 3
=0
i=1
2
You should notice that one could maximize the likelihood of a normal distribution with respect to mean and standard deviation in one go (i.e. I could have
done worked examples 14.6 and 14.7 in one worked example, instead of two). I did
this example in two parts because I felt it was more accessible that way; if you
object, youre likely to be able to fill in the details yourself very easily.
The maximum likelihood principle has a variety of neat properties we cannot
expound. One worth knowing about is consistency; for our purposes, this means
that the maximum likelihood estimate of parameters can be made arbitrarily close
to the right answer by having a sufficiently large dataset. Now assume that our data
doesnt actually come from the underlying model. This is the usual case, because
we cant usually be sure that, say, the data truly is normal or truly comes from a
Poisson distribution. Instead we choose a model that we think will be useful. When
the data doesnt come from the model, maximum likelihood produces an estimate
of that corresponds to the model that is (in quite a strong sense, which we cant
explore here) the closest to the source of the data. Maximum likelihood is very
widely used because of these neat properties. But there are some difficulties.
14.1.2 Cautions about Maximum Likelihood
One important problem is that it might be hard to find the maximum of the likelihood exactly. There are strong numerical methods for maximizing functions, and
Section 14.2
301
these are very helpful, but even today there are likelihood functions where it is very
hard to find the maximum.
The second is that small amounts of data can present nasty problems. For
example, in the binomial case, if we have only one flip we will estimate p as either
1 or 0. We should find this report unconvincing. In the geometric case, with a
fair coin, there is a probability 0.5 that we will perform the estimate and then
report that the coin has p = 1. This should also worry you. As another example,
if we throw a die only a few times, we could reasonably expect that, for some
i, ni = 0. This doesnt necessarily mean that pi = 0, though thats what the
maximum likelihood inference procedure will tell us.
This creates a very important technical problem how can I estimate the
probability of events that havent occurred? This might seem like a slightly silly
question to you, but it isnt. Solving this problem has really significant practical
consequences. As one example, consider a biologist trying to count the number of
butterfly species on an island. The biologist catches and classifies a lot of butterflies,
then leaves. But are there more butterfly species on the island? To get some sense
that we can reason successfully about this problem, compare two cases. In the
first, the biologist catches many individuals of each of the species observed. In this
case, you should suspect that catching more butterflies is unlikely to yield more
species. In the second case, there are many species where the biologist sees only
one individual of that species. In this case, you should suspect that catching more
butterflies might very well yield new species.
As another example, a really important part of natural language processing
involves estimating the probability of groups of three words. These groups are
usually known as trigrams. People typically know an awful lot of words (tens
to hundreds of thousands, depending on what you mean by a word). This means
that there are a tremendous number of trigrams, and you can expect that any real
dataset lacks almost all of them, because the dataset isnt big enough for there to
be even just one of each trigram. Some are missing because they dont occur in
real life, but others are not there simply because they are unusual (eg Atom Heart
Mother actually occurs in real life, but you may not have seen it try a web
search if the phrase doesnt ring a bell). Modern speech recognition systems need
to know how probable every trigram is. If the speech system thinks a trigram has
zero probability and the trigram actually occurs, the system will make a mistake.
We cant solve this problem just by giving each trigram a very small non-zero
probability, because there are too many trigrams it is important to distinguish
between rare ones, and ones that dont ever occur. But what probability should
I use for a rare trigram? Maximum likelihood would say use zero, but this would
generate problems. Formalizing all this gets difficult quite quickly,
14.2 INCORPORATING PRIORS WITH BAYESIAN INFERENCE
Another important issue with maximum likelihood is that there is no mechanism
to incorporate prior beliefs. For example, imagine you get a new die from a reliable
store, roll it six times and see a one once. You would be happy to believe that
p(6) = 1/6 for this die. Now imagine you borrow a die from a friend with a long
history of making weighted dice. Your friend tells you this die is weighted so that
Section 14.2
302
p(1) = 1/2. You roll the die six times and see a one once; in this case, you might
worry that p(6) isnt 1/6, and you just happened to get a slightly unusual set
of rolls. Youd worry because you have good reason to believe the die isnt fair,
and youd want more evidence to believe p(6) = 1/6. Maximum likelihood cant
distinguish between these two cases.
The difference lies in prior information information we possess before we
look at the data. We would like to take this information into account when we
estimate the model. One way to do so is to place a prior probability distribution
p() on the parameters . Then, rather than working with the likelihood p(D|), we
could apply Bayes rule, and form the posterior p(|D). This posterior represents
the probability that takes various values, given the data D.
Definition: 14.4 Bayesian inference
Extracting information from the posterior p(|D) is usually called
Bayesian inference
P (D|)P ()
P (D)
but (as we shall see) it can be hard to work out P (D). For some problems, we
might not need to know it.
Section 14.2
Posterior value
0
0
303
0.2
0.4
0.6
P(H)
0.8
0
0
0.2
0.4
0.6
P(H)
0.8
FIGURE 14.1: The curves show a function proportional to the posterior on , for
the two cases of example ??. Notice that this information is rather richer than the
single value we would get from maximum likelihood inference.
Flipping a coin
Section 14.2
posterior on p(H)
posterior on p(H)
3
2
1
0.2
0
0
0.4
0.6
0.8
1
p(H)
Posterior on p(H), given 17H and 13T.
10
3
2
1
0
0
0.2
0.4
p(H)
0.6
0.8
0.2
0.4
p(H)
0.6
0.8
5
posterior on p(H)
posterior on p(H)
0
0
304
6
4
2
0
0
0.2
0.4
p(H)
0.6
0.8
FIGURE 14.2: The probability that an unknown coin will come up heads when flipped
is p(H). For these figures, I simulated coin flips from a coin with p = 0.75. I
then plotted the posterior for various data. Notice how, as we see more flips, we
get more confident about p. The vertical axis changes substantially between plots in
this figure.
In some cases, P () and P (D|), when multiplied together, take a familiar
form. This happens when P (D|) and P () each belong to parametric families
where there is a special relationship between the families. When a prior has this
property, it is called a conjugate prior. There are some cases worth knowing, given
in the worked examples.
Section 14.2
305
Flipping a coin - II
(1 )(1) .
h
()()
and we can write
P (|N, h, , ) (+h1) (1 )(+N h1) .
Notice this has the form of a Beta distribution, so it is easy to recover the
constant of proportionality. We have
P (|N, h, , ) =
( + + N )
(+h1) (1 )(+N h1) .
( + h)( + N h)
Section 14.2
306
Example 14.5 gives some data from a sweary politician. Assume we have only
the first 10 intervals of observations, and we wish to estimate the intensity using
a Poisson model. Write for this parameter. Use a Gamma distribution as a
prior, and write out the posterior.
Solution: We have that
p(D|)
0 e
0!
2 e
2!
9 e10
12
and
p(|, ) =
5
2
1 e
1!
3 e
3!
2
1
(1)
e
()
e
( + 9)
Section 14.2
307
with tape measures, or by analysis of likely errors in our braking system. The depth
of the object is the unknown parameter of the model; we write this depth . Now
the model says that is a normal random variable with mean 0 and standard
deviation 0 .
Notice that this model probably isnt exactly right for example, there must
be some probability in the model that the object falls beyond the bottom of the
hole, which it cant do but it captures some important properties of our system.
The device should stop at or close to 0 meters most of the time, and its unlikely
to be too far away.
Now assume we receive a single measurement what do we now know about
the devices depth? The first thing to notice is that there is something to do here.
Ignoring the prior and taking the measurement might not be wise. For example,
imagine that the noise in the wireless system is large, so that the measurement is
often corrupted our original guess about the devices location might be better
than the measurement. Write x1 for the measurement. Notice that the scale of
the measurement may not be the same as the scale of the depth, so the mean of
the measurement is c1 , where c1 is a change of scale (for example, from inches to
meters). We have that p(x1 |) is normal with mean c1 and standard deviation
n1 . We would like to know p(|x1 ).
We have that
log p(, x1 ) = log p(x1 |) + log p()
( 0 )2
(x1 c1 )2
=
2
2n1
202
+ terms not depending on or x.
We have two estimates of the position, , and we wish to come up with a representation of what we know about . One is x1 , which is a measurement we know
its value. The expected value of x1 is c1 , so we could infer from x1 . But we
have another estimate of the position, which is 0 . The posterior, p(|x1 ), is a
probability distribution on the variable ; it depends on the known values x1 , 0 ,
0 and n1 . We need to determine its form. We can do so by some rearrangement
of the expression for log p(, x1 ).
Notice first that this expression is of degree 2 in (i.e. it has terms 2 ,
and things that dont depend on ). This means that p(|x1 ) must be a normal
distribution, because we can rearrange its log into the form of the log of a normal
distribution. This yields a fact of crucial importance.
Section 14.2
308
Write 1 for the mean of this distribution, and 1 for its standard deviation.
The log of the distribution must be
( 1 )2
+ terms not depending on .
212
The terms not depending on are not interesting, because if we know 1 those
terms must add up to
1
log
21
so that the probability density function sums to one. Our goal is to rearrange terms
into the form above. Notice that
1
( 1 )2
1
2
+ 2 2 + term not depending on
2p2
212
2p
We have
log p(|x1 ) =
=
(c1 x1 )2
( 0 )2
n1
0
x1
1
2 2 2 + 2 c1 2 + 2
2n1
20
0
2 2 n1
+c2 2
n1
2
n1
02
+ c21 02
2
n1
and
1
2
x1
0
n1
02
= 2 c1 2 + 2
2 + c2 2
2n1
20 n1
1 0
2
2
2
c1 x1 0 + 0 n1
n1
02
=
2 2
2 + c2 2
n1
n1
0
1 0
2
2
c1 x1 0 + 0 n1
=
.
2 + c2 2
n1
1 0
These equations make sense. Imagine that 0 is very small, and n1 is very
big; then our new expected value of which is 1 is about 0 . Equivalently,
because our prior was very accurate, and the measurement was unreliable, our
expected value is about the prior value. Similarly, if the measurement is reliable
(i.e. n1 is small) and the prior has high variance (i.e. 0 is large), then our
expected value of is about x1 /c1 i.e. the measurement, rescaled. I have put
these equations, in a more general form, in a box below.
Section 14.2
309
2 2
m
.
2 + c2 2
m
P (D)
This is the MAP estimate. If we wish to perform MAP inference, P (D) doesnt
matter (it changes the value, but not the location, of the maximum). This means
we can work with P (, D), often called the joint distribution.
Section 14.2
310
Flipping a coin - II
( + + N )
(+h1) (1 )(+N h1) .
( + h)( + N h)
You can get the MAP estimate by differentiating and setting to 0, yielding
=
1+h
.
+2+N
This has rather a nice interpretation. You can see and as extra counts of
heads (resp. tails) that are added to the observed counts. So, for example, if
you were fairly sure that the coin should be fair, you might make and large
and equal. When = 1 and = 1, we have a uniform prior as in the previous
examples.
PN
i=1 .
p(|D) =
and the MAP estimate is
We have that
e
( + T )
( 1 + T )
=
( + N )
(which you can get by differentiating with respect to , then setting to zero).
Notice that if is close to zero, you can interpret as extra counts; if is large,
even if the counts are large.
then it strongly discourages large values of ,
14.2.4 Filtering
We can make online estimates of the maximum likelihood value of mean and standard deviation for a normal distribution. Assume, rather than seeing N elements
Section 14.2
311
of a dataset in one go, you get to see each one once, and you cannot store them.
Assume that this dataset is modelled as normal data. Write
k for the maximum
likelihood estimate of the mean based on data items 1 . . . k (and
k for the maximum
likelihood estimate of the standard deviation, etc.). Notice that
k+1 =
and that
k+1 =
(k
k ) + xk+1
(k + 1)
(k
k2 ) + (xk+1
k+1 )2
(k + 1)
This means that you can incorporate new data into your estimate as it arrives
without keeping all the data. This process of updating a representation of a dataset
as new data arrives is known as filtering.
This is particularly useful in the case of normal posteriors. Recall that if we
have a normal prior and a normal likelihood, the posterior is normal. Now consider
a stream of incoming measurements. Our initial representation of the parameters
we are trying to estimate is the prior, which is normal. We allow ourselves to see
one measurement, which has normal likelihood; then the posterior is normal. You
can think of this posterior as a prior for the parameter estimate based on the next
measurement. But we know what to do with a normal prior, a normal likelihood,
and a measurement; so we can incorporate the measurement and go again. This
means we can exploit our expression for the posterior mean and standard deviation
in the case of normal likelihood and normal prior and a single measurement to deal
with multiple measurements very easily.
Assume a second measurement, x2 arrives. We know that p(x2 |, c2 , n2 ) is
normal with mean c2 and standard deviation n2 . In the example, we have a new
measurement of depth perhaps in a new, known, scale with new noise (which
might have larger, or smaller, standard deviation than the old noise) added. Then
we can use p(|x1 , c1 , n1 ) as a prior to get a posterior p(|x1 , x2 , c1 , c2 , n1 , n2 ).
Each is normal, by useful fact 14.1. Not only that, but we can easily obtain the
expressions for the mean 2 and the standard deviation 2 recursively as functions
of 1 and 1 .
Applying useful fact 14.2, we have
2 =
2
c2 x2 12 + 1 n2
2
2
2
n2 + c2 1
and
22 =
2
n2
12
.
+ c22 12
2
n2
But what works for 2 and 1 will work for k + 1 and k. We know the posterior after
k measurements will be normal, with mean k and standard deviation k . The
k + 1th measurement xk+1 arrives, and we have p(xk+1 |, ck+1 , n(k+1) ) is normal.
Then the posterior is normal, and we can write the mean k+1 and the standard
deviation k+1 recursively as functions of k and k . The result is worth putting
in a box.
Section 14.2
312
2
ck+1 xk+1 k2 + k n(k+1)
and
2
k+1
=
2
+ c2k+1 k2
n(k+1)
2
n(k+1)
k2
2
+ c2k+1 k2
n(k+1)
Again, notice the very useful fact that, if everything is normal, we can update
our posterior representation when new data arrives using a very simple recursive
form.
Section 14.3
313
Normal data
2
2
xN N
1 + N 1
2
2
+ N 1
and
2
N
=
2
2 N
1
.
2
2 + N
1
Section 14.3
314
on the dataset relate to the measurements we could have made, if we had all the
data. This situation occurs very often. For example, imagine we wish to know the
average weight of a rat. This isnt random; you could weigh every rat on the planet,
and then average the answers. But doing so would absurd (among other things,
youd have to weigh them all at the same time, which would be tricky). Instead,
we weigh a small set of rats, chosen rather carefully. If we have chosen sufficiently
carefully, then the answer from the small set is quite a good representation of the
answer from the whole set.
The data we could have observed, if we could have seen everything, is the
population. The data we actually have is the sample. We would like to know
the mean of the population, but can see only the sample; surprisingly, we can say
a great deal from the sample alone, assuming that it is chosen appropriately.
14.3.1 Estimating the Population Mean from a Sample
Assume we have a population {x}, for i = 1, . . . , Np . Notice the subscript here
this is the number of items in the population. The population could be unreasonably
big: for example, it could consist of all the people in the world. We want to know
the mean of this dataset, but we do not get to see the whole dataset. Instead, we
see the sample.
How the sample is obtained is key to describing the population. We will focus
on only one model (there are lots of others). In our model, the sample is obtained by
choosing a fixed number of data items. Write k for the number of data items in the
sample. We expect k is a lot smaller than Np . Each item is chosen independently,
and fairly. This means that each time we choose, we choose one from the entire
set of Np data items, and each has the same probability of being chosen. This is
sometimes referred to as sampling with replacement.
One natural way to think about sampling with replacement is to imagine the
data items as being written on tickets, which are placed in an urn (old-fashioned
word for a jar, now used mainly by statisticians and morticians). You obtain the
sample by repeating the following experiment k times: shake the urn; take a ticket
from the urn and write down the data on the ticket; put it back in the urn. Notice
that, in this case, each sample is drawn from the same urn. This is important, and
makes the analysis easier. If we had not put the ticket back, the urn would change
between samples.
We summarize the whole dataset with its mean, which we write popmean ({x}).
This is known as the population mean. The notation is just to drive home the
facts that its the mean of the whole population, and that we dont, and cant,
know it. The whole point of this exercise is to estimate this mean.
We would like to estimate the mean of the whole dataset from the items that
we actually see. Imagine we draw k tickets from the urn as above, and average the
values. The result is a random variable, because different draws of k tickets will
give us different values. Write X (k) for this random variable, which is referred to
as the sample mean. Because expectations are linear, we must have that
i
i
h
i
h
i 1 h
h
E X (1) + . . . + E X (1) = E X (1)
E X (k) =
k
(where X (1) is the random variable whose value is obtained by drawing one ticket
Section 14.3
xi p(i)
xi
315
i1,...Np
i1,...Np
i1,...Np
1
Np
xi
Np
popmean ({x})
which is the mean value of the items in the urn. This means that
i
h
E X (k) = popmean ({xi }).
Under our sampling model, the expected value of the sample mean is the population
mean.
We will not get the same value of X (k) each time we perform the experiment,
because we see different data items in each sample. So X (k) has variance, and this
variance is important. If it is large, then each estimate is quite different. If it is
small, then the estimates cluster. Knowing the variance of X (k) would tell us how
accurate our estimate of the population mean is.
14.3.2 The Variance of the Sample Mean
We write popsd ({x}) for the standard deviation of the whole population of {x}.
Again, we write it like this to keep track of the facts that (a) its for the whole
population and (b) we dont and usually cant know it.
We can compute the variance of X (k) (the sample mean) easily. We have
i2
i
i
h
h
i
h
h
2
var X (k) = E (X (k) )2 E X (k) = E (X (k) )2 (popmean ({x}))
so we need to know E (X (k) )2 . We can compute this by writing
X (k) =
1
(X1 + X2 + . . . Xk )
k
Section 14.3
316
where X1 is the value of the first ticket drawn from the urn, etc. We then have
2
X (k) =
2 2
1
X 1 + X 2 2 + . . . X 2 k + X1 X2 + . . .
.
X1 Xk + X2 X1 + . . . X2 Xk + . . . Xk1 Xk
k
Urn variances
Show that
i PNp x2
h
2
2
E (X (1) )2 = i=1 i = popsd ({x}) + popmean ({x})
Np
Solution: First, we have (X (1) )2 is the number obtained by taking a ticket
out of the urn and squaring its data item. Now
so
i2
i
i
h
h
h
2
2
popsd ({x}) = E (X (1) )2 E X (1) = E (X (1) )2 popmean ({x})
i
h
2
2
E (X (1) )2 = popsd ({x}) + popmean ({x})
Section 14.3
317
Urn variances
Show that
Solution: This looks hard, but isnt. Recall from the facts in chapter ??
(useful facts ??, page ??) that if X and Y are independent random variables,
E[XY ] = E[X]E[Y ]. But X1 and X2 are independent they are different
random draws from the same urn. So
E[X1 X2 ] = E[X1 ]E[X2 ]
but E[X1 ] = E[X2 ] (they are draws from the same urn) and E[X] =
popmean ({x}). So
E[X1 X2 ] = popmean ({x})2 .
Now
i
h
E (X (k) )2
=
=
=
=
kE (X (1) )2 + k(k 1)E[X1 X2 ]
k2
(1) 2
E (X ) + (k 1)E[X1 X2 ]
k
2
2
2
(popsd ({x}) + popmean ({x}) ) + (k 1)popmean ({x})
k
popsd ({x})2
+ popmean ({x})2
k
so we have
i
h
var X (k)
i2
i
h
h
= E (X (k) )2 E X (k)
2
=
=
popsd ({x})
2
2
+ popmean ({x}) popmean ({x})
k
2
popsd ({x})
.
k
This is a very useful result which is well worth remembering together with our facts
on the sample mean, so well put them in a box together.
Section 14.3
318
popsd ({x})
k
popsd ({x})
k
which means that (a) the more samples you draw, the better your estimate becomes
and (b) the estimate improves rather slowly for example, to halve the standard
deviation in your estimate, you need to draw four times as many samples. The
standard deviation of the estimate of the mean is often known as the standard
error of the mean. This allows us to draw a helpful distinction: the population
has a standard deviation, and our estimate of its mean (or other things but we
wont go into this) has a standard error.
Notice we cannot state the standard error of our estimate exactly, because we
do not know popsd ({x}). But we could make a good estimate of popsd ({x}), by
computing the standard deviation of the examples that we have. It is now
P helpful
to have some notation for the particular sample we have. I will write isample
for a sum over the sample items, and we will use
P
isample xi
mean ({x}) =
k
for the mean of the sample that is, the mean of the data we actually see; this is
consistent with our old notation, but theres a little reindexing to keep track of the
fact we dont see all of the population. Similarly, I will write
sP
2
isample (xi mean ({xi }))
std ({x}) =
k
for the sample standard deviation. Again, this is the standard deviation of the data
we actually see; and again, this is consistent with our old notation, again with a
Section 14.3
319
little reindexing to keep track of the fact we dont see all of the population. We
could estimate
popsd ({x}) std ({x})
and as long as we have enough examples, this estimate is good. If the number of
samples k is small, it is better to use
sP
2
isample (xi mean ({x}))
.
popsd ({x})
k1
In fact, much more is known about the distribution of X (k) .
14.3.3 The Probability Distribution of the Sample Mean
The sample mean is a random variable. We know an expression for its mean, and
we can estimate its variance. In fact, we can determine its probability distribution,
though I wont do this rigorously. In section 13.4.3, I mentioned that adding a
number of independent random variables almost always got you a normal random
variable, a fact sometimes known as the central limit theorem. I didnt prove it,
and Im not going to now. But when we form X (k) , were adding random variables.
This means that X (k) is a normal random variable, for sufficiently big k (for some
reason, k > 30 is usually seen as right).
This is important, because it has the following consequence. Draw a large
number of different samples of k elements from the population. Each is a dataset of
k items. Compute mean ({x}) for each, and regard the resulting numbers e1 , . . . , er
as data items. Convert the ei to standard coordinates si , where
si =
(i.e. by subtracting the mean of the ei , and dividing by their standard deviation).
Now construct a construct a histogram of the s. If r is sufficiently large, the
histogram will be close to the standard normal curve.
14.3.4 When The Urn Model Works
In our model, there was a population of Np data items xi , and we saw k of them,
chosen at random. In particular, each choice was fair (in the sense that each data
item had the same probability of being chosen) and independent. These assumptions are very important for our analysis to apply. If our data does not have these
properties, bad things can happen.
For example, assume we wish to estimate the percentage of the population
that has beards. This is a mean (the data items take the value 1 for a person with
a beard, and 0 without a beard). If we select people according to our model, then
ask them whether they have a beard, then our estimate of the percentage of beards
should behave as above.
The first thing that should strike you is that it isnt at all easy to select people
according to this model. For example, we might select phone numbers at random,
then call and ask the first person to answer the phone whether they have a beard;
Section 14.3
320
but many children wont answer the phone because they are too small. The next
important problem is that errors in selecting people can lead to massive errors in
your estimate. For example, imagine you decide to survey all of the people at a
kindergarten on a particular day; or all of the people in a womens clothing store;
or everyone attending a beard growing competition (they do exist). In each case,
you will get an answer that is a very poor estimate of the right answer, and the
standard error might look very small. Of course, it is easy to tell that these cases
are a bad choice.
It may not be easy to tell what a good choice is. You should notice the similarity between estimating the percentage of the population that wears a beard,
and estimating the percentage that will vote for a particular candidate. There is
a famous example of a survey that mispredicted the result of the Dewey-Truman
presidential election in 1948; poll-takers phoned random phone numbers, and asked
for an opinion. But at that time, telephones tended to be owned by a small percentage of rather comfortable households, who tended to prefer one candidate, and
so the polls mispredicted the result rather badly.
Sometimes, we dont really have a choice of samples. For example, we might
be presented with a small dataset of (say) human body temperatures. If we can be
satisfied that the people were selected rather randomly, we might be able to use this
dataset to predict expected body temperature. But if we knew that the subjects
had their temperatures measured because they presented themselves at the doctor
with a suspected fever, then we most likely cannot use it to predict expected body
temperature.
One important and valuable case where this model works is in simulation. If
you can guarantee that your simulations are independent (which isnt always easy),
this model applies to estimates obtained from a simulation. Notice that it is usually
straightforward to build a simulation so that the ith simulation reports an xi where
popmean ({x}) gives you the thing you want to measure. For example, imagine you
wish to measure the probability of winning a game; then the simulation should
report one when the game is won, and zero when it is lost. As another example,
imagine you wish to measure the expected number of turns before a game is won;
then your simulation should report the number of turns elapsed before the game
was won.
Section 14.4
You should
321
Inference . . . . . . . . . . . . . . . . . . . . .
point estimate . . . . . . . . . . . . . . . . . .
interval estimates . . . . . . . . . . . . . . . .
hypothesis testing . . . . . . . . . . . . . . . .
likelihood . . . . . . . . . . . . . . . . . . . . .
Likelihood . . . . . . . . . . . . . . . . . . . .
maximum likelihood principle . . . . . . . . .
The maximum likelihood principle . . . . . . .
independent and identically distributed . . . .
IID . . . . . . . . . . . . . . . . . . . . . . . .
The log-likelihood of a dataset under a model
consistency . . . . . . . . . . . . . . . . . . . .
prior probability distribution . . . . . . . . . .
posterior . . . . . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
282
282
282
282
284
284
284
285
285
285
289
292
294
294
Section 14.4
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
You should
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
322
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
294
294
294
294
294
296
299
301
301
303
304
306
306
306
306
307
310
310