zfit: Scalable Pythonic Model Fitting for High Energy Physics
zfit is a modern, scalable Python library for statistical model fitting, primarily designed for High Energy Physics but applicable broadly. It leverages TensorFlow (and optionally JAX) for accelerated computations on CPUs and GPUs, offering a user-friendly API for defining, manipulating, and fitting complex probability density functions. The current version is 0.28.0, with a release cadence of typically a few months between minor versions, often driven by new feature development and backend compatibility updates.
Warnings
- breaking The `effsize` weight correction parameter was renamed to `sumw2` in certain contexts for asymptotic uncertainty calculations.
- gotcha The default padding for KDE (Kernel Density Estimation) functions, specifically `KDE1DimExact`, changed from `False` to `0.1`.
- gotcha zfit frequently updates its TensorFlow and TensorFlow-Probability backend requirements. Major version bumps of these dependencies can cause environment conflicts or necessitate upgrades.
- breaking As of v0.27.0, zfit explicitly requires Python 3.10 or newer. Support for Python 3.13 was added in v0.28.0.
Install
-
pip install zfit[tf] -
pip install zfit[jax] -
pip install zfit
Imports
- Parameter
from zfit import Parameter
- Space
from zfit import Space
- pdf
import zfit.pdf as zpdf
- data
import zfit.data as zdata
- loss
import zfit.loss as zloss
- minimize
import zfit.minimize as zmin
Quickstart
import zfit
from zfit import z
# Define parameters
mu = zfit.Parameter('mu', 0.0, -1.0, 1.0)
sigma = zfit.Parameter('sigma', 1.0, 0.1, 10.0)
# Define the observable space
obs = zfit.Space('x', limits=(-5, 5))
# Create a Gaussian PDF
gauss = zfit.pdf.Gauss(mu=mu, sigma=sigma, obs=obs)
# Generate some data
data = gauss.sample(n=1000)
# Create a likelihood loss function
nll = zfit.loss.UnbinnedNLL(model=gauss, data=data)
# Create a minimizer
minimizer = zfit.minimize.Minuit()
# Perform the fit
result = minimizer.minimize(nll)
# Print the fit results
print(result.params)