lmfit


Namelmfit JSON
Version 1.3.2 PyPI version JSON
download
home_pageNone
SummaryLeast-Squares Minimization with Bounds and Constraints
upload_time2024-07-19 16:17:57
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseBSD-3 Copyright 2022 Matthew Newville, The University of Chicago Renee Otten, Brandeis University Till Stensitzki, Freie Universitat Berlin A. R. J. Nelson, Australian Nuclear Science and Technology Organisation Antonino Ingargiola, University of California, Los Angeles Daniel B. Allen, Johns Hopkins University Michal Rawlik, Eidgenossische Technische Hochschule, Zurich Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Some code has been taken from the scipy library whose licence is below. Copyright (c) 2001, 2002 Enthought, Inc. All rights reserved. Copyright (c) 2003-2019 SciPy Developers. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: a. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. b. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. c. Neither the name of Enthought nor the names of the SciPy Developers may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Some code has been taken from the AMPGO library of Andrea Gavana, which was released under a MIT license.
keywords curve-fitting least-squares minimization
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            LMfit-py
========

.. image:: https://dev.azure.com/lmfit/lmfit-py/_apis/build/status/lmfit.lmfit-py?branchName=master
    :target: https://dev.azure.com/lmfit/lmfit-py/_build/latest?definitionId=1&branchName=master

.. image:: https://codecov.io/gh/lmfit/lmfit-py/branch/master/graph/badge.svg
  :target: https://codecov.io/gh/lmfit/lmfit-py

.. image:: https://img.shields.io/pypi/v/lmfit.svg
   :target: https://pypi.org/project/lmfit

.. image:: https://img.shields.io/pypi/dm/lmfit.svg
   :target: https://pypi.org/project/lmfit

.. image:: https://img.shields.io/badge/docs-read-brightgreen
   :target: https://lmfit.github.io/lmfit-py/

.. image:: https://zenodo.org/badge/4185/lmfit/lmfit-py.svg
   :target: https://doi.org/10.5281/zenodo.598352

.. _LMfit google mailing list: https://groups.google.com/group/lmfit-py
.. _Github Discussions: https://github.com/lmfit/lmfit-py/discussions
.. _Github Issues: https://github.com/lmfit/lmfit-py/issues


..
   Note: the Zenodo target should be
   https://zenodo.org/badge/latestdoi/4185/lmfit/lmfit-py
   but see https://github.com/lmfit/lmfit-py/discussions/862


Overview
---------

The lmfit Python library supports provides tools for non-linear least-squares
minimization and curve fitting.  The goal is to make these optimization
algorithms more flexible, more comprehensible, and easier to use well, with the
key feature of casting variables in minimization and fitting routines as named
parameters that can have many attributes beside just a current value.

LMfit is a pure Python package, built on top of Scipy and Numpy, and so easy to
install with ``pip install lmfit``.

For questions, comments, and suggestions, please use the `LMfit google mailing
list`_ or `Github discussions`_.  For software issues and bugs, use `Github
Issues`_, but please read `Contributing.md <.github/CONTRIBUTING.md>`_ before
creating an Issue.


Parameters and Minimization
------------------------------

LMfit provides optimization routines similar to (and based on) those from
``scipy.optimize``, but with a simple, flexible approach to parameterizing a
model for fitting to data using named parameters. These named Parameters can be
held fixed or freely adjusted in the fit, or held between lower and upper
bounds. Parameters can also be constrained as a simple mathematical expression
of other Parameters.

A Parameters object (which acts like a Python dictionary) contains named
parameters, and can be built as with::

    import lmfit
    fit_params = lmfit.Parameters()
    fit_params['amp'] = lmfit.Parameter(value=1.2)
    fit_params['cen'] = lmfit.Parameter(value=40.0, vary=False)
    fit_params['wid'] = lmfit.Parameter(value=4, min=0)
    fit_params['fwhm'] = lmfit.Parameter(expr='wid*2.355')

or using the equivalent::

    fit_params = lmfit.create_params(amp=1.2,
                                     cen={'value':40, 'vary':False},
                                     wid={'value': 4, 'min':0},
                                     fwhm={'expr': 'wid*2.355'})



