Tutorials › Statistical Methods for Single-Cell RNA Sequencing › A Worked t-SNE Example With Toy Cells

Dimension Reduction and Embeddings · Tutorial 215 of 700

A Worked t-SNE Example With Toy Cells

Follow a small t-SNE example from high-dimensional distances through pairwise probabilities to neighborhood interpretation in a two-dimensional map.

Intermediate 12 min read

What You'll Learn

  • Calculate Gaussian-kernel conditional similarities from distances and a bandwidth.
  • Check how conditional probabilities are normalized for each cell.
  • Symmetrize conditional similarities to obtain joint high-dimensional probabilities.
  • Compare those probabilities with low-dimensional t-SNE similarities.
  • Interpret neighborhood preservation without treating map distances as original distances.
  • Recognize when a toy embedding is illustrative rather than evidence of biological clusters.

From Cell Distances to a Map

t-SNE turns pairwise distances among cells into a two-dimensional map by trying to preserve local neighborhood relationships. A map is therefore not a direct picture of the original expression space: it is an arrangement whose pairwise similarities are compared with similarities calculated from the high-dimensional data. A small calculation makes the steps—and the limits of the interpretation—concrete.

For a toy example, call four cells A, B, C, and D. Their feature vectors could have many coordinates, as gene-expression profiles do. We will use specified pairwise distances rather than list every coordinate. The numbers are hypothetical and are chosen to make the probability calculations transparent.

Definition: Given high-dimensional distances \(d_{ij}\), t-SNE assigns each ordered pair of distinct cells a conditional similarity \(p_{j\mid i}\), using a Gaussian kernel centered on cell \(i\). The bandwidth \(\sigma_i\) controls how quickly similarity decreases with distance from that cell: $$ p_{j\mid i}= \frac{\exp\!\left(-d_{ij}^{2}/(2\sigma_i^{2})\right)} {\sum_{k\ne i}\exp\!\left(-d_{ik}^{2}/(2\sigma_i^{2})\right)}. $$ The probabilities in each row sum to one. The joint high-dimensional probabilities are \(P_{ij}=(p_{j\mid i}+p_{i\mid j})/(2n)\) for \(i\ne j\), with \(P_{ii}=0\). Thus \(P_{ij}=P_{ji}\), and the sum over all ordered pairs is one.

The bandwidth is cell-specific in the standard formulation. In ordinary t-SNE, it is selected so that the conditional distribution around each cell has a target perplexity. Here we specify a bandwidth directly to keep the arithmetic visible; we are not claiming that this value solves a particular perplexity target.

A Conditional Similarity Calculation

First consider cell A. Suppose the squared distances from A to B, C, and D, together with A’s bandwidth, give Gaussian weights \(2^{-1}\), \(2^{-4}\), and \(2^{-9}\), respectively. These are unnormalized weights: they need not sum to one. Normalize them by their sum before interpreting them as conditional probabilities.

Worked Example: Normalizing A’s Neighborhood

The three weights are \(0.5\), \(0.0625\), and \(0.001953125\). Their sum is \(0.564453125\). Therefore,

$$ \begin{aligned} p_{B\mid A} &=\frac{0.5}{0.564453125} =\frac{256}{289} \approx 0.8858,\\ p_{C\mid A} &=\frac{0.0625}{0.564453125} =\frac{32}{289} \approx 0.1107,\\ p_{D\mid A} &=\frac{0.001953125}{0.564453125} =\frac{1}{289} \approx 0.003460. \end{aligned} $$

The probabilities sum to one: \((256+32+1)/289=1\). Under this conditional distribution, B is A’s strongest neighbor, C has a smaller but nonzero similarity, and D has very little similarity. These are relative weights within A’s row, not probabilities that the cells belong to the same biological type.

A conditional row is directional because its denominator depends on the center cell’s bandwidth and distances to all other cells. It is possible for \(p_{j\mid i}\) to differ from \(p_{i\mid j}\). Symmetrization combines the two directions and divides by \(2n\), ensuring the joint probabilities across all ordered pairs sum to one.

Symmetrizing the Toy Similarities

To carry the example through to a joint distribution, suppose the other conditional rows are chosen so that this deliberately simple toy table is symmetric. The table uses the exact row-A probabilities just calculated; the remaining rows complete a valid set of conditional distributions. Each row sums to one. This is a hypothetical probability setup for illustrating symmetrization, not a claim that all four rows came from the same equally spaced feature vectors.

