---
title: Gaussian processes
heading: A prior placed directly on functions, the squared-exponential covariance and its length scale, the posterior mean and variance after noisy observations, choosing hyperparameters by the marginal likelihood, and the link to kernel ridge and Bayesian regression
eyebrow: S14 · Lesson 34 · Gaussian processes
xp: 50
---

::: hook
Lesson 10 put a Gaussian prior on a model's weights and got error bars on its predictions. A Gaussian process skips the weights and puts the prior directly on the function: any finite set of function values is jointly Gaussian, with a covariance given by a kernel. Conditioning on data then uses nothing but lesson 8's Gaussian conditioning. The result is a flexible regression method that says how sure it is everywhere, and it's the engine behind Bayesian optimization, which tunes everything from circuit parameters to neural network hyperparameters with as few expensive evaluations as possible.
:::

::: play
- A Gaussian process with a squared-exponential kernel (length scale ℓ, prior variance 1) conditioned on two noisy observations, at x = −1 and x = 1.5.
- Near the data the band narrows to about the noise; far away it relaxes to the prior's ±2.
- Short length scales let the function wiggle and forget the data quickly; long ones tie the two points together into one smooth curve.
:::

```sim plot
view: graph
x: x = -5..5
y: f = -3..3
slider: l = 0.2..3 step 0.05 = 1
slider: v = 0.001..1 = 0.05 log
plot: exp(-(x + 1)^2/(2*l^2))*((1 + v) + 0.5*exp(-3.125/l^2))/((1 + v)^2 - exp(-3.125/l^2)^2) + exp(-(x - 1.5)^2/(2*l^2))*(-0.5*(1 + v) - exp(-3.125/l^2))/((1 + v)^2 - exp(-3.125/l^2)^2) | samples: 400 | label: posterior mean
plot: exp(-(x + 1)^2/(2*l^2))*((1 + v) + 0.5*exp(-3.125/l^2))/((1 + v)^2 - exp(-3.125/l^2)^2) + exp(-(x - 1.5)^2/(2*l^2))*(-0.5*(1 + v) - exp(-3.125/l^2))/((1 + v)^2 - exp(-3.125/l^2)^2) + 2*sqrt((1 - ((1 + v)*(exp(-(x + 1)^2/(2*l^2))^2 + exp(-(x - 1.5)^2/(2*l^2))^2) - 2*exp(-3.125/l^2)*exp(-(x + 1)^2/(2*l^2))*exp(-(x - 1.5)^2/(2*l^2)))/((1 + v)^2 - exp(-3.125/l^2)^2))) | samples: 400 | dashed | color: ch3 | label: +2σ
plot: exp(-(x + 1)^2/(2*l^2))*((1 + v) + 0.5*exp(-3.125/l^2))/((1 + v)^2 - exp(-3.125/l^2)^2) + exp(-(x - 1.5)^2/(2*l^2))*(-0.5*(1 + v) - exp(-3.125/l^2))/((1 + v)^2 - exp(-3.125/l^2)^2) - 2*sqrt((1 - ((1 + v)*(exp(-(x + 1)^2/(2*l^2))^2 + exp(-(x - 1.5)^2/(2*l^2))^2) - 2*exp(-3.125/l^2)*exp(-(x + 1)^2/(2*l^2))*exp(-(x - 1.5)^2/(2*l^2)))/((1 + v)^2 - exp(-3.125/l^2)^2))) | samples: 400 | dashed | color: ch3 | label: −2σ
point: -1, 1
point: 1.5, -0.5
readout: correlation of the two inputs = exp(-3.125/l^2) | digits: 4
```

::: predict
## A Gaussian process with prior variance 1, length scale 1 and noise variance 0.1 observes f(0) = 1. What's its posterior mean at x = 1?

- [ ] 1
- [ ] 0.91
- [x] About 0.55
- [ ] 0

::: reveal
The covariance between f(1) and the observation is $e^{-1/2}$ = 0.607, and the observation's variance is 1 + 0.1. Gaussian conditioning gives a mean of 0.607 × 1/1.1 = 0.551. At x = 0 itself the mean is 1/1.1 = 0.909, pulled slightly toward the prior's 0 by the noise.
:::
:::

