Maximum likelihood estimation
Where loss functions actually come from. Pick a probability model, ask which parameters make the data you saw most probable, and out fall the sample mean, the fraction of heads, and the cross-entropy loss you have been using since chapter 11.
The MLE pre-reading (v1.1) has a few slips. This chapter uses the corrected versions:
- Fair coin, eq. (14): $\tfrac12\cdot\tfrac12\cdot(1-\tfrac12)\cdot\tfrac12 = \tfrac{1}{16}$, not $\tfrac18$.
- Gaussian grades, eqs. (36)–(40): the density values don't match the stated parameters. With $\mu = 80, \sigma^2 = 5^2$ the three densities are $0.04839, 0.07979, 0.000886$ and the likelihood is $3.42\times10^{-6}$ (notes: $6.02\times10^{-11}$). With $\mu = 85, \sigma^2 = 10^2$ they are $0.02420, 0.03521, 0.02420$, likelihood $2.06\times10^{-5}$ (notes: $2.19\times10^{-9}$). The conclusion still holds — the second parameter set is better — but by a factor of about 6, not 36.
- §3.2: "to find the parameters that minimize the likelihood" should be maximize.
- §3.5.4 writes $\mathcal N(\mu, \sigma)$ where the rest of the notes write $\mathcal N(\mu, \sigma^2)$. The formulas use $\sigma^2$ as the variance throughout.
1. Why we need this: where do loss functions come from?
So far each model came with a loss that was just handed to you. Linear regression used squared error. Logistic regression used cross-entropy, justified by "it's convex and the gradient is clean". Why those? Would $|y - \hat y|^3$ be just as good?
Maximum likelihood estimation (MLE) is the principle behind those choices. It goes in two steps:
- Say what you believe about how the data was generated, as a probability distribution with unknown parameters $\theta$: "each flip is heads with probability $\phi$", "grades are bell-shaped around some $\mu$".
- Choose the $\theta$ under which the data you actually observed is most probable.
Do that and the loss isn't a choice any more. It's derived. By the end of this chapter, cross-entropy will come out of a coin-flip model with no extra assumptions (§8).
You find a coin that came up heads in 9 of 10 flips. Suspects: "fair coin", "coin with $\phi = 0.9$", "coin with $\phi = 0.5$ but you got lucky". The detective asks of each suspect: if it were you, how likely is what we saw? The suspect that makes the evidence least surprising is the best explanation. MLE is that detective, searching over every possible $\phi$. (Where it breaks: a real detective also weighs how plausible each suspect was beforehand. MLE doesn't — that's the extra step in MAP estimation, later in the course.)
2. The four distributions we'll use
The first two are discrete (they give probabilities of outcomes); the normal distributions are continuous (they give probability densities).
| Bernoulli | Categorical | Normal | Multivariate normal | |
|---|---|---|---|---|
| Canonical example | one flip of a (maybe biased) coin | one roll of a (maybe weighted) $K$-sided die | height of a person | height and weight of a person |
| Random variable | $Y\in\{0,1\}$ | one-hot $\mathbf Y = [Y_1,\dots,Y_K]^\top$ | $Y\in\R$ | $\mathbf X\in\R^M$ |
| Parameters | $\phi\in[0,1]$ | $\phi_k\in[0,1]$, $\sum_k\phi_k = 1$ | $\mu\in\R$, $\sigma^2 > 0$ | $\boldsymbol\mu\in\R^M$, $\Sigma\in\R^{M\times M}$ |
| p.m.f. / p.d.f. | $p(y\mid\phi) = \begin{cases}\phi & y=1\\ 1-\phi & y=0\end{cases}$ | $P(Y_k = 1\mid\boldsymbol\phi) = \phi_k$ | $\dfrac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{(y-\mu)^2}{2\sigma^2}}$ | $\dfrac{1}{(2\pi)^{M/2}|\Sigma|^{1/2}}e^{-\frac12(\mathbf x-\boldsymbol\mu)^\top\Sigma^{-1}(\mathbf x-\boldsymbol\mu)}$ |
Two notes on notation. In the categorical row, the outcome is stored as a one-hot vector: "green" on a red/green/blue/magenta die is $[0,1,0,0]^\top$, so exactly one $Y_k$ is 1. In the normal row, $\sigma^2$ is the variance and $\sigma$ the standard deviation. For the multivariate normal, $|\Sigma|$ is the determinant of the covariance matrix and $\Sigma^{-1}$ its inverse; in 2-D the bell curve becomes a "hat".
"A density can't be bigger than 1." It can. A normal with $\sigma = 0.1$ has a peak density of $\frac{1}{\sqrt{2\pi}\,0.1} \approx 3.99$. Only the area must be 1. So a Gaussian likelihood can be larger than 1, and its log can be positive, and neither means something went wrong.
3. Likelihood: how probable is the data, given the parameters?
For a dataset $\D = \{y^{(1)},\dots,y^{(N)}\}$ and parameters $\theta$, the likelihood is the probability (or density) of the data given the parameters:
$$ p(\D\mid\theta) = \prod_{i=1}^{N} p\bigl(y^{(i)}\mid\theta\bigr). $$The superscript $(i)$ is an index ("the $i$-th data point"), not a power.
Why can we multiply? Because we assume the data is i.i.d.: independent (one flip doesn't affect the next) and identically distributed (every flip uses the same $\theta$). For independent events, the joint probability is the product. Without that assumption you'd be stuck with each outcome depending on all the previous ones, each with its own parameters:
$$ p(y^{(1)}\mid\theta_1)\,p(y^{(2)}\mid y^{(1)},\theta_2)\,p(y^{(3)}\mid y^{(1)},y^{(2)},\theta_3)\cdots $$which you couldn't estimate from one dataset.
Three worked likelihoods
Compute $p(\D\mid\phi)$ in general, then for a fair coin and for $\phi = 3/10$.
General form
Plug in
Fair, $\phi = \tfrac12$: $\left(\tfrac12\right)^3\cdot\tfrac12 = \tfrac{1}{16} = 0.0625$. (The notes say $\tfrac18$, which is a typo.)
Biased, $\phi = \tfrac{3}{10}$: $\left(\tfrac{3}{10}\right)^3\cdot\tfrac{7}{10} = \tfrac{189}{10000} = 0.0189$.
Takeaway
The fair coin explains 3-heads-in-4 better than the tails-biased one, by a factor of about 3.3. Hold on to that "compare two $\theta$'s" idea, because it's the whole of MLE.
Sides in order red, green, blue, magenta with probabilities $\phi_1,\phi_2,\phi_3,\phi_4$.
General form
Blue never appeared, so $\phi_3$ doesn't show up at all.
Plug in
Fair die, $\boldsymbol\phi = [\tfrac14,\tfrac14,\tfrac14,\tfrac14]$: $\left(\tfrac14\right)^5 = \tfrac{1}{1024} \approx 0.000977$.
Weighted, $\boldsymbol\phi = [\tfrac{2}{10},\tfrac{1}{10},\tfrac{4}{10},\tfrac{3}{10}]$: $\tfrac{2}{10}\cdot\left(\tfrac{1}{10}\right)^2\cdot\left(\tfrac{3}{10}\right)^2 = \tfrac{18}{100000} = 0.00018$.
Takeaway
The weighted die puts 40% on blue, which never came up, and only 10% on green, which came up twice. So it explains this data about 5× worse than a fair die.
$p(\D\mid\mu,\sigma^2) = p(75\mid\mu,\sigma^2)\,p(95\mid\mu,\sigma^2)\,p(80\mid\mu,\sigma^2)$, each factor the normal density. Compare $(\mu,\sigma^2) = (80, 5^2)$ with $(85, 10^2)$.
$\mu = 80$, $\sigma = 5$
$p(75) = \frac{1}{\sqrt{2\pi}\cdot5}e^{-25/50} = 0.07979\cdot0.6065 = 0.04839$; $p(80) = 0.07979$ (the peak); $p(95) = 0.07979\cdot e^{-225/50} = 0.000886$.
Likelihood $= 0.04839\times0.07979\times0.000886 = 3.42\times10^{-6}$.
$\mu = 85$, $\sigma = 10$
$p(75) = p(95) = \frac{1}{\sqrt{2\pi}\cdot10}e^{-100/200} = 0.02420$; $p(80) = 0.03989\cdot e^{-25/200} = 0.03521$.
Likelihood $= 0.02420^2\times0.03521 = 2.06\times10^{-5}$.
Takeaway
The second is about 6× more likely. The narrow curve at 80 almost rules out the 95 (density 0.000886), and that one point drags its whole product down. Notice also how small these numbers are with only three points. That becomes a real problem in §5.
4. Maximum likelihood: flip the question around
A single likelihood value like $3.42\times10^{-6}$ means almost nothing on its own. Comparing two values does: it told us $(85, 10^2)$ beats $(80, 5^2)$. MLE takes that to its conclusion and searches over all parameter values for the best one:
$$ \hat\theta_{\text{MLE}} = \argmax_\theta\ p(\D\mid\theta) = \argmax_\theta \prod_{i=1}^N p\bigl(y^{(i)}\mid\theta\bigr). $$To stress that we're now varying $\theta$ with the data held fixed, it's written as the likelihood function $L(\theta;\D) = p(\D\mid\theta)$, often just $L(\theta)$. It is the same formula as before. The only thing that changed is which symbol we treat as the variable.
It's tempting to read "the likelihood of $\phi = 0.75$ is 0.105" as "there's a 10.5% chance $\phi$ is 0.75". It isn't. The likelihood is still the probability of the data given the parameters. We're just looking for the parameters that make it largest. Figure 2's area readout makes it concrete: integrate $L$ over $\phi$ and you don't get 1. Turning it into $p(\theta\mid\D)$ needs Bayes' rule and a prior.
5. Take the log, then negate it
In practice we never maximise $L$ directly. We use the log-likelihood
$$ \ell(\theta;\D) = \log L(\theta;\D) = \log\prod_{i=1}^N p\bigl(y^{(i)}\mid\theta\bigr) = \sum_{i=1}^N \log p\bigl(y^{(i)}\mid\theta\bigr), $$for two reasons:
A product of many numbers below 1 shrinks exponentially. Three grades already gave $10^{-6}$. With thousands of points, the true value is smaller than the smallest number a computer can store, so it gets rounded to exactly 0. Then every $\theta$ "has likelihood 0" and you can't compare them. A sum of logs stays a reasonable size.
Differentiating a product of $N$ factors needs the product rule $N$ times. The log turns it into a sum, which you differentiate term by term. It also cancels the $\exp$ in Gaussians (and other exponential-family distributions).
Why is it allowed? $\log$ is monotonically increasing: if $L(\theta_B) > L(\theta_A)$ then $\log L(\theta_B) > \log L(\theta_A)$. It changes the values but never the order, so the $\theta$ at the top is the same:
$$ \hat\theta_{\text{MLE}} = \argmax_\theta\ p(\D\mid\theta) = \argmax_\theta\ \log p(\D\mid\theta). $$Then negate. We've built all our tools around minimising (gradient descent), so flip the sign: $\argmax_\theta \ell(\theta) = \argmin_\theta\,[-\ell(\theta)]$. The negative log-likelihood (NLL) is now an ordinary loss to minimise.
- Write down the likelihood $p(\D\mid\theta)$.
- Set the objective to the negative log-likelihood: $J(\theta) = -\log p(\D\mid\theta)$.
- Compute $\partial J/\partial\theta$.
- Find $\hat\theta_{\text{MLE}}$ by (a) setting the derivative to zero and solving, or (b) gradient descent.
Steps 3 and 4 are exactly what we've done for linear regression, logistic regression, and neural networks. Only steps 1–2 are new.
6. MLE for a coin: the fraction of heads
A notation trick first. With specific data we could write each factor as $\phi$ or $1-\phi$ by inspection. With a general $y^{(i)}$ we need one expression that picks the right one automatically. Use the indicator $\mathbb 1(\cdot)$, which is 1 when its condition is true and 0 otherwise, as an exponent:
$$ p\bigl(y^{(i)}\mid\phi\bigr) = \phi^{\,\mathbb 1(y^{(i)}=1)}\,(1-\phi)^{\,\mathbb 1(y^{(i)}=0)}. $$If $y^{(i)} = 1$ this is $\phi^1(1-\phi)^0 = \phi$. If $y^{(i)} = 0$ it's $\phi^0(1-\phi)^1 = 1-\phi$. Anything to the power 0 is 1, so the factor that doesn't apply drops out.
Derivation
Suppose $\D$ has $N_H$ heads and $N_T$ tails ($N = N_H + N_T$).
Log-likelihood. The log of a product is the sum of logs, and $\log a^b = b\log a$:
$$ \ell(\phi) = \sum_{i=1}^N\Bigl[\mathbb 1(y^{(i)}=1)\log\phi + \mathbb 1(y^{(i)}=0)\log(1-\phi)\Bigr] = N_H\log\phi + N_T\log(1-\phi). $$(Summing the indicator $\mathbb 1(y^{(i)}=1)$ over all $i$ just counts the heads.)
Differentiate and set to zero.
$$ \frac{d\ell}{d\phi} = \frac{N_H}{\phi} - \frac{N_T}{1-\phi} = \frac{(1-\phi)N_H - \phi N_T}{\phi(1-\phi)} = 0. $$A fraction is zero when its numerator is: $(1-\phi)N_H - \phi N_T = 0 \Rightarrow N_H - \phi(N_H + N_T) = 0$, so
$$ \hat\phi_{\text{MLE}} = \frac{N_H}{N_H + N_T} = \frac{N_H}{N}. $$Is it a maximum? $\frac{d^2\ell}{d\phi^2} = -\frac{N_H}{\phi^2} - \frac{N_T}{(1-\phi)^2} < 0$ everywhere, so $\ell$ is concave and this is the maximum. (The notes skip this check.)
That's the answer you'd have guessed, the fraction of flips that came up heads. MLE agrees with intuition, and the same machinery keeps working when intuition runs out.
Find $\hat\phi_{\text{MLE}}$ and its likelihood. Compare with the fair coin's $\tfrac1{16}$.
Step 1 — Count
$N_H = 3$, $N_T = 1$, so $\hat\phi = 3/4$.
Step 2 — Likelihood at the MLE
$L(3/4) = (3/4)^3(1/4) = 27/256 \approx 0.1055$.
Answer & the takeaway
$0.1055$ vs $0.0625$ (fair) vs $0.0189$ ($\phi = 0.3$). Nothing beats the MLE, by definition. Now the warning: after one flip that lands heads, the MLE is $\hat\phi = 1$, "this coin always lands heads". With little data, MLE trusts the data completely and overfits. That's the same disease regularisation treated in chapter 12, and MAP estimation will treat it here.
7. MLE for a die: the trick the notes held back
The same indicator trick, with one-hot data. For data point $i$, exactly one $y_k^{(i)}$ is 1:
$$ p\bigl(\mathbf y^{(i)}\mid\boldsymbol\phi\bigr) = \prod_{k=1}^{K}\phi_k^{\,\mathbb 1(y^{(i)}_k = 1)}, \qquad L(\boldsymbol\phi) = \prod_{i=1}^{N}\prod_{k=1}^{K}\phi_k^{\,\mathbb 1(y^{(i)}_k = 1)}. $$For "green" $= [0,1,0,0]^\top$, the inner product is $\phi_1^0\phi_2^1\phi_3^0\phi_4^0 = \phi_2$. Every non-matching side contributes a factor of 1. Taking logs and swapping the order of the two sums:
$$ \ell(\boldsymbol\phi) = \sum_{i=1}^N\sum_{k=1}^K \mathbb 1(y_k^{(i)} = 1)\log\phi_k = \sum_{k=1}^K\Bigl[\sum_{i=1}^N\mathbb 1(y_k^{(i)} = 1)\Bigr]\log\phi_k = \sum_{k=1}^K N_k\log\phi_k, $$where $N_k$ is how many times side $k$ came up.
The notes stop here: "we're going to hold off… as it involves one more trick related to the fact that $\sum_k\phi_k = 1$". Here's that trick.
Why the usual move fails. Set $\partial\ell/\partial\phi_k = N_k/\phi_k = 0$? That has no solution. $\ell$ just keeps rising as every $\phi_k$ grows. Without the constraint, "make every side more likely" always helps. But the $\phi_k$ must sum to 1: raising one forces another down.
Lagrange multiplier. To maximise $\ell$ subject to $\sum_k\phi_k = 1$, maximise $\ell(\boldsymbol\phi) - \lambda\bigl(\sum_k\phi_k - 1\bigr)$ over $\boldsymbol\phi$, where $\lambda$ is a new unknown:
$$ \frac{\partial}{\partial\phi_k}: \quad \frac{N_k}{\phi_k} - \lambda = 0 \quad\Longrightarrow\quad \phi_k = \frac{N_k}{\lambda}. $$Pin down $\lambda$ using the constraint: $\sum_k\phi_k = \sum_k N_k/\lambda = N/\lambda = 1$, so $\lambda = N$. Therefore
$$ \hat\phi_{k,\text{MLE}} = \frac{N_k}{N}. $$Intuition for $\lambda$: at the optimum, the marginal gain from moving a little probability onto side $k$, which is $N_k/\phi_k$, must be the same for every side. If it weren't, you'd shift probability from the side with the smaller gain to the one with the larger gain and do better. $\lambda$ is that common value.
No-Lagrange check: with $K = 2$, substitute $\phi_2 = 1 - \phi_1$. You get $N_1\log\phi_1 + N_2\log(1-\phi_1)$, which is exactly the coin from §6, with answer $N_1/N$. ✓
$\D = \{\text{green}, \text{magenta}, \text{magenta}, \text{red}, \text{green}\}$. Find $\hat{\boldsymbol\phi}_{\text{MLE}}$ and its likelihood.
Step 1 — Counts
Red 1, green 2, blue 0, magenta 2; $N = 5$.
Step 2 — Estimates and likelihood
$\hat{\boldsymbol\phi} = [\tfrac15, \tfrac25, 0, \tfrac25]$. Likelihood $\phi_1\phi_2^2\phi_4^2 = 0.2\cdot0.4^2\cdot0.4^2 = 0.00512$, about 5× the fair die's $0.000977$.
Answer & the takeaway
Look at $\hat\phi_3 = 0$. Blue never appeared, so MLE says blue is impossible. Roll blue once at test time and the model assigns it probability 0 (NLL $= \infty$). With five rolls that's clearly overconfident. It's the categorical version of the one-flip problem, and the usual fix is to add a small pseudo-count to every side (smoothing). going deeper
8. MLE for a Gaussian: the mean, and the variance
Data $x^{(1)},\dots,x^{(N)}$, model $\mathcal N(\mu,\sigma^2)$. Log of each density:
$$ \log\Bigl[\frac{1}{\sqrt{2\pi\sigma^2}}e^{-\frac{(x^{(i)}-\mu)^2}{2\sigma^2}}\Bigr] = -\log\sqrt{2\pi} - \log\sigma - \frac{(x^{(i)}-\mu)^2}{2\sigma^2}, $$so summing over $i$,
$$ \ell(\mu,\sigma) = -N\log\sqrt{2\pi} - N\log\sigma - \sum_{i=1}^N\frac{(x^{(i)}-\mu)^2}{2\sigma^2}. $$The log has removed the exponential, as promised.
Only the last term involves $\mu$:
$$ \frac{\partial\ell}{\partial\mu} = \sum_{i=1}^N\frac{2(x^{(i)}-\mu)}{2\sigma^2} = \frac{1}{\sigma^2}\sum_{i=1}^N(x^{(i)} - \mu) = 0 \ \Longrightarrow\ \sum_i x^{(i)} = N\mu \ \Longrightarrow\ \hat\mu = \frac1N\sum_{i=1}^N x^{(i)}. $$The sample average, as you'd expect. Notice that $\sigma$ dropped out, so $\hat\mu$ is the same whatever the variance is.
Differentiate with respect to $\sigma$. The first term is constant, $\frac{d}{d\sigma}(-N\log\sigma) = -\frac N\sigma$, and $\frac{d}{d\sigma}\frac{1}{2\sigma^2} = -\frac{1}{\sigma^3}$:
$$ \frac{\partial\ell}{\partial\sigma} = -\frac{N}{\sigma} + \frac{1}{\sigma^3}\sum_{i=1}^N(x^{(i)}-\mu)^2 = 0 \ \Longrightarrow\ \hat\sigma^2 = \frac1N\sum_{i=1}^N\bigl(x^{(i)} - \hat\mu\bigr)^2. $$The average squared distance from the mean. The two terms pull in opposite directions. $-N\log\sigma$ rewards a narrow curve (tall peak, high density). The squared-error term punishes a curve too narrow to reach the data. The MLE balances them.
going deeper $\hat\sigma^2$ divides by $N$. On average it slightly underestimates the true variance, because $\hat\mu$ was fitted to the same data and sits closer to it than the true $\mu$ does. The "sample variance" in statistics divides by $N-1$ to correct this (108.33 instead of 72.22 for the grades). MLE gives the $N$ version.
9. The payoff: logistic regression is MLE
So far we've modelled one variable on its own. For prediction we want the distribution of the label given the input, $p(y\mid\mathbf x,\theta)$. The recipe: collect i.i.d. pairs $(\mathbf x^{(i)}, y^{(i)})$, choose a form for $p(y\mid\mathbf x,\theta)$, and maximise
$$ L(\theta) = p\bigl(y^{(1)},\dots,y^{(N)}\mid\mathbf x^{(1)},\dots,\mathbf x^{(N)},\theta\bigr) = \prod_{i=1}^N p\bigl(y^{(i)}\mid\mathbf x^{(i)},\theta\bigr). $$This is the conditional likelihood: we condition on the inputs and only model the labels. Maximising it is strictly "maximum conditional likelihood estimation". In ML it's used so often that people drop the "conditional" and call it the likelihood and MLE. Some even write it $p(\D\mid\theta)$, which is confusing because $\D$ contains the $\mathbf x$'s on the right of the bar. Read it as conditional.
Binary
Logistic regression says $p(y = 1\mid\mathbf x) = \sigma(\theta^\top\mathbf x) = \frac{1}{1+e^{-\theta^\top\mathbf x}}$. Write $\phi^{(i)} = \sigma(\theta^\top\mathbf x^{(i)})$. Each label is now a coin flip whose bias depends on its input. So the likelihood is the Bernoulli one from §6, with a different $\phi$ per point:
$$ L(\theta) = \prod_{i=1}^N\bigl(\phi^{(i)}\bigr)^{\mathbb 1(y^{(i)}=1)}\bigl(1-\phi^{(i)}\bigr)^{\mathbb 1(y^{(i)}=0)}, \qquad -\ell(\theta) = -\sum_{i=1}^N\Bigl[\mathbb 1(y^{(i)}=1)\log\phi^{(i)} + \mathbb 1(y^{(i)}=0)\log(1-\phi^{(i)})\Bigr]. $$Now make three substitutions, each true by definition: $\mathbb 1(y^{(i)}=1) = y^{(i)}$ (since $y\in\{0,1\}$), $\mathbb 1(y^{(i)}=0) = 1 - y^{(i)}$, and $\phi^{(i)} = \hat y^{(i)}$:
$$ \hat\theta_{\text{MLE}} = \argmin_\theta\ -\sum_{i=1}^N\Bigl[y^{(i)}\log\hat y^{(i)} + \bigl(1-y^{(i)}\bigr)\log\bigl(1-\hat y^{(i)}\bigr)\Bigr]. $$That's exactly the binary cross-entropy objective from chapter 11, up to the constant factor $\frac1N$, which doesn't move the argmin. Cross-entropy was not an arbitrary choice. It is the NLL of a Bernoulli model.
Multi-class
With softmax, $\phi_k^{(i)} = \frac{\exp(\theta_k^\top\mathbf x^{(i)})}{\sum_{l=1}^K\exp(\theta_l^\top\mathbf x^{(i)})}$: each label is a die roll whose weights depend on the input. That gives the categorical likelihood from §7:
$$ -\ell(\Theta) = -\sum_{i=1}^N\sum_{k=1}^K\mathbb 1(y_k^{(i)}=1)\log\phi_k^{(i)} = -\sum_{i=1}^N\sum_{k=1}^K y_k^{(i)}\log\hat y_k^{(i)}, $$which is the multi-class cross-entropy, again.
And linear regression? going deeper
Assume $y = \theta^\top\mathbf x + \varepsilon$ with noise $\varepsilon\sim\mathcal N(0,\sigma^2)$, so $p(y\mid\mathbf x) = \mathcal N(\theta^\top\mathbf x,\sigma^2)$. Then
$$ -\log p\bigl(y^{(i)}\mid\mathbf x^{(i)}\bigr) = \frac{\bigl(y^{(i)} - \theta^\top\mathbf x^{(i)}\bigr)^2}{2\sigma^2} + \log\bigl(\sqrt{2\pi}\,\sigma\bigr). $$The second term doesn't involve $\theta$ and $\frac{1}{2\sigma^2}$ is a positive constant, so minimising the NLL over $\theta$ is minimising the sum of squared errors. Squared error is the NLL of Gaussian noise.
10. Recap
- Likelihood $p(\D\mid\theta) = \prod_i p(y^{(i)}\mid\theta)$ under i.i.d. As a function of $\theta$ it's $L(\theta)$. It is not a distribution over $\theta$.
- MLE: $\hat\theta = \argmax_\theta L(\theta) = \argmax_\theta \ell(\theta) = \argmin_\theta -\ell(\theta)$. Logs avoid underflow and simplify the calculus, and they're safe because log is monotonic.
- Recipe: likelihood → NLL → derivative → set to 0 or run gradient descent.
- Indicator-exponent trick: $\phi^{\mathbb 1(y=1)}(1-\phi)^{\mathbb 1(y=0)}$ and $\prod_k\phi_k^{\mathbb 1(y_k=1)}$.
- Results: Bernoulli $\hat\phi = N_H/N$; categorical $\hat\phi_k = N_k/N$ (Lagrange multiplier, $\lambda = N$); Gaussian $\hat\mu = $ mean and $\hat\sigma^2 = \frac1N\sum(x-\hat\mu)^2$.
- Conditional MLE with a Bernoulli/categorical label model is binary/multi-class cross-entropy. With Gaussian noise it's squared error.
- MLE overfits with little data: one heads gives $\hat\phi = 1$, and an unseen class gets probability 0.