---
title: The Gaussian distribution in learning
heading: The multivariate Gaussian and its Mahalanobis distance, conditioning and marginalizing a joint Gaussian, Bayesian inference for a Gaussian's mean where precisions add, and sequential estimation that updates with each new sample
eyebrow: S14 · Lesson 8 · The Gaussian distribution in learning
xp: 50
---

::: hook
The Gaussian is the workhorse of learning, and not only because of the central limit theorem. Conditioning one Gaussian variable on another gives a Gaussian whose mean is a linear function of what was observed: that's linear regression and the Kalman filter of S9 in one line. Combining a Gaussian prior with Gaussian measurements gives a Gaussian posterior whose precision is simply the sum of the precisions. Those two facts carry much of the rest of the course.
:::

::: play
- Bayesian inference for a Gaussian's mean: a prior centered at 0 with standard deviation $\sigma_0$, the likelihood of N measurements averaging 2 (noise σ = 1), and the posterior.
- The posterior sits between the prior and the data, closer to whichever is more precise. Its precision is the prior's plus N times each measurement's.
- With many measurements the prior stops mattering.
:::

```sim plot
view: graph
x: m = -4..5
y: p = 0..2
slider: N = 0..50 step 1 = 4
slider: s = 0.3..4 step 0.1 = 2
plot: exp(-m^2/(2*s^2))/sqrt(2*pi*s^2) | samples: 400 | dashed | color: ch3 | label: prior
plot: exp(-(m - 2*N/(N + 1/s^2))^2*(N + 1/s^2)/2)/sqrt(2*pi/(N + 1/s^2)) | samples: 400 | label: posterior
readout: posterior mean = 2*N/(N + 1/s^2) | digits: 3
readout: posterior standard deviation = 1/sqrt(N + 1/s^2) | digits: 3
```

::: predict
## A prior says a sensor's offset is 0 ± 2 (standard deviation). Four readings with noise standard deviation 1 average 2. What's the posterior mean?

- [ ] 0
- [ ] 1
- [x] About 1.88
- [ ] 2

::: reveal
Precisions add: the prior contributes 1/4 and the four readings 4 × 1 = 4, for a posterior precision of 4.25 (standard deviation 0.485). The posterior mean weights each source by its precision: (0 × 0.25 + 2 × 4)/4.25 = 1.88. The readings dominate, but the prior still pulls slightly toward 0.
:::
:::

::: nameit
## The multivariate Gaussian

$$\mathcal{N}(\mathbf{x}|\boldsymbol{\mu}, \Sigma) = \frac{1}{(2\pi)^{D/2}|\Sigma|^{1/2}}\exp\left(-\tfrac{1}{2}\Delta^2\right), \qquad \Delta^2 = (\mathbf{x} - \boldsymbol{\mu})^T\Sigma^{-1}(\mathbf{x} - \boldsymbol{\mu})$$

Δ is the **Mahalanobis distance**: Euclidean distance after scaling each direction by its spread. For a diagonal Σ it's $\sqrt{\sum(x_i - \mu_i)^2/\sigma_i^2}$.

## Conditioning

For jointly Gaussian x and y with standard deviations $\sigma_x$, $\sigma_y$ and correlation ρ,

$$E[y|x] = \mu_y + \rho\frac{\sigma_y}{\sigma_x}(x - \mu_x), \qquad \text{var}(y|x) = \sigma_y^2(1 - \rho^2)$$

Marginals of a joint Gaussian are Gaussian with the corresponding parts of μ and Σ.

## Inference for the mean

With prior $\mathcal{N}(\mu_0, \sigma_0^2)$ and N observations of mean $\bar{x}$ and noise variance $\sigma^2$, the posterior is Gaussian with

$$\frac{1}{\sigma_N^2} = \frac{1}{\sigma_0^2} + \frac{N}{\sigma^2}, \qquad \mu_N = \sigma_N^2\left(\frac{\mu_0}{\sigma_0^2} + \frac{N\bar{x}}{\sigma^2}\right)$$

## Sequential estimation

The sample mean can be updated one sample at a time: $\mu_N = \mu_{N - 1} + \frac{1}{N}(x_N - \mu_{N - 1})$, a correction proportional to the surprise, with a gain that shrinks as data accumulate.
:::

