ode_filters fits ODE models to data and accounts for the numerical error of
the solver. It is a probabilistic ODE solver: it returns a Gaussian
distribution over the solution instead of a single trajectory, and the spread
of this distribution estimates the numerical error. The ODE enters as
information about the solution, in the same way as data. One square-root Kalman
filter therefore conditions on the ODE, on noisy measurements, on conservation
laws and on an unknown input that drives the system. The result is a likelihood
of the data that includes the numerical error and is differentiable in every
parameter of the model. The library is written in JAX, so fits can be
compiled, batched and differentiated.
import jax
import jax.numpy as np
from ode_filters import (
IWP,
InferenceProblem,
Measurement,
ODEInformation,
PositiveReal,
fit_scipy,
marginal_loglik,
taylor_mode_initialization,
)
jax.config.update("jax_enable_x64", True)
# Noisy observations of the logistic ODE x' = r x (1 - x), x(0) = 0.1
ts = np.linspace(0.0, 10.0, 101)
x = 1.0 / (1.0 + 9.0 * np.exp(-0.8 * ts[1:]))
y = x + 0.02 * jax.random.normal(jax.random.PRNGKey(0), x.shape)
prior = IWP(q=2, d=1)
data = [Measurement(np.eye(1), y[:, None], ts[1:], noise=0.02**2)]
# The ODE, as a function of the unknown rate r
def build(theta):
def vf(x, *, t):
return theta["r"] * x * (1.0 - x)
m_0, P_0_sqr = taylor_mode_initialization(vf, np.array([0.1]), q=2)
return m_0, P_0_sqr, ODEInformation(vf, prior.E0, prior.E1)
# Maximize the likelihood of the data, numerical error included
problem = InferenceProblem(build=build, prior=prior, ts=ts)
theta, _ = fit_scipy(
lambda th: -marginal_loglik(th, data, model=problem), {"r": PositiveReal(0.3)}
)
print(f"r = {theta['r'].unwrap():.3f} (true: 0.8)")
# r = 0.793 (true: 0.8)
For an introduction, read What is a probabilistic ODE solver?, then solve an ODE in the Quickstart and fit one in the Parameter estimation tutorial.
Where to go next¶
-
Fit a model to data
Estimate ODE parameters by maximum likelihood, check the fit, and sample the posterior.
-
Recover a hidden input
Infer the unknown force that drives an oscillator, with its uncertainty.
-
Understand the idea
What a probabilistic ODE solver computes, and what its uncertainty is used for.
-
Solve an ODE
Solve the logistic ODE end to end, smooth it, and plot the posterior.
-
Pick the components
Prior, order, linearization, step size, calibration and the inference tools, and their defaults.
-
Avoid the pitfalls
Float64,
jitand pytrees, data on the grid, and other common errors. -
Browse the API
Every public function and class, grouped by subpackage.
Features¶
- Inference from data: a differentiable marginal likelihood of the data for
any parameter of the ODE or the prior;
fit_scipy(L-BFGS-B) andfit(Optax) for point estimates, NumPyro and BlackJAX for posteriors. - Hidden inputs: joint state–input priors recover an unknown force that drives the system, with a consistent initialization and information criteria for what the data say about it.
- Evidence for hyperparameters: the sequential data evidence and the conditional evidence of the data given the ODE (Fenrir, Dalton).
- Outputs: smoothed marginals, joint posterior samples, dense output between the grid nodes, the predictive distribution of observations, and innovation diagnostics (NIS, whitened residuals) to check a fit.
- The ODE as information: first- and second-order ODEs, conservation laws, black-box and transformed operators, and data at any time, all in one filter.
- Pluggable linearization: EK0, EK1, iterated (IEKF) and statistical linearization, plus the trajectory-level IEKS and IPLS.
- Priors: integrated Wiener (IWP), Matérn, integrated Ornstein–Uhlenbeck (probabilistic exponential integrators) and joint priors, plain or preconditioned.
- Calibrated uncertainty: online and post-hoc diffusion calibration, fixed grids (any spacing) and adaptive steps.
- Pure JAX, square-root form:
jit,vmapandgradthrough every solver; numerically stable QR-based filtering and smoothing (Krämer & Hennig 2024).
Installation¶
From source, with the development tools (tests, linting, type checking):
With pip 25.1 or newer, pip install -e . --group dev does the same.