Skip to content

[ENH] Add weight_bounds to HRPOpt (constrained HRP) - #786

Open
abhnxvj wants to merge 3 commits into
PyPortfolio:mainfrom
abhnxvj:feature/hrp-weight-bounds
Open

abhnxvj wants to merge 3 commits into
PyPortfolio:mainfrom
abhnxvj:feature/hrp-weight-bounds

Conversation

@abhnxvj

@abhnxvj abhnxvj commented Dec 2, 2026

Copy link
Copy Markdown

Closes #398

Plain HRP can put a large weight on one low-volatility asset, as the example in #398 shows. This adds weight_bounds to HRPOpt, in the same form as EfficientFrontier (a single (lower, upper) pair or one pair per asset):

hrp = HRPOpt(returns, weight_bounds=(0.02, 0.10))
weights = hrp.optimize()

How it works. The bounds are applied inside the recursive bisection, following the constrained HRP in the paper linked in the issue (Pfitzinger and Katzke, 2019). At each split, the share alpha given to the first sub-cluster is clipped so that each sub-cluster's weight stays between the sum of its assets' lower bounds and the sum of their upper bounds. When the bisection reaches single assets, every weight is within its own bounds and the weights still sum to 1. The result is still HRP, not a projection of the HRP weights after the fact.

  • The default (0, 1) gives exactly the same weights as before.
  • Bounds are checked up front: 0 <= lower <= upper, the lower bounds must add up to at most 1, and the upper bounds to at least 1 (otherwise ValueError).
  • Works with both returns and cov_matrix inputs.

Example with the bundled stock_prices.csv and weight_bounds=(0.02, 0.10): XOM, T and WMT go from about 11% each to 10%, and JPM, AMZN and BAC go from 1.4-1.8% to 2%. The weights still sum to 1.

Tests: 5 new tests in tests/test_hrp.py cover bounds respected, default unchanged, per-asset bounds, cov_matrix input, and error cases. Docs: short section in OtherOptimizers.rst with the reference.

Note: on SciPy 1.18, HRPOpt.optimize currently fails on main because of _LINKAGE_METHODS (#754, fixed in #755). I ran the full suite on SciPy 1.17 (308 passed). ruff check and format are clean.

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.

Feature request: Constraints on HRP

1 participant