---
title: Likelihood and Bayes' theorem for learning
heading: The likelihood of a data set, maximum-likelihood estimates of a Gaussian's mean and variance and the bias of the variance, the log-likelihood, Bayes' theorem with priors and posteriors, and how a Gaussian prior on the weights turns least squares into regularized least squares
eyebrow: S14 · Lesson 2 · Likelihood and Bayes' theorem for learning
xp: 50
---

::: hook
Lesson 1 chose parameters by minimizing squared error, which seems arbitrary. Probability gives it a reason: if the targets are the model's prediction plus Gaussian noise, least squares is exactly the choice that makes the observed data most probable, the maximum-likelihood fit. Treating the parameters themselves as uncertain, with a prior, then gives regularization a reason too. A11 built probability; this lesson puts it to work estimating parameters.
:::

::: play
- The likelihood of the mean μ of a Gaussian (known σ = 1) given N observations averaging x̄ = 2, normalized to a peak of 1.
- The maximum sits at the sample mean, and the curve's width, the uncertainty in μ, shrinks as $\sigma/\sqrt{N}$.
- With 100 observations the mean is pinned down ten times more tightly than with one.
:::

```sim plot
view: graph
x: m = -1..5
y: L = 0..1.05
slider: N = 1..100 step 1 = 4
plot: exp(-N*(m - 2)^2/2) | samples: 400 | label: likelihood of the mean
readout: standard error of the mean = 1/sqrt(N) | digits: 3
```

::: predict
## Four measurements read 2, 4, 4 and 6. What's the maximum-likelihood estimate of their Gaussian variance?

- [ ] 2.67
- [x] 2
- [ ] 4
- [ ] 8

::: reveal
The maximum-likelihood mean is the sample mean, 4, and the maximum-likelihood variance is the average squared deviation, (4 + 0 + 0 + 4)/4 = 2. It's biased low: on average it equals $\frac{N-1}{N}\sigma^2$, because the mean was fitted to the same data. Dividing by N − 1 instead gives the unbiased 2.67.
:::
:::

::: nameit
## Likelihood

For independent observations $x_1, \ldots, x_N$ from a density $p(x|\theta)$, the **likelihood** of the parameters is $p(\mathcal{D}|\theta) = \prod_np(x_n|\theta)$; its logarithm is a sum, easier to work with. For a Gaussian,

$$\ln p(\mathcal{D}|\mu, \sigma^2) = -\frac{N}{2}\ln(2\pi\sigma^2) - \frac{1}{2\sigma^2}\sum_n(x_n - \mu)^2$$

## Maximum likelihood

Maximizing gives $\mu_\text{ML} = \frac{1}{N}\sum x_n$ and $\sigma^2_\text{ML} = \frac{1}{N}\sum(x_n - \mu_\text{ML})^2$, whose expectation is $\frac{N - 1}{N}\sigma^2$. If targets are $t = y(x, \mathbf{w}) + \epsilon$ with Gaussian ε, maximizing the likelihood over **w** is minimizing the sum of squared errors.

## Bayes' theorem

$$p(\theta|\mathcal{D}) = \frac{p(\mathcal{D}|\theta)\,p(\theta)}{p(\mathcal{D})}$$

posterior ∝ likelihood × prior. The **maximum a posteriori** (MAP) estimate maximizes the posterior. With Gaussian noise of variance $\sigma^2$ and a Gaussian prior $\mathbf{w} \sim \mathcal{N}(0, \sigma_w^2I)$, MAP is regularized least squares with $\lambda = \sigma^2/\sigma_w^2$.
:::

::: example
## Find the log-likelihood of the data 2, 4, 4, 6 at the maximum-likelihood parameters.

::: solution
1. **Maximum-likelihood parameters.**
   $$\mu = 4, \quad \sigma^2 = 2$$
2. **Log-likelihood.**
   $$\begin{aligned} \ln p &= -2\ln(4\pi) - \frac{8}{2 \times 2} \\ &= -5.062 - 2 \\ &= -7.062 \end{aligned}$$
3. **Compare other parameters.** Any other μ or σ² gives a lower value; with σ² = 8/3 (the unbiased estimate), for example, it's −7.14.

