---
title: Midterm exam
heading: "Midterm exam: fitting and generalization, likelihood and Bayes, decisions, information, Bayesian inference, regression and classification"
eyebrow: S14 · Midterm exam
type: problems
---

::: hook
Twelve problems from lessons 1 to 20: least squares and the gap between training and test error, generalization bounds, maximum likelihood and Bayes' theorem, decision boundaries and the Bayes error, entropy and cross-entropy, conjugate priors and Gaussian inference, ridge and Bayesian regression, Fisher's discriminant, logistic regression, generative classifiers, nearest neighbors and model evaluation. Give yourself two hours and work without the lessons open. "Another set" deals a different exam.
:::

::: pool
pick: 12

::: check
kind: parts
given: a = 1 | 3; b = 2 | 5; c = 6 | 9; s = 0.02 | 0.1; p = 4 | 12; N = 30 | 150

## Points (0, {a}), (1, {b}), (3, {c}); separately, a correct model with {p} parameters fitted to {N} examples with noise variance {s}.

::: part
answer: (3*(b + 3*c) - 4*(a + b + c))/(3*10 - 16)
tolerance: 0.5%

### Least-squares slope?

::: misses
- [any] $(N\sum xt - \sum x\sum t)/(N\sum x^2 - (\sum x)^2)$.
:::
:::

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

### Expected test error?

::: misses
- [any] $\sigma^2(1 + p/N)$.
:::
:::

::: part
answer: 2*s*p/N
tolerance: 0.5%

### Expected gap between test and training error?

::: misses
- [any] $2\sigma^2p/N$.
:::
:::
:::

::: check
kind: parts
given: v = 2 | 9; N = 4 | 25; S = 10 | 60

## {N} Gaussian samples whose maximum-likelihood variance is {v}; and a log-likelihood with squared deviations summing to {S} at σ² = {v}.

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

### Unbiased variance?

::: misses
- [any] $\sigma^2_{ML}N/(N - 1)$.
:::
:::

::: part
answer: -N/2*ln(2*pi*v) - S/(2*v)
tolerance: 0.5%

### Log-likelihood?

::: misses
- [any] $-\frac{N}{2}\ln(2\pi\sigma^2) - S/(2\sigma^2)$.
:::
:::
:::

::: check
kind: parts
given: s = 0.01 | 0.2; w = 0.3 | 3; c = 1 | 10; m = 50 | 500

## Noise variance {s} with a weight prior of standard deviation {w}; a false alarm costing {c} against a miss costing {m}.

::: part
answer: s/w^2
tolerance: 0.5%

### Equivalent regularization constant?

::: misses
- [any] $\sigma^2/\sigma_w^2$.
:::
:::

::: part
answer: c/(c + m)
tolerance: 0.5%

### Posterior threshold for deciding positive?

::: misses
- [any] $c_{FA}/(c_{FA} + c_{miss})$.
:::
:::
:::

::: check
kind: parts
given: a = 0 | 1; d = 1 | 3; s = 0.5 | 1.5; P = 0.25 | 0.8; let b = a + d

## Gaussian classes with means {a} and {b}, σ = {s}, class 1's prior {P}.

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

### Minimum-error boundary?

::: misses
- [any] Midpoint shifted by $\frac{\sigma^2}{\Delta\mu}\ln(P_1/P_2)$.
:::
:::

::: part
answer: Q(d/(2*s))
tolerance: 0.5%

### Bayes error if the priors were equal?

::: misses
- [any] $Q(\Delta\mu/2\sigma)$.
:::
:::
:::

::: check
kind: parts
given: p = 0.1 | 0.4; q = 0.2 | 0.7; e = 0.02 | 0.15

## A binary event with probability {p}, modeled as {q}; a binary symmetric channel with flip probability {e}.

::: part
answer: -(p*log(p) + (1 - p)*log(1 - p))/log(2)
unit: bits
tolerance: 0.5%

### Entropy of the event?

::: misses
- [any] $-\sum p\log_2 p$.
:::
:::

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

### KL divergence in nats?

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

::: part
answer: 1 + (e*log(e) + (1 - e)*log(1 - e))/log(2)
unit: bits
tolerance: 0.5%

### Channel mutual information with uniform input?

::: misses
- [any] $1 - H_2(\epsilon)$.
:::
:::
:::

::: check
kind: parts
given: a = 1 | 4; b = 2 | 6; m = 4 | 9; N = 12 | 30

## A beta({a}, {b}) prior updated with {m} successes in {N} trials.

::: part
answer: (a + m)/(a + b + N)
tolerance: 0.5%

### Posterior mean?

::: misses
- [any] $(a + m)/(a + b + N)$.
:::
:::

::: part
answer: (a + m - 1)/(a + b + N - 2)
tolerance: 0.5%

### MAP?

::: misses
- [any] $(a + m - 1)/(a + b + N - 2)$.
:::
:::

::: part
answer: (m + 1)/(N + 2)
tolerance: 0.5%

### Laplace's rule, ignoring this prior?

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

::: check
kind: parts
given: u = 0 | 1; s = 0.5 | 2; v = 0.5 | 3; N = 4 | 20; x = 2 | 6

## Prior N({u}, {s}²) on a mean; {N} observations averaging {x} with noise variance {v}.

