TExcelTAD
Home › Math for Machine Learning › Marginals and Conditionals of Gaussians

Marginals and Conditionals of Gaussians

Split a Gaussian random vector into two parts, x\mathbf{x} and y\mathbf{y}. Two facts make Gaussians the workhorse of probabilistic ML: the marginal of x\mathbf{x} is Gaussian, and the conditional of x\mathbf{x} given an observed y\mathbf{y} is Gaussian too, with closed-form mean and covariance. Gaussian processes, Kalman filters and Bayesian linear regression are all this one formula.

The setup

[xy]∼N ⁣([μxμy],[ΣxxΣxyΣyxΣyy])\begin{bmatrix} \mathbf{x} \\ \mathbf{y} \end{bmatrix} \sim \mathcal{N}\!\left(\begin{bmatrix} \boldsymbol{\mu}_x \\ \boldsymbol{\mu}_y \end{bmatrix}, \begin{bmatrix} \Sigma_{xx} & \Sigma_{xy} \\ \Sigma_{yx} & \Sigma_{yy} \end{bmatrix}\right)

Marginal: just read off the blocks

p(x)=N(x∣μx,Σxx)p(\mathbf{x}) = \mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}_x, \Sigma_{xx})

Marginalizing a Gaussian means deleting the rows and columns of the other variables. No integral needed.

Conditional: update with the observation

p(x∣y)=N(μx∣y,Σx∣y)μx∣y=μx+ΣxyΣyy−1(y−μy)Σx∣y=Σxx−ΣxyΣyy−1Σyx\begin{aligned} p(\mathbf{x} \mid \mathbf{y}) &= \mathcal{N}(\boldsymbol{\mu}_{x \mid y}, \Sigma_{x \mid y}) \\ \boldsymbol{\mu}_{x \mid y} &= \boldsymbol{\mu}_x + \Sigma_{xy}\Sigma_{yy}^{-1}(\mathbf{y} - \boldsymbol{\mu}_y) \\ \Sigma_{x \mid y} &= \Sigma_{xx} - \Sigma_{xy}\Sigma_{yy}^{-1}\Sigma_{yx} \end{aligned}

Read it as a recipe: the mean moves in proportion to how surprising y\mathbf{y} was (y−μy\mathbf{y} - \boldsymbol{\mu}_y), scaled by how strongly x\mathbf{x} and y\mathbf{y} co-vary. The covariance always shrinks (we learned something), and it does not depend on the observed value. In one dimension with correlation ρ\rho, the conditional variance is σx2(1−ρ2)\sigma_x^2(1 - \rho^2).

python · try it yourself
import numpy as np

mu = np.array([170.0, 70.0])                      # height, weight
S = np.array([[64.0, 40.0], [40.0, 100.0]])
y_obs = 85.0                                      # we observe the weight
mu_c = mu[0] + S[0, 1] / S[1, 1] * (y_obs - mu[1])
var_c = S[0, 0] - S[0, 1]**2 / S[1, 1]
print(f"p(height | weight=85): mean {mu_c:.2f}, std {np.sqrt(var_c):.3f}  (prior std {np.sqrt(S[0,0]):.1f})")
# check by brute force: sample, keep weights near 85
rng = np.random.default_rng(0)
Z = rng.multivariate_normal(mu, S, size=2_000_000)
near = Z[np.abs(Z[:, 1] - y_obs) < 0.5, 0]
print(f"simulated:              mean {near.mean():.2f}, std {near.std():.3f}  from {len(near)} samples")
p(height | weight=85): mean 176.00, std 6.928  (prior std 8.0)
simulated:              mean 175.94, std 6.930  from 25681 samples

Product of Gaussian densities

The product of two Gaussian densities in the same variable is an unnormalized Gaussian. In one dimension,

N(x∣a,A) N(x∣b,B)∝N ⁣(x  |  aB+bAA+B, ABA+B)\mathcal{N}(x \mid a, A)\,\mathcal{N}(x \mid b, B) \propto \mathcal{N}\!\left(x \;\middle|\; \frac{aB + bA}{A + B},\ \frac{AB}{A + B}\right)

The new mean is a precision-weighted average: the more certain source (smaller variance) pulls harder. This is how a Kalman filter fuses a prediction with a measurement.

python · try it yourself
import numpy as np

a, A = 10.0, 4.0        # prediction: position 10, variance 4
b, B = 12.0, 1.0        # sensor: position 12, variance 1
m = (a * B + b * A) / (A + B); v = A * B / (A + B)
print(f"fused estimate: mean {m}, variance {v}")
x = np.linspace(0, 20, 200001); dx = x[1] - x[0]
prod = np.exp(-(x - a)**2 / (2 * A)) * np.exp(-(x - b)**2 / (2 * B))
prod /= prod.sum() * dx
print(f"numeric:        mean {round(float((x * prod).sum() * dx), 4)}, variance {round(float(((x - m)**2 * prod).sum() * dx), 4)}")
fused estimate: mean 11.6, variance 0.8
numeric:        mean 11.6, variance 0.8

In software

  • Gaussian process regression predicts with the conditional formula, with Σ\Sigma given by a kernel (The Dual SVM and Kernels introduces kernels).
  • Kalman filters in robotics and tracking alternate a linear-Gaussian prediction with this product-of-Gaussians update.
  • Missing-value imputation under a Gaussian model uses the conditional mean of the missing features given the observed ones.

Exercises

Exercise 1: Marginal

(x,y,z)∼N(0,Σ)(x, y, z) \sim \mathcal{N}(\mathbf{0}, \Sigma) with Σ=[410121013]\Sigma = \begin{bmatrix} 4 & 1 & 0 \\ 1 & 2 & 1 \\ 0 & 1 & 3 \end{bmatrix}. What is the distribution of (x,z)(x, z)?

Answer

N ⁣(0,[4003])\mathcal{N}\!\left(\mathbf{0}, \begin{bmatrix} 4 & 0 \\ 0 & 3 \end{bmatrix}\right).

Solution

Keep rows and columns 1 and 3. They are uncorrelated, so (being jointly Gaussian) xx and zz are independent, even though both are correlated with yy.

Exercise 2: Conditional in 1D

xx and yy are standard normal with correlation 0.80.8. Find p(x∣y=2)p(x \mid y = 2).

Answer

N(1.6,0.36)\mathcal{N}(1.6, 0.36).

Solution

Mean 0+0.8⋅1−1⋅(2−0)=1.60 + 0.8 \cdot 1^{-1} \cdot (2 - 0) = 1.6; variance 1−0.82=0.361 - 0.8^2 = 0.36. Note the shrinkage towards the mean: 1.6<21.6 < 2 ("regression to the mean").

Exercise 3: Fusion (code)

Fuse two measurements of the same quantity: N(5,1)\mathcal{N}(5, 1) and N(7,1)\mathcal{N}(7, 1). Then fuse N(5,1)\mathcal{N}(5, 1) with N(7,9)\mathcal{N}(7, 9).

Answer

N(6,0.5)\mathcal{N}(6, 0.5) and N(5.2,0.9)\mathcal{N}(5.2, 0.9).

Solution

Equal variances give the plain average; the noisy second sensor barely moves the estimate.

python · try it yourself
def fuse(a, A, b, B): return (a * B + b * A) / (A + B), A * B / (A + B)
print(fuse(5, 1, 7, 1), fuse(5, 1, 7, 9))
(6.0, 0.5) (5.2, 0.9)

Test yourself

The marginal of a joint Gaussian is obtained by…

  1. integrating numerically
  2. taking the corresponding blocks of the mean and covariance
  3. inverting the covariance
  4. it is not Gaussian

The conditional covariance Σx∣y\Sigma_{x \mid y} depends on the observed value y\mathbf{y}…

  1. linearly
  2. quadratically
  3. not at all
  4. only through its sign

When fusing two Gaussian estimates, the result's variance is…

  1. larger than both
  2. between the two
  3. smaller than both
  4. their sum