# Johnson–Lindenstrauss lemma

In mathematics, the Johnson–Lindenstrauss lemma is a result named after William B. Johnson and Joram Lindenstrauss concerning low-distortion embeddings of points from high-dimensional into low-dimensional Euclidean space. The lemma states that a set of points in a high-dimensional space can be embedded into a space of much lower dimension in such a way that distances between the points are nearly preserved. In the classical proof of the lemma, the embedding is a random orthogonal projection.
The lemma has applications in compressed sensing, manifold learning, dimensionality reduction, graph embedding, and natural language processing. Much of the data stored and manipulated on computers, including text and images, can be represented as points in a high-dimensional space (see vector space model for the case of text). However, the essential algorithms for working with such data tend to become bogged down very quickly as dimension increases. It is therefore desirable to reduce the dimensionality of the data in a way that preserves its relevant structure.

## Statement

Given $0<\varepsilon <1$, a set $X$ of $N$ points in $\mathbb {R} ^{n}$ , and an integer $k>8(\ln N)/\varepsilon ^{2}$, there is a linear map $f:\mathbb {R} ^{n}\rightarrow \mathbb {R} ^{k}$ such that
    $(1-\varepsilon )\|u-v\|^{2}\leq \|f(u)-f(v)\|^{2}\leq (1+\varepsilon )\|u-v\|^{2}$
for all $u,v\in X$.
The formula can be rearranged:$(1+\varepsilon )^{-1}\|f(u)-f(v)\|^{2}\leq \|u-v\|^{2}\leq (1-\varepsilon )^{-1}\|f(u)-f(v)\|^{2}$
Alternatively, for any $\epsilon \in (0,1)$ and any integer $k\geq 15(\ln N)/\varepsilon ^{2}$ there exists a linear function $f:\mathbb {R} ^{n}\rightarrow \mathbb {R} ^{k}$ such that the restriction $f|_{X}$ is $(1+\varepsilon )$-bi-Lipschitz.
Also, the lemma is tight up to a constant factor, i.e. there exists a set of points of size N that needs dimension
    $\Omega \left({\frac {\log(N)}{\varepsilon ^{2}}}\right)$
in order to preserve the distances between all pairs of points within a factor of $(1\pm \varepsilon )$.
The classical proof of the lemma takes $f$ to be a scalar multiple of an orthogonal projection $P$ onto a random subspace of dimension $k$ in $\mathbb {R} ^{n}$. An orthogonal projection collapses some dimensions of the space it is applied to, which reduces the length of all vectors, as well as distance between vectors in the space. Under the conditions of the lemma, concentration of measure ensures there is a nonzero chance that a random orthogonal projection reduces pairwise distances between all points in $X$ by roughly a constant factor $c$. Since the chance is nonzero, such projections must exist, so we can choose one $P$ and set $f(v)=Pv/c$.
To obtain the projection algorithmically, it suffices with high probability to repeatedly sample orthogonal projection matrices at random. If you keep rolling the dice, you will eventually obtain one in polynomial random time.

## Proof

Based on.
Construct a random matrix $A\sim {\mathcal {N}}(0,1)^{k\times n}$, obtained by sampling each entry from the standard normal distribution. Then define $P:=A/{\sqrt {k}}$. Then, for any nonzero vector $x\in \mathbb {R} ^{n}$, let the projected vector be ${\hat {x}}:=Px$. Standard geometric argument show that $r:={\frac {\|{\hat {x}}\|^{2}}{\|x\|^{2}}}$ is chi-square distributed, that is, $r\sim \chi ^{2}(k)$. Thus, it satisfies a concentration inequality$Pr(r\in (1\pm \epsilon )k)\geq 1-2e^{-{\frac {k}{2}}({\frac {1}{2}}\epsilon ^{2}-{\frac {1}{3}}\epsilon ^{3})}$By the union bound, the probability that this relation is true for all of $x_{1},\dots ,x_{N}$ is greater than $1-2Ne^{-{\frac {k}{2}}({\frac {1}{2}}\epsilon ^{2}-{\frac {1}{3}}\epsilon ^{3})}$.
When $k\geq {\frac {4\ln 2N}{\epsilon ^{2}(1-2\epsilon /3)}}$, the probability is nonzero.
More generally, when $k\geq {\frac {4(d+1)\ln 2N}{\epsilon ^{2}(1-2\epsilon /3)}}$, the probability is $\geq 1-1/(2N)^{d}$, allowing arbitrarily high probability of success per sample, and a fortiori polynomial random time.

