Metadata-Version: 2.4
Name: StochasticForceInference
Version: 2.1.0
Summary: Learn stochastic differential equations (Langevin dynamics: drift/force and diffusion) from time series and trajectory data
Author-email: Pierre Ronceray <pierre.ronceray@univ-amu.fr>
License: MIT
Project-URL: Homepage, https://github.com/ronceray/StochasticForceInference
Project-URL: Documentation, https://stochasticforceinference.readthedocs.io
Project-URL: Gallery, https://stochasticforceinference.readthedocs.io/en/stable/gallery/index.html
Project-URL: Guide for AI agents, https://github.com/ronceray/StochasticForceInference/blob/main/SFI/AGENT_GUIDE.md
Project-URL: Repository, https://github.com/ronceray/StochasticForceInference
Project-URL: Issues, https://github.com/ronceray/StochasticForceInference/issues
Project-URL: Changelog, https://github.com/ronceray/StochasticForceInference/blob/main/CHANGELOG.md
Keywords: stochastic force inference,Langevin dynamics,stochastic differential equations,SDE,diffusion,inference,drift estimation,diffusion estimation,Kramers-Moyal,Fokker-Planck,time series,trajectory analysis,system identification,equation discovery,sparse model selection,particle tracking,single-particle tracking,cell migration,population dynamics,gene expression,entropy production,Ornstein-Uhlenbeck,active matter,biophysics,JAX
Classifier: Development Status :: 5 - Production/Stable
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Scientific/Engineering :: Physics
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: Topic :: Scientific/Engineering :: Information Analysis
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy
Requires-Dist: scipy
Requires-Dist: jax!=0.10.2,!=0.11.0,>=0.10.1
Requires-Dist: jaxlib!=0.10.2,!=0.11.0,>=0.10.1
Requires-Dist: pandas
Requires-Dist: matplotlib
Requires-Dist: pyyaml
Requires-Dist: equinox>=0.12
Requires-Dist: opt-einsum
Provides-Extra: docs
Requires-Dist: sphinx; extra == "docs"
Requires-Dist: furo; extra == "docs"
Requires-Dist: sphinx-autodoc-typehints; extra == "docs"
Requires-Dist: myst-parser; extra == "docs"
Requires-Dist: sphinx-gallery; extra == "docs"
Requires-Dist: sphinxcontrib-video; extra == "docs"
Requires-Dist: sphinxcontrib-mermaid; extra == "docs"
Provides-Extra: dev
Requires-Dist: pytest; extra == "dev"
Requires-Dist: pytest-xdist; extra == "dev"
Requires-Dist: ruff; extra == "dev"
Requires-Dist: pyright; extra == "dev"
Requires-Dist: StochasticForceInference[docs]; extra == "dev"
Provides-Extra: io
Requires-Dist: pyarrow>=14; extra == "io"
Requires-Dist: h5py>=3.9; extra == "io"
Requires-Dist: pandas>=2.0; extra == "io"
Requires-Dist: PyYAML>=6.0; extra == "io"
Dynamic: license-file

# Stochastic Force Inference (SFI)

