Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Densities of multivariate distributions

Recall: if f ⁣:R→[0,∞)f \colon \mathbb{R} \to [0,\infty) is a density function belonging to the distribution PP of a real random variable XX, then P(X<c)=∫−∞cf(x)dxP(X < c) = \int_{-\infty}^c f(x)dx.

Generalising this to two dimensions, that is, to a random variable (X,Y)(X,Y) with values in R2\mathbb{R}^2, gives P(X<c,Y<d)=∫−∞c∫−∞df(x,y) dy dxP(X < c, Y < d) = \int_{-\infty}^c \int_{-\infty}^d f(x,y)\,dy\,dx.

So the density function must have the signature f ⁣:R2→[0,∞)f \colon \mathbb{R}^2 \to [0,\infty): it encodes a probability density for every possible value of (X,Y)(X,Y).

More generally, a function f ⁣:Rn→[0,∞)f \colon \mathbb{R}^n \to [0,\infty) with ∫Rnf(x)dx=1\int_{\mathbb{R}^n} f(x)dx = 1 defines a probability measure PP by P(A)=∫Af(x)dxP(A) = \int_A f(x)dx, and ff is called a density for the distribution PP.

Derivation: from the symmetry to the formula

Before we define the multidimensional normal distribution, we want to find it. The question is: which distribution on the plane R2\mathbb{R}^2 plays the role that N(0,1)\mathcal{N}(0,1) plays on the line? The one-dimensional normal distribution has two properties that single it out: it is symmetric about its expected value, and among all distributions with a given variance it is the one with the greatest entropy, that is, the one that assumes nothing beyond its expected value and variance (see The normal distribution; exactly what entropy means for continuous distributions is settled in the chapter Relative entropy).

We already know one way of building a two-dimensional distribution out of two one-dimensional ones: the product distribution of two independent random variables, whose density is the product of the two densities. Let us try out a few candidates and look down on the density from above:

<Figure size 1200x400 with 3 Axes>

Only the third picture is rotationally symmetric: you can turn it about the origin without anything changing. That is no accident; it is a calculation:

ϕ(x) ϕ(y)=12π e−x2/2 e−y2/2=12π e−(x2+y2)/2=12π e−r2/2,\phi(x)\,\phi(y) = \frac{1}{2\pi}\, e^{-x^2/2}\, e^{-y^2/2} = \frac{1}{2\pi}\, e^{-(x^2+y^2)/2} = \frac{1}{2\pi}\, e^{-r^2/2},

the density depends only on the distance rr from the origin. The product of two standard normal distributions is therefore the “most symmetric” distribution on the plane, and it inherits the maximum entropy property. This is our standard normal distribution in two dimensions, and in nn dimensions it works the same way: ϕ(z)=(2π)−n/2e−∥z∥2/2\phi(z) = (2\pi)^{-n/2} e^{-\lVert z \rVert^2/2} for z∈Rnz \in \mathbb{R}^n.

Every other normal distribution arises from it by three operations, which we add one at a time. Let ZZ be standard normally distributed in Rn\mathbb{R}^n.

  1. Shifting: X=Z+μX = Z + \mu has expected value μ\mu. The density is then evaluated at z=x−μz = x - \mu.

  2. Scaling: X=DZX = DZ with a diagonal matrix D=diag⁡(d1,…,dn)D = \operatorname{diag}(d_1,\dots,d_n) stretches the ii-th axis by did_i. The variance in that direction becomes di2d_i^2, since V(diZi)=di2V(Zi)\mathbb{V}(d_i Z_i) = d_i^2 \mathbb{V}(Z_i): double the data and you quadruple the variance.

  3. Rotating: X=RZX = RZ with an orthogonal matrix RR (that is, RTR=IR^T R = I) turns the cloud of points without changing any distances.

Put together: X=AZ+μX = A Z + \mu with A=RDA = R D. What density does XX have? We undo the transformation, Z=A−1(X−μ)Z = A^{-1}(X - \mu), and substitute into e−∥z∥2/2e^{-\lVert z \rVert^2/2}. For the squared length, every matrix MM satisfies

∥Mz∥2=(Mz)T(Mz)=zTMTMz,\lVert M z \rVert^2 = (Mz)^T (Mz) = z^T M^T M z,

because transposing a product reverses the order of the factors. With M=A−1M = A^{-1} and z=x−μz = x - \mu the exponent therefore reads (x−μ)T(A−1)TA−1(x−μ)=(x−μ)T(AAT)−1(x−μ)(x-\mu)^T (A^{-1})^T A^{-1} (x - \mu) = (x-\mu)^T (A A^T)^{-1} (x-\mu). The matrix

Σ:=AAT=RD2RT\Sigma := A A^T = R D^2 R^T

is exactly the covariance matrix of XX (check this with the rules of calculation from the chapter Covariance), and Σ−1\Sigma^{-1} in the exponent undoes the rotation and the stretching: it measures the distance from xx to μ\mu not with an ordinary ruler but in units of the spread in each direction. The normalising factor also picks up det⁡(Σ)−1/2=∣det⁡A∣−1\det(\Sigma)^{-1/2} = \lvert\det A\rvert^{-1}, because the rotation and the stretching enlarge areas (volumes, in general) by ∣det⁡A∣\lvert \det A \rvert and thin the density out accordingly.

We can follow this through on the computer: draw a standard normal sample, apply AA to it, and read Σ\Sigma and the stretch factors back off the sample:

Sigma = A A^T =
 [[3.063 1.624]
 [1.624 1.187]]
empirical covariance matrix of the sample:
 [[3.094 1.61 ]
 [1.61  1.164]]
square roots of the eigenvalues: [0.5 2. ] -- these are the stretch factors 1/2 and 2
<Figure size 1000x500 with 2 Axes>

