3D-Var for methane retrieval¶
Ported from plumax 0.1.2 (28f257e); module and link paths updated for radtrax.
This note derives the cost function used by radtrax.retrieval.var3d, walks through the three control-variable formulations we implement, and then maps each piece of the maths onto a concrete jax, lineax, optimistix or gaussx call. The aim is that you can read it side by side with solve.py and see which line implements which equation.
The derivation follows Bouttier & Courtier’s Data assimilation concepts and methods [1] and Rodgers’ Inverse methods for atmospheric sounding [2], although the letters are not theirs: we write the GeoStack notation, and the crosswalk table below maps their letters (and the code’s argument names) onto ours. The JAX-side implementation tricks are from the lineax docs [3] and the optimistix paper [4].
Symbols
| Symbol | Meaning | Shape | Unit |
|---|---|---|---|
| number of state pixels, for an field | scalar | dimensionless | |
| number of observations, the flattened band-radiance cube () | scalar | dimensionless | |
| state: the per-pixel VMR field, flattened | VMR | ||
| observations: the band-integrated radiance cube, flattened | radiance | ||
| prior (background) mean of the state | VMR | ||
| prior covariance of the state | VMR² | ||
| observation-error covariance | radiance² | ||
| nonlinear observation operator | VMR to radiance | ||
| tangent-linear of at the prior mean | radiance / VMR | ||
| innovation, | radiance | ||
| increment, | VMR | ||
| 3D-Var cost over the state | scalar | dimensionless | |
| posterior precision, the Hessian of | VMR⁻² | ||
| square root of the prior covariance, | VMR | ||
| whitened control variable, | dimensionless | ||
| dual (PSAS) variable | radiance⁻¹ | ||
| hyperparameters of the retrieval | various |
Crosswalk from the classic data-assimilation letters, and to the argument names in radtrax.retrieval.var3d:
| Classic (Bouttier & Courtier) | Rodgers | This page | radtrax argument |
|---|---|---|---|
background_state | |||
background_op, background | |||
obs_inv_variance (passes , scalar or diagonal) | |||
forward_fn | |||
none (from jax.jvp / jax.vjp of forward_fn) | |||
observation | |||
| n/a | Cost (build_cost_x, build_cost_xi) | ||
| n/a | WhiteningTransform.cholesky_op | ||
| n/a | lam inside run_dual_psas |
1. From Bayes to the 3D-Var cost¶
We have a state vector , which for our methane retrieval is the per-pixel VMR field flattened to length , and an observation vector , the band-integrated radiance cube. Both come with Gaussian uncertainty:
Prior: . The background mean is whatever we knew before the observation arrived (typically a uniform climatological VMR), and encodes the per-pixel variance and any spatial correlations.
Likelihood: , with the (possibly nonlinear) observation operator and the observation-error covariance . In our case .
By Bayes’ rule the posterior is proportional to , so we will have:
The MAP estimate is the 3D-Var analysis. The “3D” is historical: in weather prediction it refers to the three spatial dimensions of the state, and what it really says is that there is no time axis, which is what distinguishes 3D-Var from 4D-Var (our state is a 2-D field).
1.1 Incremental form¶
In practice we minimise over the increment , for two reasons: 1) the background term then has a clean quadratic form, and 2) the observation operator is often linearised around (the inner loop of incremental 3D-Var). Substituting and writing for the innovation, we will have:
where evaluated at is the tangent-linear operator. Setting the gradient to zero gives the normal equations:
The matrix on the left is the Hessian of (2), which is the posterior precision, (the prior precision plus the information the observations add). Its inverse is the textbook posterior covariance for the linear-Gaussian case.
In code. Equation (1) is built by
build_cost_x. The prior term isgaussx_linalg.quadratic_form(background_op, δx_flat), one structurally dispatched solve, so any structured from background.py (diagonal, Kronecker, low-rank) is plugged in without being materialised. The observation term differentiates through the JAXforward_fnby reverse-mode AD, so we never write by hand.
2. Why preconditioning matters¶
The catch in (3) is that the conditioning of tracks the spread of its eigenvalues. In a typical retrieval has a large dynamic range (some pixels are tightly constrained by the prior, others very loosely), so a plain minimiser like L-BFGS can take hundreds of iterations to make progress along the floppy directions.
The control variable transform (CVT) removes this by changing variables to , where is any square root of the prior covariance:
We pick the Cholesky factor, but other choices (an eigendecomposition, balance operators) work the same way. Substituting into (2) gives:
The prior term collapses to , a sphere, and the Hessian in becomes:
This is the identity plus a positive-semidefinite term of rank at most , so every eigenvalue is at least one and at most of them differ from one. So the badly conditioned prior has dropped out entirely: what is left is set by how informative the observations are (the largest eigenvalue is one plus the largest whitened signal-to-noise ratio squared), and in exact arithmetic conjugate gradients on this Hessian finishes in at most iterations. In practice this is the biggest lever we have in variational DA, and L-BFGS benefits from it the same way.
In code. Equation (5) is built by
build_cost_xi; the operator is constructed byWhiteningTransform.from_background, which callsgaussx_linalg.cholesky(background). gaussx returns a structured operator (a Cholesky of aKroneckeris itself a Kronecker of Cholesky factors), so all the matvecs and inverse solves that follow stay in the cheap regime.
3. Primal vs dual: PSAS¶
The normal equations (3) are an system. When the observation count is small (, typical for sparse plumes seen by an instrument with a few bands), the dual form is a lot cheaper. Applying the matrix-inversion lemma to (3), we will have:
The middle factor is an system. Defining the dual variable (it is the Lagrange multiplier of the observation constraint, often written , a letter we keep for wavelength), the recipe is:
Solve for (CG-friendly, since the matrix is symmetric positive definite).
Set .
This is the Physical-Space Statistical Analysis System (PSAS) [5]. For a linear , the dual and primal give the same analysis. For a nonlinear , both solve the same linearised problem, the inner loop of each outer iteration of incremental 3D-Var or 4D-Var, so the choice between them is about cost ( against ) and not about the answer.
In code.
run_dual_psaswraps the dual matrix as alineax.FunctionLinearOperator, tags it symmetric and PSD, and feeds it tolineax.linear_solve(..., solver=lineax.CG(...)). Inside the matvec, is , which is exact because the solver expects a forward that is linear in (callers passmodel.make_forward(linear=True)), and comes from a singlejax.vjppullback hoisted out of the CG loop. There is no hand-written adjoint of the observation operator anywhere in the code.
3.1 The matched filter as the flat-prior limit of PSAS¶
Now take a single pixel and restrict the increment to one direction, a uniform enhancement of strength , so that with the target signature ( applied to a unit enhancement). In addition, let the prior on be flat (its variance ) and let be the per-pixel clutter covariance , with the departure of the pixel spectrum from the background mean. Then the dual system is , and in the limit (7) becomes the pixel-wise matched filter:
The full limit, with the Sherman–Morrison step, is on the matched filter page (§4). So the matched filter, a narrow-band retrieval and full 3D-Var are the same estimator with progressively richer prior assumptions, and the matched filter in radtrax.retrieval.matched_filter is the simplest case of the framework here. Note that it is the exact, closed-form solution of the restricted one-parameter problem (a single scalar solve); it is not what one CG iteration on the full dual system returns, since the first CG step moves along rather than along .
4. Adjoint methods: four flavours¶
Computing the gradient needs the adjoint (transpose) of the tangent-linear . Historically this was the part of variational DA you wrote and maintained by hand. With JAX we get four options out of the box, each with a different cost and memory profile:
| Method | When it applies | Cost | Memory | JAX call |
|---|---|---|---|---|
| Discrete adjoint (reverse-mode AD) | is a JAX program; one forward pass | one VJP is ; a full Jacobian is of them | jax.grad(loss) | |
| Tangent-linear (forward-mode AD) | sensitivities, ensemble propagation | one JVP is ; a full Jacobian is of them | jax.jvp(h, (x,), (v,)) | |
| Implicit-function adjoint | differentiating through a converged inner solve | one linear solve | optimistix.ImplicitAdjoint(linear_solver=...) | |
| Checkpointed adjoint | memory-bounded long unrolls (ODE, iterative) | with checkpoints, about two forwards with | for checkpoints | optimistix.RecursiveCheckpointAdjoint(...) |
4.1 Discrete adjoint (jax.grad)¶
This is what we use for the observation-term gradient in (1). Concretely, build_cost_xi wraps the cost in jax.value_and_grad(value), and reverse-mode AD walks back through forward_fn applying each elementary VJP, including the kernel-flipped convolution adjoint inside PointSpreadFunction and the spread-with- adjoint inside GroundSamplingDistance. The unit tests in test_psf.py verify the inner-product identity for both operators, so the autodiff gradient is the one a hand-rolled adjoint would give.
4.2 Tangent-linear (jax.jvp)¶
jax.jvp(h, (x,), (v,)) computes in one forward pass without ever forming the Jacobian. It is useful for:
The dual matvec inside (7), one per CG iteration.
run_dual_psasgets the same product withoutjax.jvp, as the difference , which is exact for the linear forward it expects; ajax.jvpwould be the drop-in for a nonlinear .Hessian-vector products by forward-over-reverse:
jax.jvp(jax.grad(J), (x,), (v,))returns at cost. This is whatCost.hvpdoes, and it is what feeds the diagnostics in diagnostics.py.
4.3 Implicit-function adjoint¶
Suppose we want , the derivative of the converged 3D-Var solution with respect to a hyperparameter (e.g. a regularisation weight, an observation-error variance, a prior length scale). Differentiating through the L-BFGS unroll would be both expensive and numerically suspect.
The implicit-function theorem gives a closed form instead. The optimum satisfies , and differentiating that identity in gives:
where is the mixed second derivative. So we only need one linear solve with the Hessian (the posterior precision again). For a loss evaluated at the analysis, the total derivative then follows from the chain rule:
This is what optimistix.ImplicitAdjoint(linear_solver=lineax.CG(...)) does internally: you write a normal optimistix.minimise(...) call, and gradients with respect to anything captured by the cost flow back through this single adjoint solve, with no tape and no checkpoints.
In code. The solvers in solve.py run optimistix with its default adjoint,
ImplicitAdjoint(), but the wrappers do not expose anadjointargument. To differentiate the analysis with respect to its inputs, calloptimistix.minimisedirectly with a customlinear_solver(e.g.lineax.CG(rtol=...)for matrix-free Hessians, orlineax.Cholesky()for small dense ones).
4.4 Checkpointed adjoint¶
When the forward map is itself an iterative procedure (e.g. a chemistry solver inside the observation operator, or a Kalman-smoother prior), the reverse-mode tape can blow up memory. optimistix.RecursiveCheckpointAdjoint(checkpoints=k) does binomial (treeverse) checkpointing: only forward states are stored, and the rest are recomputed on the backward pass, so memory is traded for recomputation as in the table above. We don’t currently need this, since the methane forward map is a single fused convolution chain, but it is the right tool the moment we couple the retrieval to a dynamical model.
5. Mapping maths to libraries¶
| Math object | Library call |
|---|---|
| (Kronecker, low-rank, diagonal) | gaussx_linalg.Kronecker, gaussx_linalg.LowRankUpdate, lineax.DiagonalLinearOperator |
gaussx_linalg.quadratic_form(background_op, δx) (structured dispatch) | |
gaussx_linalg.solve(background_op, δx) (structured dispatch) | |
| such that | gaussx_linalg.cholesky(background) |
| (forward operator, JAX) | RadianceObservationModel.forward(...), make_forward(...) |
| (tangent-linear) | jax.jvp(forward_fn, (x,), (v,)) |
| (adjoint) | jax.vjp(forward_fn, x)[1](u), no manual adjoint |
| (discrete adjoint) | jax.grad(J) |
| (Hessian-vector) | jax.jvp(jax.grad(J), (x,), (v,)) |
| Outer minimiser (whitened L-BFGS) | optimistix.LBFGS(...) via optimistix.minimise(...) |
| Gauss–Newton with a custom inner solver | optimistix.GaussNewton(...) with lineax.CG(...) for , the residual_fn |
| Dual (PSAS) solve | lineax.linear_solve(dual_op, d, solver=lineax.CG(...)) |
| Adjoint through the converged solve | optimistix.ImplicitAdjoint(linear_solver=lineax.CG(...)) |
reduced_chi_squared | |
| (Gauss–Newton, whitened residuals) and its inverse | argminax.curvature.gauss_newton(...) through gauss_newton_posterior, then Laplace.covariance() |
| Averaging kernel | argminax.diagnose.averaging_kernel(posterior, background_op), or averaging_kernel from a Cost.hvp |
| DFS , Rademacher probes from a JAX key | argminax.diagnose.degrees_of_freedom(kernel, key=key), through degrees_of_freedom_for_signal |
from a Cost.hvp | posterior_covariance_proxy (argminax.curvature.Laplace.covariance() with a CG solve) |
6. Worked example¶
The end-to-end demo lives in plumax’s 06
Bouttier, F. & Courtier, P. (1999). Data assimilation concepts and methods. Meteorological Training Course Lecture Series, ECMWF. https://
www .ecmwf .int /sites /default /files /elibrary /2003 /16928 -data -assimilation -concepts -and -methods .pdf Rodgers, C. D. (2000). Inverse methods for atmospheric sounding: theory and practice. World Scientific.
Rader, J., Lyons, T., & Kidger, P. (2024). Lineax: a library for linear solvers in JAX. https://
docs .kidger .site /lineax/ Rader, J., Lyons, T., & Kidger, P. (2024). Optimistix: modular optimisation in JAX and Equinox. arXiv:2402.09983.
Cohn, S. E., et al. (1998). Assessing the effects of data selection with the DAO Physical-Space Statistical Analysis System. Mon. Wea. Rev. 126, 2913-2926.