VIEW THIS AS

Auto mode follows the Route Engine until you choose a viewpoint.

YOU ARE HERE

ROUTE CHECK

CONNECTED TO

WHAT NEXT

Use the canonical route for this room, or HELP if you are unsure.

Why Mathematics? | scVI for Single-Cell Modelling: Variational Inference, Negative Binomial Counts, Latent Variables and Batch Correction

WHY MATHEMATICS?

Why Mathematics? | scVI for Single-Cell Modelling: Variational Inference, Negative Binomial Counts, Latent Variables and Batch Correction

How scVI builds a probabilistic neural model of single-cell counts, approximates each cell posterior with variational inference, and uses the latent space for integration while preserving uncertainty.

Why mathematics is important becomes easier to see when a real decision depends on it. This guide follows the mechanism from definitions and dimensions to a worked calculation, validation, practical learning and responsible use. It keeps factual evidence separate from interpretation and treats every illustrative scenario as an example rather than a promise of results.

Choose a reading route

  • Start with counts, batches and independent donors
  • Build a probabilistic generative model
  • Approximate posteriors with variational inference
  • Validate likelihood, biology and downstream decisions

Chapter map

  1. 1. Why counts need a probability model
  2. 2. The negative-binomial family
  3. 3. Zero inflation and likelihood choices
  4. 4. Latent biological state
  5. 5. Library size as a latent or observed quantity
  6. 6. The neural decoder
  7. 7. The encoder and amortised inference
  8. 8. Variational inference
  9. 9. The evidence lower bound
  10. 10. Mini-batches and stochastic optimisation
  11. 11. Batch covariates in the model
  12. 12. Posterior predictive checks
  13. 13. Differential-expression uncertainty
  14. 14. Latent-space integration
  15. 15. A student probabilistic project
  16. 16. Governance and careers

Visit the How Mathematics Works hub for the wider series and its connections to education, reasoning, science, computing, design and careers.


1. Why counts need a probability model

This is where modelling choices begin to affect evidence. Single-cell RNA counts vary because of biology, sequencing depth and technical sampling. A probabilistic model makes those sources explicit.

A concrete situation

Two similar cells can have different totals and many zeros.

What the mathematics does not license

A zero does not always mean a gene is biologically absent. That distinction matters because a correct formula can still answer the wrong operational question.

Try it, check it, transfer it

Plot mean and variance for several genes. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


2. The negative-binomial family

The central mathematical idea is concrete. The negative binomial models counts with variance greater than the mean through a dispersion parameter.

A concrete situation

A gene with mean five can vary far more than a Poisson count.

What the mathematics does not license

One dispersion rule may not fit every gene or state. The habit of checking assumptions is one of the practical benefits of learning mathematics.

Try it, check it, transfer it

Calculate variance mu plus mu-squared over theta. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


3. Zero inflation and likelihood choices

Here the geometry becomes operational. The original framework can use zero-inflated negative-binomial or related count likelihoods. The choice is an assumption to test.

A concrete situation

A dropout component adds extra zeros beyond the count model.

What the mathematics does not license

Flexible zero models can be weakly identified. The same reasoning helps students move from an exercise to a real dataset.

Try it, check it, transfer it

Compare held-out likelihood under documented choices. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


4. Latent biological state

This step deserves a careful pause. Each cell receives a low-dimensional latent variable z drawn from a prior. The decoder connects z and covariates to expression rates.

A concrete situation

Nearby z values can represent similar cell programmes.

What the mathematics does not license

Latent coordinates are model-dependent, not direct physical measurements. That distinction matters because a correct formula can still answer the wrong operational question.

Try it, check it, transfer it

Draw the generative arrows from z and batch to counts. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.

Did You Know? In this chapter, the most important number is useful only together with its construction rule. Changing scaling, sample membership or validation design can change the numerical answer without changing the arithmetic.


5. Library size as a latent or observed quantity

