Statistical Learning · Midterm + Final retake

Pass the retake.

Every question type from the 2026 midterm and final, explained from zero, in the order you should learn it. Built to fit into three hours.

Start with chapter 0 Test yourself first

Your three hours

Tap a block to jump. Mark each chapter done at its end.

Basics Midterm topics Final topics Practice

Short on time? Do only the chapters marked Must. They cover every task that appeared in 2026.

Midterm 2026

April 22 · 90 minutes · 4 tasks

  1. 1aDefine the Beta density (with its range) · Ch 1
  2. 1bDice: P(A twice | A at least once) · Ch 2
  3. 1cGeometric is memoryless · Ch 2
  4. 2Negative binomial + Beta: MLE, posterior, mean, predictive · Ch 3
  5. 3Poisson + Gamma: likelihood, posterior, mode, MAP · Ch 3
  6. 4Bayes for small data; flat prior true/false · Ch 5

Final 2026

June 24 · 90 minutes · 5 tasks

  1. 1a–hEight short theory questions · Ch 6
  2. 2Geometric (drinks) + Beta posterior · Ch 3
  3. 3QDA: define, classify a point, logistic regression · Ch 7
  4. 4Naive Bayes: classify, why priors · Ch 8
  5. 5Four true/false statements with reasons · Ch 13
Chapter 08 min · BasicsMust

Five moves solve almost the whole exam.

Every task in both 2026 exams was one of five moves, applied to a different distribution. Learn the words and the moves here. Every later chapter is practice of these moves.

The six words you need

Parameter \(\theta\)

The unknown number. The chance a coin shows heads, or the average goals per match \(\lambda\). Also called \(p, q, \lambda, \eta\).

Data \(D\)

What you observed: \(D=\{x_1,\dots,x_n\}\). Independent data means you multiply the probabilities.

Likelihood \(p(D\mid\theta)\)

How well a value of \(\theta\) explains the data. Same formula as the model, read as a function of \(\theta\).

Prior \(p(\theta)\)

What you believe about \(\theta\) before the data. Usually Beta or Gamma.

Posterior \(p(\theta\mid D)\)

Your belief after the data. Always: posterior \(\propto\) likelihood \(\times\) prior.

\(\propto\) “proportional to”

Equal up to a constant. You may throw away every factor that does not contain \(\theta\): \(k!\), binomial coefficients, \(\Gamma(a)\), \(b^a\).

The five moves

  1. MLE (maximum likelihood estimate). Take the log of the likelihood, differentiate, set it to 0, solve for \(\theta\).
    Hook: log, derivative, zero.
  2. Posterior. Multiply likelihood by prior, drop constants, add the exponents, recognise the family (Beta or Gamma).
    Hook: multiply, drop, add, recognise.
  3. Point estimates. The posterior mean (the average) or the MAP (the peak, which is the mode). Read them from the family’s formula.
  4. Predictive. The probability of the next observation: average the model over the posterior, \(p(x_{\text{new}}\mid D)=\int p(x_{\text{new}}\mid\theta)\,p(\theta\mid D)\,d\theta\).
  5. Classify. For each class compute score \(=\log(\text{prior}) + \log p(x\mid\text{class})\). The largest score wins.

See all five on one coin

You flip a coin \(n=10\) times and see \(S=7\) heads. One flip has probability \(\theta^{x}(1-\theta)^{1-x}\) with \(x\in\{0,1\}\).

1 · MLE. Likelihood \(\theta^{7}(1-\theta)^{3}\). Log: \(\ell(\theta)=7\log\theta+3\log(1-\theta)\).