In the general minimization case (see below for Curve-fitting), the user will
also write an objective function to be minimized (in the least-squares sense)
with its first argument being this Parameters object, and additional positional
and keyword arguments as desired::

    def myfunc(params, x, data, someflag=True):
        amp = params['amp'].value
        cen = params['cen'].value
        wid = params['wid'].value
        ...
        return residual_array

For each call of this function, the values for the ``params`` may have changed,
subject to the bounds and constraint settings for each Parameter. The function
should return the residual (i.e., ``data-model``) array to be minimized.

The advantage here is that the function to be minimized does not have to be
changed if different bounds or constraints are placed on the fitting Parameters.
The fitting model (as described in myfunc) is instead written in terms of
physical parameters of the system, and remains remains independent of what is
actually varied in the fit. In addition, which parameters are adjusted and which
are fixed happens at run-time, so that changing what is varied and what
constraints are placed on the parameters can easily be modified by the user in
real-time data analysis.

To perform the fit, the user calls::

    result = lmfit.minimize(myfunc, fit_params, args=(x, data), kws={'someflag':True}, ....)

After the fit, a ``MinimizerResult`` class is returned that holds the results
the fit (e.g., fitting statistics and optimized parameters). The dictionary
``result.params`` contains the best-fit values, estimated standard deviations,
and correlations with other variables in the fit.

By default, the underlying fit algorithm is the Levenberg-Marquardt algorithm
with numerically-calculated derivatives from MINPACK's lmdif function, as used
by ``scipy.optimize.leastsq``. Most other solvers that are present in ``scipy``
(e.g., Nelder-Mead, differential_evolution, basin-hopping, and more) are also
supported.


Curve-Fitting with lmfit.Model
----------------------------------

One of the most common use of least-squares minimization is for curve fitting,
where minimization of ``data-model``, or ``(data-model)*weights``.  Using
``lmfit.minimize`` as above, the objective function would take ``data`` and
``weights`` and effectively calculated the model and then return the value of
``(data-model)*weights``.

To simplify this, and make curve-fitting more flexible, lmfit provides a Model
class that wraps a *model function* that represents the model (without the data
or weights).  Parameters are then automatically found from the named arguments
of the model function.  In addition, simple model functions can be readily
combined and reused, and several common model functions are included in lmfit.

Exploration of Confidence Intervals
-------------------------------------