The useful question is not only how to calculate, but what the calculation assumes. Sequencing depth changes total counts. scVI models library size so biological state is not forced to explain every total.

A concrete situation

Two cells with similar proportions can have different totals.

What the mathematics does not license

Depth and biology can remain confounded in weak designs. The habit of checking assumptions is one of the practical benefits of learning mathematics.

Try it, check it, transfer it

Compare count proportions and totals for two cells. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


6. The neural decoder

A small example makes the mechanism visible. A neural network maps latent state and batch information to parameters of the gene-count distribution.

A concrete situation

Nonlinear layers can represent interactions among latent programmes.

What the mathematics does not license

Flexibility can fit nuisance or noise. The same reasoning helps students move from an exercise to a real dataset.

Try it, check it, transfer it

List inputs, outputs and dimensions for one decoder pass. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


7. The encoder and amortised inference

This is where modelling choices begin to affect evidence. The encoder maps observed counts to approximate posterior parameters for z and library size. One trained network handles many cells.

A concrete situation

A cell receives a mean and variance for its latent posterior.

What the mathematics does not license

Approximate posteriors can be miscalibrated. That distinction matters because a correct formula can still answer the wrong operational question.

Try it, check it, transfer it

Sample two z values from a toy Gaussian posterior. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


8. Variational inference

The central mathematical idea is concrete. Exact posterior calculation is difficult, so scVI optimises a tractable approximation. The reparameterisation trick supports gradient learning.

A concrete situation

Write z=mu+sigma epsilon with epsilon standard normal.

What the mathematics does not license

Low-variance gradients do not guarantee correct model assumptions. The habit of checking assumptions is one of the practical benefits of learning mathematics.

Try it, check it, transfer it

Generate three samples from chosen mu and sigma. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.

Did You Know? In this chapter, the most important number is useful only together with its construction rule. Changing scaling, sample membership or validation design can change the numerical answer without changing the arithmetic.


9. The evidence lower bound

Here the geometry becomes operational. ELBO equals expected log likelihood minus a KL-divergence regulariser under the common formulation. Maximising it balances reconstruction and prior conformity.

A concrete situation

A model can improve reconstruction by encoding more detail in z.

What the mathematics does not license

ELBO values are not directly comparable across every preprocessing or likelihood. The same reasoning helps students move from an exercise to a real dataset.

Try it, check it, transfer it

Compute a toy fit-minus-penalty value. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


10. Mini-batches and stochastic optimisation

This step deserves a careful pause. Training uses subsets of cells to estimate gradients, making large datasets feasible. Shuffling and donor structure affect evaluation.

A concrete situation

A mini-batch can contain cells from several experiments.

What the mathematics does not license

Random cell validation leaks donor patterns. That distinction matters because a correct formula can still answer the wrong operational question.

Try it, check it, transfer it

Separate optimisation mini-batches from donor-level test splits. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


11. Batch covariates in the model

The useful question is not only how to calculate, but what the calculation assumes. Batch enters the decoder or related components so the latent representation need not encode every technical difference.

A concrete situation

The same z can decode differently by batch.

What the mathematics does not license

If condition and batch are confounded, the model cannot know which to remove. The habit of checking assumptions is one of the practical benefits of learning mathematics.

Try it, check it, transfer it

Cross-tab batch, donor and condition before training. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


12. Posterior predictive checks

A small example makes the mechanism visible. Generate replicated counts from the fitted model and compare distributions, zeros, totals and gene correlations with observed data.

A concrete situation

A good model should reproduce key count summaries.

What the mathematics does not license

Good global fit can hide poor rare-state fit. The same reasoning helps students move from an exercise to a real dataset.

Try it, check it, transfer it

Check predictive summaries by donor and cell type. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.

Did You Know? In this chapter, the most important number is useful only together with its construction rule. Changing scaling, sample membership or validation design can change the numerical answer without changing the arithmetic.


13. Differential-expression uncertainty

This is where modelling choices begin to affect evidence. Posterior distributions can compare expression rates between cell groups with uncertainty. Group definitions and donor replication remain crucial.

A concrete situation

Posterior samples can estimate a probability of a fold-change threshold.

What the mathematics does not license

Thousands of cells do not create thousands of independent people. That distinction matters because a correct formula can still answer the wrong operational question.

Try it, check it, transfer it

Aggregate or model donor-level evidence explicitly. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


14. Latent-space integration

The central mathematical idea is concrete. The posterior mean of z supports visualisation, neighbours and clustering across batches.

A concrete situation

Shared cell types can overlap in z.

What the mathematics does not license

A smooth latent map can still overcorrect condition-specific states. The habit of checking assumptions is one of the practical benefits of learning mathematics.

Try it, check it, transfer it

Compare raw markers and held-out-donor neighbourhoods. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


15. A student probabilistic project

Here the geometry becomes operational. Simulate negative-binomial counts from two latent groups and two library sizes. Fit a simple latent model.

A concrete situation

Known truth reveals recovery and calibration.

What the mathematics does not license

A toy model omits gene networks and platform effects. The same reasoning helps students move from an exercise to a real dataset.

Try it, check it, transfer it

Vary dispersion and confounding to find failure. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.


16. Governance and careers

This step deserves a careful pause. Version counts, genes, covariates, architecture, priors, seeds, checkpoints and posterior summaries. Deep generative modelling joins probability, optimisation and biology.

A concrete situation

Students can start with distributions and the reparameterisation equation.

What the mathematics does not license

A latent model guarantees no diagnosis, mechanism or career outcome. That distinction matters because a correct formula can still answer the wrong operational question.

Try it, check it, transfer it

Write a model card with calibration and coverage limits. Then explain the result to someone who has not seen the formula. If the explanation needs a hidden assumption, add that assumption to the model record.

Did You Know? In this chapter, the most important number is useful only together with its construction rule. Changing scaling, sample membership or validation design can change the numerical answer without changing the arithmetic.


A seven-day practice lab

  1. Day 1: define the decision, observation unit, X variables and Y or reference condition.
  2. Day 2: reproduce the smallest worked example and check every dimension, unit and sign.
  3. Day 3: build a transparent baseline and write what it assumes.
  4. Day 4: fit the main method inside a training-only pipeline; record every parameter.
  5. Day 5: challenge one assumption with a deliberately difficult dataset or operating regime.
  6. Day 6: compare validation metrics with the practical workload created by errors.
  7. Day 7: present one supported conclusion, one unresolved uncertainty and one safe next experiment.

Questions for a student, parent or mentor

  • Which part of the model is a mathematical definition, and which part is a choice?
  • What small calculation would catch a sign, scale or dimension error?
  • What evidence comes from training, validation and the untouched test?
  • What benign condition could look like a fault or prediction failure?
  • How would the answer change for another response, asset, school project or time period?
  • What should a human review before acting on the score?

A method-specific audit before deployment

An scVI audit begins with count matrices, batch and sample metadata, a documented gene set and an explicit downstream task. The generative model uses latent variables for biological state and library size, a neural decoder for expected expression and a negative-binomial family for overdispersed counts. Batch information enters the model rather than being erased by a single subtraction.

Record train-validation-test boundaries, donor grouping, architecture, latent dimension, likelihood choice, priors, optimiser, random seeds, early stopping and posterior summaries. The encoder performs amortised variational inference, so every cell receives approximate posterior parameters. A high evidence lower bound is evidence about the fitted probabilistic model, not proof of biological truth.

Validate reconstruction, held-out likelihood, batch mixing, biological conservation, posterior calibration and downstream decisions on untouched donors or studies. Deep generative flexibility can absorb signal or nuisance depending on design. Preserve raw counts and compare with simpler baselines under identical splits.


Frequently asked questions

What is scVI?

It is a probabilistic deep generative framework for modelling single-cell count data with latent variables, batch covariates and amortised variational inference.