\[\ell'(\theta)=\frac{7}{\theta}-\frac{3}{1-\theta}=0\;\Rightarrow\;7(1-\theta)=3\theta\;\Rightarrow\;\hat\theta_{\text{MLE}}=\frac{7}{10}.\]

2 · Posterior. Prior \(\mathrm{Beta}(2,2)\propto\theta^{1}(1-\theta)^{1}\). Multiply and add exponents:

\[p(\theta\mid D)\propto\theta^{7}(1-\theta)^{3}\cdot\theta^{1}(1-\theta)^{1}=\theta^{8}(1-\theta)^{4}\;\Rightarrow\;\mathrm{Beta}(9,5).\]

3 · Estimates. Mean \(\frac{9}{9+5}=0.64\). MAP \(\frac{9-1}{9+5-2}=\frac{8}{12}=0.67\).

4 · Predictive. \(P(\text{next flip is heads}\mid D)=\) posterior mean \(=0.64\).

5 · Classify is the same idea with classes instead of \(\theta\). You will see it in chapters 7 and 8.

Math toolbox

These are the only calculation rules the exams need.

ToolRule
Logs\(\log(ab)=\log a+\log b,\;\;\log a^k=k\log a,\;\;\log\prod_i=\sum_i\log\)
Derivatives\((\log x)'=\tfrac1x,\;\;(\log(1-x))'=-\tfrac{1}{1-x},\;\;(e^{ax})'=ae^{ax}\)
Powers\(x^a x^b=x^{a+b},\;\;e^{a}e^{b}=e^{a+b},\;\;\prod_i x^{k_i}=x^{\sum_i k_i}\)
Gamma function\(\Gamma(n)=(n-1)!,\;\;\Gamma(a+1)=a\,\Gamma(a)\)
Beta integral\(\int_0^1\theta^{x-1}(1-\theta)^{y-1}d\theta=B(x,y)=\frac{\Gamma(x)\Gamma(y)}{\Gamma(x+y)}\)
Gamma integral\(\int_0^\infty\lambda^{a-1}e^{-b\lambda}d\lambda=\frac{\Gamma(a)}{b^a}\)
Geometric series\(\sum_{j\ge0}q^j=\frac{1}{1-q}\) for \(|q|\lt 1\)
2×2 matrix\(\det\begin{pmatrix}a & b\\ c & d\end{pmatrix}=ad-bc,\;\;\begin{pmatrix}a & b\\ c & d\end{pmatrix}^{-1}=\frac{1}{ad-bc}\begin{pmatrix}d & -b\\ -c & a\end{pmatrix}\)
Quadratic form\(v^TAv=a v_1^2+2b\,v_1v_2+d\,v_2^2\) for symmetric \(A=\begin{pmatrix}a & b\\ b & d\end{pmatrix}\)
Remember

A density is fixed by its shape. The constant only makes it integrate to 1. So: find the shape, name the family, done.

Chapter 112 min · MidtermMust

Distributions to know by heart.

The midterm opens with “define this distribution”. The whole exam uses the same dozen formulas. Learn them here, then switch on quiz mode and test yourself.

Asked

Midterm 2026, 1(a): Define the Beta distribution by giving the density. Don’t forget to specify the range of values.
Midterm 2025, 1(a–b): Define the Binomial distribution. Give its conjugate prior by its density.

Write this

For parameters \(a\gt 0,\ b\gt 0\):

\[p(\theta\mid a,b)=\frac{1}{B(a,b)}\,\theta^{a-1}(1-\theta)^{b-1}\quad\text{for }\theta\in[0,1],\qquad p(\theta)=0\text{ otherwise},\]

with \(B(a,b)=\frac{\Gamma(a)\Gamma(b)}{\Gamma(a+b)}\). Mean \(\frac{a}{a+b}\).

Remember

Always write three things: the formula, the range of the variable, the range of the parameters. Add the mean for a bonus.

The dozen

Bernoulli(\(\theta\)) one coin flip

\[\theta^{x}(1-\theta)^{1-x},\quad x\in\{0,1\}\]

\(\theta\in[0,1]\). Mean \(\theta\), variance \(\theta(1-\theta)\).

Binomial(\(N,\theta\)) successes in N flips

\[\binom{N}{k}\theta^{k}(1-\theta)^{N-k},\quad k=0,\dots,N\]

\(\theta\in[0,1]\). Mean \(N\theta\). \(\binom Nk=\frac{N!}{k!(N-k)!}\).

Geometric, trials tries until first success

\[p(1-p)^{k-1},\quad k=1,2,\dots\]

\(p\in(0,1]\). Mean \(\frac1p\). Memoryless.

Geometric, failures failures before success

\[q(1-q)^{k},\quad k=0,1,2,\dots\]

Mean \(\frac{1-q}{q}\). Same model, counted from 0.

Negative binomial failures before r-th success

\[\binom{k+r-1}{k}p^{r}(1-p)^{k},\quad k=0,1,\dots\]

\(r\) known. Mean \(\frac{r(1-p)}{p}\). \(r=1\) gives the geometric.

Poisson(\(\lambda\)) counts: goals, calls

\[e^{-\lambda}\frac{\lambda^{k}}{k!},\quad k=0,1,2,\dots\]

\(\lambda\gt 0\). Mean \(=\) variance \(=\lambda\).

Exponential(\(\lambda\)) waiting times

\[\lambda e^{-\lambda x},\quad x\ge 0\]

\(\lambda\gt 0\). Mean \(\frac1\lambda\). \(P(X\gt t)=e^{-\lambda t}\).

Gaussian \(\mathcal N(\mu,\sigma^2)\) measurements

\[\frac{1}{\sqrt{2\pi\sigma^2}}\,e^{-\frac{(x-\mu)^2}{2\sigma^2}},\quad x\in\mathbb R\]

Mean \(\mu\), variance \(\sigma^2\gt 0\).

Beta(\(a,b\)) a probability

\[\frac{\theta^{a-1}(1-\theta)^{b-1}}{B(a,b)},\quad\theta\in[0,1]\]

\(a,b\gt 0\). Mean \(\frac{a}{a+b}\), mode \(\frac{a-1}{a+b-2}\).

Gamma(\(a,b\)) a positive rate

\[\frac{b^{a}}{\Gamma(a)}\lambda^{a-1}e^{-b\lambda},\quad\lambda\gt 0\]

\(a,b\gt 0\). Mean \(\frac ab\), mode \(\frac{a-1}{b}\), variance \(\frac{a}{b^2}\).

Dirichlet(\(\alpha\)) a probability vector

\[\frac{\Gamma(\sum_k\alpha_k)}{\prod_k\Gamma(\alpha_k)}\prod_{k=1}^{K}\theta_k^{\alpha_k-1}\]

\(\theta_k\ge0,\ \sum_k\theta_k=1\). Mean \(\frac{\alpha_k}{\sum_j\alpha_j}\).

Multivariate Gaussian vectors in \(\mathbb R^D\)

\[\frac{1}{(2\pi)^{D/2}|\Sigma|^{1/2}}e^{-\frac12(x-\mu)^T\Sigma^{-1}(x-\mu)}\]

\(\Sigma\) symmetric, positive definite. \(|\Sigma|=\det\Sigma\).

What the two priors look like

Beta lives on [0, 1]

Beta(1,1) flatBeta(2,2)Beta(5,2)

Gamma lives on (0, ∞)

Gamma(1,1)Gamma(2,1)Gamma(5,1)

Which prior goes with which model

Unknown parameter is…Choose priorUsed with
a probability, between 0 and 1BetaBernoulli, Binomial, Geometric, Negative binomial
a rate, any number above 0GammaPoisson, Exponential, any \(\lambda^{(\dots)}e^{-(\dots)\lambda}\) shape
a vector of probabilities summing to 1DirichletCategorical, Multinomial, bag of words, Naive Bayes class prior
a mean of Gaussian dataGaussianGaussian with known variance, Bayesian linear regression
Remember

Beta for a probability. Gamma for a rate. Dirichlet for several probabilities at once.

Chapter 212 min · MidtermMust

Probability basics that score easy points.

Warm-up questions: a conditional probability with dice, the memoryless property, Bayes’ theorem, and a few definitions. Each has one recipe.

Conditional probability by counting

Midterm 2026, 1(b)

A fair die has sides {A, B, C}, each with probability 1/3. It is rolled twice. Calculate the probability that A occurs twice, given that it occurred at least once.

Draw all \(3\times3=9\) equally likely outcomes. Blue cells contain at least one A. The green cell is AA.

ABC AAAABAC BBABBBC CCACBCC
  1. Definition: \(P(E\mid F)=\dfrac{P(E\cap F)}{P(F)}\). “A twice” already implies “A at least once”, so \(P(E\cap F)=P(AA)\).
  2. \(P(AA)=\frac13\cdot\frac13=\frac19\).
  3. \(P(\text{at least one A})=1-P(\text{no A})=1-\left(\frac23\right)^2=\frac59\).
  4. \(P(AA\mid\text{at least one A})=\dfrac{1/9}{5/9}=\dfrac15\).
Remember

Shrink the world to the condition, then count: 1 good cell out of 5.

Trap

\(\frac13\) is the answer to a different question: “A twice, given the first roll is A”. “At least once” allows the A in either roll.

If they change it

A die with \(m\) sides, two rolls: \(\dfrac{1/m^2}{1-(1-1/m)^2}=\dfrac{1}{2m-1}\). Four sides: \(\frac17\). Six sides: \(\frac1{11}\). More rolls: list or count outcomes the same way.

Bayes’ theorem, marginal and conditional

Final 2026 1(a) · Final 2025 1(a) · Midterm 2025 1(c)

State Bayes’ theorem for densities. Give the posterior \(p(\lambda\mid D)\). Give the density of \(X\), and of \(X\) given \(Y=y\).

Write this \[p(\theta\mid D)=\frac{p(D\mid\theta)\,p(\theta)}{p(D)},\qquad p(D)=\int p(D\mid\theta)\,p(\theta)\,d\theta.\] \[\text{marginal: } p(x)=\int p(x,y)\,dy,\qquad\text{conditional: } p(x\mid y)=\frac{p(x,y)}{p(y)}\ \ (p(y)\gt 0).\]
Remember

Posterior = likelihood × prior / evidence. Marginal = integrate the other variable out. Conditional = joint / marginal. For discrete variables replace \(\int\) by \(\sum\).

The memoryless property

Midterm 2026, 1(c)

\(P(X=k)=p(1-p)^{k-1}\), \(k\in\{1,2,\dots\}\). Calculate \(P(X\ge n+m\mid X\ge n)\). Hint: \(\sum_{k\ge L}p(1-p)^{k-1}=(1-p)^{L-1}\sum_{k\ge1}p(1-p)^{k-1}\). The last sum is 1, but why?

  1. Why the sum is 1. It adds the probabilities of all possible values of \(X\). All probabilities together give 1. (Or: geometric series \(p\cdot\frac{1}{1-(1-p)}=1\).)
  2. Tail. By the hint, \(P(X\ge L)=(1-p)^{L-1}\). In words: the first \(L-1\) tries all failed.
  3. Intersection. \(\{X\ge n+m\}\) is inside \(\{X\ge n\}\), so the intersection is just \(\{X\ge n+m\}\).
  4. Divide. \[P(X\ge n+m\mid X\ge n)=\frac{(1-p)^{n+m-1}}{(1-p)^{n-1}}=(1-p)^{m}.\]
Conclusion to write

The answer \((1-p)^m\) does not depend on \(n\). Having already waited \(n\) tries changes nothing. The geometric distribution is memoryless.

If they change it

Exponential: \(P(X\gt t)=\int_t^\infty\lambda e^{-\lambda x}dx=e^{-\lambda t}\), so \(P(X\gt s+t\mid X\gt s)=\frac{e^{-\lambda(s+t)}}{e^{-\lambda s}}=e^{-\lambda t}\). Geometric counted from 0 (\(q(1-q)^k\)): \(P(X\ge L)=(1-q)^{L}\), same conclusion.

Remember

The coin has no memory.

Independent vs uncorrelated

Final 2026, 5 (true/false)

There exists a pair \((X,Y)\) with correlation 0 but \(X\) and \(Y\) dependent. True.

Take \(X\) uniform on \([-1,1]\) and \(Y=X^2\). Then \(\mathrm{Cov}(X,Y)=E[X^3]-E[X]E[X^2]=0-0=0\), but \(Y\) is completely determined by \(X\). Correlation only measures straight-line dependence; a parabola has none.

  • Independent \(\Rightarrow\) uncorrelated (always).
  • Uncorrelated \(\Rightarrow\) independent only for jointly Gaussian variables.
Remember

Parabola: dependent, but zero correlation.

Exponential family

Midterm 2025, 1(d)

Give one distribution in the exponential family (not Gamma or Exponential) and one that is not.

Form: \(p(x\mid\theta)=h(x)\exp\big(\eta(\theta)\,T(x)-A(\theta)\big)\), and the support (where \(p\gt 0\)) must not depend on \(\theta\).

Write this

In: Poisson, because \(e^{-\lambda}\frac{\lambda^x}{x!}=\frac{1}{x!}\exp(x\log\lambda-\lambda)\). Also Bernoulli, Binomial (fixed \(N\)), Gaussian, Beta.

Out: Uniform on \([0,\theta]\): density \(\frac1\theta\) on \([0,\theta]\). Its support moves with \(\theta\). Also Student-t, Cauchy, Gaussian mixtures.

Chapter 325 min · Midterm and FinalMust

The posterior task. It comes every time.

Six exams in a row had the same task with a different distribution: Negative binomial, Poisson, Geometric, Exponential, even an unknown lifetime density. The steps never change. This is the most valuable chapter.

Asked
  • Midterm 2026, 2: Negative binomial + Beta: MLE, posterior, mean, predictive.
  • Midterm 2026, 3: Poisson (goals) + Gamma: likelihood constant, posterior, prior mode, MAP.
  • Final 2026, 2: Geometric “one more drink” \(\lambda(1-\lambda)^{y-1}\): choose a prior, compute the posterior.
  • Final 2025, 3 and Midterm 2025, 3 and Retake 2025, 3: Geometric, Exponential, Gompertz. Same steps.

The template

  1. Likelihood. Multiply over all data points. Collect the parameter into powers. Everything without the parameter is the constant.
  2. MLE. \(\ell=\log L\). Solve \(\ell'=0\).
  3. Posterior. Likelihood × prior. Add exponents. Recognise Beta or Gamma and read off the new parameters.
  4. Mean / MAP / mode. Plug the new parameters into the family’s formulas.
  5. Predictive. \(\int p(x\mid\theta)p(\theta\mid D)d\theta\) = model constant × ratio of normalising constants (chapter 4).

The kernel table

With \(n\) data points and \(S=\sum_i k_i\) (the sum of the data). This table is the whole task in one line per model.

ModelLikelihood \(\propto\)MLEPriorPosterior
Bernoulli\(\theta^{S}(1-\theta)^{n-S}\)\(\frac Sn\)Beta(a, b)\(\mathrm{Beta}(a+S,\ b+n-S)\)
Binomial, \(N\) each\(\theta^{S}(1-\theta)^{nN-S}\)\(\frac{S}{nN}\)Beta(a, b)\(\mathrm{Beta}(a+S,\ b+nN-S)\)
Geometric \(q(1-q)^k,\ k\ge0\)\(q^{n}(1-q)^{S}\)\(\frac{n}{n+S}\)Beta(a, b)\(\mathrm{Beta}(a+n,\ b+S)\)
Geometric \(\lambda(1-\lambda)^{y-1},\ y\ge1\)\(\lambda^{n}(1-\lambda)^{S-n}\)\(\frac nS\)Beta(a, b)\(\mathrm{Beta}(a+n,\ b+S-n)\)
Negative binomial (\(r\) known)\(p^{nr}(1-p)^{S}\)\(\frac{nr}{nr+S}\)Beta(a, b)\(\mathrm{Beta}(a+nr,\ b+S)\)
Poisson\(e^{-n\lambda}\lambda^{S}\)\(\frac Sn\)Gamma(a, b)\(\mathrm{Gamma}(a+S,\ b+n)\)
Exponential\(\lambda^{n}e^{-\lambda\sum x_i}\)\(\frac{n}{\sum x_i}\)Gamma(a, b)\(\mathrm{Gamma}(a+n,\ b+\sum x_i)\)
Remember

“a gets the successes (or events), b gets the failures (or exposure).” Beta counts successes vs failures. Gamma counts events vs time or matches.

Worked: Poisson + Gamma (Midterm 2026, Task 3)

Goals per match: \(p(k\mid\lambda)=e^{-\lambda}\frac{\lambda^k}{k!}\). Prior \(p(\lambda)=\frac{b^a}{\Gamma(a)}\lambda^{a-1}e^{-b\lambda}\).

(a) Show \(p(D\mid\lambda)\propto e^{-n\lambda}\lambda^S\). Find the constant.

\[p(D\mid\lambda)=\prod_{i=1}^n e^{-\lambda}\frac{\lambda^{k_i}}{k_i!}=\frac{1}{k_1!\cdots k_n!}\;e^{-n\lambda}\,\lambda^{S}.\]

\(n\) factors \(e^{-\lambda}\) give \(e^{-n\lambda}\); the powers add to \(S\). The constant is \(\dfrac{1}{\prod_i k_i!}\).

(b) Posterior.

\[p(\lambda\mid D)\propto e^{-n\lambda}\lambda^{S}\cdot\lambda^{a-1}e^{-b\lambda}=\lambda^{a+S-1}e^{-(b+n)\lambda}\;\Rightarrow\;\mathrm{Gamma}(a+S,\ b+n).\]

(c) Mode of the prior (\(a\gt1\)). Log, derivative, zero:

\[\frac{d}{d\lambda}\big[(a-1)\log\lambda-b\lambda\big]=\frac{a-1}{\lambda}-b=0\;\Rightarrow\;\lambda=\frac{a-1}{b}.\]

Second derivative \(-\frac{a-1}{\lambda^2}\lt 0\), so it is a maximum.

(d) MAP = mode of the posterior. Use (c) with the new parameters: \(\ \hat\lambda_{\text{MAP}}=\dfrac{a+S-1}{b+n}.\)

Numbers: prior Gamma(2, 1), goals {3, 1, 4, 2}: \(n=4,\ S=10\). Posterior Gamma(12, 5). MLE \(2.5\), MAP \(\frac{11}{5}=2.2\), mean \(\frac{12}5=2.4\).

Trap

In the Poisson the rate \(b\) grows by \(n\) (matches), the shape \(a\) grows by \(S\) (goals). Mode \(\frac{a-1}{b}\) is not the mean \(\frac ab\).

Worked: Negative binomial + Beta (Midterm 2026, Task 2)

\(f(k\mid p)=\binom{k+r-1}{k}p^{r}(1-p)^{k}\), \(r\) known. Hint: “things which don’t depend on \(p\) can be … ignored”.

(a) MLE. The binomial coefficient has no \(p\): it is a constant.

\[L(p)\propto p^{nr}(1-p)^{S},\quad\ell(p)=nr\log p+S\log(1-p),\quad\ell'(p)=\frac{nr}{p}-\frac{S}{1-p}=0\;\Rightarrow\;\hat p=\frac{nr}{nr+S}.\]

(b) Posterior with prior Beta(a, b):

\[f(p\mid D)\propto p^{nr}(1-p)^{S}\,p^{a-1}(1-p)^{b-1}=p^{a+nr-1}(1-p)^{b+S-1}\;\Rightarrow\;\mathrm{Beta}(a+nr,\ b+S).\]

(c) Posterior mean: \(\ E[p\mid D]=\dfrac{a+nr}{a+b+nr+S}.\)

(d) Predictive. Put \(\alpha=a+nr,\ \beta=b+S\). Here the coefficient stays, because it contains the new \(k\):

\[f(k\mid D)=\int_0^1\binom{k+r-1}{k}p^{r}(1-p)^{k}\,\frac{p^{\alpha-1}(1-p)^{\beta-1}}{B(\alpha,\beta)}\,dp=\binom{k+r-1}{k}\frac{B(\alpha+r,\ \beta+k)}{B(\alpha,\beta)}.\]
Remember

Successes over (successes + failures): \(nr\) successes in total, \(S\) failures in total. Predictive: “new B over old B”.

Trap

The exponent of \(p\) is \(nr\), not \(n\) or \(r\). Drop the coefficient for the MLE and posterior, but keep it in the predictive.

Worked: Geometric “one more drink” (Final 2026, Task 2)

\(p(y_i\mid\lambda)=\lambda(1-\lambda)^{y_i-1}\), \(y_i\in\{1,2,\dots\}\). Choose a prior, compute the posterior.

Likelihood. \(p(D\mid\lambda)=\prod_i\lambda(1-\lambda)^{y_i-1}=\lambda^{n}(1-\lambda)^{S-n}\), with \(S=\sum_i y_i\).

Prior. \(\lambda\in(0,1)\) is a probability, so choose \(\mathrm{Beta}(a,b)\propto\lambda^{a-1}(1-\lambda)^{b-1}\).

\[p(\lambda\mid D)\propto\lambda^{n+a-1}(1-\lambda)^{S-n+b-1}\;\Rightarrow\;\mathrm{Beta}(a+n,\ b+S-n).\]

Extras if asked: MLE \(\frac nS\), mean \(\frac{a+n}{a+b+S}\), MAP \(\frac{a+n-1}{a+b+S-2}\).

Remember

The exponent of \((1-\lambda)\) counts failures: \(S-n\), because each person keeps the promise exactly once.

Unknown density? Same moves.

Retake 2025 gave a density nobody had seen: the Gompertz lifetime \(f(x\mid\eta)=\eta\,e^{\eta+x-\eta e^{x}}\), \(x\gt0\), with a Gamma(\(a,\lambda\)) prior. Don’t panic. Collect \(\eta\).

(a) With \(S=\sum x_i\) and \(T=\sum e^{x_i}\):

\[f(D\mid\eta)=\prod_i\eta\,e^{\eta+x_i-\eta e^{x_i}}=\eta^{n}e^{n\eta+S-\eta T}=e^{S}\cdot\eta^{n}e^{-\eta(T-n)}.\]

(b) Times the prior \(\eta^{a-1}e^{-\lambda\eta}\):

\[f(\eta\mid D)\propto\eta^{a+n-1}e^{-\eta(\lambda+T-n)}\;\Rightarrow\;\mathrm{Gamma}(a+n,\ \lambda+T-n).\]

(c) With \(n=4,\ S=2,\ T=20,\ a=\lambda=1\): posterior Gamma(5, 17), so \(E[\eta\mid D]=\frac{5}{17}\approx0.29\). This is what the hint about the Gamma mean points to.

If they change it

Look at the kernel. \(\theta^{(\dots)}(1-\theta)^{(\dots)}\) means a Beta prior. \(\theta^{(\dots)}e^{-(\dots)\theta}\) means a Gamma prior. Then read the new parameters straight off the exponents.

Try it: the posterior machine

Pick a model, set the prior, type data. It writes every step the exam wants. Use it to check your own practice answers.

Posterior machine

Prior + data → every step
priorposteriorMLE
Chapter 412 min · MidtermGood to know

Predictions, bets and the two dice.

After the posterior, the exam asks what happens next: the mean, the MAP, the probability of the next observation, or whether to take a bet.

Three estimates, three meanings

EstimateDefinitionBeta(α, β)Gamma(α, β)
MLEmaximises \(p(D\mid\theta)\), data onlyfrom the kernel table (chapter 3)
MAPmaximises \(p(D\mid\theta)p(\theta)\) = posterior mode\(\frac{\alpha-1}{\alpha+\beta-2}\)\(\frac{\alpha-1}{\beta}\)
Posterior mean\(E[\theta\mid D]\), the average\(\frac{\alpha}{\alpha+\beta}\)\(\frac{\alpha}{\beta}\)

As \(n\to\infty\) all three agree: the prior becomes negligible. With a uniform prior, MAP = MLE.

Predictive: average, never plug in

The predictive averages the model over every possible \(\theta\), weighted by the posterior. The integral always becomes a ratio of normalising constants.

Model + posteriorPredictive
Bernoulli, Beta(α, β)\(P(x=1\mid D)=\frac{\alpha}{\alpha+\beta}\)
Geometric \(q(1-q)^k\), Beta(α, β)\(p(k\mid D)=\frac{B(\alpha+1,\ \beta+k)}{B(\alpha,\beta)},\quad p(0\mid D)=\frac{\alpha}{\alpha+\beta}\)
Binomial (N), Beta(α, β)\(p(k\mid D)=\binom Nk\frac{B(\alpha+k,\ \beta+N-k)}{B(\alpha,\beta)}\)
Poisson, Gamma(α, β)\(p(0\mid D)=\int e^{-\lambda}\mathrm{Gamma}(\lambda\mid\alpha,\beta)d\lambda=\left(\frac{\beta}{\beta+1}\right)^{\alpha}\)
Exponential, Gamma(α, β)\(P(X\gt t\mid D)=\left(\frac{\beta}{\beta+t}\right)^{\alpha}\)
Categorical, Dir(α)\(P(x=k\mid D)=\frac{\alpha_k+N_k}{\sum_j\alpha_j+N}\)
Final 2025, 3(b)

COVID infections are geometric \((1-q)^kq\). The posterior is Beta(10, 50). Probability that the next person infects no one?

Write this

\(p(0\mid D)=\int_0^1 q\cdot\mathrm{Beta}(q\mid10,50)\,dq=E[q\mid D]=\frac{10}{10+50}=\frac16.\)

How to get the Poisson \(p(0\mid D)\) in one lineShow
\[\int_0^\infty e^{-\lambda}\frac{\beta^\alpha}{\Gamma(\alpha)}\lambda^{\alpha-1}e^{-\beta\lambda}d\lambda=\frac{\beta^\alpha}{\Gamma(\alpha)}\cdot\frac{\Gamma(\alpha)}{(\beta+1)^\alpha}=\left(\frac{\beta}{\beta+1}\right)^\alpha.\]

The integral is the Gamma integral from the toolbox with \(b\) replaced by \(\beta+1\).

Should you take the bet?

A bet question is expected value: probability of winning × win, minus probability of losing × loss.

Goals. Prior Gamma(1, 1), goals (0, 3, 1, 2). If the next game has no goal you win 50, otherwise you lose 100.

Posterior Gamma(1+6, 1+4) = Gamma(7, 5). \(p(0\mid D)=(5/6)^7\approx0.279\).

EV \(=50\cdot0.279-100\cdot0.721\approx-58\). Don’t take it.


Students. Prior belief: 50% AI, 25% maths, 25% data science, e.g. Dir(2, 1, 1). You see 10 AI, 3 maths, 5 data science. Posterior Dir(12, 4, 6).

\(P(\text{next is AI})=\frac{12}{22}\approx0.545\). Two AI in a row: \(\frac{12}{22}\cdot\frac{13}{23}\approx0.308\) (update after the first).

Win 100 if both are AI, else pay 80: EV \(=100\cdot0.308-80\cdot0.692\approx-24.5\). Don’t take it.

The two dice (Midterm 2025, Task 2)

Die \(h_1\) has faces 1–4, die \(h_2\) has faces 1–8. You see throws, not the die. Only two hypotheses, so every sum has two terms.

  1. Likelihood. \(p(D\mid h_1)=(1/4)^n\) if all throws are ≤ 4, else 0. \(p(D\mid h_2)=(1/8)^n\).
  2. Posterior (uniform prior, all ≤ 4): \(p(h_1\mid D)=\frac{(1/4)^n}{(1/4)^n+(1/8)^n}=\frac{2^n}{2^n+1}\). Each fitting throw doubles the odds for the small die: the size principle.
  3. Model averaging. \(p(x_{n+1}\mid D)=\sum_i p(x_{n+1}\mid h_i)\,p(h_i\mid D)\). For faces 1–4: \(\frac14 w_1+\frac18 w_2\); faces 5–8: \(\frac18 w_2\).
  4. MAP with a strong prior. \(p(h_1)=10^{-6}\), data {3, 4}: \(h_1\): \(10^{-6}\cdot\frac1{16}\approx6\cdot10^{-8}\); \(h_2\): \(\approx\frac1{64}\approx0.016\). MAP picks \(h_2\), even though \(h_1\) is the MLE.
  5. Expected next throw after 15 throws all ≤ 4: \(w_1=\frac{2^{15}}{2^{15}+1}\approx1\), so \(E[X\mid D]=2.5\,w_1+4.5\,w_2\approx2.5\).
Remember

A factor of 4 from the data cannot beat a factor of a million from the prior. MAP = prior × likelihood, not likelihood alone.

Chapter 58 min · MidtermMust

Priors: small data, flat priors, narrow priors.

A pure reasoning task. Write the posterior parameters first, then ask: who is bigger, the prior numbers or the data numbers?

Midterm 2026, 4(a)

Argue true/false: a Bayesian approach is particularly strong at dealing with small datasets. Give an example.

Write this

True. With little data the MLE can be extreme and overconfident. The prior acts like extra pseudo-observations that stabilise the estimate, and the posterior shows how uncertain we still are.

Example: 3 coin flips, 3 heads. MLE \(\hat\theta=\frac33=1\): tails is “impossible”. With prior Beta(1, 1) the posterior is Beta(4, 1) and \(P(\text{heads})=\frac45\). Heads is likely, tails is still possible.

More points you can add: the zero-count (black swan) problem in Naive Bayes; prior knowledge from experts can be built in. Downside: with small data the answer depends a lot on the prior.

Midterm 2026, 4(b)

\(X_i\sim\mathrm{Exp}(\lambda)\), prior \(\lambda\sim\mathrm{Gamma}(\alpha,\beta)\) with prior variance \(10^{10}\) (extremely flat). True or false?

First the posterior: \(\mathrm{Gamma}(\alpha+n,\ \beta+\sum x_i)\). Huge variance \(\frac{\alpha}{\beta^2}\) means tiny \(\beta\): the prior says “I know almost nothing”.

StatementAnswerReason
The posterior equals the prior, ignoring the data.FalseThe posterior depends on \(n\) and \(\sum x_i\). A flat prior has no information, so the data dominates.
The posterior variance tends to infinity; inference is impossible.False\(\frac{\alpha+n}{(\beta+\sum x_i)^2}\) is finite once there is data, and shrinks as \(n\) grows.
For \(n=10^{100}\) the posterior mean ≈ MLE.True\(\frac{\alpha+n}{\beta+\sum x_i}\approx\frac{n}{\sum x_i}=\hat\lambda_{\text{MLE}}\).
The model is undefined because the prior is not conjugate.FalseGamma is conjugate to the Exponential. Even a non-conjugate prior gives a valid posterior.
Remember

Flat prior: data wins. F, F, T, F. More data: posterior mean → MLE, width → 0.

If they change it

Very narrow prior (tiny variance, e.g. Beta(1000, 1000)): the prior wins for small \(n\). After 5 heads in 5 flips the mean is \(\frac{1005}{2005}\approx0.501\). Only a huge \(n\) moves it.

Uniform prior: MAP = MLE exactly. Posterior mean vs MAP: different in general (Beta(2, 5): mean \(\frac27\), mode \(\frac15\)).

Recipe for any prior true/false

  1. Write the posterior parameters (prior numbers + data counts).
  2. Compare sizes: prior numbers vs \(n\) and \(\sum x_i\).
  3. Bigger data ⇒ posterior ≈ MLE. Flat prior ⇒ data wins. Narrow prior ⇒ prior wins for small \(n\).
Chapter 612 min · FinalMust

The short theory questions.

The final opens with eight quick questions worth easy points. Each one has a two-line answer. Read the question, cover the answer, say it out loud, then check.

Final 2026, Task 1

1(a)

State Bayes’ theorem for densities.

\[p(x\mid y)=\frac{p(y\mid x)\,p(x)}{p(y)},\qquad p(y)=\int p(y\mid x)\,p(x)\,dx.\]
Flip the condition, times the prior, over the evidence.
1(b)

Give two modifications of standard linear regression that help against overfitting or sensitivity to outliers.

Ridge regression (penalised least squares), against overfitting: minimise \(\sum_i(y_i-w^Tx_i)^2+\lambda\lVert w\rVert^2\), so \(\hat w=(X^TX+\lambda I)^{-1}X^Ty\). It is the MAP with a Gaussian prior on \(w\).

Laplace (absolute-loss) regression, against outliers: minimise \(\sum_i|y_i-w^Tx_i|\). It is the MLE with Laplace noise; big errors are not squared.

Also accepted: Lasso \(\lambda\lVert w\rVert_1\), Huber loss, Student-t noise.

Penalty on w fights overfitting. Absolute loss fights outliers.
1(c)

Write down a way to generate \(\theta\) so that its density looks like a spike at 0 on top of a wide bump.

Spike-and-slab: first flip a coin, then draw from a thin or a wide Gaussian.

\[\gamma\sim\mathrm{Ber}(\tfrac12),\qquad\theta\mid\gamma\sim\mathcal N(0,\ \sigma^2v_\gamma),\qquad v_1=0.001\ (\text{spike}),\ v_0=1\ (\text{slab}).\]

Used for variable selection: the spike says “this weight is 0”, the slab says “it can be anything”. A two-bump picture at different places is a mixture with different means.

Coin first, then a thin or a fat Gaussian.
1(d)

Which algorithm fits the weights of logistic regression? Describe the iteration step.

Gradient descent on the negative log-likelihood:

\[w_{k+1}=w_k-\eta_k\,g_k,\qquad g_k=\sum_i\big(\mathrm{sigm}(w_k^Tx_i)-y_i\big)x_i.\]

Or Newton / IRLS: \(w_{k+1}=w_k-H_k^{-1}g_k\) with \(H=X^TSX\), \(S=\mathrm{diag}(\mu_i(1-\mu_i))\). No closed form exists, so we iterate (chapter 9).

New = old − step × gradient.
1(e)

In Bayesian linear regression \(y_i=\beta_0+\beta_1x_i+\varepsilon_i\), \(\varepsilon_i\sim\mathcal N(0,\sigma^2)\): which prior is put on \((\beta_0,\beta_1)\), and which class is the posterior?

Prior: a (bivariate) Gaussian, usually \(\mathcal N(0,\tau^2I)\). Posterior: again a multivariate Gaussian, because the Gaussian prior is conjugate to the Gaussian likelihood.

Gaussian in, Gaussian out.
1(f)

Zellner’s g-prior \(\beta\mid\sigma^2\sim\mathcal N\big(\mu_\beta,\ g\sigma^2(X^TX)^{-1}\big)\). Compute the posterior. What happens to the mean as \(g\to\infty\)?

Complete the square (chapter 10 has the steps). Result:

\[\beta\mid y\sim\mathcal N\!\left(\frac{g}{1+g}\hat\beta_{\text{OLS}}+\frac{1}{1+g}\mu_\beta,\ \ \frac{g}{1+g}\sigma^2(X^TX)^{-1}\right),\qquad\hat\beta_{\text{OLS}}=(X^TX)^{-1}X^Ty.\]

As \(g\to\infty\) the mean tends to \(\hat\beta_{\text{OLS}}\): the prior becomes flat. As \(g\to0\) the mean tends to \(\mu_\beta\): the prior dominates.

g/(1+g) of the data plus 1/(1+g) of the prior.
1(g)

Duffing oscillator: \(F_i=-kz_i+\alpha z_i^3+n_i\), \(n_i\sim\mathcal N(0,s^2)\), \(k,\alpha\) unknown. Which model estimates \(k\) and \(\alpha\)? Write the MLE.

The law is linear in the parameters, so use linear regression with basis functions \(z\) and \(z^3\):

\[X=\begin{pmatrix}z_1 & z_1^3\\ \vdots & \vdots\\ z_n & z_n^3\end{pmatrix},\quad\beta=\begin{pmatrix}-k\\ \alpha\end{pmatrix},\quad\hat\beta_{\text{MLE}}=(X^TX)^{-1}X^TF.\]
Linear in the parameters, not in z.
1(h)

What does the mutual information \(I(X,Y)\) describe? How is it used for feature selection in Naive Bayes?

\[I(X;Y)=\sum_{x,y}p(x,y)\log\frac{p(x,y)}{p(x)p(y)}=\mathrm{KL}\big(p(x,y)\,\Vert\,p(x)p(y)\big).\]

It measures how much knowing one variable tells you about the other: how far they are from independent. \(I\ge0\), and \(I=0\) exactly when independent. In NBC: compute \(I(X_j;Y)\) between each feature and the class label, keep the features with the largest values, drop the rest.

Distance from independence.

Also asked in 2025 finals and the retake

Retake 2025, 1(a) · Final 2025, 1(d)

Can logistic regression usually be fitted without a computer? Is there an explicit formula for its weights?

No. Setting the gradient to zero gives \(X^T(\mathrm{sigm}(Xw)-y)=0\). The weights sit inside the sigmoid, so the equation is non-linear and has no closed-form solution. We need an iterative algorithm (gradient descent, Newton, IRLS), which in practice means a computer.

Final 2025, 1(c)

Features are independent and Bernoulli; you want a classifier that trains quickly on little data.

The (Bernoulli) Naive Bayes classifier. It needs only \(C\cdot D\) parameters, each estimated by counting.

Retake 2025, 1(c)

Features are correlated and Gaussian, with known, non-identical covariances per class.

QDA (Gaussian discriminant analysis with a full covariance \(\Sigma_c\) for each class). Not LDA, because the covariances differ. Not Naive Bayes, because the features are correlated.

Retake 2025, 1(d)

Which probabilistic model is the foundation of Google’s PageRank?

A Markov chain: a random surfer jumps from page to page along links. The PageRank of a page is its probability in the stationary distribution of that chain.

Midterm 2025, 4(d)

4 Gaussian features; features 1, 3 are correlated, 2, 4 are correlated, no correlation between the groups. Which model captures this?

A Gaussian per class with a block covariance: full 2×2 blocks for \((x_1,x_3)\) and \((x_2,x_4)\), zeros between the groups. Equivalently \(p(x\mid c)=\mathcal N(x_1,x_3\mid\mu_c^{(13)},\Sigma_c^{(13)})\cdot\mathcal N(x_2,x_4\mid\mu_c^{(24)},\Sigma_c^{(24)})\).

Chapter 718 min · FinalMust

Gaussian classifiers: QDA and LDA.

Every final had one: “classify the point \(x\)” with three Gaussian classes. It is pure arithmetic with 2×2 matrices once you know the score.

Final 2026, Task 3

(a) Explain what QDA is short for, what it tries to achieve, and its assumptions. (b) \(\mu_1=(2,0)^T\), \(\mu_2=(0,2)^T\), \(\mu_3=(0,0)^T\), \(\Sigma_1=\begin{pmatrix}2 & 0\\ 0 & 1\end{pmatrix}\), \(\Sigma_2=\begin{pmatrix}1 & 0\\ 0 & 2\end{pmatrix}\), \(\Sigma_3=\begin{pmatrix}3 & 2\\ 2 & 2\end{pmatrix}\). Classify \(x=(1,0)^T\) with equal priors. (c) Give a classifier for non-Gaussian data and its log-likelihood.

The idea

Each class is a Gaussian cloud with its own centre \(\mu_c\) and shape \(\Sigma_c\). A new point goes to the class whose cloud, weighted by the prior \(\pi_c\), is highest at that point:

\[P(y=c\mid x)=\frac{\pi_c\,\mathcal N(x\mid\mu_c,\Sigma_c)}{\sum_{c'}\pi_{c'}\,\mathcal N(x\mid\mu_{c'},\Sigma_{c'})}.\]

Take the log and drop the \((2\pi)^{-D/2}\) that every class shares. That gives the score:

The score \[g_c(x)=\log\pi_c-\tfrac12\log|\Sigma_c|-\tfrac12\,d_c^2,\qquad d_c^2=(x-\mu_c)^T\Sigma_c^{-1}(x-\mu_c).\]

Largest \(g_c\) wins. \(d_c^2\) is the Mahalanobis distance: distance measured in the class’s own shape.

Four models, one formula

ModelAssumption on \(\Sigma_c\)Decision boundary
QDAeach class has its own full \(\Sigma_c\)quadratic curves
LDAall classes share one \(\Sigma\)straight lines
Gaussian Naive Bayes\(\Sigma_c\) diagonal (features independent given class)quadratic, axis-aligned
Nearest centroid\(\Sigma=\sigma^2I\), equal priorsperpendicular bisectors

LDA. Same shape in every class, so the boundary is a straight line.

QDA. Different shapes, so the boundary curves; here it even closes around the tight class.

(a) The definitions to write

QDA

Quadratic Discriminant Analysis. Goal: put \(x\) into the class with the highest posterior \(P(y=c\mid x)\propto\pi_c\,\mathcal N(x\mid\mu_c,\Sigma_c)\). Assumption: within each class the data are Gaussian with their own mean \(\mu_c\) and their own covariance \(\Sigma_c\). The boundaries \(g_c=g_{c'}\) are quadratic in \(x\), hence the name.

LDA

Linear Discriminant Analysis. Same as QDA, but all classes share one covariance \(\Sigma\). That is the condition for using it. Then \(x^T\Sigma^{-1}x\) and \(\log|\Sigma|\) are the same in every class and cancel, which leaves a score that is linear in \(x\):

\[\delta_c(x)=x^T\Sigma^{-1}\mu_c-\tfrac12\mu_c^T\Sigma^{-1}\mu_c+\log\pi_c.\]

“Discriminant” = the function that separates (discriminates) the classes. “Linear” = its boundaries are straight lines.

Remember

Same Σ → Linear. Own Σ → Quadratic. Diagonal Σ → Naive.

(b) Classify a point: the recipe

  1. For each class compute \(v=x-\mu_c\).
  2. Compute \(|\Sigma_c|=ad-b^2\) and \(\Sigma_c^{-1}=\frac{1}{ad-b^2}\begin{pmatrix}d & -b\\ -b & a\end{pmatrix}\).
  3. Compute \(d_c^2=v^T\Sigma_c^{-1}v\). For a symmetric matrix: \(a'v_1^2+2b'v_1v_2+d'v_2^2\).
  4. Score \(g_c=\log\pi_c-\frac12\log|\Sigma_c|-\frac12d_c^2\). Largest wins.
  5. If asked for probabilities: \(P(c\mid x)=e^{g_c}/\sum_{c'}e^{g_{c'}}\).
Shortcut

Equal priors and equal determinants: just pick the smallest \(d^2\). Equal \(d^2\) and equal determinants: the prior decides.

Worked: Final 2026, 3(b)

\(|\Sigma_1|=|\Sigma_2|=2\), \(|\Sigma_3|=6-4=2\). All equal, priors equal, so compare \(d^2\) only. \(\Sigma_1^{-1}=\mathrm{diag}(\frac12,1)\), \(\Sigma_2^{-1}=\mathrm{diag}(1,\frac12)\), \(\Sigma_3^{-1}=\frac12\begin{pmatrix}2 & -2\\ -2 & 3\end{pmatrix}=\begin{pmatrix}1 & -1\\ -1 & 1.5\end{pmatrix}\).

Class\(x-\mu_c\)\(d_c^2\)\(g_c\) (drop common terms)
1\((-1,0)\)\(\frac12\cdot1=0.5\)−0.25
2\((1,-2)\)\(1+\frac12\cdot4=3\)−1.5
3\((1,0)\)\(1\)−0.5

Class 1. Posteriors \(\approx0.48,\ 0.14,\ 0.38\).

Drill: Final 2025, 2(c)Show answer

\(\mu_1=(3,0)\), \(\mu_2=(0,3)\), \(\mu_3=(0,0)\); \(\Sigma_1=\Sigma_2=I\), \(\Sigma_3=\begin{pmatrix}2 & 1\\ 1 & 1\end{pmatrix}\); \(x=(1,1)\); equal priors.

\(|\Sigma_3|=2-1=1\), so all determinants are 1. \(\Sigma_3^{-1}=\begin{pmatrix}1 & -1\\ -1 & 2\end{pmatrix}\).

\(d_1^2=(-2)^2+1^2=5\), \(d_2^2=1+(-2)^2=5\), \(d_3^2=1-2+2=1\). Class 3.

Drill: Retake 2025, 2(c)Show answer

\(\mu_1=(1,0)\), \(\mu_2=(0,1)\), \(\mu_3=(0,0)\); \(\Sigma_1=\Sigma_2=I\), \(\Sigma_3=\begin{pmatrix}5 & 3\\ 3 & 2\end{pmatrix}\); \(x=(1,1)\); priors \(\frac14,\frac14,\frac12\).

\(|\Sigma_3|=10-9=1\), \(\Sigma_3^{-1}=\begin{pmatrix}2 & -3\\ -3 & 5\end{pmatrix}\). \(d_1^2=0+1=1\), \(d_2^2=1+0=1\), \(d_3^2=2-6+5=1\).

All distances and determinants are equal, so the prior decides: class 3 (prior \(\frac12\)). The exam was built to test exactly this.

QDA calculator

3 classes · 2 features · every step

Estimating the parameters (MLE)

\(\hat\pi_c=\frac{N_c}{N}\), \(\hat\mu_c=\frac{1}{N_c}\sum_{i\in c}x_i\) (class average), QDA: \(\hat\Sigma_c=\frac1{N_c}\sum_{i\in c}(x_i-\hat\mu_c)(x_i-\hat\mu_c)^T\). LDA pools all classes into one \(\hat\Sigma\) and divides by \(N\).

Worked in 1D: cats vs dogs (Retake 2025, Task 5)

Running times: cats 7, 9, 11, 13; dogs 10, 12. \(p(x\mid h)=\mathcal N(\mu_h,\sigma_h^2)\). Classify \(x=10\). Hints: \(\sqrt5\approx2.24\), \(\sqrt e\approx1.65\).

(a) MLE. Cats: \(\mu_C=10\), \(\sigma_C^2=\frac{9+1+1+9}{4}=5\). Dogs: \(\mu_D=11\), \(\sigma_D^2=\frac{1+1}{2}=1\). Priors \(\pi_C=\frac46,\ \pi_D=\frac26\).

(b) Classify 10. Compare \(\pi_h\,\mathcal N(10\mid\mu_h,\sigma_h^2)\); the \(\frac{1}{\sqrt{2\pi}}\) is common, drop it:

\[\text{cat: }\frac23\cdot\frac{1}{\sqrt5}\,e^{0}\approx\frac{0.667}{2.24}\approx0.298,\qquad\text{dog: }\frac13\cdot\frac{1}{1}\,e^{-1/2}\approx\frac{0.333}{1.65}\approx0.202.\]

Cat, with posterior \(\frac{0.298}{0.500}\approx0.60\).

(c) A classifier for non-Gaussian data

Write this

Logistic regression. It models \(P(y=1\mid x)\) directly and makes no assumption on how \(x\) is distributed:

\[p_i=P(y_i=1\mid x_i)=\frac{1}{1+e^{-x_i^T\beta}},\qquad LL(\beta)=\sum_{i=1}^n\Big[y_i\log p_i+(1-y_i)\log(1-p_i)\Big].\]
If they change it

Unequal priors: keep \(\log\pi_c\). Unequal determinants: keep \(-\frac12\log|\Sigma_c|\). LDA version: one shared \(\Sigma\), use \(\delta_c\) above. Gaussian NB version: keep only the diagonals of each \(\Sigma_c\).

Trap

\(d^2\) is never negative; if you get a negative number, the inverse is wrong. Check \(\Sigma\Sigma^{-1}=I\) on one entry.

Next: Naive Bayes →
Chapter 812 min · FinalMust

Naive Bayes: count, multiply, compare.

The naive assumption: given the class, the features are independent. That turns every classification into a product of simple counts.

Asked

Final 2025, 5: pass/fail from attendance and homework data: write the NBC, fit by MLE, classify (0, 1), explain the Bayes approach.
Final 2026, 4: given estimated probabilities, classify (1, 1, 0); explain priors for small data; give the conjugate priors on \(\theta\) and \(\pi\).

The model

\[P(y=c\mid x)\ \propto\ \pi_c\prod_{j=1}^{D}\theta_{jc}^{\,x_j}(1-\theta_{jc})^{1-x_j},\qquad\hat\pi_c=\frac{N_c}{N},\quad\hat\theta_{jc}=\frac{N_{jc}}{N_c}.\]

\(\theta_{jc}\) = probability that feature \(j\) is 1 in class \(c\). \(N_c\) = number of points in class \(c\). \(N_{jc}\) = how many of them have feature \(j\) equal to 1.

  1. Count. Sort the data by class. Count \(N_c\) and \(N_{jc}\).
  2. Table. For the new point use \(\theta\) where the feature is 1, and \(1-\theta\) where it is 0.
  3. Multiply the factors and the prior \(\pi_c\).
  4. Compare (and normalise if probabilities are asked).

Worked: Final 2025, Task 5

Data (class, attendance, homework): (F,1,1), (F,0,0), (P,1,1), (P,1,0), (F,0,1), (F,1,0), (P,1,1). Classify (0, 1).

Class\(\hat\pi_c\)\(\hat\theta_{\text{att}}\)\(\hat\theta_{\text{hw}}\)Score for (0, 1)
Pass (3 points)3/73/3 = 12/3\(\frac37\cdot(1-1)\cdot\frac23=0\)
Fail (4 points)4/72/42/4\(\frac47\cdot\frac12\cdot\frac12=\frac17\)

Fail. Notice why Pass got exactly 0: every passing student attended, so “no attendance” has estimated probability 0. That is the zero-count problem.

Worked: Final 2026, 4(a)

\(\hat\theta_{\text{hw,pass}}=\frac12,\ \hat\theta_{\text{att,pass}}=\frac12,\ \hat\theta_{\text{q,pass}}=0\); \(\hat\theta_{\text{hw,fail}}=0,\ \hat\theta_{\text{att,fail}}=\frac12,\ \hat\theta_{\text{q,fail}}=0\); \(\hat\pi_{\text{pass}}=\frac35\). Classify (1, 1, 0).

Pass: \(\frac35\cdot\frac12\cdot\frac12\cdot(1-0)=\frac{3}{20}\). Fail: \(\frac25\cdot0\cdot\frac12\cdot1=0\).

Pass.

(b) Why priors matter for small data

Write this

With small data, some feature never appears in a class, so \(\hat\theta_{jc}=0\) (or 1). One zero kills the whole product: the class gets probability 0 no matter what the other features say, and if every class gets a zero we even get \(\frac00\). This is the zero-count or black swan problem, a form of overfitting.

Priors add pseudo-counts and remove the zeros. Conjugate priors: \(\theta_{jc}\sim\mathrm{Beta}(a,b)\) and \(\pi\sim\mathrm{Dir}(\alpha_1,\dots,\alpha_C)\). Posterior means:

\[\bar\theta_{jc}=\frac{N_{jc}+a}{N_c+a+b},\qquad\bar\pi_c=\frac{N_c+\alpha_c}{N+\sum_{c'}\alpha_{c'}}\qquad(\text{Laplace: }a=b=\alpha_c=1).\]
Remember

One zero kills the product. Beta for \(\theta\), Dirichlet for \(\pi\).

Advantages of the Bayes approach (Final 2025, 5c): no zero probabilities, sensible estimates on small data, uncertainty is kept, prior knowledge can enter, and it still trains fast by counting.

Other NBC facts they ask

  • Can one feature influence another? No, not within a class: features are conditionally independent given the class. (They can be dependent overall, through the class.)
  • Danger of MLE with fewer points than \(K\cdot D\)? Many counts are 0, so many \(\hat\theta=0\) or 1: zero probabilities and overfitting.
  • Class prior Dir(1, …, 1) after \(N\) observations? Posterior \(\mathrm{Dir}(1+N_1,\dots,1+N_K)\).
  • Many features? Add logs instead of multiplying, to avoid numbers too small for the computer (underflow).
  • Continuous features? Use a 1D Gaussian per feature and class: Gaussian Naive Bayes (like the cats and dogs in chapter 7).

Mutual information for feature selection

\[I(X_j;Y)=\sum_{x_j,\,c}p(x_j,c)\log\frac{p(x_j,c)}{p(x_j)\,p(c)}.\]

Compute it for each feature with the class label. A feature with the same \(\theta\) in every class tells you nothing about the class and has \(I=0\). Keep the top features, drop the ones with small \(I\).

Chapter 98 min · FinalMust

Logistic regression.

A straight line squeezed through an S-curve so the output is always a probability. The exams ask three things: its log-likelihood, why it has no formula, and how to fit it.

The model

\[P(y=1\mid x)=\mu=\mathrm{sigm}(w^Tx)=\frac{1}{1+e^{-w^Tx}}\in(0,1).\]
sigmoid: stays inside (0, 1)OLS line on 0/1 data: leaves [0, 1]

Log-likelihood, gradient, Hessian

\[LL(w)=\sum_i\Big[y_i\log\mu_i+(1-y_i)\log(1-\mu_i)\Big],\qquad\mathrm{NLL}=-LL.\] \[g=\nabla\mathrm{NLL}=\sum_i(\mu_i-y_i)\,x_i=X^T(\mu-y),\qquad H=X^TSX,\ \ S=\mathrm{diag}\big(\mu_i(1-\mu_i)\big).\]
Remember

Gradient = (prediction − truth) × input.

Why there is no formula

Set the gradient to zero: \(X^T(\mathrm{sigm}(Xw)-y)=0\). The unknown \(w\) is trapped inside the sigmoid, so the equation is non-linear. Compare linear regression, where \(X^TXw=X^Ty\) is linear and solvable by hand. So we iterate:

AlgorithmStepNeeds
Gradient (steepest) descent\(w_{k+1}=w_k-\eta_k g_k\)gradient only; step size \(\eta\)
Newton = IRLS\(w_{k+1}=w_k-H_k^{-1}g_k\)gradient and Hessian (2nd derivatives); few steps

The NLL is convex, so any minimum the algorithm finds is the global one.

One step by hand

Data \(x_1=(1,2),\ y_1=1\); \(x_2=(-1,1),\ y_2=0\). Start at \(w_0=(0,0)\), so \(\mu_1=\mu_2=\mathrm{sigm}(0)=0.5\).

\[g=(0.5-1)(1,2)+(0.5-0)(-1,1)=(-0.5,-1)+(-0.5,0.5)=(-1,\,-0.5).\]

Gradient step with \(\eta=1\): \(w_1=w_0-g=(1,\ 0.5)\). The Hessian at \(w_0\) is \(0.25\,X^TX=\begin{pmatrix}0.5 & 0.25\\ 0.25 & 1.25\end{pmatrix}\).

Related facts

The decision boundary \(w^Tx=0\) is a straight line. It is the discriminative twin of LDA. More than 2 classes: softmax \(P(y=c\mid x)=\frac{e^{\beta_c^Tx}}{\sum_{c'}e^{\beta_{c'}^Tx}}\). The Bernoulli in exponential-family form has natural parameter \(\log\frac{\theta}{1-\theta}\), the logit: that is where the sigmoid comes from.

Chapter 1012 min · FinalMust

Linear regression and its relatives.

One model, many variations. The key sentence: Gaussian gives squares, Laplace gives absolute values. On the noise it is the loss; on the weights it is the penalty.

Asked

Final 2025, 4: vector outputs? only linear relations? quadratic or absolute loss with outliers? what is ridge? MLE of \(\sigma^2\).
Final 2026, 1(b, e, f, g) and Retake 2025, 4(a): does standard regression minimise absolute differences?

The model and its MLE

\[y_i=w^Tx_i+\varepsilon_i,\quad\varepsilon_i\sim\mathcal N(0,\sigma^2)\quad\Rightarrow\quad\hat w=(X^TX)^{-1}X^Ty.\]

Maximising the Gaussian likelihood is the same as minimising the residual sum of squares \(\mathrm{RSS}(w)=\sum_i(y_i-w^Tx_i)^2\). So standard linear regression minimises squares, not absolute values (Retake 4a: false).

MLE of the noise variance (Final 2025, 4e)

With \(n\) data points (the \(n\) comes from summing \(n\) copies of \(-\frac12\log\sigma^2\)):

\[\ell(\sigma^2)=-\frac n2\log\sigma^2-\frac{1}{2\sigma^2}\sum_{i=1}^n(y_i-\hat w^Tx_i)^2.\] \[\frac{d\ell}{d\sigma^2}=-\frac{n}{2\sigma^2}+\frac{1}{2\sigma^4}\sum_i(y_i-\hat w^Tx_i)^2=0\;\Rightarrow\;\hat\sigma^2=\frac1n\sum_{i=1}^n(y_i-\hat w^Tx_i)^2=\frac{\mathrm{RSS}}{n}.\]

Differentiate with respect to \(\sigma^2\) as one variable. Tip: call it \(v\) while you differentiate.

True or false, with reasons

StatementAnswerReason
Linear regression can predict vector outputs.True\(\hat W=(X^TX)^{-1}X^TY\): one regression per output column.
It can only model linear relations between \(x\) and \(y\).FalseLinear in \(w\), not in \(x\). Use basis functions \(\phi(x)=(1,x,x^2,\sin x,\dots)\): \(\hat w=(\Phi^T\Phi)^{-1}\Phi^Ty\).
Large noise and outliers: use the quadratic loss.FalseUse the absolute loss (Laplace MLE). A square makes big errors huge, so outliers drag the fit.
\(r^2\): Gaussian, explodes on outliers\(|r|\): Laplace, grows slowly

Fighting overfitting and outliers

MethodMinimisesProbabilistic viewEffect
Least squares\(\sum r_i^2\)Gaussian noise, MLEclosed form
Robust (Laplace)\(\sum|r_i|\)Laplace noise, MLEignores outliers better
Ridge\(\sum r_i^2+\lambda\lVert w\rVert_2^2\)Gaussian prior on \(w\), MAPshrinks \(w\): \((X^TX+\lambda I)^{-1}X^Ty\)
Lasso\(\sum r_i^2+\lambda\lVert w\rVert_1\)Laplace prior on \(w\), MAPsets some weights exactly to 0
Final 2025, 4(d): explain ridge regression

Ridge (penalised least squares) adds a penalty on the size of the weights: minimise \(\sum_i(y_i-w^Tx_i)^2+\lambda\lVert w\rVert^2\). Solution \(\hat w=(X^TX+\lambda I)^{-1}X^Ty\), always invertible for \(\lambda\gt0\). It shrinks the weights towards 0, which reduces overfitting. It equals the MAP estimate with prior \(w\sim\mathcal N(0,\tau^2I)\), \(\lambda=\sigma^2/\tau^2\).

Bayesian linear regression

Prior \(w\sim\mathcal N(0,\tau^2I)\), Gaussian noise. The posterior is Gaussian:

\[\Sigma_N=\Big(\tfrac1{\sigma^2}X^TX+\tfrac1{\tau^2}I\Big)^{-1},\qquad\mu_N=\tfrac{1}{\sigma^2}\Sigma_NX^Ty\ \ (=\text{ridge solution}).\]
Zellner’s g-prior: the derivation in four linesShow

Prior \(\beta\sim\mathcal N(\mu_\beta,\ g\sigma^2(X^TX)^{-1})\), likelihood \(y\mid\beta\sim\mathcal N(X\beta,\sigma^2I)\). Collect the terms in \(\beta\) in the exponent as \(-\frac12[\beta^TA\beta-2\beta^Tb]\); then the posterior is \(\mathcal N(A^{-1}b,\ A^{-1})\).

\[A=\frac{X^TX}{\sigma^2}+\frac{X^TX}{g\sigma^2}=\frac{1+g}{g}\cdot\frac{X^TX}{\sigma^2},\qquad b=\frac{X^Ty}{\sigma^2}+\frac{X^TX\mu_\beta}{g\sigma^2}.\] \[A^{-1}=\frac{g}{1+g}\sigma^2(X^TX)^{-1},\qquad A^{-1}b=\frac{g}{1+g}(X^TX)^{-1}X^Ty+\frac{1}{1+g}\mu_\beta.\]

So the mean is \(\frac{g}{1+g}\hat\beta_{\text{OLS}}+\frac1{1+g}\mu_\beta\). \(g\to\infty\): OLS. \(g\to0\): prior mean. \(g=1\): halfway.

Any law “number × known function”

Duffing \(F=-kz+\alpha z^3\): columns \(z_i,\ z_i^3\). Drag \(F=c_1v+c_2v^2\): columns \(v_i,\ v_i^2\). \(y=a\sin t+bt^2\): columns \(\sin t_i,\ t_i^2\). Add a column of ones for an intercept. Then \(\hat\beta=(X^TX)^{-1}X^Ty\). Not linear if the parameter sits inside the function, like \(e^{kz}\).

Optimisation facts (Retake 2025 true/false)

  • A twice-differentiable \(f\) is convex if \(f''\ge0\) everywhere (curves up). “\(f''\) always negative” describes a concave function: false.
  • Newton’s method uses the second derivative (the Hessian). “Convenient because it does not need the second derivative” is false. Gradient descent is the one that needs only the gradient.
  • Least squares is convex, with one global minimum.
Chapter 118 min · FinalGood to know

Text models: bag of words and Markov chains.

Both 2025 finals ended with this task. The bag of words forgets the order. The Markov chain remembers the last word.

Bag of words (Final 2025, 6a)

Write this

Model: each word is \(x_i\sim\mathrm{Cat}(\theta)\) over \(K\) words, independent. Only counts \(N_k\) matter: \(p(D\mid\theta)=\prod_k\theta_k^{N_k}\).

Prior: \(\theta\sim\mathrm{Dir}(\alpha_1,\dots,\alpha_K)\). Posterior: \(\mathrm{Dir}(\alpha_1+N_1,\dots,\alpha_K+N_K)\).

Next word: \(P(x_{n+1}=k\mid D)=\frac{\alpha_k+N_k}{\sum_j\alpha_j+n}\).

Good for document classification (spam), bad for predicting the next word, because it ignores order.

Markov chain

The next word depends only on the current word. Parameters: start distribution \(\pi_i=P(X_0=i)\) and transition matrix \(A_{ij}=P(X_{t+1}=j\mid X_t=i)\). Row = from, column = to. Each row sums to 1.

  • MLE = count and divide: \(\hat\pi_i=\frac{\#\text{sentences starting with }i}{\#\text{sentences}}\), \(\hat A_{ij}=\frac{N_{ij}}{\sum_{j'}N_{ij'}}\).
  • One step: read \(A_{ij}\). Two steps: \((A^2)_{ij}=\sum_kA_{ik}A_{kj}\).
  • A whole sequence: \(\pi(x_0)\prod_tA_{x_t,x_{t+1}}\). So \(\pi\) and \(A\) give the full distribution (Retake 4f: true).

Worked: Final 2025, 6(c–d)

Sentences: “I Love Math”, “I Love Love Math”, “Math I Love”.

Starts: I, I, Math. \(\pi(I)=\frac23,\ \pi(\text{Love})=0,\ \pi(\text{Math})=\frac13\).

Transitions (inside sentences only): I→Love ×3; Love→Math ×2; Love→Love ×1; Math→I ×1.

from \ toILoveMath
I010
Love01/32/3
Math100

\(P(X_1=\text{Math}\mid X_0=I,\hat A)=\hat A_{I,\text{Math}}=0\). A sign of too little data.

Worked: weather chain (Retake 2025, Task 6)

Rows: sunny \((\frac13,\frac13,\frac13)\), cloudy \((\frac12,\frac14,\frac14)\), rainy \((0,\frac12,\frac12)\). Order: sunny, cloudy, rainy.

(a) The graph. There is no arrow rainy → sunny: probability 0.

(b) Rainy today, cloudy in two days:

\[(A^2)_{R,C}=\underbrace{0\cdot\tfrac13}_{R\to S\to C}+\underbrace{\tfrac12\cdot\tfrac14}_{R\to C\to C}+\underbrace{\tfrac12\cdot\tfrac12}_{R\to R\to C}=\tfrac18+\tfrac14=\tfrac38.\]

(c) Flaw: rainy → sunny is 0, so after rain the sun can never come directly: an artefact of a small sample. Fix: a Dirichlet prior on each row (add-one smoothing) \(\hat A_{ij}=\frac{N_{ij}+1}{N_{i\cdot}+K}\).

Remember

Bag forgets the order; Markov remembers the last word. Row = from, column = to. Zeros in \(A\) from small data: smooth them.

Chapter 125 min · FinalGood to know

Gaussian extras.

Smaller lecture topics that can appear as a true/false or a one-line question.

Linear transformations

If \(X\sim\mathcal N(\mu,\Sigma)\), then \(Y=AX+b\sim\mathcal N(A\mu+b,\ A\Sigma A^T)\).

Example: \(X,Y\) independent \(\mathcal N(0,1)\). \((X+Y,X-Y)\) has covariance \(\begin{pmatrix}2 & 0\\ 0 & 2\end{pmatrix}\). \((X+Y,X+2Y)\) has covariance \(\begin{pmatrix}2 & 3\\ 3 & 5\end{pmatrix}\).

Marginals and conditionals

Split \(x=(x_1,x_2)\). The marginal of \(x_1\) is \(\mathcal N(\mu_1,\Sigma_{11})\): just read the block. The conditional is Gaussian too:

\[x_1\mid x_2\sim\mathcal N\big(\mu_1+\Sigma_{12}\Sigma_{22}^{-1}(x_2-\mu_2),\ \ \Sigma_{11}-\Sigma_{12}\Sigma_{22}^{-1}\Sigma_{21}\big).\]

Learning a Gaussian mean (known \(\sigma^2\))

Prior \(\mu\sim\mathcal N(\mu_0,\tau_0^2)\), \(n\) data with average \(\bar x\). Precisions (1 / variance) add, and the mean is a precision-weighted average:

\[\frac1{\tau_n^2}=\frac1{\tau_0^2}+\frac{n}{\sigma^2},\qquad\mu_n=\tau_n^2\Big(\frac{\mu_0}{\tau_0^2}+\frac{n\bar x}{\sigma^2}\Big).\]

This is the same idea as sensor fusion: a more reliable measurement gets more weight.

Gaussian interpolation (Retake 2025, 4d)

The lecture puts a smoothness prior on the unknown function values that penalises second differences \(x_j-2x_{j+1}+x_{j+2}\) (curvature). With noise-free data the posterior mean passes exactly through the data points and, among all such curves, has the smallest total squared curvature. It is a smooth curve, not a set of straight lines. The uncertainty is 0 at the data points and grows between them.

Answer to the true/false

“The result is straight lines between data points”: false. The curvature prior gives a smooth curve through the points. With noisy data the curve no longer passes exactly through them.

Chapter 138 min · PracticeMust

True or false? Train the reflex.

Every exam has true/false with a justification. The recipe: circle the answer, give one reason, add a formula or a counterexample. Pick an answer on each card, then read the reason.

0 of 0 answered · 40 statements
Chapter 1410 min · PracticeMust

Mock midterm retake.

Same structure as April 2026, new numbers. For each part, say the first step out loud before you open the solution. That is exactly the skill the exam tests.

Task 1 · Warm-up

(a) Define the Gamma distribution by its density. Give the range of values and of the parameters.

SolutionShow

\(p(\lambda\mid a,b)=\frac{b^a}{\Gamma(a)}\lambda^{a-1}e^{-b\lambda}\) for \(\lambda\gt0\) (and 0 otherwise), with \(a\gt0,\ b\gt0\). Mean \(\frac ab\).

(b) A fair die has four sides {A, B, C, D}. It is rolled twice. What is the probability that A occurs twice, given that A occurred at least once?

SolutionShow

16 outcomes. \(P(AA)=\frac1{16}\). \(P(\text{at least one A})=1-(\frac34)^2=\frac7{16}\). Answer \(\frac{1/16}{7/16}=\frac17\). Check with the formula \(\frac1{2m-1}=\frac17\).

(c) Let \(X\sim\mathrm{Exp}(\lambda)\). Show \(P(X\gt t)=e^{-\lambda t}\) and compute \(P(X\gt s+t\mid X\gt s)\).

SolutionShow

\(P(X\gt t)=\int_t^\infty\lambda e^{-\lambda x}dx=\big[-e^{-\lambda x}\big]_t^\infty=e^{-\lambda t}\).

\(\{X\gt s+t\}\subseteq\{X\gt s\}\), so \(P(X\gt s+t\mid X\gt s)=\frac{e^{-\lambda(s+t)}}{e^{-\lambda s}}=e^{-\lambda t}\). It does not depend on \(s\): memoryless.

Task 2 · Binomial + Beta

Every day \(N=10\) people visit a shop; \(k_i\) of them buy. \(f(k\mid\theta)=\binom{N}{k}\theta^k(1-\theta)^{N-k}\). Data \(D=\{k_1,\dots,k_n\}\).

(a) MLE of \(\theta\). (b) Posterior with prior Beta(a, b). (c) Posterior mean and MAP. (d) Numbers: \(D=\{3,5,4\}\), \(a=b=2\).

SolutionShow

(a) \(L(\theta)\propto\theta^{S}(1-\theta)^{nN-S}\), \(S=\sum k_i\). \(\ell'=\frac S\theta-\frac{nN-S}{1-\theta}=0\Rightarrow\hat\theta=\frac{S}{nN}\).

(b) \(\theta^{S}(1-\theta)^{nN-S}\cdot\theta^{a-1}(1-\theta)^{b-1}\Rightarrow\mathrm{Beta}(a+S,\ b+nN-S)\).

(c) Mean \(\frac{a+S}{a+b+nN}\). MAP \(\frac{a+S-1}{a+b+nN-2}\).

(d) \(S=12,\ nN=30\). MLE \(0.4\). Posterior Beta(14, 20). Mean \(\frac{14}{34}\approx0.412\). MAP \(\frac{13}{32}\approx0.406\). Probability the next visitor buys = posterior mean \(\approx0.412\).

Task 3 · Exponential + Gamma

Minutes between buses: \(p(x\mid\lambda)=\lambda e^{-\lambda x}\). Prior Gamma(a, b).

(a) Show \(p(D\mid\lambda)\propto\lambda^ne^{-\lambda T}\) with \(T=\sum x_i\); what is the constant? (b) Posterior. (c) Prior mode for \(a\gt1\). (d) MAP. (e) Numbers: \(a=2,\ b=4\), \(D=\{1,3,2,2\}\). (f) Probability the next wait is longer than 2 minutes.

SolutionShow

(a) \(\prod_i\lambda e^{-\lambda x_i}=\lambda^ne^{-\lambda T}\). It is an equality: the constant is 1.

(b) \(\lambda^{n}e^{-\lambda T}\lambda^{a-1}e^{-b\lambda}\Rightarrow\mathrm{Gamma}(a+n,\ b+T)\).

(c) \(\frac{d}{d\lambda}[(a-1)\log\lambda-b\lambda]=0\Rightarrow\frac{a-1}{b}\). (d) \(\hat\lambda_{\text{MAP}}=\frac{a+n-1}{b+T}\).

(e) \(n=4,\ T=8\). Posterior Gamma(6, 12). MLE \(\frac48=0.5\). MAP \(\frac5{12}\approx0.417\). Mean \(\frac6{12}=0.5\). Prior mode \(\frac14\).

(f) \(\int_0^\infty e^{-2\lambda}\,\mathrm{Gamma}(\lambda\mid6,12)\,d\lambda=\left(\frac{12}{12+2}\right)^6=\left(\frac67\right)^6\approx0.397\).

Task 4 · Priors

(a) Explain MLE, MAP and posterior mean in one sentence each. When do they agree?

SolutionShow

MLE: the \(\theta\) that makes the data most likely (data only). MAP: the peak of the posterior (data and prior). Posterior mean: the average of the posterior. MAP = MLE with a uniform prior; all three agree as \(n\to\infty\).

(b) A coin gets the prior Beta(1000, 1000). True or false? (1) After 5 flips, all heads, the posterior mean is still about 0.5. (2) The prior says “I am almost sure the coin is fair.” (3) After \(10^8\) flips the posterior mean ≈ MLE. (4) MAP = MLE for this prior.

SolutionShow

(1) True: Beta(1005, 1000), mean \(\approx0.501\). (2) True: tiny variance around 0.5. (3) True: \(10^8\gg2000\), data dominates. (4) False: only a uniform prior Beta(1, 1) gives MAP = MLE.

Chapter 1510 min · PracticeMust

Mock final retake.

Same structure as June 2026, with a Markov task added because both 2025 finals had one.

Task 1 · Short questions

(a) Write the density of \(\mathcal N(x\mid\mu,\Sigma)\) in \(\mathbb R^D\). (b) Which classifier for correlated Gaussian features with a different covariance per class? (c) Name the algorithm for logistic regression and write one step. (d) A drag law \(F=c_1v+c_2v^2+\varepsilon\): which model, which MLE? (e) Zellner’s g-prior: what happens to the posterior mean as \(g\to0\)?

SolutionShow

(a) \(\frac{1}{(2\pi)^{D/2}|\Sigma|^{1/2}}\exp\!\big(-\frac12(x-\mu)^T\Sigma^{-1}(x-\mu)\big)\). (b) QDA.

(c) Gradient descent: \(w_{k+1}=w_k-\eta\sum_i(\mathrm{sigm}(w_k^Tx_i)-y_i)x_i\). Or Newton \(w_{k+1}=w_k-H^{-1}g\).

(d) Linear regression with columns \(v_i,\ v_i^2\): \(\hat c=(X^TX)^{-1}X^TF\). (e) The mean \(\frac{g}{1+g}\hat\beta_{\text{OLS}}+\frac1{1+g}\mu_\beta\to\mu_\beta\): the prior dominates.

Task 2 · An unfamiliar density

\(f(x\mid\eta)=\eta\,x^{\eta-1}\) for \(x\in(0,1)\), \(\eta\gt0\). Let \(T=-\sum_i\log x_i\) (a positive number).

(a) Compute \(f(D\mid\eta)\). (b) The MLE. (c) Choose a prior and compute the posterior. (d) Numbers: \(n=4,\ T=2\), prior Gamma(2, 2).

SolutionShow

(a) \(\prod_i\eta x_i^{\eta-1}=\eta^n\exp\big((\eta-1)\sum\log x_i\big)=\eta^ne^{-(\eta-1)T}=e^{T}\cdot\eta^ne^{-\eta T}\).

(b) \(\ell=n\log\eta-\eta T+\text{const}\), \(\frac n\eta-T=0\Rightarrow\hat\eta=\frac nT\).

(c) Kernel \(\eta^{(\dots)}e^{-(\dots)\eta}\), so choose Gamma(a, b): posterior \(\mathrm{Gamma}(a+n,\ b+T)\).

(d) Gamma(6, 4). Mean \(1.5\), MAP \(\frac54\), MLE \(2\).

Task 3 · Gaussian classification

\(\mu_1=(1,1)\), \(\mu_2=(-1,0)\), \(\mu_3=(0,2)\); \(\Sigma_1=I\), \(\Sigma_2=\begin{pmatrix}2 & 0\\ 0 & \frac12\end{pmatrix}\), \(\Sigma_3=\begin{pmatrix}2 & 1\\ 1 & 1\end{pmatrix}\); \(x=(0,1)\).

(a) What is LDA short for? Can it be used here? (b) Classify \(x\) with equal priors. (c) Classify with priors 0.2, 0.2, 0.6. (d) Write the log-likelihood of logistic regression.

SolutionShow

(a) Linear Discriminant Analysis. It needs one shared covariance; here the \(\Sigma_c\) differ, so use QDA.

(b) All determinants are 1 (\(1,\ 2\cdot\frac12,\ 2-1\)). \(\Sigma_2^{-1}=\mathrm{diag}(\frac12,2)\), \(\Sigma_3^{-1}=\begin{pmatrix}1 & -1\\ -1 & 2\end{pmatrix}\).

\(d_1^2=(-1)^2+0=1\). \(d_2^2\): \(v=(1,1)\), \(\frac12+2=2.5\). \(d_3^2\): \(v=(0,-1)\), \(2\cdot1=2\). Smallest is class 1: class 1.

(c) \(g_1=\log0.2-0.5=-2.11\), \(g_2=\log0.2-1.25=-2.86\), \(g_3=\log0.6-1=-1.51\). Class 3: the prior flipped the answer.

(d) \(LL(\beta)=\sum_i[y_i\log p_i+(1-y_i)\log(1-p_i)]\), \(p_i=\frac1{1+e^{-x_i^T\beta}}\). Load “Mock final” in the QDA calculator to check (b) and (c).

Task 4 · Naive Bayes

Emails (class, contains “free”, has a link): (S,1,1), (S,1,0), (S,0,1), (H,0,0), (H,1,0), (H,0,0), (H,0,1). S = spam, H = normal.

(a) Fit the NBC by MLE. (b) Classify (1, 1) and (0, 0). (c) Redo (1, 1) with Laplace smoothing (Beta(1,1) and Dir(1,1) priors). (d) Why use priors?

SolutionShow

(a) \(\hat\pi_S=\frac37,\ \hat\pi_H=\frac47\). Spam: \(\hat\theta_{\text{free}}=\frac23,\ \hat\theta_{\text{link}}=\frac23\). Normal: \(\hat\theta_{\text{free}}=\frac14,\ \hat\theta_{\text{link}}=\frac14\).

(b) (1, 1): S \(\frac37\cdot\frac23\cdot\frac23=\frac4{21}\approx0.190\); H \(\frac47\cdot\frac14\cdot\frac14=\frac1{28}\approx0.036\): spam (\(P\approx0.84\)). (0, 0): S \(\frac37\cdot\frac13\cdot\frac13\approx0.048\); H \(\frac47\cdot\frac34\cdot\frac34\approx0.321\): normal.

(c) \(\bar\theta_S=\frac{2+1}{3+2}=\frac35\), \(\bar\theta_H=\frac{1+1}{4+2}=\frac13\), \(\bar\pi_S=\frac{3+1}{7+2}=\frac49\). S \(\frac49\cdot\frac9{25}=0.16\); H \(\frac59\cdot\frac19\approx0.062\): still spam (\(P\approx0.72\), less extreme).

(d) Small data gives zero counts, and one zero sets the whole class to 0. Priors add pseudo-counts: Beta for \(\theta\), Dirichlet for \(\pi\).

Task 5 · True or false

(1) Newton’s method needs only the gradient. (2) LDA with a shared covariance gives linear boundaries. (3) Laplace noise leads to minimising the sum of absolute errors. (4) For a Markov chain, \(P(X_2=j\mid X_0=i)=(A_{ij})^2\).

SolutionShow

(1) False: it needs the Hessian. (2) True: the quadratic term cancels. (3) True: \(-\log\) of \(e^{-|r|/b}\) is \(|r|/b\). (4) False: it is \((A^2)_{ij}=\sum_kA_{ik}A_{kj}\), the entry of the matrix product.

Task 6 · Markov chain

Weather sequences: (sun, sun, rain), (rain, sun, sun, sun), (sun, rain, rain).

(a) MLE of \(\pi\) and \(A\). (b) \(P(X_2=\text{rain}\mid X_0=\text{sun})\). (c) Probability of the sequence (sun, sun, rain).

SolutionShow

(a) Starts: sun, rain, sun: \(\pi(\text{sun})=\frac23,\ \pi(\text{rain})=\frac13\). Transitions: sun→sun 3, sun→rain 2, rain→sun 1, rain→rain 1. Rows: sun \((\frac35,\frac25)\), rain \((\frac12,\frac12)\).

(b) \(\frac35\cdot\frac25+\frac25\cdot\frac12=\frac6{25}+\frac5{25}=\frac{11}{25}=0.44\).

(c) \(\pi(\text{sun})\,A_{ss}\,A_{sr}=\frac23\cdot\frac35\cdot\frac25=\frac4{25}=0.16\).

Chapter 16Read right before the exam

Cheat sheet.

Everything on one screen. Read it once more just before you walk in.

Bayes\(p(\theta\mid D)\propto p(D\mid\theta)p(\theta)\); marginal \(\int p(x,y)dy\); conditional \(\frac{p(x,y)}{p(y)}\)
MLElog, derivative, zero
Posteriormultiply, drop constants, add exponents, recognise
Beta(a, b)\(\frac{1}{B(a,b)}\theta^{a-1}(1-\theta)^{b-1}\), \(\theta\in[0,1]\); mean \(\frac{a}{a+b}\); mode \(\frac{a-1}{a+b-2}\)
Gamma(a, b)\(\frac{b^a}{\Gamma(a)}\lambda^{a-1}e^{-b\lambda}\), \(\lambda\gt0\); mean \(\frac ab\); mode \(\frac{a-1}b\); var \(\frac a{b^2}\)
Dice\(\frac{P(E\cap F)}{P(F)}\); at least once \(1-(1-p)^n\); \(m\) sides, twice given once: \(\frac1{2m-1}\)
Memoryless\(P(X\ge L)=(1-p)^{L-1}\); \(P(X\ge n+m\mid X\ge n)=(1-p)^m\)
Poisson + Gammaconstant \(\frac1{\prod k_i!}\); Gamma\((a+S,\,b+n)\); MLE \(\frac Sn\); MAP \(\frac{a+S-1}{b+n}\)
Exp + GammaGamma\((a+n,\,b+\sum x_i)\); MLE \(\frac{n}{\sum x_i}\)
Neg. bin + BetaBeta\((a+nr,\,b+S)\); MLE \(\frac{nr}{nr+S}\); predictive \(\binom{k+r-1}{k}\frac{B(\alpha+r,\beta+k)}{B(\alpha,\beta)}\)
Geometric + Beta\(k\ge0\): Beta\((a+n,\,b+S)\), \(p(0\mid D)=\frac\alpha{\alpha+\beta}\); \(y\ge1\): Beta\((a+n,\,b+S-n)\)
Poisson predictive\(p(0\mid D)=\big(\frac{\beta}{\beta+1}\big)^{\alpha}\); bets: expected value
Flat priordata wins; variance finite; mean → MLE; still conjugate (F, F, T, F)
Gaussian\((2\pi)^{-D/2}|\Sigma|^{-1/2}\exp(-\frac12(x-\mu)^T\Sigma^{-1}(x-\mu))\)
QDA score\(\log\pi_c-\frac12\log|\Sigma_c|-\frac12d_c^2\); own Σ quadratic, shared Σ linear (LDA)
2×2 inverse\(\frac1{ad-bc}\begin{pmatrix}d & -b\\ -c & a\end{pmatrix}\); \(v^TAv=av_1^2+2bv_1v_2+dv_2^2\)
Naive Bayes\(\pi_c\prod_j\theta_{jc}^{x_j}(1-\theta_{jc})^{1-x_j}\); zeros → Beta(θ), Dirichlet(π)
Logistic\(\mathrm{sigm}(w^Tx)\); \(g=X^T(\mu-y)\); \(w\leftarrow w-\eta g\); Newton \(w-H^{-1}g\); no formula
Linear reg.\(\hat w=(X^TX)^{-1}X^Ty\); \(\hat\sigma^2=\frac{\mathrm{RSS}}n\); ridge \((X^TX+\lambda I)^{-1}X^Ty\)
Robust / sparseoutliers → \(|r|\) (Laplace noise); Lasso → exact zeros (Laplace prior)
g-priormean \(\frac g{1+g}\hat\beta_{\text{OLS}}+\frac1{1+g}\mu_\beta\); \(g\to\infty\): OLS
Spike-and-slab\(\gamma\sim\mathrm{Ber}(\frac12)\), \(\theta\mid\gamma\sim\mathcal N(0,\sigma^2v_\gamma)\), \(v_1=0.001,\ v_0=1\)
Markov\(\hat A_{ij}=\frac{N_{ij}}{N_{i\cdot}}\); two steps \(A^2\); zeros → add-one smoothing
Bag of wordsDir\((\alpha+N)\); next word \(\frac{\alpha_k+N_k}{\sum\alpha+n}\)
Mutual info\(\mathrm{KL}(p(x,y)\Vert p(x)p(y))\); 0 iff independent; drop small-MI features

Ten hooks

Log, derivative, zeroMLE
Multiply, drop, add, recogniseposterior
a gets successes, b gets failuresconjugate update
Shrink the world, then countconditional probability
The coin has no memorygeometric, exponential
Flat prior: data winspriors true/false
Same Σ linear, own Σ quadraticLDA vs QDA
One zero kills the productNaive Bayes
New = old − step × gradientlogistic regression
Gaussian squares, Laplace absolutesregression variants