Chen 2018 - Adjoint Sensitivity Method
Summary
To differentiate a scalar loss of an ODE solution, define the adjoint . It obeys , solved backwards in time together with and a running integral for . One augmented ODE call returns and with memory in depth, using only vector-Jacobian products (VJPs) of . This is the machinery behind custom differentiable ODE Ops in probabilistic programming.
Overview
Backpropagating through each solver step is possible but stores the whole forward trajectory and propagates numerical error through the solver’s internals. The adjoint method (Pontryagin et al. 1962) instead treats the solver as a black box. The paper’s contribution is to present it in the language of reverse-mode autodiff, with a generic VJP interface so an ODE solve composes with any other differentiable component.
Main Content
Setting: a loss of the terminal state of an initial value problem
Adjoint state
(the instantaneous analogue of the backpropagated gradient at a hidden layer).
Adjoint ODE and parameter gradient (eqs. 4-5)
Let solve . Then
Starting from and solving backward to gives .
Proof sketch (Appendix B.1). Over a short time step , . The chain rule (as in layer-wise backprop) gives . Hence
Parameters and times (B.2). Treat and as extra states with , , giving an augmented state whose Jacobian has zero rows for . Applying the adjoint equation to the augmented system gives with , and similarly , (eq. 52 and Algorithm 2 in Appendix C: , and with initialization ).
Algorithm 1: reverse-mode derivative of an ODE initial value problem
Input: , final state , .
- Initial augmented state .
- Dynamics (two VJPs by autodiff).
- .
Multiple observation times. If the loss depends on intermediate states (as in any time-series likelihood), split the backward solve into one solve per interval and, at each observation time, add to the adjoint (Fig. 2). Without this jump the gradient of an ODE-based likelihood is wrong.
Properties. The state is not stored: it is recomputed backwards along with . All three integrals () are computed in a single solver call. Cost scales linearly in the state dimension. This extends Stapor et al. 2018 sec. 2.4.2. Contrast with forward sensitivity analysis (used by Stan’s ODE interface, per the paper): quadratic-time in the number of variables, whereas adjoint is linear.
Caveat on exactness. Recomputing backward can diverge from the forward trajectory (reconstruction error), and for chaotic or stiff dynamics this can make adjoint gradients unreliable; checkpointing (store some forward states) fixes it. See Chen 2018 - Scope, Limitations and Statistical Use.
Examples
Worked example: scalar decay. , , , , with .
- Analytic: , , .
- Adjoint: , , . Solve backward from to ; the third component ends at .
import numpy as np
from scipy.integrate import solve_ivp
theta, z0, T, y = 0.7, 2.0, 1.5, 0.3
f = lambda z, th: -th*z
dfdz = lambda z, th: -th
dfdth = lambda z, th: -z
zT = solve_ivp(lambda t,z: f(z,theta), [0,T], [z0], rtol=1e-10, atol=1e-12).y[0,-1]
aug = lambda t,s: [f(s[0],theta), -s[1]*dfdz(s[0],theta), -s[1]*dfdth(s[0],theta)]
sol = solve_ivp(aug, [T,0], [zT, zT-y, 0.0], rtol=1e-10, atol=1e-12)
z0_rec, dL_dz0, dL_dtheta = sol.y[:, -1]
print(z0_rec, dL_dz0, dL_dtheta) # 2.0, 0.139931..., -0.419794...Output matches the analytic values to about (verified when writing this note).
Mapping to PyMC/PyTensor (E09). A custom Op needs perform (forward solve) and grad, which receives the upstream gradient and must return . That is the adjoint seed ; jax.vjp(solve, theta) supplies the pullback so no hand-derived sensitivity ODE is needed. Pseudocode (untested):
import jax
def make_op(solve_jax): # solve_jax(theta) -> predicted trajectory
out, pullback = jax.vjp(solve_jax, theta0)
# forward: out; backward: pullback(upstream) -> d logp / d thetaConnections
- The likelihood-level use (ODE solution as the mean of a Gaussian observation model): Likelihoods Built on Differential Equations (Concept Note).
- For chaotic dynamics gradients explode and the likelihood surface is rough; see Chaos and Phase-Insensitive Statistics and Chaos, Rough Likelihoods and Data Assimilation (Concept Note).
- HMC needs these gradients: HMC and Stan in Practice; the planetary-motion multimodality example: Challenge of Multimodality - Differential Equation for Planetary Motion.
See Also
Chen 2018 - Neural ODEs - Overview, ODE Filters and Smoothers (solver uncertainty as inference), Automatic Differentiation Variational Inference (ADVI).