In [1]:
%matplotlib inline
import numpy as np
from matplotlib import pylab

from curve_fit import *

Create dataset


In [2]:
def linear_fit(x, a, b):
    return a*x + b

In [3]:
x = np.arange(0, 1000, 1)
y = 1.5*x+10 + np.random.uniform(-100, 100, len(x))

In [4]:
pylab.plot(x, y, '.')


Out[4]:
[<matplotlib.lines.Line2D at 0x7f70bf162358>]

Create parameters


In [13]:
parameters = Parameters([Parameter("a", 1), Parameter("b", 0)])
data = SimpleFitInputData(x, y, id="Example Fit")
fit_performer = SimpleMinimizeFit(data, parameters, fit_func=linear_fit)

The perform_fit method does fit and returns a fitted function.


In [18]:
fitted_func = fit_performer.perform_fit()

Parameter values are accessible using:


In [20]:
fit_performer.fit_result.params.param_values


Out[20]:
{'b': 9.8271467541218485, 'a': 1.5076467079644329}

In [21]:
pylab.plot(x, y, '.')
pylab.plot(fit_performer.fit_result.plot_geometry, fit_performer.fit_result.plot_profile, linewidth=3)


Out[21]:
[<matplotlib.lines.Line2D at 0x7f70bf0e3160>]

Use disabled parameters

Let's assume that we know that in this particular case "true" value of parameter b is 10. In this case we still pass "b" parameter to the fitter, but we mark it as disabled --- it will still be passed to linear_fit but will not take part in fitting at all.


In [22]:
parameters = Parameters([Parameter("a", 1), Parameter("b", 10, enabled=False)])
data = SimpleFitInputData(x, y, id="Example Fit")
fit_performer = SimpleMinimizeFit(data, parameters, fit_func=linear_fit)

In [23]:
fit_performer.perform_fit()


Out[23]:
functools.partial(<bound method SimpleMinimizeFit.__eval_function_wrapper of <curve_fit.api.SimpleMinimizeFit object at 0x7f70bf0239e8>>, array([ 1.50738729]))

In [24]:
fit_performer.fit_result.params.param_values


Out[24]:
{'b': 10, 'a': 1.5073872949958238}

Let's use bounds


In [27]:
parameters = Parameters([Parameter("a", 1, min_bound=0), Parameter("b", 10, min_bound=5, max_bound=15)])
data = SimpleFitInputData(x, y, id="Example Fit")
fit_performer = SimpleMinimizeFit(data, parameters, fit_func=linear_fit)

In [28]:
fitted_func = fit_performer.perform_fit()

In [29]:
fit_performer.fit_result.params.param_values


Out[29]:
{'b': 9.8267684715858348, 'a': 1.5076472069299807}

In [30]:
pylab.plot(x, y, '.')
pylab.plot(fit_performer.fit_result.plot_geometry, fit_performer.fit_result.plot_profile, linewidth=3)


Out[30]:
[<matplotlib.lines.Line2D at 0x7f70bf12d908>]

In [ ]: