---
title: "Problem set 4: classification and evaluation"
heading: "Problem set 4: linear discriminants and Fisher's criterion, the perceptron, logistic regression, Gaussian and naive Bayes classifiers, and evaluating models"
eyebrow: S14 · Problem set 4
type: problems
---

::: hook
These problems cover lessons 15 to 17, 20 and 21: anomaly detection and base rates, distances to decision boundaries, Fisher's direction and separation with correlated features, perceptron updates and mistake bounds, sigmoids, log-odds, softmax losses and cross-entropy gradients, the logistic posterior of Gaussian classes, naive Bayes evidence, and precision, recall, AUC, the error bars on a test score and McNemar's test for comparing two classifiers.
:::

::: pool
pick: 12

::: check
kind: parts
given: a = 2 | 5; b = 1 | 3; c = -8 | -3; x = 1 | 4; y = 2 | 5

## A linear discriminant with w = ({a}, {b}) and $w_0$ = {c}.

::: part
answer: (a*x + b*y + c)/sqrt(a^2 + b^2)
tolerance: 0.5%

### Signed distance of ({x}, {y})?

::: misses
- [any] $(\mathbf{w}^T\mathbf{x} + w_0)/\|\mathbf{w}\|$.
:::
:::

::: part
answer: -c/sqrt(a^2 + b^2)
tolerance: 0.5%

### Distance of the boundary from the origin?

::: misses
- [any] $-w_0/\|\mathbf{w}\|$.
:::
:::
:::

::: check
kind: parts
given: p = 1 | 2; q = 2 | 4; a = 0.5 | 2; b = 3 | 8

## Classes whose means differ by ({p}, {q}), with a diagonal within-class scatter of variances {a} and {b}.

::: part
answer: atan((q/b)/(p/a))*180/pi
unit: °
tolerance: 0.5%

### Fisher's direction, from the first axis?

::: misses
- [any] $S_W^{-1}\Delta\mathbf{m}$.
:::
:::

::: part
answer: p^2/a + q^2/b
tolerance: 0.5%

### Its separation J?

::: misses
- [any] $\sum\Delta m^2/\sigma^2$.
:::
:::

::: part
answer: (p^2 + q^2)^2/(a*p^2 + b*q^2)
tolerance: 0.5%

### J along the line joining the means?

::: misses
- [any] $(\Delta\mathbf{m}\cdot\hat{u})^2/(\hat{u}^TS_W\hat{u})$ with $\hat{u}$ along Δm.
:::
:::
:::

::: check
kind: parts
given: w = 1 | -2; v = 2 | -1; x = 1 | 3; y = -2 | 2; t = 1 | -1; e = 0.25 | 1; R = 3 | 10; g = 0.2 | 1

## A perceptron with w = ({w}, {v}), rate {e}, updating on a misclassified ({x}, {y}) with target {t}; separately, data of radius {R} and margin {g}.

::: part
answer: w + e*t*x
tolerance: 0.5%

### New first component?

::: misses
- [any] $w_1 + \eta tx_1$.
:::
:::

::: part
answer: (R/g)^2
tolerance: 0.5%

### Mistake bound?

::: misses
- [any] $(R/\gamma)^2$.
:::
:::
:::

::: check
kind: parts
given: a = -5 | -1; b = 0.5 | 2.5; x = 1 | 4

## A logistic model with $w_0$ = {a} and $w_1$ = {b}.

::: part
answer: 1/(1 + exp(-(a + b*x)))
tolerance: 0.5%

### Probability at x = {x}?

::: misses
- [any] $\sigma(w_0 + w_1x)$.
:::
:::

::: part
answer: -a/b
tolerance: 0.5%

### Decision boundary?

::: misses
- [any] $-w_0/w_1$.
:::
:::

::: part
answer: exp(b)
tolerance: 0.5%

### Odds ratio per unit of x?

::: misses
- [any] $e^{w_1}$.
:::
:::
:::

::: check
kind: parts
given: a = -3 | 0.5; b = 0.5 | 2; x = 1 | 3; t = 0 | 1

## The same kind of model ($w_0$ = {a}, $w_1$ = {b}) trains on an example at x = {x} with target {t}.

::: part
answer: (1/(1 + exp(-(a + b*x))) - t)*x
tolerance: 0.5%

### Gradient with respect to $w_1$?

::: misses
- [any] $(y - t)x$.
:::
:::

