PIXELBANKv8.2.1
Menu
Back to Diffusion Models Study Plan
Week 2

Chapter 2: Mathematical Foundations

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.

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

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

The Gaussian (normal) distribution is the most important distribution in diffusion models. Every forward step adds Gaussian noise, and the reverse process predicts Gaussian parameters. The remarkable algebraic properties of Gaussians---closure under addition, linear transformation, conditioning, and marginalization---make the entire diffusion framework analytically tractable.

A deep understanding of multivariate Gaussians, their parameterizations, and the reparameterization trick is essential before proceeding to later chapters.

In this topic

1Univariate and Multivariate Gaussian
2Closure Under Linear Transformations
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 mean μ\mu and variance σ2\sigma^2. 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}. In diffusion models, the covariance is typically isotropic: Σ=σ2I\boldsymbol{\Sigma} = \sigma^2 \mathbf{I}, meaning noise is added independently to each dimension with equal variance.

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 xN(μ,Σ), then Ax+bN(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 remains Gaussian. This is fundamental to diffusion: the forward step xt=αtxt1+1αtϵ\mathbf{x}_t = \sqrt{\alpha_t}\,\mathbf{x}_{t-1} + \sqrt{1 - \alpha_t}\,\boldsymbol{\epsilon} is a linear combination of a Gaussian input and Gaussian noise, so xt\mathbf{x}_t is also Gaussian. This closure property allows us to compute q(xtx0)q(\mathbf{x}_t | \mathbf{x}_0) in closed form for any tt without iterating through intermediate steps.

Mathematical Intuition

Closure means the family of Gaussians is preserved under affine maps: if xN(μ,Σ)x \sim \mathcal{N}(\mu, \Sigma), then Ax+bN(Aμ+b,AΣAT)Ax + b \sim \mathcal{N}(A\mu + b, A\Sigma A^T). In DDPM, xt=αˉtx0+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 xN(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 non-differentiable with respect to μ\boldsymbol{\mu} and σ\boldsymbol{\sigma}. The reparameterization trick rewrites the sample as a deterministic function of the parameters plus fixed noise ϵ\boldsymbol{\epsilon}. This makes the sampling operation differentiable, enabling backpropagation through the sampling step. It is essential for training both VAEs and diffusion models.

Mathematical Intuition

Sampling xN(μ,σ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 ExN(μ,σ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(xtx0)=N(xt;αˉtx0,  (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(xtxt1)=N(xt;αtxt1,  (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)
Mathematical Foundations — Diffusion Models | PixelBank