Near-orthogonality of random vectors
and a connection to superposition and other phenomena
This is entry #5 in the high-dimensional space series.
There’s this curious property called near-orthogonality of random vectors in high dimensional space, which is linked with the idea of superposition in neural networks (Elhage et al., 2022) and other fascinating concepts.
Let’s take a look at it and derive it ourselves for a particular set of probability distributions. Afterwards, we’ll also look at concepts in machine learning that are connected to this property.
The near-orthogonality principle
Last time, I left off with this particular statement:
In high dimensional space, two random vectors sampled from a Gaussian distribution are nearly orthogonal with high probability.
Now, some remarks before we continue.
- Within this post, we’re going to look at an isotropic Gaussian with zero mean for us to bound it clearly. Non-zero means and anisotropic Gaussians (i.e., different directions having different variances) are a bit of a deal-breaker for our proof within the post.
- However, the claim actually extends beyond the isotropic Gaussian and holds for any rotation-invariant distribution (so, the ball and the sphere, and even funkier stuff like a spherical Cauchy distribution).
- Near-orthogonality also extends to i.i.d. zero-mean coordinates more generally, such as the uniform distribution $[-k, k]^d$. It’s closely related to the Johnson–Lindenstrauss lemma, but our method today doesn’t cover this.
But before we look at proving it, let’s start with some evidence.
We can easily sample pairs of vectors from a multivariate Gaussian distribution, compute their cosines, and inspect the distribution of cosine values as we increase their dimensions, as plotted below:
The outer band in particular, showing about $99.7\%$ of random pairs concentrating around zero, illustrates near-orthogonality quite well. Random pairs from common embedding dimensions of $d = 512$-$2048$ appear to be well concentrated in terms of cosine.
We can also inspect the breakdown of angles with a histogram view. It seems to show the concentration of angles quite clearly:
Now, having seen some empirical results, let’s show that we can derive specific bounds as well.
A proof for the Gaussian case
Let’s be a bit more thorough with our claim, as “high probability” is a bit loose.
We’d like to show that for a fixed $\varepsilon > 0$, $\mathbb{P}[\lvert\cos\theta\rvert > \varepsilon] \to 0$ as $d \to \infty$, and ideally by deriving an upper bound that closely tracks this based on the dimensionality $d$.
We’ll first demonstrate an $O(1/d)$ bound from a well-known argument, and it’s possible to derive an exponential bound as well. Luckily, the previous posts on the $n$-ball and sphere give us precisely the tool we need to derive this particular exponential bound.
Setup, and the rotational symmetry trick
Let $x, y$ be independent random vectors sampled from an isotropic Gaussian, $\mathcal{N}(0, \sigma^2I_d)$. We use $d$ for the dimension.
The argument goes like this: given that the Gaussian is rotationally symmetric, we can rotate the space (with some orthogonal matrix $Q$ depending on $x$) such that $x$ now points along the first coordinate axis. We have the new point as
\[x' = Qx = (\lVert x\rVert, 0, ..., 0),\]and the rotation retains norm. Of course, the same rotation is applied to $y$ as well:
\[y' = Qy = (y'_1, ..., y'_d)\]with again $\lVert y’\rVert = \lVert y\rVert$ by the same principle, and the angle between $x$ and $y$ is the same angle between $x’$ and $y’$, as shown visually in the figure below.
Is $y'$ still Gaussian?
Yes it is, and we can show it as well. We condition on $x$ first: $Q$ is then a fixed orthogonal matrix, and since $y$ is independent of $x$, it is still $\mathcal{N}(0, \sigma^2 I_d)$.
For $z = Qy$, the change of variables formula gives
\[\begin{align} p_z(z) &= p_y(Q^\top z)\,\lvert\det Q^\top\rvert \\ &= \frac{1}{(2\pi\sigma^2)^{d/2}} \exp\left(-\frac{\lVert Q^\top z\rVert^2}{2\sigma^2}\right) \\ &= \frac{1}{(2\pi\sigma^2)^{d/2}} \exp\left(-\frac{\lVert z\rVert^2}{2\sigma^2}\right), \end{align}\]using $\lvert \det Q \rvert = 1$ and $\lVert Q^\top z\rVert = \lVert z \rVert$. The density depends only on the radius, and rotations preserve both radius and volume. So the conditional distribution of $y’$ given $x$ is $\mathcal{N}(0, \sigma^2 I_d)$ for every $x$, which means $y’$ is independent of $x$ and unconditionally $\mathcal{N}(0, \sigma^2 I_d)$.
With this transformation, we now have
\[\begin{align} \cos\theta &= \frac{x \cdot y}{\lVert x \rVert \lVert y\rVert} \\ &= \frac{\lVert x \rVert\cdot y'_1}{\lVert x \rVert\lVert y'\rVert} \\ &= \frac{y'_1}{\lVert y'\rVert}. \end{align}\]There are two things about this result.
- The cosine is now independent of $x$, which means $x$ doesn’t even have to be from the same Gaussian in the first place. It can be any random vector independent of $y$ (except $\vec{0}$).
- More specifically, the cosine is equal to the first component of the rotated and normalized vector $y’ / \lVert y’ \rVert$, which now resides on the unit sphere.
The point $y’ / \lVert y’ \rVert$ is uniformly distributed on the hypersphere. We’ve actually looked at this concept in a previous post on the Gaussian, and in particular derived the marginal density of the first coordinate parameterized by $t$ as
\[p(t) \propto (1 - t^2)^{(d-3)/2}, \qquad t \in [-1, 1].\]This will be helpful later. In the meantime, let’s go back to deriving our first bound.
Also note that this result about the cosine holds for any distribution of $y$ that is rotationally symmetric. This includes the Gaussian, the ball, and the sphere.
The $O(1/d)$ bound
Markov’s inequality is helpful here. Let’s recall how it is defined:
Markov’s inequality
If $X \geq 0$ and $\alpha > 0$, then $\mathbb{P}[X \ge \alpha] \le \frac{\mathbb{E}[X]}{\alpha}.$
This lets us bound a tail probability using expectation. The trick is to let $X = \cos^2\theta$, i.e., we need to calculate $\mathbb{E}[\cos^2\theta]$. To do this, we utilize rotational symmetry once more. Recall that
\[(y')_{1}^{2} + (y')_{2}^{2} + \cdots + (y')_{d}^{2} = \lVert y'\rVert^2.\]Each dimension is symmetrical and has the same marginal distribution, so we can write
\[1 = \mathbb{E}\left[\sum_{i=1}^d \frac{(y'_i)^2}{\lVert y'\rVert^2}\right] = \sum_{i=1}^d \mathbb{E}\left[\frac{(y'_i)^2}{\lVert y'\rVert^2}\right] = d\cdot\mathbb{E}\left[\frac{(y'_1)^2}{\lVert y'\rVert^2}\right] = d\cdot\mathbb{E}[\cos^2\theta].\]Swapping two coordinates is an orthogonal map, which leaves $\mathcal{N}(0, \sigma^2 I_d)$ unchanged, so every ratio $(y’_i)^2/\lVert y’\rVert^2$ has the same distribution, which justifies the third equality above.
From this, we obtain $\mathbb{E}[\cos^2\theta] = 1/d$. Also, note that $\lvert\cos\theta\rvert > \varepsilon$ holds if and only if $\cos^2\theta > \varepsilon^2$, so they represent the same event.
Applying Markov’s inequality1 gives us
\[\mathbb{P}[\lvert\cos\,\theta\rvert > \varepsilon] = \mathbb{P}[\cos^2\theta > \varepsilon^2] \le \frac{\mathbb{E}[\cos^2\theta]}{\varepsilon^2} = \frac{1}{d\varepsilon^2}.\]For any fixed $\varepsilon > 0$, for increasing $d$ we obtain the $O(1/d)$ bound with the process above.
However, this bound doesn’t demonstrate a useful consequence of near-orthogonality, which is the existence of exponentially many near-orthogonal directions. For this, we need the second bound.
A tighter exponential bound using the marginal
Recall that after the rotational symmetry trick, we have $y’/\lVert y’ \rVert$ as a uniform point on the sphere. Its first coordinate, $y’_1 / \lVert y’ \rVert$, is equal to $\cos\theta$ and has a parametric distribution in the form of
\[p(t) \propto (1-t^2)^{(d-3)/2} \qquad t \in [-1, 1].\]We can use the marginal to obtain a tighter bound than the Markov bound above.
Deriving the exponential bound
We can make the bound tighter by using the marginal distribution, and bounding its tail:
\[\mathbb{P}[\lvert\cos\,\theta\rvert > \varepsilon] = 2\int_{\varepsilon}^{1}p(t)\,dt,\]where we have
\[p(t) = C_d (1-t^2)^{(d-3)/2}\]from before. $\mathbb{P}[\cos\theta > \varepsilon]$ is the survival function of the marginal $p(t)$, and we’re interested in how fast it decays, which will give us our bound. The CDF of $p(t)$ does not have an elementary form, but we just need to look at the tail.
We have a nice way of actually ignoring the constant $C_d$ and continuing without explicitly deriving the survival function. First, we note that
\[C_d \int_{-1}^{1} (1-t^2)^{(d-3)/2}\,dt = 2\,C_d \int_{0}^{1} (1-t^2)^{(d-3)/2}\,dt = 1.\]We can use this to directly express our target as the ratio
\[\begin{align} \mathbb{P}[\lvert\cos\,\theta\rvert > \varepsilon] &= \frac{2C_d\int_{\varepsilon}^{1}(1-t^2)^{(d-3)/2}\,dt}{2C_d \int_{0}^{1}(1-t^2)^{(d-3)/2}\,dt} \\ &= \frac{\int_{\varepsilon}^{1}(1-t^2)^{(d-3)/2}\,dt}{\int_{0}^{1}(1-t^2)^{(d-3)/2}\,dt}. \end{align}\]Let us introduce a change of variables $u$ where $t=\sqrt{\varepsilon^2+(1-\varepsilon^2)u^2}$, which follows from $(1-t^2) = (1 - \varepsilon^2)(1-u^2)$ with $\varepsilon \in (0, 1)$. Applying this transformation we have
\[dt=\frac{(1-\varepsilon^2)u}{\sqrt{\varepsilon^2+(1-\varepsilon^2)u^2}}\,du,\]and the numerator above can be expressed as
\[\int_\varepsilon^1(1-t^2)^{(d-3)/2}\,dt = (1-\varepsilon^2)^{(d-1)/2} \int_0^1 G(u)\,(1-u^2)^{(d-3)/2}\,du,\]where we define $G(u)$ as
\[G(u) = \frac{u}{\sqrt{\varepsilon^2+(1-\varepsilon^2)u^2}}.\]We can directly show that $G(u) \le 1$ by looking at the value within the square root in the denominator. For a fixed $\varepsilon > 0$, we have \(\varepsilon^2+(1-\varepsilon^2)u^2 = u^2 + \varepsilon^2(1-u^2)\ge u^2,\) because $u$ is confined to $[0, 1]$. This means that $G(u) \le 1$.
Now, we continue with
\[\begin{align} \mathbb{P}[\lvert\cos\,\theta\rvert > \varepsilon] &= \frac{\int_{\varepsilon}^{1}(1-t^2)^{(d-3)/2}\,dt}{\int_{0}^{1}(1-t^2)^{(d-3)/2}\,dt} \\ &= (1-\varepsilon^2)^{(d-1)/2}\cdot \frac{\int_{0}^{1}G(u)(1-u^2)^{(d-3)/2}\,du}{\int_{0}^{1}(1-t^2)^{(d-3)/2}\,dt} \\ &\le (1 - \varepsilon^2)^{(d-1)/2}. \end{align}\]Finally, we can make use of the standard inequality, $1 - \varepsilon^2 \le e^{-\varepsilon^2}$, to obtain
\[\begin{align} \mathbb{P}[\lvert\cos\,\theta\rvert > \varepsilon] &\le (1 - \varepsilon^2)^{(d-1)/2} \\ &\le e^{-\varepsilon^2 (d-1)/2}. \tag*{$\Box$} \end{align}\]So, for a given $\varepsilon > 0$, the probability of a cosine being larger in magnitude than $\varepsilon$ is bounded by $e^{-\varepsilon^2(d-1)/2}$. Ball’s well-known cap bound (Ball, 1997) gives $\mathbb{P}[\cos\theta > \varepsilon] \le e^{-d\varepsilon^2/2}$ for one side, hence $2e^{-d\varepsilon^2/2}$ for $\lvert\cos\theta\rvert$. Our two-sided bound here is slightly tighter, but the exponential rate is the same.
We can compare our two bounds in the figure below. The exponential bound is much stronger than the Markov bound in the first section.
Exponentially many near-orthogonal vectors
Within a $d$-dimensional space, at most $d$ vectors with distinct directions within a set can be mutually orthogonal (e.g., the basis vectors). However, if we relax this to near-orthogonality, we can fit much more. How much more follows from our exponential bound, which we can “reverse-engineer” as follows.
Our argument is like this: if two random Gaussian vectors fail to be near-orthogonal with probability less than $e^{-\varepsilon^2(d-1)/2}$, then we can expect to “fit” exponentially many near-orthogonal vectors for a given $\varepsilon$ and $d$. Let’s look at estimating this number from above and below.
A lower bound. Let’s pick any unit vector $v_1$. It rules out the region $\{u : \lvert u \cdot v_1\rvert > \varepsilon\}$, which are two antipodal spherical caps whose total measure is exactly $\mathbb{P}[\lvert\cos\theta\rvert > \varepsilon]$. Then, we pick some $v_2$ outside the excluded region, and rule out its two caps, and repeat this process (Feels like packing, doesn’t it?). We can only get stuck once the excluded caps cover the whole sphere, which requires $N \cdot \mathbb{P}[\lvert\cos\theta\rvert > \varepsilon] \ge 1$.
With our previous exponential bound, this gives
\[N_{\max} \;\ge\; \frac{1}{\mathbb{P}[\lvert\cos\theta\rvert > \varepsilon]} \;\ge\; (1-\varepsilon^2)^{-(d-1)/2} \;\ge\; e^{\varepsilon^2(d-1)/2}.\]So, we are guaranteed at minimum $e^{\varepsilon^{2}(d-1)/2}$ vectors within the set for this $d$ and $\varepsilon$ value. Visually, the greedy approach works like the figure below for $d = 3$ and $\varepsilon = 0.7$.
An upper bound. There also has to be an upper bound, given that at some point the space will “run out” of mutually near-orthogonal directions. Here, a search in the literature points towards Alon’s bound (Alon, 2003), which yields for $1/\sqrt{N} \le \varepsilon \le 1/2$ and an absolute constant $C$ the bound
\[\log N_{\max} \;\le\; C\, \varepsilon^2\, d\, \log(1/\varepsilon).\]How many vectors, exactly? It’s quite interesting that the exact cardinality of two-sided near-orthogonal vector sets for some dimension $d$ and threshold $\varepsilon > 0$ has no closed form. It is, indeed, a type of packing problem: specifically, it corresponds to line packing (Grassmannian packing). This is actually an open problem, in terms of both exact numbers, as well as asymptotic bounds! The one-sided case for $\varepsilon = 0.5$ in particular is the Kissing number problem.
Here’s some example near-orthogonal sets that correspond to various $\varepsilon$ values, for the “smaller” case of $d=3$:
Estimates for real embedding spaces. Let’s take $d = 768$ (e.g., the dimensionality of an embedding from the DINOv2 encoder with ViT-B/14 (Oquab et al., 2024)). The lower bound for $\varepsilon = 0.3$ gives $(1-\varepsilon^2)^{-(d-1)/2} = 0.91^{-383.5} \approx 5 \times 10^{15}$ near-orthogonal directions, compared with only $768$ exactly orthogonal ones. For a stricter $\varepsilon = 0.1$ though, the lower bound gives only about $47$, much fewer than the $768$ basis vectors (which trivially satisfy any $\varepsilon$). So, the exponential regime of the lower bound is more relevant once $\varepsilon^2 d$ is large. The intermediate $\varepsilon = 0.2$ gives $0.96^{-383.5} \approx 6 \times 10^{6}$, i.e., over 6 million directions.
Applications of near-orthogonality
There are many interactions in machine learning with this phenomenon. We already saw the connection to the line packing problem, and I’ll briefly mention some others here.
Superposition and near-orthogonality. Superposition, as mentioned in the introduction, is the hypothesis that neural networks represent more features than they have dimensions by assigning features to nearly orthogonal directions (for a precise definition, see (Elhage et al., 2022)). With exactly orthogonal directions, a network could store only $d$ features. As we’ve just seen, though, the space has room for exponentially many near-orthogonal ones.
The price of having much more feature directions compared to exact orthogonality is interference between active features. Reading off feature $i$ from an embedding picks up $\sum_{j \ne i} a_j \cos\theta_{ij}$ from the other active features. This stays small when only a few features are active at once, i.e., when features are sparse. It’s also part of why individual neurons are hard to interpret, as each one participates in many features. Below, we illustrate a toy example of the effects of “choosing” a threshold $\varepsilon$. A looser threshold gives us a set with more directions to choose, but in return, the interference between directions becomes harder to manage.
The equator phenomenon. In our analysis of the high-dimensional unit sphere and the equator phenomenon, we showed that for any unit vector on the sphere, the corresponding equator band for that vector keeps approximately $68\%$ of the mass while becoming relatively thinner with increasing dimension. Now, we see that this is closely related with near-orthogonality, as our exponential bound utilizes the spherical caps which are “what’s left of the sphere” besides the equator (in other words, $\mathbb{P}[\lvert\cos\theta\rvert \le \varepsilon]$ is the mass of the $\varepsilon$-band around the equator).
Separability in high-dimensional space. High-dimensional space makes it increasingly likely for points to be separated from each other with trivial boundaries (known as one of the “blessings of dimensionality” (Donoho, 2000)), including using discriminants based on simple pairwise dot products, which make use of this near-orthogonality property. This topic is something that I’m thinking of discussing in detail later, so we’ll leave it here.
When is near-orthogonality not as applicable?
Learned representations. Of course, our discussion up to this point has been on random vectors in high-dimensional space. While the features that networks learn may be near-orthogonal directions, the representations that neural networks learn are sparse combinations of these directions, and similar inputs share and so have higher cosine values between their representations. This is somewhat enforced by supervised learning and auxiliary regularization methods (e.g., pushing and pulling samples in space with contrastive objectives). Also, while the ambient dimension $d$ of the representation vector may be large, the intrinsic dimensionality of learned representations is often much smaller than the ambient dimension2.
Non-zero means and anisotropic distributions. Both of these break the rotational symmetry around the origin that our proof relies on, but that doesn’t automatically rule out near-orthogonality. Vectors from a Gaussian whose mean has a fixed norm $\lVert\mu\rVert$ are still near-orthogonal as $d$ grows, though proving it takes more work. But if every coordinate has the same non-zero mean, $\lVert\mu\rVert$ grows like $\sqrt{d}$, and the cosine converges to a non-zero constant. For example, vectors uniform on $[0, 1]^d$ have $\lVert\mu\rVert = 0.5\sqrt{d}$ and $\cos\theta \to 3/4$.
Strong anisotropy has a similar effect, as samples are stretched along a few directions. So the cosines are larger, and what matters then is the effective number of directions, $d_\mathrm{eff}$, rather than the ambient dimension $d$. The effective dimension is often estimated by the participation ratio as
\[d_\mathrm{eff} = \frac{(\mathrm{tr}\,\Sigma)^2}{\mathrm{tr}(\Sigma^2)} = \frac{(\sum_i \lambda_i)^2}{\sum_i \lambda_i^2},\]where $\Sigma$ is the covariance matrix of the embeddings and $\lambda_i$ are its eigenvalues. Real embeddings often show both of these effects. For example, it is known that CLIP’s image and text embeddings each occupy a narrow cone (Liang et al., 2022) and therefore a large shared mean direction, where even unrelated inputs have clearly positive cosines.
Closing remarks
Near-orthogonality is an interesting property that primarily arises from the exponentially expanding nature of high-dimensional space. Its connections to superposition and line packing are perhaps not obvious at first glance, but it is linked to many applications in machine learning that deal with high-dimensional inputs and representations.
-
A small aside: We could say this is by Markov’s inequality on $\cos^2\theta$, or Chebyshev’s inequality on $\cos\theta$, as they coincide given that we have zero mean here. Also note we’re dealing with a continuous distribution, so we can interchange $>$ and $\ge$. ↩
-
Even without a model, the manifolds of natural images themselves are believed to have a low intrinsic dimensionality. This is referred to as the Manifold hypothesis, and is also something we’ll explore in a later post. ↩
References
- Elhage, N., Hume, T., Olsson, C., Schiefer, N., Henighan, T., Kravec, S., Hatfield-Dodds, Z., Lasenby, R., Drain, D., Chen, C., & others. (2022). Toy models of superposition. ArXiv Preprint ArXiv:2209.10652.
- Ball, K. (1997). An Elementary Introduction to Modern Convex Geometry. In S. Levy (Ed.), Flavors of Geometry (Vol. 31, pp. 1–58). Cambridge University Press.
- Alon, N. (2003). Problems and results in extremal combinatorics—I. Discrete Mathematics, 273(1-3), 31–53.
- Oquab, M., Darcet, T., Moutakanni, T., Vo, H. V., Szafraniec, M., Khalidov, V., Fernandez, P., Haziza, D., Massa, F., El-Nouby, A., Assran, M., Ballas, N., Galuba, W., Howes, R., Huang, P.-Y., Li, S.-W., Misra, I., Rabbat, M., Sharma, V., … Bojanowski, P. (2024). DINOv2: Learning Robust Visual Features without Supervision. Transactions on Machine Learning Research. https://openreview.net/forum?id=a68SUt6zFt
- Donoho, D. L. (2000). High-dimensional data analysis: The curses and blessings of dimensionality. AMS Math Challenges Lecture, 1(2000), 32.
- Liang, V. W., Zhang, Y., Kwon, Y., Yeung, S., & Zou, J. (2022). Mind the gap: Understanding the modality gap in multi-modal contrastive representation learning. Advances in Neural Information Processing Systems, 35, 17612–17625.