Tutorials › Statistical Methods for Single-Cell RNA Sequencing › Selecting Neighborhood and Kernel Scales

Pseudotime and Trajectory Inference · Tutorial 375 of 700

Selecting Neighborhood and Kernel Scales

You will learn to choose and assess neighborhood and kernel scales by tracing their effects on cell-graph edges, transition probabilities, and diffusion smoothing.

Advanced 10 min read

What You'll Learn

  • Distinguish neighborhood size from kernel bandwidth and identify what each changes.
  • Calculate Gaussian edge weights and normalized transition probabilities at different scales.
  • Diagnose underconnected neighborhoods, weak links, and shortcuts caused by overly large neighborhoods.
  • Compare global-bandwidth, local-bandwidth, and radius-based constructions.
  • Use graph and weight diagnostics to assess scale sensitivity and recognize limitations.

Why Scale Choices Matter

A diffusion-based trajectory depends on which cells are allowed to exchange information and how strongly each connected pair is linked. These are related but distinct choices: the neighborhood size determines which cell pairs can have edges, while the kernel bandwidth controls how quickly edge weights decrease with distance. A graph can include the right local edges but weight them so unevenly that diffusion rarely crosses some of them. Conversely, a large neighborhood can introduce shortcuts between cells that are far apart along the biological trajectory.

This tutorial focuses on the effect of these scale choices on connectivity and smoothing. Earlier material on diffusion kernels and diffusion pseudotime establishes how cell-to-cell transitions are used downstream. Here, the practical question is whether the chosen graph and weights represent local transitions plausibly enough for those downstream calculations to be informative.

Definition: Let \(d_{ij}\) be the distance between cells \(i\) and \(j\) in the chosen representation. A neighborhood rule selects which pairs may be connected. A kernel assigns a nonnegative affinity \(W_{ij}\) to selected pairs, usually giving greater affinity to nearer pairs. The row-normalized transition probability is \(P_{ij}=W_{ij}/\sum_{\ell}W_{i\ell}\), when the row sum is positive. Neighborhood size controls the candidate edges; bandwidth controls their relative weights.

Two Scale Parameters, Two Different Effects

In a \(k\)-nearest-neighbor graph, each cell selects its \(k\) closest other cells. Because these selections can be asymmetric, a graph must specify how to turn them into undirected edges. The union rule retains an edge if either cell selects the other; the mutual rule retains it only if both cells select one another. The union rule tends to include more edges, while the mutual rule is more restrictive. Thus, the same \(k\) can produce different connectivity under the two rules.

A radius-based neighborhood instead connects pairs whose distance is no greater than a chosen radius. It applies the same distance threshold everywhere, so the number of neighbors can vary substantially across the representation. A \(k\)-nearest-neighbor rule fixes the number of selections per cell, but the distances to those neighbors can vary.

A common Gaussian kernel for a selected edge is

$$ W_{ij}=\exp\!\left(-\frac{d_{ij}^{2}}{2h^{2}}\right), $$

where \(h>0\) is the bandwidth. Smaller \(h\) makes affinities fall sharply with distance; larger \(h\) makes them more similar. A global bandwidth uses the same \(h\) for all pairs. An adaptive or locally scaled kernel uses cell-specific scales, for example \(W_{ij}=\exp(-d_{ij}^{2}/(\sigma_i\sigma_j))\), where \(\sigma_i\) and \(\sigma_j\) are positive local distance scales. The precise local-scaling rule varies, so it should be reported rather than assumed.

If the graph is sparse, affinities for unselected pairs are set to zero. Increasing \(k\) can then change the graph’s topology by adding edges. Changing \(h\) on a fixed graph does not add edges, but it can make some existing edges carry so little transition probability that they are effectively weak links. These distinctions are important: a zero weight caused by graph construction is not the same as a very small positive weight caused by the kernel.

Worked Example: A Local Neighborhood Around Cell D

Consider five hypothetical cells represented by positions on a line: A at 0, B at 1, C at 4, D at 7, and E at 8. Distances are absolute differences between positions. Cell D’s distances to A, B, C, and E are 7, 6, 3, and 1, respectively. Therefore, when \(k=2\), D’s two nearest other cells are E and C, at distances 1 and 3.

With a union \(k\)-nearest-neighbor rule, D contributes the candidate edges D–E and D–C. The kernel can weight these differently. For a global bandwidth \(h=1\), the Gaussian weights are

