Marginals and Conditionals of Gaussians
Split a Gaussian random vector into two parts, and . Two facts make Gaussians the workhorse of probabilistic ML: the marginal of is Gaussian, and the conditional of given an observed is Gaussian too, with closed-form mean and covariance. Gaussian processes, Kalman filters and Bayesian linear regression are all this one formula.
The setup
Marginal: just read off the blocks
Marginalizing a Gaussian means deleting the rows and columns of the other variables. No integral needed.
Conditional: update with the observation
Read it as a recipe: the mean moves in proportion to how surprising was (), scaled by how strongly and co-vary. The covariance always shrinks (we learned something), and it does not depend on the observed value. In one dimension with correlation , the conditional variance is .
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,
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.
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 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
with . What is the distribution of ?
Answer
.
Solution
Keep rows and columns 1 and 3. They are uncorrelated, so (being jointly Gaussian) and are independent, even though both are correlated with .
Exercise 2: Conditional in 1D
and are standard normal with correlation . Find .
Answer
.
Solution
Mean ; variance . Note the shrinkage towards the mean: ("regression to the mean").
Exercise 3: Fusion (code)
Fuse two measurements of the same quantity: and . Then fuse with .
Answer
and .
Solution
Equal variances give the plain average; the noisy second sensor barely moves the estimate.
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…
- integrating numerically
- taking the corresponding blocks of the mean and covariance
- inverting the covariance
- it is not Gaussian
The conditional covariance depends on the observed value …
- linearly
- quadratically
- not at all
- only through its sign
When fusing two Gaussian estimates, the result's variance is…
- larger than both
- between the two
- smaller than both
- their sum