Skip to content

moments

Streaming covariance estimators using batches of normal scores.

The estimators here depend only on the first two moments, and these can be streamed batchwise. :class:_MomentEstimator accumulates the three summaries, the count, per-feature sum, and the uncentered cross-product Z.T @ Z. It then centers these at the end of the stream, once the mean is known.

SampleCovariance

Bases: _MomentEstimator

Unregularized sample covariance.

Note that top_k must be smaller than the smallest within-group sample size. :class:~scdesigner.covariance.LedoitWolf and :class:~scdesigner.covariance.OAS can be applied in the more general setting.

Examples:

>>> import numpy as np
>>> from scdesigner.covariance import SampleCovariance
>>>
>>> rng = np.random.default_rng(0)
>>> estimator = SampleCovariance()
>>> estimator.start(4)
>>> estimator.update(rng.standard_normal((50, 4)))
>>> estimator.finalize().covariance_.shape
(4, 4)
Source code in scdesigner/src/scdesigner/covariance/moments.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class SampleCovariance(_MomentEstimator):
    """
    Unregularized sample covariance.

    Note that ``top_k`` must be smaller than the smallest within-group sample
    size.  :class:`~scdesigner.covariance.LedoitWolf` and
    :class:`~scdesigner.covariance.OAS` can be applied in the more general
    setting.

    Examples
    --------
    >>> import numpy as np
    >>> from scdesigner.covariance import SampleCovariance
    >>>
    >>> rng = np.random.default_rng(0)
    >>> estimator = SampleCovariance()
    >>> estimator.start(4)
    >>> estimator.update(rng.standard_normal((50, 4)))
    >>> estimator.finalize().covariance_.shape
    (4, 4)
    """

    def _finalize(self) -> np.ndarray:
        return self._biased_covariance()