cogpy.regression

Linear regression primitives: lagged design matrices, OLS fit / predict / residual for signal decomposition and artifact removal.

Submodules

cogpy.regression.design

Design matrix construction primitives.

cogpy.regression.ols

Ordinary least-squares regression primitives.

Linear regression primitives for signal decomposition.

Provides atomic building blocks for constructing design matrices, fitting linear models, and subtracting predicted components from signals.

cogpy.regression.event_design_matrix(n_time, event_samples, template, *, intercept=True)

Build a design matrix from an event train and a template waveform.

Each column corresponds to one event and contains the template placed at the event onset. Useful for template-regression removal.

Parameters:
  • n_time (int) – Total number of time samples.

  • event_samples ((n_events,) int) – Sample indices of event onsets.

  • template ((n_lag,) float) – Template waveform (1-D).

  • intercept (bool) – If True (default), prepend a column of ones.

Returns:

X – Design matrix. Sparse in practice but returned dense. Columns beyond the intercept contain the placed template.

Return type:

(n_time, n_events + int(intercept)) float

cogpy.regression.lagged_design_matrix(reference, lags, *, intercept=True)

Build a Toeplitz-like design matrix from a reference signal and lag set.

Each column is a time-shifted copy of reference. Useful for FIR-model regression where the response depends on recent values of a predictor.

Parameters:
  • reference ((n_time,) float) – 1-D reference signal.

  • lags (array-like of int) – Sample lags to include. Lag k means the predictor at time t is reference[t - k]. Negative lags look forward.

  • intercept (bool) – If True (default), prepend a column of ones.

Returns:

X – Design matrix. n_predictors = len(lags) + int(intercept). Out-of-bounds samples are zero-padded.

Return type:

(n_time, n_predictors) float

Examples

Build a design matrix with lags 0..5 for a TTL reference signal, then regress each channel against it to remove the artifact:

X = lagged_design_matrix(ttl_ref, range(6))
beta = ols_fit(X, channel_data)
cleaned = ols_residual(X, channel_data, beta)
cogpy.regression.ols_fit(X, Y, *, rcond=None)

Fit ordinary least-squares: find beta minimizing ||Y - X @ beta||^2.

Parameters:
  • X ((n_time, n_predictors) float) – Design matrix.

  • Y ((n_time,) or (n_time, n_channels) float) – Response signal(s). Multiple channels are fit independently.

  • rcond (float, optional) – Cutoff for small singular values (passed to np.linalg.lstsq).

Returns:

beta – Regression coefficients.

Return type:

(n_predictors,) or (n_predictors, n_channels) float

cogpy.regression.ols_predict(X, beta)

Compute predicted signal: Y_hat = X @ beta.

Parameters:
  • X ((n_time, n_predictors) float) – Design matrix.

  • beta ((n_predictors,) or (n_predictors, n_channels) float) – Regression coefficients from ols_fit().

Returns:

Y_hat – Predicted signal.

Return type:

(n_time,) or (n_time, n_channels) float

cogpy.regression.ols_residual(X, Y, beta)

Compute residual signal: Y - X @ beta.

Parameters:
  • X ((n_time, n_predictors) float) – Design matrix.

  • Y ((n_time,) or (n_time, n_channels) float) – Original signal.

  • beta ((n_predictors,) or (n_predictors, n_channels) float) – Regression coefficients.

Returns:

residual – Signal with predicted component removed.

Return type:

same shape as Y