Post · 11 min read· pp. 103–108

Addendum: quantifying our flawed intuitions

This post is an addendum to my blog post about typicality. Please consider reading that first, if you haven’t already. Here, I will try to quantify what happens when our intuitions fail us in high-dimensional spaces.

Note that the practical relevance of this is limited, so consider this a piece of optional extra content!

In the ‘unfair coin flips’ example from the main blog post, it’s actually pretty clear what happens when our intuitions fail us: we think of the binomial distribution, ignoring the order of the sequences as a factor, when we should actually be taking it into account. Referring back to the table from section 2.1, we use the probabilities in the rightmost column, when we should be using those in the third column. But when we think of a high-dimensional Gaussian distribution and come to the wrong conclusion, what distribution are we actually thinking of?

The Gaussian distribution NK\mathcal{N}_K

Let’s start by quantifying what a multivariate Gaussian distribution actually looks like: let x∼N(0,IK)\mathbf{x} \sim \mathcal{N}(\mathbf{0}, I_K), a standard Gaussian distribution in KK dimensions, henceforth referred to as NK\mathcal{N}_K. We can sample from it by drawing KK independent one-dimensional samples xi∼N(0,1)x_i \sim \mathcal{N}(0, 1), and joining them into a vector x\mathbf{x}. This distribution is spherically symmetric, which makes it very natural to think about samples in terms of their distance to the mode (in this case, the origin, corresponding to the zero-vector 0\mathbf{0}), because all samples at a given distance rr have the same density.

Now, let’s look at the distribution of rr: it seems as if the multivariate Gaussian distribution NK\mathcal{N}_K naturally arises by taking a univariate version of it, and rotating it around the mode in every possible direction in KK-dimensional space. Because each of these individual rotated copies is Gaussian, this in turn might seem to imply that the distance from the mode rr is itself Gaussian (or rather half-Gaussian, since it is a nonnegative quantity). But this is incorrect! rr actually follows a chi distribution with KK degrees of freedom: r∼χKr \sim \chi_K.

Note that for K=1K = 1, this does indeed correspond to a half-Gaussian distribution. But as KK increases, the mode of the chi distribution rapidly shifts away from 0: it actually sits at K−1\sqrt{K - 1}. This leaves considerably less probability mass near 0, where the mode of our original multivariate Gaussian NK\mathcal{N}_K is located.

This exercise yields an alternative sampling strategy for multivariate Gaussians: first, sample a distance from the mode r∼χKr \sim \chi_K. Then, sample a direction, i.e. a vector on the KK-dimensional unit sphere SKS^K, uniformly at random: θ∼U[SK]\mathbf{\theta} \sim U[S^K]. Multiply them together to obtain a Gaussian sample: x=r⋅θ∼NK\mathbf{x} = r \cdot \mathbf{\theta} \sim \mathcal{N}_K.

The Gaussian mirage distribution MK\mathcal{M}_K

What if, instead of sampling r∼χKr \sim \chi_K, we sampled r∼N(0,K)r \sim \mathcal{N}(0, K) instead? Note that σχK2=K\sigma^2_{\chi_K} = K, so this change preserves the scale of the resulting vectors. For K=1K = 1, we get the same distribution for x\mathbf{x}, but for K>1K > 1, we get something very different. The resulting distribution represents what we might think the multivariate Gaussian distribution looks like, if we rely on a mistaken intuition and squint a bit. Let’s call this the Gaussian mirage distribution, denoted by M\mathcal{M}: x=r⋅θ∼MK\mathbf{x} = r \cdot \mathbf{\theta} \sim \mathcal{M}_K. (If this thing already has a name, I’m not aware of it, so please let me know!)

We’ve already established that M1≡N1\mathcal{M}_1 \equiv \mathcal{N}_1. But in higher dimensions, these distributions behave very differently. One way to comprehend this is to look at a flattened histogram of samples across all coordinates:

python
import matplotlib.pyplot as plt
import numpy as np

def gaussian(n, k):
    return np.random.normal(0, 1, (n, k))

