Skip to content

OASCovariance

Online Oracle Approximating Shrinkage (OAS) covariance.

Like LedoitWolfCovariance, OAS shrinks the exponentially weighted sample covariance towards a scaled identity, but uses the Chen, Wiesel, Eldar & Hero (2010) shrinkage intensity, which is often better conditioned for approximately Gaussian data. The intensity is a closed form in the traces of the running covariance, applied on read; the per-step update is a plain O(d^2) exponentially weighted covariance update.

When to use it. The same high-dimensional / few-sample situations as LedoitWolfCovariance. OAS tends to shrink slightly more aggressively and is a good choice when the data are close to Gaussian; otherwise the two are interchangeable and worth comparing.

Parameters

  • fading_factor

    Typefloat

    Default0.5

    Recency weight of the underlying exponentially weighted covariance. The effective sample size is roughly 1 / fading_factor.

Attributes

  • matrix

Examples

import numpy as np
from river import covariance, datasets

oas = covariance.OASCovariance(fading_factor=0.05)
for x, _ in datasets.SP500Stocks():
    oas.update(x)

The shrinkage keeps the matrix positive-definite and invertible:

names = sorted({i for i, _ in oas.matrix})
M = np.array([[oas[i, j] for j in names] for i in names])
bool(np.all(np.linalg.eigvalsh(M) > 0))
True

Methods

update

Update with a single sample.

Parameters

  • xdict

update_many

Update with a dataframe of samples.

Any narwhals-compatible eager dataframe (pandas, polars, pyarrow, ...) is accepted. The result is identical to feeding the rows one at a time with update, in row order.

Parameters

  • XIntoDataFrame

References