# MxlPy — Global sensitivity analysis

[← back to index](llms.txt)

`mxlpy.sensitivity` ranks and quantifies how much each parameter drives a chosen model output. `SALib` is a core dependency (no extra install). Workflow: **screen** cheaply with Morris, then **quantify** the survivors with Sobol.

## The `output` and `param_bounds` contract (shared by both)

- `output` — a callable `(model, samples: pd.DataFrame) -> np.ndarray` that maps the whole sample matrix (one row per sample, columns = parameter names) to **one scalar per row**. There are no built-in output helpers: build the reduction on `mxlpy.scan`, which runs samples in parallel, caches, and returns `NaN` on integration failure (the `NaN` then propagates into the indices).
- `param_bounds` — `dict[str, tuple[float, float]]` of `(lower, upper)` sampling bounds per parameter.
- `seed` — for reproducible sampling and bootstrap.

```python
from mxlpy import scan, sensitivity
import numpy as np

# Steady-state value of a species
def steady_state_x(model, samples):
    return scan.steady_state(model, to_scan=samples).variables["x"].to_numpy()

# Peak over a time course
def max_x(model, samples):
    return scan.time_course(model, to_scan=samples, time_points=time_points).get_agg_per_run("max")["x"].to_numpy()

# Area under the curve (pass np.trapezoid as the aggregator)
def auc_x(model, samples):
    return scan.time_course(model, to_scan=samples, time_points=time_points).get_agg_per_run(np.trapezoid)["x"].to_numpy()
```

## Morris screening

`sensitivity.morris(model, *, output, param_bounds, n_trajectories=10, num_levels=4, seed=None) -> pd.DataFrame`

Elementary-effects screening: which parameters matter at all, at only `n_trajectories * (k + 1)` evaluations for `k` parameters. Result is indexed by parameter with columns:

- `mu` — mean elementary effect
- `mu_star` — mean **absolute** effect; the importance ranking
- `sigma` — std of effects (nonlinearity / interaction)
- `mu_star_conf` — bootstrap CI

Reading: low `mu_star` = unimportant; high `mu_star` + low `sigma` = important & roughly linear; high `mu_star` + high `sigma` = important & nonlinear/interacting.

```python
result = sensitivity.morris(
    model,
    output=steady_state_x,
    param_bounds={"k1": (0.5, 2.0), "k2": (0.5, 2.0), "k_out": (0.5, 2.0)},
    n_trajectories=20,
    seed=0,
)
result.sort_values("mu_star", ascending=False)
```

## Sobol variance decomposition

`sensitivity.sobol(model, *, output, param_bounds, n_samples=1024, seed=None) -> pd.DataFrame`

Once Morris has screened out the noise, Sobol quantifies *how much* of the output variance each survivor explains. Cost is `n_samples * (k + 2)` evaluations, so it is a deliberate second pass. `n_samples` **must be a power of two** (`ValueError` otherwise). Result is indexed by parameter with columns:

- `S1` — first-order index: variance explained by the parameter **alone**
- `ST` — total-order index: variance explained **including all interactions**
- `S1_conf`, `ST_conf` — bootstrap CIs

Reading: high `S1` = direct driver; large `ST − S1` gap = acts mainly through interactions; `ST ≈ 0` = negligible.

```python
result = sensitivity.sobol(
    model,
    output=steady_state_x,
    param_bounds={"k1": (0.5, 2.0), "k2": (0.5, 2.0)},
    n_samples=1024,   # power of two; total evals = n_samples * (k + 2)
    seed=0,
)
result.sort_values("ST", ascending=False)
```

## Local (forward) sensitivities

`mxlpy.forward_sensitivities` (`mxlpy.sensitivity` is the global analysis above) computes the derivatives `d y(t) / d theta` of every variable, derived quantity and reaction w.r.t. parameters and variable initial values. Forward sensitivities solve `dS/dt = J S + df/dtheta` alongside the model, with `J` and `df/dtheta` generated symbolically (needs sympy-convertible rate functions); steady states use the implicit function theorem (conserved totals are fixed by the initial values). Models that can't be differentiated symbolically (NN surrogates, unparsable functions) fall back to central finite differences with a warning. Works for `Model`/`KineticModelBuilder` and `OdeModelBuilder`.

```python
from mxlpy import forward_sensitivities

sens = forward_sensitivities.time_course(model, time_points, ["k1", "k2", "x"]).unwrap_or_err()
sens.simulated                 # DataFrame: time x (variables, derived, reactions)
sens.derivatives["k1"]         # same shape: d simulated / d k1
sens.method                    # "forward" | "finite_difference"
sens.to_array()                # (time, column, parameter)

forward_sensitivities.steady_state(model, ["k1"])   # single row

# explicit initial values / output subset / method
forward_sensitivities.time_course(model, time_points, ["k1"], y0={...}, outputs=["v1"], method="auto")
forward_sensitivities.steady_state(model, ["k1"], method="finite_difference", displacement=1e-3)

# runners provide the same, with their integrator / steady-state settings
KineticRunner(derivatives="auto", displacement=1e-3).time_course_sensitivities(model, time_points, ["k1"])
KineticRunner().steady_state_sensitivities(model, ["k1"])
```