**Answer:** the log-likelihood at the maximum-likelihood parameters is −7.062.
:::
:::

::: example
## A fault detector flags 99% of faulty units and 5% of good ones; 1% of units are faulty. Given a flag, what's the chance the unit is faulty?

::: solution
1. **Probability of a flag.**
   $$\begin{aligned} p(\text{flag}) &= 0.99 \times 0.01 \\ &\quad + 0.05 \times 0.99 \\ &= 0.0594 \end{aligned}$$
2. **Bayes' rule.**
   $$\begin{aligned} p(\text{fault}|\text{flag}) &= \frac{0.99 \times 0.01}{0.0594} \\ &= \frac{0.0099}{0.0594} \\ &= 0.167 \end{aligned}$$
3. **Why it's so low.** Most flags are false alarms because good units vastly outnumber faulty ones: the prior matters as much as the detector.

**Answer:** given a flag, the unit is faulty with probability 0.167.
:::
:::

::: example
## Five units run a life test for 1000 hours. Three fail, at 200, 500 and 800 hours; two are still running at the end. Assuming a constant failure rate, what's the maximum-likelihood rate and mean time to failure?

::: solution
1. **Each unit's share of the likelihood.** A unit that fails at time t contributes $\lambda e^{-\lambda t}$; one still running at time T contributes the probability of surviving, $e^{-\lambda T}$.
2. **Log-likelihood.**
   $$\ln L = 3\ln\lambda - \lambda\sum t_i$$
   where the sum is the total time on test:
   $$\begin{aligned} \sum t_i &= 200 + 500 + 800 \\ &\quad + 1000 + 1000 \\ &= 3500\ \text{hours} \end{aligned}$$
3. **Maximize.** Setting the derivative to zero gives
   $$\begin{aligned} \lambda &= 3/3500 \\ &= 8.57 \times 10^{-4}\ \text{per hour} \end{aligned}$$
   a mean time to failure of 1167 hours.
4. **Ignoring the survivors.** That would give 500 hours, badly pessimistic: censored data still carry information.

**Answer:** the maximum-likelihood rate is $8.57 \times 10^{-4}$ per hour, a mean time to failure of 1167 hours.
:::
:::

::: check
given: a = 1 | 3; b = 2 | 6; c = 5 | 9; d = 4 | 10
answer: ((a - (a + b + c + d)/4)^2 + (b - (a + b + c + d)/4)^2 + (c - (a + b + c + d)/4)^2 + (d - (a + b + c + d)/4)^2)/4
tolerance: 0.5%

## Four measurements read {a}, {b}, {c} and {d}. What's the maximum-likelihood estimate of the Gaussian variance?

::: right
Right. The average squared deviation from the mean: {answer}.
:::

::: misses
- [((a - (a + b + c + d)/4)^2 + (b - (a + b + c + d)/4)^2 + (c - (a + b + c + d)/4)^2 + (d - (a + b + c + d)/4)^2)/3] That's the unbiased estimate; maximum likelihood divides by N.
- [any] Not yet. $\frac{1}{N}\sum(x_n - \bar{x})^2$.
:::
:::

::: check
given: v = 1.5 | 4 | 20; N = 3 | 10 | 50
answer: v*N/(N - 1)
tolerance: 0.5%

## The maximum-likelihood variance of {N} samples is {v}. What's the unbiased estimate?

::: right
Right. $\sigma^2_\text{ML}N/(N - 1)$ = {answer}.
:::

::: misses
- [v*(N - 1)/N] Correct upward: the ML estimate is low by $(N - 1)/N$.
- [any] Not yet. The bias factor is $(N - 1)/N$.
:::
:::

::: check
given: p = 0.001 | 0.01 | 0.1; s = 0.9 | 0.99; f = 0.01 | 0.05
answer: s*p/(s*p + f*(1 - p))
tolerance: 0.5%

## A test detects a condition with probability {s} and falsely flags {f} of negatives. The condition's prevalence is {p}. Given a positive, what's the probability of the condition?

::: right
Right. $sp/(sp + f(1 - p))$ = {answer}.
:::

