Skip to content

Fix HRP SciPy linkage method validation - #755

Open
Samarthuday wants to merge 1 commit into
PyPortfolio:mainfrom
Samarthuday:fix-hrp-scipy-linkage
Open

Fix HRP SciPy linkage method validation#755
Samarthuday wants to merge 1 commit into
PyPortfolio:mainfrom
Samarthuday:fix-hrp-scipy-linkage

Conversation

@Samarthuday

Copy link
Copy Markdown

Summary

Fixes #754

HRPOpt.optimize() failed with current SciPy versions because it accessed
the private scipy.cluster.hierarchy._LINKAGE_METHODS attribute.

Changes

  • Replace the private SciPy API usage with explicit supported linkage-method validation.
  • Add regression tests for all supported linkage methods:
    • single
    • complete
    • average
    • weighted
    • centroid
    • median
    • ward

Testing

  • Targeted HRP and plotting tests: 31 passed
  • Full test suite: 320 passed, 4 skipped

Checklist

  • Tests added or updated
  • Documentation is not required for this internal compatibility fix
  • Existing behavior for invalid linkage methods is preserved

Copilot AI review requested due to automatic review settings August 2, 2026 14:44

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 thread tests/test_hrp.py
Comment on lines +35 to +36
assert isinstance(weights, dict)
np.testing.assert_almost_equal(sum(weights.values()), 1)
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.

HRPOpt fails with current SciPy because _LINKAGE_METHODS was removed

2 participants