Appendix C: Behind the Scenes of Diffusion Models: Itô’s Calculus and Girsanov’s Theorem
(Score-based) Diffusion models are built on SDEs: a drift that pushes states and a Brownian term that jitters them. Unlike ODE paths, Brownian paths are nowhere differentiable, so the ordinary chain rule fails. In this section, we introduce two fundamental tools that make the math precise:
Itô’s Formula is the correct chain rule for stochastic trajectories. It tells us how a function \({\mathbf{h}}({\mathbf{x}}_t,t)\) evolves when \({\mathbf{x}}_t\) follows an SDE. It enables derivations of the Fokker–Planck equation, moment dynamics, the Itô product rule, and the identities used in score-based training.
Girsanov’s Theorem is a change-of-measure result on path probabilities. It quantifies how likelihoods change when the noise is fixed but the drift is altered. This links likelihood-weighted score matching to path-space KL divergence and, through a variational bound, to data likelihood.
With these tools, the standard diffusion model derivations (Fokker–Planck, reverse time SDE, training objectives, and likelihood relations) follow cleanly and without hand waving.
After this chapter, you will be able to...
understand why ordinary calculus is not enough for SDEs, and how Itô’s formula gives the correct chain rule for stochastic trajectories;
see how Itô’s formula works behind the scenes in important diffusion-model derivations, including the Fokker–Planck equation and linear SDEs;
understand Girsanov’s theorem as a way to compare the probabilities of stochastic paths when their drifts are different but their diffusion is the same;
distinguish matching distributions at each time from matching the full stochastic path, and see why likelihood-weighted score matching is related to path-space KL divergence and data likelihood.
Reader route. This appendix develops some of the mathematical tools that operate behind the scenes of diffusion models. Itô calculus makes stochastic differentiation precise, while Girsanov’s theorem explains how changing the drift changes the probability of entire stochastic paths. These tools underlie several standard diffusion-model derivations that are often used without being shown explicitly.
Itô’s Formula: The Chain Rule for Random Processes
Standard calculus does not directly apply to stochastic processes because Wiener processes are not differentiable in the classical sense. Instead, we use Itô’s calculus, which provides rules for working with stochastic integrals.
Motivation: Why Do We Need a Special Chain Rule?
Consider a deterministic time-varying function \({\mathbf{y}}_t\) that evolves smoothly with time \(t\) (e.g., an ODE). If we have a function \({\mathbf{h}}({\mathbf{y}}_t, t)\), the usual chain rule tells us: \[\frac{\mathop{}\!\mathrm{d}{\mathbf{h}}}{\mathop{}\!\mathrm{d}t} = \frac{\partial {\mathbf{h}}}{\partial t} + \nabla_{{\mathbf{y}}}{\mathbf{h}}\frac{\mathop{}\!\mathrm{d}{\mathbf{y}}_t}{\mathop{}\!\mathrm{d}t} .\] Here, \(\nabla_{{\mathbf{y}}}{\mathbf{h}}\) is the Jacobian of \({\mathbf{h}}\). This works perfectly for deterministic paths \({\mathbf{y}}_t\).
But what happens if \({\mathbf{x}}_t\) is a stochastic process, say, driven by an SDE \[\mathop{}\!\mathrm{d}{\mathbf{x}}_t = {\mathbf{f}}({\mathbf{x}}_t, t)\mathop{}\!\mathrm{d}t + g(t) \mathop{}\!\mathrm{d}{\mathbf{w}}_t\] as in Equation in Appendix A? What SDE does the process \({\mathbf{h}}({\mathbf{x}}_t, t)\) satisfy?
Why the Ordinary Chain Rule Fails?
Naïvely applying the classical chain rule yields \[\mathop{}\!\mathrm{d}{\mathbf{h}}= \frac{\partial {\mathbf{h}}}{\partial t} \mathop{}\!\mathrm{d}t + \nabla_{{\mathbf{y}}}{\mathbf{h}}\cdot \mathop{}\!\mathrm{d}{\mathbf{x}}_t.\] However, Brownian increments scale as \(\mathcal{O}(\sqrt{\mathop{}\!\mathrm{d}t})\), while their quadratic variation is encoded in Itô calculus by \[(\mathop{}\!\mathrm{d}{\mathbf{w}}_t)^2 = \mathop{}\!\mathrm{d}t.\] Thus, second-order terms in \(\mathop{}\!\mathrm{d}{\mathbf{w}}_t\) do not vanish in stochastic calculus, unlike classical calculus where \((\mathop{}\!\mathrm{d}t)^2\) terms are negligible.
Example: Simple Example–\(h(x_t) = x_t^2\)
To see the intuition, let us consider the simple real-valued function \(h(x_t) = x_t^2 \in \mathbb{R}\) where the random variable \(x_t \in \mathbb{R}\) satisfies \[\mathop{}\!\mathrm{d}x_t = \sigma \mathop{}\!\mathrm{d}w_t, \quad x_0=0,\] with a constant \(\sigma>0\). If we try the classical chain rule, \[\mathop{}\!\mathrm{d}h = 2 x_t \mathop{}\!\mathrm{d}x_t = 2 x_t \sigma \mathop{}\!\mathrm{d}w_t.\] If this were true, the expectation of \(h(x_t)\) would be constant in time because \[\mathbb{E}[\mathop{}\!\mathrm{d}h ] = 2 \sigma \mathbb{E}[x_t \mathop{}\!\mathrm{d}w_t]= 2 \sigma \mathbb{E}[x_t] \underbrace{\mathbb{E}[\mathop{}\!\mathrm{d}w_t]}_{=0}=0.\] But we know from classical Brownian motion properties (see Equation in Appendix A) that \[\mathbb{E}[x_t^2] = \sigma^2 t,\] which grows linearly in time. So the ordinary chain rule misses an important term.
Deriving 1D Itô’s Formula from Taylor Expansion
Deterministic Chain Rule via Taylor Expansion.
To understand why the classical chain rule fails for stochastic processes defined by SDEs, we first revisit it in the deterministic setting using Taylor expansion. We consider the scalar case: \(y_t \in \mathbb{R}\) and \(h(\cdot,\cdot)\in \mathbb{R}\). Formally treating \(\mathop{}\!\mathrm{d}y_t = y_{t+\mathop{}\!\mathrm{d}t} - y_t\), with \(\mathop{}\!\mathrm{d}t \approx 0\), we expand: \[\begin{align*} &h(y_{t+\mathop{}\!\mathrm{d}t}, t+\mathop{}\!\mathrm{d}t) - h(y_t, t) \\ = ~&\frac{\partial h}{\partial t} \mathop{}\!\mathrm{d}t + \frac{\partial h}{\partial y} \mathop{}\!\mathrm{d}y_t + \frac{1}{2} \left(\textcolor{gray}{\frac{\partial^2 h}{\partial y^2} (\mathop{}\!\mathrm{d}y_t)^2} + 2 \textcolor{gray}{\frac{\partial^2 h}{\partial t \partial y} \mathop{}\!\mathrm{d}t \mathop{}\!\mathrm{d}y_t} + \textcolor{gray}{\frac{\partial^2 h}{\partial t^2} (\mathop{}\!\mathrm{d}t)^2} \right) + \mathcal{O}(\mathop{}\!\mathrm{d}t^3), \end{align*}\] Here, \(\mathop{}\!\mathrm{d}t \mathop{}\!\mathrm{d}y_t = \left(\frac{\mathop{}\!\mathrm{d}y_t}{\mathop{}\!\mathrm{d}t}\right)(\mathop{}\!\mathrm{d}t)^2 = \mathcal{O}(\mathop{}\!\mathrm{d}t^2)\), and similarly \((\mathop{}\!\mathrm{d}t)^2 = \mathcal{O}(\mathop{}\!\mathrm{d}t^2)\). Therefore, all the gray parts are ignorable, and the full differential is: \[\mathop{}\!\mathrm{d}h = \frac{\partial h}{\partial t} \mathop{}\!\mathrm{d}t + \frac{\partial h}{\partial y} \mathop{}\!\mathrm{d}y_t + \mathcal{O}(\mathop{}\!\mathrm{d}t^2).\]
Itô’s Formula via Stochastic Taylor Expansion.
Now consider a stochastic process \(x_t \in \mathbb{R}\) governed by the SDE: \[\mathop{}\!\mathrm{d}x_t = f(x_t, t) \mathop{}\!\mathrm{d}t + g(t) \mathop{}\!\mathrm{d}w_t,\] where \(w_t\) is standard Brownian motion. We aim to compute the differential of a scalar-valued function \(h(x_t, t)\).
Using the stochastic Taylor expansion (Kloeden et al. 1992), which retains second-order terms in \(\mathop{}\!\mathrm{d}x_t\), we have: \[\begin{align*} &h(x_{t+\mathop{}\!\mathrm{d}t}, t+\mathop{}\!\mathrm{d}t) - h(x_t, t) \\ =~&\frac{\partial h}{\partial t} \mathop{}\!\mathrm{d}t + \frac{\partial h}{\partial x} \mathop{}\!\mathrm{d}x_t + \frac{1}{2} \left( \frac{\partial^2 h}{\partial x^2} (\mathop{}\!\mathrm{d}x_t)^2 + 2\textcolor{gray}{{\frac{\partial^2 h}{\partial t \partial x} \mathop{}\!\mathrm{d}t \mathop{}\!\mathrm{d}x_t}} + \textcolor{gray}{{ \frac{\partial^2 h}{\partial t^2} (\mathop{}\!\mathrm{d}t)^2}} \right) + \cdots \end{align*}\]
Negligible Cross Terms.
By the scaling property of Brownian motion (Equation in Appendix A), \[\mathop{}\!\mathrm{d}w_t = \mathcal{O}(\sqrt{\mathop{}\!\mathrm{d}t}) \quad \Rightarrow \quad \mathop{}\!\mathrm{d}t \cdot \mathop{}\!\mathrm{d}w_t = \mathcal{O}((\mathop{}\!\mathrm{d}t)^{3/2}).\] Therefore, \[\begin{align*} \mathop{}\!\mathrm{d}t \cdot \mathop{}\!\mathrm{d}x_t &= \mathop{}\!\mathrm{d}t \left(f \mathop{}\!\mathrm{d}t + g \mathop{}\!\mathrm{d}w_t\right) = f (\mathop{}\!\mathrm{d}t)^2 + g \cdot \mathop{}\!\mathrm{d}t \cdot \mathop{}\!\mathrm{d}w_t = \mathcal{O}((\mathop{}\!\mathrm{d}t)^{3/2}). \end{align*}\] So, the gray terms are negligible: \(\mathcal{O}((\mathop{}\!\mathrm{d}t)^{3/2})\) or smaller.
Second-Order Term \((\mathop{}\!\mathrm{d}x_t)^2\).
Expanding using the SDE: \[\begin{align*} (\mathop{}\!\mathrm{d}x_t)^2 &= \left( f \mathop{}\!\mathrm{d}t + g \mathop{}\!\mathrm{d}w_t \right)^2 \\ &= f^2 (\mathop{}\!\mathrm{d}t)^2 + 2fg \mathop{}\!\mathrm{d}t \mathop{}\!\mathrm{d}w_t + g^2 (\mathop{}\!\mathrm{d}w_t)^2 \\ &= g^2(t) \mathop{}\!\mathrm{d}t, \end{align*}\] where the last equality uses the Itô differential rules \((\mathop{}\!\mathrm{d}t)^2=0\), \(\mathop{}\!\mathrm{d}t\,\mathop{}\!\mathrm{d}w_t=0\), and \((\mathop{}\!\mathrm{d}w_t)^2=\mathop{}\!\mathrm{d}t\), with the last identity understood in the quadratic-variation sense.
Combining terms, we obtain the differential: \[\mathop{}\!\mathrm{d}h(x_t, t) = \frac{\partial h}{\partial t} \mathop{}\!\mathrm{d}t + \frac{\partial h}{\partial x} \mathop{}\!\mathrm{d}x_t + \frac{1}{2} \frac{\partial^2 h}{\partial x^2} g^2(t) \mathop{}\!\mathrm{d}t.\] Substituting \(\mathop{}\!\mathrm{d}x_t = f(x_t, t) \mathop{}\!\mathrm{d}t + g(t) \mathop{}\!\mathrm{d}w_t\) yields: \[\mathop{}\!\mathrm{d}h(x_t, t) = \left( \frac{\partial h}{\partial t} + f \frac{\partial h}{\partial x} + \frac{1}{2} g^2 \frac{\partial^2 h}{\partial x^2} \right) \mathop{}\!\mathrm{d}t + g \frac{\partial h}{\partial x} \mathop{}\!\mathrm{d}w_t.\] This is the 1D version of Itô’s formula.
Example: Simple Example–\(h(x_t) = x_t^2\)
We revisit the simple example: \(h(x_t) = x_t^2\), where the stochastic process \(x_t \in \mathbb{R}\) satisfies \[\mathop{}\!\mathrm{d}x_t = \sigma \mathop{}\!\mathrm{d}w_t,\] with a constant \(\sigma > 0\). Applying Itô’s formula correctly to \(h(x_t) = x_t^2\), we obtain: \[\mathop{}\!\mathrm{d}h(x_t) = \mathop{}\!\mathrm{d}(x_t^2) = 2 x_t \mathop{}\!\mathrm{d}x_t + \sigma^2 \mathop{}\!\mathrm{d}t.\] Substituting \(\mathop{}\!\mathrm{d}x_t = \sigma \mathop{}\!\mathrm{d}w_t\), this becomes: \[\mathop{}\!\mathrm{d}(x_t^2) = 2 x_t \sigma \mathop{}\!\mathrm{d}w_t + \sigma^2 \mathop{}\!\mathrm{d}t.\]
Itô’s Formula: The Chain Rule for SDEs
We summarize the one-dimensional Itô’s formula derived above. Using similar arguments, the result extends naturally to the multi-dimensional setting. While we omit the detailed derivation, we state the general formula for completeness.
Finally, we illustrate an application of Itô’s formula by deriving the Itô product rule, which enables computation of \(\mathop{}\!\mathrm{d}({\mathbf{x}}_t^\top {\mathbf{y}}_t)\) for stochastic processes \({\mathbf{x}}_t\) and \({\mathbf{y}}_t\).
\(1\)D Itô’s Formula.
Let \(x_t \in \mathbb{R}\) be a stochastic process satisfying the SDE: \[\mathop{}\!\mathrm{d}x_t = f(x_t, t) \mathop{}\!\mathrm{d}t + g(t) \mathop{}\!\mathrm{d}w_t.\] For a scalar function \(h \colon \mathbb{R} \times [0, T] \to \mathbb{R}\), the process \(h(x_t, t)\) satisfies:
\[\mathop{}\!\mathrm{d}h(x_t, t) = \left( \frac{\partial h}{\partial t} + f \frac{\partial h}{\partial x} + \frac{1}{2} g^2 \frac{\partial^2 h}{\partial x^2} \right) \mathop{}\!\mathrm{d}t + g \frac{\partial h}{\partial x} \mathop{}\!\mathrm{d}w_t.\]
Multidimensional Itô’s Formula with Scalar Output.
Let \({\mathbf{x}}_t \in \mathbb{R}^D\) satisfy the SDE: \[\mathop{}\!\mathrm{d}{\mathbf{x}}_t = {\mathbf{f}}({\mathbf{x}}_t, t) \mathop{}\!\mathrm{d}t + g(t) \mathop{}\!\mathrm{d}{\mathbf{w}}_t,\] where \({\mathbf{f}}\colon \mathbb{R}^D \times [0, T] \to \mathbb{R}^D\), \(g \colon [0, T] \to \mathbb{R}\), and \({\mathbf{w}}_t \in \mathbb{R}^D\) is a \(D\)-dimensional Brownian motion. Let \(h \colon \mathbb{R}^D \times [0, T] \to \mathbb{R}\) be a scalar-valued function. Then \(h({\mathbf{x}}_t, t)\) satisfies:
\[\begin{align} \label{eq:Ito-scalar} \mathop{}\!\mathrm{d}h({\mathbf{x}}_t, t) = \left( \frac{\partial h}{\partial t} + \nabla_{{\mathbf{x}}} h^\top {\mathbf{f}} + \frac{1}{2} g^2(t) \operatorname{Tr} \left( \nabla^2_{{\mathbf{x}}} h \right) \right) \mathop{}\!\mathrm{d}t + g(t) \nabla_{{\mathbf{x}}} h^\top \mathop{}\!\mathrm{d}{\mathbf{w}}_t, \end{align}\]
where \(\nabla_{{\mathbf{x}}} h \in \mathbb{R}^D\) is the gradient and \(\nabla^2_{{\mathbf{x}}} h \in \mathbb{R}^{D \times D}\) is the Hessian matrix of \(h\) with respect to \({\mathbf{x}}\).
Example: Itô’s Product Rule
Let \({\mathbf{x}}_t, {\mathbf{y}}_t \in \mathbb{R}^D\) be vector-valued stochastic processes governed by the SDEs: \[\begin{aligned} \mathop{}\!\mathrm{d}{\mathbf{x}}_t &= {\mathbf{a}}({\mathbf{x}}_t, t) \mathop{}\!\mathrm{d}t + b(t) \mathop{}\!\mathrm{d}{\mathbf{w}}_t, \\ \mathop{}\!\mathrm{d}{\mathbf{y}}_t &= {\mathbf{c}}({\mathbf{y}}_t, t) \mathop{}\!\mathrm{d}t + d(t) \mathop{}\!\mathrm{d}{\mathbf{w}}_t, \end{aligned}\] where \({\mathbf{a}}, {\mathbf{c}}: \mathbb{R}^D \times [0, T] \to \mathbb{R}^D\) are vector fields, and \(b(t), d(t) \in \mathbb{R}\) are scalar-valued functions. Here, \({\mathbf{w}}_t \in \mathbb{R}^D\) denotes a standard \(D\)-dimensional Brownian motion.
We aim to derive the SDE for the scalar-valued process \[z(t) := {\mathbf{x}}_t^\top {\mathbf{y}}_t.\]
Applying the multivariate Itô formula to the bilinear function \(h({\mathbf{x}}, {\mathbf{y}}) := {\mathbf{x}}^\top {\mathbf{y}}\), we obtain: \[\mathop{}\!\mathrm{d}({\mathbf{x}}^\top {\mathbf{y}}) = (\mathop{}\!\mathrm{d}{\mathbf{x}})^\top {\mathbf{y}}+ {\mathbf{x}}^\top \mathop{}\!\mathrm{d}{\mathbf{y}}+ \mathop{\mathrm{Tr}}\left[ \mathop{}\!\mathrm{d}{\mathbf{x}}\cdot (\mathop{}\!\mathrm{d}{\mathbf{y}})^\top \right].\]
The Itô correction term is computed as: \[\begin{align*} \mathop{}\!\mathrm{d}{\mathbf{x}}\cdot (\mathop{}\!\mathrm{d}{\mathbf{y}})^\top &= b(t) \mathop{}\!\mathrm{d}{\mathbf{w}}_t \cdot \left[ d(t) \mathop{}\!\mathrm{d}{\mathbf{w}}_t \right]^\top \\&= b(t) d(t) \mathop{}\!\mathrm{d}{\mathbf{w}}_t \cdot \mathop{}\!\mathrm{d}{\mathbf{w}}_t^\top \\&= b(t)d(t) \mathop{}\!\mathrm{d}t \cdot {\mathbf{I}}_D. \end{align*}\] Thus, \[\mathop{\mathrm{Tr}}\left[ \mathop{}\!\mathrm{d}{\mathbf{x}}\cdot (\mathop{}\!\mathrm{d}{\mathbf{y}})^\top \right] = b(t)d(t) \mathop{\mathrm{Tr}}({\mathbf{I}}_D) \mathop{}\!\mathrm{d}t = D b(t)d(t) \mathop{}\!\mathrm{d}t.\]
Putting everything together, the resulting SDE is: \[\begin{align} \label{eq:ito-product} \begin{aligned} \mathop{}\!\mathrm{d}({\mathbf{x}}^\top {\mathbf{y}}) &= (\mathop{}\!\mathrm{d}{\mathbf{x}})^\top {\mathbf{y}}+ {\mathbf{x}}^\top \mathop{}\!\mathrm{d}{\mathbf{y}}+ D b(t)d(t) \mathop{}\!\mathrm{d}t \end{aligned} \end{align}\]
Itô’s Formula’s Application: Derivation of Fokker-Planck Equation
In this section, we apply Itô’s formula from Equation \(\eqref{eq:Ito-scalar}\) to derive the Fokker–Planck equation, a PDE that characterizes the time evolution of the probability density \(p_t({\mathbf{x}})\) associated with the \(D\)-dimensional diffusion process defined by the SDE in Equation in Appendix A.
Step 1: Apply Itô’s Formula.
Let \(\phi({\mathbf{x}}, t)\) be a smooth test function \(\phi \colon \mathbb{R}^D \times [0, T] \to \mathbb{R}\). By Itô’s Formula: \[\mathop{}\!\mathrm{d}\phi({\mathbf{x}}_t, t) = \left( \frac{\partial \phi}{\partial t} + \nabla_{{\mathbf{x}}} \phi^\top {\mathbf{f}}({\mathbf{x}}_t, t) + \frac{1}{2} g^2(t) \mathop{\mathrm{Tr}}[\nabla_{{\mathbf{x}}}^2 \phi] \right) \mathop{}\!\mathrm{d}t + g(t) \nabla_{{\mathbf{x}}} \phi^\top \mathop{}\!\mathrm{d}{\mathbf{w}}_t.\]
Step 2: Take Expectation.
Taking expectation and using that the Itô integral has zero expectation under the standard integrability conditions: \[\mathbb{E}[\mathop{}\!\mathrm{d}\phi({\mathbf{x}}_t, t)] = \mathbb{E} \left[ \left( \frac{\partial \phi}{\partial t} + \nabla_{{\mathbf{x}}} \phi^\top {\mathbf{f}}({\mathbf{x}}_t, t) + \frac{1}{2} g^2(t) \mathop{\mathrm{Tr}}[\nabla_{{\mathbf{x}}}^2 \phi] \right) \mathop{}\!\mathrm{d}t \right].\]
Step 3: Express Expectation via Density.
This expectation can be written as: \[\mathbb{E}[\mathop{}\!\mathrm{d}\phi({\mathbf{x}}_t, t)] = \int \left( \frac{\partial \phi}{\partial t} + \nabla_{{\mathbf{x}}} \phi^\top {\mathbf{f}}({\mathbf{x}}, t) + \frac{1}{2} g^2(t) \mathop{\mathrm{Tr}}[\nabla_{{\mathbf{x}}}^2 \phi] \right) p_t({\mathbf{x}}) \mathop{}\!\mathrm{d}{\mathbf{x}}\mathop{}\!\mathrm{d}t.\]
Step 4: Integrate by Parts.
Use integration by parts (divergence theorem) in \(\mathbb{R}^D\): \[\begin{align*} \int \nabla_{{\mathbf{x}}} \phi^\top {\mathbf{f}}p_t \mathop{}\!\mathrm{d}{\mathbf{x}}&= -\int \phi \nabla_{{\mathbf{x}}} \cdot ({\mathbf{f}}p_t) \mathop{}\!\mathrm{d}{\mathbf{x}}, \\ \int \mathop{\mathrm{Tr}}[\nabla_{{\mathbf{x}}}^2 \phi] p_t \mathop{}\!\mathrm{d}{\mathbf{x}}&= \int \phi \Delta p_t \mathop{}\!\mathrm{d}{\mathbf{x}}. \end{align*}\]
Step 5: Substitute and Rearrange.
Note that \[\mathbb{E}[\phi({\mathbf{x}}_t,t)] = \int \phi({\mathbf{x}},t) p_t({\mathbf{x}}) \mathop{}\!\mathrm{d}{\mathbf{x}}.\] Differentiating in time, we have \[\frac{\mathop{}\!\mathrm{d}}{\mathop{}\!\mathrm{d}t}\mathbb{E}[\phi({\mathbf{x}}_t,t)] = \frac{\mathop{}\!\mathrm{d}}{\mathop{}\!\mathrm{d}t}\int \phi({\mathbf{x}},t) p_t({\mathbf{x}}) \mathop{}\!\mathrm{d}{\mathbf{x}} = \int\Bigl(\partial_t\phi({\mathbf{x}},t) p_t({\mathbf{x}}) + \phi({\mathbf{x}},t) \partial_t p_t({\mathbf{x}})\Bigr)\mathop{}\!\mathrm{d}{\mathbf{x}}.\] Therefore, \[\mathbb{E}[\mathop{}\!\mathrm{d}\phi({\mathbf{x}}_t,t)] = \frac{\mathop{}\!\mathrm{d}}{\mathop{}\!\mathrm{d}t}\mathbb{E}[\phi({\mathbf{x}}_t,t)] \mathop{}\!\mathrm{d}t = \int\Bigl(\partial_t\phi({\mathbf{x}},t) p_t({\mathbf{x}}) + \phi({\mathbf{x}},t) \partial_t p_t({\mathbf{x}})\Bigr)\mathop{}\!\mathrm{d}{\mathbf{x}}\mathop{}\!\mathrm{d}t.\] Equating this expression with Step 3, the terms \(\int \partial_t\phi\,p_t\,\mathop{}\!\mathrm{d}{\mathbf{x}}\,\mathop{}\!\mathrm{d}t\) cancel. Applying Step 4 to the remaining spatial terms gives \[\int \phi({\mathbf{x}},t) \left[ \frac{\partial p_t}{\partial t} + \nabla_{{\mathbf{x}}}\cdot\bigl({\mathbf{f}}({\mathbf{x}},t)p_t({\mathbf{x}})\bigr) - \frac{1}{2}g^2(t)\Delta p_t({\mathbf{x}}) \right] \mathop{}\!\mathrm{d}{\mathbf{x}} = 0.\] Since this holds for every smooth test function \(\phi\), we obtain the Fokker–Planck equation \[\frac{\partial p_t({\mathbf{x}})}{\partial t} = -\nabla_{{\mathbf{x}}}\cdot\bigl({\mathbf{f}}({\mathbf{x}},t)p_t({\mathbf{x}})\bigr) + \frac{1}{2}g^2(t)\Delta p_t({\mathbf{x}}).\]
Itô’s Formula Application: Closed-Form Solution of a Linear SDE
This subsection demonstrates how to obtain a closed-form solution for a linear SDE by using an integration factor (similar to the ODE case) and Itô’s formula. The approach mirrors classical techniques for solving linear ODEs, but adapted to the stochastic setting.
We consider a linear SDE of the form \[\begin{align} \label{eq:app-linear-sde} \mathrm{d}{\mathbf{x}}_t = f(t){\mathbf{x}}_t \mathrm{d}t + g(t) \mathrm{d}{\mathbf{w}}_t, \end{align}\] where \(f(t)\) and \(g(t)\) are deterministic functions and \({\mathbf{w}}_t\) is a standard Wiener process.
Closed-Form Solution of a Linear SDE.
We derive the explicit solution to the linear SDE in Equation \(\eqref{eq:app-linear-sde}\) using the method of integrating factors. This type of forward SDE commonly arises in diffusion models (see Section (Score SDE: Its Principles) in Chapter 4).
Step 1: Define an Integration Factor.
Let \[\Psi(t) := \exp\left( -\int_0^t f(s) \mathrm{d}s \right), \quad \text{and define} \quad {\mathbf{y}}_t := \Psi(t){\mathbf{x}}_t.\]
Step 2: Apply Itô’s Formula.
We apply Itô’s formula to the function \({\mathbf{h}}({\mathbf{x}}, t) := \Psi(t){\mathbf{x}}\). This is actually a special case of the Itô product rule in Equation \(\eqref{eq:ito-product}\). Since \(\Psi(t)\) is deterministic, there is no cross-variation term, and the formula simplifies to: \[\begin{align*} \mathrm{d}{\mathbf{y}}_t &= \mathrm{d}[\Psi(t){\mathbf{x}}_t] \\ &= \Psi'(t){\mathbf{x}}_t \mathrm{d}t + \Psi(t) \mathrm{d}{\mathbf{x}}_t \\ &= -f(t)\Psi(t){\mathbf{x}}_t \mathrm{d}t + \Psi(t)\left[f(t){\mathbf{x}}_t \mathrm{d}t + g(t) \mathrm{d}{\mathbf{w}}_t\right] \\ &= \Psi(t)g(t) \mathrm{d}{\mathbf{w}}_t. \end{align*}\] Hence, \[{\mathbf{y}}_t = {\mathbf{y}}_0 + \int_0^t \Psi(s)g(s) \mathrm{d}{\mathbf{w}}(s) = {\mathbf{x}}_0 + \int_0^t \Psi(s)g(s) \mathrm{d}{\mathbf{w}}(s),\] since \(\Psi(0) = 1\).
Step 3: Solve for \({\mathbf{x}}_t\).
Using \({\mathbf{x}}_t = \Psi(t)^{-1}{\mathbf{y}}_t\), we obtain \[\begin{align} \label{eq:lienar-sde-solution} {\mathbf{x}}_t &= e^{ \int_0^t f(s)\mathrm{d}s} \left[ {\mathbf{x}}_0 + \int_0^t e^{ -\int_0^s f(r) \mathrm{d}r } g(s) \mathrm{d}{\mathbf{w}}(s) \right]. \end{align}\] This provides an explicit solution to the vector-valued SDE.
Below, we demonstrate two alternative approaches to compute the analytical form of \(p_t({\mathbf{x}}_t|{\mathbf{x}}_0)\).
Analysis of the Closed-Form Solution.
Equation \(\eqref{eq:lienar-sde-solution}\) reconfirms that \(p_t({\mathbf{x}}_t|{\mathbf{x}}_0)\) is Gaussian. To see this, define \[\phi(s) := e^{-\int_0^s f(u) \mathop{}\!\mathrm{d}u} g(s),\] which is a deterministic matrix-valued function of time (assuming \(f(u)\) and \(g(s)\) are deterministic). The Itô integral \(\int_0^t \phi(s) \mathop{}\!\mathrm{d}{\mathbf{w}}_s\) is then a zero-mean Gaussian random variable, as it is the stochastic integral of a deterministic function with respect to Brownian motion. Therefore, \({\mathbf{x}}_t|{\mathbf{x}}_0\) is an affine transformation of a Gaussian random variable and hence itself Gaussian. Its distribution is fully characterized by its conditional mean and covariance.
We define the conditional mean and covariance (given initial condition \({\mathbf{x}}_0\)) as \[{\mathbf{m}}(t) := \mathbb{E}[{\mathbf{x}}_t | {\mathbf{x}}_0], \quad {\mathbf{P}}(t) := \mathbb{E}[({\mathbf{x}}_t - {\mathbf{m}}(t))({\mathbf{x}}_t - {\mathbf{m}}(t))^\top|{\mathbf{x}}_0].\]
Mean.
Using linearity of expectation and the fact that the Itô integral of a deterministic function has zero mean: \[\begin{align*} {\mathbf{m}}(t) = \mathbb{E}\left[ e^{\int_0^t f(s) \mathop{}\!\mathrm{d}s} \left( {\mathbf{x}}_0 + \int_0^t \phi(s) \mathop{}\!\mathrm{d}{\mathbf{w}}_s \right) \bigg| {\mathbf{x}}_0 \right] = e^{\int_0^t f(s) \mathop{}\!\mathrm{d}s} {\mathbf{x}}_0. \end{align*}\]
Covariance.
Let \({\mathbf{z}}_t := \int_0^t \phi(s) \mathop{}\!\mathrm{d}{\mathbf{w}}_s\). Then \({\mathbf{x}}_t - {\mathbf{m}}(t) = A(t) {\mathbf{z}}_t\), so \[{\mathbf{P}}(t) = e^{2\int_0^t f(s) \mathop{}\!\mathrm{d}s} \mathbb{E}[{\mathbf{z}}_t {\mathbf{z}}_t^\top].\] By Itô isometry1, \[\mathbb{E}[{\mathbf{z}}_t {\mathbf{z}}_t^\top] = \left( \int_0^t \phi^2(s) \mathop{}\!\mathrm{d}s \right) {\mathbf{I}}_D,\] hence, \[{\mathbf{P}}(t) = e^{2\int_0^t f(s) \mathop{}\!\mathrm{d}s} \left( \int_0^t \left( e^{-\int_0^s f(u) \mathop{}\!\mathrm{d}u} g(s) \right)^2 \mathop{}\!\mathrm{d}s \right) {\mathbf{I}}_D.\] This shows the conditional covariance is isotropic.
Derivation of Mean and Variance ODEs in Equation in Chapter 4.
Alternatively, we can derive the moment evolution equations directly from the linear SDE Equation \(\eqref{eq:app-linear-sde}\).
Mean Evolution.
Taking the conditional expectation of both sides of the SDE and using linearity: \[\frac{\mathop{}\!\mathrm{d}{\mathbf{m}}(t)}{\mathop{}\!\mathrm{d}t} = \mathbb{E}[f(t) {\mathbf{x}}_t|{\mathbf{x}}_0] = f(t) \mathbb{E}[{\mathbf{x}}_t|{\mathbf{x}}_0] = f(t) {\mathbf{m}}(t).\]
Covariance Evolution.
Define the centered process \(\tilde{{\mathbf{x}}}_t := {\mathbf{x}}_t - {\mathbf{m}}(t)\). Applying Itô’s product rule (see Equation \(\eqref{eq:ito-product}\)): \[\mathop{}\!\mathrm{d}\left( \tilde{{\mathbf{x}}}_t \tilde{{\mathbf{x}}}_t^\top \right) = \mathop{}\!\mathrm{d}\tilde{{\mathbf{x}}}_t \cdot \tilde{{\mathbf{x}}}_t^\top + \tilde{{\mathbf{x}}}_t \cdot \mathop{}\!\mathrm{d}\tilde{{\mathbf{x}}}_t^\top + \mathop{}\!\mathrm{d}\tilde{{\mathbf{x}}}_t \cdot \mathop{}\!\mathrm{d}\tilde{{\mathbf{x}}}_t^\top.\]
From the SDE, we compute: \[\mathop{}\!\mathrm{d}\tilde{{\mathbf{x}}}_t = \mathop{}\!\mathrm{d}{\mathbf{x}}_t - \mathop{}\!\mathrm{d}{\mathbf{m}}(t) = f(t) \tilde{{\mathbf{x}}}_t \mathop{}\!\mathrm{d}t + g(t) \mathop{}\!\mathrm{d}{\mathbf{w}}_t.\]
Substituting into the product rule and taking expectation: \[\begin{align*} \frac{\mathop{}\!\mathrm{d}{\mathbf{P}}(t)}{\mathop{}\!\mathrm{d}t} &= \mathbb{E}[f(t) \tilde{{\mathbf{x}}}_t \tilde{{\mathbf{x}}}_t^\top + \tilde{{\mathbf{x}}}_t \tilde{{\mathbf{x}}}_t^\top f(t) + g^2(t) {\mathbf{I}}_D] \\ &= 2 f(t) {\mathbf{P}}(t) + g^2(t) {\mathbf{I}}_D. \end{align*}\] Thus, we recover the moment evolution equations in Equation in Chapter 4.
Change-of-Variable For Measures: Girsanov’s Theorem in Diffusion Models
Diffusion models harness SDEs to transform simple noise into rich data distributions. At the heart of this transformation lies a profound idea: we can reinterpret randomness by modifying only the deterministic part of an SDE (the drift) while preserving its underlying stochasticity. This is precisely where Girsanov’s theorem enters the picture.
The Core Idea.
Consider an observed continuous trajectory that describes the data’s evolution from time \(t=0\) to \(t=T\), denoted as \({\mathbf{x}}_{0:T}:=\{ {\mathbf{x}}_t|t \in [0,T] \}\). Girsanov’s theorem addresses a fundamental question:
Given this single observed path, what is its likelihood if we assume it was generated by one SDE, versus if we assume it was generated by a different SDE?
We compare two hypothetical models for generating the same trajectory. They have the same initial distribution and diffusion coefficient, but differ in their deterministic “push” or drift function. We assume \({\mathbf{x}}_0\) has the same initial distribution for both assumed generating processes.
To build intuition, imagine \({\mathbf{x}}_{0:T}\) as a wiggly line drawn on paper. One hypothesis assigns trajectory probabilities according to drift \({\mathbf{f}}\), while another uses a different drift \(\tilde{{\mathbf{f}}}\) with the same diffusion coefficient. Girsanov’s theorem compares the corresponding path measures and quantifies how changing the drift changes the relative weighting assigned to the same trajectories.
The Setup.
Let \(P_{{\mathbf{f}}}\) and \(P_{\tilde{{\mathbf{f}}}}\) denote the path measures induced by two SDEs with the same initial distribution and the same diffusion coefficient, but different drifts: \[\begin{align*} \mathop{}\!\mathrm{d}{\mathbf{x}}_t &= {\mathbf{f}}({\mathbf{x}}_t,t)\mathop{}\!\mathrm{d}t + g(t)\mathop{}\!\mathrm{d}{\mathbf{w}}_t, \\ \mathop{}\!\mathrm{d}{\mathbf{x}}_t &= \tilde{{\mathbf{f}}}({\mathbf{x}}_t,t)\mathop{}\!\mathrm{d}t + g(t)\mathop{}\!\mathrm{d}{\mathbf{w}}_t. \end{align*}\] For simplicity, assume \(g(t)>0\) on the interval of interest and the standard integrability conditions required by Girsanov’s theorem. Take \(P_{\tilde{{\mathbf{f}}}}\) as the reference path measure, and let \({\mathbf{w}}_t\) denote the Brownian motion under this reference measure. Define \[\bm{\delta}_t := {\mathbf{f}}({\mathbf{x}}_t,t)-\tilde{{\mathbf{f}}}({\mathbf{x}}_t,t).\]
Girsanov’s Likelihood Ratio.
Girsanov’s theorem gives the Radon–Nikodym derivative between the two path measures:
\[\frac{\mathop{}\!\mathrm{d}P_{{\mathbf{f}}}}{\mathop{}\!\mathrm{d}P_{\tilde{{\mathbf{f}}}}}({\mathbf{x}}_{0:T}) = \exp\left( \int_0^T \bm{\delta}_t^\top g(t)^{-1}\mathop{}\!\mathrm{d}{\mathbf{w}}_t - \frac{1}{2} \int_0^T \big\|g(t)^{-1}\bm{\delta}_t\big\|^2 \mathop{}\!\mathrm{d}t \right).\]
This compact formula is an exponential of two integrals. The first is an Itô integral, while the second is a standard Riemann integral. This ratio is crucial in diffusion models, allowing us to compare different stochastic dynamics and to connect path-space discrepancies with likelihood-based training objectives.
Girsanov’s theorem is best understood as a change-of-variable formula for measures. Just as a change of variables in calculus transforms an integral between coordinate systems via the Jacobian determinant, Girsanov’s theorem provides the corresponding factor (the Radon–Nikodym derivative) to transform probabilities or expectations between two stochastic processes, when the drift changes but the diffusion remains the same.
Girsanov’s Theorem as a Bridge Between Likelihood Training and Score Matching
After understanding how Girsanov’s theorem relates the likelihoods of a single path under different drift assumptions, we now delve into its implications for diffusion models.
Recall the forward SDE in diffusion models: \[\mathop{}\!\mathrm{d}{\mathbf{x}}_t = {\mathbf{f}}({\mathbf{x}}_t, t) \mathop{}\!\mathrm{d}t + g(t) \mathop{}\!\mathrm{d}{\mathbf{w}}_t,\] which induces a path distribution \(P\) over full trajectories \({\mathbf{x}}_{0:T} := \{{\mathbf{x}}_t\}_{t=0}^T\) (that is, the joint law of the process over the entire time interval). The reverse-time SDE, parameterized by a learnable score function \({\mathbf{s}}_{\bm{\phi}}({\mathbf{x}}_t, t)\), is given by \[\mathop{}\!\mathrm{d}{\mathbf{x}}_t = \big[{\mathbf{f}}({\mathbf{x}}_t, t) - g^2(t) {\mathbf{s}}_{\bm{\phi}}({\mathbf{x}}_t, t)\big] \mathop{}\!\mathrm{d}t + g(t) \mathop{}\!\mathrm{d}\bar{{\mathbf{w}}}_t,\] which in turn defines another path distribution \(P_{\bm{\phi}}\) over trajectories.
Two Notions in Diffusion Models.
It is useful to distinguish two levels at which two stochastic processes can agree:
Concept 1. Marginal Distribution Matching: The processes have the same distribution \(p_t({\mathbf{x}}_t)\) at every fixed time \(t\). Thus, their time-indexed “snapshots” agree.
Concept 2. Joint Path Distribution Matching: The processes have the same probability law over entire trajectories. This stronger notion also matches how states at different times are coupled, including their transition laws and temporal dependencies.
Matching the full path law automatically implies marginal matching, since each \(p_t\) is obtained by projecting the path law onto the state at time \(t\). The converse is generally false: two processes can have identical marginals at every time while inducing different couplings between those marginals. The SDE and its associated PF-ODE provide an important example: they share the same marginal density path, but generally have different sample trajectories and path laws.
Girsanov Connects Score Error to Path-Law Mismatch.
The exact reverse-time SDE satisfies the stronger notion above. Under standard regularity conditions, when initialized from the exact terminal marginal \(p_T\) and equipped with the true score \(\nabla_{{\mathbf{x}}}\log p_t({\mathbf{x}})\), it is the time reversal of the forward diffusion and therefore reproduces its full time-reversed path law.
Let \(P^{\mathrm{rev}}\) denote this exact reverse-time path law, and let \(P_{\bm{\phi}}\) denote the learned reverse-time path law obtained by replacing the true score with \({\mathbf{s}}_{\bm{\phi}}\) and initializing from \(p_{\mathrm{prior}}\). Decomposing the path-space KL into the mismatch between the initial distributions and the conditional path laws, and applying Girsanov’s theorem to the latter after expressing both processes using the same increasing reverse-time clock, gives, in the original diffusion-time label \(t\), \[\begin{align} \label{eq:girsanov-KL} \begin{aligned} \mathcal{D}_{\mathrm{KL}} \bigl(P^{\mathrm{rev}}\|P_{\bm{\phi}}\bigr) = &\; \mathcal{D}_{\mathrm{KL}} \bigl(p_T\|p_{\mathrm{prior}}\bigr) \\ &+ \frac{1}{2} \int_0^T g^2(t) \mathbb{E}_{{\mathbf{x}}_t\sim p_t} \left[ \left\| {\mathbf{s}}_{\bm{\phi}}({\mathbf{x}}_t,t) - \nabla_{{\mathbf{x}}}\log p_t({\mathbf{x}}_t) \right\|^2 \right] \mathop{}\!\mathrm{d}t . \end{aligned} \end{align}\] The first term measures the mismatch between the true terminal distribution and the prior used to initialize generation; it vanishes when \(p_T=p_{\mathrm{prior}}\). The second term measures the accumulated mismatch between the learned and exact reverse drifts.
Thus, score matching with the particular likelihood weighting \(g^2(t)\) has a direct path-space interpretation: minimizing the weighted score error minimizes the KL divergence between the exact and learned reverse path laws, up to the fixed terminal-distribution term. When denoising score matching is used instead of marginal score matching, the corresponding objectives differ by a parameter-independent constant. A different time weighting generally does not correspond to this same path-space KL.
Connection to Data Likelihood.
The path-space KL also controls the discrepancy between the generated and data marginals. Since taking the endpoint of a trajectory is a measurable projection and KL divergence cannot increase under marginalization, \[\begin{align} \label{eq:girsanov-likelihood} \mathcal{D}_{\mathrm{KL}} \bigl(p_{\mathrm{data}}\|p_{\bm{\phi}}\bigr) \leq \mathcal{D}_{\mathrm{KL}} \bigl(P^{\mathrm{rev}}\|P_{\bm{\phi}}\bigr), \end{align}\] where \(p_{\bm{\phi}}\) denotes the generated marginal at data time.
Combining this with Equation \(\eqref{eq:girsanov-KL}\) shows that likelihood-weighted score matching, together with the terminal-distribution mismatch, provides an upper bound on the data-side KL divergence and therefore on the expected negative log-likelihood up to a constant independent of \(\bm{\phi}\) (Song et al. 2021).
Itô’s isometry links stochastic integrals to standard integrals in expectation; we omit the proof as it requires the full machinery of Itô calculus. For a process \(\bm{\psi} : [0,T] \to \mathbb{R}^{D \times D}\), Itô isometry states \[\mathbb{E}\left[\left\| \int_0^T \bm{\psi}(t) \mathrm{d}{\mathbf{w}}_t \right\|^2 \right] = \mathbb{E}\left[\int_0^T \|\bm{\psi}(t)\|_F^2 \mathrm{d}t \right],\] where \(\|\bm{\psi}(t)\|_F^2 = \sum_{i,j=1}^D |\psi_{ij}(t)|^2\) is the Frobenius norm. For \(\bm{\psi}(t) \in \mathbb{R}^D\) (a vector), the integral is scalar and the isometry simplifies to \[\mathbb{E}\left[\left(\int_0^T \bm{\psi}(t) \mathrm{d}{\mathbf{w}}_t\right)^2\right] = \mathbb{E}\left[\int_0^T \|\bm{\psi}(t)\|^2 \mathrm{d}t \right].\]↩︎