Skip to content

Warn when covariance matrix is PSD but ill-conditioned#726

Open
Bortlesboat wants to merge 1 commit intoPyPortfolio:mainfrom
Bortlesboat:fix/warn-ill-conditioned-covariance
Open

Warn when covariance matrix is PSD but ill-conditioned#726
Bortlesboat wants to merge 1 commit intoPyPortfolio:mainfrom
Bortlesboat:fix/warn-ill-conditioned-covariance

Conversation

@Bortlesboat
Copy link

Summary

Adds a RuntimeWarning in fix_nonpositive_semidefinite() when the covariance matrix passes the positive semidefinite check but has a condition number exceeding 1e10, indicating potential numerical instability in downstream portfolio optimization.

Changes:

  • Check np.linalg.cond(matrix) after the PSD gate in fix_nonpositive_semidefinite()
  • Emit RuntimeWarning with the computed condition number when threshold is exceeded
  • Wrapped in try/except LinAlgError for robustness
  • The matrix is returned unchanged (it IS valid PSD — this is purely informational)

Test coverage:

  • Verifies warning fires for an ill-conditioned PSD matrix (condition number ~1e12)
  • Verifies no warning for a well-conditioned PSD matrix (identity)

Closes #694

Add a RuntimeWarning in fix_nonpositive_semidefinite() when the matrix
passes the PSD check but has a condition number exceeding 1e10. This
alerts users to potential numerical instability in downstream
optimization without altering the matrix.

Closes PyPortfolio#694
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ENH] emitting a warning when a covariance matrix is positive semidefinite but has a very large condition number

1 participant