SMLDM HT 2014 - Part C Problem Sheet 3 - Oxford Statisticsteh/teaching/smldmHT2014/PartC-3S.pdf ·...

7
SMLDM HT 2014 - Part C Problem Sheet 3 1. Consider two univariate normal distributions N (μ, σ 2 ) with known parameters μ A = 10 and σ A =5 for class A and μ B = 20 and σ B =5 for class B. Suppose class A represents the random score X of a medical test of normal patients and class B represents the score of patients with a certain disease. A priori there are 100 times more healthy patients than patients carrying the disease. (a) Find the optimal decision rule in terms of misclassification error (0-1 loss) for allocating a new observation x to either class A or B. Answer: The optimal decision for X = x is to allocate to class argmax k∈{A,B} π k f k (x). The patients should be classified as healthy iff π A 1 2πσ A exp(- (x - μ A ) 2 2σ 2 A ) π B 1 2πσ B exp(- (x - μ B ) 2 2σ 2 B ), that is, using σ A = σ B , iff -2σ 2 A log(π A B )+(x - μ A ) 2 (x - μ B ) 2 . The decision boundary is attained for equality, that is if x fulfills 2x(μ B - μ A )+ μ 2 A - μ 2 B - 2σ 2 A log(π A B )=0. For the given values, this implies that the decision boundary is at x = (50 log 100 - 100 + 400)/(2 · 10) 26.51, that is all patients with a test score above 26.51 are classfied as having the disease. (b) Repeat (a) if the cost of a false negative (allocating a sick patient to group A) is θ> 1 times that of a false positive (allocating a healthy person to group B). Describe how the rule changes as θ increases. For which value of θ are 84.1% of all patients with disease correctly classified? Answer: The optimal decision minimizes E(L(Y, ˆ Y (x))|X = x). It is hence optimal to choose class A (healthy) over class B if and only if P (Y = A|X = x) θP (Y = B|X = x). Using the same argument as above, the patients should be classified as healthy now iff (ig- noring again the common denominator k∈{A,B} π k f k (x)), π A 1 2πσ A exp(- (x - μ A ) 2 2σ 2 A ) θπ B 1 2πσ B exp(- (x - μ B ) 2 2σ 2 B ). The decision boundary is now attained if x fulfills 2x(μ B - μ A )+ μ 2 A - μ 2 B - 2σ 2 A log(π A /(θπ B )) = 0. 1

Transcript of SMLDM HT 2014 - Part C Problem Sheet 3 - Oxford Statisticsteh/teaching/smldmHT2014/PartC-3S.pdf ·...

Page 1: SMLDM HT 2014 - Part C Problem Sheet 3 - Oxford Statisticsteh/teaching/smldmHT2014/PartC-3S.pdf · SMLDM HT 2014 - Part C Problem Sheet 3 ... score Xof a medical test of normal patients

SMLDM HT 2014 - Part C Problem Sheet 31. Consider two univariate normal distributions N (µ, σ2) with known parameters µA = 10 andσA = 5 for class A and µB = 20 and σB = 5 for class B. Suppose class A represents the randomscore X of a medical test of normal patients and class B represents the score of patients witha certain disease. A priori there are 100 times more healthy patients than patients carrying thedisease.

(a) Find the optimal decision rule in terms of misclassification error (0-1 loss) for allocating anew observation x to either class A or B.

Answer: The optimal decision for X = x is to allocate to class

argmaxk∈{A,B}πkfk(x).

The patients should be classified as healthy iff

πA1√

2πσAexp(−(x− µA)2

2σ2A) ≥ πB

1√2πσB

exp(−(x− µB)2

2σ2B),

that is, using σA = σB , iff

−2σ2A log(πA/πB) + (x− µA)2 ≤ (x− µB)2.

The decision boundary is attained for equality, that is if x fulfills

2x(µB − µA) + µ2A − µ2B − 2σ2A log(πA/πB) = 0.

For the given values, this implies that the decision boundary is at

x = (50 log 100− 100 + 400)/(2 · 10) ≈ 26.51,

