Metadata-Version: 2.4
Name: symbfitrs
Version: 0.1.3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: MacOS
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Rust
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Dist: numpy>=1.23
Requires-Dist: tqdm>=4.60
License-File: LICENSE
Summary: Fast nonlinear least-squares fitting on millisecond CPU budgets.
Keywords: curve fitting,nonlinear least squares,symbolic regression,levenberg-marquardt
Author: William Dennis
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Repository, https://github.com/wd7512/symbfitrs

# symbfitrs

A fast CPU fitter for nonlinear equations, written in Rust, for symbolic regression.

## Install

```bash
uv pip install symbfitrs  # Linux, macOS
```

## Usage

```python
import symbfitrs

# one fit
symbfitrs.compile("a*exp(-b*x) + c").fit(x, y, budget_ms=5)

# a batch, in parallel
symbfitrs.Pool(threads=8).fit_many([(expr, x, y), ...])

# many curves sharing one equation: shrunken fits under a learned population prior
eb = symbfitrs.empirical_bayes("a*exp(-k*x) + c", [(x1, y1), (x2, y2), ...])
eb.params, eb.mean, eb.cov
```

See [docs/empirical-bayes.md](docs/empirical-bayes.md) for how `empirical_bayes` works and how it scores.

## Benchmarks

Share of fits reaching each suite's loose (top) and strict (bottom) target against time per fit, one thread per fit:

<picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/wd7512/symbfitrs/main/docs/benchmark-dark.png"><img alt="Share of fits reaching the target against time per fit, one panel per suite at a loose and a strict target. symbfitrs leads every suite: at 1 ms it reaches 100% / 98% on NIST against 87% / 85% for native Ceres with autodiff, and 80% / 78% on the synthetic suite against 48% / 39%; symbfitrs Adam reaches 20-52% of loose targets at the full budget while PyTorch Adam, identical step for step but about 300 times slower per step, reaches almost none" src="https://raw.githubusercontent.com/wd7512/symbfitrs/main/docs/benchmark-light.png"></picture>

Fits in parallel (1 to 5 at once, 4 CPUs), and the speed-up clearing a queue of 5,133 fits:

<picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/wd7512/symbfitrs/main/docs/benchmark-threads-dark.png"><img alt="Share of symbfitrs fits reaching the target against time per fit with 1 to 5 fits running in parallel, mean of four suites: 72% at 1 ms, 93% at 5 ms and 99% at 25 ms with one at a time; 71%, 89% and 98.5% with five in parallel on four CPUs" src="https://raw.githubusercontent.com/wd7512/symbfitrs/main/docs/benchmark-threads-light.png"></picture>
<picture><source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/wd7512/symbfitrs/main/docs/benchmark-throughput-dark.png"><img alt="Speed-up in clearing a queue of 5,133 fits with 1 to 5 workers: 1.98x with 2 workers, 2.97x with 3, 3.90x with 4 and 4.19x with 5; wall time 139 s with one worker, 33 s with five" src="https://raw.githubusercontent.com/wd7512/symbfitrs/main/docs/benchmark-throughput-light.png"></picture>

### Method

- **Suites:** NIST StRD, Moré–Garbow–Hillstrom, FitBenchmarking, Stringer 2019, synthetic up to 99 parameters; SAGE-Fit when fetched.
- **Baselines at their best setting:** Ceres in C++ with automatic differentiation on the same compiled expressions, tolerances tightened to 1e-15 (its defaults stop at a relative change of 1e-6); iminuit with `tol=1e-6`; SciPy, GSL, lmfit and Optimistix at their defaults.
- **Line styles:** solid is native with exact derivatives; dashed is driven from Python with finite-difference Jacobians, except PyTorch, which uses autograd.
- **Adam:** both run at the usual defaults (lr 1e-3); symbfitrs's follows `torch.optim.Adam` step for step (`bench/adam_check.py`) at 2.6–180× less time per step.
- **Timing:** setup (compiling, building Ceres's problem, PyTorch's first step) is not timed for any solver.
- **Parallel charts:** rerun through the package on the four single-expression suites (not MGH); the queue uses a 100 ms budget per fit, and every fit that settles on its own gives the same loss at every worker count.
- **Version:** symbfitrs 0.1.1 plus unreleased commits (`v0.1.1-21-g42ecd87` to `v0.1.1-30-g6b27e84`). 0.1.2 adds one fitter change after that (`6d75eee`, same results with less work per fit), so the charts were not rerun for it.
- **Machine:** one sweep on an Intel Xeon at 2.3 GHz with 4 CPUs (single-thread runs pinned to one core), recorded in `docs/benchmark-manifest.json` with the commit and library versions.

More solvers and settings are in `results/<suite>_rust/summary.md`; see `bench/baselines.py`.

### Reproduce

Needs [uv](https://docs.astral.sh/uv/) and [cargo](https://rustup.rs/). SAGE-Fit data is Python code the benchmark executes, so fetch only data you trust.

```bash
uv sync && uv run python -m bench.fetch nist fitbenchmarking sagefit stringer2019  # 860 MB
for s in nist_strd mgh fitbenchmarking synthetic_scale sagefit_spb stringer2019_gratings; do
  uv run python -m bench run $s && uv run python -m bench run $s --fitter; done
uv run python -m bench.figure
uv run python -m bench.eb run && uv run python -m bench.eb figure  # empirical Bayes
```

For iterating on the fitter itself, `python -m bench.jobqueue` is a local queue of quick
`quick-micro` / `quick-package` runs: one worker executes timing jobs one at a time, so
parallel ideas cannot confound each other. See [docs/queue.md](docs/queue.md).

## Licence

MIT; downloaded benchmark data keeps its own.

