Skip to content

Spatial cv - #14

Merged
orabe merged 3 commits into
mainfrom
spatial_CV
Jun 30, 2025
Merged

orabe merged 3 commits into
mainfrom
spatial_CV

Conversation

@AliHashemi-ai

Copy link
Copy Markdown
Contributor

Pull Request: Add Temporal and Spatial Cross-Validation Solvers

Summary

This PR introduces two cross-validation strategies to select the optimal regularization parameter $\alpha$ (i.e., noise level $\lambda$) for Champagne and LowSNR-BSI models:

  • TemporalCVSolver implements $k$-fold temporal cross-validation based on the Type-II log-likelihood, evaluated via log-det Bregman divergence.
  • SpatialCVSolver implements spatial cross-validation based on the Type-I log-likelihood, evaluated as the mean squared error (MSE) on held-out sensors.

Both solvers inherit from a shared BaseCVSolver class, follow scikit-learn conventions, and support parallel grid search over a configurable range of $\alpha$ values.


Background

Temporal Cross-Validation

In temporal CV, the data $\mathbf{Y} \in \mathbb{R}^{M \times T}$ are split into $k$ contiguous temporal folds. For each split, the model is trained on $k-1$ folds and evaluated on the remaining fold using the out-of-sample Type-II log-likelihood:

$$ \mathcal{L}^{\text{II}}(\mathbf{Y}^{\text{train}}, \mathbf{Y}^{\text{test}}) = \mathrm{tr}(\mathbf{C}_{\mathbf{y}^{\text{test}}} \boldsymbol{\Sigma}_{\mathbf{y}^{\text{train}}}^{-1}) + \log|\boldsymbol{\Sigma}_{\mathbf{y}^{\text{train}}}| $$

Here:

  • $\mathbf{C}_{\mathbf{y}^{\text{test}}}$ is the empirical covariance of the test data
  • $\boldsymbol{\Sigma}_{\mathbf{y}^{\text{train}}}$ is the model covariance estimated from the training set

This quantity corresponds to the log-det Bregman divergence and is theoretically justified as a second-order comparison between model and empirical covariances.


Spatial Cross-Validation

In spatial CV, the sensor space is split into training and test sets. The model is trained on a subset of sensors and the predicted source activity $\mathbf{X}^{\text{train}}$ is mapped back to the test sensors using the corresponding lead field matrix. The Type-I log-likelihood is then computed as:

$$ \mathcal{L}^{\text{I}}(\mathbf{Y}^{\text{test}}, \mathbf{L}^{\text{test}}, \mathbf{X}^{\text{train}}) = \left\Vert \mathbf{Y}^{\text{test}} - \mathbf{L}^{\text{test}} \mathbf{X}^{\text{train}} \right\Vert_F^2 $$

This metric corresponds to the MSE (first-order discrepancy) between observed and predicted data on the held-out sensors.


Implementation Highlights

  • BaseCVSolver defines shared initialization, fit, and predict logic.
  • SpatialCVSolver uses GridSearchCV with MSE scoring to select $\alpha$.
  • TemporalCVSolver manually implements k-fold splitting and uses log-det Bregman divergence to select $\alpha$.
  • Utility functions:
    • _logdet() — numerically stable log-determinant computation
    • logdet_bregman_div_distance_nll() — implements the divergence metric

✅ Example Usage

solver = TemporalCVSolver(
    solver=some_solver_fn,
    cov_type='full',
    cov=estimated_noise_cov,
    n_orient=1,
    cv=4,
    alphas=np.logspace(0, -3, 10)
)
solver.fit(leadfield, Y)
X_hat = solver.predict(Y)

@AliHashemi-ai

Copy link
Copy Markdown
Contributor Author

@IsmailHuseynov @orabe

@orabe
orabe merged commit 203d0fc into main Jun 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants