Skip to content

EmpiricalPrecision

Empirical precision matrix.

The precision matrix is the inverse of the covariance matrix.

This implementation leverages the Sherman-Morrison formula. The resulting inverse covariance matrix is not guaranteed to be identical to a batch computation. However, the difference shrinks with the number of observations.

Attributes

  • matrix

Examples

import numpy as np
import pandas as pd
from river import covariance

np.random.seed(42)
X = pd.DataFrame(np.random.random((1000, 3)))
X.head()
          0         1         2
0  0.374540  0.950714  0.731994
1  0.598658  0.156019  0.155995
2  0.058084  0.866176  0.601115
3  0.708073  0.020584  0.969910
4  0.832443  0.212339  0.181825

prec = covariance.EmpiricalPrecision()
for x in X.to_dict(orient="records"):
    prec.update(x)

prec
    0        1        2
0   12.026   -0.122   -0.214
1   -0.122   11.276   -0.026
2   -0.214   -0.026   11.632

pd.DataFrame(np.linalg.inv(np.cov(X.T, ddof=1)))
           0          1          2
0  12.159791  -0.124966  -0.218671
1  -0.124966  11.393394  -0.026662
2  -0.218671  -0.026662  11.756907

Methods

update

Update with a single sample.

Parameters

  • x

update_many

Update with a dataframe of samples.

Parameters

  • X'pd.DataFrame'