Tutorials › Statistical Methods for Single-Cell RNA Sequencing › The Law Of Total Variance For Counts

Noise Models and Dropout · Tutorial 48 of 700

The Law Of Total Variance For Counts

Learn to decompose the variance of a single-cell count into average within-condition variance and variance between conditional means, and interpret both terms without imposing a particular count distribution.

Intermediate 9 min read

What You'll Learn

  • State the law of total variance and its finite-moment condition.
  • Derive the identity by separating a count into a conditional residual and a conditional mean.
  • Calculate both variance components for hypothetical single-cell count distributions.
  • Explain how the choice of conditioning variable affects interpretation of the components.
  • Recognize estimation and interpretation pitfalls when applying the identity to count data.

Why Separate the Two Parts of Count Variability?

For a gene, a count recorded in one cell can differ from a count in another for more than one reason. Even among cells regarded as comparable, the observed count may vary around its conditional average. Also, the conditional average itself may differ across cell states, library exposures, or other recorded characteristics. The law of total variance gives an exact accounting identity for these two kinds of variation.

This identity does not, by itself, say what biological or technical mechanism produced either component. Its interpretation depends on the variable we condition on and on how well that variable represents the relevant grouping of cells. It is a way to organize variance, not a claim that the components are separate causal mechanisms.

Definition: Let \(Y\) be a random gene count for a cell, and let \(Z\) denote a conditioning variable, such as a cell group or measured cell characteristic. Write \(E(Y\mid Z)\) for the conditional mean and \(\operatorname{Var}(Y\mid Z)\) for the conditional variance. If \(Y\) has a finite second moment, the law of total variance is
$$ \operatorname{Var}(Y) = E\!\left[\operatorname{Var}(Y\mid Z)\right] + \operatorname{Var}\!\left(E[Y\mid Z]\right). $$
The first term is the expected conditional variance; the second is the variance of the conditional mean.

In single-cell applications, \(Y\) can be the observed count for one gene in a randomly selected cell from a defined population of cells. \(Z\) might indicate a cell type, a biological state, or a library-size group. The first term averages the count variance within values of \(Z\), weighting each value by its probability in the population. The second term measures how much the expected count changes across those values. Both terms have units of squared counts, and their sum is the marginal variance of \(Y\).

Deriving the Decomposition

The identity follows by adding and subtracting the conditional mean. Define \(m(Z)=E(Y\mid Z)\) and the conditional residual \(\varepsilon=Y-m(Z)\). Then \(Y-E(Y)=\varepsilon+\{m(Z)-E(Y)\}\). Squaring and taking expectations yields

$$ \begin{aligned} \operatorname{Var}(Y) &= E\!\left[(Y-E(Y))^2\right] \\ &= E(\varepsilon^2) + 2E\!\left[\varepsilon\{m(Z)-E(Y)\}\right] + E\!\left[(m(Z)-E(Y))^2\right]. \end{aligned} $$

The cross term is zero. In fact, \(m(Z)-E(Y)\) is determined by \(Z\), while the conditional residual has \(E(\varepsilon\mid Z)=0\). Therefore,

$$ E\!\left[\varepsilon\{m(Z)-E(Y)\}\right] = E\!\left[E(\varepsilon\mid Z)\{m(Z)-E(Y)\}\right] =0. $$

Also, \(E(\varepsilon^2)=E[\operatorname{Var}(Y\mid Z)]\), because the conditional residual has conditional mean zero, and \(E[(m(Z)-E(Y))^2]=\operatorname{Var}(E[Y\mid Z])\). Substitution gives the stated identity. No Poisson, negative binomial, or other specific count distribution is required. The assumptions are that the variables and conditional moments are defined and that \(Y\) has a finite second moment; the conditioning variable may be discrete or continuous.

Applying the Identity to Gene Counts

