Package ivs :: Package sigproc :: Module fit :: Class Minimizer
[hide private]
[frames] | no frames]

Class Minimizer

source code


A minimizer class on the lmfit Python package, which provides a simple, flexible interface to non-linear least-squares optimization, or curve fitting. The package is build around the Levenberg-Marquardt algorithm of scipy, but 2 other minimizers: limited memory Broyden-Fletcher-Goldfarb-Shanno and simulated annealing are partly supported as well. For the Levenberg-Marquardt algorithm, the estimated uncertainties and correlation between fitted variables are calculated as well.

This minimizer finds the best parameters to fit a given Model or Function to a set of data. You only need to provide the Model and data. Weighted fitting is supported.

This minimizer uses the basic residual function:

   residuals = ( data - model(x) ) * weights

If a more advanced residual functions is required, fx when working with multi dimentional data, the used can specify its own residual function in the provided Function or Model, or by setting the resfunc keyword. This residual function needs to be of the following call sign:

   resfunc(synth, data, weights=None, errors=None, **kwargs)

Functions

A Function is basicaly a function together with a list of the parameters that is needs. In the internal representation the parameters are represented as a Parameters object. However, the user does not need to now how to handle this, and can just provided or retrieve the parameter values using arrays. Every fitting parameter are extensions of simple numerical variables with the following properties:

Models

A Model is a combination of Functions with its own parameter set. The Functions are provided as a list, and a gamma function can be given to describe how the functions are combined. Methods to handle the parameters in a model are provided, but the user is recommended handle the parameters at Function level as the naming of the parameters changes at Model level.

Instance Methods [hide private]
 
__init__(self, x, y, model, errors=None, weights=None, resfunc=None, engine='leastsq', args=None, kws=None, grid_points=1, grid_params=None, verbose=False, **kwargs)
x.__init__(...) initializes x; see help(type(x)) for signature
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __subclasshook__

    Error determination
dict
calculate_CI(self, parameters=None, sigmas=[0.654,0.95,0.997], maxiter=200, **kwargs)
Returns the confidence intervalls of the given parameters.
source code
(array, array, 2d array)
calculate_CI_2D(self, xpar=None, ypar=None, res=10, limits=None, ctype='prob')
Calculates the confidence interval for 2 given parameters.
source code
array or dict
calculate_MC_error(self, points=100, errors=None, distribution='gauss', short_output=True, verbose=True, **kwargs)
Use Monte-Carlo simulations to estimate the error of each parameter.
source code
    Plotting Functions
 
plot_fit(self, points=1000, axis=0, legend=False, **kwargs)
Plot the original data together with the best fit results.
source code
 
plot_residuals(self, axis=0, legend=False, **kwargs)
Plot the residuals of the best fit.
source code
 
plot_results(self, points=1000, axis=0, legend=False, **kwargs)
Creates a basic plot with the fit results and corresponding residuals.
source code
 
plot_confidence_interval(self, xpar=None, ypar=None, res=10, limits=None, ctype='prob', filled=True, **kwargs)
Plot the confidence interval for 2 given parameters.
source code
 
plot_grid_convergence(self, xpar=None, ypar=None, chi2lim=None, chi2scale='log', show_colorbar='True', **kwargs)
Plot the convergence path of the results from grid_minimize stored in this minimizer.
source code
    Advanced attributes
 
minimizer(self, val)
set minimizer
source code
 
errors(self, val)
set error
source code
 
grid(self)
get minimizer grid
source code
    Internal Functions
 
__getattr__(self, name)
allow to reach the attributes of the best fitting minimizer directly
source code
 
_setup_residual_function(self)
Internal function to setup the residual function for the minimizer.
source code
 
_setup_jacobian_function(self)
Internal function to setup the jacobian function for the minimizer.
source code
 
_prepare_minimizer(self, fcn_args, fcn_kws, grid_points=1, grid_params=None, append=False)
Internal function to prepare the minimizer
source code
 
_start_minimize(self, engine, verbose=False, **kwargs)
Internal function that starts all minimizers one by one
source code
 
_perturb_input_data(self, points, **kwargs)
Internal function to perturb the input data for MC simulations
source code
 
_mc_error_from_parameters(self, params)
Use standard deviation to get the error on a parameter
source code
 
__str__(self)
String representation of the Minimizer object
source code
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, x, y, model, errors=None, weights=None, resfunc=None, engine='leastsq', args=None, kws=None, grid_points=1, grid_params=None, verbose=False, **kwargs)
(Constructor)

source code 

x.__init__(...) initializes x; see help(type(x)) for signature

Overrides: object.__init__
(inherited documentation)

calculate_CI(self, parameters=None, sigmas=[0.654,0.95,0.997], maxiter=200, **kwargs)

source code 

Returns the confidence intervalls of the given parameters. This function uses the F-test method described below to calculate confidence intervalls. The sigma parameter describes which confidence level is required in percentage: sigma=0.654 corresponds with the standard 1 sigma level, 0.95 with 2 sigma and 0.997 with 3 sigma.

The output is a dictionary containing for each parameter the lower and upper boundary of the asked confidence level. If short_output is True, an array of tupples is returned instead. When only one parameter is given, and short_output is True, only a tupple of the lower and upper boundary is returned.

The confidence intervalls calculated with this function are stored in the Model or Function as well.

F-test

The F-test is used to compare the null model, which is the best fit found by the minimizer, with an alternate model, where one of the parameters is fixed to a specific value. The value is changed util the differnce between chi2_0 and chi2_f can't be explained by the loss of a degree of freedom with a certain confidence.

F = (chi2_f / chi2_0 - 1) * (N-P)/P_fix

N is the number of data-points, P the number of parameter of the null model. P_fix is the number of fixed parameters (or to be more clear, the difference of number of parameters betweeen the null model and the alternate model).

This method relies completely on the I(conf_interval) method of the lmfit package.

Parameters:
  • parameters (array of strings) - Names of the parameters to calculate the CIs from (if None, all parameters are used)
  • sigmas (array or float) - The probability levels used to calculate the CI
Returns: dict
the confidence intervals.

calculate_CI_2D(self, xpar=None, ypar=None, res=10, limits=None, ctype='prob')

source code 

Calculates the confidence interval for 2 given parameters. Both the confidence interval calculated using the F-test method from the estimate_error method, and the normal chi squares can be obtained using the ctype keyword.

The confidence intervall is returned as a grid, together with the x and y distribution of the parameters: (x-values, y-values, grid)

Parameters:
  • xname - The parameter on the x axis
  • yname - The parameter on the y axis
  • res - The resolution of the grid over which the confidence intervall is calculated
  • limits - The upper and lower limit on the parameters for which the confidence intervall is calculated. If None, 5 times the stderr is used.
  • ctype - 'prob' for probabilities plot (using F-test), 'chi2' for chi-squares.
Returns: (array, array, 2d array)
the x values, y values and confidence values

calculate_MC_error(self, points=100, errors=None, distribution='gauss', short_output=True, verbose=True, **kwargs)

source code 

Use Monte-Carlo simulations to estimate the error of each parameter. In this approach each datapoint is perturbed by its error, and for each new dataset best fitting parameters are calculated. The MC error of a parameter is its deviation over all iterations (points).

The errors supplied to this function will overwrite the errors already stored in this Minimizer. If however no errors are supplied, the already stored ones will be used. For now only symetric errors are supported.

Currently all datapoints are considered to have a symetric gaussian distribution, but in future version more distributions will be supported.

The MC errors are saved in the Model or Function supplied to this fitter, and can be returned as an array (short_output=True), or as a dictionary (short_output=False).

Parameters:
  • points (int) - The number of itterations
  • errors (array or float) - Possible new errors on the input data.
  • distribution (str) - Not yet supported
  • short_output (bool) - True if you want array, False if you want dictionary
Returns: array or dict
The MC errors of all parameters.

plot_fit(self, points=1000, axis=0, legend=False, **kwargs)

source code 

Plot the original data together with the best fit results. This method has some functionality to plot multi-dimensional data, but is limited to 2D data which it will plot consecutively allong the specified axis.

Parameters:
  • points (int) - Number of points to use when plotting the best fit model.
  • axis (int) - In case of multi-dim input along which axis to split (0 or 1)
  • legend (bool) - Display legend on plot

plot_residuals(self, axis=0, legend=False, **kwargs)

source code 

Plot the residuals of the best fit. This method has some functionality to plot multi-dimensional data, but is limited to 2D data which it will plot consecutively allong the specified axis.

Parameters:
  • axis (int) - In case of multi-dim input along which axis to split (0 or 1)
  • legend (bool) - Display legend on plot

plot_results(self, points=1000, axis=0, legend=False, **kwargs)

source code 

Creates a basic plot with the fit results and corresponding residuals. This method has some functionality to plot multi-dimensional data, but is up till now limited to 2D data which it will plot consecutively allong the specified axis. Based on the plot_fit and plot_residuals functions

Parameters:
  • points (int) - Number of points to use when plotting the best fit model.
  • axis (int) - In case of multi-dim input along which axis to split (0 or 1)
  • legend (bool) - Display legend on plot
  • xlabel (str) - label for the x-axis
  • ylabel (str) - label for the y-axis
  • title (str) - title of the plot

plot_confidence_interval(self, xpar=None, ypar=None, res=10, limits=None, ctype='prob', filled=True, **kwargs)

source code 

Plot the confidence interval for 2 given parameters. Both the confidence interval calculated using the F-test method from the estimate_error method, and the normal chi squares can be plotted using the type keyword. In case of chi2, the log10 of the chi squares is plotted to improve the clarity of the plot.

Extra kwargs are passed to confourf or contour.

Parameters:
  • xname - The parameter on the x axis
  • yname - The parameter on the y axis
  • res - The resolution of the grid over which the confidence intervall is calculated
  • limits - The upper and lower limit on the parameters for which the confidence intervall is calculated. If None, 5 times the stderr is used.
  • ctype - 'prob' for probabilities plot (using F-test), 'chi2' for chisquare plot.
  • filled - True for filled contour plot, False for normal contour plot
  • limits - The upper and lower limit on the parameters for which the confidence intervall is calculated. If None, 5 times the stderr is used.

plot_grid_convergence(self, xpar=None, ypar=None, chi2lim=None, chi2scale='log', show_colorbar='True', **kwargs)

source code 

Plot the convergence path of the results from grid_minimize stored in this minimizer. The start values of the two selected parameters are plotted conected to there final best fit values, while using a color coding for the chisqr value of the fit result.

Parameters:
  • xpar - The parameter on the x axis
  • ypar - The parameter on the y axis
  • chi2lim - Optional limit on the chi2 value (in % of the maximum chi2)
  • chi2scale - Scale for the chi2 values: 'log' or 'linear'

minimizer(self, val)

source code 

set minimizer

Decorators:
  • @minimizer.setter

errors(self, val)

source code 

set error

Decorators:
  • @errors.setter

grid(self)

source code 

get minimizer grid

Decorators:
  • @property

__str__(self)
(Informal representation operator)

source code 

String representation of the Minimizer object

Overrides: object.__str__