Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Open
Bortlesboat wants to merge 1 commit into
PyPortfolio:main from
Bortlesboat:fix/warn-ill-conditioned-covariance
Open

Warn when covariance matrix is PSD but ill-conditioned #726
Bortlesboat wants to merge 1 commit into
PyPortfolio:main from
Bortlesboat:fix/warn-ill-conditioned-covariance

Conversation

@Bortlesboat

@Bortlesboat Bortlesboat commented Mar 23, 2026

Copy link
Copy Markdown

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 

@atharvajoshi01 atharvajoshi01 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Clean addition. The 1e10 condition number threshold is reasonable — typical rule of thumb in numerical linear algebra. The try/except around np.linalg.cond handles degenerate cases. Test coverage is solid with both the warning and no-warning paths.

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

Reviewers

1 more reviewer
@atharvajoshi01 atharvajoshi01 atharvajoshi01 left review comments
Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

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

AltStyle によって変換されたページ (->オリジナル) /