The black axes are the eigenvectors of Σ\Sigma, scaled by the square roots of the eigenvalues: the rotation RR sits in the eigenvectors, the stretching DD in the eigenvalues. In practice this works the other way round: if your data have correlated features, you do not first have to “rotate them straight” before modelling them with a normal distribution. You substitute the empirical mean and the empirical covariance matrix directly into the formula, which we now record as a definition.

The multivariate normal distribution

This distribution really does have the expected value and the covariance matrix its name promises; it is also written N(μ,Σ)\mathcal{N}(\mu,\Sigma).

In deep learning, a variational autoencoder models every data point as a normal distribution N(μ,diag⁡(σ2))\mathcal{N}(\mu, \operatorname{diag}(\sigma^2)) in a latent space, where μ\mu and σ\sigma are produced by the encoder network (the course Introduction to Deep Learning).

Tip: read up on how meshgrids work in Numpy (and, with them, the function mgrid). A hint: a meshgrid is a kind of explicit broadcasting done in advance, so that the arithmetic afterwards runs vectorised and faster. Try it out yourself on small meshgrids by evaluating a function of two variables, x2+y2x^2+y^2 say, on a meshgrid in vectorised form.

<Figure size 900x900 with 4 Axes>
<Figure size 900x900 with 4 Axes>

So the marginal distributions look exactly the same – the correlation shows only in the joint distribution, where it is plain enough to see: in the two-dimensional histogram (also called a heatmap) and in the contour plot (the ellipses).

The covariance matrix has a direct geometric interpretation: every positive definite symmetric matrix AA defines an inner product ⟨v,w⟩A:=⟨Av,w⟩=(Av)Tw=vTATw=vTAw=⟨v,Aw⟩\langle v, w \rangle_A := \langle Av, w\rangle = (Av)^Tw = v^TA^Tw = v^TAw = \langle v, Aw\rangle and with it a so-called quadratic form v↦⟨v,v⟩Av \mapsto \langle v, v \rangle_A. The contour lines (level sets) of the quadratic form are ellipses, with the same axes as the contour lines of the corresponding normal distribution.

The formula defining the density of the multivariate normal distribution contains the expression vTΣ−1vv^T \Sigma^{-1} v for the vector v=x−μv = x - \mu, and that is exactly the inner product defined by the matrix Σ−1\Sigma^{-1}. The density is therefore c⋅e−Q(v)/2c \cdot e^{-Q(v)/2} with Q(v):=vTΣ−1vQ(v) := v^T \Sigma^{-1} v and a constant cc, and the level sets of the density are precisely the level sets of QQ. For Σ=(1ρρ1)\Sigma = \begin{pmatrix} 1 & \rho \\ \rho & 1 \end{pmatrix} we have Σ−1=11−ρ2(1−ρ−ρ1)\Sigma^{-1} = \frac{1}{1-\rho^2}\begin{pmatrix} 1 & -\rho \\ -\rho & 1 \end{pmatrix}.

The covariance matrix encodes exactly how the ellipse arises from a perfect circle. The circle corresponds to the identity matrix as covariance matrix, and Σ=RD2R⊤\Sigma = R D^2 R^\top from the derivation above says in which directions (the columns of RR) and how strongly (the squares of the entries of DD) the stretching happens. Σ\Sigma itself, being symmetric, rotates nothing; Σ−1\Sigma^{-1} has the same eigenvectors and compresses where Σ\Sigma stretches. That is precisely why Σ−1\Sigma^{-1} in the density undoes the stretching and measures the distance to μ\mu in units of the spread.

To get a better grip on the geometry, start with bivariate normal distributions and make the matrix Σ\Sigma more complicated step by step (multiples of the identity matrix, diagonal matrices with different entries on the diagonal, arbitrary matrices). Any covariance matrix Σ\Sigma whatsoever can be brought into diagonal form by an orthogonal change of basis, which is to say that rotating the space makes the ellipse (the contour line of the normal distribution) parallel to the axes. Scaling the axes then turns the ellipse into a circle. Seen that way, every normal distribution is a standard normal distribution, just in different coordinates.

<Figure size 1600x800 with 2 Axes>

The axes of the ellipses can also be computed: they are the eigenvectors of Σ\Sigma, and the square roots of the eigenvalues give (up to a common factor) the lengths of the semi-axes. For Σ=(1ρρ1)\Sigma = \begin{pmatrix} 1 & \rho \\ \rho & 1 \end{pmatrix} these are the two diagonals, with eigenvalues 1+ρ1+\rho and 1−ρ1-\rho. The chapter on Dimensionality reduction builds precisely on this eigendecomposition:

Eigenvalues: [0.3 1.7]
Eigenvectors (columns):
[[-0.70710678  0.70710678]
 [ 0.70710678  0.70710678]]

To get to know three-dimensional plotting with matplotlib as well, let us take another look at the density function of the normal distribution:

<Figure size 1500x1500 with 1 Axes>
<Figure size 1000x1000 with 4 Axes>

Careful

This does not mean that every linear combination of normally distributed random variables is again normally distributed! Here is an example that could almost be from the Datasaurus: XX and YY are each normally distributed on their own but dependent on one another – and bang, X+YX+Y is not normally distributed (because X+Y=0 has probability 50%):

<Figure size 1300x1300 with 4 Axes>

Wikipedia has a nice article on the fact that normally distributed uncorrelated random variables can perfectly well be dependent on one another.

The random variables X, Y from the previous plot are exactly such an example: their correlation coefficient is 0.

In the other direction there is a statement with strong hypotheses:

Exercise:

Convince yourself that you know what the covariance matrix looks like in the case of independence.