rsatoolbox.model.fitter module

Parameter fitting methods for models

class rsatoolbox.model.fitter.Fitter(fit_fun, **kwargs)[source]

Bases: object

Object to specify a fitting function and parameters

Effectively this gives the user a convenient way to specify fitting functions with different settings than the defaults.

Create this object with the fitting function to use and additional keyword arguments for settings you wish to change. The resulting object then behaves as the fitting function itself with the keyword arguments set to the different values provided at object creation.

Example

generate Fitter-object for ridge regression:

fit = pyrsa.model.Fitter(pyrsa.model.fit_regress, ridge_weight=1)

the resulting object ‘fit’ now does the same as pyrsa.model.fit_regress when run with the additional argument ridge_weight=1, i.e. the following two lines now yield equal results:

fit(model, data)
pyrsa.model.fit_regress(model, data, ridge_weight=1)

For a general introduction to flexible models see demo_flex.

rsatoolbox.model.fitter.fit_interpolate(model, data, method='cosine', pattern_idx=None, pattern_descriptor=None, sigma_k=None)[source]

fitting theta using bisection optimization allowed for ModelInterpolate only

Parameters:
  • model (Model) – the model to be fit

  • data (rsatoolbox.rdm.RDMs) – data to be fit

  • method (String, optional) – evaluation metric The default is ‘cosine’.

  • pattern_idx (numpy.ndarray, optional) – sampled patterns The default is None.

  • pattern_descriptor (String, optional) – descriptor used for fitting. The default is None.

  • sigma_k (matrix) – pattern-covariance matrix used only for whitened distances (ending in _cov) to compute the covariance matrix for rdms

Returns:

theta, parameter vector for the model

Return type:

numpy.ndarray

rsatoolbox.model.fitter.fit_mock(model, data, method='cosine', pattern_idx=None, pattern_descriptor=None, sigma_k=None)[source]

formally acceptable fitting method which always returns a vector of zeros

Parameters:
  • model (rsatoolbox.model.Model) – model to be fit

  • data (rsatoolbox.rdm.RDMs) – Data to fit to

  • method (String) – Evaluation method

  • pattern_idx (numpy.ndarray) – Which patterns are sampled

  • pattern_descriptor (String) – Which descriptor is used

  • sigma_k (matrix) – pattern-covariance matrix used only for whitened distances (ending in _cov) to compute the covariance matrix for rdms

Returns:

parameter vector

Return type:

theta(numpy.ndarray)

rsatoolbox.model.fitter.fit_optimize(model, data, method='cosine', pattern_idx=None, pattern_descriptor=None, sigma_k=None, ridge_weight=0)[source]

fitting theta using optimization currently allowed for ModelWeighted only

Parameters:
  • model (Model) – the model to be fit

  • data (rsatoolbox.rdm.RDMs) – data to be fit

  • method (String, optional) – evaluation metric The default is ‘cosine’.

  • pattern_idx (numpy.ndarray, optional) – sampled patterns The default is None.

  • pattern_descriptor (String, optional) – descriptor used for fitting. The default is None.

  • sigma_k (matrix) – pattern-covariance matrix used only for whitened distances (ending in _cov) to compute the covariance matrix for rdms

Returns:

theta, parameter vector for the model

Return type:

numpy.ndarray

rsatoolbox.model.fitter.fit_optimize_positive(model, data, method='cosine', pattern_idx=None, pattern_descriptor=None, sigma_k=None, ridge_weight=0)[source]

fitting theta using optimization enforcing positive weights currently allowed for ModelWeighted only

Parameters:
  • model (Model) – the model to be fit

  • data (pyrsa.rdm.RDMs) – data to be fit

  • method (String, optional) – evaluation metric The default is ‘cosine’.

  • pattern_idx (numpy.ndarray, optional) – sampled patterns The default is None.

  • pattern_descriptor (String, optional) – descriptor used for fitting. The default is None.

  • sigma_k (matrix) – pattern-covariance matrix used only for whitened distances (ending in _cov) to compute the covariance matrix for rdms

Returns:

theta, parameter vector for the model

Return type:

numpy.ndarray

rsatoolbox.model.fitter.fit_regress(model, data, method='cosine', pattern_idx=None, pattern_descriptor=None, ridge_weight=0, sigma_k=None)[source]

fitting theta using linear algebra solutions to the OLS problem allowed for ModelWeighted only This method first normalizes the data and model RDMs appropriately for the measure to be optimized. For ‘cosine’ similarity this is a normalization of the data-RDMs to vector length 1. For correlation the mean is removed from both model and data rdms additionally. Then the parameters are estimated using ordinary least squares.

Parameters:
  • model (Model) – the model to be fit

  • data (pyrsa.rdm.RDMs) – data to be fit

  • method (String, optional) – evaluation metric The default is ‘cosine’.

  • pattern_idx (numpy.ndarray, optional) – sampled patterns The default is None.

  • pattern_descriptor (String, optional) – descriptor used for fitting. The default is None.

  • ridge_weight (float, default=0) – weight for the ridge-regularisation of the regression weight is in comparison to the final regression problem on the appropriately normalized regressors

  • sigma_k (matrix) – pattern-covariance matrix used only for whitened distances (ending in _cov) to compute the covariance matrix for rdms

Returns:

theta, parameter vector for the model

Return type:

numpy.ndarray

rsatoolbox.model.fitter.fit_regress_nn(model, data, method='cosine', pattern_idx=None, pattern_descriptor=None, ridge_weight=0, sigma_k=None)[source]

fitting theta using linear algebra solutions to the OLS problem allowed for ModelWeighted only This method first normalizes the data and model RDMs appropriately for the measure to be optimized. For ‘cosine’ similarity this is a normalization of the data-RDMs to vector length 1. For correlation the mean is removed from both model and data rdms additionally. Then the parameters are estimated using ordinary least squares.

Parameters:
  • model (Model) – the model to be fit

  • data (pyrsa.rdm.RDMs) – data to be fit

  • method (String, optional) – evaluation metric The default is ‘cosine’.

  • pattern_idx (numpy.ndarray, optional) – sampled patterns The default is None.

  • pattern_descriptor (String, optional) – descriptor used for fitting. The default is None.

  • ridge_weight (float, default=0) – weight for the ridge-regularisation of the regression weight is in comparison to the final regression problem on the appropriately normalized regressors

  • sigma_k (matrix) – pattern-covariance matrix used only for whitened distances (ending in _cov) to compute the covariance matrix for rdms

Returns:

theta, parameter vector for the model

Return type:

numpy.ndarray

rsatoolbox.model.fitter.fit_select(model, data, method='cosine', pattern_idx=None, pattern_descriptor=None, sigma_k=None)[source]

fits selection models by evaluating each rdm and selcting the one with best performance. Works only for ModelSelect

Parameters:
  • model (rsatoolbox.model.Model) – model to be fit

  • data (rsatoolbox.rdm.RDMs) – Data to fit to

  • method (String) – Evaluation method

  • pattern_idx (numpy.ndarray) – Which patterns are sampled

  • pattern_descriptor (String) – Which descriptor is used

  • sigma_k (matrix) – pattern-covariance matrix used only for whitened distances (ending in _cov) to compute the covariance matrix for rdms

Returns:

parameter vector

Return type:

theta(int)