aquakin.fit#

aquakin.fit(reactor, C0, observations, t_obs, free_params, *, method='adjoint', initial_params=None, observed_species=None, time_unit=None)[source]#

Least-squares fit of selected parameters to time-series observations.

This is the lightweight fitter: box-constrained sum-of-squares least squares on a single batch, with no parameter transforms, priors, Laplace posterior, free initial conditions, or multi-batch support. For anything beyond a quick point estimate, prefer aquakin.calibrate() (use calibrate(..., laplace=False) for a bare point fit) — it is a strict superset of this function.

Note

The reported losses are not comparable between the two. fit reports FitResult.loss as the sum of squared residuals (SciPy least_squares cost), whereas calibrate with loss="mse" reports the mean squared error. The optima coincide (a positive constant factor does not move the minimiser); only the absolute loss value differs in scale.

Parameters:
  • reactor (BatchReactor) – The reactor to integrate. (Only batch reactors are supported here; the PFR case can be wrapped analogously.)

  • C0 (jnp.ndarray) – Initial concentration vector.

  • observations (jnp.ndarray) – Observed values. Shape (n_t, n_observed) or (n_t,).

  • t_obs (jnp.ndarray) – Observation times, shape (n_t,). C0 is taken to be the state at t = 0; integration runs from 0 to t_obs[-1] and the solution is sampled at t_obs. In the model’s native time unit unless time_unit is given.

  • free_params (list[str]) – Namespaced parameter names to optimise. Other parameters are held at their default (or initial_params) values.

  • method (str) – Currently only "adjoint" is supported, which uses Diffrax’s recursive-checkpoint adjoint via jax.grad() and SciPy L-BFGS-B.

  • initial_params (jnp.ndarray, optional) – Starting parameter vector. Defaults to reactor.model.default_parameters().

  • observed_species (list[str], optional) – Species names corresponding to columns of observations. If None, observations is assumed to be over all species in model order.

  • time_unit (str, optional) – The time unit t_obs is expressed in ("s", "min", "h", "d"), matching BatchReactor.solve(). t_obs is converted into the model’s native (rate-constant) time unit before the solve, so a user who standardises on e.g. hours can pass the same hour-valued t_obs here as to solve. Default None interprets t_obs in the native unit. The fitted rate constants are always in native units.

Return type:

FitResult

Notes

Box bounds are applied per parameter from each parameter’s declared bounds. A free parameter without declared bounds is left unbounded ((-inf, +inf)) while the others keep their boxes; a warning is emitted in that mixed case. If no free parameter has bounds, the solve is fully unconstrained.