BayFit
Bayesian inference-based curve fitting for generic x/y data.
BayFit is a Python library for Bayesian inference on generic x/y curve
data. It pairs MCMC and nested-sampling backends with multi-dataset,
multi-model fitting machinery, ships a small library of analytic model
components, and reads data from dictionaries, DataFrames, CSV, JSON, and
NumPy .npz archives out of the box.
It is the curve-fitting sibling of
BaySpec: the same inference and
model-composition design, generalised from high-energy spectra to
arbitrary x/y curves.
Features
- Inference backends. Posterior sampling via MCMC
(
emcee) or nested sampling (MultiNestviapymultinest); maximum-likelihood fits vialmfitoriminuitfor quick checks. - Multi-dataset, multi-model. Simultaneously fit any number of
(data, model)pairs; freeze or link parameters across pairs. - Multi-dimensional inputs. Data carry an
(npoint, ndim)design matrix, so models can depend on more than one independent variable. - Model algebra. Combine components with
+,-,*,/to build composite models from additive, multiplicative, and mathematical building blocks. - Local model library. Linear, power-law, exponential decay,
single/double/triple smoothly broken power-laws, magnetar spin-down,
and power-spectral-density models, plus an exponential-cutoff factor
and a constant — discover them with
list_local_models(). - Flexible data loaders. Build a
DataUnitfrom a dict, apandasDataFrame, a CSV, a JSON file, or a NumPy.npzarchive, with symmetric or asymmetric x/y errors and upper/lower limits. - Plotting. Plotly and Matplotlib helpers for data, models, fit results, and posterior corner plots.
Installation
BayFit is available on PyPI:
pip install bayfit
Optional: MultiNest sampler
To enable MultiNest for
nested sampling, follow the
pymultinest install
guide (it requires the MultiNest C library in addition to the Python
package).
Quick start
Fit a straight line — recovering its slope, intercept, and intrinsic scatter — to noisy data with MCMC:
import numpy as np
from bayfit.data.data import Data, DataUnit
from bayfit.infer.infer import BayesInfer
from bayfit.model.local import line
from bayfit.util.plot import Plot
# synthetic data: y = 2x + 1 with measurement error *and* intrinsic scatter
rng = np.random.default_rng(1)
x = np.linspace(0, 10, 40)
yerr = np.full(x.size, 0.3) # measurement error
sigma_int = 1.0 # intrinsic scatter (logv_true = log10(1) = 0)
y = 2.0 * x + 1.0 + rng.normal(0, np.hypot(yerr, sigma_int))
# one data unit -> a Data container; 'chi2f' fits an extra variance term
unit = DataUnit(x, y, yerr=yerr, stat='chi2f')
data = Data([('d', unit)])
# a linear model; slope k, intercept b, and log intrinsic scatter logv are all free
model = line()
# pair data with the model and sample the posterior
infer = BayesInfer([(data, model)])
post = infer.multinest(nlive=400, resume=False, savepath='./out')
k, b, logv = post.par_best_ci[:3]
# recovers the slope, intercept, and intrinsic scatter sigma_int = 10**logv
print(f'k = {k:.2f}, b = {b:.2f}, sigma_int = {10 ** logv:.2f}') # -> ~2.0, ~1.0, ~1.0
# inspect the fit and the posterior (auto-display inline in notebooks)
Plot.infer(post) # data, best-fit model, and residuals
Plot.post_corner(post) # parameter corner plot
Both Plot.* calls return a Figure that displays inline in notebooks.
From a script, choose a file-writing backend and save a PDF:
fig = Plot.infer(post, ploter='matplotlib')
fig.save('./out/fit')
fig = Plot.post_corner(post, ploter='cornerpy')
fig.save('./out/corner')
Swap line() for any component returned by list_local_models(), or
compose several with +/*, to fit other curves. Load real data with
DataUnit.from_csv, from_json, from_dataframe, or from_npz.
License
BayFit is distributed under the
GPL-3.0 license.
Metadata
Release files for bayfit 0.1.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| bayfit-0.1.3.tar.gz | 75.4 kB | Without provenance | Details for bayfit-0.1.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| bayfit-0.1.3-py3-none-any.whl | Python 3 | none | any | Details for bayfit-0.1.3-py3-none-any.whl |
Total release size: 155.4 kB
Release files / bayfit-0.1.3.tar.gz
| Download URL | bayfit-0.1.3.tar.gz |
|---|---|
| Size | 75.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
db91dbb75972e3952d8126ca0c2e502e9fd925e6a291420a85ce5c70eca74b34
|
|
BLAKE2b-256 checksum How to use checksums |
3a4f1c20e00be7c586d419984ba8bcf37bbc509becc98b9c31d404a66b47bd98
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Release files / bayfit-0.1.3-py3-none-any.whl
| Download URL | bayfit-0.1.3-py3-none-any.whl |
|---|---|
| Size | 80.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
64c55542c0bcc3982b0d36c4fc2a179fdac82c71050ecc33206f332c993fbd9d
|
|
BLAKE2b-256 checksum How to use checksums |
7c1026b9e6e7370b67fe302ad796e3992a54536bc56cfd6aaf9c245e5d6fde45
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.1.0 CPython/3.12.8
|
Security for bayfit 0.1.3
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.