## Alternate statement

A related lemma is the distributional JL lemma. This lemma states that for any $0<\varepsilon ,\delta <1/2$ and positive integer $d$, there exists a distribution over $\mathbb {R} ^{k\times d}$ from which the matrix $A$ is drawn such that for $k=O(\varepsilon ^{-2}\log(1/\delta ))$ and for any unit-length vector $x\in \mathbb {R} ^{d}$, the claim below holds.
    $P(|\Vert Ax\Vert _{2}^{2}-1|>\varepsilon )<\delta$
One can obtain the JL lemma from the distributional version by setting $x=(u-v)/\|u-v\|_{2}$ and $\delta <1/n^{2}$ for some pair u,v both in X. Then the JL lemma follows by a union bound over all such pairs.

## Sparse JL transform
### Database-friendly JL transform

(Achlioptas, 2003) proposed "database-friendly" JL transform, using matrices with only entries from (-1, 0, +1).
Theorem (Achlioptas, 2003, Theorem 1.1) — Let the random k×n{\textstyle k\times n} projection matrix R{\textstyle R} have entries drawn i.i.d., either from
$R_{ij}={\begin{cases}+1&{\text{ with probability }}1/2\\-1&{\text{ with probability }}1/2\end{cases}}$
or from $R_{ij}={\begin{cases}+{\sqrt {3}}&{\text{ with probability }}1/6\\0&{\text{ with probability }}2/3\\-{\sqrt {3}}&{\text{ with probability }}1/6\end{cases}}$
Given a vector v{\textstyle v}, we define the random projection f(v)=1/√k Rv{\textstyle f(v)={\frac {1}{\sqrt {k}}}Rv}. Then for any vector v∈R$^n${\textstyle v\in \mathbb {R} ^{n}}, we have ${\begin{aligned}&-\ln Pr(\|f(v)\|_{2}^{2}\geq (1+\epsilon )\|v\|_{2}^{2})\geq {\frac {k}{2}}\left({\frac {\epsilon ^{2}}{2}}-{\frac {\epsilon ^{3}}{3}}\right)\quad &\forall \epsilon >0\\&-\ln Pr(\|f(v)\|_{2}^{2}\leq (1-\epsilon )\|v\|_{2}^{2})\geq {\frac {k}{2}}\left({\frac {\epsilon ^{2}}{2}}-{\frac {\epsilon ^{3}}{3}}\right)\quad &\forall \epsilon \in (0,1)\end{aligned}}$
Fix some unit vector v∈R$^n${\textstyle v\in \mathbb {R} ^{n}}. Define Q$_i$:=∑$_j$R$_{ij}$v$_j${\textstyle Q_{i}:=\sum _{j}R_{ij}v_{j}}. We have ‖f(v)‖$_2$$^2$=1/k ∑$_i$Q$_i$$^2${\textstyle \|f(v)\|_{2}^{2}={\frac {1}{k}}\sum _{i}Q_{i}^{2}}.
Now, since the Q$_1$, …, Q$_k${\textstyle Q_{1}, \dots , Q_{k}} are IID, we want to apply a Chernoff concentration bound for 1/k ∑$_i$Q$_i$$^2${\textstyle {\frac {1}{k}}\sum _{i}Q_{i}^{2}} around 1. This requires upper-bounding the cumulant generating function (CGF).
Moment bounds (Achlioptas, 2003, Section 6) — For any k∈1, 2, …{\textstyle k\in 1, 2, \dots }, the moment of Q$_i${\textstyle Q_{i}} is upper-bound by the standard gaussian Z∼N(0, 1){\textstyle Z\sim N(0, 1)}: $E[Q_{i}^{2k-1}]=0=E[Z^{2k-1}],\quad E[Q_{i}^{2k}]\leq E[Z^{2k}]$
E[Q$_i$$^{2k−1}$]=0{\textstyle E[Q_{i}^{2k-1}]=0} is easy: just apply the fact that E[R$_{ij1}$…R$_{ijl}$]=0{\textstyle E[R_{ij_{1}}\dots R_{ij_{l}}]=0} when l{\textstyle l} is odd, since we can decompose it into a product of expectations, and one of those is the expectation of an odd power of Radamacher, which is zero.
Now, the trick is that we can rewrite Z{\textstyle Z} as Z=∑$_i$Z$_i$v$_i${\textstyle Z=\sum _{i}Z_{i}v_{i}}, where each Z$_1$, …, Z$_d${\textstyle Z_{1}, \dots , Z_{d}} is a standard gaussian. Then we need to compare: $E[Q_{i}^{2k}]=\sum _{j_{1},j_{2},\dots ,j_{2k-1},j_{2k}}E[R_{ij_{1}}R_{ij_{2}}\dots R_{ij_{2k-1}}R_{ij_{2k}}]v_{j_{1}}v_{j_{2}}\dots v_{j_{2k-1}}v_{j_{2k}}$ and
$E[Z^{2k}]=\sum _{j_{1},j_{2},\dots ,j_{2k-1},j_{2k}}E[Z_{j_{1}}Z_{j_{2}}\dots Z_{j_{2k-1}}Z_{j_{2k}}]v_{j_{1}}v_{j_{2}}\dots v_{j_{2k-1}}v_{j_{2k}}$
In the top sum, a term $E[R_{ij_{1}}R_{ij_{2}}\dots R_{ij_{2k-1}}R_{ij_{2k}}]v_{j_{1}}v_{j_{2}}\dots v_{j_{2k-1}}v_{j_{2k}}$ decomposes into a product of expectations, times v$_{j1}$v$_{j2}$…v$_{j2k−1}$v$_{j2k}${\textstyle v_{j_{1}}v_{j_{2}}\dots v_{j_{2k-1}}v_{j_{2k}}}. The product of expectations is zero, unless the indices j$_1$, j$_2$, …, j$_{2k}${\textstyle j_{1}, j_{2}, \dots , j_{2k}} are paired off. In that case, the term v$_{j1}$v$_{j2}$…v$_{j2k−1}$v$_{j2k}${\textstyle v_{j_{1}}v_{j_{2}}\dots v_{j_{2k-1}}v_{j_{2k}}} is the square of something, and so
$v_{j_{1}}v_{j_{2}}\dots v_{j_{2k-1}}v_{j_{2k}}\geq 0$ while R$_{ij1}$R$_{ij2}$…R$_{ij2k−1}$R$_{ij2k}${\textstyle R_{ij_{1}}R_{ij_{2}}\dots R_{ij_{2k-1}}R_{ij_{2k}}} is also the square of ±1{\textstyle \pm 1}, and so
$E[R_{ij_{1}}R_{ij_{2}}\dots R_{ij_{2k-1}}R_{ij_{2k}}]=1$
In the bottom sum, we run a similar argument with each such term $E[Z_{j_{1}}Z_{j_{2}}\dots Z_{j_{2k-1}}Z_{j_{2k}}]v_{j_{1}}v_{j_{2}}\dots v_{j_{2k-1}}v_{j_{2k}}$ but in this case, since we have E[Z$^{2k}$]=(2k−1)!!≥1{\textstyle E[Z^{2k}]=(2k-1)!!\geq 1}, we find that in each case,
$E[R_{ij_{1}}R_{ij_{2}}\dots R_{ij_{2k-1}}R_{ij_{2k}}]v_{j_{1}}v_{j_{2}}\dots v_{j_{2k-1}}v_{j_{2k}}\leq E[Z_{j_{1}}Z_{j_{2}}\dots Z_{j_{2k-1}}Z_{j_{2k}}]v_{j_{1}}v_{j_{2}}\dots v_{j_{2k-1}}v_{j_{2k}}$
And so, summing all of them up, E[Q$_i$$^{2k}$]≤E[Z$^{2k}$]{\textstyle E[Q_{i}^{2k}]\leq E[Z^{2k}]}.
The same argument works for the other case. Specifically, if R$_{ij}${\textstyle R_{ij}} is distributed like that, then E[R$_{ij}$$^{2k}$]=3$^{k−1}$≤(2k−1)!!{\textstyle E[R_{ij}^{2k}]=3^{k-1}\leq (2k-1)!!}, and the proof goes through exactly the same way.
Now that Q$_i${\textstyle Q_{i}} is stochastically dominated by the standard gaussian, and E[Q$_i$$^2$]=1{\textstyle E[Q_{i}^{2}]=1}, it remains to perform a Chernoff bound for Q$_i$$^2${\textstyle Q_{i}^{2}}, which requires bounding the cumulant generating function on both ends.
For any t∈(0, 1/2){\textstyle t\in (0, 1/2)}, we can compute the cumulant generating function ${\begin{aligned}K_{Q_{i}^{2}}(t)&=\ln E[e^{Q_{i}^{2}t}]\\&=\ln \sum _{k}{\frac {t^{k}}{k!}}E[Q_{i}^{2k}]\\&\leq \ln \left(1+\sum _{k}{\frac {t^{k}}{k!}}(2k-1)!!\right)\\&=-{\frac {1}{2}}\ln(1-2t)\end{aligned}}$
Similarly, for any t∈(0, k/2){\textstyle t\in (0, k/2)}, $K_{{\frac {1}{k}}\sum _{i}Q_{i}^{2}}(t)=\sum _{i}K_{Q_{i}^{2}}(t/k)\leq -{\frac {k}{2}}\ln(1-2t/k)$
So by the standard Chernoff bound method, for any t∈(0, k/2){\textstyle t\in (0, k/2)} and any ϵ>0{\textstyle \epsilon >0}, $-\ln Pr\left({\frac {1}{k}}\sum _{i}Q_{i}^{2}\geq 1+\epsilon \right)\geq (1+\epsilon )t+{\frac {k}{2}}\ln(1-2t/k)$
The right side is maximized at t=kϵ/(2(1+ϵ)) {\textstyle t={\frac {k\epsilon }{2(1+\epsilon )}}}, at which point we have $-\ln Pr\left({\frac {1}{k}}\sum _{i}Q_{i}^{2}\geq 1+\epsilon \right)\geq {\frac {k}{2}}(\epsilon -\ln(1+\epsilon ))\geq {\frac {k}{2}}(\epsilon ^{2}/2-\epsilon ^{3}/3)$
That’s one half of the bound done. For the other half, begin with some t>0{\textstyle t>0}, and expand the exponential to the second order: ${\begin{aligned}K_{Q_{i}^{2}}(-t)&=\ln E[e^{-Q_{i}^{2}t}]\\&\leq \ln E[1-Q_{i}^{2}t+Q_{i}^{4}t^{2}/2]\\&\leq \ln(1-t+3t^{2}/2)\\\end{aligned}}$
$K_{{\frac {1}{k}}\sum _{i}Q_{i}^{2}}(-t)\leq k\ln(1-t/k+3t^{2}/(2k^{2}))$
So by the standard Chernoff bound method, for any t>0{\textstyle t>0} and any ϵ∈(0, 1){\textstyle \epsilon \in (0, 1)}, $-\ln Pr\left({\frac {1}{k}}\sum _{i}Q_{i}^{2}\leq 1-\epsilon \right)\geq -k[(1-\epsilon )(t/k)+\ln(1-t/k+3t^{2}/(2k^{2}))]$
Plug in t=kϵ/(2(1+ϵ)) {\textstyle t={\frac {k\epsilon }{2(1+\epsilon )}}}, and simplify, we find the right side is $\geq k\left({\frac {(\epsilon -1)\epsilon }{2(\epsilon +1)}}-\ln \left({\frac {7\epsilon ^{2}+12\epsilon +8}{8(\epsilon +1)^{2}}}\right)\right)$ and expand to third Taylor power,
$\geq k(\epsilon ^{2}/4-7\epsilon ^{3}/48)>{\frac {k}{2}}(\epsilon ^{2}/2-\epsilon ^{3}/3)$