::: nameit
## The prior

A **Gaussian process** $f \sim \mathcal{GP}(0, k)$ says that for any inputs $x_1, \ldots, x_N$ the values $f(x_n)$ are jointly Gaussian with zero mean and covariance $K_{nm} = k(x_n, x_m)$. The **squared-exponential** kernel

$$k(x, x') = \sigma_f^2\exp\left(-\frac{(x - x')^2}{2\ell^2}\right)$$

gives smooth functions of typical size $\sigma_f$ that vary over a distance ℓ: values a distance d apart have correlation $e^{-d^2/2\ell^2}$, falling to r at $d = \ell\sqrt{-2\ln r}$.

## The posterior

Observations $t_n = f(x_n) + \epsilon_n$ with noise variance $\sigma_n^2$ have covariance $C = K + \sigma_n^2I$. At a new input with covariances $\mathbf{k}_*$ to the data,

$$\mu(x) = \mathbf{k}_*^TC^{-1}\mathbf{t}, \qquad \sigma^2(x) = k(x, x) - \mathbf{k}_*^TC^{-1}\mathbf{k}_*$$

For one observation: $\mu = k(x, x_1)t_1/(\sigma_f^2 + \sigma_n^2)$ and $\sigma^2 = \sigma_f^2 - k(x, x_1)^2/(\sigma_f^2 + \sigma_n^2)$. The mean is exactly kernel ridge regression with $\lambda = \sigma_n^2$.

## Hyperparameters

ℓ, $\sigma_f$ and $\sigma_n$ are chosen by maximizing the **marginal likelihood** of the data,

$$\ln p(\mathbf{t}) = -\tfrac{1}{2}\mathbf{t}^TC^{-1}\mathbf{t} - \tfrac{1}{2}\ln|C| - \tfrac{N}{2}\ln 2\pi$$

whose first term rewards fitting the data and second penalizes a flexible model, an automatic Occam's razor.

## Cost

Training needs a solve with the N × N matrix C, about $N^3/3$ operations by Cholesky factorization, which limits exact Gaussian processes to tens of thousands of points.
:::

::: example
## For the predict question's setup, find the posterior standard deviation at x = 0 and at x = 1.

::: solution
1. **At x = 0.**
   $$\begin{aligned} \sigma^2 &= 1 - 1^2/1.1 \\ &= 0.0909 \end{aligned}$$
   so σ = 0.302, below the noise's 0.316, because it describes f itself; a new noisy reading would add the noise variance back.
2. **At x = 1.**
   $$\begin{aligned} \sigma^2 &= 1 - 0.3679/1.1 \\ &= 0.666 \end{aligned}$$
   so σ = 0.816, most of the way back to the prior's 1.

**Answer:** the posterior standard deviation is 0.302 at x = 0 and 0.816 at x = 1.
:::
:::

::: example
## Compare the log marginal likelihood of the single observation t = 1 under noise variances 0.1 and 1 (with $\sigma_f^2$ = 1).

::: solution
1. **Noise variance 0.1.** With C = 1.1:
   $$\begin{aligned} \ln p &= -1/2.2 - \tfrac12\ln 1.1 \\ &\quad - \tfrac12\ln 2\pi \\ &= -0.4545 - 0.0477 - 0.9189 \\ &= -1.421 \end{aligned}$$
2. **Noise variance 1.** With C = 2:
   $$\begin{aligned} \ln p &= -0.250 - 0.347 - 0.919 \\ &= -1.516 \end{aligned}$$
3. **Compare.** The quieter model explains the data slightly better; with more data the comparison sharpens.

**Answer:** the log marginal likelihood is −1.421 with noise variance 0.1 and −1.516 with noise variance 1, so the quieter model fits slightly better.
:::
:::

::: example
## A Gaussian process with $\sigma_f$ = 1, ℓ = 1 and noise variance 0.01 observes t = 1 at x = 0 and at x = 2. What does it predict at x = 1, halfway between?

