Fix CLA.max_sharpe() TypeError when all expected returns are equal - #753
Open
saket3395 wants to merge 1 commit into
Open
Fix CLA.max_sharpe() TypeError when all expected returns are equal#753saket3395 wants to merge 1 commit into
saket3395 wants to merge 1 commit into
Conversation
Fixes PyPortfolio#738. CLA.max_sharpe() raised TypeError: '>' not supported between instances of 'NoneType' and 'float' whenever all expected returns were equal. _compute_lambda returns (None, None) when its denominator c is exactly zero, which is guaranteed when all expected returns are identical. The case-a) branch of _solve already guards against this by comparing CLA._infnone(lam) instead of the raw lam, but the case-b) branch compared the raw lam directly, causing the crash. Wraps lam with CLA._infnone(...) in both comparisons of the case-b) branch, mirroring the existing case-a) pattern. When lam is None this now resolves to -inf, so the candidate is simply never selected as i_out/l_out, and the algorithm falls through to the minimum-variance solution instead of crashing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #738 —
CLA.max_sharpe()raisesTypeError: '>' not supported between instances of 'NoneType' and 'float'whenever all expected returns are equal.Root cause (as diagnosed in the issue):
_compute_lambdareturns(None, None)when its denominatorcis exactly zero, which is mathematically guaranteed when all expected returns are identical. In_solve, the case-a) branch (bounding a free weight) already guards against this by comparingCLA._infnone(lam)rather than the rawlam:But the case-b) branch (freeing a bounded weight), a few lines down, compares the raw
lamdirectly:When
lamisNone, both comparisons raiseTypeError.Fix
Wrap
lamwithCLA._infnone(...)in both comparisons of the case-b) branch, mirroring the existing case-a) pattern:_infnone(None)resolves to-inf, so a candidate with an undefinedlamis simply never selected asi_out/l_out— consistent with how the case-a) branch already treats an undefinedlamas "never the best candidate." No special-casing needed; the algorithm falls through naturally to the minimum-variance solution when no valid turning point exists, matching one of the two behaviors suggested in the issue.Test plan
_infnone-guarded comparison already present a few lines above in the same method (case-a) branch), so it doesn't introduce a new code pattern.mu = [0.1, 0.1],S = [[0.01, 0.01], [0.01, 0.02]]) by hand against the patched logic — withlam = None,CLA._infnone(lam) = -inf, so the case-b) branch no longer raises and simply skips that candidate.pypfoptpackage__init__pulls incvxpy, not installed here, and I didn't want to add a new dependency just to verify a two-line fix). Flagging for CI/maintainer verification — happy to iterate if anything surfaces.