my_code_base.linalg

Package Contents

my_code_base.linalg.empirical_covariance(x, bias=False)[source]

Compute the empirical covariance matrix of a given dataset:

\[\Sigma = \frac{1}{\text{dof}} DD^\intercal\]

where \(D\) is the matrix of the anomalies (\(x-\mu\)) and dof is the degrees of freedom. Since for the matrix of the anomalies the mean has to be build first, one degree of freedom is gone. Therefore, for the empirical covariance matrix, the normalization is usually done by (m-1).

Depending on the parameter bias, the degrees of freedom (dof) are either m or (m-1).

Parameters:
  • x (array-like) – Input dataset. It should be a 2-dimensional array-like object.

  • bias (bool, optional) – If False, the normalization by the degrees of freedom (dof) is (m-1). Otherwise (bias=True), the normalization is by m.

Returns:

The empirical covariance matrix of the input dataset.

Return type:

array-like

Example

>>> import numpy as np
>>> x = np.array([[92, 80], [60, 30], [100, 70]])
>>> empirical_covariance(x)
array([[ 72., 180., 180.],
       [180., 450., 450.],
       [180., 450., 450.]])
>>> df = pd.DataFrame({"A": [92, 60, 100], "B": [80, 30, 70]}, index=[1, 2, 3])
>>> df
     A   B
1   92  80
2   60  30
3  100  70
>>> empirical_covariance(df)
       1      2      3
1   72.0  180.0  180.0
2  180.0  450.0  450.0
3  180.0  450.0  450.0
my_code_base.linalg.inv(x)[source]

Invert a quadratic-shape numpy.ndarray object.