::: solution
1. **Kernel values.**
   $$\begin{aligned} k(0, 2) &= e^{-2} \\ &= 0.135 \\ k(1, 0) &= k(1, 2) \\ &= e^{-1/2} \\ &= 0.607 \end{aligned}$$
2. **Mean.** By symmetry the two observations get equal weight $1/(1.01 + 0.135)$, so
   $$\begin{aligned} \mu &= 2 \times 0.607/1.145 \\ &= 1.06 \end{aligned}$$
   a little above both observations: the two together are evidence for a function staying near 1 throughout.
3. **Variance.** Since $0.607^2 = e^{-1}$:
   $$\begin{aligned} \sigma^2 &= 1 - \frac{2e^{-1}}{1.01 + e^{-2}} \\ &= 0.358 \end{aligned}$$
   a standard deviation of 0.60.
4. **Why it's wide.** It's far larger than at the observations because one length scale away the correlation has fallen to 0.6.

**Answer:** at x = 1 it predicts a mean of 1.06 with a standard deviation of 0.60.
:::
:::

::: check
given: s = 0.5 | 2 | 3; l = 0.5 | 1 | 3; d = 0.5 | 2
answer: s^2*exp(-d^2/(2*l^2))
tolerance: 0.5%

## A squared-exponential kernel has $\sigma_f$ = {s} and ℓ = {l}. What's the covariance of two function values {d} apart?

::: right
Right. $\sigma_f^2e^{-d^2/2\ell^2}$ = {answer}.
:::

::: misses
- [exp(-d^2/(2*l^2))] That's the correlation; multiply by $\sigma_f^2$.
- [any] Not yet. Squared-exponential covariance.
:::
:::

::: check
given: t = 0.5 | 2; d = 0.5 | 1.5; l = 1 | 2; n = 0.05 | 0.5
answer: exp(-d^2/(2*l^2))*t/(1 + n)
tolerance: 0.5%

## A Gaussian process with $\sigma_f$ = 1, length scale {l} and noise variance {n} observes {t} at one point. What's its posterior mean {d} away?

::: right
Right. $k(x, x_1)t_1/(\sigma_f^2 + \sigma_n^2)$ = {answer}.
:::

::: misses
- [exp(-d^2/(2*l^2))*t] Divide by the observation's variance, including the noise.
- [any] Not yet. Gaussian conditioning on one value.
:::
:::

::: check
given: d = 0.5 | 1.5; l = 1 | 2; n = 0.05 | 0.5
answer: 1 - exp(-d^2/l^2)/(1 + n)
tolerance: 0.5%

## For that process ($\sigma_f$ = 1, ℓ = {l}, noise variance {n}, one observation), what's the posterior variance {d} away?

::: right
Right. $\sigma_f^2 - k(x, x_1)^2/(\sigma_f^2 + \sigma_n^2)$ = {answer}.
:::

::: misses
- [1 - exp(-d^2/(2*l^2))/(1 + n)] Square the covariance: $k^2 = e^{-d^2/\ell^2}$.
- [any] Not yet. Prior variance minus what the observation explains.
:::
:::

::: check
given: s = 0.5 | 1 | 2; n = 0.1 | 0.5 | 1
answer: s^2*n/(s^2 + n)
tolerance: 0.5%

## A process with prior variance {s}² observes a point with noise variance {n}. What's the posterior variance of f at that same point?

::: right
Right. $\sigma_f^2\sigma_n^2/(\sigma_f^2 + \sigma_n^2)$ = {answer}, below both.
:::

::: misses
- [n] The prior adds information too; the variances combine like resistors in parallel.
- [any] Not yet. Precisions add.
:::
:::

::: check
given: t = 0.5 | 1.5; C = 0.5 | 1.2 | 3
answer: -t^2/(2*C) - ln(C)/2 - ln(2*pi)/2
tolerance: 0.5%

## A single observation {t} has total variance C = {C} under a model. What's its log marginal likelihood?

::: right
Right. $-t^2/2C - \frac{1}{2}\ln C - \frac{1}{2}\ln 2\pi$ = {answer}.
:::

::: misses
- [-t^2/(2*C) - ln(C)/2] Include the normalizing constant, −½ ln 2π.
- [any] Not yet. Log of a Gaussian density.
:::
:::

