Fix HRP SciPy linkage method validation - #755
Open
Samarthuday wants to merge 1 commit into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR fixes HRP optimization compatibility with newer SciPy versions by removing reliance on the private scipy.cluster.hierarchy._LINKAGE_METHODS attribute and replacing it with explicit linkage-method validation, plus regression tests covering all supported linkage methods.
Changes:
- Replace private SciPy API usage with explicit validation of supported linkage methods in
HRPOpt.optimize(). - Add a parameterized regression test that runs
HRPOpt.optimize()across all supported linkage methods.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
pypfopt/hierarchical_portfolio.py |
Introduces an explicit list of valid linkage methods and uses it for HRPOpt.optimize() validation instead of SciPy’s removed private attribute. |
tests/test_hrp.py |
Adds a parameterized regression test to ensure HRPOpt.optimize() works for each supported linkage method. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+26
to
+34
| _VALID_LINKAGE_METHODS = { | ||
| "single", | ||
| "complete", | ||
| "average", | ||
| "weighted", | ||
| "centroid", | ||
| "median", | ||
| "ward", | ||
| } |
Comment on lines
+35
to
+36
| assert isinstance(weights, dict) | ||
| np.testing.assert_almost_equal(sum(weights.values()), 1) |
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 #754
HRPOpt.optimize()failed with current SciPy versions because it accessedthe private
scipy.cluster.hierarchy._LINKAGE_METHODSattribute.Changes
singlecompleteaverageweightedcentroidmedianwardTesting
31 passed320 passed, 4 skippedChecklist