::: example
## Finish the sensor example: posterior variance and mean.

::: solution
1. **Posterior precision.**
   $$\begin{aligned} 1/\sigma_N^2 &= 1/4 + 4 \\ &= 4.25 \end{aligned}$$
   so $\sigma_N^2$ = 0.235 and $\sigma_N$ = 0.485.
2. **Posterior mean.**
   $$\begin{aligned} \mu_N &= \sigma_N^2\,(0 + 4 \times 2) \\ &= 8/4.25 \\ &= 1.882 \end{aligned}$$
3. **More readings.** With 40 readings instead the mean would be 1.988; the prior's influence fades as 1/N.

**Answer:** the posterior has variance 0.235 (standard deviation 0.485) and mean 1.882.
:::
:::

::: example
## x and y are Gaussian with zero means, $\sigma_x$ = 1, $\sigma_y$ = 2 and ρ = 0.6. Given x = 1, what do you know about y?

::: solution
1. **Conditional mean.**
   $$\begin{aligned} E[y|x] &= 0.6 \times 2/1 \times 1 \\ &= 1.2 \end{aligned}$$
2. **Conditional variance.**
   $$\begin{aligned} \text{var}(y|x) &= 4(1 - 0.36) \\ &= 2.56 \end{aligned}$$
   a standard deviation of 1.6 instead of 2.
3. **What x told us.** Knowing x removed 36% of y's variance, $\rho^2$.

**Answer:** given x = 1, y has mean 1.2 and standard deviation 1.6 (variance 2.56).
:::
:::

::: example
## A quantity x has prior N(0, 4). A sensor with gain 2 reports $y = 2x + n$, with noise variance 1, and reads y = 3. What's the posterior for x?

::: solution
1. **Posterior precision.** It's the prior's plus the measurement's, scaled by the gain squared:
   $$\frac14 + \frac{2^2}{1} = 4.25$$
   a variance of 0.235.
2. **Posterior mean.** It's the precision-weighted information:
   $$\begin{aligned} \mu &= \frac{2 \times 3/1}{4.25} \\ &= 1.41 \end{aligned}$$
3. **Compare the naive estimate.** The naive estimate y/2 = 1.5 is pulled slightly toward the prior's 0, because the measurement is good but not perfect.
4. **Spread.** The posterior standard deviation, 0.485, is a little better than the measurement alone would give (0.5).

**Answer:** the posterior for x has mean 1.41 and variance 0.235 (standard deviation 0.485).
:::
:::

::: check
given: s = 0.5 | 2 | 5; v = 0.25 | 1 | 4; N = 1 | 5 | 20
answer: 1/(1/s^2 + N/v)
tolerance: 0.5%

## A mean has a prior with standard deviation {s}. {N} observations with noise variance {v} arrive. What's the posterior variance?

::: right
Right. $1/(1/\sigma_0^2 + N/\sigma^2)$ = {answer}.
:::

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

::: check
given: u = 0 | 1; s = 0.5 | 2; v = 0.25 | 1; N = 2 | 10; x = 3 | 5
answer: (u/s^2 + N*x/v)/(1/s^2 + N/v)
tolerance: 0.5%

## Prior N({u}, {s}²), and {N} observations averaging {x} with noise variance {v}. What's the posterior mean?

::: right
Right. The precision-weighted average: {answer}.
:::

::: misses
- [any] Not yet. $\sigma_N^2(\mu_0/\sigma_0^2 + N\bar{x}/\sigma^2)$.
:::
:::

::: check
given: a = 1 | 3; b = 0.5 | 2 | 5; r = -0.5 | 0.3 | 0.8; x = 0.5 | 2
answer: r*b/a*x
tolerance: 0.5%

## x and y are zero-mean jointly Gaussian with $\sigma_x$ = {a}, $\sigma_y$ = {b} and correlation {r}. Given x = {x}, what's E[y|x]?

::: right
Right. $\rho(\sigma_y/\sigma_x)x$ = {answer}.
:::

::: misses
- [r*x] Scale by the ratio of standard deviations.
- [any] Not yet. The regression line of y on x.
:::
:::

::: check
given: b = 0.5 | 2 | 5; r = 0.3 | 0.6 | 0.9
answer: b^2*(1 - r^2)
tolerance: 0.5%

