# Lecture - Uncertainty in Deep Learning MT25, Uncertainty over functions

> Source: https://ollybritton.com/notes/uni/part-c/mt25/uncertainty-in-deep-learning/lectures/uncertainty-over-functions/ · Updated: 2025-12-28 · Tags: uni, notes

- [Course - Uncertainty in Deep Learning MT25](https://ollybritton.com/notes/uni/part-c/mt25/uncertainty-in-deep-learning/)
	- Previous lecture: [Lecture - Uncertainty in Deep Learning MT25, Bayesian probabilistic modelling of functions](https://ollybritton.com/notes/uni/part-c/mt25/uncertainty-in-deep-learning/lectures/bayesian-probabilistic-modelling-of-functions/)
	- Next lecture: [Lecture - Uncertainty in Deep Learning MT25, Approximate inference](https://ollybritton.com/notes/uni/part-c/mt25/uncertainty-in-deep-learning/lectures/approximate-inference/)

In this lecture, we interpret the variance of the predictive distribution as splitting into the aleatoric uncertainty and the epistemic uncertainty.

### Decomposing predictive uncertainty
<!-- flash-start -->
Suppose:

- We are consider a deep neural network with scalar outputs $y$
- $W \in \mathbb R^{K \times 1}$ is the weight matrix of the last layer
- $b = 0$ for the output layer
- We don't consider the rest of the weights of the network, so that we may write $f^W(\pmb x) = \sum w_k \phi_k(\pmb x) = W^\top \phi(\pmb x)$, where $\phi(\pmb x)$ is the feature map implemented by the previous layers of the network

and consider the following generative story:

- Nature chose $W$ which defines a function $f^W(\pmb x) := W^\top \phi(x)$
- Then nature generated function values with inputs $x_1, \ldots, x_N$ given by $f^W(x_n)$
- These were corrupted with additive Gaussian noise $y_n := f^W(x_n) + \epsilon_n$, $\epsilon_n \sim \mathcal N(0, \sigma^2)$
- We then observe these corrupted values $\{(x_1, y_1), \ldots, (x_N, y_N)\}$

Recall that this means we have:

- **Prior**: $\mathbb P(w_k) = \mathcal N(w_k; 0, s^2)$
- **Likelihood**: $\mathbb P(Y \mid W, X) = \prod_n \mathcal N(y \mid W^\top \phi(x), \sigma^2)$
- **Posterior**: $\mathbb P(W \mid X, Y) = \mathcal N(W \mid \mu', \Sigma')$, where:
	- $\Sigma' = (\sigma^{-2} \phi(X)^\top \phi(X) + s^{-2} I_k)^{-1}$
	- $\mu' = \Sigma' \sigma^{-2} \phi(X)^\top Y$
- **Predictive**: $\mathbb P(y^\ast \mid x^\ast, X, Y) = \mathcal N (y^\ast \mid \mu^\ast, \sigma^\ast)$, where:
	- $\sigma^\ast = \sigma^2 + \phi(x^\ast)^\top \Sigma' \phi(x^\ast)$
	- $\mu^\ast$: $(\mu')^\top \phi(x^\ast)$

How can you interpret the predictive uncertainty $\sigma^\ast$?::

Decompose it into
$$
\underbrace{\sigma^2}_\text{aleatoric uncertainty}
+
\underbrace{\phi(x^\ast) \Sigma' \phi(x^\ast)}_\text{epistemic uncertainty}
$$

^predictive-variance-decomposition
<!-- flash-end -->

<!-- flash-start -->
Suppose:

- We are consider a deep neural network with scalar outputs $y$
- $W \in \mathbb R^{K \times 1}$ is the weight matrix of the last layer
- $b = 0$ for the output layer
- We don't consider the rest of the weights of the network, so that we may write $f^W(\pmb x) = \sum w_k \phi_k(\pmb x) = W^\top \phi(\pmb x)$, where $\phi(\pmb x)$ is the feature map implemented by the previous layers of the network

and consider the following generative story:

- Nature chose $W$ which defines a function $f^W(\pmb x) := W^\top \phi(x)$
- Then nature generated function values with inputs $x_1, \ldots, x_N$ given by $f^W(x_n)$
- These were corrupted with additive Gaussian noise $y_n := f^W(x_n) + \epsilon_n$, $\epsilon_n \sim \mathcal N(0, \sigma^2)$
- We then observe these corrupted values $\{(x_1, y_1), \ldots, (x_N, y_N)\}$

Recall that this means we have:

- **Prior**: $\mathbb P(w_k) = \mathcal N(w_k; 0, s^2)$
- **Likelihood**: $\mathbb P(Y \mid W, X) = \prod_n \mathcal N(y \mid W^\top \phi(x), \sigma^2)$
- **Posterior**: $\mathbb P(W \mid X, Y) = \mathcal N(W \mid \mu', \Sigma')$, where:
	- $\Sigma' = (\sigma^{-2} \phi(X)^\top \phi(X) + s^{-2} I_k)^{-1}$
	- $\mu' = \Sigma' \sigma^{-2} \phi(X)^\top Y$
- **Predictive**: $\mathbb P(y^\ast \mid x^\ast, X, Y) = \mathcal N (y^\ast \mid \mu^\ast, \sigma^\ast)$, where:
	- $\sigma^\ast = \sigma^2 + \phi(x^\ast)^\top \Sigma' \phi(x^\ast)$
	- $\mu^\ast$: $(\mu')^\top \phi(x^\ast)$

Recall that you can decompose the predictive variance into two terms
$$
\underbrace{\sigma^2}_\text{aleatoric uncertainty}
+
\underbrace{\phi(x^\ast) \Sigma' \phi(x^\ast)}_\text{epistemic uncertainty}
$$
How could you re-frame the generative story for this data to make this distinction between the aleatoric uncertainty and the epistemic uncertainty more clear?

@Prove that you can recover the aleatoric uncertainty and epistemic uncertainty separately under this new generative story by taking the variance over different conditions.::

The initial generative story looks like this:

![graphviz(2).png](https://ollybritton.com/assets/attachments/img/graphviz(2).png)

If we insert a new intermediate latent variable $f_n$ which represents $W^\top \phi(x_n)$ before it is corrupted with noise, we have:

![graphviz(3).png](https://ollybritton.com/assets/attachments/img/graphviz(3).png)

Mathematically, we can represent this with a Dirac delta distribution:
$$
\begin{aligned}
&f_n \mid x_n, W \sim \delta(f_n = W^\top \phi(x_n)) \\
&y_n \mid f_n \sim \mathcal N(y_n \mid f_n, \sigma^2)
\end{aligned}
$$
The overall joint distribution over $W, x_n, y_n$ is the same, but now we have an extra variable that we can condition over. The interesting result is as follows:
$$
\begin{aligned}
&\text{Var}_{\mathbb P(y^\ast \mid f^\ast, X, Y)}[y^\ast] = \sigma^2 \\
&\text{Var}_{\mathbb P(f^\ast \mid x^\ast, X, Y)} [f^\ast] = \phi(x^\ast)^\top \Sigma' \phi(x^\ast)
\end{aligned}
$$
This demonstrates that the epistemic uncertainty term really is the epistemic uncertainty in what we believe the model parameters are, and the aleatoric uncertainty really is the inherent noise in the corrupted data.

**To see $\text{Var}_{\mathbb P(y^\ast \mid f^\ast, X, Y)}[y^\ast] = \sigma^2$**, note that by the assumptions $y^\ast$ conditioned on $f^\ast$ is now independent of $X$ and $Y$. Therefore $\mathbb P(y^\ast \mid f^\ast, X, Y) = \mathbb P(y^\ast \mid f^\ast)$, and so since $y_n \mid f_n \sim \mathcal N(y_n \mid f_n, \sigma^2)$, it follows that
$$
\text{Var}_{\mathbb P(y^\ast \mid f^\ast, X, Y)} (y^\ast) = \text{Var}_{\mathbb P(y^\ast \mid f^\ast)}(y^\ast) = \sigma^2 \\
$$

Seeing the other inequality is a bit more involved:

![Pasted image 20251030215327.png](https://ollybritton.com/assets/attachments/img/Pasted image 20251030215327.png)
![Pasted image 20251030215339.png](https://ollybritton.com/assets/attachments/img/Pasted image 20251030215339.png)
![Pasted image 20251030215400.png](https://ollybritton.com/assets/attachments/img/Pasted image 20251030215400.png)

(there is a minor mistake somewhere here involving transposes...)

^predictive-variance-decomposition-proof
<!-- flash-end -->

#### How well does the epistemic uncertainty term really capture epistemic uncertainty?
<!-- flash-start -->
Suppose:

- We are consider a deep neural network with scalar outputs $y$
- $W \in \mathbb R^{K \times 1}$ is the weight matrix of the last layer
- $b = 0$ for the output layer
- We don't consider the rest of the weights of the network, so that we may write $f^W(\pmb x) = \sum w_k \phi_k(\pmb x) = W^\top \phi(\pmb x)$, where $\phi(\pmb x)$ is the feature map implemented by the previous layers of the network

and consider the following generative story:

- Nature chose $W$ which defines a function $f^W(\pmb x)$ := W^\top \phi(x)$
- Then nature generated function values with inputs $x_1, \ldots, x_N$ given by $f^W(x_n)$
- These were corrupted with additive Gaussian noise $y_n := f^W(x_n) + \epsilon_n$, $\epsilon_n \sim \mathcal N(0, \sigma^2)$
- We then observe these corrupted values $\{(x_1, y_1), \ldots, (x_N, y_N)\}$

Recall that this means we have:

- **Prior**: $\mathbb P(w_k) = \mathcal N(w_k; 0, s^2)$
- **Likelihood**: $\mathbb P(Y \mid W, X) = \prod_n \mathcal N(y \mid W^\top \phi(x), \sigma^2)$
- **Posterior**: $\mathbb P(W \mid X, Y) = \mathcal N(W \mid \mu', \Sigma')$, where:
	- $\Sigma' = (\sigma^{-2} \phi(X)^\top \phi(X) + s^{-2} I_k)^{-1}$
	- $\mu' = \Sigma' \sigma^{-2} \phi(X)^\top Y$
- **Predictive**: $\mathbb P(y^\ast \mid x^\ast, X, Y) = \mathcal N (y^\ast \mid \mu^\ast, \sigma^\ast)$, where:
	- $\sigma^\ast = \sigma^2 + \phi(x^\ast)^\top \Sigma' \phi(x^\ast)$
	- $\mu^\ast$: $(\mu')^\top \phi(x^\ast)$

We may decompose the predictive variance into two components:
$$
\underbrace{\sigma^2}_\text{aleatoric uncertainty}
+
\underbrace{\phi(x^\ast) \Sigma' \phi(x^\ast)}_\text{epistemic uncertainty}
$$
Let
$$
\mathcal U(x^\ast) := \phi(x^\ast)^\top \Sigma' \phi(x^\ast)
$$
@State some (informal, but possible to formalise) results about the behaviour of $\mathcal U(x^\ast)$.::

- $U(x^\ast) \gg 0$ when $x^\ast$ is dissimilar to the training data
- $U(x^\ast) \approx 0$ when $x^\ast$ is similar to training data

^epistemic-uncertainty-behaviour
<!-- flash-end -->

...the lecturer plans to return the proof if there is time at the end of the course, since it's quite long and quite involved...

---
Olly Britton — https://ollybritton.com. Machine-readable index: https://ollybritton.com/llms.txt
