3.11.6.1.3. statsmodels.stats.correlation_tools.corr_nearest

statsmodels.stats.correlation_tools.corr_nearest(corr, threshold=1e-15, n_fact=100)[source]

Find the nearest correlation matrix that is positive semi-definite.

The function iteratively adjust the correlation matrix by clipping the eigenvalues of a difference matrix. The diagonal elements are set to one.

Parameters:

corr : ndarray, (k, k)

initial correlation matrix

threshold : float

clipping threshold for smallest eigenvalue, see Notes

n_fact : int or float

factor to determine the maximum number of iterations. The maximum number of iterations is the integer part of the number of columns in the correlation matrix times n_fact.

Returns:

corr_new : ndarray, (optional)

corrected correlation matrix

Notes

The smallest eigenvalue of the corrected 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 correlation matrix is symmetric.

Stops after the first step if correlation matrix is already positive semi-definite or positive definite, so that smallest eigenvalue is above threshold. In this case, the returned array is not the original, but is equal to it within numerical precision.