Skip to main content

scalable pythonic model fitting for high energy physics

Reason this release was yanked:

Has no upper Python bound -> installs if new, unsupported Python version out

Project description

zfit: scalable pythonic fitting

https://zenodo.org/badge/126311570.svg https://img.shields.io/pypi/v/zfit.svg https://img.shields.io/travis/zfit/zfit.svg https://coveralls.io/repos/github/zfit/zfit/badge.svg?branch=meta_changes CodeFactor

zfit is a highly scalable and customizable model manipulation and fitting library. It uses TensorFlow as its computational backend and is optimised for simple and direct manipulation of probability density functions.

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.

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 to 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.

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', limits=(-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=10000)
data = zfit.Data.from_numpy(obs=obs, array=data_np)

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

# minimize
minimizer = zfit.minimize.Minuit()
result = minimizer.minimize(nll)

# calculate errors
param_errors = result.error()

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', limits=(-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.from_numpy(obs=obs, array=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)

Errors are calculated with a further function call to avoid running potentially expensive operations if not needed:

param_errors = result.error()

Once we’ve performed the fit and obtained the corresponding uncertainties, we can examine the fit results:

print("Function minimum:", result.fmin)
print("Converged:", result.converged)
print("Full minimizer information:", result.info)

# 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 documentation.

Prerequisites

zfit works with Python versions 3.6 and 3.7. The following packages (amongst others) are required:

… and some minor packages. For a full list, check the requirements.

Installing

To install zfit, run this command in your terminal:

$ pip install zfit

For the newest development version, you can install the version from git with

$ pip install git+https://github.com/zfit/zfit

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:

Main Authors

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

Acknowledgements

zfit has been developed with support from the University of Zürich 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 using the TensorFlow open source library.

Changelog

Develop

Major Features and Improvements

  • full TF 2.0 compatibility, tests against 1.x and 2.x

Behavioral changes

Bug fixes and small changes

Requirement changes

Thanks

0.3.6 (12.10.19)

Special relese for conda deployment and version fix (TF 2.0 is out)

This is the last release before breaking changes occur

Major Features and Improvements

  • added ConstantParameter and zfit.param namespace

  • Available on conda-forge

Behavioral changes

  • an implicitly created parameter with a Python numerical (e.g. when instantiating a model) will be converted to a ConstantParameter instead of a fixed Parameter and therefore cannot be set to floating later on.

Bug fixes and small changes

  • added native support TFP distributions for analytic sampling

  • fix Gaussian (TFP Distribution) Constraint with mixed up order of parameters

  • from_numpy automatically converts to default float regardless the original numpy dtype, dtype has to be used as an explicit argument

Requirement changes

  • TensorFlow >= 1.14 is required

Thanks

  • Chris Burr for the conda-forge deployment

0.3.4 (30-07-19)

This is the last release before breaking changes occur

Major Features and Improvements

  • create Constraint class which allows for more fine grained control and information on the applied constraints.

  • Added Polynomial models

  • Improved and fixed sampling (can still be slightly biased)

Behavioral changes

None

Bug fixes and small changes

  • fixed various small bugs

Thanks

for the contribution of the Constraints to Matthieu Marinangeli <matthieu.marinangeli@cern.ch>

0.3.3 (15-05-19)

Fixed Partial numeric integration

Bugfixes mostly, a few major fixes. Partial numeric integration works now.

Bugfixes
  • data_range cuts are now applied correctly, also in several dimensions when a subset is selected (which happens internally of some Functors, e.g. ProductPDF). Before, only the selected obs was respected for cuts.

  • parital integration had a wrong take on checking limits (now uses supports).

0.3.2 (01-05-19)

With 0.3.2, bugfixes and three changes in the API/behavior

Breaking changes

  • tfp distributions wrapping is now different with dist_kwargs allowing for non-Parameter arguments (like other dists)

  • sampling allows now for importance sampling (sampler in Model specified differently)

  • model.sample now also returns a tensor, being consistent with pdf and integrate

Bugfixes

  • shape handling of tfp dists was “wrong” (though not producing wrong results!), fixed. TFP distributions now get a tensor with shape (nevents, nobs) instead of a list of tensors with (nevents,)

Improvements

  • refactor the sampling for more flexibility and performance (less graph constructed)

  • allow to use more sophisticated importance sampling (e.g. phasespace)

  • on-the-fly normalization (experimentally) implemented with correct gradient

0.3.1 (30-04-19)

Minor improvements and bugfixes including:

  • improved importance sampling allowing to preinstantiate objects before it’s called inside the while loop

  • fixing a problem with ztf.sqrt

0.3.0 (2019-03-20)

Beta stage and first pip release

0.0.1 (2018-03-22)

  • First creation of the package.

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

zfit-0.3.6.tar.gz (241.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

zfit-0.3.6-py2.py3-none-any.whl (139.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file zfit-0.3.6.tar.gz.

File metadata

  • Download URL: zfit-0.3.6.tar.gz
  • Upload date:
  • Size: 241.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

File hashes

Hashes for zfit-0.3.6.tar.gz
Algorithm Hash digest
SHA256 26e76eb100c95ed52241f3b552d7dd16f59091a83f5e01b263f6fa9f12b30cfe
MD5 842c7026f4b881f0ba4c8a8a72eb623e
BLAKE2b-256 f1d11606cf1a526fefaf570b3cdaa52d336df60f27f4caced27ff43326cf8f35

See more details on using hashes here.

File details

Details for the file zfit-0.3.6-py2.py3-none-any.whl.

File metadata

  • Download URL: zfit-0.3.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 139.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.1

File hashes

Hashes for zfit-0.3.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ace00ff965574026034bce9c9b17d92a50efc1808a925daa476631a7c823e642
MD5 3620c166c5fa50b91e81c6093b8c28c6
BLAKE2b-256 f5691501f47b35636c0ceca5f756c7cee429d36fe96739a77459e25861b67e3e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page