statsmodels.stats.correlation_tools.cov_nearest¶
-
statsmodels.stats.correlation_tools.cov_nearest(cov, method=
'clipped'
, threshold=1e-15
, n_fact=100
, return_all=False
)[source]¶ Find the nearest covariance matrix that is positive (semi-) definite
This leaves the diagonal, i.e. the variance, unchanged
- Parameters:¶
- cov
ndarray
, (k,k) initial covariance matrix
- method
str
if “clipped”, then the faster but less accurate
corr_clipped
is used.if “nearest”, thencorr_nearest
is used- threshold
float
clipping threshold for smallest eigen value, see Notes
- n_fact
int
orfloat
factor to determine the maximum number of iterations in
corr_nearest
. See its doc string- return_allbool
if False (default), then only the covariance matrix is returned. If True, then correlation matrix and standard deviation are additionally returned.
- cov
- Returns:¶
See also
Notes
This converts the covariance matrix to a correlation matrix. Then, finds the nearest correlation matrix that is positive semidefinite and converts it back to a covariance matrix using the initial standard deviation.
The smallest eigenvalue of the intermediate correlation matrix is approximately equal to the
threshold
. If the threshold=0, then the smallest eigenvalue of the correlation matrix might be negative, but zero within a numerical error, for example in the range of -1e-16.Assumes input covariance matrix is symmetric.