MATH 4310 · Computational Bioinformatics
Single-Cell Data and Embeddings
Lesson 20 · 2027-04-07 · Week 13
Core question
Which cells show similar RNA expression, and how faithfully can a two-dimensional picture display those relationships?
Seeing the cells inside a tissue
A mouse retina contains rods, cones, bipolar cells, and several other neuron and support-cell classes, each with a different role in turning light into a nerve signal. A bulk RNA measurement grinds the tissue together and reports one average profile, so it cannot say which genes are used by which cells. Single-cell RNA sequencing measures RNA separately in each of thousands of cells. Figure 20.1 shows the kind of picture such an experiment produces: 44,808 retina cells measured by the droplet method Drop-seq,1 each drawn as a dot, positioned by a method called t-SNE, and colored by the cell class the original study assigned.

Figure 20.1. A t-SNE embedding of 44,808 mouse retina cells from a Drop-seq experiment, colored by the cell classes assigned in the original study; single letters mark smaller groups named there. Cells of one class form compact islands, while the axes, island areas, and gaps have no units. Reproduced, cropped to panel a, from Kobak and Berens2 (CC BY 4.0); data from Macosko et al.1.
Reading such a picture requires knowing how the experiment turned cells into numbers and how the method turned those numbers into positions. Lesson 19 compared cells through distances and neighbor graphs; t-SNE uses the same neighbor relationships to place every cell in the plane, and some features of the result are evidence about the cells while others are not.
From droplets to a count matrix
In Drop-seq, a microfluidic device encloses single cells with single beads in nanoliter droplets. Each bead carries many DNA capture strands. All strands on one bead share a cell barcode sequence that differs between beads, and each strand also carries its own random unique molecular identifier (UMI). Inside a droplet the cell bursts, its messenger RNA sticks to the bead’s strands, and copying each captured RNA into DNA attaches the barcode and one UMI to it. The droplets are then broken, and everything is amplified and sequenced together.1
Each read therefore records a gene (by matching to gene sequences, as in Lesson 15), a cell (by its barcode), and a captured molecule (by its UMI). Amplification makes many reads from one molecule, all sharing barcode, gene, and UMI, so counting distinct UMIs rather than reads counts molecules. Figure 20.2 follows two toy cells through this bookkeeping. Cell A contributes three molecules from gene 1 and one from gene 2, cell B the reverse, and their average row \((2,2)\) would hide both patterns.
Figure 20.2. A toy capture-and-tag schematic connects two cells’ RNA molecules to barcode-labeled records and count rows. Retaining cell identity reveals expression differences hidden by the average row (2, 2).
The result is a count matrix \(C\in\mathbb Z_{\geq0}^{n\times p}\), where \(C_{ij}\) is the number of distinct UMIs for gene \(j\) in cell \(i\). Cells take the rows that samples occupied in Lessons 15 and 16, and an experiment can have thousands to millions of them.
Why most entries are zero
A cell yields relatively few captured molecules, spread over thousands of genes. Recall Lesson 16’s model \(\mathbb E[C_{ij}\mid L_i]=L_i\rho_{ij}\), with \(L_i=\sum_jC_{ij}\) the cell’s total and \(\rho_{ij}\) the expected fraction from gene \(j\). Suppose the cell holds \(N\) copies of gene \(j\)’s RNA and each is captured independently with small probability \(q\). Then no copy is captured with probability \[ (1-q)^N=e^{N\ln(1-q)}\approx e^{-Nq}=e^{-m}, \] where \(m=Nq\) is the expected count and we used \(\ln(1-q)\approx-q\). A gene expected to supply one molecule in a thousand, in a cell with \(L_i=300\), has \(m=0.3\) and reads zero with probability \(e^{-0.3}\approx0.74\). A zero, then, usually says that the expected count was small, not that the gene was off. Such sampling zeros are often called dropouts.
To have data with a known answer, we simulate an experiment. Six cell types share a baseline profile over \(p=300\) genes. Types 1 to 4 each raise their own block of 12 marker genes eightfold; types 5a and 5b share a fifth block and differ only in four genes each. The types have 150, 80, 50, 40, 20, and 20 cells, every cell has the same amount of random variation around its type’s profile, and each captures about 300 molecules by the independent-capture model.
Python Simulate UMI counts for six cell types
import numpy as np
TYPE_NAMES = ["1", "2", "3", "4", "5a", "5b"]
TYPE_MARKERS = ["o", "s", "^", "D", "v", "P"]
def simulate_counts(seed: int = 0) -> tuple[np.ndarray, np.ndarray]:
"""Simulated UMI counts (cells x genes) and the true type of each cell."""
rng = np.random.default_rng(seed)
n_genes, block, fold = 300, 12, np.log(8)
log_profile = np.tile(rng.normal(0, 1.5, n_genes), (6, 1))
for t in range(5): # types 1-4, then the program shared by 5a and 5b
log_profile[t, block * t:block * (t + 1)] += fold
log_profile[5, 4 * block:5 * block] += fold
log_profile[4, 60:64] += fold # 5a and 5b differ in four genes each
log_profile[5, 64:68] += fold
counts, labels = [], []
for t, size in enumerate([150, 80, 50, 40, 20, 20]):
for _ in range(size):
rho = np.exp(log_profile[t] + rng.normal(0, 0.25, n_genes))
depth = rng.lognormal(np.log(300), 0.4)
# independent capture of many copies gives Poisson counts
counts.append(rng.poisson(depth * rho / rho.sum()))
labels.append(t)
return np.array(counts), np.array(labels)
counts, cell_type = simulate_counts()Figure 20.3. Simulated UMI counts for five cells of each type over the first 72 genes, which contain every marker block; white entries are zeros. Even marker genes are often recorded as zero, so a cell’s type shows only as a pattern across many genes.
In Figure 20.3 the marker blocks form a diagonal pattern, but each cell shows only scattered counts within its block, and types 5a and 5b look nearly identical. Real data over twenty thousand genes is sparser still. The task is to recover groups of similar cells from many such noisy rows and to draw them faithfully.
From counts to a cell-by-cell comparison
As in Lesson 16, we first put cells on a common scale: \[ X_{ij}=\log_2\!\Bigl(1+10^4\,\frac{C_{ij}}{L_i}\Bigr). \] The factor \(10^4\) replaces the per-million factor used for bulk samples because a cell has only hundreds to thousands of molecules. We then center the columns to form \(Z\) and keep the first \(r=20\) PCA components. Cell \(i\) is represented by its score vector \(t_i=z_iV_r\in\mathbb R^{r}\), the \(r\)-direction version of Lesson 15’s score \(t_i=z_iv\), and distances \(d_{ij}=\|t_i-t_j\|\) define its neighbors. (Lesson 19 wrote \(x_i\) for whatever representation was chosen.)
Python Normalize counts and compute PCA scores
totals = counts.sum(axis=1)
X = np.log2(1 + 1e4 * counts / totals[:, None])
Z = X - X.mean(axis=0)
_, singular_values, Vt = np.linalg.svd(Z, full_matrices=False)
T = Z @ Vt[:20].T # row i is the score vector t_i
explained = singular_values**2 / np.sum(singular_values**2)
print("fraction of zero counts:", round(np.mean(counts == 0), 3))
print("explained by PC1-PC5:", np.round(explained[:5], 3))Of the simulated counts, 65.4% are zero. The first five PCs explain 4.7%, 3.8%, 2.4%, 2.2%, and 1.8% of the variance. Five expression programs raise disjoint gene blocks, so the type means differ along about five directions of comparable size. A PCA plot shows two of them, and types separated only along the other three land on top of each other, as in panel A of Figure 20.4 below. Most of the remaining variance is sampling noise spread over all 300 genes.
The 20 scores still recognize the types: on average 92% of each cell’s ten nearest neighbors in \(\mathbb R^{20}\) share its type (computed later). The information is in the neighbor relationships, and a linear projection to the plane discards it. We therefore seek points \(y_1,\ldots,y_n\in\mathbb R^2\), an embedding \(Y\in\mathbb R^{n\times2}\), that keep each cell’s near neighbors close while letting large distances distort.
Neighbor probabilities
The first step turns each cell’s distances into a statement about who its neighbors are. Instead of the yes-or-no choice of a \(k\)-nearest-neighbor graph, t-SNE gives every other cell a probability of being chosen that falls quickly with distance.3
Definition 20.1 Neighbor probabilities and perplexity
For score vectors \(t_1,\ldots,t_n\) with distances \(d_{ij}\) and a bandwidth \(\sigma_i>0\) for each cell, the conditional neighbor probabilities are \[ p_{j\mid i}=\frac{\exp\bigl(-d_{ij}^2/2\sigma_i^2\bigr)}{\sum_{k\ne i}\exp\bigl(-d_{ik}^2/2\sigma_i^2\bigr)}\quad(j\ne i), \qquad p_{i\mid i}=0. \] The perplexity of row \(i\) is \(\operatorname{Perp}_i=\exp\bigl(-\sum_{j}p_{j\mid i}\ln p_{j\mid i}\bigr)\), with \(0\ln0=0\). The joint neighbor probabilities are \(p_{ij}=(p_{j\mid i}+p_{i\mid j})/(2n)\).
Read \(p_{j\mid i}\) as two operations: weight every other cell by a Gaussian function of its distance from cell \(i\), then divide by the total so the row sums to one. Perplexity converts a row into an effective number of neighbors. A row with probability \(1/k\) on each of \(k\) cells has \(-\sum_jp_{j\mid i}\ln p_{j\mid i}=\ln k\), so its perplexity is \(k\); a row concentrated on one cell has perplexity near one.
t-SNE fixes a target perplexity, typically between 5 and 50, and chooses each \(\sigma_i\) so that every row reaches it. A cell in a dense region gets a small \(\sigma_i\) and a cell in a sparse region a large one. Perplexity thus plays the role of \(k\) in a neighbor graph, and the per-cell bandwidth deliberately divides out differences in local density. The joint probability combines both directions of choice, as Lesson 19’s union rule kept an edge if either cell chose the other, and the divisor \(2n\) makes all \(p_{ij}\) sum to one.
Example 20.1 Three cells on a line
Place cells at \(t=0,1,3\) and, to compute by hand, fix \(\sigma_i=1\) instead of calibrating. From cell 1 the squared distances are 1 and 9, with weights \(e^{-1/2}=0.607\) and \(e^{-9/2}=0.011\); dividing by their sum \(0.618\) gives \(p_{2\mid1}=0.982\) and \(p_{3\mid1}=0.018\). The other rows follow in the same way:
| from cell | to cell 1 | to cell 2 | to cell 3 |
|---|---|---|---|
| 1 | 0 | 0.982 | 0.018 |
| 2 | 0.818 | 0 | 0.182 |
| 3 | 0.076 | 0.924 | 0 |
Symmetrizing gives \(p_{12}=(0.982+0.818)/6=0.300\), \(p_{23}=(0.182+0.924)/6=0.184\), and \(p_{13}=0.016\). Each unordered pair appears twice in the sum over \(i\ne j\), and \(2(0.300+0.184+0.016)=1\).
Cell 3’s nearest cell is two units away, yet it gives that cell 92% of its probability: neighbor probabilities are relative to each cell’s surroundings. For a cell with unequal distances to its neighbors, increasing \(\sigma_i\) makes its probabilities more uniform and raises its perplexity. This gives a direction for adjusting a trial bandwidth.
Algorithm Calibrate neighbor probabilities by bisection
Input: squared distances \(d_{ij}^2\) between cells \(i,j=1,\ldots,n\) and a target perplexity \(\tau\).
Output: the joint neighbor probabilities \(p_{ij}\).
Foreach cell \(i=1,\ldots,n\):- Choose bounds \(a,b\) on \(\ln\sigma_i\) whose perplexities bracket \(\tau\). Use \(a\leftarrow-10\) and \(b\leftarrow10\) for these data.
Repeat50 times:- \(s\leftarrow(a+b)/2\).
// bisect the log interval - \(\sigma_i\leftarrow e^s\).
// get trial bandwidth - Compute and store the row \(p_{j\mid i}\) and its perplexity \(\operatorname{Perp}_i\) using Definition 20.1.
If\(\operatorname{Perp}_i>\tau\), set \(b\leftarrow s\).// lower the upper boundElse, set \(a\leftarrow s\).// raise the lower bound
- \(s\leftarrow(a+b)/2\).
Foreach pair \(i,j\), set \(p_{ij}\leftarrow(p_{j\mid i}+p_{i\mid j})/(2n)\) using the last stored rows.// pool both directionsReturnthe matrix \((p_{ij})\).// report symmetric matrix
Python Compute neighbor probabilities at a target perplexity
def neighbor_row(sq_dists: np.ndarray, sigma: float) -> np.ndarray:
"""Conditional probabilities p_{j|i} from cell i's squared distances to others."""
# subtracting the minimum rescales every weight equally and avoids underflow
weights = np.exp(-(sq_dists - sq_dists.min()) / (2 * sigma**2))
return weights / weights.sum()
def perplexity(p: np.ndarray) -> float:
"""Effective number of neighbors: exp of the entropy of a probability row."""
p = p[p > 0]
return float(np.exp(-np.sum(p * np.log(p))))
def calibrate_row(sq_dists: np.ndarray, target: float) -> np.ndarray:
"""Bisect on log(sigma) until the row's perplexity matches the target."""
low, high = -10.0, 10.0
for _ in range(50):
row = neighbor_row(sq_dists, np.exp((low + high) / 2))
if perplexity(row) > target:
high = (low + high) / 2
else:
low = (low + high) / 2
return row
def squared_distances(A: np.ndarray) -> np.ndarray:
"""All pairwise squared Euclidean distances between the rows of A."""
sq = np.sum(A**2, axis=1)
return np.maximum(sq[:, None] + sq[None, :] - 2 * A @ A.T, 0)
def joint_probabilities(T: np.ndarray, target: float) -> np.ndarray:
"""Symmetric neighbor probabilities p_ij, summing to one over all pairs."""
n = len(T)
D2 = squared_distances(T)
conditional = np.zeros((n, n))
for i in range(n):
others = np.arange(n) != i
conditional[i, others] = calibrate_row(D2[i, others], target)
return (conditional + conditional.T) / (2 * n)
row_from_cell_1 = neighbor_row(np.array([1.0, 9.0]), sigma=1.0)
print(np.round(row_from_cell_1, 3), round(perplexity(row_from_cell_1), 3))The hand-calculated row for cell 1 has perplexity \(1.09\), or about one effective neighbor.
Placing cells in the plane
To choose the display points \(y_i\), t-SNE describes the plane with similar probabilities but a different kernel: \[ q_{ij}=\frac{w_{ij}}{\sum_{k\ne l}w_{kl}}, \qquad w_{ij}=\frac{1}{1+\|y_i-y_j\|^2}\quad(i\ne j), \qquad q_{ii}=0. \] First compute a similarity \(w_{ij}\) for every pair of display points, then normalize over all ordered pairs. This kernel decays like \(\|y_i-y_j\|^{-2}\), far more slowly than a Gaussian, and the heavy tail addresses a shortage of room. In 20 dimensions a cell can have dozens of cells at moderate, nearly equal distances, which the plane cannot reproduce. With a slowly decaying kernel, moderately distant pairs can be pushed well apart at little cost, which opens gaps between groups.3
Definition 20.2 t-SNE embedding
Input: joint neighbor probabilities \(p_{ij}\) for \(n\) cells.
Output: a configuration \(Y=(y_1,\ldots,y_n)\) in \(\mathbb R^2\), a t-SNE embedding, that locally minimizes \[ \mathcal C(Y)=\sum_{i\ne j,\;p_{ij}>0}p_{ij}\ln\frac{p_{ij}}{q_{ij}}. \]
\(\mathcal C\), called the Kullback-Leibler divergence, is zero when every \(q_{ij}=p_{ij}\). Each term is weighted by \(p_{ij}\). Input neighbors have large \(p_{ij}\), and placing them far apart makes \(q_{ij}\) small and the term large. When \(p_{ij}\approx0\) the term is negligible wherever the two cells sit. Crowding unrelated cells together is penalized only indirectly: because the \(q_{ij}\) sum to one, probability spent on unrelated pairs is taken from neighbor pairs.
Continuing Example 20.1, try \(y=(0,1,3)\), a copy of the input. The kernel values for pairs 12, 13, and 23 are \(1/2\), \(1/10\), and \(1/5\), which total \(1.6\) over ordered pairs, so \(q_{12}=0.3125\), \(q_{13}=0.0625\), and \(q_{23}=0.125\). The pair terms are \(0.300\ln(0.300/0.3125)=-0.0122\), \(0.016\ln(0.016/0.0625)=-0.0218\), and \(0.184\ln(0.184/0.125)=0.0712\); counting each pair twice gives \(\mathcal C\approx0.074\). Using the same rounded probabilities, we can compare three changes to the placement.
Python Compute the t-SNE cost for four placements
def low_dim_affinities(Y: np.ndarray) -> tuple[np.ndarray, np.ndarray]:
"""Kernel values w_ij and normalized probabilities q_ij for an embedding."""
W = 1 / (1 + squared_distances(Y))
np.fill_diagonal(W, 0)
return W, W / W.sum()
def tsne_cost(P: np.ndarray, Y: np.ndarray) -> float:
"""C(Y): sum of p_ij log(p_ij / q_ij) over pairs with p_ij > 0."""
_, Q = low_dim_affinities(Y)
keep = P > 0
return float(np.sum(P[keep] * np.log(P[keep] / Q[keep])))
P_line = np.array([[0, 0.300, 0.016], [0.300, 0, 0.184], [0.016, 0.184, 0]])
for y in [(0, 1, 3), (0, 2, 6), (0, 1, 1.5), (0, 3, 1)]:
print(y, round(tsne_cost(P_line, np.array(y, dtype=float)[:, None]), 3))| Placement \((y_1,y_2,y_3)\) | Change from \((0,1,3)\) | Cost \(\mathcal C\) |
|---|---|---|
| \((0,1,3)\) | Copy the input | 0.074 |
| \((0,2,6)\) | Double every distance | 0.087 |
| \((0,1,1.5)\) | Move cell 3 toward cell 1 | 0.226 |
| \((0,3,1)\) | Swap cells 2 and 3 | 0.988 |
Doubling every distance raises the cost slightly. Swapping cells 2 and 3 moves cell 2 away from both of its neighbors and multiplies the cost by thirteen. The objective cares a great deal about who is next to whom and much less about how far apart things are.
How the cost moves the points
To minimize \(\mathcal C\) we follow its gradient3:
Equation (20.1)
\[ \frac{\partial\mathcal C}{\partial y_i}=4\sum_{j\ne i}\bigl(p_{ij}-q_{ij}\bigr)\,w_{ij}\,(y_i-y_j). \]
Gradient descent moves \(y_i\) against this vector, so each cell \(j\) exerts a force along the line joining \(y_i\) and \(y_j\). The \(p_{ij}\) part pulls \(y_i\) toward input neighbors; the \(q_{ij}\) part pushes it away from every cell it currently sits near. For two cells in well-separated groups, \(p_{ij}\approx0\) and only the push remains, with size proportional to \(w_{ij}^2\|y_i-y_j\|\), which falls like \(\|y_i-y_j\|^{-3}\). Once two islands have separated, the forces between them are negligible, so their final distance reflects where the optimization left them rather than how different their expression is.2
Rotating, reflecting, or shifting every point by \(y_i'=Ry_i+b\), with \(R\) an orthogonal \(2\times2\) matrix, leaves every distance \(\|y_i-y_j\|\) unchanged, hence every \(w_{ij}\), every \(q_{ij}\), and the cost. A minimizer is therefore never unique, and the axes of a t-SNE plot mean nothing, unlike PCA axes, which are fixed gene combinations. The cost is also not convex, so different random starts can reach different local minima with islands arranged differently.
Algorithm t-SNE by gradient descent
Input: score vectors \(t_1,\ldots,t_n\), a target perplexity, step size \(\eta\), number of iterations \(N\), and a random seed.
Output: the final embedding \(Y\), with row \(y_i\in\mathbb R^2\) for each cell \(i\).
- Set \(d_{ij}^2\leftarrow\|t_i-t_j\|^2\) for each pair \(i,j\). Calibrate the \(p_{ij}\) by bisection at the target perplexity.
- Seed the random generator once.
Foreach cell \(i=1,\ldots,n\):- Draw both
coordinates of \(y_i\) independently
from a normal distribution with mean 0 and standard deviation \(10^{-4}\).
// start near the origin - Set the
previous move \(v_i\leftarrow(0,0)\).
// start with no motion
- Draw both
coordinates of \(y_i\) independently
from a normal distribution with mean 0 and standard deviation \(10^{-4}\).
Foriteration \(k=1,\ldots,N\):If\(k\le250\), set the attraction multiplier \(\gamma\leftarrow12\) and momentum \(\mu\leftarrow0.5\). This early exaggeration strengthens attraction while groups form.Else, set \(\gamma\leftarrow1\) and \(\mu\leftarrow0.8\).// end early exaggeration- Set \(w_{ij}\leftarrow 1/(1+\|y_i-y_j\|^2)\) for
\(i\ne j\) and \(w_{ii}\leftarrow0\).
// weight embedding pairs Foreach pair \(i,j\), set \(q_{ij}\leftarrow w_{ij}/\sum_{a\ne b}w_{ab}\).// normalize over ordered pairsForeach cell \(i\), compute the gradient term with \(p_{ij}\) replaced by \(\gamma p_{ij}\) in Equation 20.1: \[ g_i\leftarrow 4\sum_{j\ne i}(\gamma p_{ij}-q_{ij})w_{ij}(y_i-y_j). \]- After all \(g_i\) have been computed,
foreach cell \(i\):// use pre-move gradients- \(v_i\leftarrow\mu v_i-\eta g_i\).
// step with momentum - \(y_i\leftarrow y_i+v_i\).
// move the embedded cell
- \(v_i\leftarrow\mu v_i-\eta g_i\).
Return\(Y\) with rows \(y_1,\ldots,y_n\).// report cell coordinates
The Python implementation stores \(F_{ij}=(\gamma p_{ij}-q_{ij})w_{ij}\) in a matrix \(F\). It computes the sum \(\sum_j F_{ij}(y_i-y_j)\) as \(\bigl(\sum_jF_{ij}\bigr)y_i-(FY)_i\), where \((FY)_i\) is row \(i\) of the matrix product of \(F\) and \(Y\).
Python t-SNE by gradient descent
def tsne(T: np.ndarray, target: float, seed: int = 0,
n_iter: int = 1000, step: float = 200.0) -> np.ndarray:
"""Two-dimensional t-SNE embedding of the rows of T by gradient descent."""
rng = np.random.default_rng(seed)
P = joint_probabilities(T, target)
Y = rng.normal(0, 1e-4, size=(len(T), 2))
velocity = np.zeros_like(Y)
for iteration in range(n_iter):
boost, momentum = (12.0, 0.5) if iteration < 250 else (1.0, 0.8)
W, Q = low_dim_affinities(Y)
F = (boost * P - Q) * W
gradient = 4 * (F.sum(axis=1)[:, None] * Y - F @ Y)
velocity = momentum * velocity - step * gradient
Y = Y + velocity
return Y
embeddings = {target: tsne(T, target, seed=0) for target in (5, 30, 100)}t-SNE on the simulated cells
Figure 20.4 compares the first two PCA scores with t-SNE embeddings computed from the same 20 scores at three perplexities. At perplexity 30 (panel C), types 1 to 4 each form an island, and 5a and 5b share a fifth, as their construction suggests: two subtypes differing in eight genes are each other’s closest relatives.
Figure 20.4. The simulated cells drawn by their first two PCA scores (A) and by t-SNE from 20 PCA scores at perplexity 5, 30, and 100 (B to D); color and marker show the true type. The linear projection overlaps several types, while every embedding separates the same five groups in a different arrangement.
Because we simulated the data, we can test the picture against the truth. For each representation, measure the average fraction of each cell’s ten nearest neighbors that share its type. To compare island spacing, take the smallest and largest distances among the six pairs of centers for types 1 through 4.
Python Measure same-type neighbors and gaps between cell types
def same_type_fraction(A: np.ndarray, k: int = 10) -> float:
"""Average fraction of each cell's k nearest neighbors that share its type."""
D2 = squared_distances(A)
np.fill_diagonal(D2, np.inf)
neighbors = np.argsort(D2, axis=1, kind="stable")[:, :k]
return float(np.mean(cell_type[neighbors] == cell_type[:, None]))
def center_gaps(A: np.ndarray) -> dict[str, float]:
"""Distances between the centers of types 1-4, keyed by the pair of types."""
centers = [A[cell_type == t].mean(axis=0) for t in range(4)]
gaps = {}
for a in range(4):
for b in range(a + 1, 4):
gaps[f"{a + 1}-{b + 1}"] = round(float(np.linalg.norm(centers[a] - centers[b])), 1)
return gaps
print(f"{'Representation':14s} {'Same type':>9s} {'Min gap':>8s} {'Max gap':>8s}")
for name, A in [("First 20 PCs", T), ("PC1 and PC2", T[:, :2]), ("t-SNE 30", embeddings[30])]:
gaps = center_gaps(A).values()
print(f"{name:14s} {same_type_fraction(A):9.0%} {min(gaps):8.1f} {max(gaps):8.1f}")Representation Same type Min gap Max gap
First 20 PCs 92% 17.6 20.5
PC1 and PC2 68% 3.8 17.5
t-SNE 30 94% 11.6 28.4
Neighbors in the t-SNE embedding share a type about as often as neighbors in the 20 scores and far more often than neighbors in the PCA plane. This agreement supports the island memberships. The type centers in 20 dimensions are close to equidistant, whereas the embedded gaps are more uneven. Their spread reflects how attraction and repulsion placed the islands in the plane.
Reading an embedding
Perplexity changes the picture without any change in the data. All three t-SNE panels of Figure 20.4 recover the same five groups, but the shapes and arrangement differ. At perplexity 5 each cell keeps only a few neighbors, and a few cells end up stranded between islands; in noisier real data, small perplexities can split one population into fragments, and even structureless random data can form convincing clumps.4 No setting is the true one. The five groups are worth reporting because they appear at every perplexity we tried; their arrangement, which changes with perplexity, is not.
Island area is unreliable too. Every simulated type has the same variation around its profile, yet type 1, with 150 cells, covers a far larger area than type 5b, with 20. Because each \(\sigma_i\) was tuned to give every cell the same number of effective neighbors, differences in local density were removed in the first step, and area tracks the number of cells more than their variability.4 The large rod island in Figure 20.1 reflects at least partly how many rods were captured.
Caution Gaps and areas in an embedding are not measurements
A t-SNE or UMAP plot can support the claim that cells in one island were each other’s neighbors in the input. It does not show how different two islands are, how variable a group is, or that an empty region contains no intermediate cells.
UMAP, the other widely used embedding, builds the \(k\)-nearest-neighbor graph of Lesson 19 explicitly, with \(k\) playing the role of perplexity, and lays it out by pulling neighbors together and pushing sampled non-neighbors apart.5 The warning above applies to it unchanged.
In a typical analysis, clusters are computed on the neighbor graph built from PCA scores, as in Lesson 19, and the embedding is drawn alongside for display, not used to define them. Naming a cluster as a cell type is a further biological claim, made from marker genes; the colors in Figure 20.1 are such claims.1
Exercises
Exercise 20.1. Three cells lie on a line at \(t=0,2,3\). With \(\sigma_i=1\) for every cell, compute all \(p_{j\mid i}\) and \(p_{ij}\). Which pair has the largest joint probability, and is it the closest pair?
Exercise 20.2. Compute the perplexity of the conditional row \((1/2,1/4,1/4)\) and explain why it lies between 2 and 3.
Exercise 20.3. Derive Equation 20.1. Write \(S=\sum_{k\ne l}w_{kl}\) and \(\ln q_{kl}=\ln w_{kl}-\ln S\), use \(\sum_{k\ne l}p_{kl}=1\), and show that the repulsion comes entirely from \(\ln S\).
Exercise 20.4. In
real data the types are unknown. Write a label-free check: for each
cell, the fraction of its ten nearest neighbors in the 20 PCA scores
that remain among its ten nearest in an embedding. Compare the first two
PCA scores with the perplexity-30 embedding, then rerun t-SNE with
seed=1 and compare the center gaps.
Exercise 20.5. For each statement about Figure 20.1, decide whether the figure supports it, and name the additional evidence needed if not. (a) Cones are more similar in expression to rods than bipolar cells are. (b) Bipolar cells comprise several distinct groups of similar cells. (c) Rods vary more in expression than amacrine cells. (d) The cells labeled amacrine are amacrine cells.
References 6 sources
Cited in the text
Also drawn on