Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Issue 1251: covariance calc for insufficient data points #1253

Merged
merged 2 commits into from
Mar 26, 2025

Conversation

jensnesten
Copy link
Contributor

@jensnesten jensnesten commented Mar 23, 2025

Fixes #1251

The code was generating runtime warnings when calculating covariance with insufficient data points:

RuntimeWarning: Degrees of freedom <= 0 for slice
cov_matrix = np.cov(equity_log_returns, market_log_returns)

Added length checks before covariance calculation to handle cases where:

  • There are no trades executed
  • The data series is too short
  • There are NaN values in the data

The fix returns np.nan for both Alpha and Beta metrics when there isn't enough data for a valid covariance calculation.

@kernc
Copy link
Owner

kernc commented Mar 26, 2025

Applied with slight modification. Thanks for figuring it out!

@kernc kernc merged commit 84e685a into kernc:master Mar 26, 2025
7 checks passed
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.

RuntimeWarning: Degrees of freedom <= 0 for slice
2 participants