curve - Model a fit function

Curve Model a measurement with a user defined function.
PoissonCurve Model a measurement with Poisson uncertainty.
plot_err Plot data y and error dy against x.

Build a bumps model from a function and data.

Example

Given a function sin_model which computes a sine wave at times t:

from numpy import sin
def sin_model(t, freq, phase):
    return sin(2*pi*(freq*t + phase))

and given data (y,dy) measured at times t, we can define the fit problem as follows:

from bumps.names import *
M = Curve(sin_model, t, y, dy, freq=20)

The freq and phase keywords are optional initial values for the model parameters which otherwise default to zero. The model parameters can be accessed as attributes on the model to set fit range:

M.freq.range(2, 100)
M.phase.range(0, 1)

As usual, you can initialize or assign parameter expressions to the the parameters if you want to tie parameters together within or between models.

Note: there is sometimes difficulty getting bumps to recognize the function during fits, which can be addressed by putting the definition in a separate file on the python path. With the windows binary distribution of bumps, this can be done in the problem definition file with the following code:

import os
from bumps.names import *
sys.path.insert(0, os.getcwd())

The model function can then be imported from the external module as usual:

from sin_model import sin_model
class bumps.curve.Curve(fn, x, y, dy=None, name='', plot=None, **fnkw)[source]

Bases: object

Model a measurement with a user defined function.

The function fn(x,p1,p2,…) should return the expected value y for each point x given the parameters p1, p2, etc. dy is the uncertainty for each measured value y. If not specified, it defaults to 1. Initial values for the parameters can be set as p=value arguments to Curve. If no value is set, then the initial value will be taken from the default value given in the definition of fn, or set to 0 if the parameter is not defined with an initial value. Arbitrary non-fittable data can be passed to the function as parameters, but only if the parameter is given a default value of None in the function definition, and has the initial value set as an argument to Curve. Defining state=dict(key=value, …) before Curve, and calling Curve as Curve(…, **state) works pretty well.

Curve takes two special keyword arguments: name and plot. name is added to each parameter name when the parameter is defined. The filename for the data is a good choice, since this allows you to keep the parameters straight when fitting multiple datasets simultaneously.

Plotting defaults to a 1-D plot with error bars for the data, and a line for the function value. You can assign your own plot function with the plot keyword. The function should be defined as plot(x,y,dy,fy,**kw). The keyword arguments will be filled with the values of the parameters used to compute fy. It will be easiest to list the parameters you need to make your plot as positional arguments after x,y,dy,fy in the plot function declaration. For example, plot(x,y,dy,fy,p3,**kw) will make the value of parameter p3 available as a variable in your function. The special keyword view will be a string containing linear, log, logx or loglog.

The data uncertainty is assumed to follow a gaussian distribution. If measurements draw from some other uncertainty distribution, then subclass Curve and replace nllf with the correct probability given the residuals. See the implementation of PoissonCurve for an example.

nllf()[source]
numpoints()[source]
parameters()[source]
plot(view=None)[source]
residuals()[source]
save(basename)[source]
simulate_data(noise=None)[source]
theory(x=None)[source]
update()[source]
class bumps.curve.PoissonCurve(fn, x, y, name='', **fnkw)[source]

Bases: bumps.curve.Curve

Model a measurement with Poisson uncertainty.

The nllf is calculated using Poisson probabilities, but the curve itself is displayed using the approximation that \(\sigma_y \approx \sqrt(y)\).

See Curve for details.

nllf()[source]
numpoints()
parameters()
plot(view=None)
residuals()
save(basename)[source]
simulate_data(noise=None)[source]
theory(x=None)
update()
bumps.curve.plot_err(x, y, dy, fy, view=None, **kw)[source]

Plot data y and error dy against x.

view is one of linear, log, logx or loglog.