The Gaussian Distribution
The Gaussian (normal) distribution is the default model for continuous noise, the prior of choice for weights, the building block of mixture models and the result of averaging many small effects (the central limit theorem). It is also algebraically convenient: sums, linear maps, marginals and conditionals of Gaussians are Gaussian again.
Univariate and multivariate densities
The exponent is times the squared Mahalanobis distance. Level sets are ellipsoids whose axes are the eigenvectors of , with semi-axis lengths proportional to .
The log-density in code
Never evaluate the density and then take a log: for high dimensions the density under- or overflows. Compute the log-density directly, with a Cholesky factor :
import numpy as np
def gauss_logpdf(x, mu, Sigma):
D = len(mu)
L = np.linalg.cholesky(Sigma)
z = np.linalg.solve(L, x - mu) # L z = x - mu
return -0.5 * D * np.log(2 * np.pi) - np.log(np.diag(L)).sum() - 0.5 * z @ z
mu = np.array([1.0, -1.0]); Sigma = np.array([[2.0, 0.8], [0.8, 1.0]])
x = np.array([1.5, 0.0])
direct = np.exp(-0.5 * (x - mu) @ np.linalg.inv(Sigma) @ (x - mu)) / np.sqrt((2 * np.pi)**2 * np.linalg.det(Sigma))
print("log-density:", round(gauss_logpdf(x, mu, Sigma), 6), " log of direct formula:", round(np.log(direct), 6))
D = 500
print("D = 500, standard normal at 0: density", np.exp(gauss_logpdf(np.zeros(D), np.zeros(D), np.eye(D))),
" log-density", round(gauss_logpdf(np.zeros(D), np.zeros(D), np.eye(D)), 2))log-density: -2.524708 log of direct formula: -2.524708
D = 500, standard normal at 0: density 2.8512343234609535e-200 log-density -459.47
In 500 dimensions the peak density is already about , near the float64 underflow limit of about (a few hundred more dimensions and it rounds to exactly 0), while the log-density is a perfectly ordinary number.
Closed under linear maps and sums
If then
and independent Gaussians add: . Sampling uses the first fact: transform standard normal draws with .
The central limit theorem
The mean of independent variables with finite variance is approximately Gaussian for large , whatever their own distribution. That is one justification for Gaussian noise models: measurement error is often a sum of many small effects.
import numpy as np
rng = np.random.default_rng(0)
N = 30
means = rng.exponential(1.0, size=(100000, N)).mean(axis=1) # very skewed summands
z = (means - 1.0) / (1.0 / np.sqrt(N))
for k in (1, 2, 3):
print(f"P(|Z| < {k}): simulated {np.mean(np.abs(z) < k):.4f} Gaussian {[0.6827, 0.9545, 0.9973][k-1]}")P(|Z| < 1): simulated 0.6879 Gaussian 0.6827
P(|Z| < 2): simulated 0.9566 Gaussian 0.9545
P(|Z| < 3): simulated 0.9957 Gaussian 0.9973
With only 30 skewed summands, the 68-95-99.7 rule already roughly holds.
In software
rng.normal(mu, sigma, size)andrng.multivariate_normal(mu, Sigma, size);scipy.stats.multivariate_normal.logpdf.- Linear regression with squared loss is maximum likelihood under Gaussian noise (Maximum Likelihood Estimation).
- Weight initializations draw from scaled Gaussians (Initialization); diffusion models add and remove Gaussian noise.
Exercises
Exercise 1: Standardize
. What is , and what is in terms of ?
Answer
; .
Solution
A linear map of a Gaussian is Gaussian with mean and variance . .
Exercise 2: Sum of independent Gaussians
and are independent. What is the distribution of ?
Answer
.
Solution
Mean ; variance .
Exercise 3: Density at the mean (code)
What is the density of at in dimensions?
Answer
: about , , .
Solution
At the mean the exponent is 0 and , leaving the normalizing constant.
import numpy as np
for D in (1, 2, 10):
print(D, (2 * np.pi) ** (-D / 2))1 0.3989422804014327
2 0.15915494309189535
10 0.00010211761384541831
Test yourself
If , then has covariance…
Why compute directly instead of of the density?
- It is a different quantity
- The density under- or overflows in high dimensions
- Logs are faster than exponentials
- The density can be negative
The central limit theorem says the mean of many independent variables with finite variance is…
- exactly Gaussian
- approximately Gaussian
- uniform
- always equal to zero