Commit 8c265765 authored by Andrew Quinn's avatar Andrew Quinn

Add first pass at helper to compute fits on dimensionality reduced models

parent 941ca696
Pipeline #25370 passed with stage
in 1 minute and 51 seconds
......@@ -573,6 +573,41 @@ def sliding_window_fit(model_class, data, delay_vect,
__all__.append('sliding_window_fit')
def pca_reduced_fit(X, delay_vect, ndim, linear_model=VieiraMorfLinearModel):
""" First pass at a helper for computing an MVAR on dimensionality reduced
data. Returns a model fitted to reduced data, the reduced model projected
back to original data dimensions and the pca object used for reduction
"""
# PCA requires [samples x channels] whereas data is [channels x samples]
from .utils import PCA
pc = PCA(X.T, ndim)
Xred = pc.scores.T
Xred = Xred[:, :, None]
# Fit MVAR to dim-reduced data
red_model = linear_model.fit_model(Xred, delay_vect)
# Project back to data dimensions
Afull = np.zeros((X.shape[0], X.shape[0], len(delay_vect)))
Afull[:, :, 0] = -np.eye(X.shape[0]) # Don't project identity at start
for ii in range(1, len(delay_vect)):
Afull[:, :, ii] = np.dot(pc.components.T, red_model.parameters[:, :, ii]).dot(pc.components)
resid_cov_full = np.dot(pc.components.T, red_model.resid_cov[:, :]).dot(pc.components)[:, :, None]
# Store projected model
proj_model = AbstractLinearModel()
proj_model.parameters = Afull
proj_model.resid_cov = resid_cov_full
proj_model.delay_vect = delay_vect
return red_model, proj_model, pc
__all__.append('pca_reduced_fit')
def coloured_noise_fit(X, model_order):
"""A helper function to fit an autoregressive model whose parameters are
constrained to be coloured noise as definede by [Kasdin1995]_.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment