Skip to main content

Stochastic Force Inference (SFI)

Documentation PyPI License: MIT DOI

Infer force and diffusion fields from stochastic trajectory data.

SFI is a JAX-based Python package for learning the drift (force) and diffusion of Langevin stochastic differential equations from time-series observations. It handles both overdamped and underdamped dynamics, supports interacting particles and spatially-extended (SPDE) systems, and provides built-in diagnostics, sparse model selection, and bootstrapped validation.

Designed for experimental data. SFI is built for real experimental trajectories (tracked particles, cells, organisms, …) where no dynamical model pre-exists. Two first-class estimator families share one API: fast closed-form linear estimators that require no initial guess, and parametric estimators that model measurement noise and finite sampling explicitly — robust where real data is hard. The PASTIS information criterion rigorously identifies which terms the data actually supports. Synthetic examples in the gallery serve as runnable demonstrations.

Key features

  • Two estimator families — closed-form linear estimators (infer_force_linear, seconds even on large datasets) and parametric estimators (infer_force / infer_diffusion: noise-aware likelihood fit, robust to localization error and coarse sampling, accepts nonlinear models such as neural-network drifts).
  • Overdamped & underdamped inference — works with position-only data; velocities are reconstructed automatically for underdamped systems.
  • Composable state functions — build force/diffusion models from monomials, custom basis functions, pair interactions, or arbitrary parametric families, all with automatic differentiation via JAX.
  • Sparse model selection — Pareto-front beam search with AIC / BIC / PASTIS information criteria.
  • Simulation — simulate Langevin SDEs (Euler–Maruyama) from the same model objects used for inference.
  • Trajectory toolkit — mask-aware increments, synthetic degradation (noise, downsampling, data loss, motion blur), streaming for large datasets, I/O (CSV / Parquet / HDF5).
  • Diagnostics — compare inferred fields to exact models, compute normalized errors, generate bootstrapped trajectories.

Installation

pip install StochasticForceInference

For development (editable install with test/doc dependencies):

git clone https://github.com/ronceray/StochasticForceInference.git
cd StochasticForceInference
pip install -e ".[dev,io]"

Note: SFI requires Python ≥ 3.11 and JAX ≥ 0.10.

Quick start

For experimental data, load your trajectories and infer directly:

import numpy as np
from SFI import OverdampedLangevinInference, TrajectoryCollection
from SFI.bases import monomials_up_to

# Load your tracked data (positions: T×d array, dt: time step)
positions = np.load("my_experiment.npz")["positions"]  # shape (T, d)
coll = TrajectoryCollection.from_arrays(X=positions, dt=0.01)

# Infer using a polynomial basis — no model needed
B = monomials_up_to(order=3, dim=2, rank="vector")
inf = OverdampedLangevinInference(coll)
inf.compute_diffusion_constant()
inf.infer_force_linear(B)
inf.sparsify_force(criterion="PASTIS")  # optional: find the minimal model
inf.print_report()

# Noisy or coarsely-sampled recordings? Use the parametric estimator
# instead: inf.infer_force(B) — it models the measurement noise natively.

For a synthetic example (useful for validation):

import jax.numpy as jnp
from jax import random

from SFI import OverdampedLangevinInference
from SFI.bases import X, monomials_up_to
from SFI.langevin import OverdampedProcess

# 1. Build a force model (Ornstein–Uhlenbeck: F = -k x) and simulate a trajectory.
#    X(dim) is the identity basis x ↦ x; theta_F holds its coefficient, so F(x) = -x.
proc = OverdampedProcess(F=X(dim=2), D=jnp.eye(2) * 0.5, theta_F=jnp.array([-1.0]))
proc.initialize(jnp.zeros(2))
coll = proc.simulate(dt=0.01, Nsteps=10_000, key=random.PRNGKey(0))

# 2. Infer from the trajectory using a generic polynomial basis
B = monomials_up_to(order=2, dim=2, rank="vector")
inf = OverdampedLangevinInference(coll)
inf.compute_diffusion_constant()
inf.infer_force_linear(B)
inf.compute_force_error()

# 3. Inspect results and validate against the known model
inf.print_report()
inf.compare_to_exact(model_exact=proc)

Documentation

Full documentation is available at stochasticforceinference.readthedocs.io, including:

Package structure

Subpackage Purpose
SFI.statefunc Composable state functions: Basis, PSF, SF
SFI.inference Overdamped & underdamped inference engines
SFI.trajectory TrajectoryCollection / TrajectoryDataset
SFI.langevin Langevin simulators (OverdampedProcess, …)
SFI.bases Ready-made basis builders (monomials, constants, …)
SFI.integrate Time-averaging integration engine
SFI.utils Math helpers, formatting, plotting

For contributors and AI coding agents: see AGENTS.md for the canonical imports table, task playbooks, and "do not re-implement" guidance.

Citation

If you use SFI, please cite the software (see CITATION.cff) together with the method paper(s) relevant to the features you use.

The software is archived on Zenodo. Cite the concept DOI 10.5281/zenodo.21532842, which always resolves to the latest release; each version also has its own DOI (v2.0.3: 10.5281/zenodo.21532843).

See How to cite in the documentation for the full, feature-specific list (overdamped, underdamped/ULI, trapeze, and PASTIS references).

Funding

Funded by the European Union (ERC, SuperStoc, 101117322). Views and opinions expressed are however those of the author(s) only and do not necessarily reflect those of the European Union or the European Research Council Executive Agency. Neither the European Union nor the granting authority can be held responsible for them. P. R. also acknowledges funding from the French National Research Agency (ANR-16-CONV-0001) and from the Excellence Initiative of Aix-Marseille University A*MIDEX.

License

MIT — see LICENSE for details.

Metadata

Release files for StochasticForceInference 2.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for StochasticForceInference 2.0.4
File Size Uploaded Provenance Actions
stochasticforceinference-2.0.4.tar.gz 704.3 kB Without provenance Details for stochasticforceinference-2.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for StochasticForceInference 2.0.4
File Interpreter ABI Platform Actions
stochasticforceinference-2.0.4-py3-none-any.whl Python 3 none any Details for stochasticforceinference-2.0.4-py3-none-any.whl

Total release size: 1.3 MB

Release files / stochasticforceinference-2.0.4.tar.gz

Download URL stochasticforceinference-2.0.4.tar.gz
Size 704.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5e67dd4898e05835f192c68bf5f918dd37a05b5640483689685b2687a88330af
BLAKE2b-256 checksum
How to use checksums
7d43ddaae1a2efc0c5b006ba49364e2cf4753bdca4fe036ad6bf3236f079c087
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release files / stochasticforceinference-2.0.4-py3-none-any.whl

Download URL stochasticforceinference-2.0.4-py3-none-any.whl
Size 554.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
43696a9463c572a6dc83d387d74066ca8c6249b357c5e8619c4751ac8cfb2f9d
BLAKE2b-256 checksum
How to use checksums
14610a416cc964f877c895cdc9e5990ac899723b10ac02801f9efb0b3021ad3e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Security for StochasticForceInference 2.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page