Center cell \(i\)To ATo BTo CTo D
A—256/28932/2891/289
B256/289—1/28932/289
C32/2891/289—256/289
D1/28932/289256/289—

For \(n=4\), the denominator in the symmetrization formula is \(2n=8\). For example, A and D have equal conditional probabilities in this constructed table, so

$$ P_{AD} =\frac{p_{D\mid A}+p_{A\mid D}}{8} =\frac{2(1/289)}{8} \approx 0.0008651. $$

The other unordered-pair values follow the same calculation. Each value appears twice in the ordered sum, once as \(P_{ij}\) and once as \(P_{ji}\); the six unordered-pair probabilities therefore sum to \(0.5\), and the twelve ordered off-diagonal entries sum to one.

Worked Example: Reading the Symmetric Similarities

The exact conditional weights yield the following joint probabilities for unordered pairs. Each entry is computed by averaging the two conditional directions and dividing by \(n=4\), equivalently by dividing their sum by 8.

Unordered pairCalculation\(P_{ij}\)
A, B\(2(256/289)/8\)0.2215
A, C\(2(32/289)/8\)0.02768
A, D\(2(1/289)/8\)0.0008651
B, C\(2(1/289)/8\)0.0008651
B, D\(2(32/289)/8\)0.02768
C, D\(2(256/289)/8\)0.2215

The entries for A–B and C–D are largest: these are the pairs the high-dimensional distribution emphasizes most. A–C and B–D receive less weight, while A–D and B–C receive very little. The displayed values are rounded; the exact probabilities, rather than their rounded display, should be used in later calculations.

Comparing the High- and Low-Dimensional Distributions

In a two-dimensional t-SNE map, let the coordinate of cell \(i\) be \(y_i\). The low-dimensional similarity uses a Student \(t\) distribution with one degree of freedom, also called the Cauchy kernel:

$$ q_{ij}= \frac{(1+\lVert y_i-y_j\rVert^2)^{-1}} {\sum_{k\ne l}(1+\lVert y_k-y_l\rVert^2)^{-1}}, \qquad q_{ii}=0. $$

The sum in the denominator is over ordered pairs. The heavy-tailed kernel lets cells that are not neighbors in the high-dimensional similarities be placed relatively far apart without making their low-dimensional similarities vanish as quickly as a Gaussian kernel would.

For a numerical comparison, suppose we use the following hypothetical normalized low-dimensional pair probabilities. The values are for unordered pairs; each occurs in both directions in the ordered-pair distribution, so the six entries sum to \(0.5\). This abstract probability distribution need not be realizable by any two-dimensional map under the stated Student kernel.

Pair classPairsLow-dimensional \(q_{ij}\) for each pairTotal ordered-pair mass
HighA–B, C–D0.177250.709
MiddleA–C, B–D0.060250.241
LowA–D, B–C0.012500.050

For example, the two ordered directions for A–B and the two for C–D contribute \(4(0.17725)=0.709\) to the ordered \(Q\) mass. Together the three classes account for \(0.709+0.241+0.050=1\).

Worked Example: Calculating the t-SNE Objective

The t-SNE objective is the Kullback–Leibler divergence from the high-dimensional joint distribution \(P\) to the low-dimensional distribution \(Q\): \(KL(P\Vert Q)=\sum_{i\ne j}P_{ij}\ln(P_{ij}/q_{ij})\). It penalizes map similarities that are too small for pairs emphasized by \(P\). Because the distributions are symmetric, the ordered sum is twice the sum over the six unordered pairs.

The exact ordered mass for each pair class under \(P\) is \(256/289\) for the high class, \(32/289\) for the middle class, and \(1/289\) for the low class. Using the stated \(Q\) masses gives

$$ \begin{aligned} KL(P\Vert Q) &=\frac{256}{289}\ln\!\left(\frac{256/289}{0.709}\right) +\frac{32}{289}\ln\!\left(\frac{32/289}{0.241}\right) +\frac{1}{289}\ln\!\left(\frac{1/289}{0.050}\right)\\ &\approx 0.1019\ \text{nats}. \end{aligned} $$

