7.19.4.28. 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 postive (semi-) definite

This leaves the diagonal, i.e. the variance, unchanged

Parameters:

cov : ndarray, (k,k)

initial covariance matrix

method : string

if “clipped”, then the faster but less accurate corr_clipped is used. if “nearest”, then corr_nearest is used

threshold : float

clipping threshold for smallest eigen value, see Notes

nfact : int or float

factor to determine the maximum number of iterations in corr_nearest. See its doc string

return_all : bool

if False (default), then only the covariance matrix is returned. If True, then correlation matrix and standard deviation are additionally returned.

Returns:

cov_ : ndarray

corrected covariance matrix

corr_ : ndarray, (optional)

corrected correlation matrix

std_ : ndarray, (optional)

standard deviation

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.