::: part
answer: -(t*ln(1/(1 + exp(-(a + b*x)))) + (1 - t)*ln(1 - 1/(1 + exp(-(a + b*x)))))
tolerance: 0.5%

### Cross-entropy loss for this example?

::: misses
- [any] $-[t\ln y + (1 - t)\ln(1 - y)]$.
:::
:::
:::

::: check
kind: parts
given: p = 0.05 | 0.3; q = 0.6 | 0.95

## Log-odds and probabilities.

::: part
answer: ln(q/(1 - q))
tolerance: 0.5%

### Log-odds of {q}?

::: misses
- [any] $\ln(p/(1 - p))$.
:::
:::

::: part
answer: ln(q/(1 - q)) - ln(p/(1 - p))
tolerance: 0.5%

### Change in activation to move a prediction from {p} to {q}?

::: misses
- [any] Difference of log-odds.
:::
:::
:::

::: check
kind: parts
given: m = 1 | 4; n = -1 | 0; s = 0.5 | 1.5; P = 0.2 | 0.8; x = 0.5 | 2

## Gaussian classes with means {m} and {n}, shared variance {s}², class 1's prior {P}.

::: part
answer: (m - n)/s^2
tolerance: 0.5%

### Weight on x?

::: misses
- [any] $(\mu_1 - \mu_2)/\sigma^2$.
:::
:::

::: part
answer: -(m^2 - n^2)/(2*s^2) + ln(P/(1 - P))
tolerance: 0.5%

### Offset $w_0$?

::: misses
- [any] Constant terms plus the log prior ratio.
:::
:::

::: part
answer: 1/(1 + exp(-((m - n)/s^2*x - (m^2 - n^2)/(2*s^2) + ln(P/(1 - P)))))
tolerance: 0.5%

### Posterior of class 1 at x = {x}?

::: misses
- [any] $\sigma(wx + w_0)$.
:::
:::
:::

::: check
kind: parts
given: p = 0.05 | 0.4; a = 3 | 15; b = 0.2 | 0.6; c = 1.5 | 4

## Naive Bayes with prior {p} and three features whose likelihood ratios are {a}, {b} and {c}.

::: part
answer: ln(p/(1 - p)) + ln(a) + ln(b) + ln(c)
tolerance: 0.5%

### Posterior log-odds?

::: misses
- [any] Prior log-odds plus each log ratio.
:::
:::

::: part
answer: 1/(1 + exp(-(ln(p/(1 - p)) + ln(a) + ln(b) + ln(c))))
tolerance: 0.5%

### Posterior probability?

::: misses
- [any] Sigmoid of the log-odds.
:::
:::
:::

::: check
kind: parts
given: D = 4 | 8 | 30

## Two-class classifiers in {D} dimensions.

::: part
answer: 2*D + D*(D + 1)/2 + 1
tolerance: 0.5%

### Shared-covariance Gaussian parameters?

::: misses
- [any] $2D + D(D + 1)/2 + 1$.
:::
:::

::: part
answer: 2*D + D*(D + 1) + 1
tolerance: 0.5%

### Separate covariances?

::: misses
- [any] $2D + D(D + 1) + 1$.
:::
:::

::: part
answer: D + 1
tolerance: 0.5%

### Logistic regression?

::: misses
- [any] One weight per feature plus a bias.
:::
:::
:::

::: check
kind: parts
given: T = 25 | 90; F = 4 | 15; N = 6 | 30; M = 400 | 900

## A confusion matrix: TP = {T}, FP = {F}, FN = {N}, TN = {M}.

::: part
answer: T/(T + F)
tolerance: 0.5%

### Precision?

::: misses
- [any] TP/(TP + FP).
:::
:::

::: part
answer: T/(T + N)
tolerance: 0.5%

### Recall?

::: misses
- [any] TP/(TP + FN).
:::
:::

::: part
answer: 2*T/(2*T + F + N)
tolerance: 0.5%

### F1?

::: misses
- [any] Harmonic mean.
:::
:::

::: part
answer: (T + M)/(T + F + N + M)
tolerance: 0.5%

### Accuracy?

::: misses
- [any] Correct over total.
:::
:::
:::

::: check
kind: parts
given: s = 0.85 | 0.98; f = 0.9 | 0.995; p = 0.002 | 0.05; d = 0.8 | 2.5

## A test with sensitivity {s} and specificity {f} at prevalence {p}; separately, Gaussian scores {d} standard deviations apart.

::: part
answer: s*p/(s*p + (1 - f)*(1 - p))
tolerance: 0.5%