$$ W_{DE}=\exp(-1^{2}/2)\approx 0.6065,\qquad W_{DC}=\exp(-3^{2}/2)=\exp(-4.5)\approx 0.01111. $$

Considering just these two outgoing weights, the normalized probabilities are

$$ P_{DE}=\frac{0.6065}{0.6065+0.01111}\approx 0.9820,\qquad P_{DC}=\frac{0.01111}{0.6065+0.01111}\approx 0.0180. $$

Thus, both pairs are graph neighbors, but under this bandwidth nearly all of D’s transition probability between these two choices goes to E. The example also illustrates why neighbor rank alone does not describe effective connectivity: C is the second selected neighbor, but its edge is much weaker than D–E.

With local scaling, raw distance alone need not determine the relative weight. For instance, suppose the local kernel’s normalized squared-distance costs for D–E and D–C are 5 and 6. The cost difference is \(5-6=-1\), so \(W_{DE}/W_{DC}=\exp(-5)/\exp(-6)=\exp(1)\approx 2.718\). This calculation is consistent with the specified costs; it does not follow from distances 1 and 3 without knowing the local scales.

Bandwidth Changes the Strength of Smoothing

For a fixed edge set, a larger bandwidth makes distances matter less in the weights. This generally spreads transition probability more evenly among a cell’s neighbors, so one diffusion step mixes information across more of the local neighborhood. A smaller bandwidth concentrates probability on the closest neighbors. This can preserve fine local distinctions, but it can also make a graph behave as if it were nearly disconnected if some bridging edges receive negligible weight.

The limiting cases clarify the mechanism. On a fixed graph with positive edge distances, as \(h\) becomes very small, the longest edges’ weights approach zero faster than the shortest edges’ weights. As \(h\) becomes very large, every selected edge weight approaches 1. The latter does not create new graph edges, but it makes all existing neighbors nearly equally likely at a given cell, regardless of distance. These statements concern the Gaussian weights; the exact resulting transition probabilities also depend on each cell’s selected neighbors.

Worked Example: Comparing Two Bandwidths

Return to D’s two candidate edges, at distances 1 and 3, but compare bandwidths \(h=0.5\) and \(h=2\). At \(h=0.5\), the weights are

$$ W_{DE}=\exp\!\left(-\frac{1}{2(0.5)^2}\right)=\exp(-2)\approx0.1353,\qquad W_{DC}=\exp\!\left(-\frac{9}{2(0.5)^2}\right)=\exp(-18)\approx1.523\times10^{-8}. $$

The transition probability to C, considering these two edges, is approximately \(1.523\times10^{-8}/(0.1353+1.523\times10^{-8})\approx1.125\times10^{-7}\). Almost all probability goes to E.

At \(h=2\), the corresponding weights are

$$ W_{DE}=\exp(-1/8)\approx0.8825,\qquad W_{DC}=\exp(-9/8)\approx0.3247, $$

so \(P_{DC}=0.3247/(0.8825+0.3247)\approx0.2690\), and \(P_{DE}\approx0.7310\). The larger bandwidth gives the distance-3 edge appreciably more transition probability. It has not changed the candidate edge set; it has changed how strongly the two edges contribute to smoothing.

Choosing and Diagnosing Scales

There is no scale value that is automatically correct for every cell population or representation. The working assumption behind a local neighborhood is that the chosen distance measure gives a meaningful ordering of local similarity, and that nearby cells are plausible candidates for local transitions. The kernel further assumes that distance should reduce affinity according to its specified rule. These assumptions can fail if the representation is dominated by irrelevant variation, if distances do not reflect state similarity, or if a single scale is unsuitable across the dataset.

A practical assessment compares a small, defensible range of neighborhood sizes and bandwidths rather than selecting a value from a single visual impression. Examine the graph and the transition weights together. Useful checks include:

  • Neighbor distances and degree patterns. Inspect the distances to selected neighbors and the resulting numbers of undirected edges. Very long selected edges may indicate that a cell has no convincing local neighbors under the chosen representation.
  • Weak links. Compare edge weights or normalized transition probabilities. A graph may be technically connected while a few bridge edges carry almost no probability. Repeating the calculation at a moderately larger bandwidth can show whether this is a weighting issue rather than a missing edge.
  • Shortcuts. Check whether increasing \(k\) adds edges between regions that appear separated in the representation or that would bypass an expected local sequence. Such edges can make diffusion mix across a trajectory too quickly.
  • Sensitivity. Compare neighborhoods and downstream pseudotime patterns across nearby parameter choices. Large changes indicate that the inferred structure is scale-dependent and should be reported as such, not hidden by choosing one setting.

