zfit


Namezfit JSON
Version 0.6.5 PyPI version JSON
download
home_pagehttps://github.com/zfit/zfit
Summaryscalable pythonic model fitting for high energy physics
upload_time2021-05-04 19:13:09
maintainerzfit
docs_urlNone
authorJonas Eschle, Albert Puig, Rafael Silva Coutinho, Matthieu Marinangeli
requires_python<3.9,>=3.6
licenseBSD-3-Clause
keywords tensorflow model fitting scalable hep
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            |zfit_logo|

*******************************
zfit: scalable pythonic fitting
*******************************

.. image:: https://scikit-hep.org/assets/images/Scikit--HEP-Affiliated-blue.svg
   :target: https://scikit-hep.org

.. image:: https://img.shields.io/pypi/v/zfit.svg
   :target: https://pypi.python.org/pypi/zfit

.. image:: https://img.shields.io/conda/vn/conda-forge/zfit
   :alt: conda-forge
   :target: https://anaconda.org/conda-forge/zfit

.. image:: https://github.com/zfit/zfit/workflows/CI/badge.svg
   :target: https://github.com/zfit/zfit/actions

.. image:: https://github.com/zfit/zfit/workflows/build/badge.svg
   :target: https://github.com/zfit/zfit/actions

.. image:: https://coveralls.io/repos/github/zfit/zfit/badge.svg?branch=meta_changes
   :target: https://coveralls.io/github/zfit/zfit?branch=meta_changes

.. image:: https://www.codefactor.io/repository/github/zfit/zfit/badge
   :target: https://www.codefactor.io/repository/github/zfit/zfit
   :alt: CodeFactor


.. |zfit_logo| image:: docs/images/zfit-logo_hires.png
   :target: https://github.com/zfit/zfit
   :alt: zfit logo

.. |scikit-hep_logo| image:: docs/images/scikit-hep-logo_168x168.png
   :target: https://scikit-hep.org/affiliated
   :alt: scikit-hep logo

zfit is a highly scalable and customizable model manipulation and fitting library. It uses
`TensorFlow <https://www.tensorflow.org/>`_ as its computational backend
and is optimised for simple and direct manipulation of probability density functions. The project is affiliated with
and well integrated into `Scikit-HEP <https://scikit-hep.org/>`_, the HEP Python ecosystem.

- **Tutorials**: `Interactive IPython Tutorials <https://github.com/zfit/zfit-tutorials>`_
- **Quick start**: `Example scripts <examples>`_
- **Documentation**: See `stable documentation`_ or `latest documentation`_
- **Questions**: see the `FAQ <https://github.com/zfit/zfit/wiki/FAQ>`_,
  `ask on StackOverflow <https://stackoverflow.com/questions/ask?tags=zfit>`_ (with the **zfit** tag) or `contact`_ us directly.
- **Physics/HEP**: `zfit-physics <https://github.com/zfit/zfit-physics>`_ is the place to contribute/find more HEP
  related content



If you use zfit in **research**, please consider `citing <https://www.sciencedirect.com/science/article/pii/S2352711019303851>`_.

*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 <https://root.cern.ch/>`_ 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
----------------
.. code-block:: python

    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.hesse()

This follows the zfit workflow

.. image:: docs/images/zfit_workflow_v2.png
    :alt: 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:


.. code-block:: python

    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.

.. code-block:: python

  # 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:

.. code-block:: python

    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.

.. code-block:: python

    # 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:

.. code-block:: python

    param_errors = result.hesse()

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

.. code-block:: python

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

    # 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`_.

Prerequisites
=============

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

- `tensorflow <https://www.tensorflow.org/>`_ >= 2.4
- `tensorflow_probability <https://www.tensorflow.org/probability>`_
- `scipy <https://www.scipy.org/>`_ >=1.2
- `uproot <https://github.com/scikit-hep/uproot>`_
- `iminuit <https://github.com/scikit-hep/iminuit>`_

... and some other packages. For a full list, check the `requirements <requirements.txt>`_.

Installing
==========

zfit is currently only available on pip. If possible, use a conda or virtual environment and do:

