curve - Model a fit function

Curve Model a measurement with a user defined function.
PoissonCurve Model a measurement with Poisson uncertainty.
plot_err DEPRECATED: subclass Curve and override the plot function.

Build a bumps model from a function and data.


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='', labels=None, plot=None, plot_x=None, **kwargs)[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. Multi-valued functions, which return multiple y values for each x value, should have x as a vector of length n and y, dy as arrays of size [n, k].

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 the following special keyword arguments:

  • 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.
  • plot is an alternative plotting function. 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. If only showing the residuals, the string will be residual.
  • plot_x is an array giving the sample points to use when plotting the theory function, if different from the x values at which the function is sampled. Use this to draw a smooth curve between the fitted points. This value is ignored if you provide your own plot function.
  • labels are the axis labels for the plot. This should include units in parentheses. If the function is multi-valued then use [‘x axis’, ‘y axis’, ‘line 1’, ‘line 2’, …].

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.

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.

bumps.curve.plot_err(x, y, dy, fy, view=None, **kw)[source]

DEPRECATED: subclass Curve and override the plot function.

Plot data y and error dy against x.

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