- Variable names in `wrt` give sensitivities w.r.t. their initial values. Derived parameters / initial-assignment parameters are rejected (differentiate w.r.t. the parameters they're computed from).
- Without `y0`, initial values come from the model, so parameter-dependent initial assignments are differentiated too.
- `method="forward"` raises if symbolic differentiation isn't possible; `displacement` (default `1e-3`) is the relative finite-difference step — smaller steps amplify integrator noise.
- Integrators compiling the model themselves (`compiles_model = True`: `Native`, `Diffrax`) always use finite differences (use (discrete) adjoint gradients with `Native` for loss gradients).
- Returns `Result[ForwardSensitivities]`; integration failures are values, call `.unwrap_or_err()`.
- Generated code is cached per model structure: parameter/variable value updates reuse it, structural changes (adding/removing/redefining components, initial assignments) regenerate it. Copies and pickled models share it.
- `mca.response_coefficients` and gradient-based fits use the runner's sensitivities.
- `Simulator(model, use_jacobian=True)` now also compiles the analytic Jacobian for `OdeModelBuilder`.

## Adjoint gradients (vector-Jacobian products)

`mxlpy.adjoint_sensitivities` computes `d/dtheta sum(w(t) * y(t))` for given cotangents `w` (e.g. `d loss / d y`), i.e. the gradient of a scalar, by one forward and one backward pass regardless of the number of parameters (forward sensitivities integrate n per parameter). Two kinds:
- Discrete adjoint (`Native`, all methods Radau/LSODA/CVODE_BDF/CVODE_Adams, llvm or wasm): the forward solve records every accepted step on a tape in C (mxl-solvers discrete_adjoint.c: RADAU5 stages; LSODA/CVODE's operations on their Nordsieck array via hooks), the backward pass differentiates those steps: step sizes frozen (as diffrax), Newton/corrector by the implicit function theorem, dense output exact. Gradient = exact gradient of the computed solution ("discretise then optimise"), agrees with the loss a line search sees (C tests: <= 1e-7 rel. vs central differences of the frozen-step replay). The recording tightens the solver's Newton tolerance x1e-3 (`_discrete.NEWTON_SCALE`). Tape memory bounded by `Native(max_tape_bytes=...)` (default 1 GiB): beyond, `discrete_adjoint` raises `adjoint_sensitivities.TapeTooLarge`, `adjoint`/`auto` use the continuous adjoint (also if the taped solve fails). Not included: derivatives through step-size selection (adaptive-step central differences differ by O(tol)).
- Continuous adjoint (any integrator): `dlambda/dt = -J^T lambda`, jumps by `w` at every time point, `dL/dtheta = int lambda^T df/dtheta dt + lambda(0)^T dx0/dtheta`; both products generated symbolically in reverse mode. The backward pass Hermite-interpolates the forward trajectory (grids refined by an ODE defect test), never integrates the model backwards. Gradient of the exact solution: off by O(tol) from the computed one. Compiled for `Native` Radau/LSODA (`mxn_adjoint_vjp`, only with `method="continuous_adjoint"`).
- Steady states: adjoint of the implicit function theorem (one linear solve, conserved totals handled), `method == "adjoint"`.

Pullback API (as `jax.vjp`): simulate once, evaluate a loss on `simulated`, then call with cotangents; reusable, no new simulation.

```python
from mxlpy import adjoint_sensitivities

pb = adjoint_sensitivities.time_course_pullback(model, [1.0, 2.0], ["k1", "x"], columns=["x", "v1"], integrator=Native).unwrap_or_err()
pb.simulated      # time x columns (from the same solve the gradient differentiates)
pb.method         # "discrete_adjoint" | "continuous_adjoint" | "forward" | "finite_difference"
res = pb(w).unwrap_or_err()   # w: DataFrame (times subset of time points, columns subset), missing = 0
res.gradient      # Series over wrt
res.adjoint       # lambda(t) (Python continuous), only lambda(0) for compiled adjoints
adjoint_sensitivities.protocol_time_course_pullback(model, protocol, times, wrt)  # steps set parameters; wrt can't be protocol columns
adjoint_sensitivities.steady_state_pullback(model, wrt, columns=["A"])          # pullback over a Series
# one-shot: simulate at w's index/columns and call once
adjoint_sensitivities.time_course(model, w, ["k1", "k2", "x"]).unwrap_or_err()
adjoint_sensitivities.steady_state(model, pd.Series({"A": 1.0}), ["kf"])
adjoint_sensitivities.protocol_time_course(model, protocol, w, ["k1"])
# method: "auto" (default) | "adjoint" (discrete where possible) | "discrete_adjoint" | "continuous_adjoint" | "forward" | "finite_difference"
adjoint_sensitivities.uses_adjoint(model, n_wrt, integrator)  # auto's choice
KineticRunner(derivatives="adjoint").time_course_pullback(model, times, ["k1"], columns=columns)   # also protocol_time_course_pullback, steady_state_pullback
```

- `auto` uses adjoints for time courses from `ADJOINT_THRESHOLD` (1000) variables x parameters on (measured crossover), and always with `Native` (discrete adjoint; Native can't integrate forward sensitivities); steady states always use the adjoint IFT. Order tried: discrete (Native) -> continuous -> forward/finite differences (only for `auto`). Cotangents of continuous adjoints are scaled to max |w| = 1 internally (lambda shares the states' atol), NaN cotangents mean no weight.
- `discrete_adjoint` without `Native` raises; models Native can't compile (arrays/data, events, NaN Jacobians) fall back to the continuous adjoint under `adjoint`/`auto`. Native compiles the model's Jacobian for the discrete backward pass (checked against central differences).
- Protocols: adjoints run backwards through the steps (each step's parameters); time points after the protocol are left out of `simulated`, cotangents there ignored. No forward sensitivities for protocols, so `method="forward"` raises and `auto` uses adjoints from `PROTOCOL_ADJOINT_THRESHOLD` (3) parameters, finite differences below. Parameters set by initial assignments depending on protocol columns: adjoints raise, auto uses finite differences.
- Models with events: adjoint methods raise, `auto` uses forward/finite differences. Unparsable functions: adjoints raise, `auto` falls back.
- Initial assignments (parameters, initial values) are differentiated through constant chain-rule factors, as for forward sensitivities.

