Stats Crosscov#
crosscov
#
| CLASS | DESCRIPTION |
|---|---|
RunningCrossCovariance |
Numerically stable running cross-covariance between two signals. |
Classes#
RunningCrossCovariance(state: dict | None = None, split_batch: bool = True)
#
Bases: Stats
Numerically stable running cross-covariance between two signals.
Computes cross-covariance between two aligned data sources (X and Y)
incrementally using the Chan-Golub-LeVeque algorithm. More memory-efficient
than RunningCovariance when only off-diagonal covariance blocks are needed.
Cross-covariance is computed using incremental updates:
\(\mu_{X,new} = \mu_{X,old} + \frac{\sum_i (x_i - \mu_{X,old})}{n_{total}}\), \(\mu_{Y,new} = \mu_{Y,old} + \frac{\sum_i (y_i - \mu_{Y,old})}{n_{total}}\), \(C_{XY,new} = C_{XY,old} + \delta_X^T \delta'_Y\),
where:
- \(\delta_X = x_i - \mu_{X,old}\) (X deviation from old mean)
- \(\delta'_Y = y_i - \mu_{Y,new}\) (Y deviation from new mean)
- \(C_{XY}\) is the cross-covariance second moment
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Previous state to restore from.
TYPE:
|
split_batch
|
Reserved for future batch splitting functionality.
TYPE:
|
Properties
count : int Total number of samples processed.
mean : list[torch.Tensor, torch.Tensor] Current mean estimates for X and Y.
cmom2 : torch.Tensor Cross-covariance second central moment matrix (n_features_X, n_features_Y).
v_cmom2 : list[torch.Tensor, torch.Tensor] Variance second moments for X and Y (for diagonal covariances).
Examples:
Basic usage with two aligned signals:
>>> from spectre.stats import RunningCrossCovariance
>>> import torch
>>>
>>> stat = RunningCrossCovariance()
>>> for _ in range(10):
... X = torch.randn(100, 5) # Signal 1
... Y = torch.randn(100, 3) # Signal 2
... stat.add(X, Y)
>>>
>>> means = stat.mean() # [mean_X, mean_Y]
>>> cross_cov = stat.covariance() # Shape: (5, 3)
>>> correlation = stat.correlation()
Memory-efficient cross-covariance:
>>> # When full covariance (X+Y, X+Y) doesn't fit in GPU memory
>>> stat = RunningCrossCovariance()
>>> X = torch.randn(1000, 1000) # Large feature space
>>> Y = torch.randn(1000, 500) # Different feature space
>>> stat.add(X, Y)
>>> cross_cov = stat.covariance() # Only (1000, 500) instead of (1500, 1500)
| METHOD | DESCRIPTION |
|---|---|
add |
Add aligned batch of X and Y samples to cross-covariance computation. |
mean |
Get current mean estimates for both signals. |
variance |
Get variance estimates for both signals. |
stdev |
Get standard deviation estimates for both signals. |
covariance |
Get cross-covariance matrix between X and Y. |
correlation |
Get cross-correlation matrix between X and Y. |
to |
Move internal tensors to specified device. |
state_dict |
Save state to dictionary. |
load_state_dict |
Load state from dictionary. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
count |
Get total number of samples processed.
TYPE:
|
cmom2 |
Get cross-covariance second central moment matrix.
TYPE:
|
v_cmom2 |
Get variance second central moments for X and Y.
TYPE:
|
Source code in spectre/stats/crosscov.py
Attributes#
count: int
property
writable
#
Get total number of samples processed.
cmom2: torch.Tensor
property
#
Get cross-covariance second central moment matrix.
v_cmom2: torch.Tensor | list[torch.Tensor] | None
property
#
Get variance second central moments for X and Y.
Functions#
add(X: torch.Tensor, Y: torch.Tensor) -> None
#
Add aligned batch of X and Y samples to cross-covariance computation.
Updates means, variances, and cross-covariance incrementally using numerically stable Chan algorithm. X and Y must have the same batch size.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
First batch with shape (n_samples, n_features_X, ...).
TYPE:
|
Y
|
Second batch with shape (n_samples, n_features_Y, ...). Must have same batch size as X.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If X and Y have different batch sizes (len(X) != len(Y)). |
Source code in spectre/stats/crosscov.py
mean() -> list[torch.Tensor]
#
Get current mean estimates for both signals.
| RETURNS | DESCRIPTION |
|---|---|
list[Tensor, Tensor]
|
List containing [mean_X, mean_Y]. |
variance(unbiased: bool = True) -> list[torch.Tensor] | None
#
Get variance estimates for both signals.
| PARAMETER | DESCRIPTION |
|---|---|
unbiased
|
If True, use Bessel's correction (divide by n-1). If False, use biased estimate (divide by n).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Tensor, Tensor] | None
|
List containing [variance_X, variance_Y]. |
Source code in spectre/stats/crosscov.py
stdev(unbiased: bool = True) -> list[torch.Tensor] | None
#
Get standard deviation estimates for both signals.
| PARAMETER | DESCRIPTION |
|---|---|
unbiased
|
If True, use Bessel's correction (divide by n-1). If False, use biased estimate (divide by n).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list[Tensor, Tensor] | None
|
List containing [stdev_X, stdev_Y]. |
Source code in spectre/stats/crosscov.py
covariance(unbiased: bool = True) -> torch.Tensor
#
Get cross-covariance matrix between X and Y.
| PARAMETER | DESCRIPTION |
|---|---|
unbiased
|
If True, use Bessel's correction (divide by n-1). If False, use biased estimate (divide by n).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Cross-covariance matrix with shape (n_features_X, n_features_Y). |
Source code in spectre/stats/crosscov.py
correlation() -> torch.Tensor
#
Get cross-correlation matrix between X and Y.
Computes Pearson correlation coefficients by normalizing cross-covariance by the product of standard deviations.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Cross-correlation matrix with shape (n_features_X, n_features_Y). Values range from -1 to 1. NaN values are replaced with 0. |
Source code in spectre/stats/crosscov.py
to(device: str = 'cpu') -> None
#
Move internal tensors to specified device.
| PARAMETER | DESCRIPTION |
|---|---|
device
|
Target device ("cpu", "cuda", "cuda:0", etc.).
TYPE:
|
Source code in spectre/stats/crosscov.py
state_dict() -> dict
#
Save state to dictionary.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
State dictionary containing count, means, variances, and cross-covariance matrix. |
Source code in spectre/stats/crosscov.py
load_state_dict(state: dict) -> None
#
Load state from dictionary.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
State dictionary from
TYPE:
|