that is all patients with a test score above 26.51 are classfied as having the disease.

(b) Repeat (a) if the cost of a false negative (allocating a sick patient to group A) is θ > 1

times that of a false positive (allocating a healthy person to group B). Describe how the rulechanges as θ increases. For which value of θ are 84.1% of all patients with disease correctlyclassified?

Answer: The optimal decision minimizes E(L(Y, Y (x))|X = x). It is hence optimal tochoose class A (healthy) over class B if and only if

P (Y = A|X = x) ≥ θP (Y = B|X = x).

Using the same argument as above, the patients should be classified as healthy now iff (ig-noring again the common denominator

∑k∈{A,B} πkfk(x)),

πA1√

2πσAexp(−(x− µA)2

2σ2A) ≥ θπB

1√2πσB

exp(−(x− µB)2

2σ2B).

The decision boundary is now attained if x fulfills

2x(µB − µA) + µ2A − µ2B − 2σ2A log(πA/(θπB)) = 0.

1

Page 2: SMLDM HT 2014 - Part C Problem Sheet 3 - Oxford Statisticsteh/teaching/smldmHT2014/PartC-3S.pdf · SMLDM HT 2014 - Part C Problem Sheet 3 ... score Xof a medical test of normal patients

For increasing values of θ, patients with decreasingly smaller test scores are classified ashaving the disease.

84.1% of all patients carrying the disease are correctly classified if the decision boundary is atthe 15.9%-quantile of theN (µB, σ

2B)-distribution, which is at q = 20+5Φ−1(0.159) ≈ 15.

This decision boundary is attained if

15 = q = (50 log(100/θ)− 100 + 400)/20,

which implies that for

θ = 100 exp(−20q − 300

50) = 100,

approximately 84.1% of all patients are correctly classified as carrying the disease.

2. For a given loss function L, the risk R is given by the expected loss

R(Y ) = E(L(Y, Y (X))),

where Y = Y (X) is a function of the random predictor variable X .

(a) Consider a regression problem and the squared error loss

L(Y, Y (X)) = (Y − Y (X))2.

Derive the expression of Y = Y (X) minimizing the associated risk.

Answer: We have

R = E((

Y − Y (X))2)

=

∫E((

Y − Y (X))2∣∣∣∣X = x

)fX (x) dx

so minimizing the risk can be achieved by minimizing for any x

E((

Y − Y (X))2∣∣∣∣X = x

)= E

(Y 2∣∣X = x

)− 2Y (x)E (Y |X = x) + Y (x)2 .

This is clearly minimized for the conditional mean:

Y (X) = E (Y |X) .

