Model Fitting

Model fitting is normally performed by either the sails.modelfit.VieiraMorfLinearModel or sails.modelfit.OLSLinearModel class. If you wish to fit a model using another method, an example of this can be found in :ref:tutorial8.

class sails.modelfit.VieiraMorfLinearModel[source]

A class implementing the Vieira-Morf linear model fit

classmethod fit_model(data, delay_vect)[source]

Estimates the multichannel autoregressive spectral estimators using a Vieira-Morf algorithm. Equations are referenced to [Marple1987], appendix 15.B.

This is the multitrial versions of the algorithm, using the AMVAR method outlined in [Ding2000].

Parameters
  • data (numpy.ndarray) – array of shape [nsignals, nsamples, ntrials]

  • delay_vect (numpy.ndarray) – Vector containing evenly spaced delays to be assessed in the model. Delays are represented in samples. Must start with zero.

Returns

A populated object containing the fitted forward and backwards coefficients and several other useful variables and methods.

Return type

sails.VieiraMorfLinearModel

get_residuals(data, forward_parameters=True, mode='valid')[source]

Returns the prediction error from a fitted model. This is a wrapper function for get_residuals()

Parameters
  • data (ndarray) – Data to compute the residuals from, of size [nchannels x nsamples x nrealisations]

  • forward_parameters (bool) – If True, use forward parameters, otherwise use backward parameters (Default value = True)

Returns

Residual data

Return type

ndarray

class sails.modelfit.OLSLinearModel[source]

A class implementing ordinary least squares linear model fit

classmethod fit_model(data, delay_vect, estimator=None)[source]

This is a class method which fits a linear model and returns a populated OLSLinearModel instance containing the fitted model.

Parameters
  • data (ndarray) – The data to be used to compute the model fit

  • delay_vect (ndarray) – A vector of lag indices defining the lags to fit

  • estimator (None or sklearn class) – If None, fit using standard OLS normal equations. If set to an appropriate sklearn class, use that estimator.

Returns

A populated object containing the fitted coefficients and several other useful variables and methods.

Return type

sails.OLSLinearModel

Model fitting helper functions

In addition to basic model fitting routines, some helper functions exist; for instance to help with fitting a number of models to a data series following a “sliding window” pattern or by applying PCA before the fit.

sails.modelfit.sliding_window_fit(model_class, data, delay_vect, win_len_samp, win_step_samp, compute_diagnostics=True)[source]

A helper function for fitting many MVAR models within sliding windows across a dataset.

Parameters
  • model_class (LinearModel) – The SAILS linear model typee to fit

  • data (ndarray) – Data to fit the model on, of size [nchannels x nsamples x nrealisations]

  • delay_vect (ndarray) – A vector of lags specifying which lags to fit

  • win_len_samp (int) – The window length in samples

  • win_step_samp (int) – The step size between windows in samples

  • compute_diagnostics (boolean) – Flag indicating whether to compute model diagnostics at each window (Default value = True)

Returns

An instance of linear model passed into the function containing MVAR parameters for all windows. The parameters are stored in model.parameters which is of size: [nchannels x nchannels x model order x nwindows]

Return type

LinearModel

sails.modelfit.pca_reduced_fit(X, delay_vect, ndim, linear_model=<class 'sails.modelfit.VieiraMorfLinearModel'>)[source]

Helper for computing an MVAR on dimensionality reduced data (using PCA).

Returns a model fitted to reduced data, the reduced model projected back to original data dimensions and the pca object used for reduction

Parameters
  • X (ndarray) – The data to compute the reduced model fit on

  • delay_vect (ndarray) – A vector of lags specifying which lags to fit

  • ndim (int) – Number of components to reduce data to.

  • linear_model (class) – Subclass of AbstractLinearModel to use for fit. Defaults to VieiraMorfLinearModel. (Default value = True)

Returns

  • red_model (AbstractLinearModel) – An instance of the linear model passed into the function containing MVAR parameters for all windows for the reduced model.

  • proj_model (AbstractLinearModel) – An instance of the linear model passed into the function containing MVAR parameters for all windows for the projected model.

  • pc (sails.utils.PCA) – PCA class with information about the PCA projection included