v9.1.1
Menu

Chapter 2: Mathematical Foundations

Week 2

Master the essential mathematical toolkit underpinning diffusion models: Gaussian distributions, Markov chains, KL divergence, variational inference, stochastic differential equations, and Langevin dynamics. These foundations are critical for understanding both the theory and implementation of modern diffusion-based generative models.

Topics
3
Videos
3
Min read
19

Chapter Overview

Diffusion models rest on a surprisingly elegant mathematical framework. At their core, they define a forward process that gradually destroys data by adding Gaussian noise, and a learned reverse process that reconstructs data from pure noise. Understanding why this works---and how to train it efficiently---requires fluency in several branches of mathematics.

We begin with Gaussian distributions, the building block of the noise process. Every step of diffusion adds Gaussian noise, and the remarkable closure properties of Gaussians make the entire forward process analytically tractable. Next, Markov chains formalize the sequential structure: each noising step depends only on the previous state, enabling efficient factorization of joint distributions.

KL divergence measures how one probability distribution differs from another, and it is the natural loss function for training generative models. Variational inference provides the framework for optimizing intractable likelihoods by maximizing a tractable lower bound (the ELBO), which is exactly how diffusion models are trained.

Finally, the continuous-time perspective connects diffusion models to stochastic differential equations and Langevin dynamics. Score-based generative models use the score function (gradient of the log-density) to reverse the noising process, unifying denoising diffusion with classical MCMC sampling.

This chapter covers:

  • Gaussian Distributions: Properties that make the forward process analytically tractable
  • Markov Chains: The sequential structure underlying discrete diffusion steps
  • KL Divergence: The divergence measure used to train the reverse process
  • Variational Inference: Deriving the ELBO training objective
  • Stochastic Differential Equations: Continuous-time formulation of diffusion
  • Langevin Dynamics: Score-based sampling and its connection to diffusion models

Chapter Roadmap

Click any topic to jump in

Explainer video
1
Gaussian Distributions

Properties of Gaussians and reparameterization — the building block of diffusion.

Univariate and Multivariate GaussianClosure Under Linear TransformationsThe Reparameterization Trick
Process and objective

Sequential structure and optimization target

2
Markov Chains

Transition kernels and stationary distributions — the sequential structure of diffusion.

Transition ProbabilitiesStationary DistributionErgodicity and Mixing Time
3
KL Divergence

Measuring distribution distance — the objective connecting forward and reverse processes.

Definition and AsymmetryForward vs Reverse KLKL Between Gaussians

Topics

3 topics in this chapter

Before we can add noise to an image, we need to say precisely what noise means --- and in diffusion models the answer is always the same distribution: the Gaussian. This topic opens the mathematical-foundations chapter because everything downstream depends on it. Chapter 1 argued that diffusion models generate by reversing a gradual noising process; that process adds Gaussian noise at every step, and the reverse network predicts Gaussian parameters. Why does one distribution get to play both roles? Because Gaussians have algebraic properties no other distribution shares: they stay Gaussian under addition, under linear maps, under conditioning, and under marginalization. Those closure properties are what make the entire forward process solvable in closed form --- you can jump straight to any noise level without simulating the steps in between, a fact we will exploit constantly. This topic builds that toolkit: the univariate and multivariate density, closure under affine transformations, and the reparameterization trick that makes sampling differentiable. Master these and the DDPM equations in later chapters read as routine algebra rather than magic.

Definition

A Gaussian (normal) distribution N(μ,Σ)\mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma}) is the distribution over Rd\mathbb{R}^d specified entirely by a mean vector μ\boldsymbol{\mu} and covariance matrix Σ\boldsymbol{\Sigma}. It is closed under affine transformation, marginalization, and conditioning --- properties that make the diffusion forward process analytically tractable at every timestep.

In this topic

  1. 1Univariate and Multivariate Gaussian
  2. 2Closure Under Linear Transformations
  3. 3The Reparameterization Trick
