Fitting

Functions for fitting

pyddm.functions.fit_adjust_model(sample, model, fitparams=None, fitting_method='differential_evolution', lossfunction=<class 'pyddm.models.loss.LossLikelihood'>, verify=False, method=None, verbose=True)[source]

Modify parameters of a model which has already been fit.

The data sample should be a Sample object of the reaction times to fit in seconds (NOT milliseconds). At least one of the parameters for one of the components in the model should be a “Fitted()” instance, as these will be the parameters to fit.

fitting_method specifies how the model should be fit. “differential_evolution” is the default, which accurately locates the global maximum without using a derivative. “simple” uses a derivative-based method to minimize, and just uses randomly initialized parameters and gradient descent. “simplex” is the Nelder-Mead method, and is a gradient-free local search. “basin” uses “scipy.optimize.basinhopping” to find an optimal solution, which is much slower but also gives better results than “simple”. It does not appear to give better or faster results than “differential_evolution” in most cases. Alternatively, a custom objective function may be used by setting fitting_method to be a function which accepts the “x_0” parameter (for starting position) and “constraints” (for min and max values). In general, it is recommended you almost always use differential evolution, unless you have a model which is highly-constrained (e.g. only one or two parameters to estimate with low covariance) or you already know the approximate parameter values. In practice, besides these two special cases, changing the method is unlikely to give faster or more reliable estimation.

fitparams is a dictionary of kwargs to be passed directly to the minimization routine for fine-grained low-level control over the optimization. Normally this should not be needed.

lossfunction is a subclass of LossFunction representing the method to use when calculating the goodness-of-fit. Pass the subclass itself, NOT an instance of the subclass.

name gives the name of the model after it is fit.

If verify is False (the default), checking for programming errors is disabled during the fit. This can decrease runtime and may prevent crashes. If verification is already disabled, this does not re-enable it.

method gives the method used to solve the model, and can be “analytical”, “numerical”, “cn”, “implicit”, or “explicit”.

verbose enables out-of-boundaries warnings and prints the model information at each evaluation of the fitness function.

Returns the same model object that was passed to it as an argument. However, the parameters will be modified. The model is modified in place, so a reference is returned to it for convenience only.

After running this function, the model will be modified to include a “FitResult” object, accessed as m.fitresult. This can be used to get the value of the objective function, as well as to access diagnostic information about the fit.

This function will automatically parallelize if set_N_cpus() has been called.

pyddm.functions.fit_model(sample, drift=DriftConstant(drift=0), noise=NoiseConstant(noise=1), bound=BoundConstant(B=1), IC=ICPointSourceCenter(), dt=0.005, dx=0.005, fitparams=None, fitting_method='differential_evolution', method=None, overlay=OverlayNone(), lossfunction=<class 'pyddm.models.loss.LossLikelihood'>, verbose=True, name='fit_model', verify=False)[source]

Fit a model to reaction time data.

The data sample should be a Sample object of the reaction times to fit in seconds (NOT milliseconds). This function will generate a model using the drift, noise, bound, and IC parameters to specify the model. At least one of these should have a parameter which is a “Fittable()” instance, as this will be the parameter to be fit.

Optionally, dt specifies the temporal resolution with which to fit the model.

method specifies how the model should be fit. “differential_evolution” is the default, which accurately locates the global maximum without using a derivative. “simple” uses a derivative-based method to minimize, and just uses randomly initialized parameters and gradient descent. “simplex” is the Nelder-Mead method, and is a gradient-free local search. “basin” uses “scipy.optimize.basinhopping” to find an optimal solution, which is much slower but also gives better results than “simple”. It does not appear to give better or faster results than “differential_evolution” in most cases. Alternatively, a custom objective function may be used by setting method to be a function which accepts the “x_0” parameter (for starting position) and “constraints” (for min and max values). In general, it is recommended you almost always use differential evolution, unless you have a model which is highly-constrained (e.g. only one or two parameters to estimate with low covariance) or you already know the approximate parameter values. In practice, besides these two special cases, changing the method is unlikely to give faster or more reliable estimation.

fitparams is a dictionary of kwargs to be passed directly to the minimization routine for fine-grained low-level control over the optimization. Normally this should not be needed.

lossfunction is a subclass of LossFunction representing the method to use when calculating the goodness-of-fit. Pass the subclass itself, NOT an instance of the subclass.

name gives the name of the model after it is fit.

If verify is False (the default), checking for programming errors is disabled during the fit. This can decrease runtime and may prevent crashes. If verification is already disabled, this does not re-enable it.

verbose enables out-of-boundaries warnings and prints the model information at each evaluation of the fitness function.

Returns a “Model()” object with the specified drift, noise, bound, IC, and overlay.

The model will include a “FitResult” object, accessed as m.fitresult. This can be used to get the value of the objective function, as well as to access diagnostic information about the fit.

This function will automatically parallelize if set_N_cpus() has been called.

Fit result

class pyddm.fitresult.FitResult(fitting_method, method, loss, value, **kwargs)[source]

An object to describe the result of a model fit.

This keeps track of information related to the fitting procedure. It has the following elements:

  • method: the name of the solver used to solve the model, e.g. “analytical” or “implicit”
  • fitting_method: the name of the algorithm used to minimize the loss function method (e.g. “differential_evolution”)
  • loss: the name of the loss function (e.g. “BIC”)
  • properties: a dictionary containing any additional values saved by the loss function or fitting procedure (e.g. “likelihood” for BIC loss function, or “mess” for a message describing the output).

So, for example, can access FitResult.method to get the name of the numerical algorithm used to solve the equation.

To access the output value of the loss function, use FitResult.value().

value()[source]

Returns the objective function value of the fit.

If there was an error, or if no fit was performed, return inf.

class pyddm.fitresult.FitResultEmpty[source]

A default Fit object before a model has been fit.