Class Function
source code
Class to define a function with associated parameters. The function
can be evaluated using the evaluate method. Parameters can be added/updated,
together with boundaries and expressions, and can be hold fixed and
released by adjusting the vary keyword in setup_parameters.
The provided function needs to take two arguments. The first one is a
list of parameters, the second is a list of x-values for which the
function will be evaluated. For example if you want to define a quadratic
function it will look like:
>>> func = lambda pars, x: pars[0] + pars[1] * x + pars[2] * x**2
Functions can be combined using the Model class, or
can be fitted directly to data using the Minimizer
class.
To improve the fit, a jacobian can be provided as well. However, some
care is nessessary when defining this function. When using the Minimizer
class to fit a Function to data, the residual function is defined as:
residuals = data - Function.evaluate()
To derive the jacobian you have to take the derivatives of -1 *
function. Furthermore the derivatives have to be across the rows. For the
example quadratic function above, the jacobian would look like:
>>> jacobian = lambda pars, x: np.array([-np.ones(len(x)), -x, -x**2]).T
If you get bad results, try flipping all signs. The jacobian does not
really improve the speed of the fitprocess, but it does help to reach the
minimum in a more consistent way (see examples).
The internal representation of the parameters uses a parameter object
of the lmfit package. No knowledge of this repersentation is
required as methods for direct interaction with the parameter values and
other settings are provided. If wanted, the parameters object itself can
be obtained with the parameters
attribute.
|
__init__(self,
function=None,
par_names=None,
jacobian=None,
resfunc=None)
Create a new Function by providing the function of which it consists
together with the names of each parameter in this function. |
source code
|
|
Inherited from object :
__delattr__ ,
__format__ ,
__getattribute__ ,
__hash__ ,
__new__ ,
__reduce__ ,
__reduce_ex__ ,
__repr__ ,
__setattr__ ,
__sizeof__ ,
__subclasshook__
|
numpy array
|
evaluate(self,
x,
*args,
**kwargs)
Evaluate the function for the given values and optional the given
parameter object. |
source code
|
|
|
|
|
setup_parameters(self,
value=None,
bounds=None,
vary=None,
expr=None,
**kwargs)
Create or adjust a parameter object based on the parameter names and
if provided the values, bounds, vary and expressions. |
source code
|
|
|
|
numpy arrays
|
get_parameters(self,
parameters=None,
error='stderr',
full_output=False)
Returns the parameter values together with the errors if they exist. |
source code
|
|
string
|
param2str(self,
**kwargs)
Converts the parameter object of this model to an easy printable
string, including the value, error, boundaries, if the parameter is
varied, and if in the fitting process on of the boundaries was
reached. |
source code
|
|
string
|
correl2str(self,
**kwargs)
Convert the correlations between the different parameters of this
function to an easy printable string. |
source code
|
|
string
|
ci2str(self,
**kwargs)
Convert the confidence intervals of the parameters of this model to
an easy printable string. |
source code
|
|
|
|
Inherited from object :
__class__
|
__init__(self,
function=None,
par_names=None,
jacobian=None,
resfunc=None)
(Constructor)
| source code
|
Create a new Function by providing the function of which it consists
together with the names of each parameter in this function. You can
specify the jacobian as well.
- Parameters:
function (function) - A function expression
par_names (list of strings) - The names of each parameter of function
jacobian (function) - A function expression for the jacobian of function.
resfunc (function) - Function to calculate the residuals. (Not obligatory)
- Overrides:
object.__init__
|
Evaluate the function for the given values and optional the given
parameter object. If no parameter object is given then the parameter
object belonging to the function is used.
>>>
>>>
- Parameters:
x (numpy array) - the independant data for which to evaluate the function
- Returns: numpy array
- Function(x), same size as x
|
Evaluates the jacobian if that function is provided, using the given
parameter object. If no parameter object is given then the parameter
object belonging to the function is used.
|
setup_parameters(self,
value=None,
bounds=None,
vary=None,
expr=None,
**kwargs)
| source code
|
Create or adjust a parameter object based on the parameter names and
if provided the values, bounds, vary and expressions. Basic checking if
the parameter boundaries are consistent is performed.
Example Use:
>>> setup_parameters(values=[v1, v2, ...], bounds=[(b1, b2), (b3, b4), ...], vary=[True, False, ...])
- Parameters:
values (array) - The values of the parameters
bounds (array of tuples) - min and max boundaries on the parameters [(min,max),...]
vary (array) - Boolean array, true to vary a parameter, false to keep it fixed
in the fitting process
exprs (array) - array of expressions that the paramters have to follow
|
update_parameter(self,
parameter=None,
**kwargs)
| source code
|
Updates a specified parameter. The parameter can be given by name or
by index. This method also supports the use of min and max keywords to
set the lower and upper boundary seperatly.
Example Use:
>>> update_parameter(parameter='parname', value=10.0, min=5.0, vary=True)
>>> update_parameter(parameter=2, value=0.15, bounds=(0.10, 0.20))
|
get_parameters(self,
parameters=None,
error='stderr',
full_output=False)
| source code
|
Returns the parameter values together with the errors if they exist.
If No fitting has been done, or the errors could not be calculated, None
is returned for the error.
The parameters of which the settings should be returned can be given
in parameters. If None is given, all parameters are returned.
- Parameters:
parameters (array) - Parameters to be returned or None for all parameters.
error (string) - Which error to return ('stderr', 'mcerr')
full_output (bool) - When True, also vary, the boundaries and expression are returned
- Returns: numpy arrays
- the parameter values and there errors: value, err, [vary, min,
max, expr]
|
Converts the parameter object of this model to an easy printable
string, including the value, error, boundaries, if the parameter is
varied, and if in the fitting process on of the boundaries was
reached.
The error to be printed can be set with the error keyword. You can
chose from the standard error: 'stderr', the monte carlo error: 'mcerr',
or any of the confidence intervalls that you have calculated by coding
them like: 'ci###'. Fx: 95% (sigma = 0.95) use 'ci95', for 99.7% (sigma =
0.997) use 'ci997'. Don't put decimal signs in the ci!
The accuracy with which the parameters are printed can be set with the
accuracy keyword. And the amount of information that is printed is
determined by full_output. If False, only parameter value and error are
printed, if True also boundaries and vary are shown.
- Parameters:
accuracy (int) - Number of decimal places to print
error (string) - Which error type to print ('stderr', 'mcerr' or 'ci###')
full_output (bool) - Amount of information to print
- Returns: string
- parameters in string format
|
Convert the correlations between the different parameters of this
function to an easy printable string. The correlations are only printed
if they are larger than a certain provided limit. And the accuracy
keyword sets the amount of decimals to print
- Parameters:
accuracy - number of decimal places to print
limit - minimum correlation value to print
- Returns: string
- correlation in string format
|
Convert the confidence intervals of the parameters of this model to an
easy printable string.
The accuracy with wich the CIs should be printed can be set with the
accuracy keyword.
- Parameters:
accuracy (int) - Number of decimal places to print
- Returns: string
- confidence intervals in string format
|
__str__(self)
(Informal representation operator)
| source code
|
String representation of the Function object
- Overrides:
object.__str__
|