1 of 3
Univariate and Multivariate Gaussian

N(x;μ,σ2)=12πσ2exp⁡(−(x−μ)22σ2)\mathcal{N}(x; \mu, \sigma^2) = \frac{1}{\sqrt{2\pi\sigma^2}} \exp\left(-\frac{(x - \mu)^2}{2\sigma^2}\right)

The univariate Gaussian is parameterized by a mean μ\mu and variance σ2\sigma^2; the density peaks at μ\mu and its width scales with σ\sigma. The multivariate generalization replaces the scalar mean with a vector μ∈Rd\boldsymbol{\mu} \in \mathbb{R}^d and the variance with a covariance matrix Σ∈Rd×d\boldsymbol{\Sigma} \in \mathbb{R}^{d \times d}, whose off-diagonal entries encode correlations between dimensions. In diffusion models the covariance is almost always isotropic, Σ=σ2I\boldsymbol{\Sigma} = \sigma^2 \mathbf{I}, so noise is added independently and equally to every dimension. This is deliberate: correlated noise would bias the forward process toward certain directions in data space, breaking the symmetry that later lets a single scalar schedule describe corruption.

Mathematical Intuition

The Gaussian is the maximum entropy distribution for a given mean and variance — it makes the fewest assumptions beyond those two moments. In diffusion, isotropic noise N(0,σ2I)\mathcal{N}(0, \sigma^2 I) treats all dimensions equally, which is essential: the forward process should not favor any direction in data space. The normalization constant 1/(2π)d∣Σ∣1/\sqrt{(2\pi)^d |\Sigma|} ensures the density integrates to 1 over Rd\mathbb{R}^d.

Example:

Write the PDF of a 2D isotropic Gaussian with mean μ=[1,2]T\boldsymbol{\mu} = [1, 2]^T and variance σ2=0.5\sigma^2 = 0.5.

2 of 3
Closure Under Linear Transformations

If x∼N(μ,Σ), then Ax+b∼N(Aμ+b,  AΣAT)\text{If } \mathbf{x} \sim \mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma}), \text{ then } \mathbf{A}\mathbf{x} + \mathbf{b} \sim \mathcal{N}(\mathbf{A}\boldsymbol{\mu} + \mathbf{b},\; \mathbf{A}\boldsymbol{\Sigma}\mathbf{A}^T)

Any affine transformation of a Gaussian random variable is again Gaussian: if x∼N(μ,Σ)\mathbf{x} \sim \mathcal{N}(\boldsymbol{\mu}, \boldsymbol{\Sigma}) then Ax+b\mathbf{A}\mathbf{x} + \mathbf{b} has mean Aμ+b\mathbf{A}\boldsymbol{\mu} + \mathbf{b} and covariance AΣAT\mathbf{A}\boldsymbol{\Sigma}\mathbf{A}^T. This is the property that makes diffusion tractable. The forward step xt=αt xt−1+1−αt ϵ\mathbf{x}_t = \sqrt{\alpha_t}\,\mathbf{x}_{t-1} + \sqrt{1 - \alpha_t}\,\boldsymbol{\epsilon} is an affine combination of a Gaussian input and independent Gaussian noise, so xt\mathbf{x}_t is Gaussian too. Applying closure repeatedly collapses the whole chain, letting us write q(xt∣x0)q(\mathbf{x}_t \mid \mathbf{x}_0) in closed form for any tt --- no need to iterate through intermediate steps, which is what makes training on random timesteps possible.

Mathematical Intuition

Closure means the family of Gaussians is preserved under affine maps: if x∼N(μ,Σ)x \sim \mathcal{N}(\mu, \Sigma), then Ax+b∼N(Aμ+b,AΣAT)Ax + b \sim \mathcal{N}(A\mu + b, A\Sigma A^T). In DDPM, xt=αˉt x0+1−αˉt ϵx_t = \sqrt{\bar{\alpha}_t}\,x_0 + \sqrt{1-\bar{\alpha}_t}\,\epsilon is affine in both x0x_0 and ϵ\epsilon. Since both are Gaussian, xtx_t is Gaussian with analytically computable moments — no simulation needed to characterize the distribution at any timestep.