::: check
given: l = 0.5 | 2 | 10; r = 0.1 | 0.5 | 0.9
answer: l*sqrt(-2*ln(r))
tolerance: 0.5%

## A squared-exponential process has length scale {l}. At what separation does the correlation between function values fall to {r}?

::: right
Right. $\ell\sqrt{-2\ln r}$ = {answer}.
:::

::: misses
- [l*sqrt(-ln(r))] The exponent has a 2 in its denominator.
- [any] Not yet. Solve $e^{-d^2/2\ell^2} = r$.
:::
:::

::: check
kind: parts
given: D = 1 | 2 | 3; l = 0.5 | 1 | 2; n = 0.01 | 0.1; t = 1 | 2

## A Gaussian process ($\sigma_f$ = 1, length scale {l}, noise variance {n}) observes the same value {t} at x = 0 and x = {D}.

::: part
answer: 2*exp(-D^2/(8*l^2))*t/(1 + n + exp(-D^2/(2*l^2)))
tolerance: 0.5%

### What mean does it predict halfway between?

::: misses
- [exp(-D^2/(8*l^2))*t/(1 + n)] Both observations contribute, and they're correlated with each other.
- [any] $2k_*t/(1 + \sigma^2 + k_{12})$.
:::
:::

::: part
answer: sqrt(1 - 2*exp(-D^2/(4*l^2))/(1 + n + exp(-D^2/(2*l^2))))
tolerance: 0.5%

### What standard deviation?

::: misses
- [any] $\sqrt{1 - 2k_*^2/(1 + \sigma^2 + k_{12})}$.
:::
:::
:::

::: check
kind: derivation
vars: l = 0.1..5, n = 0.001..1, t = -3..3, d = 0..5

## A Gaussian process with $\sigma_f$ = 1, length scale ℓ and noise variance σ² has one observation t; predict at distance d from it.

::: step
answer: exp(-d^2/(2*l^2))

### What is the prior correlation between the two points' function values?
:::

::: step
answer: exp(-d^2/(2*l^2))*t/(1 + n)

### What is the predictive mean?
:::

::: step
answer: 1 - exp(-d^2/l^2)/(1 + n)

### What is the predictive variance of the function value?
:::

::: right
Right. Far from the data both return to the prior: mean 0, variance 1.
:::
:::

::: explain
## How does a Gaussian process decide how confident to be at a new input?

::: model
The process treats the unknown function's values at the new input and at the training inputs as jointly Gaussian, with covariances set by the kernel according to how far apart the inputs are. Conditioning on the observed values removes the part of the new value's variance that the observations can explain. If the new input is close to training points, its value is strongly correlated with theirs, so most of the prior variance is explained and the posterior is narrow; if it's far away, the correlations are weak, little variance is removed, and the band relaxes to the prior's. The noise level limits how much any single observation can explain, so even at a training point some uncertainty remains.
:::
:::

::: practice

::: check
given: s = 0.2 | 5; l = 0.1 | 10; d = 0.05 | 8
answer: s^2*exp(-d^2/(2*l^2))
tolerance: 0.5%

## SE covariance: σ_f = {s}, ℓ = {l}, d = {d}?

::: misses
- [any] $\sigma_f^2e^{-d^2/2\ell^2}$.
:::
:::

::: check
given: t = -2 | 3; d = 0.2 | 2; l = 0.5 | 3; n = 0.01 | 1
answer: exp(-d^2/(2*l^2))*t/(1 + n)
tolerance: 0.5%

## Posterior mean: t = {t}, d = {d}, ℓ = {l}, σ_n² = {n}?

::: misses
- [any] $kt/(1 + \sigma_n^2)$.
:::
:::

::: check
given: s = 0.3 | 3; n = 0.001 | 2
answer: s^2*n/(s^2 + n)
tolerance: 0.5%

## Posterior variance at the observed point: σ_f = {s}, σ_n² = {n}?

::: misses
- [any] $\sigma_f^2\sigma_n^2/(\sigma_f^2 + \sigma_n^2)$.
:::
:::
:::