Lmfit tries to always estimate uncertainties in fitting parameters and
correlations between them.  It does this even for those methods where the
corresponding ``scipy.optimize`` routines do not estimate uncertainties.  Lmfit
also provides methods to explicitly explore and evaluate the confidence
intervals in fit results.

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "lmfit",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": "curve-fitting, least-squares minimization",
    "author": null,
    "author_email": "LMFit Development Team <matt.newville@gmail.com>",
    "download_url": "https://files.pythonhosted.org/packages/ad/e0/e6ced7c4f7c3e7aa64057d6482bcfc798fbe731fb3226918769b7b76aba1/lmfit-1.3.2.tar.gz",
    "platform": null,
    "description": "LMfit-py\n========\n\n.. image:: https://dev.azure.com/lmfit/lmfit-py/_apis/build/status/lmfit.lmfit-py?branchName=master\n    :target: https://dev.azure.com/lmfit/lmfit-py/_build/latest?definitionId=1&branchName=master\n\n.. image:: https://codecov.io/gh/lmfit/lmfit-py/branch/master/graph/badge.svg\n  :target: https://codecov.io/gh/lmfit/lmfit-py\n\n.. image:: https://img.shields.io/pypi/v/lmfit.svg\n   :target: https://pypi.org/project/lmfit\n\n.. image:: https://img.shields.io/pypi/dm/lmfit.svg\n   :target: https://pypi.org/project/lmfit\n\n.. image:: https://img.shields.io/badge/docs-read-brightgreen\n   :target: https://lmfit.github.io/lmfit-py/\n\n.. image:: https://zenodo.org/badge/4185/lmfit/lmfit-py.svg\n   :target: https://doi.org/10.5281/zenodo.598352\n\n.. _LMfit google mailing list: https://groups.google.com/group/lmfit-py\n.. _Github Discussions: https://github.com/lmfit/lmfit-py/discussions\n.. _Github Issues: https://github.com/lmfit/lmfit-py/issues\n\n\n..\n   Note: the Zenodo target should be\n   https://zenodo.org/badge/latestdoi/4185/lmfit/lmfit-py\n   but see https://github.com/lmfit/lmfit-py/discussions/862\n\n\nOverview\n---------\n\nThe lmfit Python library supports provides tools for non-linear least-squares\nminimization and curve fitting.  The goal is to make these optimization\nalgorithms more flexible, more comprehensible, and easier to use well, with the\nkey feature of casting variables in minimization and fitting routines as named\nparameters that can have many attributes beside just a current value.\n\nLMfit is a pure Python package, built on top of Scipy and Numpy, and so easy to\ninstall with ``pip install lmfit``.\n\nFor questions, comments, and suggestions, please use the `LMfit google mailing\nlist`_ or `Github discussions`_.  For software issues and bugs, use `Github\nIssues`_, but please read `Contributing.md <.github/CONTRIBUTING.md>`_ before\ncreating an Issue.\n\n\nParameters and Minimization\n------------------------------\n\nLMfit provides optimization routines similar to (and based on) those from\n``scipy.optimize``, but with a simple, flexible approach to parameterizing a\nmodel for fitting to data using named parameters. These named Parameters can be\nheld fixed or freely adjusted in the fit, or held between lower and upper\nbounds. Parameters can also be constrained as a simple mathematical expression\nof other Parameters.\n\nA Parameters object (which acts like a Python dictionary) contains named\nparameters, and can be built as with::\n\n    import lmfit\n    fit_params = lmfit.Parameters()\n    fit_params['amp'] = lmfit.Parameter(value=1.2)\n    fit_params['cen'] = lmfit.Parameter(value=40.0, vary=False)\n    fit_params['wid'] = lmfit.Parameter(value=4, min=0)\n    fit_params['fwhm'] = lmfit.Parameter(expr='wid*2.355')\n\nor using the equivalent::\n\n    fit_params = lmfit.create_params(amp=1.2,\n                                     cen={'value':40, 'vary':False},\n                                     wid={'value': 4, 'min':0},\n                                     fwhm={'expr': 'wid*2.355'})\n\n\n\nIn the general minimization case (see below for Curve-fitting), the user will\nalso write an objective function to be minimized (in the least-squares sense)\nwith its first argument being this Parameters object, and additional positional\nand keyword arguments as desired::\n\n    def myfunc(params, x, data, someflag=True):\n        amp = params['amp'].value\n        cen = params['cen'].value\n        wid = params['wid'].value\n        ...\n        return residual_array\n\nFor each call of this function, the values for the ``params`` may have changed,\nsubject to the bounds and constraint settings for each Parameter. The function\nshould return the residual (i.e., ``data-model``) array to be minimized.\n\nThe advantage here is that the function to be minimized does not have to be\nchanged if different bounds or constraints are placed on the fitting Parameters.\nThe fitting model (as described in myfunc) is instead written in terms of\nphysical parameters of the system, and remains remains independent of what is\nactually varied in the fit. In addition, which parameters are adjusted and which\nare fixed happens at run-time, so that changing what is varied and what\nconstraints are placed on the parameters can easily be modified by the user in\nreal-time data analysis.\n\nTo perform the fit, the user calls::\n\n    result = lmfit.minimize(myfunc, fit_params, args=(x, data), kws={'someflag':True}, ....)\n\nAfter the fit, a ``MinimizerResult`` class is returned that holds the results\nthe fit (e.g., fitting statistics and optimized parameters). The dictionary\n``result.params`` contains the best-fit values, estimated standard deviations,\nand correlations with other variables in the fit.\n\nBy default, the underlying fit algorithm is the Levenberg-Marquardt algorithm\nwith numerically-calculated derivatives from MINPACK's lmdif function, as used\nby ``scipy.optimize.leastsq``. Most other solvers that are present in ``scipy``\n(e.g., Nelder-Mead, differential_evolution, basin-hopping, and more) are also\nsupported.\n\n\nCurve-Fitting with lmfit.Model\n----------------------------------\n\nOne of the most common use of least-squares minimization is for curve fitting,\nwhere minimization of ``data-model``, or ``(data-model)*weights``.  Using\n``lmfit.minimize`` as above, the objective function would take ``data`` and\n``weights`` and effectively calculated the model and then return the value of\n``(data-model)*weights``.\n\nTo simplify this, and make curve-fitting more flexible, lmfit provides a Model\nclass that wraps a *model function* that represents the model (without the data\nor weights).  Parameters are then automatically found from the named arguments\nof the model function.  In addition, simple model functions can be readily\ncombined and reused, and several common model functions are included in lmfit.\n\nExploration of Confidence Intervals\n-------------------------------------\n\nLmfit tries to always estimate uncertainties in fitting parameters and\ncorrelations between them.  It does this even for those methods where the\ncorresponding ``scipy.optimize`` routines do not estimate uncertainties.  Lmfit\nalso provides methods to explicitly explore and evaluate the confidence\nintervals in fit results.\n",
    "bugtrack_url": null,
    "license": "BSD-3  Copyright 2022 Matthew Newville, The University of Chicago Renee Otten, Brandeis University Till Stensitzki, Freie Universitat Berlin A. R. J. Nelson, Australian Nuclear Science and Technology Organisation Antonino Ingargiola, University of California, Los Angeles Daniel B. Allen, Johns Hopkins University Michal Rawlik, Eidgenossische Technische Hochschule, Zurich  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.  3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.   Some code has been taken from the scipy library whose licence is below.  Copyright (c) 2001, 2002 Enthought, Inc. All rights reserved.  Copyright (c) 2003-2019 SciPy Developers. All rights reserved.  Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:  a. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. b. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. c. Neither the name of Enthought nor the names of the SciPy Developers may be used to endorse or promote products derived from this software without specific prior written permission.   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.  Some code has been taken from the AMPGO library of Andrea Gavana, which was released under a MIT license. ",
    "summary": "Least-Squares Minimization with Bounds and Constraints",
    "version": "1.3.2",
    "project_urls": {
        "Changelog": "https://lmfit.github.io/lmfit-py/whatsnew.html",
        "Documentation": "https://lmfit.github.io/lmfit-py/",
        "Homepage": "https://lmfit.github.io//lmfit-py/"
    },
    "split_keywords": [
        "curve-fitting",
        " least-squares minimization"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "1c3b594a8d453a1970ca20d0a39b310882279a9396285f45f89b08e8f6b60e1f",
                "md5": "3f8b4824c518a88450f63b0d1bf81b9d",
                "sha256": "2b834f054cd7a5172f3b431345b292e5d95ea387d6f96d60ad35a11b8efee6ac"
            },
            "downloads": -1,
            "filename": "lmfit-1.3.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "3f8b4824c518a88450f63b0d1bf81b9d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 98887,
            "upload_time": "2024-07-19T16:17:55",
            "upload_time_iso_8601": "2024-07-19T16:17:55.822126Z",
            "url": "https://files.pythonhosted.org/packages/1c/3b/594a8d453a1970ca20d0a39b310882279a9396285f45f89b08e8f6b60e1f/lmfit-1.3.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ade0e6ced7c4f7c3e7aa64057d6482bcfc798fbe731fb3226918769b7b76aba1",
                "md5": "214b4eada76c06b6209df54462e3894e",
                "sha256": "31beeae1f027c1b8c14dcd7f2e8488a80b75fb389e77fca677549bdc2fe597bb"
            },
            "downloads": -1,
            "filename": "lmfit-1.3.2.tar.gz",
            "has_sig": false,
            "md5_digest": "214b4eada76c06b6209df54462e3894e",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 623945,
            "upload_time": "2024-07-19T16:17:57",
            "upload_time_iso_8601": "2024-07-19T16:17:57.660454Z",
            "url": "https://files.pythonhosted.org/packages/ad/e0/e6ced7c4f7c3e7aa64057d6482bcfc798fbe731fb3226918769b7b76aba1/lmfit-1.3.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-07-19 16:17:57",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "lmfit"
}
        
Elapsed time: 0.82012s