### Sparser JL transform on well-spread vectors

(Matoušek, 2008) proposed a variant of the above JL transform that is even more sparsified, though it only works on "well-spread" vectors.
Theorem (Matoušek 2008, Theorem 4.1) — Define n∈N, ϵ∈(0, 1/2), δ∈(0, 1), α∈[n$^{−1/2}$, 1], q∈[C$_0$α$^2$ln⁡(n/ϵδ), 1], k∈[C$_1$ϵ$^{−2}$ln⁡ 4/δ, n]{\textstyle n\in \mathbb {N} , \epsilon \in (0, 1/2), \delta \in (0, 1), \alpha \in [n^{-1/2}, 1], q\in [C_{0}\alpha ^{2}\ln(n/\epsilon \delta ), 1], k\in [C_{1}\epsilon ^{-2}\ln {\frac {4}{\delta }}, n]}, where C$_0$, C$_1${\textstyle C_{0}, C_{1}} are absolute constants.
Let R{\textstyle R} be a k×n{\textstyle k\times n} matrix sampled IID with
$R_{ij}={\begin{cases}+q^{-1/2}&{\text{ with probability }}{\frac {1}{2}}q\\-q^{-1/2}&{\text{ with probability }}{\frac {1}{2}}q\\0&{\text{ with probability }}1-q\end{cases}}$
Then, for any unit vector v∈R$^n${\textstyle v\in \mathbb {R} ^{n}} such that ‖v‖$_∞$≤α{\textstyle \|v\|_{\infty }\leq \alpha }, we have $Pr(\|f(v)\|_{2}^{2}\in [1\pm \epsilon ])\geq 1-\delta$
where $f(v)={\frac {1}{\sqrt {k}}}Rv$.
The above cases are generalized to the case for matrices with independent, mean-zero, unit variance, subgaussian entries in (Dirksen, 2016).

