feat(hierarchical): add HERCOpt (HERC) and NCOpt (NCO) portfolio allocation#742
Open
hass-nation wants to merge 1 commit into
Open
feat(hierarchical): add HERCOpt (HERC) and NCOpt (NCO) portfolio allocation#742hass-nation wants to merge 1 commit into
hass-nation wants to merge 1 commit into
Conversation
Extends the hierarchical portfolio module with two additional methods: HERCOpt - Hierarchical Equal Risk Contribution (Raffinot 2018) Equal Risk Contribution (ERC) weights at every level of the hierarchy instead of the inverse-variance weights used by HRP. ERC accounts for within-cluster correlations, producing more balanced risk allocation when intra-cluster correlations are high. Implemented via cyclical coordinate descent (Roncalli 2013). NCOpt - Nested Cluster Optimization (Lopez de Prado 2019) Two-level nested procedure: within-cluster optimization followed by meta-portfolio optimization across clusters. Supports three objectives at each level - min_variance, erc, and equal-weight - giving 9 combinations. The meta-covariance is computed analytically from the full covariance matrix, so no return history is required. Both classes: - Share the same API as HRPOpt (fit from returns or cov_matrix) - Export portfolio_performance() with expected return and Sharpe ratio - Are exported from pypfopt.__init__ and __all__ - Accept all scipy linkage methods Also adds two private helpers: - _erc_weights_ccd: ERC via multiplicative CCD (Roncalli 2013) - _min_var_weights: long-only min-variance with analytic + SLSQP fallback 53 tests across 9 test classes; all existing HRP tests still pass. References ---------- Raffinot, T. (2018). Hierarchical clustering-based asset allocation. Journal of Portfolio Management, 44(2), 89-99. Lopez de Prado, M. (2019). A Robust Estimator of the Efficient Frontier. SSRN Working Paper. https://ssrn.com/abstract=3469961 Roncalli, T. (2013). Introduction to Risk Parity and Budgeting. CRC Press. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
Extends
pypfopt.hierarchical_portfoliowith two new portfolio allocation methods that the module docstring already identifies as missing:HERCOpt— Hierarchical Equal Risk Contribution (Raffinot 2018)NCOpt— Nested Cluster Optimization (Lopez de Prado 2019)Both classes share the same API as
HRPOptand are exported frompypfopt.__init__.HERCOpt — Hierarchical Equal Risk Contribution
HERC extends HRP by replacing inverse-variance (IVP) cluster weights with Equal Risk Contribution (ERC) weights at every level of the hierarchy. The recursive bisection structure is identical to HRP, but the variance of each sub-cluster is computed as the variance of its ERC portfolio rather than its IVP portfolio.
Because ERC accounts for within-cluster correlations, HERC produces more balanced risk allocation when intra-cluster correlations are high — a common situation in equity portfolios where sector peers cluster together.
The ERC weights are solved via the multiplicative cyclical coordinate descent update of Roncalli (2013), which converges reliably and requires no SciPy optimization calls for individual clusters.
NCOpt — Nested Cluster Optimization
NCO partitions the asset universe into clusters, optimizes within each cluster, and then optimizes across the resulting cluster-portfolios in a two-level nested procedure.
The meta-covariance across clusters is computed analytically from the full covariance matrix (w_i' Sigma w_j), so NCO works correctly even when only a covariance matrix (no return history) is supplied.
Supports all 9 combinations of (internal_opt x meta_opt).
Private helpers (also usable by downstream code)
_erc_weights_ccd(cov)_min_var_weights(cov)Files changed
pypfopt/hierarchical_portfolio.pypypfopt/__init__.pytests/test_herc_nco.pyTests
Coverage includes: ERC weight correctness (equal RC property), min-var analytic solution, instantiation errors, weight validity (sum=1, non-negative), all 9 NCO method combinations, cov-only mode, comparison vs HRP (weights differ meaningfully), portfolio performance.
References
Generated with Claude Code