For a single gene, suppose a cell is sampled from a target population and \(Z\) records a characteristic available for conditioning. For each possible value \(z\), the conditional mean \(E(Y\mid Z=z)\) describes the average count among cells with that characteristic, while \(\operatorname{Var}(Y\mid Z=z)\) describes their within-group count variability. The identity says that the population-wide variance can be recovered by averaging the within-group variances and adding the variance among the group means.

Worked Example: Two Cell Groups With Unequal Frequencies

Suppose, hypothetically, that 25% of cells belong to group A and 75% to group B. For a particular gene, assume the conditional count means are 2 and 6 molecules per cell, and the conditional variances are 2 and 6 squared molecules per cell, respectively. These are stipulated population moments for the example; no count distribution is being assumed.

The marginal mean is the weighted average of the group means:

$$ E(Y)=0.25(2)+0.75(6)=0.5+4.5=5 $$

The expected conditional variance is

$$ E[\operatorname{Var}(Y\mid Z)] =0.25(2)+0.75(6) =0.5+4.5=5. $$

The variance of the conditional means is

$$ \begin{aligned} \operatorname{Var}(E[Y\mid Z]) &=0.25(2-5)^2+0.75(6-5)^2\\ &=0.25(9)+0.75(1)=2.25+0.75=3. \end{aligned} $$

Thus the marginal count variance is \(5+3=8\) squared molecules per cell. In this example, 5 of the 8 variance units are the average within-group variance, and 3 arise from differences between the two conditional means. The calculation describes variation in this specified cell population; it does not establish why the groups have different means.

Worked Example: Equal Contributions to the Marginal Variance

Consider a different hypothetical population, with two equally common cell states. The gene count has conditional mean 2 and conditional variance 4 in state A, and conditional mean 6 and conditional variance 4 in state B. The conditional variances are given in squared counts.

First, the marginal mean is \(0.5(2)+0.5(6)=4\) counts per cell. The average conditional variance is \(0.5(4)+0.5(4)=4\) squared counts. The variance of the conditional means is

$$ 0.5(2-4)^2+0.5(6-4)^2 =0.5(4)+0.5(4)=4. $$

The law therefore gives a marginal variance of \(4+4=8\) squared counts. Each component contributes half of the total in this constructed example. That fraction is a descriptive summary of the chosen population and grouping, not a general proportion that can automatically be labelled “technical” or “biological.”

The Conditioning Variable Changes the Split

The total variance belongs to the marginal distribution of \(Y\); the two components depend on what information is placed in \(Z\). A finer conditioning variable can move variation from the expected conditional variance into variation among conditional means. The sum remains unchanged. This matters when deciding whether a component represents variability within a scientifically meaningful group or variability left unexplained by the chosen grouping.

Worked Example: Refining the Cell Grouping

Suppose, hypothetically, that cells belong with equal probability to four subgroups. Two subgroups form broad type A, and two form broad type B. Given the four subgroups, the gene count has means 1, 3, 5, and 7 counts per cell, respectively, and conditional variance 1 squared count in each subgroup. Thus the four subgroup means are equally likely.

If \(Z\) records only the broad type, the conditional mean is 2 for type A and 6 for type B. Within either broad type, the two subgroup means differ by 2 counts, so their variance is 1 squared count. Adding the subgroup-level conditional variance of 1 gives \(\operatorname{Var}(Y\mid Z)=2\) in each broad type. The average conditional variance is therefore 2. The variance of the broad-type means is \(0.5(2-4)^2+0.5(6-4)^2=4\). The marginal variance is \(2+4=6\).

Now condition on the four subgroups instead. The expected conditional variance is 1. The variance of the four conditional means around their overall mean of 4 is

$$ 0.25(1-4)^2+0.25(3-4)^2+0.25(5-4)^2+0.25(7-4)^2 =0.25(9+1+1+9)=5. $$

The total is again \(1+5=6\) squared counts. The finer grouping moved 1 variance unit from the conditional-variance component to the conditional-mean component. The marginal variance did not change; only the accounting relative to the conditioning variable changed.

