.. note::
    :class: sphx-glr-download-link-note

    Click :ref:`here <sphx_glr_download_examples_example_reduce_fcn.py>` to download the full example code
.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_example_reduce_fcn.py:


Fit Specifying Different Reduce Function
========================================

The reduce_fcn specifies how to convert a residual array to a scalar value for
the scalar minimizers. The default value is None (i.e., "sum of squares of
residual") - alternatives are: 'negentropy' and 'neglogcauchy' or a
user-specified "callable". For more information please refer to:
https://lmfit.github.io/lmfit-py/fitting.html#using-the-minimizer-class

Here, we use as an example the Student's t log-likelihood for robust fitting
of data with outliers.



.. image:: /examples/images/sphx_glr_example_reduce_fcn_001.png
    :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 Out:

 .. code-block:: none

    # Fit using sum of squares:

    [[Fit Statistics]]
        # fitting method   = L-BFGS-B
        # function evals   = 130
        # data points      = 101
        # variables        = 4
        chi-square         = 32.1674767
        reduced chi-square = 0.33162347
        Akaike info crit   = -107.560626
        Bayesian info crit = -97.1001440
    [[Variables]]
        offset:  1.10392444 +/- 0.05751441 (5.21%) (init = 2)
        omega:   3.97313427 +/- 0.02073920 (0.52%) (init = 3.3)
        amp:     1.69977080 +/- 0.21587445 (12.70%) (init = 2.5)
        decay:   7.64654024 +/- 1.87208687 (24.48%) (init = 1)
    [[Correlations]] (unreported correlations are < 0.100)
        C(amp, decay) = -0.773


    # Robust Fit, using log-likelihood with Cauchy PDF:

    [[Fit Statistics]]
        # fitting method   = L-BFGS-B
        # function evals   = 130
        # data points      = 101
        # variables        = 4
        chi-square         = 33.5081495
        reduced chi-square = 0.34544484
        Akaike info crit   = -103.436516
        Bayesian info crit = -92.9760337
    [[Variables]]
        offset:  1.02005941 +/- 0.06642641 (6.51%) (init = 2)
        omega:   3.98224469 +/- 0.02898704 (0.73%) (init = 3.3)
        amp:     1.83231477 +/- 0.27241847 (14.87%) (init = 2.5)
        decay:   5.76205973 +/- 1.45140075 (25.19%) (init = 1)
    [[Correlations]] (unreported correlations are < 0.100)
        C(amp, decay)  = -0.758
        C(offset, amp) = -0.107
    /Users/Newville/Codes/lmfit-py/examples/example_reduce_fcn.py:72: UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.
      plt.show()





|


.. code-block:: default

    import matplotlib.pyplot as plt
    import numpy as np

    import lmfit

    np.random.seed(2)
    x = np.linspace(0, 10, 101)

    # Setup example
    decay = 5
    offset = 1.0
    amp = 2.0
    omega = 4.0

    y = offset + amp * np.sin(omega*x) * np.exp(-x/decay)
    yn = y + np.random.normal(size=y.size, scale=0.250)

    outliers = np.random.randint(int(len(x)/3.0), len(x), int(len(x)/12))
    yn[outliers] += 5*np.random.random(len(outliers))


    def resid(params, x, ydata):
        decay = params['decay'].value
        offset = params['offset'].value
        omega = params['omega'].value
        amp = params['amp'].value

        y_model = offset + amp * np.sin(x*omega) * np.exp(-x/decay)
        return y_model - ydata


    params = lmfit.Parameters()

    params.add('offset', 2.0)
    params.add('omega', 3.3)
    params.add('amp', 2.5)
    params.add('decay', 1.0, min=0)

    method = 'L-BFGS-B'

    o1 = lmfit.minimize(resid, params, args=(x, yn), method=method)
    print("# Fit using sum of squares:\n")
    lmfit.report_fit(o1)

    o2 = lmfit.minimize(resid, params, args=(x, yn), method=method,
                        reduce_fcn='neglogcauchy')
    print("\n\n# Robust Fit, using log-likelihood with Cauchy PDF:\n")
    lmfit.report_fit(o2)

    plt.plot(x, y, 'ko', lw=2)
    plt.plot(x, yn, 'k--*', lw=1)
    plt.plot(x, yn+o1.residual, 'r-', lw=2)
    plt.plot(x, yn+o2.residual, 'b-', lw=2)
    plt.legend(['True function',
                'with noise+outliers',
                'sum of squares fit',
                'robust fit'], loc='upper left')
    plt.show()


.. rst-class:: sphx-glr-timing

   **Total running time of the script:** ( 0 minutes  0.319 seconds)


.. _sphx_glr_download_examples_example_reduce_fcn.py:


.. only :: html

 .. container:: sphx-glr-footer
    :class: sphx-glr-footer-example



  .. container:: sphx-glr-download

     :download:`Download Python source code: example_reduce_fcn.py <example_reduce_fcn.py>`



  .. container:: sphx-glr-download

     :download:`Download Jupyter notebook: example_reduce_fcn.ipynb <example_reduce_fcn.ipynb>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