def mirage(n, k):
    direction = np.random.normal(0, 1, (n, k))
    direction /= np.sqrt(np.sum(direction**2, axis=-1, keepdims=True))
    distance = np.random.normal(0, np.sqrt(k), (n, 1))
    return distance * direction

def plot_histogram(x):
    plt.hist(x.ravel(), bins=100)
    plt.ylim(0, 80000)
    plt.xlim(-4, 4)
    plt.tick_params(labelleft=False, left=False, labelbottom=False, bottom=False)

plt.figure(figsize=(9, 3))
ks = [1, 3, 10, 100]
for i, k in enumerate(ks):
    plt.subplot(2, len(ks), i + 1)
    plt.title(f'K = {k}')
    plot_histogram(gaussian(10**6 // k, k))
    plt.subplot(2, len(ks), i + 1 + len(ks))
    plot_histogram(mirage(10**6 // k, k))

Histograms of the flattened coordinates of the multivariate Gaussian distribution (top) and the Gaussian mirage (bottom).

Histograms of the flattened coordinates of the multivariate Gaussian distribution (top) and the Gaussian mirage (bottom), for different dimensionalities (K). For the mirage, the histograms become increasingly peaked around 0 as the dimensionality increases.

For NK\mathcal{N}_K, this predictably looks like a univariate Gaussian for all KK. For MK\mathcal{M}_K, it becomes highly leptokurtic as KK increases, indicating that dramatically more probability mass is located close to the mode.

Typical sets of NK\mathcal{N}_K and MK\mathcal{M}_K

Let’s also look at the typical sets for both of these distributions. For NK\mathcal{N}_K, the probability density function (pdf) has the form:

fNK(x)=(2π)−K2exp⁡(−xTx2),f_{\mathcal{N}_K}(\mathbf{x}) = (2 \pi)^{-\frac{K}{2}} \exp \left( -\frac{\mathbf{x}^T \mathbf{x}}{2} \right),

and the differential entropy is given by:

HNK=K2log⁡(2πe).H_{\mathcal{N}_K} = \frac{K}{2} \log \left(2 \pi e \right) .

To find the typical set, we just need to look for the x\mathbf{x} where fNK(x)≈2−HNK=(2πe)−K2f_{\mathcal{N}_K}(\mathbf{x}) \approx 2^{-H_{\mathcal{N}_K}} = (2 \pi e)^{-\frac{K}{2}} (assuming the entropy is measured in bits). This is clearly the case when xTx≈K\mathbf{x}^T\mathbf{x} \approx K, or in other words, for any x\mathbf{x} whose distance from the mode is close to K\sqrt{K}. This is the Gaussian annulus from before.

Let’s subject the Gaussian mirage MK\mathcal{M}_K to the same treatment. It’s not obvious how to express the pdf in terms of x\mathbf{x}, but it’s easier if we rewrite x\mathbf{x} as r⋅θr \cdot \mathbf{\theta}, as before, and imagine the sampling procedure: first, pick a radius r∼HN(0,K)r \sim \mathcal{HN}(0, K) (the half-Gaussian distribution — using the Gaussian distribution complicates the math a bit, because the radius should be nonnegative), and then pick a position on the KK-sphere with radius rr, uniformly at random:

fMK(x)=fHN(0,K)(r)⋅fU[SK(r)](θ)=22πKexp⁡(−r22K)⋅1rK−1Γ(K2)2πK2.f_{\mathcal{M}_K}(\mathbf{x}) = f_{\mathcal{HN}(0, K)}(r) \cdot f_{U[S^K(r)]}(\theta) = \frac{2}{\sqrt{2 \pi K}} \exp \left( -\frac{r^2}{2 K} \right) \cdot \frac{1}{r^{K-1}} \frac{\Gamma\left( \frac{K}{2} \right)}{2 \pi ^ \frac{K}{2}} .

The former factor is the density of the half-Gaussian distribution: note the additional factor 2 compared to the standard Gaussian density, because we only consider nonnegative values of rr. The latter is the density of a uniform distribution on the KK-sphere with radius rr (which is the inverse of its surface area). As an aside, this factor is worth taking a closer look at, because it behaves in a rather peculiar way. Here’s the surface area of a unit KK-sphere for increasing KK:

python
import matplotlib.pyplot as plt
import numpy as np
import scipy.special

K = np.arange(0, 30 + 1)
A = (2 * np.pi**(K / 2.0)) / scipy.special.gamma(K / 2.0)
plt.figure(figsize=(9, 3))
plt.stem(K, A, basefmt=' ')
plt.ylim(0, 35)

Surface area of a K-dimensional unit sphere, for K ranging from 0 to 30.

Surface area of a K-dimensional unit sphere, for K ranging from 0 to 30.

Confused? You and me both! Believe it or not, the surface area of a KK-sphere tends to zero with increasing KK — but only after growing to a maximum at K=7K = 7 first. High-dimensional spaces are weird.

Another thing worth noting is that the density at the mode fMK(0)=+∞f_{\mathcal{M}_K}(\mathbf{0}) = +\infty for K>1K > 1, which already suggests that this distribution has a lot of its mass concentrated near the mode.

Computing the entropy of this distribution takes a bit of work. The differential entropy is:

HMK=−∫RKfMK(x)log⁡fMK(x)dx.H_{\mathcal{M}_K} = - \int_{\mathbb{R}^K} f_{\mathcal{M}_K}(\mathbf{x}) \log f_{\mathcal{M}_K}(\mathbf{x}) \mathrm{d}\mathbf{x} .

We can use the radial symmetry of this density to reformulate this as an integral of a scalar function:

HMK=−∫0+∞fMK(r)log⁡fMK(r)SK(r)dr,H_{\mathcal{M}_K} = - \int_0^{+\infty} f_{\mathcal{M}_K}(r) \log f_{\mathcal{M}_K}(r) S^K(r) \mathrm{d} r,

where SK(r)S^K(r) is the surface area of a KK-sphere with radius rr. Filling in the density function, we get:

HMK=−∫0+∞22πKexp⁡(−r22K)⋅log⁡(22πKexp⁡(−r22K)⋅1rK−1Γ(K2)2πK2)dr,H_{\mathcal{M}_K} = - \int_0^{+\infty} \frac{2}{\sqrt{2 \pi K}} \exp \left( -\frac{r^2}{2 K} \right) \cdot \log \left( \frac{2}{\sqrt{2 \pi K}} \exp \left( -\frac{r^2}{2 K} \right) \cdot \frac{1}{r^{K-1}} \frac{\Gamma\left( \frac{K}{2} \right)}{2 \pi ^ \frac{K}{2}} \right) \mathrm{d} r,

where we have made use of the fact that SK(r)S^K(r) cancels out with the second factor of fMK(r)f_{\mathcal{M}_K}(r). We can split up the log⁡\log into three different terms, HMK=H1+H2+H3H_{\mathcal{M}_K} = H_1 + H_2 + H_3:

H1=−∫0+∞22πKexp⁡(−r22K)(−r22K)dr=∫0+∞r22πexp⁡(−r22)dr=12,H_1 = - \int_0^{+\infty} \frac{2}{\sqrt{2 \pi K}} \exp \left( -\frac{r^2}{2 K} \right) \left(-\frac{r^2}{2 K} \right) \mathrm{d} r = \int_0^{+\infty} \frac{r^2}{\sqrt{2 \pi}} \exp \left( -\frac{r^2}{2} \right) \mathrm{d} r = \frac{1}{2},
H2=−∫0+∞22πKexp⁡(−r22K)log⁡(1rK−1)dr=K−12(log⁡K2−γ),H_2 = - \int_0^{+\infty} \frac{2}{\sqrt{2 \pi K}} \exp \left( -\frac{r^2}{2 K} \right) \log \left( \frac{1}{r^{K-1}} \right) \mathrm{d} r = \frac{K - 1}{2} \left( \log \frac{K}{2} - \gamma \right),
H3=−∫0+∞22πKexp⁡(−r22K)log⁡(22πKΓ(K2)2πK2)dr=−log⁡(12πKΓ(K2)πK2),H_3 = - \int_0^{+\infty} \frac{2}{\sqrt{2 \pi K}} \exp \left( -\frac{r^2}{2 K} \right) \log \left( \frac{2}{\sqrt{2 \pi K}} \frac{\Gamma\left( \frac{K}{2} \right)}{2 \pi ^ \frac{K}{2}} \right) \mathrm{d} r = - \log \left( \frac{1}{\sqrt{2 \pi K}} \frac{\Gamma\left( \frac{K}{2} \right)}{\pi ^ \frac{K}{2}} \right),

where we have taken log⁡\log to be the natural logarithm for convenience, and γ\gamma is the Euler-Mascheroni constant. In summary:

HMK=12+K−12(log⁡K2−γ)−log⁡(12πKΓ(K2)πK2).H_{\mathcal{M}_K} = \frac{1}{2} + \frac{K - 1}{2} \left( \log \frac{K}{2} - \gamma \right) - \log \left( \frac{1}{\sqrt{2 \pi K}} \frac{\Gamma\left( \frac{K}{2} \right)}{\pi ^ \frac{K}{2}} \right) .

Note that HM1=12log⁡(2πe)H_{\mathcal{M}_1} = \frac{1}{2} \log (2 \pi e), matching the standard Gaussian distribution as expected.

Because this is measured in nats, not in bits, we find the typical set where fMK(x)≈exp⁡(−HMK)f_{\mathcal{M}_K}(\mathbf{x}) \approx \exp(-H_{\mathcal{M}_K}). We must find r≥0r \geq 0 so that

r22K+(K−1)log⁡r=12+K−12(log⁡K2−γ).\frac{r^2}{2 K} + (K - 1) \log r = \frac{1}{2} + \frac{K - 1}{2} \left( \log \frac{K}{2} - \gamma \right) .

We can express the solution of this equation in terms of the Lambert WW function:

r=K(K−1)W(1K(K−1)exp⁡(1K−1+log⁡K2−γ)).r = \sqrt{K (K - 1) W\left(\frac{1}{K (K - 1)} \exp \left( \frac{1}{K - 1} + \log \frac{K}{2} - \gamma \right) \right)} .

python
import matplotlib.pyplot as plt
import numpy as np
import scipy.special

K = np.unique(np.round(np.logspace(0, 6, 100)))
w_arg = np.exp(1 / (K - 1) + np.log(K / 2) - np.euler_gamma) / (K * (K - 1))
r = np.sqrt(K * (K - 1) * scipy.special.lambertw(w_arg))
r[0] = 1  # Special case for K = 1.

plt.figure(figsize=(9, 3))
plt.plot(K, r / np.sqrt(K))
plt.xscale('log')
plt.ylim(0, 1.2)
plt.xlabel('$K$')
plt.ylabel('$\\frac{r}{\\sqrt{K}}$')

The distance from the mode at which the typical set of the Gaussian mirage is found, as a function of K.

The distance from the mode at which the typical set of the Gaussian mirage is found, normalised by the standard deviation, as a function of K.

As K→+∞K \to +\infty, this seems to converge to the value 0.52984K0.52984 \sqrt{K}, which is somewhere in between the mode (00) and the mean (2Kπ≈0.79788K\sqrt{\frac{2K}{\pi}} \approx 0.79788 \sqrt{K}) of the half-Gaussian distribution (which rr follows by construction). This is not just an interesting curiosity: although it is clear that the typical set of MK\mathcal{M}_K is much closer to the mode than for NK\mathcal{N}_K (because r<Kr < \sqrt{K}), the mode is not unequivocally a member of the typical set. In fact, the definition of typical sets sort of breaks down for this distribution, because we need to allow for a very large range of probability densities to capture the bulk of its mass. In this sense, it behaves a lot more like the one-dimensional Gaussian. Nevertheless, even this strange concoction of a distribution exhibits unintuitive behaviour in high-dimensional space!

If you would like to cite this post in an academic context, you can use this BibTeX snippet:

text
@misc{dieleman2020typicality,
  author = {Dieleman, Sander},
  title = {Musings on typicality},
  url = {https://benanne.github.io/2020/09/01/typicality.html},
  year = {2020}
}