Conversation
Closes PyPortfolio#763. bl_weights() normalizes the solved weights to sum to 1 before returning them, which discards the total leverage information in the raw (pre-normalization) solution - there's no way to recover how much borrowing/lending the posterior implied once it's normalized away. Adds a return_raw=False parameter; when True, returns the raw weights via the same _make_output_weights() path instead. self.weights is always set to the normalized version regardless of the flag, so portfolio_performance() and every other existing caller is unaffected. Verified: full test suite passes (280 passed, 32 skipped; the one pre-existing HRPOpt/scipy failure in test_hrp.py is unrelated and already tracked in PyPortfolio#754/PyPortfolio#755). ruff and black clean on both changed files.
Author
|
Noting for the reviewer: @jrvarma suggested on #763 that this might read better as |
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.
Closes #763.
What
bl_weights()normalizes the solved weights to sum to 1 before returning them. That's the right default, but it discards information: the raw (pre-normalization) solution's total encodes how much net borrowing/lending the posterior implies, and there's no way to recover it once it's been normalized away.Adds
return_raw=Falsetobl_weights(). WhenTrue, it returns the raw weights through the same_make_output_weights()path instead of the normalized ones.Why this is safe
self.weightsis always set to the normalized version regardless of the flag — only the return value changes.optimize()(the alias) and every other caller that readsself.weightsafterward (e.g.portfolio_performance()) sees identical behavior to before this change.Verification
test_bl_weights_return_raw: confirms the raw weights are proportional to the normalized weights by exactly the sum that normalization divided out, confirmsself.weightsstays normalized, and confirms the default (return_raw=False) call is byte-identical to pre-change behavior.test_hrp.py::test_hrp_errorsis the pre-existing, already-trackedscipy._LINKAGE_METHODSincompatibility (HRPOpt fails with current SciPy because _LINKAGE_METHODS was removed #754, already has an open PR at Fix HRP SciPy linkage method validation #755) — unrelated to this change, reproduces identically onmainwithout it.ruff checkandblack --checkclean on both changed files.