Perplexity Measures the Spread of a Cell’s Conditional Neighbors
In t-SNE, each cell \(i\) is associated with a conditional probability distribution over the other cells. The distribution gives higher probability to cells that are closer to \(i\) in the original space. Perplexity summarizes how spread out that distribution is: it is the effective number of neighbors receiving probability mass, not a literal count of neighbors selected by a cutoff.
The bandwidth is adjusted separately for each cell so that these conditional distributions have a specified perplexity. This matters when cells occupy regions with different distance scales. A single bandwidth could make neighborhoods too concentrated in one region and too diffuse in another.
If the probability is uniform over exactly \(k\) cells and zero elsewhere, then \(H_i=\log_2 k\), so \(\operatorname{Perp}_i=k\). For a nonuniform distribution, perplexity is the number of equally likely neighbors that would give the same entropy. It is generally not an integer.
Bandwidth Controls Entropy
Let \(d_{ij}^2\) denote the squared distance between cells \(i\) and \(j\) in the representation used to construct t-SNE’s input similarities. Excluding the self-comparison \(j=i\), the conditional probabilities have the Gaussian-weight form
Here \(\sigma_i>0\) is the bandwidth for cell \(i\), and \(\beta_i\) is its inverse-variance parameter. Increasing \(\beta_i\) (equivalently, decreasing \(\sigma_i\)) makes differences in distance matter more, concentrating probability on the nearest cells. Decreasing \(\beta_i\) spreads probability more evenly. The target perplexity \(P^*\) is set by the analyst; the algorithm seeks a bandwidth for each cell whose conditional distribution has perplexity near \(P^*\).
The limits describe the attainable range. With distinct nearest distances, perplexity approaches 1 at the concentrated end, but does not equal 1 for a finite bandwidth when there are other cells at finite distances. Tied nearest cells can make the smallest attainable perplexity greater than 1.
Why Binary Search Works
The monotonicity of entropy provides the basis for binary search. To see it, write \(q_j=d_{ij}^2\), \(Z(\beta)=\sum_{j\ne i}\exp(-\beta q_j)\), and use natural-log entropy temporarily. The resulting entropy in bits is the natural-log entropy divided by \(\ln 2\).
Differentiating \(\ln Z\) gives \(-\mathbb{E}_{p_\beta}[q]\), while differentiating the probability-weighted mean distance gives \(-\operatorname{Var}_{p_\beta}(q)\). Therefore,
For \(\beta>0\), the derivative is strictly negative whenever the squared distances are not all equal. Thus a trial value of \(\beta\) gives an entropy that can be compared with the target \(\log_2 P^*\). If the trial entropy is too high, increase \(\beta\); if it is too low, decrease \(\beta\). Repeatedly halving a bracket around the target is a straightforward binary search.
For target perplexity \(P^*\), the target entropy is \(H^*=\log_2 P^*\) bits.
Calculate the normalized conditional probabilities and their entropy for a trial \(\beta_i\).
If \(H_i>H^*\), raise \(\beta_i\); if \(H_i<H^*\), lower it. Monotonicity ensures the target is bracketed when it is attainable.
Stop when the entropy or perplexity is sufficiently close to its target, or when a prespecified iteration limit is reached.
Implementations often search in \(\beta_i\) or in \(\log \beta_i\), and use numerical tolerances rather than requiring exact equality. The search is performed for each cell because each cell has its own set of distances.
Worked Example: Searching for Perplexity Two
Consider a hypothetical cell A with three other cells at squared distances \(0,1,2\). Its unnormalized conditional weights are \(1,e^{-\beta},e^{-2\beta}\). The target perplexity is \(P^*=2\), so the target entropy is \(\log_2 2=1\) bit.
At \(\beta=0\), the weights are equal, each probability is \(1/3\), and the entropy is \(\log_2 3\approx1.585\) bits. The perplexity is 3, above target. Try \(\beta=\ln 4\). The weights are \(1,1/4,1/16\); dividing them by their sum \(21/16\) gives probabilities \(16/21,4/21,1/21\). Their entropy is
Thus the perplexity is \(2^{0.9637}\approx1.950\), below target. Since the entropy decreases with \(\beta\), the solution lies between \(0\) and \(\ln 4\). A further trial at \(\beta=1.326\) gives probabilities approximately \(0.7485,0.1987,0.0528\), entropy about \(1.0001\) bits, and perplexity about \(2.0001\). This is sufficiently close to the target for a typical numerical tolerance.
How Bandwidths Adapt to Local Distance Scales
A fixed target perplexity does not imply a fixed bandwidth. A cell in a dense region may have small distances to its neighbors, while a cell in a sparse region may have much larger distances. To obtain similarly concentrated probability distributions, the dense-region cell will generally need a smaller \(\sigma_i\), and the sparse-region cell a larger \(\sigma_i\). This is adaptation to local distance scale, not a direct estimate of cell density.
Worked Example: Rescaling Distances Changes the Bandwidth
Use the hypothetical distances for cell A above, \(d^2=(0,1,2)\), and the approximate target-setting value \(\beta_A=1.326\). Now consider cell B with squared distances \(d^2=(0,4,8)\), four times as large in every entry. Set \(\beta_B=1.326/4=0.3315\). Then the exponents for cell B are \(0,-1.326,-2.652\), exactly the exponents for cell A at \(\beta_A=1.326\). Their normalized probabilities, entropy, and perplexity therefore match: approximately \(0.7485,0.1987,0.0528\), \(1.0001\) bits, and \(2.0001\).
Since \(\sigma=1/\sqrt{2\beta}\), these parameters correspond to \(\sigma_A\approx0.6141\) and \(\sigma_B\approx1.228\), in the distance units used for the example. Cell B’s distances are twice as large, and its bandwidth is twice as large; the relative probability pattern is unchanged. This scaling illustrates how a per-cell bandwidth accommodates different local distance scales. It does not imply that real neighborhoods differ only by a single scale factor.
The target perplexity is a tuning choice, not a guarantee of a particular biological neighborhood. Conditional probabilities depend on the supplied representation, distance metric, and cell set. Even when two cells have the same perplexity, their probability mass can be distributed over different cells and with different shapes.
Feasibility, Diagnostics, and Alternatives
Before trusting a binary search, check whether the target is attainable for each cell. If there are \(n\) cells, each conditional distribution has \(n-1\) candidate neighbors, so its perplexity cannot exceed \(n-1\). At the concentrated end, ties among nearest cells can impose a lower limit above 1. For example, if two cells are tied at the minimum distance, their limiting probabilities are \(1/2\) each and the limiting perplexity is 2.
Worked Example: A Target Made Infeasible by a Tie
Suppose a hypothetical cell has three candidate neighbors at squared distances \(0,0,1\), and the requested perplexity is \(1.5\). For any finite \(\beta>0\), the unnormalized weights are \(1,1,t\), where \(t=e^{-\beta}>0\). The first two probabilities are equal. Their total probability is \(2/(2+t)\), and the third probability is \(t/(2+t)\).
The entropy can be written as the entropy of the two groups (the tied pair and the third cell), plus the one-bit entropy within the tied pair:
where \(h_2(u)=-u\log_2u-(1-u)\log_2(1-u)\). The inequality holds because \(0<t\), so both groups have positive probability and \(h_2(2/(2+t))>0\). As \(\beta\to\infty\), \(t\to0\), the entropy approaches 1 bit and perplexity approaches 2. A target of 1.5 is therefore infeasible: increasing \(\beta\) cannot achieve it. A search that reports a boundary value or fails to reach tolerance should prompt checks for tied distances, target limits, and numerical handling of extremely small weights.
Other useful diagnostics include checking that probabilities sum to 1, that the reported perplexity is close to the target, and that the fitted bandwidths are finite and positive. A very large or very small bandwidth can reflect unusual distances, duplicates, a target near a limit, or preprocessing problems. Underflow in \(\exp(-\beta_i d_{ij}^2)\) can also make some weights numerically zero; stable implementations rescale exponents or use log-probability calculations.
A fixed Gaussian bandwidth is a simple alternative, but it does not adapt to different local distance scales. A fixed-\(k\) nearest-neighbor rule instead selects a number of neighbors, whereas perplexity controls entropy: with nonuniform probabilities, its effective number can differ from the number of neighbors with appreciable weight. Neither choice removes the need to inspect distances and assess whether the resulting neighborhoods are appropriate. Perplexity is not a cluster count, a hard radius, or proof that cells form discrete populations.
Check Your Understanding
Answer each question, then compare with the short solutions below.
- If a cell’s conditional probabilities are uniform over eight other cells, what are its entropy and perplexity?
- For distinct squared distances, what should happen to perplexity when \(\beta_i\) increases?
- A data set has 20 cells. What is the maximum possible conditional perplexity for any cell?
- Why can two tied nearest neighbors prevent a target perplexity below 2?
Solutions. (1) The entropy is \(-8(1/8)\log_2(1/8)=3\) bits, so perplexity is \(2^3=8\). (2) It decreases: \(dH_i/d\beta_i=-\beta_i\operatorname{Var}(d_{ij}^2)/\ln 2<0\) when the distances are not all equal and \(\beta_i>0\). (3) There are only \(20-1=19\) candidate neighbors, and entropy is at most \(\log_2 19\), so perplexity is at most 19. (4) As \(\beta_i\) grows, probability concentrates equally on the tied nearest pair; the limiting distribution has entropy 1 bit and perplexity \(2^1=2\). Finite bandwidth leaves some probability on other cells, so the perplexity is above 2 rather than below it.