## Speeding up the JL transform

Given A, computing the matrix vector product takes $O(kd)$ time. There has been some work in deriving distributions for which the matrix vector product can be computed in less than $O(kd)$ time.
There are two major lines of work. The first, Fast Johnson Lindenstrauss Transform (FJLT), was introduced by Ailon and Chazelle in 2006. This method allows the computation of the matrix vector product in just $d\log d+k^{2+\gamma }$ for any constant $\gamma >0$.
Another approach is to build a distribution supported over matrices that are sparse. This method allows keeping only an $\varepsilon$ fraction of the entries in the matrix, which means the computation can be done in just $kd\varepsilon$ time. Furthermore, if the vector has only $b$ non-zero entries, the Sparse JL takes time $kb\varepsilon$, which may be much less than the $d\log d$ time used by Fast JL.

## Tensorized random projections

It is possible to combine two JL matrices by taking the so-called face-splitting product, which is defined as the tensor products of the rows (was proposed by V. Slyusar in 1996 for radar and digital antenna array applications). More directly, let ${C}\in \mathbb {R} ^{3\times 3}$ and ${D}\in \mathbb {R} ^{3\times 3}$ be two matrices. Then the face-splitting product ${C}\bullet {D}$ is
    ${C}\bullet {D}=\left[{\begin{array}{c }{C}_{1}\otimes {D}_{1}\\\hline {C}_{2}\otimes {D}_{2}\\\hline {C}_{3}\otimes {D}_{3}\\\end{array}}\right].$
