Hierarchical Risk Parity

PortfolioOptimisers.HierarchicalRiskParity — Type
struct HierarchicalRiskParity{__T_opt, __T_r, __T_sca, __T_fb} <: ClusteringOptimisationEstimator

Allocates 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 by factory against the prior. When r is a vector, hrp_scalarised_risk states the pair $\left(\tilde{\rho}(C_1), \tilde{\rho}(C_2)\right)$ that the scalariser sca makes.
  • $\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. nothing gives the sum of SumScalariser.
  • 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) -> HierarchicalRiskParity

Keywords 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 r is a vector: !isempty(r). The constructor throws an IsEmptyError otherwise.
  • r holds no NoRisk, alone or in a vector. The constructor throws an ArgumentError otherwise, through assert_risk_measure_required, because a zero risk leaves the split factor undefined.
  • fb schedules: bind !== :nearest. The constructor throws an ArgumentError otherwise.
  • If r holds a DataBenchmarkTracking: opt.pe must not state x_src = ExcessSource(), see assert_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, see warn_raw_readers. The head then builds as configured.

Propagated parameters

factory passes its arguments on to these @fprop fields:

  • opt: Recursively updated through factory.
  • r: Recursively updated through factory.
  • fb: Recursively updated through factory.

View parameters

HierarchicalRiskParity defines its own port_opt_view method rather than deriving one from field tags.

  • The method reads the returns matrix X as its third argument. When opt.pe holds a prior result, the method replaces X with opt.pe.X, so it views the children against the observations of that prior, not against the caller's matrix.
  • r and opt recurse through port_opt_view with that matrix.
  • fb recurses through view_child with that matrix.
  • sca stays 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 ┴ nothing

Related

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.
source
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

source
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

  1. Resolve every TimeDependent field of hrp to its static default, with reset_time_dependent_estimator.
  2. Fit the prior pr with opt.pe on rd. The prior reads the returns in the frame that its own x_src states. rd keeps its raw returns, and the clustering reads the frame that its own x_src states.
  3. Find the Investable Mask imsk of pr. Resolve the fees fees on the full universe, and place them on the assets of imsk.
  4. Remove the two liquidation charges, lq and flq, from fees, giving cfees, the fees that the risks read.
  5. Reduce pr, hrp and rd to the assets of imsk, with investable_reduction. X is the returns matrix of the reduced pr.
  6. Cluster the assets with opt.cle, giving clr. The call passes no branchorder, so the leaf order clr.res.order keeps the default optimal order, because a different order changes the allocation.
  7. Resolve the risk measure r against pr and opt.slv, with factory.
  8. Compute the risk of each asset held alone, rku, with unitary_expected_risks.
  9. Resolve the weight bounds wb from opt.wb and opt.sets. Expand a side that resolves to a scalar or to nothing to one bound per asset, because split_factor_weight_constraints sums the bounds of each half.
  10. Set every entry of w to one, and set items to the one part clr.res.order.
  11. Replace each part in items that holds more than one asset by its two halves. Stop when items is empty.
  12. For each pair of halves lc and rc, write their naive risk parity weights into the two columns of wu, and compute their risks lrisk and rrisk with expected_risk.
  13. Form the split factor alpha, and clamp it with split_factor_weight_constraints.
  14. Multiply w[lc] by alpha and w[rc] by 1 - alpha, then go back to step 12.
  15. Divide w by its sum, and apply the weight finaliser opt.wf with finalise_weight_bounds, giving retcode and the final w.
  16. Return a HierarchicalRiskParityResult. Its HierarchicalResult expands w back onto the full asset universe through imsk.

Arguments

  • hrp: The hierarchical risk parity optimiser to use.
  • rd: The returns result to use. When hrp.opt.pe holds a prior result, the allocation reads no returns from rd, but a fallback or the clusterisation can.
  • rmsk: The mask of a fit again after a refusal, or nothing on the first fit.
  • kwargs: Ignored. A branchorder passed here has no effect.

Related

source
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

  1. Run steps 1 to 8 of the single-measure method. r is now a vector of resolved measures.
  2. Compute the unitary risks of each measure once, rkus, with unitary_expected_risks. They do not depend on the split.
  3. Run steps 9 to 11 of the single-measure method.
  4. For each pair of halves lc and rc, combine the measures into one pair lrisk and rrisk, with hrp_scalarised_risk and sca.
  5. 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. When hrp.opt.pe holds a prior result, the allocation reads no returns from rd, but a fallback or the clusterisation can.
  • rmsk: The mask of a fit again after a refusal, or nothing on the first fit.
  • kwargs: Ignored. A branchorder passed here has no effect.

Related

source

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).