::: misses
- [s] That's the detection rate, the probability of a positive given the condition, the other way round.
- [any] Not yet. Bayes' theorem.
:::
:::

::: check
given: N = 4 | 10; v = 1 | 2.5; S = 4 | 20
answer: -N/2*ln(2*pi*v) - S/(2*v)
tolerance: 0.5%

## {N} Gaussian observations have squared deviations from μ summing to {S}. With σ² = {v}, what's the log-likelihood?

::: right
Right. $-\frac{N}{2}\ln(2\pi\sigma^2) - S/(2\sigma^2)$ = {answer}.
:::

::: misses
- [any] Not yet. Sum the log densities.
:::
:::

::: check
given: s = 0.01 | 0.1 | 1; w = 0.5 | 2 | 10
answer: s/w^2
tolerance: 0.5%

## Targets have Gaussian noise of variance {s}, and the weights a Gaussian prior with standard deviation {w}. What regularization constant does MAP estimation correspond to?

::: right
Right. $\lambda = \sigma^2/\sigma_w^2$ = {answer}.
:::

::: misses
- [s/w] Use the prior's variance, $\sigma_w^2$.
- [any] Not yet. Compare the log-posterior with the regularized error.
:::
:::

::: check
given: s = 1 | 4 | 9; N = 2 | 5 | 20
answer: s*(N - 1)/N
tolerance: 0.5%

## Samples of size {N} come from a Gaussian of variance {s}. What's the expected value of the maximum-likelihood variance estimate?

::: right
Right. $\frac{N - 1}{N}\sigma^2$ = {answer}.
:::

::: misses
- [s] It's biased: fitting the mean to the same data removes one degree of freedom.
- [any] Not yet. $E[\sigma^2_\text{ML}] = \frac{N - 1}{N}\sigma^2$.
:::
:::

::: check
kind: parts
given: a = 2 | 3 | 7; b = 1 | 4 | 6; c = 0 | 5; d = 3 | 8

## A photon counter records {a}, {b}, {c} and {d} counts in four equal intervals, assumed Poisson.

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

### What is the maximum-likelihood count rate per interval?

::: misses
- [any] The Poisson rate's ML estimate is the sample mean.
:::
:::

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

### At that rate, what's the probability of an interval with no counts?

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

::: check
kind: derivation
vars: F = 1..20, T = 100..100000, t = 1..10000

## A life test sees F failures in a total time on test T, with a constant failure rate assumed.

::: step
answer: F/T

### What is the maximum-likelihood failure rate?
:::

::: step
answer: T/F

### What mean time to failure does it imply?
:::

::: step
answer: exp(-F*t/T)

### What probability does it give that a unit survives a time t?
:::

::: right
Right. Survivors count through their time on test, which is how censored data enter the likelihood.
:::
:::

::: explain
## Why does minimizing squared error amount to maximum likelihood?

::: model
Suppose each target is the model's output plus independent Gaussian noise of fixed variance. The probability density of observing a target t given the model's prediction y is proportional to $e^{-(t - y)^2/2\sigma^2}$, so the likelihood of the whole data set is the product of these, and its logarithm is a constant minus the sum of squared errors divided by $2\sigma^2$. The parameters that maximize the log-likelihood are therefore exactly those that minimize the sum of squared errors. The connection also says when least squares is the wrong choice: if the noise has heavy tails or depends on the input, a different likelihood, and a different error function, fits better.
:::
:::

::: practice

::: check
given: v = 0.5 | 30; N = 2 | 100
answer: v*N/(N - 1)
tolerance: 0.5%

## Unbiased variance from σ²_ML = {v}, N = {N}?

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

::: check
given: p = 0.0005 | 0.2; s = 0.8 | 0.999; f = 0.001 | 0.1
answer: s*p/(s*p + f*(1 - p))
tolerance: 0.5%

## Posterior: prevalence {p}, detection {s}, false alarms {f}?

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

::: check
given: s = 0.001 | 5; w = 0.1 | 20
answer: s/w^2
tolerance: 0.5%

## MAP λ: noise variance {s}, prior std {w}?

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