Stable Diffusion, DALL-E, Sora — every diffusion model is solving a stochastic differential equation. The "noise schedule" you've heard about is the diffusion coefficient. The "denoiser" is approximating a score function. This lesson is heavier than the rest of the track — if you're not headed toward generative models, you can skim. If you are, this is the bedrock under everything in track-08.
Learning Objectives
After this lesson, you will be able to:
Understand Brownian motion as the continuous-time limit of an infinite random walk, and write down its three defining properties — Gaussian increments, independence, continuity
Read and write stochastic differential equations of the form dx = f dt + g dW, identify drift and diffusion, and connect SDEs to the Fokker-Planck PDE that governs the marginal density
See how the score function ∇_x log p(x) — the same gradient-of-log-density object from MLE — drives Langevin sampling, score matching, and the entire score-based generative-modelling stack
Walk through the DDPM forward process from definition to its closed-form q(x_t | x_0), and recognise denoising score matching as the unifying objective behind every modern diffusion model
Why this is the climax of the math track -- the score ∇_x log p(x) from this lesson is the same gradient-of-log-density object you met as the MLE score; the forward chain is the same Markov chain you saw in N5; the closed-form q(x_t | x_0) is a multivariate Gaussian from N3; the loss is squared-error from N6 thanks to a Hyvärinen identity that uses Jensen-style tricks from information theory. Diffusion is where every previous lesson cashes in
Build this --> Build a "1-D Score Visualiser" that picks a target distribution (mixture of two Gaussians), trains a tiny score network with denoising score matching, then runs Langevin dynamics from random noise and animates the samples migrating to the data manifold -- the entire stack of Stable Diffusion, in 100 lines, on a 1-D toy
This lesson is the most advanced one in the math track. Skip it if you are not heading into diffusion models, score-based generative methods, or continuous-time RL theory -- you will not need it for classical ML, transformer training, or applied DL. Come back when you want to read Song et al. 2020, Ho et al. 2020, or any paper with dx_t in it.
Stochastic calculus is the language of randomness that flows through time. Newton's calculus tracks deterministic motion -- a planet's orbit, a falling ball. Stochastic calculus tracks motion that is jittered by noise at every instant. The mathematics looks like ordinary calculus until you take derivatives, and then a strange new term appears that has no analogue in the deterministic world. That term -- the Itô correction -- is what makes diffusion models possible.
The good news: you can do almost everything in modern ML without measure-theoretic stochastic calculus. The heuristic, "treat dW_t like a Gaussian random variable with variance dt," gets you 95% of the way. This lesson teaches the heuristic, points to where it breaks, and shows how the resulting machinery powers every diffusion paper of the last five years.
Try it! Open the Python REPL (bottom-right of the screen: click Quick Actions, then Python) and type these lines yourself.
You already know random walks from the Markov chain lesson. Here is the punch line of stochastic calculus: take a random walk, shrink the step size, speed up time, and you get Brownian motion.
Look at it. The viz below draws Brownian paths in real time — drag the drift and diffusion sliders to see how the same dW_t engine produces everything from pure noise to drifting random walks to Geometric Brownian Motion (the model behind every Black-Scholes options price).
A standard Brownian motion (or Wiener process) W_t satisfies:
W_0 = 0 (start at the origin)
Gaussian independent increments: for any s < t, W_t − W_s ~ N(0, t − s), and increments over disjoint time intervals are independent
Continuous paths (almost surely) -- but W_t is nowhere differentiable
Wt−Ws∼N(0,t−s)Var(Wt)=t
The non-differentiability is the strange part. If you zoom in on W_t, you see more wiggles, not a tangent line. The path has fractal-like structure all the way down. This is why ordinary calculus fails: there is no dW_t/dt to take a derivative of.
What Do You Think?
A standard Brownian motion W_t has Var(W_t) = t. You simulate W_T over the interval [0, 4] and over [0, 16]. How does the typical magnitude of W_T compare between the two simulations?
We want to integrate a function f(t) against the noise dW_t. Discretise time into steps t_0 < t_1 < ... < t_n = T and define:
∫0Tf(t)dWt≈i∑f(ti)(Wti+1−Wti)
Why "left endpoint" and not the midpoint? It matters. The Itô convention uses the left endpoint, which makes f(t_i) independent of W_{t_{i+1}} − W_{t_i} -- a property that yields clean martingale theory. Using the midpoint gives you the Stratonovich integral, which obeys ordinary chain-rule calculus but loses some of Itô's nice properties. ML almost universally uses Itô.
The proper definition needs measure theory (the integrand and dW_t live in different function spaces), but the discrete heuristic above is the only thing you need for ML papers.
Run a Brownian path yourself and verify Itô's most famous identity: the quadratic variation of W over [0, T] converges to T (not zero, as it would for a smooth function). This is the source of the "extra term" in Itô's lemma.
Now the strange part. Suppose X_t evolves according to dX_t = μ dt + σ dW_t, and you want to track Y_t = g(t, X_t) -- some smooth function of time and the noisy state. In ordinary calculus the chain rule gives dY = (∂g/∂t) dt + (∂g/∂x) dX. In stochastic calculus you get an extra term:
dYt=(∂t∂g+μ∂x∂g+21σ2∂x2∂2g)dt+σ∂x∂gdWt
The intuition: Brownian increments have (dW_t)² ≈ dt (their variance is dt), not zero. In a Taylor expansion of g(X + dX) around X, the (dX)² term -- which would normally be discarded as "higher order" -- contributes a non-negligible σ² dt. That is the Itô correction.
(Stratonovich calculus does obey the ordinary chain rule, at the cost of the integrand and the noise becoming statistically dependent. Some physics literature uses it; ML almost never does.)
Quick check
What is the practical difference between the Itô and Stratonovich interpretations of a stochastic integral for ML purposes?
Putting it together: a stochastic differential equation (SDE) is a recipe for evolving a noisy state through time:
dxt=f(xt,t)dt+g(t)dWt
A particular SDE worth memorising is the overdamped Langevin equation, where the drift is the gradient of a potential U(x):
Before reading on, let's simulate the canonical SDE — the Ornstein-Uhlenbeck process dx = -θ x dt + σ dW. It's the "spring + random kick" model used in physics, neuroscience, and finance. Watch the path get yanked back to zero whenever the noise sends it too far.
Loading visualization...
Now the abstract SDE form, ready to morph into anything you want:
Each SDE has a partner PDE that governs the marginal densityp_t(x) of the state at time t. For an SDE dx = f(x,t) dt + g(t) dW, the density evolves according to the Fokker-Planck equation:
∂t∂pt(x)=−∇⋅(f(x,t)pt(x))+21∇2(g(t)2pt(x))
You will not need to solve Fokker-Planck PDEs by hand. What matters: for the SDE dx = ∇log p(x) dt + √2 dW, the stationary distribution (the t → ∞ limit of p_t) is exactly p(x). Plug this into Fokker-Planck and the time-derivative drops to zero -- this is the foundation of Langevin sampling.
What Do You Think?
Consider the SDE dx = μ dt + σ dW (constant drift, constant diffusion). What kind of process is this?
Recall from MLE/MAP/Fisher: the score of a likelihood is ∇_θ log p(x | θ), the gradient of log-density with respect to parameters. In stochastic calculus and diffusion, "score" usually means something different: the gradient of log-density with respect to data.
Earlier we noted that the SDE dx = ∇log p(x) dt + √2 dW has stationary distribution p(x). Discretising this SDE with step size ε gives Langevin dynamics, the universal sampler:
Langevin needs ∇log p(x). If p(x) is a complicated data distribution -- the distribution of natural images, say -- you do not have it in closed form. The brilliant move: train a neural network s_θ(x) to estimate the score, using only data samples.
The natural objective is the squared-error mismatch between the network and the true score:
Hyvärinen (2005) showed that the squared-error objective can be rewritten in a form that does NOT need ∇log p:
L(θ)=Ex∼p[21∥sθ(x)∥2+tr(∇xsθ(x))]+const
The trace of the Jacobian is expensive in high dimensions (each pixel adds a partial derivative). Implicit score matching is conceptually clean but rarely used directly at scale. The next idea -- denoising score matching -- replaces it.
Perturb the data with Gaussian noise. Define q(x̃ | x) = N(x̃; x, σ²I) (a known Gaussian whose score is computable). Train s_θ(x̃, σ) to match the score of the perturbation kernel:
LDSM(θ)=Ex∼p,x~∼q(⋅∣x)[21∥sθ(x~,σ)−σ2x−x~∥2]
This is the single trick that made score-based generative modelling tractable: instead of estimating the unknown score directly, estimate the score of a known Gaussian-perturbed version, sweep over noise levels, and stitch the results together.
Ho et al. 2020 ("Denoising Diffusion Probabilistic Models") used exactly this idea to train pixel-space generative models. The setup:
A forward process is a Markov chain that gradually adds Gaussian noise:
q(xt∣xt−1)=N(xt;1−βtxt−1,βtI)
This is exactly the Markov chain you saw in the previous lesson, with state space R^D (continuous) and Gaussian transition kernel.
The DDPM forward chain is a discrete-time relative of a stochastic differential equation — the same Brownian-noise engine, just with a contraction step and a noise schedule mixed in. Reopen the viz and flip into the GBM (Geometric Brownian Motion) mode to see a multiplicative cousin: the same noise mechanism powers everything from stock prices to image diffusion to neural network parameter Langevin dynamics.
Loading visualization...
Quick check
In DDPM, the forward step is x_t = √(1 − β_t) x_{t-1} + √(β_t) ε where ε ~ N(0, I). What does the contraction factor √(1 − β_t) accomplish?
Given the closed form, training is shockingly simple. Sample x_0 ~ data, sample t ~ Uniform({1, ..., T}), sample ε ~ N(0, I), and form x_t = √(ᾱ_t) x_0 + √(1 − ᾱ_t) ε. Train a network ε_θ(x_t, t) to predict the noise:
LDDPM(θ)=Ex0,ε,t[∥ε−εθ(xt,t)∥2]
The connection to denoising score matching: predicting ε from x_t is equivalent (up to a known factor) to predicting the score s_θ(x_t, t) = -ε_θ(x_t, t) / √(1 − ᾱ_t). DDPM's ε-prediction parameterisation is the most numerically stable form of denoising score matching at every noise level simultaneously.
Once the score is learned, how do you generate samples? Run the SDE backwards in time. Anderson (1982) and later Song et al. (2020) showed that for a forward SDE dx = f(x, t) dt + g(t) dW, the reverse-time SDE is:
dx=[f(x,t)−g(t)2∇xlogpt(x)]dt+g(t)dWˉ
This is the unifying view: every modern diffusion model -- DDPM, score-based, EDM, latent diffusion -- is some discretisation of the reverse SDE, with some parameterisation of the score. The architecture (U-Net, DiT) is implementation detail; the math is invariant.
A 2022-2023 line of work (Lipman et al., Liu et al.) replaces the stochastic reverse SDE with a deterministic ODE flow. Instead of learning the score, you learn the velocity fieldv_θ(x, t) such that integrating dx/dt = v_θ(x, t) from noise to data produces samples. Same fundamental idea (transport mass from a known distribution to data), less stochasticity, often faster sampling. See track-05's diffusion lesson for implementation details. The math primitives are the same as this lesson minus the dW term.
This lesson tied together every previous chapter of the math track:
Markov chains (N5) gave you the discrete forward process q(x_t | x_{t-1}).
Multivariate Gaussians (N3) gave you the closed form q(x_t | x_0) and the reparameterisation trick that made everything trainable.
MLE / score functions (N4) defined the score ∇_x log p(x) and showed why fitting it is principled.
Information theory gave you Jensen's inequality, KL divergence, and the variational lower bounds that diffusion's full derivation rests on.
Matrix calculus and gradient descent (N2, N6) are how you actually train the score network.
Random variables, expectation, variance (N7-original) are the language every loss is written in.
If you can derive the DDPM forward closed form from the Markov definition, recognise the Itô correction in a derivation, write down the reverse SDE, and explain why score matching's loss is just regression on noise -- you have the math required to read any diffusion or score-based paper from 2020 onwards. The math track has paid for itself.
What is the defining property of Brownian motion that makes it different from a smooth function?
Next up: with the math foundations complete, the rest of the curriculum -- classical ML, deep learning, NLP, RL, generative AI, agents -- has the language it needs. Every loss function, every architecture, every training trick you encounter from here is built on the seventeen lessons you just finished.