Skip to content

Weighted adjoints: the gradient of the data misfit from one adjoint solve per member - #205

Merged
KriFos1 merged 2 commits into
Python-Ensemble-Toolbox:mainfrom
MathiasMNilsen:pr/weighted-adjoint
Nov 27, 2026
Merged

KriFos1 merged 2 commits into
Python-Ensemble-Toolbox:mainfrom
MathiasMNilsen:pr/weighted-adjoint

Conversation

@MathiasMNilsen

Copy link
Copy Markdown
Contributor

What this adds

Until now an adjoint in PET was the sensitivity of every observed value to the state: the (nd, nx, ne) array
ensemble.adjoints, which costs the simulator one adjoint solve per observed value. A method that needs only the
gradient of the data misfit can now ask for that alone.

A simulator in weighted-adjoint mode is handed weights w and returns the gradient of sum_i w_i d_i per
parameter, as a pandas.Series indexed by parameter name.

  1. ForecastMixin._adjoint_array 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. ensemble.adjoints is then
    (nx, ne). Gradients that do not name the state variables one to one, or have another size, are refused.
  2. pipt.ensembles.DataMisfitWeights (new module pipt/ensembles/adjoint_weights.py) gives the weights
    w = C_d⁻¹ (d_obs − g(x)) of one member from its forward output, for a full or a diagonal data covariance and
    for max-min scaled data. With these weights the simulator returns Gᵀ C_d⁻¹ (d_obs − g(x)). It is set as the
    simulator'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, the compute_adjoints row of
docs/configuration.md, and a changelog entry under Unreleased.

Tests

New tests/assimilation/test_weighted_adjoints.py (10 tests):

  • the weights vanish at the observations, equal −½ of the misfit gradient by finite differences (also for scaled
    data and a full covariance), and are NaN for unobserved cells;
  • gradients are placed by name and mismatching ones are refused;
  • a linear simulator d = Hx driven through AssimilationEnsemble.forecast gives
    ensemble.adjoints == Hᵀ C_d⁻¹ (d_obs − Hx) for every member.

Run locally: ruff check src tests is clean; pytest -m "not slow" gives 693 passed. Seven tests in tests/optimization (test_genopt.py and one in test_ensembles.py) fail on this machine, and they fail in the same way on an untouched upstream/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's
sensitivities. This PR reaches the same quantity through run_fwd_sim: the simulator returns the weighted
gradient 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 and
Python-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

MathiasMNilsen and others added 2 commits November 26, 2026 09:07
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>
@KriFos1
KriFos1 merged commit 1be5e6b into Python-Ensemble-Toolbox:main Nov 27, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants