fitAlgs.fitAlg module

Author:Dominic Hunt
class fitAlgs.fitAlg.FitAlg(fit_sim=None, fit_measure='-loge', fit_measure_args=None, extra_fit_measures=None, bounds=None, boundary_excess_cost=None, boundary_excess_cost_properties=None, bound_ratio=1e-06, calculate_covariance=False, **kwargs)[source]

Bases: object

The abstract class for fitting data

Parameters:
  • fit_sim (fitAlgs.fitSims.FitSim instance, optional) – An instance of one of the fitting simulation methods. Default fitAlgs.fitSims.FitSim
  • fit_measure (string, optional) – The name of the function used to calculate the quality of the fit. The value it returns provides the fitter with its fitting guide. Default -loge
  • fit_measure_args (dict, optional) – The parameters used to initialise fit_measure and extra_fit_measures. Default None
  • extra_fit_measures (list of strings, optional) – List of fit measures not used to fit the model, but to provide more information. Any arguments needed for these measures should be placed in fit_measure_args. Default None
  • bounds (dictionary of tuples of length two with floats, optional) – The boundaries for methods that use bounds. If unbounded methods are specified then the bounds will be ignored. Default is None, which translates to boundaries of (0, np.inf) for each parameter.
  • boundary_excess_cost (str or callable returning a function, optional) – The function is used to calculate the penalty for exceeding the boundaries. Default is boundFunc.scalarBound()
  • boundary_excess_cost_properties (dict, optional) – The parameters for the boundary_excess_cost function. Default {}
  • calculate_covariance (bool, optional) – Is the covariance calculated. Default False
Name

The name of the fitting method

Type:string

See also

fitAlgs.fitSims.fitSim
The general fitting class
covariance(model_parameter_names, paramvals, fitinfo)[source]

The covariance at a point

Parameters:
  • paramvals (array or list) – The parameters at which the
  • fitinfo (dict) – The
Returns:

covariance – The covariance at the point paramvals

Return type:

float

extra_measures(*model_parameter_values)[source]
Parameters:*model_parameter_values (array of floats) – The parameters proposed by the fitting algorithm
Returns:fit_quality – The fit quality value calculated using the fit quality functions described in extraMeasures
Return type:dict of float
find_name()[source]

Returns the name of the class

fit(simulator, model_parameter_names, model_initial_parameters)[source]

Runs the model through the fitting algorithms and starting parameters and returns the best one. This is the abstract version that always returns (0,0)

Parameters:
  • simulator (function) – The function used by a fitting algorithm to generate a fit for given model parameters. One example is fitAlgs.fitAlg.fitness
  • model_parameter_names (list of strings) – The list of initial parameter names
  • model_initial_parameters (list of floats) – The list of the initial parameters
Returns:

  • best_fit_parameters (list of floats) – The best fitting parameters
  • fit_quality (float) – The quality of the fit as defined by the quality function chosen.
  • tested_parameters (tuple of two lists) – The two lists are a list containing the parameter values tested, in the order they were tested, and the fit qualities of these parameters.

See also

fitAlgs.fitAlg.fitness()

fitness(*params)[source]

Generates a fit quality value used by the fitting function. This is the function passed to the fitting function.

Parameters:*params (array of floats) – The parameters proposed by the fitting algorithm
Returns:fit_quality – The fit quality value calculated using the fitQualFunc function
Return type:float

See also

fitAlgs.qualityFunc()
the module of fitQualFunc functions
fitAlg.invalidParams()
Checks if the parameters are valid and if not returns inf
fitAlgs.fitSims.fitSim.fitness()
Runs the model simulation and returns the values used to calculate the fit quality
info()[source]

The information relating to the fitting method used

Includes information on the fitting algorithm used

Returns:info – The fitSims info and the fitAlgs.fitAlg info
Return type:(dict,dict)

See also

fitAlg.fitSims.fitSim.info()

invalid_parameters(*params)[source]

Identifies if the parameters passed are within the bounds provided

If they are not returns inf

Parameters:params (list of floats) – Parameters to be passed to the sim
Returns:validity – If the parameters are valid or not
Return type:Bool

Notes

No note

Examples

>>> a = FitAlg(bounds={1:(0,5), 2:(0,2), 3:(-1,1)})
>>> a.set_bounds([3, 1])
>>> a.invalid_parameters(0, 0)
False
>>> a.invalid_parameters(2, 0)
True
>>> a.invalid_parameters(0, -1)
True
>>> a.invalid_parameters(6, 6)
True
participant(model, model_parameters, model_properties, participant_data)[source]

Fit participant data to a model for a given task

Parameters:
  • model (model.modelTemplate.Model inherited class) – The model you wish to try and fit values to
  • model_parameters (dict) – The model initial parameters
  • model_properties (dict) – The model static properties
  • participant_data (dict) – The participant data
Returns:

  • model (model.modelTemplate.Model inherited class instance) – The model with the best fit parameters
  • fit_quality (float) – Specifies the fit quality for this participant to the model
  • fitting_data (tuple of OrderedDict and list) – They are an ordered dictionary containing the parameter values tested, in the order they were tested, and the fit qualities of these parameters.

set_bounds(model_parameter_names)[source]

Checks if the bounds have changed

Parameters:model_parameter_names (list of strings) – An ordered list of the names of the parameters to be fitted

Examples

>>> a = FitAlg(bounds={1: (0, 5), 2: (0, 2), 3: (-1, 1)})
>>> a.boundaries
{1: (0, 5), 2: (0, 2), 3: (-1, 1)}
>>> a.set_bounds([])
>>> a.boundaries
{1: (0, 5), 2: (0, 2), 3: (-1, 1)}
>>> a.boundary_names
[]
>>> a.set_bounds([3,1])
>>> a.boundary_values
[(-1, 1), (0, 5)]
>>> a.set_bounds([2,1])
>>> a.boundary_values
[(0, 2), (0, 5)]
classmethod startParams(initial_parameters, bounds=None, number_starting_points=3)[source]

Defines a list of different starting parameters to run the minimization over

Parameters:
  • initial_parameters (list of floats) – The initial starting values proposed
  • bounds (list of tuples of length two with floats, optional) – The boundaries for methods that use bounds. If unbounded methods are specified then the bounds will be ignored. Default is None, which translates to boundaries of (0,float(‘Inf’)) for each parameter.
  • number_starting_points (int) – The number of starting parameter values to be calculated around each initial point
Returns:

startParamSet – The generated starting parameter combinations

Return type:

list of list of floats

See also

FitAlg.start_parameter_values()
Used in this function

Examples

>>> FitAlg.startParams([0.5,0.5], number_starting_points=2)
array([[0.33333333, 0.33333333],
       [0.66666667, 0.33333333],
       [0.33333333, 0.66666667],
       [0.66666667, 0.66666667]])
static start_parameter_values(initial, boundary_min=-inf, boundary_max=inf, number_starting_points=3)[source]

Provides a set of starting points

Parameters:
  • initial (float) – The initial starting value proposed
  • boundary_min (float, optional) – The minimum value of the parameter. Default is float('-Inf')
  • boundary_max (float, optional) – The maximum value of the parameter. Default is float('Inf')
  • number_starting_points (int) – The number of starting parameter values to be calculated around the inital point
Returns:

startParams – The generated starting parameters

Return type:

list of floats

Notes

For each starting parameter provided a set of numStartPoints starting points will be chosen, surrounding the starting point provided. If the starting point provided is less than one but greater than zero it will be assumed that the values cannot leave those bounds, otherwise, unless otherwise told, it will be assumed that they can take any positive value and will be chosen to be eavenly spaced around the provided value.

Examples

>>> FitAlg.start_parameter_values(0.5)
array([0.25, 0.5 , 0.75])
>>> FitAlg.start_parameter_values(5)
array([2.5, 5. , 7.5])
>>> FitAlg.start_parameter_values(-5)
array([2.5, 5. , 7.5])
>>> FitAlg.start_parameter_values(5, boundary_min = 0, boundary_max = 7)
array([4., 5., 6.])
>>> FitAlg.start_parameter_values(5, boundary_min = -3, boundary_max = 30)
array([1., 5., 9.])
>>> FitAlg.start_parameter_values(5, boundary_min = 0, boundary_max = 30)
array([2.5, 5. , 7.5])
>>> FitAlg.start_parameter_values(5, boundary_min = 3, boundary_max = 30, number_starting_points = 7)
array([3.5, 4. , 4.5, 5. , 5.5, 6. , 6.5])
fitAlgs.fitAlg.covariance(jac)[source]

Calculates the covariance based on the estimated jacobian

Inspired by how this is calculated in scipy.optimise.curve_fit, as found at https://github.com/scipy/scipy/blob/2526df72e5d4ca8bad6e2f4b3cbdfbc33e805865/scipy/optimize/minpack.py#L739