Sensitivity analysis#
Every solve in aquakin, of a single reactor or a whole plant, is
differentiable. Rate constants are always passed in as arguments (never
baked into the rate functions), so you can take exact gradients of any output
with respect to parameters, conditions or initial states, and use them for
sensitivity analysis, parameter estimation, experiment design and design
optimization.
A plain forward simulation never needs any of this. Reach for it when you want
to know how an output depends on its inputs, or to fit a model to data. This
page covers gradients, sensitivity analysis and uncertainty propagation; the
other questions have pages of their own, and the statistical ideas they use
are explained in Key ideas. The plant-only functions
plant_fisher_information, plant_profile_likelihood and
plant_rank_sensor_sets are imported from aquakin.plant.
Which tool do I need?#
Question |
Reactor |
Plant |
Page |
|---|---|---|---|
How does an output change with each parameter, here? |
|
|
|
Which inputs matter over a whole range? |
|
|
|
How uncertain is a prediction? Which process design (a dose, a size) is best under constraints? |
|
the same |
|
Which parameter values fit my data? |
|
|
|
How well do the data determine them? |
the Laplace posterior, |
the same, with |
|
Which model structure do the data support? |
|
the same |
|
What should I measure next? |
|
|
For a complete study that uses most of these tools in turn, see Worked example: designing an experiment.
The examples on this page#
The reactor examples share one setup: the shipped ozone_bromate model in a
batch reactor (Reactors explains the reactor options), with
synthetic observations of ozone and bromate made from the default parameters
plus 3 % noise. The model’s concentrations are in mol/L and its time unit is
the second. Run this block first; every reactor block below then runs as
written, in order.
import aquakin
import jax
import jax.numpy as jnp
import numpy as np
model = aquakin.load_model("ozone_bromate")
reactor = aquakin.BatchReactor(model, model.default_conditions())
C0 = model.default_concentrations()
params = model.default_parameters()
i = model.species_index # species name -> column
free_params = ["O3_Br_direct.k1", "O3_decay_OH.k_dec"]
i_free = jnp.array([model.param_index[n] for n in free_params])
observed = ["O3", "BrO3-"]
t_obs = jnp.linspace(30.0, 600.0, 12) # seconds
truth = reactor.solve(C0, t_span=(0.0, 600.0), t_eval=t_obs, params=params)
clean = jnp.stack([truth.C_named(s) for s in observed], axis=1) # (12, 2)
sigma = 0.03 * clean.max(axis=0) # one noise level per species
rng = np.random.default_rng(0)
observations = clean + sigma * rng.standard_normal(clean.shape)
Differentiating a solve#
The simplest case is an ordinary JAX gradient through a reactor solve:
def final_bromate(p):
sol = reactor.solve(C0, t_span=(0.0, 600.0), t_eval=t_obs, params=p)
return sol.C_named("BrO3-")[-1]
g = jax.grad(final_bromate)(params) # d[BrO3-]_final / d params, shape (7,)
A reactor solve is differentiated in reverse mode (jax.grad, jax.jacrev).
jax.jacfwd through reactor.solve raises an error; the tools below that work
in forward mode build a forward-capable solve for you.
Warning
Silent non-finite reverse gradients on stiff models. A reverse-mode
gradient taken directly through the solve of a stiff model (the activated
sludge models ASM, the anaerobic digestion model ADM, the WATS sewer models)
can come back as NaN or Inf when the integrator step is uncapped. No
exception is raised, so the bad gradient flows into your optimizer.
aquakin.calibrate and aquakin.sensitivity check the gradient they compute
and raise an error naming the remedy instead of returning NaN. They do not
make it finite by themselves. To get a finite gradient through a stiff reactor:
cap the step, building the reactor with
settings=aquakin.SolverSettings(integrator=aquakin.IntegratorConfig(dtmax=...));use forward-mode sensitivities (
solve_sensitivity, ordiff=aquakin.DifferentiationConfig(mode="forward")), which need no cap; orin
calibrate, use the cap-free adjoint,diff=aquakin.DifferentiationConfig(method="stable")(batch reactors).
Differentiation options lists every setting.
In your own loss, aquakin.check_finite_gradient(g, what="my loss gradient", remedy="...") raises that kind of error for you.
Local and global sensitivity#
Local sensitivity#
aquakin.sensitivity computes the gradient of a scalar output with respect to
every parameter and every condition field in one call, and ranks them:
sens = aquakin.sensitivity(
reactor, C0,
output_fn=lambda sol: sol.C_named("BrO3-")[-1],
t_span=(0.0, 600.0), t_eval=t_obs,
)
sens.doutput_dparams # (7,) gradient with respect to each parameter
sens.doutput_dconditions["pH"] # (1,) sensitivity to a condition field
sens.ranked_params() # [(name, |elasticity|), ...] most influential first
ranked_params() ranks by the elasticity (dy/dp)·p/y, the percentage
change in the output per percentage change in a parameter
(sens.elasticities() returns them). The raw gradients in doutput_dparams
depend on each parameter’s units: this model’s rate constants span from about
10 to 10⁹, so a large gradient can belong to a parameter that barely matters.
ranked_params(elasticity=False) ranks by the raw gradient instead.
Forward sensitivity (cap-free stiff gradients)#
Differentiating through a stiff solve with ordinary AD goes non-finite
above a step-size threshold, and the usual workaround, a global dtmax cap,
forces tiny steps over the whole solve. solve_sensitivity avoids both: it
integrates the sensitivity S = dC/dθ alongside the state and lets the
adaptive step controller bound the error of the sensitivity too, so the step
shrinks only where the sensitivity needs it and the result is exact with no
cap.
uv = aquakin.load_model("uv_h2o2")
uv_reactor = aquakin.BatchReactor(uv, uv.default_conditions())
uv_sol, S = uv_reactor.solve_sensitivity(
uv.default_concentrations(), t_span=(0.0, 5.0),
t_eval=jnp.linspace(0.0, 5.0, 6), params=uv.default_parameters(),
sens_params=["H2O2_photolysis.k_photo", "OH_target.k_OH_target"],
)
S.shape # (6, 4, 2): (n_t, n_species, n_sens_params)
# The same with by-name accessors:
res = aquakin.forward_sensitivity(
uv_reactor, uv.default_concentrations(), params=uv.default_parameters(),
t_span=(0.0, 5.0), t_eval=jnp.linspace(0.0, 5.0, 6),
sens_params=["H2O2_photolysis.k_photo"])
res.dC_dparam("target", "H2O2_photolysis.k_photo").shape # (6,)
solve_sensitivity is available on BatchReactor, PlugFlowReactor and
BiofilmReactor. The plant version is
plant.solve_sensitivity(params, wrt, t_span=..., t_eval=..., y0=...), which
returns (ts, ys, S) with S of shape (n_t, n_states, k) for the k
parameters named in wrt. Repeated calls reuse one compiled solve. Its
options:
sens_error_control=Falsechooses the steps on the state’s error alone. The sensitivity is still solved at every step, but its error is no longer bounded: use it for a screening run, or one that would otherwise be very long.corrector="simultaneous"(instead of the default"staggered") solves the state and sensitivity together in a form you can differentiate again. It is much slower, andsens_error_control=Falseneeds the default corrector.return_stats=Truealso returns the step counts of the solve.
For a scalar loss of many parameters, as in calibration, reverse mode is
cheaper; plant.solve uses its cap-free adjoint for that automatically.
Global sensitivity (DGSM)#
aquakin.dgsm is a derivative-based global sensitivity measure: it averages
the squared derivative of an output over quasi-random samples of the input
ranges, which bounds the Sobol total-order index of each input. You supply a
function from an input vector to a scalar (or vector) output and a (low, high) range per input:
def bromate_umol(theta): # theta = [k1, k_dec] -> final bromate, µmol/L
sol = reactor.solve(C0, t_span=(0.0, 600.0), t_eval=t_obs,
params=params.at[i_free].set(theta))
return 1e6 * sol.C_named("BrO3-")[-1]
res = aquakin.dgsm(bromate_umol, [(80.0, 320.0), (35.0, 140.0)],
input_names=["k1", "k_dec"], n_samples=32, seed=0)
res.sobol_total_bound # (2,) upper bound on the Sobol total index per input
res.ranked() # [(name, bound), ...] most influential first
diff=aquakin.DifferentiationConfig(mode="forward") forms the per-sample
derivatives in forward mode. The result is the same; forward mode is faster
when there are many outputs, or when the reverse pass through a stiff solve is
expensive.
Variance-based global sensitivity (Sobol indices)#
DGSM gives an upper bound on each input’s total effect. It does not say what
fraction of the output variance an input explains on its own, or which inputs
interact. aquakin.sobol_indices estimates both, for each input z_j:
the first-order index
S_j, the fraction of the output variance thatz_jexplains on its own (Saltelli et al. 2010 estimator);the total-order index
S_j^tot, the same fraction including every interaction with other inputs (Jansen 1999 estimator);their difference,
res.interaction, the share thatz_jexplains only through interactions.
It takes the same function as dgsm, and its inputs are described as for
monte_carlo: a (low, high) range, or a uniform, normal or lognormal
distribution. The function does not need to be differentiable. As with
monte_carlo, it can also wrap a dynamic plant solve
(Uncertainty and scenarios).
The decomposition costs much more than DGSM. For d inputs and a base sample
of N points it runs N (d + 2) forward solves, where DGSM runs about N
differentiated ones. At N = 1024 and 17 inputs that is about 19 000 solves.
Check the count with sobol_indices_cost before you start:
Method |
Gives |
Solves for |
|---|---|---|
|
an upper bound on each |
about |
|
|
|
Use DGSM to screen many inputs. Then use Sobol indices on the few that matter, or on a cheap model.
aquakin.sobol_indices_cost(2, 64) # 256 solves for the two rate constants
sob = aquakin.sobol_indices(bromate_umol, {"k1": (80.0, 320.0), "k_dec": (35.0, 140.0)},
n_samples=64, seed=0,
bootstrap=aquakin.BootstrapConfig(confidence=0.9))
sob.first_order # (2,) share of Var(bromate) each rate explains alone
sob.total_order # (2,) the same including interactions
sob.interaction # (2,) total minus first order
sob.total_order_ci # (2, 2) bootstrap 90 % intervals, from 1000 resamples
The confidence intervals come from a bootstrap over the base sample. The
bootstrap costs no extra solves. bootstrap=aquakin.BootstrapConfig(...) sets
the number of resamples (n_resamples, default 1000, at least 100) and the
interval level (confidence, default 0.95); raise n_resamples for a high
level such as 0.99, whose tails hold few resamples. The bootstrap treats the
sample as independent random draws, so on the scrambled-Sobol design used here the intervals are
conservative: they are usually wider than the actual error. Increase
n_samples until the intervals are narrow enough to separate the inputs you
want to compare. With a small sample an index near zero can come out slightly
negative, which is sampling noise. The indices assume that the inputs are
independent.
Plant sensitivity#
A plant has its own sensitivity methods, all taking parameters by name
("asm1.muA", see plant.parameter_names()) through wrt=. A unit’s
operating settings are parameters too, named "<unit>.<setting>" and starting
at the value the unit was built with:
Unit |
Parameters |
|---|---|
Splitters |
each pump flow setpoint ( |
Clarifiers |
the controlled flow ( |
Aerated tanks |
an open-loop |
Controllers |
a PI controller’s |
|
|
|
a fixed dose |
|
|
|
|
|
|
|
|
|
|
Reactors and digesters |
the static temperature |
Influents carrying a temperature |
|
|
|
A unit’s sizes become parameters only when it is built with
size_parameters=True, so a plant that does not size its units keeps its
parameter vector. The unit’s dynamics, the heat and mass balances, and the
energy, sludge-age and N₂O figures then all read the parameter, which lets a
design study take gradients with respect to a size:
Unit |
Sizes |
|---|---|
|
|
|
|
|
|
|
|
from aquakin.plant import CSTRUnit, Plant
sized = Plant("sized", time_unit="d")
sized.add_unit(CSTRUnit(name="tank", model=aquakin.load_model("asm1"), volume=1000.0,
input_port_names=["in"], conditions={"T": 293.15},
size_parameters=True))
assert "tank.volume" in sized.parameter_names()
A storage tank’s initial level is a fraction of the volume it was built with. A setting written as an expression of time is not a parameter. The example uses the BSM1 plant of Plant-wide simulation, with its constant average-load influent:
from aquakin.plant.bsm import build_bsm1
asm1 = aquakin.load_model("asm1")
plant = build_bsm1(asm1)
plant.add_influent("feed", asm1.influent(
{"SI": 30.0, "SS": 69.5, "XI": 51.2, "XS": 202.32, "XB_H": 28.17,
"SNH": 31.56, "SND": 6.95, "XND": 10.59, "SALK": 7.0}, Q=18446.0))
si = asm1.species_index
def effluent_n(y): # flat plant state -> outputs
tank5 = plant.states_by_unit(y)["tank5"]
return jnp.array([tank5[si["SNH"]], tank5[si["SNO"]]])
# Exact steady-state sensitivity, from one steady-state solve: (2 outputs, 2 parameters)
S_ss = plant.steady_state_sensitivity(output_fn=effluent_n,
wrt=["asm1.muA", "asm1.KNH_A"])
# Sensitivity of a transient output, here the tank-5 nitrate trajectory
y_ss = plant.steady_state().state
S_dyn = plant.dynamic_sensitivity(
output_fn=lambda sol: sol.C_named("tank5", "SNO"),
t_span=(0.0, 1.0), t_eval=jnp.linspace(0.0, 1.0, 25),
wrt=["asm1.muA", "asm1.KNH_A"], mode="forward", y0=y_ss)
steady_state_sensitivityuses the implicit function theorem: no time integration, and one factorization serves every output and parameter. Passstate=to reuse a steady state you already have, andelasticity=Truefor the dimensionless(dg/dθ)(θ/g).dynamic_sensitivitydifferentiates a solve.mode="reverse"(default) uses the plant’s cap-free adjoint and suits many parameters;mode="forward"usesplant.solve_sensitivity, whose memory does not grow with the horizon, and passes its options (such assens_error_control) through.plant.steady_state_dgsm(ranges, output_fn=..., wrt=...)andplant.dynamic_dgsm(ranges, output_fn=..., wrt=..., t_span=...)are the plant versions ofdgsm, withrangesof shape(k, 2)aligned withwrt. A dynamic screen costs one differentiated solve per sample, so keepn_samplesand the parameter count modest.
Uncertainty propagation and design#
Three higher-level tools take a function of an input vector, as dgsm does.
Here they reuse bromate_umol from the DGSM example,
the final bromate as a function of [k1, k_dec]:
aquakin.monte_carlopropagates uncertain inputs (uniform, normal or lognormal marginals) through the model to an output ensemble.aquakin.compare_scenariosruns several named input sets side by side and tabulates the outputs.aquakin.optimize_designminimizes (or maximizes) an objective over bounded design variables subject to constraints, using AD gradients.
mc = aquakin.monte_carlo(
bromate_umol,
{"k1": {"dist": "lognormal", "mean": 160.0, "std": 40.0}, # physical mean, std
"k_dec": (35.0, 140.0)}, # (low, high): uniform
n_samples=64, seed=0)
mc.percentiles((2.5, 50.0, 97.5)) # (3, 1): per output
print(mc.summary())
scen = aquakin.compare_scenarios(
bromate_umol, {"slow": [80.0, 70.0], "fast": [320.0, 70.0]},
input_names=["k1", "k_dec"])
scen.output_named("output") # one value per scenario
optimize_design takes the objective and each constraint as functions of the
design variables. Here the design variable is the ozone dose in µmol/L, and
the largest dose is sought that keeps bromate at 600 s below 0.1 µmol/L:
def bromate_at_dose(x): # x = [ozone dose, µmol/L]
sol = reactor.solve(C0.at[i["O3"]].set(1e-6 * x[0]), t_span=(0.0, 600.0),
t_eval=t_obs, params=params)
return 1e6 * sol.C_named("BrO3-")[-1]
opt = aquakin.optimize_design(
objective=lambda x: x[0], maximize=True,
bounds=[(20.0, 200.0)], input_names=["O3_dose"],
constraints=[aquakin.Constraint(fn=bromate_at_dose, upper=0.1, name="BrO3-")],
x0=[50.0])
opt.x_named, opt.feasible # about 42.7 µmol/L, True
Differentiation options#
The tools above choose how to differentiate a solve for you. To change it,
pass an aquakin.DifferentiationConfig as diff= (to a reactor, inside its
settings=aquakin.SolverSettings(...)); it says how a gradient flows through a
solve. Its fields are:
mode:"reverse"(default;jax.grad, one pass for many parameters) or"forward"(jax.jacfwd, or the variational solve; finite through stiff models and suited to few parameters).method:"stable"(default) or"through_solve"."stable"replaces differentiation through the integrator with a cap-free discrete adjoint (reverse) or the variational solve (forward);"through_solve"differentiates the integrator’s own steps, which on a stiff model needs adtmaxcap in reverse mode.check_finite(defaultTrue): raise an error instead of returning a non-finite gradient.adjoint_max_steps(default 100 000) andadjoint_low_memory(defaultFalse): the step buffer of the stable reverse adjoint, and whether it recomputes each step in the backward pass instead of storing it, trading compute for memory on a long, large plant solve.
What each entry point does with it:
Entry point |
Default |
Notes |
|---|---|---|
|
through the solve |
a reactor has no cap-free adjoint for a bare solve; cap |
|
|
|
|
reverse |
|
|
|
the plant’s cap-free discrete adjoint: |