Why use a negative-binomial model?

Single-cell counts are overdispersed: their variance can exceed their mean. The negative-binomial family can represent this extra variability.

What is the latent variable z?

It is a low-dimensional random representation intended to capture recurring cell-state structure under the fitted model.

What does the encoder do?

It maps a cell’s observed counts and covariates to parameters of an approximate posterior distribution over latent variables.

What is the ELBO?

The evidence lower bound balances expected data fit with a divergence that regularises the approximate posterior toward the prior.

Does scVI prove biological mechanisms?

No. It models distributions and supports downstream evidence; mechanism requires experiments and biological validation.


Primary source and fact-check note

Romain Lopez, Jeffrey Regier, Michael B. Cole, Michael I. Jordan and Nir Yosef, “Deep generative modeling for single-cell transcriptomics,” Nature Methods 15, 1053–1058 (2018), DOI 10.1038/s41592-018-0229-2. Definitions and core claims in this article were checked against this primary publication. Calculations shown here were independently recomputed. Examples are educational illustrations; they are not reported industrial performance results.

Useful next reading

Keep the options open. Mathematics education supports clearer questions, transferable problem-solving skills and informed choices across science, engineering, computing, AI, statistics, design and many other careers. It does not guarantee intelligence, income, admission or employment; those outcomes depend on far more than one subject or model.

Extended laboratory: validation, transfer and responsible use

Count provenance

Archive raw count generation, gene filters, cell-quality rules, donor identities and batch covariates. Log-normalised values are not interchangeable with counts in a count likelihood.

Likelihood choice

Compare negative-binomial and justified alternatives on held-out cells or donors. Report dispersion conventions and zero handling. Do not select a likelihood solely for a nicer embedding.

Architecture ledger

Record encoder and decoder layers, activations, latent dimension, covariate injection and library-size treatment. Architecture is part of the statistical model.

Optimisation trace

Save seeds, mini-batches, learning rates, ELBO curves, early stopping and checkpoints. Convergence of training loss does not certify posterior calibration.

Did You Know? A sophisticated adjustment can be algebraically correct and still answer the wrong scientific question when samples, labels, batches or validation boundaries are wrong.

Posterior calibration

Use simulated or controlled data to compare posterior intervals with known truth. Overconfident approximate posteriors can make downstream evidence look stronger than it is.

Predictive checks

Generate replicated counts and compare totals, zeros, means, variances and gene correlations by donor and cell state. Global fit can hide local failure.

Batch-confounding test

Cross-tab batch with condition and donor. If they are inseparable, no latent model can identify which signal is technical without additional assumptions.

Latent stability

Repeat seeds and nearby dimensions. Compare neighbourhoods, cluster assignments and posterior means. Stable downstream conclusions matter more than rotated coordinates.

Did You Know? A sophisticated adjustment can be algebraically correct and still answer the wrong scientific question when samples, labels, batches or validation boundaries are wrong.

Rare-cell support

Inspect reconstruction and posterior uncertainty for rare states. A dense majority can dominate optimisation and make rare cells look artificially uncertain or absorbed.

Differential-expression boundary

Use donor-aware group definitions and report posterior thresholds, effect sizes and sensitivity. Cell-level posterior samples do not replace biological replication.

Baseline comparison

Compare PCA, a negative-binomial GLM or another simple model under identical splits. Deep complexity should earn its place through predictive and biological evidence.

Reader checkpoint

A complete scVI report names counts, likelihood, architecture, latent size, priors, optimisation, predictive checks, donor holdout and downstream uncertainty.

Did You Know? A sophisticated adjustment can be algebraically correct and still answer the wrong scientific question when samples, labels, batches or validation boundaries are wrong.

Deep dive: how to stress-test the mathematical claim

Deep dive 1

Fit a simulated dataset with known z and dispersion to test posterior recovery and calibration.

Deep dive 2

Hold out entire donors and genes where appropriate. Random cells overstate generalisation.

