statsmodels.stats.correlation_tools.cov_nearest#
- statsmodels.stats.correlation_tools.cov_nearest(cov, method='clipped', threshold=1e-15, n_fact=100, return_all=False, *, min_diag=None, use_namedtuple=None)[source]#
Find the nearest covariance matrix that is positive (semi-) definite
This leaves the diagonal, i.e. the variance, unchanged, unless
min_diagis used to enforce a strictly positive diagonal (see below).- Parameters:
- cov
ndarray, (k,k) initial covariance matrix
- method
str if “clipped”, then the faster but less accurate
corr_clippedis used. If “nearest”, thencorr_nearestis used- threshold
float clipping threshold for smallest eigen value, see Notes
- n_fact
intorfloat 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.
- min_diag
Noneorfloat If None (default), the diagonal of
covis left unchanged. This function converts the covariance matrix to a correlation matrix, which is not defined if a diagonal element (variance) is zero or negative and results in a matrix that containsnan. Ifmin_diagis a positive float, then diagonal elements that are smaller thanmin_diagare raised tomin_diagbefore the conversion, and aSpecificationWarningis issued. This makes it possible to correct matrices with a zero or negative diagonal, at the cost of changing those variances.- use_namedtuplebool,
optional Flag controlling whether a
CovNearestResultNamedTuple is returned. Whenreturn_all=TrueaCovNearestResultis always returned; it holds the same three elements as the legacy tuple, so it unpacks and indexes identically. Whenreturn_all=Falsea bare covariance matrix is returned unlessuse_namedtuple=True, which yields aCovNearestResultcarrying the correlation matrix and standard deviations too.
- cov
- Returns:
CovNearestResultorndarrayWhen
return_all=True(oruse_namedtuple=True), a NamedTuple with fields:- covndarray
corrected covariance matrix
- corrndarray
corrected correlation matrix
- stdndarray
standard deviation
CovNearestResulthas the same length and contents as the plain(cov_, corr_, std_)tuple it replaces, so it unpacks and indexes identically. SeeCovNearestResult.When
return_all=Falsea bare corrected covariance matrix is returned instead.
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.