You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
cov_nearest cannot not handle matrices with zero in the diagonal #9675
Matrices with values of zero along the diagonal are not handled well in cov_nearest. The result shows nan in many cells. This is likely because the implementation tries to convert to a correlation matrix first, which is not possible in this case. This is a problem in the case of quadratic optimization. Sometimes you might want a zero value for a quadratic term but the solver likes to work with positive definite matrices.
There should be a version or option to avoid this, but I have not looked at those functions in a long time.
We might need to special case this and check for zeros on the diagonal.
(I don't remember the details:
Even with eigenvalue decomposition, there is likely a problem with eigen vectors of zeros, when we have a row/column of zeros. In that case just adjusting the eigenvalue would not be enough.
Eigenvectors are normed. What's the result if the unnormed eigenvector is zeros plus floating point noise?
)
The nearest functions work through the correlation matrix.
The original intention was for applications where we need to fix the correlation coefficients. Variance was assumed to be correctly estimated.
clip_evals which just forces non-negative eigenvalues, only get's a positive semi-definite.
However, it works if we adjust the minimum required eigenvalue to, for example, 1e-8.
cov_nearest would require that we enforce a strictly positive diagonal.
I don't know which threshold would be a good default, maybe just imitate R.
Aside clip_evals works by clipping the smallest eigenvalue away from negative, but does not take into account the condition number.
We could require that the smallest eigenvalue is increased until the condition number is satisfied.
However, that does not look good, because, AFAIR, the largest eigenvalues are upward biased in small samples.
Here we are just enforcing positive (semi)-definiteness in a matrix, and not target a improved small sample shrinkage estimator of the variance.
We can add an option to impose a minimum strictly positive value for diagonal elements.
e.g. min_diag = 1e-8 or something smaller
clip the diagonal and issue a warning if a diagonal element is below the min_diag threshold
from statsmodels.stats.correlation_tools import clip_evals
result = clip_evals(mat)
print(result)
(array([[2., 0.],
[0., 0.]]), False)
result = clip_evals(mat, value=1e-8)
print(result)
(array([[2.e+00, 0.e+00],
[0.e+00, 1.e-08]]), False)
mat = np.array([[2.0, 0.0], [0.0, 1e-8]])
result = cov_nearest(mat)
print(result)
[[2.e+00 0.e+00]
[0.e+00 1.e-08]]
trying out some weird (?) cases
maybe that does not look bad
mat = np.array([[2.0, 1.0], [1.0, 1e-8]])
result = cov_nearest(mat)
print(result)
[[2.00000000e+00 1.41421356e-04]
[1.41421356e-04 1.00000000e-08]]
mat = np.array([[2.0, 4.0], [4.0, 1e-8]])
result = cov_nearest(mat)
print(result)
[[2.00000000e+00 1.41421356e-04]
[1.41421356e-04 1.00000000e-08]]
mat = np.array([[2.0, 0.0], [0.0, 1e-8]])
result = cov_nearest(mat, method="nearest")
print(result)
[[2.e+00 0.e+00]
[0.e+00 1.e-08]]
mat = np.array([[2.0, 1.0], [1.0, 1e-8]])
result = cov_nearest(mat, method="nearest")
print(result)
[[2.00000000e+00 1.41421356e-04]
[1.41421356e-04 1.00000000e-08]]
...\statsmodels\stats\correlation_tools.py:89: IterationLimitWarning:
Maximum iteration reached.
warnings.warn(iteration_limit_doc, IterationLimitWarning)
mat = np.array([[2.0, 4.0], [4.0, 1e-8]])
result = cov_nearest(mat, method="nearest")
print(result)
[[2.00000000e+00 1.41421356e-04]
[1.41421356e-04 1.00000000e-08]]
...\statsmodels\stats\correlation_tools.py:89: IterationLimitWarning:
Maximum iteration reached.
warnings.warn(iteration_limit_doc, IterationLimitWarning)
more extreme:
the current code also cannot fix a negative diagonal element
mat = np.array([[2.0, 0.0], [0.0, -1e-8]])
result = cov_nearest(mat)
print(result)
[[ 2. nan]
[nan nan]]
...\statsmodels\stats\moment_helpers.py:251: RuntimeWarning: invalid value encountered in sqrt
std_ = np.sqrt(np.diag(cov))
Aside:
I don't remember if the original methods to enforce positive definiteness used the correlation matrix and not arbitrary symmetric square matrices, or whether conversion to correlation was added because our application was for fixing the correlation coefficients and not the variances.
Activity
josef-pkt commentedon Oct 21, 2025
Thanks for reporting this.
There should be a version or option to avoid this, but I have not looked at those functions in a long time.
We might need to special case this and check for zeros on the diagonal.
(I don't remember the details:
Even with eigenvalue decomposition, there is likely a problem with eigen vectors of zeros, when we have a row/column of zeros. In that case just adjusting the eigenvalue would not be enough.
Eigenvectors are normed. What's the result if the unnormed eigenvector is zeros plus floating point noise?
)
josef-pkt commentedon Oct 22, 2025
The nearest functions work through the correlation matrix.
The original intention was for applications where we need to fix the correlation coefficients. Variance was assumed to be correctly estimated.
clip_evals which just forces non-negative eigenvalues, only get's a positive semi-definite.
However, it works if we adjust the minimum required eigenvalue to, for example, 1e-8.
cov_nearest would require that we enforce a strictly positive diagonal.
I don't know which threshold would be a good default, maybe just imitate R.
Aside clip_evals works by clipping the smallest eigenvalue away from negative, but does not take into account the condition number.
We could require that the smallest eigenvalue is increased until the condition number is satisfied.
However, that does not look good, because, AFAIR, the largest eigenvalues are upward biased in small samples.
Here we are just enforcing positive (semi)-definiteness in a matrix, and not target a improved small sample shrinkage estimator of the variance.
We can add an option to impose a minimum strictly positive value for diagonal elements.
e.g.
min_diag = 1e-8or something smallerclip the diagonal and issue a warning if a diagonal element is below the min_diag threshold
trying out some weird (?) cases
maybe that does not look bad
more extreme:
the current code also cannot fix a negative diagonal element
josef-pkt commentedon Oct 22, 2025
Aside:
I don't remember if the original methods to enforce positive definiteness used the correlation matrix and not arbitrary symmetric square matrices, or whether conversion to correlation was added because our application was for fixing the correlation coefficients and not the variances.