Repository navigation
ENH/BUG: add min_diag option to cov_nearest for zero or negative diagonal - #9898
Conversation
| SpecificationWarning, | ||
| ) |
There was a problem hiding this comment.
| SpecificationWarning, | |
| ) | |
| SpecificationWarning, | |
| stacklevel=2, | |
| ) |
There was a problem hiding this comment.
Thanks! Applied the stacklevel=2 suggestion. On the CI failure: the full test_corrpsd.py suite passes locally (22 passed, 3 skipped) including under -W error, and codecov is green. The Azure failure looks unrelated/systemic — it's also red on other current PRs (e.g. #9894) — so I believe it's not from this change, but happy to dig further if you spot something specific.
Aniketsy
left a comment
There was a problem hiding this comment.
@vsaraikin thanks for the work ❤️ . i haven't gone through the changes in depth, i've suggested one changes as i looked into ci fail.
I will look into this if you say 😸
692f82b to
b8890bf
Compare
bashtage
left a comment
There was a problem hiding this comment.
Two small changes then LGTM.
|
|
||
| def cov_nearest(cov, method="clipped", threshold=1e-15, n_fact=100, return_all=False): | ||
| def cov_nearest(cov, method="clipped", threshold=1e-15, n_fact=100, | ||
| return_all=False, min_diag=None): |
There was a problem hiding this comment.
New keyword should be keyword only, so , *, min_diag=None.
| diag = np.diag(cov) | ||
| if np.any(diag < min_diag): | ||
| warnings.warn( | ||
| "Diagonal elements below min_diag=%r have been raised to " |
There was a problem hiding this comment.
Can you rebase on main, and then run the latest ruff? This file will fail the current CI checks. The only CI checks we now have are ruff and flake8.
b8890bf to
ee4eafb
Compare
|
Thanks |
Summary
Closes #9675.
cov_nearestreturns a matrix full ofnanwhen the input covariance has a zero (or negative) element on the diagonal:The cause is the conversion to a correlation matrix (
cov2corr), which divides bystd = sqrt(diag(cov)); a zero variance divides by zero and a negative variance takessqrtof a negative number, so the result isnanand every downstream step propagates it.This adds an opt-in
min_diagparameter, following the approach @josef-pkt outlined in the issue: whenmin_diagis a positive float, diagonal elements below it are raised tomin_diagbefore the correlation conversion, and aSpecificationWarningis issued. This fixes both zero and negative diagonals:which matches the magnitude of R's
Matrix::nearPDon the same input.Default behavior is unchanged (
min_diag=None), so existing callers are unaffected — thenanresult is only corrected when the caller opts in. I kept it opt-in rather than picking a non-Nonedefault; happy to change the default or the1e-8example value if you'd prefer a specific one.Testing
test_cov_nearest_min_diag, parametrized over bothmethod="clipped"andmethod="nearest", asserting: default still returnsnan(no behavior change),min_diagremoves thenan, the result equals[[2, 0], [0, 1e-8]]and is PSD, a negative diagonal is corrected, the input array is not mutated, and no warning fires when the diagonal is already abovemin_diag.statsmodels/stats/tests/test_corrpsd.pypasses (22 passed, 3 skipped) on Python 3.13 / macOS arm64.