Skip to main content

zfit logo

zfit: scalable pythonic fitting

https://scikit-hep.org/assets/images/Scikit--HEP-Affiliated-blue.svg DOI 10.1016/j.softx.2020.100508 PyPI - Python Version https://img.shields.io/pypi/v/zfit.svg conda-forge https://img.shields.io/spack/v/py-zfit https://github.com/zfit/zfit/workflows/build/badge.svg

zfit is a highly scalable and customizable model manipulation and likelihood fitting library. It uses the same computational backend as TensorFlow and is optimised for simple and direct manipulation of probability density functions. The project is affiliated with and well integrated into Scikit-HEP, the HEP Python ecosystem.

If you use zfit in research, please consider citing.

N.B.: zfit is currently in beta stage, so while most core parts are established, some may still be missing and bugs may be encountered. It is, however, mostly ready for production, and is being used in analyses projects. If you want to use it for your project and you are not sure if all the needed functionality is there, feel free to contact us.

Installation

zfit is available on pip and conda-forge. To install it (recommended: use a virtual/conda env!) with all the dependencies (minimizers, uproot, …), use

pip install -U zfit[all]

(the -U just indicates to upgrade zfit, in case you have it already installed) or for minimal dependencies

pip install zfit

For conda/mamba, use

conda install -c conda-forge zfit

How to use

While the zfit library provides a model fitting and sampling framework for a broad list of applications, we will illustrate its main features with a simple example by fitting a Gaussian distribution with an unbinned likelihood fit and a parameter uncertainty estimation.

Example in short

obs = zfit.Space('x', -10, 10)

# create the model
mu    = zfit.Parameter("mu"   , 2.4, -1, 5)
sigma = zfit.Parameter("sigma", 1.3,  0, 5)
gauss = zfit.pdf.Gauss(obs=obs, mu=mu, sigma=sigma)

# load the data
data_np = np.random.normal(size=10_000)
data = zfit.Data(obs=obs, data=data_np)
# or sample from model
data = gauss.sample(10_000)

# build the loss
nll = zfit.loss.UnbinnedNLL(model=gauss, data=data)

# minimize (20+ interchangeable minimizers available!)
minimizer = zfit.minimize.Minuit()
result = minimizer.minimize(nll).update_params()

# calculate errors
sym_errors = result.hesse()
asym_errors = result.errors()

This follows the zfit workflow

zfit workflow

Full explanation

The default space (e.g. normalization range) of a PDF is defined by an observable space, which is created using the zfit.Space class:

obs = zfit.Space('x', -10, 10)

To create a simple Gaussian PDF, we define its parameters and their limits using the zfit.Parameter class.

# syntax: zfit.Parameter("any_name", value, lower, upper)
  mu    = zfit.Parameter("mu"   , 2.4, -1, 5)
  sigma = zfit.Parameter("sigma", 1.3,  0, 5)
  gauss = zfit.pdf.Gauss(obs=obs, mu=mu, sigma=sigma)

For simplicity, we create the dataset to be fitted starting from a numpy array, but zfit allows for the use of other sources such as ROOT files:

mu_true = 0
sigma_true = 1
data_np = np.random.normal(mu_true, sigma_true, size=10000)
data = zfit.Data(obs=obs, data=data_np)

Fits are performed in three steps:

  1. Creation of a loss function, in our case a negative log-likelihood.

  2. Instantiation of our minimiser of choice, in the example the Minuit.

  3. Minimisation of the loss function.

# Stage 1: create an unbinned likelihood with the given PDF and dataset
nll = zfit.loss.UnbinnedNLL(model=gauss, data=data)

# Stage 2: instantiate a minimiser (in this case a basic minuit)
minimizer = zfit.minimize.Minuit()

# Stage 3: minimise the given negative log-likelihood
result = minimizer.minimize(nll).update_params()

The .update_params() changes the default values of the parameters (this is currently happen by default but won’t anymore in the future)

Symmetric errors are calculated with a further function call to avoid running potentially expensive operations if not needed. Asymmetric errors using a profiling method can also be obtained:

sym_errors = result.hesse()
asym_errors = result.errors()

Once we’ve performed the fit and obtained the corresponding uncertainties, we can examine the fit results by printing it or looking at individual parts

print(result)  # nice representation of a whole result

print("Function minimum:", result.fmin)
print("Converged:", result.converged)

# Information on all the parameters in the fit
params = result.params
print(params)