Deep dive 3

Inspect posterior predictive failure by gene abundance and cell type. Average likelihood can hide systematic bias.

Deep dive 4

Vary latent dimension and prior strength. Stable biology should not require one narrow setting.

Deep dive 5

Compare posterior means with uncertainty. A neat map using means can hide broad overlapping posteriors.

Deep dive 6

Test a batch-confounded condition simulation. The model cannot learn a separation that the design does not identify.

Deep dive 7

Evaluate rare cells separately; stochastic optimisation may favour abundant populations.

Deep dive 8

Report differential-expression effect thresholds and donor replication, not only posterior probabilities.

Deep dive 9

Use out-of-distribution checks before mapping a new technology into z.

Deep dive 10

Treat ELBO, representation, clustering and causal claims as separate evidence levels.

Worked evidence notebook

NB variance

For mean mu and inverse dispersion theta, calculate mu plus mu-squared divided by theta.

Reparameterisation

With mu=1, sigma=0.5 and epsilon=-1, compute z=0.5.

ELBO ledger

Record expected log likelihood, KL term and total bound per validation epoch.

Predictive check

Compare observed and replicated zero rates, totals, means and variances.

Did You Know? A sophisticated adjustment can be algebraically correct and still answer the wrong scientific question when samples, labels, batches or validation boundaries are wrong.

Posterior interval

Draw latent or expression-rate samples and calculate an interval with its assumptions.

Decision note

Separate posterior evidence, donor replication, biological interpretation and mechanism.

Practice, misconception and transfer

A parent-friendly starting point

Start with count variation, averages and probability distributions, then show how a latent variable describes hidden structure without being directly observed.

A key misconception

A deep model does not remove uncertainty. scVI represents uncertainty, and that representation must itself be checked.

When to pause

Pause when donors are too few, conditions are confounded with batches or count provenance is unclear.

A useful extension

Simulate known counts and test interval coverage. This makes calibration concrete.

Did You Know? A sophisticated adjustment can be algebraically correct and still answer the wrong scientific question when samples, labels, batches or validation boundaries are wrong.

Career transfer

Variational inference appears in generative AI, robotics and science, but reliable use always depends on data design and evaluation.

Final learning move

Ask the learner to explain ELBO as fit minus regularisation and identify what the model can never prove.

A final worked scenario

A final probabilistic scenario

Counts from six donors and two technologies are split by donor. scVI fits a negative-binomial generative model with batch covariates, passes posterior predictive checks for common cell types and improves held-out integration over PCA. A rare state has wider posterior uncertainty and poorer reconstruction, so its conclusions are narrowed. Differential expression uses donor-aware groups and effect thresholds. The result is strong because likelihood, uncertainty, biology and decision evidence are reported separately. A smooth latent map is one output, not a claim that the model discovered the biological mechanism.

Mathematical workshop: mechanisms, evidence and limits

Why count likelihoods matter

Single-cell RNA measurements are non-negative counts with variance that often exceeds the mean. A Gaussian model with constant variance can misrepresent both small counts and highly variable genes. The negative-binomial distribution allows variance mu+mu squared over theta, so dispersion changes how surprising a count is. When mu=4 and theta=2, the variance is 4+16/2=12. That is three times the mean. This calculation explains why the likelihood influences uncertainty, latent structure and downstream comparisons; it is not merely an implementation detail.

A generative story in steps

A scVI-style generative story first draws a latent variable for the cell, then combines it with observed batch covariates through a decoder to produce gene-specific rates, and finally draws counts from a probability distribution. The encoder runs in the reverse direction for inference: it takes observed counts and returns parameters of an approximate posterior over the latent variable. Keeping these directions separate prevents a common misconception. The network does not directly discover a label. It parameterises probability distributions, and every downstream label or cluster adds another decision layer that needs its own validation.

Reparameterisation and gradients