(b) What if we use the `1 loss instead?

L(Y, Y (X)) = |Y − Y (X)|.

Answer: As before, we want to find Y (x) to minimize

E(|Y − Y (x)|

∣∣X = x)

2

Page 3: SMLDM HT 2014 - Part C Problem Sheet 3 - Oxford Statisticsteh/teaching/smldmHT2014/PartC-3S.pdf · SMLDM HT 2014 - Part C Problem Sheet 3 ... score Xof a medical test of normal patients

Differentiating the expression with respect to Y (x),

E(

sign(Y − Y (x))∣∣X = x

)= −P(Y < Y (x)|X = x) + P(Y > Y (x)|X = x)

which occurs when P(Y < Y (x)|X = x) = P(tY > Y (x)|X = x) = .5, i.e. at the medianconditional on X = x.

3. Consider applying LDA to a two-class dataset. We will verify some of the claims in the lectures.We use the notation in the lectures.

(a) Show that Σ−1(µ1 − µ2) spans the one-dimensional discriminant subspace. What is thecorresponding generalized eigenvalue?

Answer: We can write x = n1n µ1 + n2

n µ2. Hence

µ1 − x =n2n

(µ1 − µ2)

µ2 − x =n1n

(µ2 − µ1)

Plugging these intoB, we see that it can be written as c(µ1−µ2)(µ1−µ2)> where c = n1n2n2 .

Thus it is easy to verify now that Σ−1(µ1−µ2) solves the generalized eigenvector equation,with eigenvalue n1n2

n2 (µ1 − µ2)>Σ−1(µ1 − µ2).

(b) Explain why to predict the class of a data vector x it is sufficient to look at its projectiononto the subspace spanned by Σ−1(µ1 − µ2).

Answer: The projection onto the subspace spanned by Σ−1(µ1 − µ2) is simply

z = (µ1 − µ2)>Σ−1x/‖Σ−1(µ1 − µ2)‖.

The decision rule of LDA is to predict class 1 if

0 < (a1 − a2) + (µ1 − µ2)>Σ−1x = (a1 − a2) + ‖Σ−1(µ1 − µ2)‖z,

which can be computed from z.

(c) In the case where the within-class covariance is Σ = I , explain the geometry of the decisionrule of LDA with the help of a diagram.

Answer:

(μ1-μ2)

μ1

μ2

x{x : (a1-a2)+(μ1-μ2)'x=0 }

3

Page 4: SMLDM HT 2014 - Part C Problem Sheet 3 - Oxford Statisticsteh/teaching/smldmHT2014/PartC-3S.pdf · SMLDM HT 2014 - Part C Problem Sheet 3 ... score Xof a medical test of normal patients

4. Show that under a Naıve Bayes model, the Bayes classifier Y (x) minimizing the total risk for the0− 1 loss function has a linear discriminant function of the form

Y (x) = arg maxk=1,2

αk + β>k x.

and find the values of αk, βk. (Use notation from lecture slides).

Answer: The Bayes classifier is given in this discrete state space as

Y (x) = arg maxk=1,2

πkP(X = x|Y = k) = arg maxk=1,2

log πk + logP(X = x|Y = k)

Now,

logP(X = x|Y = k) =

p∑j=1

xij log φkj + (1− xij) log(1− φkj)

=

p∑j=1

log(1− φkj) + xij logφkj

1− φkj

So that the discriminant functions are linear, with:

αk = log πk +

p∑j=1

log(1− φkj)

βkj = logφkj

1− φkjfor each k and j.

5. Suppose we have a two-class setup with classes−1 and 1, that isY = {−1, 1} and a 2-dimensionalpredictor variable X . We find that the means of the two groups are at µ−1 = (−1,−1)> andµ1 = (1, 1)> respectively. The a priori probabilities are equal.

(a) Applying LDA, the covariance matrix is estimated to be, for some value of 0 ≤ ρ ≤ 1,

Σ =

(1 ρ

ρ 1

).

Find the decision boundary as a function of ρ.

Answer: The constant a∗ = a1 − a−1 is given by, using equal a priori probabilities,

a∗ = µT1 Σ−1µ1 − µT2 Σ−1µ2.

Hence a∗ = 0. The constant b∗ = b1 − b−1 is on the other hand

b∗ = Σ−1(µ1 − µ−1) = Σ−1(2, 2)> = 2/(1 + ρ)(1, 1)>,

Class 1 is chosen over class -1 for x = (x(1), x(2))> if and only if a∗ + b>∗ x > 0, that is iff

2

1 + ρ(x(1) + x(2)) > 0.

Equivalently, iffx(1) + x(2) > 0,

which could have been guessed as the solution initially.

4

Page 5: SMLDM HT 2014 - Part C Problem Sheet 3 - Oxford Statisticsteh/teaching/smldmHT2014/PartC-3S.pdf · SMLDM HT 2014 - Part C Problem Sheet 3 ... score Xof a medical test of normal patients

(b) Suppose instead that, we model each class with its own covariance matrix. We estimate thecovariance matrices for group -1 as

Σ−1 =

(5 0

0 1/5

),

and for group 1 as

Σ1 =

(1/5 0

0 5

).

Describe the decision rule and draw a sketch of it in the two-dimensional plane.

Answer: As in (a), the classification is class 1 if and only if

(x− µ1)T Σ−11 (x− µ1) < (x− µ−1)T Σ−1−1(x− µ−1).

The difference with LDA in (a) is that Σ1 6= Σ−1.

Let, as in the lectures, ak = µTk Σ−1k µk and similarly for bk (for terms linear in x) and ck (forterms quadratic in x) for k = 1, 2.

The constant a∗ = a1 − a−1 is again 0. The term b1 is now

bT1 x = −2µT1 Σ−11 x = −2(5x(1) + x(2)/5).

andbT−1x = −2µT−1Σ

−12 x = 2(x(1)/5 + 5x(2)).

The quadratic terms are

xT c1x = xT Σ−11 x = 5(x(1))2 + (x(2))2/5

andxT c−1x = xT Σ−1−1x = (x(1))2/5 + 5(x(2))2.

The observations x is thus classified as belonging to group 1 if and only if

5(x(1))2 + (x(2))2/5− 2(5x(1) + x(2)/5) < (x(1))2/5 + 5(x(2))2 + 2(x(1)/5 + 5x(2)).

Bringing all terms to the left side and dividing by 5−1/5, the classification is group 1 if andonly if

(x(1))2 − (x(2))2 − ·13

6(x(1) + x(2)) < 0.

Here, we thus obtain linear decision boundaries, even though we are using QDA (whichtypically produces quadratic decision boundaries). The decision boundaries are shown inthe figure below, along with the group means.

5

Page 6: SMLDM HT 2014 - Part C Problem Sheet 3 - Oxford Statisticsteh/teaching/smldmHT2014/PartC-3S.pdf · SMLDM HT 2014 - Part C Problem Sheet 3 ... score Xof a medical test of normal patients

−2 −1 0 1 2

−2

−1

01

2

x1

x2X

X

6. (Challenging; submit this in Friday Week 5)

Implement the EM algorithm from Problem Sheet 2 Question 4 on a dataset of handwritten digitsof ‘0’, to ‘9’.

You can obtain the dataset fromhttp://www.stats.ox.ac.uk/\%7Eteh/teaching/smldmHT2014/usps.txt

Load the dataset usingX <- as.matrix(read.table("usps.txt"))

The dataset is binary and has 1000 rows and 64 columns. Each row gives a 8 × 8 binary image,and you can visualize the images using a command likeimage(matrix(X[i,],8,8))

where i ∈ {1, . . . , 1000}.

In the E step of the algorithm, you will need to compute the the posterior probabilities q(zi = k)

in a numerically stable manner. Specifically, the unnormalized posterior probabilities are:

πk

p∏j=1

(φkj)xij (1− φkj)1−xij

We compute the logarithms instead:

Sik = log πk +

p∑j=1

xij log φkj + (1− xij) log(1− φkj)

Then the posterior probabilities are computed as:

q(zi = k) =eSik−maxk′ Sik′∑l e

Sil−maxk′ Sik′

6

Page 7: SMLDM HT 2014 - Part C Problem Sheet 3 - Oxford Statisticsteh/teaching/smldmHT2014/PartC-3S.pdf · SMLDM HT 2014 - Part C Problem Sheet 3 ... score Xof a medical test of normal patients

while the marginal log probability is computed as:

log p(xi) = log∑k

eSik =

(maxk′

Sik′

)+ log

∑k

eSik−maxk′ Sik′

This is called the “log-sum-exp” trick.

In the M step, to prevent overfitting, where the estimated probabilities approach 0 or 1, we simplyadd small constants to the formulas:

φkj =α+

∑ni=1 q(zi = k)xij

2α+∑n

i=1 q(zi = k)πk =

β +∑n

i=1 q(zi = k)

Kβ + n

You can try setting α = β = 1. (We will understand this technique, called Laplace smoothing, inthe coming lectures.)

(a) Run the EM algorithm, with 20 mixture components for 50 iterations. Plot the log likelihoodas it increases over iterations of the algorithm. Has the algorithm converged? Display thelearned means of each mixture component.

(b) Do the clusters generally correspond to the classes of handwritten digits?

(c) Do you get the same solution if you run the EM algorithm from different initial startingconfigurations?

(d) If you increase the number of components do the resulting log likelihood objective functiongenerally increase or decrease?

(e) How many components do you think is sensible?

7