Skip to content

Warn when estimating percentage returns from non-positive prices - #779

Open
yugeshsarikonda wants to merge 1 commit into
PyPortfolio:mainfrom
yugeshsarikonda:fix/warn-nonpositive-prices
Open

yugeshsarikonda wants to merge 1 commit into
PyPortfolio:mainfrom
yugeshsarikonda:fix/warn-nonpositive-prices

Conversation

@yugeshsarikonda

Copy link
Copy Markdown

Percentage-return estimates can be misleading when prices are zero or negative. returns_from_prices now emits a UserWarning naming the first affected asset and date and suggesting absolute price changes for instruments that can trade at or below zero.

Fixes #774. Existing return calculations remain unchanged, including the behavior already covered by the negative-price tests. Mean and EMA estimators inherit the warning through the shared helper. Already-computed returns passed with returns_data=True are unaffected.

Validation:

  • 13 focused pytest cases passed. Ten fail on the previous implementation because no actionable warning is emitted.
  • Covers Series and DataFrame inputs, zero and negative prices, simple and logarithmic returns, missing observations, estimator propagation, and already-computed return inputs.
  • Numerical outputs are compared with the existing pandas calculation.
  • The tests ran against the actual expected_returns module with only the package initializer bypassed because CVXPY is unavailable locally. The full optimization suite was not run.
  • git diff --check passed.

Implemented and tested with AI assistance from OpenAI Codex.

Identify the affected asset and date without changing return calculations.

Co-authored-by: Codex <noreply@openai.com>
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.

returns_from_prices / mean_historical_return give silent wrong results for non-positive prices

1 participant