### Precision?

::: misses
- [any] Bayes' theorem.
:::
:::

::: part
answer: 1 - Q(d/sqrt(2))
tolerance: 0.5%

### AUC?

::: misses
- [any] $\Phi(d/\sqrt{2})$.
:::
:::
:::

::: check
kind: parts
given: p = 0.75 | 0.92; n = 200 | 1500; N = 300 | 2000; k = 4 | 10

## A test accuracy of {p} on {n} examples; separately, {k}-fold cross-validation on {N} examples.

::: part
answer: sqrt(p*(1 - p)/n)
tolerance: 0.5%

### Standard error?

::: misses
- [any] $\sqrt{p(1 - p)/n}$.
:::
:::

::: part
answer: N*(k - 1)/k
tolerance: 0.5%

### Training examples per fit?

::: misses
- [any] $N(k - 1)/k$.
:::
:::
:::

::: check
kind: parts
given: a = 2 | 3; b = 1 | 2; c = 0.8 | -0.8; p = 1 | 2; q = 1 | -1

## Two classes share a within-class covariance $\begin{bmatrix}a & c \\ c & b\end{bmatrix}$ with a = {a}, b = {b}, c = {c}; their means differ by ({p}, {q}).

::: part
answer: (b*p^2 - 2*c*p*q + a*q^2)/(a*b - c^2)
tolerance: 0.5%

### What separation does Fisher's direction achieve?

::: misses
- [p^2/a + q^2/b] Include the correlation.
- [any] $\Delta\mathbf m^TS_W^{-1}\Delta\mathbf m$.
:::
:::

::: part
answer: (p^2 + q^2)^2/(a*p^2 + 2*c*p*q + b*q^2)
tolerance: 0.5%

### And projecting onto the mean difference itself?

::: misses
- [any] $(\Delta\mathbf m^T\Delta\mathbf m)^2/(\Delta\mathbf m^TS_W\Delta\mathbf m)$.
:::
:::
:::

::: check
kind: parts
given: a = 1 | 3; b = 0 | 2; c = -1 | 0.5

## A three-class softmax gives activations {a}, {b} and {c}; the true class is the second.

::: part
answer: exp(b)/(exp(a) + exp(b) + exp(c))
tolerance: 0.5%

### What probability does it give the true class?

::: misses
- [any] $e^{a_2}/\sum e^{a_k}$.
:::
:::

::: part
answer: -ln(exp(b)/(exp(a) + exp(b) + exp(c)))
unit: nats
tolerance: 0.5%

### What is the loss?

::: misses
- [any] $-\ln y_2$.
:::
:::

::: part
answer: exp(a)/(exp(a) + exp(b) + exp(c))
tolerance: 0.5%

### What is the loss's gradient with respect to the first activation?

::: misses
- [exp(a)/(exp(a) + exp(b) + exp(c)) - 1] The first class isn't the target, so its target value is 0.
- [any] $y_1 - t_1 = y_1$.
:::
:::
:::

::: check
kind: parts
given: b = 25 | 40; c = 10 | 18

## Two classifiers are tested on the same examples: the first is wrong and the second right on {b}, and the reverse on {c}.

::: part
answer: (abs(b - c) - 1)^2/(b + c)
tolerance: 0.5%

### What is McNemar's statistic?

::: misses
- [(b - c)^2/(b + c)] Include the continuity correction: subtract 1 from |b − c|.
- [any] $(|b - c| - 1)^2/(b + c)$.
:::
:::
:::

::: check
kind: parts
given: r = 0.9 | 0.99; f = 0.002 | 0.02; p = 0.001 | 0.01; a = 1 | 0.1

## An anomaly detector has recall {r} and false-alarm rate {f} at prevalence {p}; it thresholds a 2D Mahalanobis distance.

::: part
answer: r*p/(r*p + f*(1 - p))*100
tolerance: 0.5%

### What percentage of its alarms are real?

::: misses
- [r*100] That's the recall.
- [any] Not yet. $r\pi/(r\pi + f(1 - \pi))$.
:::
:::

::: part
answer: -2*ln(a/100)
tolerance: 0.5%

### What $d^2$ threshold gives a {a}% false-alarm rate?

::: misses
- [-ln(a/100)] It's $-2\ln\alpha$ in two dimensions.
- [any] Not yet. $-2\ln\alpha$.
:::
:::

::: right
Right. Low prevalence makes precision depend almost entirely on the false-alarm rate.
:::
:::
:::
