Skip to content

6.4 ODEs and SDEs for generation

ready≈ 11.5 hoursNeeds: 1.4, 1.6Unlocks: 6.5
  1. 1 Learn
  2. 2 Bridge & derive
  3. 3 Explore (ODE/SDE particle simulator)
  4. 4 Do
  5. 5 Check

Generation as simulation. The rest of Part 6 generates data by moving noise along a vector field. Start at x0 ~ N(0, I) and follow dXt = ut(Xt) dt from t = 0 to 1; a network learns u so that X1 looks like data. That is an ordinary differential equation (ODE), and the map from start to end is called a flow. Add a random kick, dXt = ut(Xt) dt + σt dWt, and it is a stochastic differential equation (SDE) (Holderrieth and Erives 2025, section 2).

Simulating. A neural u has no closed-form solution, so we take small steps of size h. Euler’s method moves along the current arrow: x ← x + h ut(x). Its error shrinks in proportion to h; Heun’s method averages the slope at both ends of the step and its error shrinks like h². Euler–Maruyama adds σt √h ε with ε ~ N(0, I) at each step, because Brownian motion W moves a distance of order √h in time h, not h (MIT 6.S184, lab 1).

Two processes to know. The Ornstein–Uhlenbeck process dX = −θX dt + σ dW pulls towards zero while noise pushes out; whatever it starts from, it forgets and settles at N(0, σ²/(2θ)). That is what a diffusion model’s forward process does to data (6.5). Langevin dynamics, dX = ∇log p(X) dt + √2 dW, is the reverse kind: the drift climbs the log-density and the noise keeps it from collapsing onto the peaks, so for small steps and long runs its particles are distributed as p. The gradient ∇log p is called the score (Song and Ermon 2019, section 2).

The catch. Langevin crosses between far-apart modes very rarely, so the mass ends wherever the particles started. Song and Ermon fixed this by sampling from heavily blurred versions of p first and sharpening step by step, which is the idea behind diffusion models (Song and Ermon 2019, §3.2.2 and §4.3).

Why Euler–Maruyama scales noise by √h, and why Langevin samples pstep 1 of 5
  1. Wt+h−Wt∼N(0,h)W_{t+h} - W_t \sim \mathcal{N}(0, h)

ODE/SDE particle simulator

Euler vs Euler–Maruyama sampling

Static view on small screens. Open this page on a laptop to interact.

vector field solver
arrows: the field's direction; lines: 25 simulated paths from hollow starts to filled ends
300 particles at t = 4; circles: 1 and 2 standard deviations of each blob
share ending nearest each blob: 32% / 52% / 16% (true weights 50% / 30% / 20%) typical distance from the nearest centre: 0.00 blob standard deviations (for true samples about 1.25)

An ODE moves every particle along the arrows; an SDE adds a random kick of size σ√h each step. Following the score ∇log p with no noise is gradient ascent: every particle slides to the top of a blob and stops, so the result is three points, not samples. With σ = √2 the same drift becomes Langevin dynamics, and the particles spread out to match p. The share of particles per blob still follows where they started, because crossing between blobs is rare: 6.5 fixes that by starting from a heavily blurred p. On the rotation field, Euler's straight steps cut each circle's corner and spiral outwards; Heun averages two slopes and stays on the circle much longer.

Two square plots of the same 2D vector field. Buttons choose the field (the score of a mixture of three Gaussian blobs, or a rotation) and the solver (Euler or Heun). Sliders set the number of steps and the noise level σ; a button jumps to σ = √2, which makes the score field into Langevin dynamics, and another draws new starting points. The left plot shows the field as arrows and 25 simulated paths; the right plot shows where 300 particles end, with the blobs’ 1 and 2 standard-deviation circles. The readout gives the share of particles nearest each blob against the true weights and how spread out they are, or, for the rotation, how much the radius grew. Keyboard: Tab to the buttons and sliders.
ODEs and SDEs stubs/m6_4_sde.py

Loading starter file…

Prefer your own editor? Edit exercises/stubs/m6_4_sde.py and run mlc check 6.4. The tests are identical; import the results on the progress page.

Hints · euler_maruyama 0/3 revealed
Hints · gmm_score 0/3 revealed
Hints · langevin 0/3 revealed

Then run the lab. Open notebooks/6.4-odes-sdes.ipynb (a CPU is enough) and paste in your 6.4 file. It measures the order of Euler and Heun, checks Euler–Maruyama on Brownian motion and the OU process, and samples a three-blob mixture with plain and with annealed Langevin dynamics.

1. Why does Euler–Maruyama multiply the noise by √h rather than h?
2. You halve the step size. Roughly how does the error at t = 1 change for Euler and for Heun?
3. You follow the score ∇log p with no noise (σ = 0). What happens to the particles?
4. Why can plain Langevin dynamics get the weights of well-separated modes wrong?
  • not yet: All tests pass (0/?)
  • not yet: Checkpoint quiz ≥ 80%
Finish the items above first.
Learner log: time spent and points of confusion

Every confusion you note becomes a fix before the module is marked stable. Export it from the progress page and attach it to an issue.