# Printing information on specific parameters, e.g. mu
print("mu={}".format(params[mu]['value']))

And that’s it! For more details and information of what you can do with zfit, checkout the latest documentation.

Why?

The basic idea behind zfit is to offer a Python oriented alternative to the very successful RooFit library from the ROOT data analysis package that can integrate with the other packages that are part if the scientific Python ecosystem. Contrary to the monolithic approach of ROOT/RooFit, the aim of zfit is to be light and flexible enough t o integrate with any state-of-art tools and to allow scalability going to larger datasets.

These core ideas are supported by two basic pillars:

  • The skeleton and extension of the code is minimalist, simple and finite: the zfit library is exclusively designed for the purpose of model fitting and sampling with no attempt to extend its functionalities to features such as statistical methods or plotting.

  • zfit is designed for optimal parallelisation and scalability by making use of TensorFlow as its backend. The use of TensorFlow provides crucial features in the context of model fitting like taking care of the parallelisation and analytic derivatives.

Prerequisites

zfit works with Python versions 3.10 to 3.13. The main dependency is tensorflow: zfit follows a close version compatibility with TensorFlow.

Contributing

Any idea of how to improve the library? Or interested to write some code? Contributions are always welcome, please have a look at the Contributing guide.

Contact

You can contact us directly:

Original Authors

Jonas Eschle <jonas.eschle@cern.ch>
Albert Puig <albert.puig@cern.ch>
Rafael Silva Coutinho <rsilvaco@cern.ch>

See here for all authors and contributors

Acknowledgements

zfit has been developed with support from the University of Zurich and the Swiss National Science Foundation (SNSF) under contracts 168169 and 174182.

The idea of zfit is inspired by the TensorFlowAnalysis framework developed by Anton Poluektov and TensorProb by Chris Burr and Igor Babuschkin using the TensorFlow open source library and more libraries.

Release files for zfit 0.28.0

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

Source distribution (sdist)

Source distribution for zfit 0.28.0
File Size Uploaded
zfit-0.28.0.tar.gz 3.6 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for zfit 0.28.0
File Interpreter ABI Platform
zfit-0.28.0-py3-none-any.whl Python 3 none any Details

Total release size: 4.1 MB

Release files / zfit-0.28.0.tar.gz

Download URL zfit-0.28.0.tar.gz
Size 3.6 MB
Tags Source
SHA-256 checksum
How to use checksums
80fb7b0ef5700a38f8dd4e592f3fdf5adb2fe828b1df42ebfba49c22625af55c
BLAKE2b-256 checksum
How to use checksums
091ebbab9ecf16645890db3ba4793ea29ebbf58dbc87af945ad3458c788a3eae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2025.

Transparency log

Release files / zfit-0.28.0-py3-none-any.whl

Download URL zfit-0.28.0-py3-none-any.whl
Size 473.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ce1caba9d7a553843686e56e403088705cb70e67f5fbdd8c39efbe49275b9bf4
BLAKE2b-256 checksum
How to use checksums
a529aaa8cb31c480e00b3c79df82acab1dda6af6516e02bc657dba208ea76acc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 18, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.28.0 This release

2 release files

0.27.1

2 release files

0.27.0

2 release files

0.25.0

2 release files

0.24.2

2 release files

0.24.0

2 release files

0.22.0

2 release files

0.21.1

2 release files

0.20.3

2 release files

0.20.2

2 release files

0.20.1

2 release files

0.20.0

2 release files

0.18.2

2 release files

0.18.1

2 release files

0.18.0

2 release files

0.17.0

2 release files

0.15.5

2 release files

0.15.2

2 release files

0.15.1

2 release files

0.15.0

2 release files

0.14.0

2 release files

0.13.2

2 release files

0.13.1

2 release files

0.13.0

2 release files

0.12.0

2 release files

0.11.1

2 release files

0.10.1

2 release files

0.10.0

2 release files

0.8.3

2 release files

0.8.2

2 release files

0.8.1

2 release files

0.7.2

2 release files

0.7.1

2 release files

0.7.0

2 release files

0.6.6

2 release files

0.6.5

2 release files

0.6.4

1 release file

0.6.3

2 release files

0.6.2

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.6

2 release files

0.5.5

2 release files

0.5.4

2 release files

0.5.3

2 release files

0.5.2

2 release files

0.5.1

2 release files

0.5.0

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.7

2 release files

0.3.6

2 release files

0.3.5

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.3

2 release files

0.2.0

2 release files

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