Repository navigation
Weighted adjoints: the gradient of the data misfit from one adjoint solve per member - #205
Merged
KriFos1 merged 2 commits intoNov 27, 2026
Conversation
A simulator that computes adjoints returns the sensitivity of every observed value to the state, and the ensemble stacks these as an (nd, nx, ne) array. A simulator in weighted-adjoint mode is handed weights w and returns the gradient of sum_i w_i d_i per parameter instead, from one adjoint solve per member: a Series indexed by parameter name. _adjoint_array now takes such a Series. Each gradient is placed at its state variable's rows by name, so a parameter list in another order than the state cannot pair a gradient with the wrong variable. A log_ prefix is the simulator naming the log of the variable the state holds. Gradients that do not name the state variables one to one, or have another size, are refused. The simulator protocol describes the mode and the adjoint_weights callable a caller sets on the simulator. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
A weighted adjoint is the gradient of a weighted sum of the simulator's outputs. With the weights w = C_d^-1 (d_obs - g(x)) it is G^T C_d^-1 (d_obs - g(x)), the gradient of the log-likelihood, at the cost of one adjoint solve per member whatever the number of data. DataMisfitWeights computes these weights for one member from its forward output, reading the predictions off the output as the ensemble reads them into pred_data so that they stay row for row with the observations. It takes a full or a diagonal data covariance and max-min scaled data, and it is a plain picklable object because parallel runs ship it to their workers. A caller sets it as the simulator's adjoint_weights. The test drives a linear simulator through AssimilationEnsemble.forecast and compares ensemble.adjoints with H^T C_d^-1 (d_obs - H x). Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This was referenced Nov 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this adds
Until now an adjoint in PET was the sensitivity of every observed value to the state: the
(nd, nx, ne)arrayensemble.adjoints, which costs the simulator one adjoint solve per observed value. A method that needs only thegradient of the data misfit can now ask for that alone.
A simulator in weighted-adjoint mode is handed weights
wand returns the gradient ofsum_i w_i d_iperparameter, as a
pandas.Seriesindexed by parameter name.ForecastMixin._adjoint_arraytakes such a Series. Each gradient is placed at its state variable's rows byname, so a parameter list in another order than the state cannot pair a gradient with the wrong variable. A
log_prefix is the simulator naming the log of the variable the state holds.ensemble.adjointsis then(nx, ne). Gradients that do not name the state variables one to one, or have another size, are refused.pipt.ensembles.DataMisfitWeights(new modulepipt/ensembles/adjoint_weights.py) gives the weightsw = C_d⁻¹ (d_obs − g(x))of one member from its forward output, for a full or a diagonal data covariance andfor max-min scaled data. With these weights the simulator returns
Gᵀ C_d⁻¹ (d_obs − g(x)). It is set as thesimulator's
adjoint_weights, and it is a plain picklable object because parallel runs ship it to their workers.Nothing changes for the existing Jacobian adjoints or for any existing scheme. No scheme in this PR uses the new
path; the SVGD scheme in the follow-up PR does.
Docs
ensemble.protocols.ForwardSimulator,docs/architecture.md, thecompute_adjointsrow ofdocs/configuration.md, and a changelog entry under Unreleased.Tests
New
tests/assimilation/test_weighted_adjoints.py(10 tests):data and a full covariance), and are NaN for unobserved cells;
d = Hxdriven throughAssimilationEnsemble.forecastgivesensemble.adjoints == Hᵀ C_d⁻¹ (d_obs − Hx)for every member.Run locally:
ruff check src testsis clean;pytest -m "not slow"gives 693 passed. Seven tests intests/optimization(test_genopt.pyand one intest_ensembles.py) fail on this machine, and they fail in the same way on an untouchedupstream/main; they are not related to this change.Relation to #204
#204 defines a simulator hook
run_vjp(state, weights)that a scheme calls for weighted sums of the data'ssensitivities. This PR reaches the same quantity through
run_fwd_sim: the simulator returns the weightedgradient with the member's forward output, for weights that a callback computes from that output. In code the two
only conflict in
CHANGELOG.md, but they are two interfaces for one capability (Python-Ensemble-Toolbox/SimulatorWrap#18 andPython-Ensemble-Toolbox/SimulatorWrap#20 are their wrapper sides). This is opened as a draft so that they can be reconciled.
Related
The JutulDarcy wrapper returns such an adjoint with
weighted_adjoint = {parameters = [...]}:Python-Ensemble-Toolbox/SimulatorWrap#20. PET's tests do not need it.
🤖 Generated with Claude Code