[![Documentation](https://readthedocs.org/projects/stochasticforceinference/badge/?version=latest)](https://stochasticforceinference.readthedocs.io/en/latest/)
[![PyPI](https://img.shields.io/pypi/v/StochasticForceInference.svg)](https://pypi.org/project/StochasticForceInference/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://github.com/ronceray/StochasticForceInference/blob/main/LICENSE)
[![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.21532842.svg)](https://doi.org/10.5281/zenodo.21532842)

**Learn the stochastic equation behind your time series.**

SFI is a JAX-based Python package that infers the drift (force) and
diffusion of Langevin stochastic differential equations from trajectories
and other continuous time series: tracked particles, cells and animals,
population abundances, gene-expression and other molecular levels,
concentration fields. It returns an interpretable
equation with error bars, selects the terms the data supports, and
simulates the fitted model. It handles overdamped and underdamped
dynamics, interacting particles and spatially extended (SPDE) systems.

## Is SFI for my data?

| You want to… | SFI does it with |
| --- | --- |
| estimate drift and diffusion (first and second Kramers–Moyal coefficients, a Fokker–Planck model) | `OverdampedLangevinInference`: `infer_force_linear`, `infer_diffusion_linear` |
| discover the equation of a noisy stochastic system (sparse regression, "stochastic SINDy") | a candidate basis + `sparsify_force(criterion="PASTIS")` |
| map forces and diffusivity from single-particle tracking, with localization error | the overdamped engine; the defaults correct white localization noise |
| model cell migration, animal movement or active particles (persistent velocities) | `UnderdampedLangevinInference`, from positions alone |
| measure interaction forces between particles, collective motion | pair bases (`SFI.bases.pairs`) |
| model population dynamics: microbial or ecological abundances, growth rates, species interactions | the overdamped engine on log-abundances + `sparsify_force` ([Lotka–Volterra example](https://stochasticforceinference.readthedocs.io/en/stable/gallery/lotka_volterra_demo.html)) |
| model gene-expression, protein or metabolite levels, whose noise scales with the level | the overdamped engine with a diffusion field `D(x)` (`infer_diffusion_linear`) |
| fit noisy oscillators and chaotic signals (genetic or circadian clocks, hair bundles) | a nonlinear basis; `UnderdampedLangevinInference` when one coordinate of an inertial oscillator is observed |
| quantify irreversibility: entropy production, broken detailed balance | `compute_entropy_production()` |
| decide whether the dynamics are overdamped or underdamped | `SFI.classify_dynamics` |

SFI is not the right tool for categorical data, small counts or events, for
forecasting without an equation, for snapshots without time correlation,
or for processes with jumps. More routes:
[Start here](https://stochasticforceinference.readthedocs.io/en/stable/start_here.html).

> **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; the defaults
  correct localization noise and the leading finite-time-step bias) 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.
- **Entropy production** — dissipation rate with error bar, for
  overdamped and underdamped dynamics.
- **Simulation** — simulate Langevin SDEs from the same model objects
  used for inference, including bootstrapped trajectories of a fitted model.
- **Trajectory toolkit** — mask-aware increments, synthetic degradation
  (noise, downsampling, data loss, motion blur), streaming for large
  datasets, I/O (CSV / Parquet / HDF5, tracking tables).
- **Diagnostics** — residual tests with action hints, an
  overdamped-vs-underdamped classifier, predicted errors, comparison to
  exact models.

## Installation

```bash
pip install StochasticForceInference      # import name: SFI
```

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

```bash
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:

```python
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.infer_force_linear(B)               # also estimates D and the measurement noise
inf.sparsify_force(criterion="PASTIS")  # optional: find the minimal model
inf.compute_force_error()
inf.print_report()                      # coefficients with error bars
inf.diagnose().print_summary()          # residual tests, with hints if one fails

# Coarsely sampled or very noisy recordings? Use the parametric estimator
# instead: inf.infer_force(B) — it models the measurement noise natively.
```

For a **synthetic example** (useful for validation):

```python
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.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)
```

## Using SFI with an AI agent

SFI ships a workflow guide for AI coding agents (Claude Code, Codex,
Cursor, …) inside the package, matched to the installed version:

```bash
python -m SFI guide            # Markdown: workflow, decisions, pitfalls, API names

# Install it as an Agent Skill (path shown for Claude Code; other tools that support skills use their own folder)
mkdir -p ~/.claude/skills/stochastic-force-inference
python -m SFI guide --skill > ~/.claude/skills/stochastic-force-inference/SKILL.md
```

The documentation has an [`llms.txt`](https://stochasticforceinference.readthedocs.io/llms.txt)
index. Agents that modify SFI itself should read
[`AGENTS.md`](https://github.com/ronceray/StochasticForceInference/blob/main/AGENTS.md).

**Coming from the original SFI scripts** (`StochasticForceInference.py`,
`StochasticTrajectoryData`, or the separate UnderdampedLangevinInference
package)? SFI 2 is a rewrite with a new API; the
[guide](https://github.com/ronceray/StochasticForceInference/blob/main/SFI/AGENT_GUIDE.md#9-coming-from-the-original-sfi-scripts-or-older-example-code)
maps the old calls to the new ones.

## Documentation

Full documentation is available at
[stochasticforceinference.readthedocs.io](https://stochasticforceinference.readthedocs.io), including:

- **[Start here](https://stochasticforceinference.readthedocs.io/en/latest/start_here.html)** — is SFI right for my data, and which route to take
- **[Tutorial](https://stochasticforceinference.readthedocs.io/en/latest/gallery/ou_demo.html)** — end-to-end walkthrough
- **[Examples gallery](https://stochasticforceinference.readthedocs.io/en/latest/gallery/index.html)** — worked examples covering sparsity, multi-particle, and SPDE inference
- **[Model building](https://stochasticforceinference.readthedocs.io/en/latest/statefunc/user_guide.html)** — composable state functions
- **[Trajectory handling](https://stochasticforceinference.readthedocs.io/en/latest/trajectory/user_guide.html)** — data ingestion, masking, degradation
- **[Running inference](https://stochasticforceinference.readthedocs.io/en/latest/inference/user_guide.html)** — estimator choice, overdamped/underdamped engines
- **[API reference](https://stochasticforceinference.readthedocs.io/en/latest/api_frontend.html)** — full autodoc

## 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, pairs, SPDE, …) |
| `SFI.diagnostics` | Residual tests, dynamics classifier, error checks     |
| `SFI.integrate`   | Time-averaging integration engine                    |
| `SFI.utils`       | Math helpers, formatting, plotting                   |

## Citation

If you use SFI, please cite the software (see [`CITATION.cff`](https://github.com/ronceray/StochasticForceInference/blob/main/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](https://doi.org/10.5281/zenodo.21532842), which always
resolves to the latest release; each version also has its own DOI (v2.0.4:
[10.5281/zenodo.23498743](https://doi.org/10.5281/zenodo.23498743)).

See **[How to cite](https://stochasticforceinference.readthedocs.io/en/latest/#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](https://github.com/ronceray/StochasticForceInference/blob/main/LICENSE) for details.