This idea of tensorization was used by Kasiviswanathan et al. for differential privacy.
JL matrices defined like this use fewer random bits, and can be applied quickly to vectors that have tensor structure, due to the following identity:
    $(\mathbf {C} \bullet \mathbf {D} )(x\otimes y)=\mathbf {C} x\circ \mathbf {D} y=\left[{\begin{array}{c }(\mathbf {C} x)_{1}(\mathbf {D} y)_{1}\\(\mathbf {C} x)_{2}(\mathbf {D} y)_{2}\\\vdots \end{array}}\right]$,
where $\circ$ is the element-wise (Hadamard) product. Such computations have been used to efficiently compute polynomial kernels and many other linear-algebra algorithms.
In 2020 it was shown that if the matrices $C_{1},C_{2},\dots ,C_{c}$ are independent $\pm 1$ or Gaussian matrices, the combined matrix $C_{1}\bullet \dots \bullet C_{c}$ satisfies the distributional JL lemma if the number of rows is at least
    $O(\epsilon ^{-2}\log 1/\delta +\epsilon ^{-1}({\tfrac {1}{c}}\log 1/\delta )^{c})$.
For large $\epsilon$ this is as good as the completely random Johnson-Lindenstrauss, but a matching lower bound in the same paper shows that this exponential dependency on $(\log 1/\delta )^{c}$ is necessary. Alternative JL constructions are suggested to circumvent this.