## y has standard deviation {b} and correlation {r} with x. What's var(y|x)?

::: right
Right. $\sigma_y^2(1 - \rho^2)$ = {answer}.
:::

::: misses
- [b^2*(1 - r)] Knowing x removes a fraction $\rho^2$ of the variance.
- [any] Not yet. Conditional variance of a joint Gaussian.
:::
:::

::: check
given: x = 1 | 3; y = -2 | 4; a = 0.5 | 1 | 2; b = 1 | 3
answer: sqrt(x^2/a^2 + y^2/b^2)
tolerance: 0.5%

## A point (x, y) = ({x}, {y}) is compared with a zero-mean Gaussian of diagonal covariance with standard deviations {a} and {b}. What's its Mahalanobis distance?

::: right
Right. $\sqrt{x^2/\sigma_1^2 + y^2/\sigma_2^2}$ = {answer}.
:::

::: misses
- [any] Not yet. Divide each coordinate by its standard deviation.
:::
:::

::: check
given: m = 2 | 5; x = 1 | 9; N = 3 | 10 | 50
answer: m + (x - m)/N
tolerance: 0.5%

## The mean of the first {N} − 1 samples is {m}. Sample number {N} is {x}. What's the new mean?

::: right
Right. $\mu_{N - 1} + (x_N - \mu_{N - 1})/N$ = {answer}.
:::

::: misses
- [(m + x)/2] The new sample gets weight 1/N.
- [any] Not yet. Sequential update.
:::
:::

::: check
kind: parts
given: s = 0.5 | 1 | 2; g = 0.5 | 2 | 3; v = 0.5 | 2; y = 1 | 4

## A quantity has prior N(0, {s}²). A sensor reads $y = \given{g}x + n$ with noise variance {v}, and reports y = {y}.

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

### What is the posterior variance of x?

::: misses
- [1/(1/s^2 + 1/v)] The gain scales the measurement's information by g².
- [any] Precisions add: $1/\sigma_0^2 + g^2/\sigma^2$.
:::
:::

::: part
answer: (g*y/v)/(1/s^2 + g^2/v)
tolerance: 0.5%

### What is its posterior mean?

::: misses
- [y/g] That ignores the prior.
- [any] $(gy/\sigma^2)$ times the posterior variance.
:::
:::
:::

::: check
kind: derivation
vars: a = 0.1..10, b = 0.1..10, x = -5..5, y = -5..5

## Two independent unbiased sensors read x and y with noise variances a and b.

::: step
answer: a*b/(a + b)

### What is the variance of the best combined estimate?
:::

::: step
answer: (x/a + y/b)/(1/a + 1/b)

### What is that estimate?
:::

::: right
Right. Each reading is weighted by its precision, and the combination beats either sensor alone.
:::
:::

::: explain
## Why is it the precisions that add when Gaussian information is combined?

::: model
Combining a Gaussian prior with Gaussian measurements multiplies their densities, and each density is the exponential of a quadratic in the unknown with a curvature equal to its precision, one over its variance. Multiplying exponentials adds the quadratics, so the curvatures, the precisions, add: each independent source of information tightens the estimate by its own precision. The posterior mean is the point where the summed quadratic is lowest, which is the precision-weighted average of the sources. That's why a very precise measurement swamps a vague prior, and why N equally good measurements shrink the variance by a factor of N.
:::
:::

::: practice

::: check
given: s = 0.1 | 10; v = 0.05 | 9; N = 1 | 100
answer: 1/(1/s^2 + N/v)
tolerance: 0.5%

## Posterior variance: σ₀ = {s}, σ² = {v}, N = {N}?

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

::: check
given: b = 0.2 | 8; r = 0.05 | 0.95
answer: b^2*(1 - r^2)
tolerance: 0.5%

## var(y|x): σ_y = {b}, ρ = {r}?

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

::: check
given: x = 0.5 | 5; y = 0.5 | 6; a = 0.2 | 3; b = 0.4 | 4
answer: sqrt(x^2/a^2 + y^2/b^2)
tolerance: 0.5%

## Mahalanobis: ({x}, {y}), σ = ({a}, {b})?

::: misses
- [any] $\sqrt{\sum x_i^2/\sigma_i^2}$.
:::
:::
:::