Example:

If x∼N(0,I)\mathbf{x} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) and y=0.8x+0.6ϵ\mathbf{y} = 0.8\mathbf{x} + 0.6\boldsymbol{\epsilon} where ϵ∼N(0,I)\boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}) is independent, what is the distribution of y\mathbf{y}?

3 of 3
The Reparameterization Trick

x=μ+σ⊙ϵ,ϵ∼N(0,I)\mathbf{x} = \boldsymbol{\mu} + \boldsymbol{\sigma} \odot \boldsymbol{\epsilon}, \quad \boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I})

Sampling from N(μ,diag(σ2))\mathcal{N}(\boldsymbol{\mu}, \text{diag}(\boldsymbol{\sigma}^2)) is not differentiable with respect to μ\boldsymbol{\mu} and σ\boldsymbol{\sigma}: the randomness sits between the parameters and the output, blocking gradient flow. The reparameterization trick rewrites the sample as x=μ+σ⊙ϵ\mathbf{x} = \boldsymbol{\mu} + \boldsymbol{\sigma} \odot \boldsymbol{\epsilon} with fixed noise ϵ∼N(0,I)\boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}), moving the stochasticity to an input that carries no parameters. Now the sample is a deterministic, differentiable function of μ\boldsymbol{\mu} and σ\boldsymbol{\sigma}, so gradients pass straight through it. This is what makes end-to-end training of both VAEs and diffusion models possible; without it the sampling step would break backpropagation entirely.

Mathematical Intuition

Sampling x∼N(μ,σ2)x \sim \mathcal{N}(\mu,\sigma^2) is a stochastic operation with no gradient through μ\mu or σ\sigma. The trick x=μ+σϵx = \mu + \sigma\epsilon decomposes sampling into a deterministic differentiable path (multiply and add) plus fixed randomness (ϵ\epsilon). The pathwise gradient ∂x/∂μ=1\partial x/\partial \mu = 1 and ∂x/∂σ=ϵ\partial x/\partial \sigma = \epsilon enable backpropagation through the sampling step — critical for training both VAEs and diffusion models.

Example:

Show how to compute gradients of Ex∼N(μ,σ2)[x2]\mathbb{E}_{x \sim \mathcal{N}(\mu, \sigma^2)}[x^2] with respect to μ\mu using the reparameterization trick.

Theory Exercise

Problem:

In the DDPM forward process, q(xt∣x0)=N(xt;αˉt x0,  (1−αˉt)I)q(\mathbf{x}_t | \mathbf{x}_0) = \mathcal{N}(\mathbf{x}_t; \sqrt{\bar{\alpha}_t}\,\mathbf{x}_0,\; (1 - \bar{\alpha}_t)\mathbf{I}) where αˉt=∏s=1tαs\bar{\alpha}_t = \prod_{s=1}^{t} \alpha_s. Derive this closed-form expression starting from the single-step transition q(xt∣xt−1)=N(xt;αt xt−1,  (1−αt)I)q(\mathbf{x}_t | \mathbf{x}_{t-1}) = \mathcal{N}(\mathbf{x}_t; \sqrt{\alpha_t}\,\mathbf{x}_{t-1},\; (1 - \alpha_t)\mathbf{I}).

Hints:
  • Write x_t in terms of x_{t-1} and noise using the reparameterization trick
  • Recursively substitute x_{t-1} in terms of x_{t-2}, and so on down to x_0
  • Use the fact that a sum of independent Gaussians is Gaussian, with variances adding
  • Check that the coefficients squared sum to 1 at each step (variance preservation)