To train with gradient methods, a latent sample can be written as z=mu+sigma times epsilon, where epsilon comes from a fixed standard normal distribution. If mu=1, sigma=0.5 and epsilon=-1, then z=0.5. Randomness now enters through epsilon while mu and sigma remain differentiable. This reparameterisation is a mathematical bridge between probability and optimisation. Students can repeat it for several epsilon values, estimate a mean and interval, and see why a posterior distribution carries more information than a single plotted coordinate.

ELBO as a negotiated objective

The evidence lower bound combines expected log likelihood with a penalty for moving the approximate posterior away from its prior. Better reconstruction raises the first term; overly complicated or unregularised latent codes increase the divergence term. Optimisation seeks a balance. A rising ELBO indicates progress on this objective, but it does not prove calibrated uncertainty, correct biology or usefulness on new donors. Validation therefore includes held-out likelihood, posterior predictive checks, downstream tasks and comparison with simpler models. The objective is evidence about training, not a certificate for every later claim.

Did You Know? A sophisticated adjustment can be algebraically correct and still answer the wrong scientific question when samples, labels, batches or validation boundaries are wrong.

Posterior predictive checks

After fitting, draw latent variables and replicated counts from the model. Compare replicated and observed library sizes, zero fractions, gene means, variances and correlations within donors and cell states. If replicated common cells look realistic while rare cells have far too few marker counts, the global average can hide an important failure. Posterior predictive checks localise that mismatch. They also help decide whether to change the likelihood, dispersion model, feature set or architecture. A model that cannot reproduce relevant measurements should not be trusted merely because its embedding is smooth.

Batch covariates cannot fix confounding

Suppose every treated sample was measured with technology A and every control with technology B. Treatment and technology are perfectly confounded. Supplying technology as a batch covariate does not create information that distinguishes the two causes. A flexible model may remove treatment, retain technology or distribute the signal between them depending on assumptions. The study design, not network depth, determines identifiability. Reports should cross-tab donors, conditions and batches before fitting, name confounded comparisons and avoid causal language where the design cannot support it.

Uncertainty in downstream decisions

A posterior probability or Bayes factor becomes useful only after the decision rule is stated. For differential expression, define the effect size of interest, donor groups, posterior threshold and treatment of multiple genes. Report results across reasonable thresholds and compare with donor-aware baselines. Cell-level posterior samples do not turn thousands of cells from one donor into thousands of independent biological replicates. This distinction protects against pseudoreplication and helps students see that statistical confidence depends on the sampling unit as well as the number of observations.

A careful claim from a deep model

A defensible conclusion might say that under a specified negative-binomial model, latent dimension and donor-held-out split, scVI improved predictive fit and cell-state retrieval relative to PCA, while rare-state posterior checks remained weak. The rare-state conclusions were therefore limited. This sentence credits the model for what it demonstrated and keeps uncertainty visible. It does not say that the latent coordinates are physical pathways, that batch effects vanished, or that a deep network established mechanism. Precision makes the result reusable and scientifically honest.

Did You Know? A sophisticated adjustment can be algebraically correct and still answer the wrong scientific question when samples, labels, batches or validation boundaries are wrong.

Method checklist and conclusion boundary

Reproducibility checklist

Freeze counts, genes, covariates, likelihood, architecture, priors, optimiser, seeds and checkpoint. Use donor holdouts, predictive checks, calibration and baseline comparison. Reproduce one ELBO and posterior sample.

Careful conclusion

A careful conclusion says that a probabilistic latent model fit specified count structure and supported validated downstream tasks. It does not claim that z is a physical mechanism or that batch-confounded biology was identified.

Final evidence checks

Final evidence checks 1

Preserve raw counts and provenance.

Final evidence checks 2

Report likelihood and dispersion choices.

Final evidence checks 3

Use donor-level validation.

Final evidence checks 4

Check posterior predictive fit and calibration.

Final evidence checks 5

Version checkpoints and environments.

Final evidence checks 6

Require independent reproduction of one posterior calculation.