This is the objective value for the specified toy distributions, not a measure of biological separation. If the map assigned too little \(Q\) probability to A–B or C–D, their positive contributions to the divergence would increase. The optimizer seeks map coordinates that reduce the objective, but a low value alone does not establish that the map is biologically meaningful.

What the Map Preserves—and What It Does Not

The toy similarities emphasize A–B and C–D, so a map in which those pairs are close is consistent with the dominant local relationships in \(P\). A–C and B–D carry weaker affinity, and A–D and B–C carry very little. This describes what the pairwise distributions say; it does not prove that the four cells form two distinct cell types or that the apparent neighborhoods correspond to a causal biological process.

The objective and the map are built to emphasize local relationships, not to preserve every original distance. In particular, the gap between two groups on the page is not a calibrated estimate of their separation in the original expression space. A cluster’s apparent area or density is not a direct estimate of how many high-dimensional neighbors its cells have, either. Read an embedding alongside the underlying measurements and the analysis choices that produced it.

The earlier material on distance geometry and t-SNE pairwise similarities supplies the broader context: the distance metric and feature representation determine which cells count as neighbors before the map is optimized. This calculation isolates the probability and interpretation steps; it does not settle the choice of input features, metric, perplexity, or optimization settings.

Assumptions, Diagnostics, and Alternatives

The formulas require finite, nonnegative pairwise distances and positive bandwidths. For each cell, the Gaussian weights must be normalized across the other cells. The joint probabilities must be symmetric, nonnegative, and sum to one over ordered pairs. The low-dimensional distribution likewise requires a finite normalization sum. These checks are simple but important: a missing cell, a self-pair accidentally included in a denominator, or a normalization over unordered pairs where ordered pairs are required can change the result.

Practical warning signs include a conditional row that does not sum to one, non-finite weights caused by numerical underflow, a map with many cells overlapping, and results that change substantially when the random seed or settings change. Underflow can be mitigated by subtracting the largest log weight before exponentiating; this rescales all weights in the row by the same positive constant and therefore leaves the normalized probabilities unchanged. A visually compelling map is not a diagnostic that the selected neighbors or biological interpretation are correct.

PCA is a useful alternative when a linear projection and its variance-based summaries are important: it provides a direct projection of the input matrix onto principal components. t-SNE instead optimizes a neighborhood-based objective and can represent nonlinear local structure, but its coordinates are not linear scores and it does not preserve global distances in a straightforward way. The methods answer different questions; neither embedding should be treated as a substitute for checking the input representation and the scientific context.

Common pitfall: “A and D are far apart on the t-SNE map, so their original expression profiles must be far apart by the same amount.” This conclusion is not justified. The map’s coordinates are chosen to match pairwise similarity probabilities, not to retain a common distance scale. Interpret local relationships through the analysis and use the original-space distances for claims about original-space separation.

A hand calculation is useful for checking normalization and understanding how probabilities change, but four cells cannot establish whether an embedding is stable or whether a neighborhood reflects a robust biological pattern. In real single-cell analysis, inspect whether relevant neighborhoods persist under reasonable changes in inputs and settings, and examine cell annotations or other evidence independently of the plotted geometry.

Check Your Understanding

Try each question first, then compare your reasoning with the short solutions below.

  1. Why is \(p_{B\mid A}\) not automatically equal to \(p_{A\mid B}\)?
  2. For four cells, suppose \(p_{D\mid A}=0.02\) and \(p_{A\mid D}=0.06\). Calculate \(P_{AD}\).
  3. Why must the off-diagonal ordered \(P_{ij}\) values sum to one, even though the unordered pair values sum to \(0.5\)?
  4. What does a larger \(P_{AB}\) than \(P_{AD}\) say in the toy example, and what does it not establish?
  5. Why should a distance between two groups in a t-SNE map not be interpreted as a calibrated distance in the original expression space?

Solutions. (1) The two conditional probabilities use different centers and can have different bandwidths and normalizing denominators. (2) \(P_{AD}=(0.02+0.06)/(2\cdot4)=0.01\). (3) Each unordered pair contributes two equal ordered entries, so the ordered sum is twice the unordered sum. (4) A–B has greater joint similarity under the specified high-dimensional distribution; this does not establish cell-type membership, causation, or a biological boundary. (5) t-SNE optimizes pairwise similarity probabilities rather than preserving a common scale for global distances.

Clinical Biostats