For pip (if you don't use conda):

.. code-block:: console

    $ pip install zfit


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

.. code-block:: console

   $ 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`_.

.. _Contributing guide: CONTRIBUTING.rst


Contact
=======

You can contact us directly:
 - via e-mail: zfit@physik.uzh.ch
 - join our `Gitter channel <https://gitter.im/zfit/zfit>`_


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`_

..  _all authors and contributors: AUTHORS.rst


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 <https://gitlab.cern.ch/poluekt/TensorFlowAnalysis>`_ framework
developed by Anton Poluektov and `TensorProb <https://github.com/tensorprob/tensorprob>`_ by Chris Burr and Igor Babuschkin
using the TensorFlow open source library and more libraries.

.. _`latest documentation`: https://zfit.readthedocs.io/en/latest/
.. _`stable documentation`: https://zfit.readthedocs.io/en/stable/



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/zfit/zfit",
    "name": "zfit",
    "maintainer": "zfit",
    "docs_url": null,
    "requires_python": "<3.9,>=3.6",
    "maintainer_email": "zfit@physik.uzh.ch",
    "keywords": "TensorFlow,model,fitting,scalable,HEP",
    "author": "Jonas Eschle, Albert Puig, Rafael Silva Coutinho, Matthieu Marinangeli",
    "author_email": "",
    "download_url": "https://files.pythonhosted.org/packages/a8/b0/047b6d36a5aac0cfb6b46c2a5d36b482206bb6f15074b7ec4939cec5ab20/zfit-0.6.5.tar.gz",
    "platform": "",
    "description": "|zfit_logo|\n\n*******************************\nzfit: scalable pythonic fitting\n*******************************\n\n.. image:: https://scikit-hep.org/assets/images/Scikit--HEP-Affiliated-blue.svg\n   :target: https://scikit-hep.org\n\n.. image:: https://img.shields.io/pypi/v/zfit.svg\n   :target: https://pypi.python.org/pypi/zfit\n\n.. image:: https://img.shields.io/conda/vn/conda-forge/zfit\n   :alt: conda-forge\n   :target: https://anaconda.org/conda-forge/zfit\n\n.. image:: https://github.com/zfit/zfit/workflows/CI/badge.svg\n   :target: https://github.com/zfit/zfit/actions\n\n.. image:: https://github.com/zfit/zfit/workflows/build/badge.svg\n   :target: https://github.com/zfit/zfit/actions\n\n.. image:: https://coveralls.io/repos/github/zfit/zfit/badge.svg?branch=meta_changes\n   :target: https://coveralls.io/github/zfit/zfit?branch=meta_changes\n\n.. image:: https://www.codefactor.io/repository/github/zfit/zfit/badge\n   :target: https://www.codefactor.io/repository/github/zfit/zfit\n   :alt: CodeFactor\n\n\n.. |zfit_logo| image:: docs/images/zfit-logo_hires.png\n   :target: https://github.com/zfit/zfit\n   :alt: zfit logo\n\n.. |scikit-hep_logo| image:: docs/images/scikit-hep-logo_168x168.png\n   :target: https://scikit-hep.org/affiliated\n   :alt: scikit-hep logo\n\nzfit is a highly scalable and customizable model manipulation and fitting library. It uses\n`TensorFlow <https://www.tensorflow.org/>`_ as its computational backend\nand is optimised for simple and direct manipulation of probability density functions. The project is affiliated with\nand well integrated into `Scikit-HEP <https://scikit-hep.org/>`_, the HEP Python ecosystem.\n\n- **Tutorials**: `Interactive IPython Tutorials <https://github.com/zfit/zfit-tutorials>`_\n- **Quick start**: `Example scripts <examples>`_\n- **Documentation**: See `stable documentation`_ or `latest documentation`_\n- **Questions**: see the `FAQ <https://github.com/zfit/zfit/wiki/FAQ>`_,\n  `ask on StackOverflow <https://stackoverflow.com/questions/ask?tags=zfit>`_ (with the **zfit** tag) or `contact`_ us directly.\n- **Physics/HEP**: `zfit-physics <https://github.com/zfit/zfit-physics>`_ is the place to contribute/find more HEP\n  related content\n\n\n\nIf you use zfit in **research**, please consider `citing <https://www.sciencedirect.com/science/article/pii/S2352711019303851>`_.\n\n*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.\nIt is, however, mostly ready for production, and is being used in analyses projects.\nIf you want to use it for your project and you are not sure if all the needed functionality is there, feel free to `contact`_.\n\n\nWhy?\n====\n\nThe basic idea behind zfit is to offer a Python oriented alternative to the very successful RooFit library from the `ROOT <https://root.cern.ch/>`_ data analysis package that can integrate with the other packages that are part if the scientific Python ecosystem.\nContrary 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.\n\nThese core ideas are supported by two basic pillars:\n\n- The skeleton and extension of the code is minimalist, simple and finite:\n  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.\n\n- zfit is designed for optimal parallelisation and scalability by making use of TensorFlow as its backend.\n  The use of TensorFlow provides crucial features in the context of model fitting like taking care of the parallelisation and analytic derivatives.\n\n\n\nHow to use\n==========\n\nWhile the zfit library provides a model fitting and sampling framework for a broad list of applications,\nwe will illustrate its main features with a simple example by fitting a Gaussian distribution with an unbinned\nlikelihood fit and a parameter uncertainty estimation.\n\n\nExample in short\n----------------\n.. code-block:: python\n\n    obs = zfit.Space('x', limits=(-10, 10))\n\n    # create the model\n    mu    = zfit.Parameter(\"mu\"   , 2.4, -1, 5)\n    sigma = zfit.Parameter(\"sigma\", 1.3,  0, 5)\n    gauss = zfit.pdf.Gauss(obs=obs, mu=mu, sigma=sigma)\n\n    # load the data\n    data_np = np.random.normal(size=10000)\n    data = zfit.Data.from_numpy(obs=obs, array=data_np)\n\n    # build the loss\n    nll = zfit.loss.UnbinnedNLL(model=gauss, data=data)\n\n    # minimize\n    minimizer = zfit.minimize.Minuit()\n    result = minimizer.minimize(nll)\n\n    # calculate errors\n    param_errors = result.hesse()\n\nThis follows the zfit workflow\n\n.. image:: docs/images/zfit_workflow_v2.png\n    :alt: zfit workflow\n\n\n\n\nFull explanation\n----------------\n\nThe default space (e.g. normalization range) of a PDF is defined by an *observable space*, which is created using the ``zfit.Space`` class:\n\n\n.. code-block:: python\n\n    obs = zfit.Space('x', limits=(-10, 10))\n\n\nTo create a simple Gaussian PDF, we define its parameters and their limits using the ``zfit.Parameter`` class.\n\n.. code-block:: python\n\n  # syntax: zfit.Parameter(\"any_name\", value, lower, upper)\n    mu    = zfit.Parameter(\"mu\"   , 2.4, -1, 5)\n    sigma = zfit.Parameter(\"sigma\", 1.3,  0, 5)\n    gauss = zfit.pdf.Gauss(obs=obs, mu=mu, sigma=sigma)\n\nFor 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:\n\n.. code-block:: python\n\n    mu_true = 0\n    sigma_true = 1\n    data_np = np.random.normal(mu_true, sigma_true, size=10000)\n    data = zfit.Data.from_numpy(obs=obs, array=data_np)\n\nFits are performed in three steps:\n\n1. Creation of a loss function, in our case a negative log-likelihood.\n2. Instantiation of our minimiser of choice, in the example the ``Minuit``.\n3. Minimisation of the loss function.\n\n.. code-block:: python\n\n    # Stage 1: create an unbinned likelihood with the given PDF and dataset\n    nll = zfit.loss.UnbinnedNLL(model=gauss, data=data)\n\n    # Stage 2: instantiate a minimiser (in this case a basic minuit)\n    minimizer = zfit.minimize.Minuit()\n\n    # Stage 3: minimise the given negative log-likelihood\n    result = minimizer.minimize(nll)\n\nErrors are calculated with a further function call to avoid running potentially expensive operations if not needed:\n\n.. code-block:: python\n\n    param_errors = result.hesse()\n\nOnce we've performed the fit and obtained the corresponding uncertainties, we can examine the fit results:\n\n.. code-block:: python\n\n    print(\"Function minimum:\", result.fmin)\n    print(\"Converged:\", result.converged)\n    print(\"Full minimizer information:\", result)\n\n    # Information on all the parameters in the fit\n    params = result.params\n    print(params)\n\n    # Printing information on specific parameters, e.g. mu\n    print(\"mu={}\".format(params[mu]['value']))\n\nAnd that's it!\nFor more details and information of what you can do with zfit, checkout the `latest documentation`_.\n\nPrerequisites\n=============\n\n``zfit`` works with Python versions 3.7 and 3.8.\nThe following packages (amongst others) are required:\n\n- `tensorflow <https://www.tensorflow.org/>`_ >= 2.4\n- `tensorflow_probability <https://www.tensorflow.org/probability>`_\n- `scipy <https://www.scipy.org/>`_ >=1.2\n- `uproot <https://github.com/scikit-hep/uproot>`_\n- `iminuit <https://github.com/scikit-hep/iminuit>`_\n\n... and some other packages. For a full list, check the `requirements <requirements.txt>`_.\n\nInstalling\n==========\n\nzfit is currently only available on pip. If possible, use a conda or virtual environment and do:\n\nFor pip (if you don't use conda):\n\n.. code-block:: console\n\n    $ pip install zfit\n\n\nFor the newest development version, you can install the version from git with\n\n.. code-block:: console\n\n   $ pip install git+https://github.com/zfit/zfit\n\n\nContributing\n============\n\nAny idea of how to improve the library? Or interested to write some code?\nContributions are always welcome, please have a look at the `Contributing guide`_.\n\n.. _Contributing guide: CONTRIBUTING.rst\n\n\nContact\n=======\n\nYou can contact us directly:\n - via e-mail: zfit@physik.uzh.ch\n - join our `Gitter channel <https://gitter.im/zfit/zfit>`_\n\n\nOriginal Authors\n================\n\n| Jonas Eschle <jonas.eschle@cern.ch>\n| Albert Puig <albert.puig@cern.ch>\n| Rafael Silva Coutinho <rsilvaco@cern.ch>\n\n\nSee here for `all authors and contributors`_\n\n..  _all authors and contributors: AUTHORS.rst\n\n\nAcknowledgements\n================\n\nzfit has been developed with support from the University of Zurich and the Swiss National Science Foundation (SNSF) under contracts 168169 and 174182.\n\nThe idea of zfit is inspired by the `TensorFlowAnalysis <https://gitlab.cern.ch/poluekt/TensorFlowAnalysis>`_ framework\ndeveloped by Anton Poluektov and `TensorProb <https://github.com/tensorprob/tensorprob>`_ by Chris Burr and Igor Babuschkin\nusing the TensorFlow open source library and more libraries.\n\n.. _`latest documentation`: https://zfit.readthedocs.io/en/latest/\n.. _`stable documentation`: https://zfit.readthedocs.io/en/stable/\n\n\n",
    "bugtrack_url": null,
    "license": "BSD-3-Clause",
    "summary": "scalable pythonic model fitting for high energy physics",
    "version": "0.6.5",
    "split_keywords": [
        "tensorflow",
        "model",
        "fitting",
        "scalable",
        "hep"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "b967ef577e260eb0284bef5ecb00817a",
                "sha256": "422083ffbefb47f3d968d6dd10e5340508bbc8994748ef0b7b5e56c5f7865919"
            },
            "downloads": -1,
            "filename": "zfit-0.6.5-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "b967ef577e260eb0284bef5ecb00817a",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": "<3.9,>=3.6",
            "size": 311107,
            "upload_time": "2021-05-04T19:13:05",
            "upload_time_iso_8601": "2021-05-04T19:13:05.522783Z",
            "url": "https://files.pythonhosted.org/packages/cc/cf/7ffc62112a856b864df80a061c34f586faa6b24ef1c1b1d3f9972c0ada9e/zfit-0.6.5-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "ff30281ca68910c7aaf8c227b5a158d0",
                "sha256": "23a2511865e3072805631db6362f6495fa417c6834a7655eeb6ac36d8d479e22"
            },
            "downloads": -1,
            "filename": "zfit-0.6.5.tar.gz",
            "has_sig": false,
            "md5_digest": "ff30281ca68910c7aaf8c227b5a158d0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<3.9,>=3.6",
            "size": 1844789,
            "upload_time": "2021-05-04T19:13:09",
            "upload_time_iso_8601": "2021-05-04T19:13:09.182882Z",
            "url": "https://files.pythonhosted.org/packages/a8/b0/047b6d36a5aac0cfb6b46c2a5d36b482206bb6f15074b7ec4939cec5ab20/zfit-0.6.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2021-05-04 19:13:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": null,
    "github_project": "zfit",
    "error": "Could not fetch GitHub repository",
    "lcname": "zfit"
}
        
Elapsed time: 0.37365s