Estimation, Diagnostics, and Common Pitfalls

The law is an exact population identity. In data, its terms must be estimated, and estimates can be unstable when groups are small or when the conditional mean varies in ways not captured by \(Z\). A practical calculation should make the target cell population and conditioning variables explicit. It should also check whether each group has enough observed cells to estimate its mean and variance usefully, and whether the grouping leaves important systematic differences in counts within groups.

For discrete groups with probabilities \(p_z\), the population terms can be written as

$$ \sum_z p_z\operatorname{Var}(Y\mid Z=z) \quad\text{and}\quad \sum_z p_z\{E(Y\mid Z=z)-E(Y)\}^2. $$

In a sample, replacing probabilities and conditional moments by estimates does not guarantee exact equality for every chosen estimator. For example, sample variances commonly use denominator \(n-1\), whereas a direct decomposition of observed squared deviations may use denominator \(n\). When reporting an empirical decomposition, state the estimator and denominator, and check numerically that the reported components use a consistent convention.

  • Using one group’s variance instead of an average. The first term averages conditional variances using the distribution of \(Z\); it is not simply the variance in a convenient or largest group.
  • Forgetting the variance of the conditional means. Averaging within-group variances alone omits between-group differences and generally does not equal the marginal variance.
  • Treating the split as intrinsic. The components depend on the conditioning variable. Changing the grouping can change each component even though their sum stays fixed.
  • Claiming a mechanism from the identity. A large component is not, by itself, proof of a particular biological or technical cause. The identity is descriptive and does not establish causation.

A direct marginal variance calculation is useful when the only question is how variable counts are overall. The total-variance decomposition answers a different question: how much of that variance is average variability conditional on the selected information, and how much reflects differences in conditional means? Stratified summaries can help display those differences, but they should be interpreted alongside group sizes and the definition of the target population. The earlier material on sampling noise and biological variation motivates separating kinds of variation; this identity provides a precise accounting framework without assigning causes to the terms.

The main limitation is that the identity does not choose an appropriate \(Z\), supply reliable estimates from sparse data, or identify a count distribution. It also does not imply that conditioning removes measurement noise or that the conditional variance has the same value in all groups. Those are additional modeling or scientific questions. The useful diagnostic is to verify both the arithmetic and the interpretation: the weighted conditional variances plus the variance of the conditional means must equal the marginal variance under the same population definition.

Key takeaway: For a count \(Y\) and a conditioning variable \(Z\), marginal variance is exactly the expected conditional variance plus the variance of the conditional mean. The identity requires finite second moments, not a particular count model, and its component labels must be interpreted relative to \(Z\).

Check Your Understanding

Work through these questions, then compare your reasoning with the solutions.

  1. A hypothetical gene count has conditional means 1 and 5 in two equally common groups, and conditional variance 2 in each group. Find the marginal mean, both variance components, and the marginal variance.
  2. In a cell population, the expected conditional variance is 3 squared counts and the marginal variance is 11 squared counts. What is the variance of the conditional mean?
  3. Does the law of total variance require \(Y\) to follow a Poisson distribution? State the relevant moment condition.
  4. Why can a finer cell grouping change the two terms without changing the marginal variance?

Solutions. (1) The marginal mean is \(0.5(1)+0.5(5)=3\) counts per cell. The expected conditional variance is \(0.5(2)+0.5(2)=2\) squared counts. The variance of the conditional means is \(0.5(1-3)^2+0.5(5-3)^2=0.5(4)+0.5(4)=4\) squared counts. The marginal variance is \(2+4=6\) squared counts. (2) Rearranging the identity gives \(11-3=8\) squared counts. (3) No distributional form is required. \(Y\) must have a finite second moment, with the stated conditional moments defined. (4) A finer grouping can explain differences in conditional means that were previously included in within-group variance. The identity still sums the components to the same marginal variance of \(Y\).

Clinical Biostats