renamed df to dof to avoid dataframe confusion
@@ -10,7 +10,7 @@ from collections.abc import Iterable
import numpy as np
-def get_cov_from_residuals(residuals, df=None):
+def get_cov_from_residuals(residuals, dof=None):
"""
computes an optimal shrinkage estimate of the precision matrix from
the residuals as described by Ledoit and Wolfe (2004): "A well-conditioned
@@ -19,7 +19,7 @@ def get_cov_from_residuals(residuals, df=None):
Args:
residuals(numpy.ndarray or list of these): n_obs x n_channels matrix
of residuals
- df(int or list of int): degrees of freedom for covariance estimation
+ dof(int or list of int): degrees of freedom for covariance estimation
defaults to n_obs-1, should be corrected for
Returns:
@@ -29,15 +29,15 @@ def get_cov_from_residuals(residuals, df=None):
if not isinstance(residuals, np.ndarray) or len(residuals.shape) > 2:
s_shrink = []
for i in range(len(residuals)):
- if df is None:
+ if dof is None:
s_shrink.append(get_cov_from_residuals(residuals[i]))
- elif isinstance(df, Iterable):
- s_shrink.append(get_cov_from_residuals(residuals[i], df[i]))
+ elif isinstance(dof, Iterable):
+ s_shrink.append(get_cov_from_residuals(residuals[i], dof[i]))
else:
- s_shrink.append(get_cov_from_residuals(residuals[i], df))
+ s_shrink.append(get_cov_from_residuals(residuals[i], dof))
else:
- if df is None:
- df = residuals.shape[0] - 1
+ if dof is None:
+ dof = residuals.shape[0] - 1
residuals = residuals - np.mean(residuals, axis=0, keepdims=True)
xt_x = np.einsum('ij, ik-> ijk', residuals, residuals)
s = np.sum(xt_x, axis=0) / xt_x.shape[0]
@@ -47,11 +47,11 @@ def get_cov_from_residuals(residuals, df=None):
b2 = min(d2, b2)
s_shrink = b2 / d2 * m * np.eye(s.shape[0]) \
+ (d2-b2) / d2 * s
- s_shrink = s_shrink * xt_x.shape[0] / df
+ s_shrink = s_shrink * xt_x.shape[0] / dof
return s_shrink
-def get_prec_from_residuals(residuals, df=None):
+def get_prec_from_residuals(residuals, dof=None):
"""
computes an optimal shrinkage estimate of the precision matrix from
the residuals as described by Ledoit and Wolfe (2004): "A well-conditioned
@@ -60,14 +60,14 @@ def get_prec_from_residuals(residuals, df=None):
Args:
residuals(numpy.ndarray or list of these): n_obs x n_channels matrix
of residuals
- df(int or list of int): degrees of freedom for covariance estimation
+ dof(int or list of int): degrees of freedom for covariance estimation
defaults to n_obs-1, should be corrected for
Returns:
numpy.ndarray (or list): sigma_p: covariance matrix over channels
"""
- cov = get_cov_from_residuals(residuals=residuals, df=df)
+ cov = get_cov_from_residuals(residuals=residuals, dof=dof)
if not isinstance(cov, np.ndarray) or len(cov.shape) > 2:
for i in range(len(cov)):
cov[i] = np.linalg.inv(cov[i])