These checks are diagnostics, not a proof that the graph is biologically correct. Apparent branches or gaps in a visualization do not by themselves establish that edges across them are invalid, and a stable result can still reflect a poor distance representation. Where available, known cell-state information can help assess plausibility, but it does not turn an association in the representation into evidence of a causal transition.

Worked Example: A Shortcut Alters a Transition

Suppose a hypothetical junction cell J initially has two outgoing edges, to U and V, with weights 0.8 and 0.7. Its row sum is \(0.8+0.7=1.5\), so \(P_{JU}=0.8/1.5\approx0.5333\) and \(P_{JV}=0.7/1.5\approx0.4667\). Now a larger neighborhood adds an edge from J to Q with weight 0.6. The new row sum is \(0.8+0.7+0.6=2.1\), and the probabilities become

$$ P_{JU}=\frac{0.8}{2.1}\approx0.3810,\quad P_{JV}=\frac{0.7}{2.1}\approx0.3333,\quad P_{JQ}=\frac{0.6}{2.1}\approx0.2857. $$

The new edge takes about 28.6% of J’s one-step transition probability, reducing the probability assigned to both original edges. If Q is a genuine nearby state, this may be appropriate. If it is a shortcut across an otherwise gradual path, the added edge can promote premature mixing. The arithmetic identifies the size of the change; deciding whether the edge is plausible requires inspecting its distance and context.

Alternatives and Limitations

A fixed-\(k\) graph is useful when a roughly comparable number of local candidates per cell is desired. A radius graph instead enforces a common distance cutoff, which can avoid connecting cells beyond a chosen scale but may leave some cells with few or no neighbors. A global-bandwidth kernel is simple to interpret and compare across pairs, but may not adapt well when relevant local distances vary. Local bandwidths can accommodate different distance scales, though they make an edge’s weight depend on scales at both endpoints and add another modeling choice. Union and mutual \(k\)-nearest-neighbor symmetrizations offer a separate trade-off: union retains more candidate edges, while mutual selection removes edges not supported in both directions.

None of these choices eliminates the need to inspect the representation and the resulting graph. Increasing \(k\) can connect regions that should remain locally distinct; decreasing it can remove useful links. Increasing bandwidth can blur local distinctions; decreasing it can make some edges practically irrelevant. A graph is also only as meaningful as the distance measure used to construct it. Report the neighborhood rule, symmetrization, kernel form, bandwidth or local-scaling rule, and any sensitivity checks so the scale-dependent nature of the result is clear.

Common Pitfall

Common mistake: “The graph is connected, so diffusion can move freely throughout it.” Connectivity only says that paths of selected edges exist. If one or more edges on a path have very small weights relative to the alternatives, transition across them may be rare. Conversely, a large neighborhood can create a shortcut that makes distant regions mix too readily. Correction: inspect edge weights and normalized transition probabilities as well as the unweighted graph, and compare plausible scale choices.
Key takeaway: Neighborhood size determines which pairs can communicate; kernel bandwidth determines how strongly selected pairs communicate. Diagnose both the edge set and the weight distribution, and treat major changes across plausible scales as evidence of parameter sensitivity.

Check Your Understanding

Answer each question, then compare your reasoning with the solutions.

  1. On a fixed sparse graph, what changes when the Gaussian bandwidth increases, and what does not necessarily change?
  2. For two edges with distances 1 and 2, calculate their Gaussian weights at \(h=1\), rounded to four significant figures.
  3. Why can a connected graph still have a weak diffusion link?
  4. State one difference between a fixed-\(k\) neighborhood and a radius-based neighborhood.
  5. A larger \(k\) adds a shortcut edge from a junction to a distant cell. Name one diagnostic question to ask before retaining it.

Solutions. (1) Relative weights among selected edges generally become more even; the selected edge set remains fixed unless the graph is rebuilt. (2) \(W_1=\exp(-1/2)\approx0.6065\) and \(W_2=\exp(-4/2)=\exp(-2)\approx0.1353\). (3) The edge can have a very small weight compared with the cell’s other outgoing edges, so its normalized transition probability is small even though the path exists. (4) Fixed \(k\) selects a set number of nearest candidates per cell, while a radius uses a common distance cutoff and can give cells different neighbor counts. (5) Check whether the edge’s distance and representation context support a local transition, and whether including it substantially changes transition probabilities or downstream patterns.

Clinical Biostats