::: part
answer: 1/(1/s^2 + N/v)
tolerance: 0.5%

### Posterior variance?

::: misses
- [any] Precisions add.
:::
:::

::: part
answer: (u/s^2 + N*x/v)/(1/s^2 + N/v)
tolerance: 0.5%

### Posterior mean?

::: misses
- [any] Precision-weighted average.
:::
:::
:::

::: check
kind: parts
given: a = 1 | 2; b = 2 | 5; r = 0.3 | 0.8; x = 1 | 3; y = 1 | 4

## Zero-mean jointly Gaussian x, y with $\sigma_x$ = {a}, $\sigma_y$ = {b}, ρ = {r}.

::: part
answer: r*b/a*x
tolerance: 0.5%

### E[y | x = {x}]?

::: misses
- [any] $\rho(\sigma_y/\sigma_x)x$.
:::
:::

::: part
answer: b^2*(1 - r^2)
tolerance: 0.5%

### var(y | x)?

::: misses
- [any] $\sigma_y^2(1 - \rho^2)$.
:::
:::

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

### Mahalanobis distance of ({x}, {y}) if ρ were 0?

::: misses
- [any] $\sqrt{x^2/\sigma_x^2 + y^2/\sigma_y^2}$.
:::
:::
:::

::: check
kind: parts
given: S = 4 | 25; w = 0.5 | 3; v = 1 | 8; l = 1 | 6

## Ridge on one weight: Σx² = {S}, true w = {w}, noise variance {v}, λ = {l}.

::: part
answer: (l*w/(S + l))^2 + v*S/(S + l)^2
tolerance: 0.5%

### Mean squared error of the estimate?

::: misses
- [any] Squared bias plus variance.
:::
:::

::: part
answer: v/(S + v/w^2)
tolerance: 0.5%

### Error at the best λ?

::: misses
- [any] $\sigma^2/(S + \sigma^2/w^2)$.
:::
:::
:::

::: check
kind: parts
given: a = 0.5 | 3; b = 2 | 10; S = 3 | 15; T = 2 | 12; x = 1 | 4

## Bayesian regression of one weight: α = {a}, β = {b}, Σx² = {S}, Σxt = {T}.

::: part
answer: b*T/(a + b*S)
tolerance: 0.5%

### Posterior mean?

::: misses
- [any] $\beta T/(\alpha + \beta S)$.
:::
:::

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

### Predictive standard deviation at x = {x}?

::: misses
- [any] Noise plus weight uncertainty.
:::
:::

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

### Equivalent ridge λ?

::: misses
- [any] $\alpha/\beta$.
:::
:::
:::

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

## Two classes whose means differ by ({p}, {q}), with diagonal within-class 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%

### Separation J?

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

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

## A logistic model with $w_0$ = {a}, $w_1$ = {b}, and an example at x = {x} with target {t}.

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

### Predicted probability?

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

::: 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: -a/b
tolerance: 0.5%

### Decision boundary?

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

::: check
kind: parts
given: m = 1 | 3; n = -1 | 0; s = 0.5 | 2; p = 0.1 | 0.5; r = 4 | 12; k = 0.25 | 0.5

## LDA with means {m} and {n} and variance {s}²; naive Bayes with prior {p} and feature likelihood ratios {r} and {k}.

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

### LDA weight?

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

::: part
answer: 1/(1 + exp(-(ln(p/(1 - p)) + ln(r) + ln(k))))
tolerance: 0.5%

### Naive Bayes posterior?

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

::: check
kind: parts
given: T = 20 | 70; F = 3 | 18; N = 5 | 25

## TP = {T}, FP = {F}, FN = {N}.

::: 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] $2\text{TP}/(2\text{TP} + \text{FP} + \text{FN})$.
:::
:::
:::

::: check
kind: parts
given: s = 0.8 | 0.97; f = 0.92 | 0.99; p = 0.005 | 0.08; d = 1 | 2.2; c = 0.85 | 0.95; n = 250 | 1000

## A detector with sensitivity {s} and specificity {f} at prevalence {p}; scores {d} standard deviations apart; a measured accuracy {c} on {n} test examples.

::: 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})$.
:::
:::

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

### Standard error of the accuracy?

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

::: check
kind: parts
given: R = 0.03 | 0.08 | 0.15; e = 0.01 | 0.02; D = 2 | 50

## A two-class problem has Bayes error {R}, and a 1-nearest-neighbor classifier works on {D} features.

::: part
answer: 2*R*(1 - R)
tolerance: 0.5%

### What is the most error 1-NN can have with unlimited data?

::: misses
- [any] $2R^*(1 - R^*)$.
:::
:::

::: part
answer: ln(2/0.05)/(2*e^2)
unit: examples
tolerance: 1%

### How many test examples does Hoeffding's inequality require to measure its error within ±{e} at 95% confidence?

::: misses
- [any] $\ln(2/\delta)/(2\epsilon^2)$.
:::
:::

::: part
answer: 0.01^(1/D)
tolerance: 0.5%

### What edge must a neighborhood cube have to hold 1% of uniform data in the unit cube of its features?

::: misses
- [any] $0.01^{1/D}$.
:::
:::
:::
:::
