Stats Var#
var
#
| CLASS | DESCRIPTION |
|---|---|
RunningVariance |
Numerically stable running variance and mean computation. |
Classes#
RunningVariance(state=None)
#
Bases: Stats
Numerically stable running variance and mean computation.
Computes both mean and variance incrementally using the Chan-Golub-LeVeque
algorithm. More memory-efficient than RunningCovariance when only
variance (not full covariance matrix) is needed.
The variance is computed using the numerically stable two-pass algorithm:
\(\mu_{new} = \mu_{old}+\frac{n_{batch}}{n_{total}}(\mu_{batch}-\mu_{old})\),
\(M_{2,new} = M_{2,old} + \sum_i (x_i - \mu_{batch})^2 + \frac{n_{batch} \cdot n_{old}}{n_{total}} (\mu_{batch} - \mu_{old})^2\),
\(\sigma^2 = \frac{M_2}{n - 1} \quad \text{(unbiased)}\)
where \(M_2\) is the second central moment (sum of squared deviations).
| PARAMETER | DESCRIPTION |
|---|---|
state
|
Previous state to restore from.
TYPE:
|
Properties
count : int Total number of samples processed.
batch_count : int Number of batches processed.
data_shape : tuple | None Original shape of feature dimensions.
mean : torch.Tensor | None Current mean estimate.
v_cmom2 : torch.Tensor | None Second central moment (sum of squared deviations).
weight_sum : float Cumulative sum of weights (for weighted samples).
Examples:
Basic usage:
>>> from spectre.stats import RunningVariance
>>> import torch
>>>
>>> stat = RunningVariance()
>>> for _ in range(10):
... batch = torch.randn(100, 5)
... stat.add(batch)
>>>
>>> mean = stat.mean()
>>> variance = stat.variance(unbiased=True)
>>> stdev = stat.stdev(unbiased=True)
Biased vs unbiased variance:
>>> stat = RunningVariance()
>>> X = torch.randn(1000, 5)
>>> stat.add(X)
>>>
>>> var_biased = stat.variance(unbiased=False) # Divide by n
>>> var_unbiased = stat.variance(unbiased=True) # Divide by n-1
| METHOD | DESCRIPTION |
|---|---|
add |
Add batch of samples to running variance computation. |
mean |
Get current mean estimate. |
variance |
Get current variance estimate. |
stdev |
Get current standard deviation estimate. |
to |
Move internal tensors to specified device. |
load_state_dict |
Load state from dictionary. |
state_dict |
Save state to dictionary. |
| ATTRIBUTE | DESCRIPTION |
|---|---|
count |
Get total number of samples processed.
TYPE:
|
batch_count |
Get number of batches processed.
TYPE:
|
data_shape |
Get original feature shape (or None if unknown).
TYPE:
|
weight_sum |
Get cumulative sum of weights.
TYPE:
|
v_cmom2 |
Get second central moment (sum of squared deviations).
TYPE:
|
Source code in spectre/stats/var.py
Attributes#
count: int
property
writable
#
Get total number of samples processed.
batch_count: int
property
writable
#
Get number of batches processed.
data_shape: tuple | None
property
writable
#
Get original feature shape (or None if unknown).
weight_sum: float
property
writable
#
Get cumulative sum of weights.
v_cmom2: torch.Tensor | None
property
writable
#
Get second central moment (sum of squared deviations).
Functions#
add(X: torch.Tensor, weights: torch.Tensor | None = None, *args, **kwargs) -> None
#
Add batch of samples to running variance computation.
Updates both mean and second central moment incrementally using numerically stable Chan algorithm. Supports weighted samples for robust statistics.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Batch of data with shape (n_samples, n_features, ...).
TYPE:
|
weights
|
Sample weights with shape (n_samples,). If None, all samples have equal weight (equivalent to weights=1).
TYPE:
|
Source code in spectre/stats/var.py
mean() -> torch.Tensor
#
Get current mean estimate.
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Mean with original feature shape. |
variance(unbiased: bool = True) -> torch.Tensor
#
Get current variance estimate.
Computes variance from the second central moment. For weighted data, uses reliability weights correction when unbiased=True.
| PARAMETER | DESCRIPTION |
|---|---|
unbiased
|
If True, use Bessel's correction for unweighted or reliability weights correction for weighted data. If False, use biased estimate (divide by weight_sum).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Variance with original feature shape. |
Source code in spectre/stats/var.py
stdev(unbiased: bool = True) -> torch.Tensor
#
Get current standard deviation estimate.
Computes standard deviation as square root of variance.
| 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
|
Standard deviation with original feature shape. |
Source code in spectre/stats/var.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/var.py
load_state_dict(state: dict) -> None
#
Load state from dictionary.
Restores the running variance computation from a saved state.
| PARAMETER | DESCRIPTION |
|---|---|
state
|
State dictionary from
TYPE:
|
Source code in spectre/stats/var.py
state_dict() -> dict
#
Save state to dictionary.
| RETURNS | DESCRIPTION |
|---|---|
dict
|
State dictionary containing count, weight_sum, mean, second moment, and shape info. |