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
covndarray, (k,k)

initial covariance matrix

methodstring

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

thresholdfloat

clipping threshold for smallest eigen value, see Notes

n_factint or float

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.

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.