Hierarchical Risk Parity
PortfolioOptimisers.HierarchicalRiskParity — Type
struct HierarchicalRiskParity{__T_opt, __T_r, __T_sca, __T_fb} <: ClusteringOptimisationEstimatorAllocates weights by recursively bisecting the dendrogram's leaf order and splitting each part's weight in inverse proportion to the risk of its two halves.
This is the Hierarchical Risk Parity algorithm. It clusters the assets, orders the leaves, then splits that order until every part holds one asset. The split is stage 3 of [112], recursive bisection, which measures the risk of a half as a variance. Section 12.1.3 of [12] extends it to any risk measure, and so does this type. It solves no optimisation problem, so it accepts a risk measure that is not convex. The resolved weight bounds clamp the split factor at every split, and the weight finaliser of opt acts on the result. _optimise states the steps.
Mathematical definition
The leaf order of the dendrogram is the root part. A part $C$ of more than one asset splits into its two halves $C_1$ and $C_2$. The split repeats on each half until every part holds one asset. The part's weight $W(C)$ divides between the two halves:
\[\begin{align} \tilde{w}_i(C) &= \frac{\rho(\{i\})^{-1}}{\sum_{j \in C} \rho(\{j\})^{-1}} \quad \forall\, i \in C\,,\\ \tilde{\rho}(C) &= \rho\left(\tilde{\boldsymbol{w}}(C)\right)\,,\\ \alpha &= \frac{\tilde{\rho}(C_2)}{\tilde{\rho}(C_1) + \tilde{\rho}(C_2)}\,,\\ W(C_1) &= \hat{\alpha} \, W(C)\,,\\ W(C_2) &= (1 - \hat{\alpha}) \, W(C)\,,\\ w_i &= W(\{i\})\,. \end{align}\]
Where:
- $\rho$: Risk measure
r, resolved byfactoryagainst the prior. Whenris a vector,hrp_scalarised_riskstates the pair $\left(\tilde{\rho}(C_1), \tilde{\rho}(C_2)\right)$ that the scalariserscamakes. - $\rho(\{i\})$: Risk of asset $i$ held alone, from
unitary_expected_risks. - $\tilde{\boldsymbol{w}}(C)$: Naive risk parity weights inside part $C$, zero outside it.
- $\tilde{\rho}(C)$: Risk of that naive risk parity sub-portfolio.
- $\alpha$: Split factor, the fraction of the part's weight that goes to $C_1$. $C_2$ receives $1 - \alpha$.
- $\hat{\alpha}$: The split factor $\alpha$ clamped to the weight bounds of the two halves, see
split_factor_weight_constraints. Under the default bounds, zero and one for every asset, $\hat{\alpha} = \alpha$. - $C_1$, $C_2$: Halves of a part of the dendrogram's leaf order. $C_1$ holds the first $\lfloor n/2 \rfloor$ of the part's $n$ leaves, and $C_2$ holds the rest.
- $W(C)$: Weight of part $C$, with $W = 1$ for the root part.
- $w_i$: Weight of asset $i$.
The weights of the two halves sum to the weight of their part, so $\sum_i w_i = 1$. When the weight bounds admit a portfolio, every asset holds its bounds, see split_factor_weight_constraints.
Fields
opt: Base hierarchical optimiser configuration.
r: Risk measure or vector of risk measures.
sca: Scalariser for combining multiple risk measures.nothinggives the sum ofSumScalariser.
fb: Fallback result or estimator.
Constructors
HierarchicalRiskParity(; opt::HierarchicalOptimiser = HierarchicalOptimiser(), r::TD{<:OptRM_VecOptRM} = Variance(), sca::TD_Option{<:Scalariser} = nothing, fb::TDO_Option{<:OptE_Opt} = nothing) -> HierarchicalRiskParityKeywords correspond to the struct's fields. Fields typed TD or TDO_Option may hold a TimeDependent per-fold schedule instead of a static value. The risk measure, the scalariser and the fallback are problem definition, so a cross-validation fold loop resolves them per fold. A fold-less optimise runs with each at its static default, which is nothing for fb.
Validation
- If
ris a vector:!isempty(r). The constructor throws anIsEmptyErrorotherwise. rholds noNoRisk, alone or in a vector. The constructor throws anArgumentErrorotherwise, throughassert_risk_measure_required, because a zero risk leaves the split factor undefined.fbschedules:bind !== :nearest. The constructor throws anArgumentErrorotherwise.- If
rholds aDataBenchmarkTracking:opt.pemust not statex_src = ExcessSource(), seeassert_data_benchmark_prior. - A reader that states no source and reads the raw returns data, beside a reader that states
x_src = ExcessSource(), logs one warning that names its path, seewarn_raw_readers. The head then builds as configured.
Propagated parameters
factory passes its arguments on to these @fprop fields:
opt: Recursively updated throughfactory.r: Recursively updated throughfactory.fb: Recursively updated throughfactory.
View parameters
HierarchicalRiskParity defines its own port_opt_view method rather than deriving one from field tags.
- The method reads the returns matrix
Xas its third argument. Whenopt.peholds a prior result, the method replacesXwithopt.pe.X, so it views the children against the observations of that prior, not against the caller's matrix. randoptrecurse throughport_opt_viewwith that matrix.fbrecurses throughview_childwith that matrix.scastays unchanged.
Examples
julia> HierarchicalRiskParity()HierarchicalRiskParity opt ┼ HierarchicalOptimiser │ pe ┼ EmpiricalPrior │ │ ce ┼ PortfolioOptimisersCovariance │ │ │ ce ┼ Covariance │ │ │ │ me ┼ SimpleExpectedReturns │ │ │ │ │ w ┴ nothing │ │ │ │ ce ┼ GeneralCovariance │ │ │ │ │ ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true) │ │ │ │ │ w ┴ nothing │ │ │ │ alg ┼ FullMoment() │ │ │ │ w ┴ nothing │ │ │ mp ┼ MatrixProcessing │ │ │ │ pdm ┼ Posdef │ │ │ │ │ alg ┼ UnionAll: NearestCorrelationMatrix.Newton │ │ │ │ │ kwargs ┴ @NamedTuple{}: NamedTuple() │ │ │ │ dn ┼ nothing │ │ │ │ dt ┼ nothing │ │ │ │ alg ┼ nothing │ │ │ │ order ┴ NTuple{4, Symbol}: (:pdm, :dn, :dt, :alg) │ │ me ┼ SimpleExpectedReturns │ │ │ w ┴ nothing │ │ horizon ┼ nothing │ │ fill_limit ┼ nothing │ │ x_src ┴ nothing │ cle ┼ ClustersEstimator │ │ ce ┼ PortfolioOptimisersCovariance │ │ │ ce ┼ Covariance │ │ │ │ me ┼ SimpleExpectedReturns │ │ │ │ │ w ┴ nothing │ │ │ │ ce ┼ GeneralCovariance │ │ │ │ │ ce ┼ StatsBase.SimpleCovariance: StatsBase.SimpleCovariance(true) │ │ │ │ │ w ┴ nothing │ │ │ │ alg ┼ FullMoment() │ │ │ │ w ┴ nothing │ │ │ mp ┼ MatrixProcessing │ │ │ │ pdm ┼ Posdef │ │ │ │ │ alg ┼ UnionAll: NearestCorrelationMatrix.Newton │ │ │ │ │ kwargs ┴ @NamedTuple{}: NamedTuple() │ │ │ │ dn ┼ nothing │ │ │ │ dt ┼ nothing │ │ │ │ alg ┼ nothing │ │ │ │ order ┴ NTuple{4, Symbol}: (:pdm, :dn, :dt, :alg) │ │ de ┼ Distance │ │ │ power ┼ nothing │ │ │ alg ┴ CanonicalDistance() │ │ alg ┼ HClustAlgorithm │ │ │ linkage ┴ Symbol: :ward │ │ onc ┼ OptimalNumberClusters │ │ │ max_k ┼ nothing │ │ │ alg ┼ SecondOrderDifference │ │ │ │ alg ┼ StandardisedValue │ │ │ │ │ mv ┼ MeanValue │ │ │ │ │ │ w ┴ nothing │ │ │ │ │ sv ┼ StdValue │ │ │ │ │ │ w ┼ nothing │ │ │ │ │ │ corrected ┴ Bool: true │ │ x_src ┴ nothing │ slv ┼ nothing │ wb ┼ WeightBounds │ │ lb ┼ Float64: 0.0 │ │ ub ┴ Float64: 1.0 │ fees ┼ nothing │ sets ┼ nothing │ wf ┼ IterativeWeightFinaliser │ │ iter ┴ Int64: 100 │ strict ┴ Bool: false r ┼ Variance │ settings ┼ RiskMeasureSettings │ │ scale ┼ Float64: 1.0 │ │ ub ┼ nothing │ │ rke ┴ Bool: true │ sigma ┼ nothing │ chol ┼ nothing │ rc ┼ nothing │ alg ┼ SquaredSOCRiskExpr() │ mtx_sqrt ┼ EigenFallbackSquareRoot() │ ratio_alg ┼ SquareRootRatio() │ corner ┴ nothing sca ┼ nothing fb ┴ nothingRelated
optimiseHierarchicalRiskParityResultClusteringOptimisationEstimatorHierarchicalOptimiserHierarchicalEqualRiskContributionSchurComplementHierarchicalRiskParityunitary_expected_riskssplit_factor_weight_constraintsfactoryport_opt_view
References
- [112] M. López de Prado. Building diversified portfolios that outperform out of sample. The Journal of Portfolio Management 42, 59–69 (2016). Online supplementary materials, Stage 3 and Appendix A.2.
- [12] D. Cajas. Advanced Portfolio Optimization: A Cutting-edge Quantitative Approach (Springer Nature Switzerland, 2025). Section 12.1.3.
PortfolioOptimisers.port_opt_view — Method
port_opt_view(
hrp::HierarchicalRiskParity,
i,
X::AbstractMatrix{<:Union{Number, AbstractJuMPScalar}},
args...
) -> HierarchicalRiskParity{HierarchicalOptimiser{__T_pe, __T_cle, __T_slv, __T_wb, __T_fees, __T_sets, __T_wf, __T_strict, __T_cache}} where {__T_pe, __T_cle, __T_slv, __T_wb, __T_fees, __T_sets, __T_wf, __T_strict, __T_cache}
Return a view of HierarchicalRiskParity hrp sliced to asset indices i.
When hrp.opt.pe holds a prior result, the view reads that prior's X in place of the X it receives. The struct's ## View parameters states what each field does.
Related
PortfolioOptimisers._optimise — Function
_optimise(
hrp::HierarchicalRiskParity{<:Any, <:OptimisationRiskMeasure};
...
) -> HierarchicalRiskParityResult{HierarchicalResult{__T_pr, __T_clr, __T_wb, __T_fees, __T_retcode, __T_w, __T_imsk}, Nothing} where {__T_pr, __T_clr, __T_wb, __T_fees, __T_retcode, __T_w, __T_imsk}
_optimise(
hrp::HierarchicalRiskParity{<:Any, <:OptimisationRiskMeasure},
rd::ReturnsResult;
rmsk,
kwargs...
) -> HierarchicalRiskParityResult{HierarchicalResult{__T_pr, __T_clr, __T_wb, __T_fees, __T_retcode, __T_w, __T_imsk}, Nothing} where {__T_pr, __T_clr, __T_wb, __T_fees, __T_retcode, __T_w, __T_imsk}
Run the Hierarchical Risk Parity optimisation for a single risk measure.
optimise calls this method. The struct HierarchicalRiskParity states the mathematics.
Unlike HierarchicalEqualRiskContribution and NestedClustered, this optimiser takes no branchorder keyword. Recursive bisection splits the leaf order of the dendrogram, so that order is an input of the allocation, and the clusterisation always keeps the optimal order.
Algorithm
- Resolve every
TimeDependentfield ofhrpto its static default, withreset_time_dependent_estimator. - Fit the prior
prwithopt.peonrd. The prior reads the returns in the frame that its ownx_srcstates.rdkeeps its raw returns, and the clustering reads the frame that its ownx_srcstates. - Find the Investable Mask
imskofpr. Resolve the feesfeeson the full universe, and place them on the assets ofimsk. - Remove the two liquidation charges,
lqandflq, fromfees, givingcfees, the fees that the risks read. - Reduce
pr,hrpandrdto the assets ofimsk, withinvestable_reduction.Xis the returns matrix of the reducedpr. - Cluster the assets with
opt.cle, givingclr. The call passes nobranchorder, so the leaf orderclr.res.orderkeeps the default optimal order, because a different order changes the allocation. - Resolve the risk measure
ragainstprandopt.slv, withfactory. - Compute the risk of each asset held alone,
rku, withunitary_expected_risks. - Resolve the weight bounds
wbfromopt.wbandopt.sets. Expand a side that resolves to a scalar or tonothingto one bound per asset, becausesplit_factor_weight_constraintssums the bounds of each half. - Set every entry of
wto one, and setitemsto the one partclr.res.order. - Replace each part in
itemsthat holds more than one asset by its two halves. Stop whenitemsis empty. - For each pair of halves
lcandrc, write their naive risk parity weights into the two columns ofwu, and compute their riskslriskandrriskwithexpected_risk. - Form the split factor
alpha, and clamp it withsplit_factor_weight_constraints. - Multiply
w[lc]byalphaandw[rc]by1 - alpha, then go back to step 12. - Divide
wby its sum, and apply the weight finaliseropt.wfwithfinalise_weight_bounds, givingretcodeand the finalw. - Return a
HierarchicalRiskParityResult. ItsHierarchicalResultexpandswback onto the full asset universe throughimsk.
Arguments
hrp: The hierarchical risk parity optimiser to use.rd: The returns result to use. Whenhrp.opt.peholds a prior result, the allocation reads no returns fromrd, but a fallback or the clusterisation can.rmsk: The mask of a fit again after a refusal, ornothingon the first fit.kwargs: Ignored. Abranchorderpassed here has no effect.
Related
PortfolioOptimisers._optimise — Function
_optimise(
hrp::HierarchicalRiskParity{<:Any, <:AbstractVector{<:OptimisationRiskMeasure}};
...
) -> HierarchicalRiskParityResult{HierarchicalResult{__T_pr, __T_clr, __T_wb, __T_fees, __T_retcode, __T_w, __T_imsk}, Nothing} where {__T_pr, __T_clr, __T_wb, __T_fees, __T_retcode, __T_w, __T_imsk}
_optimise(
hrp::HierarchicalRiskParity{<:Any, <:AbstractVector{<:OptimisationRiskMeasure}},
rd::ReturnsResult;
rmsk,
kwargs...
) -> HierarchicalRiskParityResult{HierarchicalResult{__T_pr, __T_clr, __T_wb, __T_fees, __T_retcode, __T_w, __T_imsk}, Nothing} where {__T_pr, __T_clr, __T_wb, __T_fees, __T_retcode, __T_w, __T_imsk}
Run the Hierarchical Risk Parity optimisation for a vector of risk measures, scalarising each cluster's risk.
optimise calls this method. It runs the steps of the single-measure method, with two changes.
Algorithm
- Run steps 1 to 8 of the single-measure method.
ris now a vector of resolved measures. - Compute the unitary risks of each measure once,
rkus, withunitary_expected_risks. They do not depend on the split. - Run steps 9 to 11 of the single-measure method.
- For each pair of halves
lcandrc, combine the measures into one pairlriskandrrisk, withhrp_scalarised_riskandsca. - Run steps 13 to 16 of the single-measure method.
Arguments
hrp: The hierarchical risk parity optimiser to use, with a vector of risk measures.rd: The returns result to use. Whenhrp.opt.peholds a prior result, the allocation reads no returns fromrd, but a fallback or the clusterisation can.rmsk: The mask of a fit again after a refusal, ornothingon the first fit.kwargs: Ignored. Abranchorderpassed here has no effect.
Related
PortfolioOptimisers.needs_previous_weights — Method
needs_previous_weights(opt::HierarchicalRiskParity) -> Any
Return whether the HierarchicalRiskParity needs previous portfolio weights.
Returns true when opt.opt, opt.r or opt.fb needs previous weights, or when a TimeDependent schedule in a field holds a value that does. A TurnoverRiskMeasure and a turnover fee both need them.
Related
References
- [12]
- D. Cajas. Advanced Portfolio Optimization: A Cutting-edge Quantitative Approach (Springer Nature Switzerland, 2025).
- [112]
- M. López de Prado. Building diversified portfolios that outperform out of sample. The Journal of Portfolio Management 42, 59–69 (2016).