2 Probability and Statistics
Generative modelling is, at its mathematical core, the study of probability distributions. Every generative model, whether a Gaussian mixture, a variational autoencoder, or a diffusion model, is ultimately an attempt to represent, approximate, or sample from a probability distribution over high-dimensional data. This chapter reviews the probability and statistics needed to make that statement precise.
We assume the reader has encountered probability in an introductory course and focus on developing the concepts that will recur throughout the book. Readers comfortable with multivariate distributions, the change-of-variables formula, and maximum likelihood estimation may skim this chapter and return to it as a reference.
Random Variables and Distributions
Definition 1 (Random Variable).
A random variable is a measurable function from a sample space to the real numbers . Informally, it assigns a numerical value to each outcome of a random experiment.
Random variables come in two flavours:
Discrete random variables take on a countable set of values (e.g., the number of heads in 10 coin tosses).
Continuous random variables take on values in an uncountable set, typically an interval of (e.g., the intensity of a pixel in a photograph).
Probability Mass Functions
A discrete random variable is characterised by its probability mass function (PMF):
Definition 2 (Probability Mass Function).
The PMF of a discrete random variable is the function defined by It satisfies for all and .
Example 1 (Fair Die).
Rolling a fair six-sided die defines a random variable with PMF for each .
Probability Density Functions
A continuous random variable is characterised by its probability density function (PDF):
Definition 3 (Probability Density Function).
The PDF of a continuous random variable is a non-negative function such that for any interval , The PDF satisfies .
Remark 1.
A common source of confusion: is not a probability. It is a probability density; it can exceed 1. Only the integral of over an interval gives a probability. For instance, a uniform distribution on has for .
Cumulative Distribution Functions
Definition 4 (Cumulative Distribution Function).
The cumulative distribution function (CDF) of a random variable is For continuous random variables, , so .
Common Discrete Distributions
Bernoulli Distribution
The simplest random experiment has two outcomes: success (1) or failure (0).
Definition 5 (Bernoulli Distribution).
A random variable follows a Bernoulli distribution with parameter , written , if
Insight.
A single pixel in a binary (black-and-white) image can be modelled as a Bernoulli random variable. A generative model for binary images assigns a Bernoulli parameter to each pixel ; this is exactly what the output layer of a VAE decoder does when generating binary MNIST digits.
Binomial Distribution
Definition 6 (Binomial Distribution).
If , then with PMF
Geometric Distribution
Definition 7 (Geometric Distribution).
A random variable models the number of trials until the first success: The geometric distribution is the only discrete distribution with the memoryless property: .
Poisson Distribution
Definition 8 (Poisson Distribution).
A random variable with rate parameter has PMF It models the number of events occurring in a fixed interval of time or space, given a constant average rate .
Example 2 (Word Counts).
The number of times a particular word appears in a document of fixed length can be modelled as a Poisson random variable. Topic models and language models use this observation to build generative models of text.
Categorical and Multinomial Distributions
Definition 9 (Categorical Distribution).
A random variable taking values in follows a categorical distribution with parameters , where and , if .
Definition 10 (Multinomial Distribution).
If are i.i.d. , the vector of counts where follows a multinomial distribution: where .
Insight.
The categorical distribution is the workhorse of autoregressive language models. When GPT generates the next word (token), it produces a categorical distribution over a vocabulary of tokens. Sampling from this distribution gives the next word. The entire generative process is a sequence of categorical samples, one per token.
Common Continuous Distributions
Uniform Distribution
Definition 11 (Uniform Distribution).
A random variable has PDF
The uniform distribution serves as the “noise source” in many generative models: to sample from a complex distribution, we often start with uniform (or Gaussian) random numbers and transform them.
Gaussian (Normal) Distribution
The Gaussian distribution is the single most important distribution in generative modelling. It appears as the noise prior in GANs, VAEs, normalizing flows, and diffusion models; it defines the likelihood in Gaussian mixture models; and it is the stationary distribution of many stochastic processes.
Definition 12 (Gaussian Distribution).
A random variable has PDF where is the mean and is the variance.
The Gaussian distribution owes its ubiquity to several remarkable properties:
It is the maximum entropy distribution among all distributions with a given mean and variance: it makes the fewest assumptions beyond these two moments.
Sums of independent random variables converge to a Gaussian (Central Limit Theorem).
It is closed under linear transformations, marginalisation, and conditioning.
Its log-density is quadratic, making optimisation tractable.
Exponential Distribution
Definition 13 (Exponential Distribution).
A random variable with rate has PDF It is the continuous analogue of the geometric distribution and models waiting times.
Beta Distribution
Definition 14 (Beta Distribution).
A random variable with shape parameters has PDF where is the Beta function.
The Beta distribution is the conjugate prior for the Bernoulli parameter , making it central to Bayesian inference (see Bayesian Inference). It can model any shape on : uniform (), U-shaped (), or peaked ().
Expectation, Variance, and Moments
Expectation
Definition 15 (Expectation).
The expectation (or mean) of a random variable is More generally, for any function , or .
Key properties. Expectation is linear: for any constants and random variables , This holds even when and are not independent.
Variance and Standard Deviation
Definition 16 (Variance).
The variance of a random variable with mean is The standard deviation is .
Key properties. For constants : . For independent : .
Covariance and Correlation
Definition 17 (Covariance and Correlation).
The covariance of random variables and is The correlation coefficient normalises covariance to : If and are independent, then . The converse is not generally true.
Remark 2.
For a random vector , the covariance matrix is the matrix This matrix encodes the variance of each component (on the diagonal) and the pairwise correlations (off-diagonal). It is symmetric and positive semi-definite. The covariance matrix is a central object in this book: it defines the shape of Gaussian distributions, appears in the FID metric, and governs the geometry of latent spaces.
Moments and Moment Generating Functions
Definition 18 (Moments).
The -th moment of is . The -th central moment is . The first moment is the mean; the second central moment is the variance.
Definition 19 (Moment Generating Function).
The moment generating function (MGF) of is provided the expectation exists in a neighbourhood of . The -th moment can be recovered as . If two random variables have the same MGF, they have the same distribution.
Joint, Marginal, and Conditional Distributions
When modelling data, we almost always deal with multiple random variables simultaneously. An image has millions of pixel values; a sentence has dozens of token indices. Understanding the relationships between these variables requires joint, marginal, and conditional distributions.
Joint Distributions
Definition 20 (Joint Distribution).
For continuous random variables and , the joint PDF satisfies for any region , and .
Marginal Distributions
Given the joint distribution of , we can recover the distribution of alone by marginalising out : For discrete variables, replace the integral with a sum.
Key Idea.
Marginalisation is one of the most important operations in generative modelling. When we write the marginal likelihood of a latent-variable model as we are marginalising out the latent variable to obtain the distribution over observed data . The intractability of this integral for complex models is the central challenge that variational inference and VAEs are designed to address.
Conditional Distributions
Definition 21 (Conditional Distribution).
The conditional PDF of given is
Rearranging gives the product rule: . Applying this repeatedly to a vector yields the chain rule of probability: (Chain RULE) This decomposition is exact: no approximation is involved, and it is the foundation of autoregressive generative models (PixelCNN, GPT, WaveNet).
Independence
Definition 22 (Independence).
Random variables and are independent if and only if Equivalently, : knowing tells you nothing about .
Independence is a very strong assumption. In an image, pixels are not independent; neighbouring pixels are highly correlated. The gap between the independent (factored) model and the true joint distribution is precisely what generative models must learn to bridge.
Bayes' Theorem
Theorem 1 (Bayes' Theorem).
For continuous random variables and with ,
In the language of Bayesian inference: The denominator is a normalising constant that ensures the posterior integrates to 1. Computing this integral is often intractable, motivating the approximate inference methods developed in Chapter 8.
The Multivariate Gaussian Distribution
The multivariate Gaussian is the cornerstone of probabilistic generative modelling. It parameterises the prior in VAEs, defines the likelihood in Gaussian mixture models, serves as the base distribution in normalizing flows, and governs the noise process in diffusion models. We develop its properties in detail.
Definition 23 (Multivariate Gaussian Distribution).
A random vector follows a multivariate Gaussian (or normal) distribution, written , if its PDF is (MVN PDF) where is the mean vector and is a symmetric positive definite covariance matrix.
Geometry of the Multivariate Gaussian
The exponent is the Mahalanobis distance from to , measured in the metric defined by . The level sets of the density are ellipsoids centred at :
The axes of the ellipsoid are aligned with the eigenvectors of .
The lengths of the axes are proportional to the square roots of the eigenvalues.
If (all eigenvalues equal), the ellipsoid becomes a sphere, and the distribution is isotropic.
Marginals and Conditionals of a Gaussian
One of the most remarkable properties of the multivariate Gaussian is that both marginals and conditionals are again Gaussian. Partition with corresponding partition of the mean and covariance:
Proposition 1 (Marginal of a Gaussian).
The marginal distribution of is
Proposition 2 (Conditional of a Gaussian).
The conditional distribution of given is
Insight.
These formulas have a beautiful interpretation. The conditional mean is the best linear prediction of given : it shifts the prior mean by an amount proportional to how much deviates from its own mean. The conditional covariance (the Schur complement) is always smaller than the marginal covariance : observing always reduces our uncertainty about .
This property is used extensively in Gaussian processes, Kalman filters, and the derivation of the ELBO for linear Gaussian models.
Linear Transformations
Proposition 3 (Affine Transformation of a Gaussian).
If and for a matrix and vector , then
Remark 3.
This closure under affine transformations is one reason why the standard Gaussian is the default choice for the latent prior in generative models: any Gaussian can be obtained from via the transformation , where (Cholesky decomposition). This is the basis of the reparameterisation trick used in VAE training.
Fundamental Theorems and Inequalities
Law of Large Numbers
Theorem 2 (Weak Law of Large Numbers).
Let be i.i.d. random variables with mean and finite variance . Then the sample mean converges to in probability:
Proof.
By Chebyshev's inequality (thm:chebyshev below),
Key Idea.
The Law of Large Numbers justifies Monte Carlo estimation: we can approximate by averaging over i.i.d. samples. This is how we estimate the ELBO in VAE training and the adversarial loss in GAN training, by averaging over minibatches.
Central Limit Theorem
Theorem 3 (Central Limit Theorem).
Let be i.i.d. with mean and finite variance . Then the standardised sum converges in distribution to a standard Gaussian:
The CLT explains why the Gaussian appears so frequently in nature and in modelling: whenever a quantity is the result of many small, independent contributions, its distribution is approximately Gaussian.
Concentration Inequalities
The following inequalities provide non-asymptotic bounds on how random variables deviate from their expectations.
Theorem 4 (Markov's Inequality).
For any non-negative random variable and ,
Theorem 5 (Chebyshev's Inequality).
For any random variable with mean and variance , and any ,
Proof.
Apply Markov's inequality to the non-negative random variable with threshold :
Theorem 6 (Jensen's Inequality).
If is a convex function and is a random variable with finite expectation, then If is concave, the inequality reverses.
Insight.
Jensen's inequality is the mathematical engine behind the ELBO. Since is concave, Jensen gives . Applied to the marginal likelihood , this yields a tractable lower bound, the starting point for variational inference. We will derive this in detail in Chapter 8.
Theorem 7 (Cauchy–Schwarz Inequality).
For any random variables and with finite second moments, Equivalently, , which shows that .
Theorem 8 (Union Bound).
For any events ,
Change of Variables
The change-of-variables formula tells us how a probability distribution transforms under a mapping. This is one of the most important technical tools in this book: it is the mathematical foundation of normalizing flows and appears in the derivation of many other generative models.
Univariate Case
Theorem 9 (Change of Variables - Univariate).
Let be a continuous random variable with PDF , and let where is a strictly monotone, differentiable function. Then the PDF of is
The factor accounts for how stretches or compresses the probability mass. If expands a region, the density must decrease to keep the total probability equal to 1.
Example 3 (Log-Normal Distribution).
Let and . Since and , we get This is the log-normal distribution.
Non-Injective Transformations
When is not one-to-one (e.g., ), multiple values of map to the same . We sum over all pre-images: where the sum ranges over all branches of the inverse.
Example 4 (Chi-Squared from Standard Normal).
If and , both and map to . Each branch contributes , giving which is the distribution.
Multivariate Case
Theorem 10 (Change of Variables - Multivariate).
Let have density , and let where is a differentiable bijection. Then (COV Multivariate) where is the Jacobian matrix of .
Key Idea.
The multivariate change-of-variables formula is the mathematical heart of normalizing flows. A normalizing flow defines an invertible neural network and uses (COV Multivariate) to compute the exact likelihood of any data point : The challenge is designing architectures where both and its Jacobian determinant are efficient to compute. We will explore this in detail in the normalizing flows chapter.
Statistical Estimation
Having developed the probabilistic tools, we now turn to the question: given observed data, how do we estimate the parameters of a distribution? This is the problem that training a generative model must solve.
Point Estimation: Bias, Variance, and Consistency
Definition 24 (Estimator, Bias, and MSE).
An estimator of a parameter is a function of the observed data .
The bias is . If , the estimator is unbiased.
The mean squared error is .
Definition 25 (Consistent Estimator).
An estimator is consistent if as , i.e.,
Proposition 4.
If , then is consistent.
Proof.
By Markov's inequality, .
Maximum Likelihood Estimation
Definition 26 (Maximum Likelihood Estimator).
Given i.i.d. observations from a distribution , the likelihood function is The log-likelihood is . The maximum likelihood estimator (MLE) is
Example 5 (MLE for a Gaussian).
Let . The log-likelihood is Setting derivatives to zero gives the MLEs: The mean estimator is unbiased; the variance estimator has bias (dividing by instead of gives the unbiased version).
Key Idea.
Training a generative model by maximum likelihood is conceptually simple: we choose the parameters that make the observed training data most probable under the model. In practice, the log-likelihood is averaged over minibatches and optimised with stochastic gradient descent: This is the training procedure for autoregressive models, normalizing flows, and (with modifications) for VAEs and diffusion models.
Bayesian Inference
While MLE gives a single “best” parameter value, Bayesian inference maintains a full distribution over parameters, incorporating prior knowledge and quantifying uncertainty.
The setup: we have a prior expressing our beliefs before seeing data, and a likelihood . After observing data , Bayes' theorem gives the posterior:
Definition 27 (MAP Estimation).
The maximum a posteriori (MAP) estimate is the mode of the posterior:
Comparing with MLE: . The prior acts as a regulariser. For example, a Gaussian prior adds an penalty , which is precisely weight decay in neural network training.
Example 6 (Conjugate Bayesian Inference for a Bernoulli).
Suppose we observe successes in Bernoulli trials and use a prior on the success probability . The posterior is This is a conjugate update: the posterior is in the same family as the prior. As , the posterior concentrates around the MLE , and the prior becomes irrelevant. As , the MAP estimate approaches the MLE.
Remark 4.
The intractability of the posterior for complex models (deep neural networks have millions of parameters) is a central challenge in Bayesian deep learning. Variational inference, which we will develop in Chapter 8, approximates the posterior with a simpler distribution by minimising the KL divergence between them.
Exercises
Exercise 1.
Let be a discrete random variable taking values in with for . Compute the expectation and the variance .
Exercise 2.
A medical test for a rare disease has a sensitivity (true positive rate) of 0.95 and a specificity (true negative rate) of 0.90. The prevalence of the disease is 0.01.
Using Bayes' theorem, compute the probability that a person who tests positive actually has the disease.
Interpret your result. Why is the positive predictive value so low despite the test's high sensitivity?
How does this relate to the challenge of evaluating generative models? Hint: think about the base rate of “real” vs. “generated” images in a dataset.
Exercise 3.
Let .
Derive the maximum likelihood estimator .
Show that is unbiased.
Show that is consistent.
Exercise 4.
Let and have joint PDF for and otherwise.
Compute the marginal PDFs and .
Compute the conditional PDF .
Are and independent? Justify your answer.
Exercise 5.
Derive Chebyshev's inequality from Markov's inequality by applying Markov's inequality to the random variable .
Exercise 6.
Let with both and unknown.
Derive the MLEs and .
Show that is biased and compute its bias.
Propose an unbiased estimator of .
Exercise 7.
Let and . Use the change-of-variables formula for non-injective transformations to derive the PDF of . Identify the resulting distribution by name.
Exercise 8.
Let have prior , and suppose we observe where is independent of .
Show that the posterior is Gaussian and find its mean and variance.
Derive the MAP estimate and express it as a weighted average of and .
What happens as ? Interpret this limit.
What happens as ? Interpret this limit.
Exercise 9.
Let .
Find the marginal distributions of and .
Find the conditional distribution of .
Verify that the conditional variance is less than the marginal variance.
Exercise 10.
Let and define where .
Show that is invertible and find .
Compute the Jacobian matrix and its determinant.
Write the density using the change-of-variables formula.
Is a Gaussian distribution? Why or why not?
This exercise illustrates how a normalizing flow can transform a simple Gaussian into a non-Gaussian distribution using an invertible mapping.
Exercise 11.
Let be a positive random variable.
Using Jensen's inequality with (a concave function), show that .
Now let be a joint distribution and let be any distribution over . Show that This inequality is the evidence lower bound (ELBO), the foundation of variational inference.