TExcelTAD
Home › Math for Machine Learning › The Gaussian Distribution

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

N(x∣μ,σ2)=12πσ2exp⁡ ⁣(−(x−μ)22σ2)\mathcal{N}(x \mid \mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}}\exp\!\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)
N(x∣μ,Σ)=(2π)−D/2 ∣Σ∣−1/2exp⁡ ⁣(−12(x−μ)TΣ−1(x−μ))\mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}, \Sigma) = (2\pi)^{-D/2}\,\lvert\Sigma\rvert^{-1/2}\exp\!\left(-\frac{1}{2}(\mathbf{x} - \boldsymbol{\mu})^T\Sigma^{-1}(\mathbf{x} - \boldsymbol{\mu})\right)

The exponent is −12-\frac{1}{2} times the squared Mahalanobis distance. Level sets are ellipsoids whose axes are the eigenvectors of Σ\Sigma, with semi-axis lengths proportional to λi\sqrt{\lambda_i}.

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 Σ=LLT\Sigma = LL^T:

log⁡N(x∣μ,Σ)=−D2log⁡2π−∑ilog⁡Lii−12∥L−1(x−μ)∥2\log\mathcal{N}(\mathbf{x} \mid \boldsymbol{\mu}, \Sigma) = -\frac{D}{2}\log 2\pi - \sum_i \log L_{ii} - \frac{1}{2}\lVert L^{-1}(\mathbf{x} - \boldsymbol{\mu}) \rVert^2
python · try it yourself
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 10−20010^{-200}, near the float64 underflow limit of about 10−30810^{-308} (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 x∼N(μ,Σ)\mathbf{x} \sim \mathcal{N}(\boldsymbol{\mu}, \Sigma) then

Ax+b∼N(Aμ+b, AΣAT)A\mathbf{x} + \mathbf{b} \sim \mathcal{N}(A\boldsymbol{\mu} + \mathbf{b},\ A\Sigma A^T)

and independent Gaussians add: x+y∼N(μx+μy,Σx+Σy)\mathbf{x} + \mathbf{y} \sim \mathcal{N}(\boldsymbol{\mu}_x + \boldsymbol{\mu}_y, \Sigma_x + \Sigma_y). Sampling uses the first fact: transform standard normal draws with μ+Lz\boldsymbol{\mu} + L\mathbf{z}.

The central limit theorem

The mean of NN independent variables with finite variance is approximately Gaussian for large NN, whatever their own distribution. That is one justification for Gaussian noise models: measurement error is often a sum of many small effects.

python · try it yourself
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) and rng.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

X∼N(100,152)X \sim \mathcal{N}(100, 15^2). What is Z=(X−100)/15Z = (X - 100)/15, and what is P(X>130)P(X > 130) in terms of ZZ?

Answer

Z∼N(0,1)Z \sim \mathcal{N}(0, 1); P(X>130)=P(Z>2)≈0.0228P(X > 130) = P(Z > 2) \approx 0.0228.

Solution

A linear map of a Gaussian is Gaussian with mean (100−100)/15=0(100 - 100)/15 = 0 and variance 152/152=115^2/15^2 = 1. P(Z>2)=(1−0.9545)/2≈0.0228P(Z > 2) = (1 - 0.9545)/2 \approx 0.0228.

Exercise 2: Sum of independent Gaussians

X∼N(1,4)X \sim \mathcal{N}(1, 4) and Y∼N(−2,5)Y \sim \mathcal{N}(-2, 5) are independent. What is the distribution of 2X+Y2X + Y?

Answer

N(0,21)\mathcal{N}(0, 21).

Solution

Mean 2⋅1−2=02 \cdot 1 - 2 = 0; variance 22⋅4+5=212^2 \cdot 4 + 5 = 21.

Exercise 3: Density at the mean (code)

What is the density of N(0,I)\mathcal{N}(\mathbf{0}, I) at 0\mathbf{0} in D=1,2,10D = 1, 2, 10 dimensions?

Answer

(2π)−D/2(2\pi)^{-D/2}: about 0.39890.3989, 0.15920.1592, 1.0×10−41.0 \times 10^{-4}.

Solution

At the mean the exponent is 0 and ∣I∣=1\lvert I \rvert = 1, leaving the normalizing constant.

python · try it yourself
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 x∼N(μ,Σ)\mathbf{x} \sim \mathcal{N}(\boldsymbol{\mu}, \Sigma), then AxA\mathbf{x} has covariance…

  1. AΣA\Sigma
  2. AΣATA\Sigma A^T
  3. Σ\Sigma
  4. A2ΣA^2\Sigma

Why compute log⁡N\log\mathcal{N} directly instead of log⁡\log of the density?

  1. It is a different quantity
  2. The density under- or overflows in high dimensions
  3. Logs are faster than exponentials
  4. The density can be negative

The central limit theorem says the mean of many independent variables with finite variance is…

  1. exactly Gaussian
  2. approximately Gaussian
  3. uniform
  4. always equal to zero