fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Hierarchical Equal Risk Contribution

Install and import#

bash
npm install fintech-algorithms
ts
import { hierarchicalEqualRiskContributionWeights } from "fintech-algorithms/portfolio-construction/risk-allocation/hierarchical-equal-risk-contribution";

Signature#

hierarchicalEqualRiskContributionWeights(assetIds, covariance, clusterCount)

Combines hierarchical clustering with equal risk contribution, and makes the cluster-count stopping decision explicit instead of leaving it implicit.

Parameters#

NameTypeNotes
assetIdsordered string arrayidentifier
covariance`N x N` numeric matrixreturn squared for one horizon
clusterCountintegercount

Worked example#

executed Captured by running this function on the input its own test provides. Real output of real code — but not asserted against a published figure.

Input#

assetIds
["A", "B", "C", "D"]
covariance
[
  [0.04, 0.036, 0.004, 0.002],
  [0.036, 0.04, 0.003, 0.001],
  [0.004, 0.003, 0.01, 0.008]
]

Showing 3 of 4 elements.

clusterCount
2

Call#

hierarchicalEqualRiskContributionWeights(assetIds, covariance, clusterCount)

Returns#

object with 27 fields: assetIds, weights, componentRiskContributions, componentRiskShares, portfolioVariance, portfolioVolatility, sumWeights, quasiDiagonalOrder, …

{
  "assetIds": ["A", "B", "C", "D"],
  "weights": [
    0.10688050930460334,
    0.10688050930460334,
    0.5443192948090108,
    0.24191968658178256
  ],
  "componentRiskContributions": [
    0.012087521651000226,
    0.01120624850215681,
    0.04639019060037835,
    0.025670836878125575
  ],
  "componentRiskShares": [
    0.12676364431805753,
    0.11752160122498086,
    0.4865008552540335,
    0.26921389920292804
  ],
  "portfolioVariance": 0.009092537431375015,
  "portfolioVolatility": 0.09535479763166096,
  "sumWeights": 1,
  "quasiDiagonalOrder": [0, 1, 2, 3],
  "correlationMatrix": [
    [1, 0.8999999999999999, 0.2, 0.06666666666666667],
    [0.8999999999999999, 1, 0.15, 0.03333333333333333],
    [0.2, 0.15, 1, 0.5333333333333333]
  ],
  "distanceMatrix": [
    [0, 0.3170569112290949, 1.0116748865867387, 1.0853926811234083],
    [0.3170569112290949, 0, 1.030619181950856, 1.0983382141884035],
    [1.0116748865867387, 1.030619181950856, 0, 0.6863742401099239]
  ],
  "merges": [
    {
      "left": [0],
      "right": [1],
      "distance": 0.3170569112290949
    },
    {
      "left": [2],
      "right": [3],
      "distance": 0.6863742401099239
    },
    {
      "left": [0, 1],
      "right": [2, 3],
      "distance": 1.0116748865867387
    }
  ],
  "clusters": [
    [0, 1],
    [2, 3]
  ],
  "clusterWeights": [0.21376101860920668, 0.7862389813907934],
  "withinClusterWeights": [
    [0.5, 0.5],
    [0.6923076923076923, 0.30769230769230765]
  ]
}

Showing 14 of 27 fields.

Diagrams#

Hierarchical Equal Risk Contribution — herc three cluster allocation

Calculation flow#

HERC computation and audit flow
flowchart LR
    A[Validated covariance and explicit K] --> B[Correlation profile distance]
    B --> C[Deterministic single-linkage tree]
    C --> D[Cut at K terminal clusters]
    D --> E[Inverse-variance weights per cluster]
    E --> F[Terminal variance proxies]
    F --> G[Follow dendrogram branches]
    G --> H[Inverse-risk split at each node]
    H --> I[Final asset weights]
    I --> J[Portfolio and component-risk audit]
    A -. upstream contract .-> U[Adjusted aligned same-currency returns]

How it works#

This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.

Read the article →

References#

  • The Hierarchical Equal Risk Contribution Portfolio — Thomas Raffinot.
  • Hierarchical Clustering-Based Portfolios, section 12.3.3 — Daniel P. Palomar, *Portfolio Optimization*.
  • HierPortfolios HERC_Portfolio documentation and source — Carlos Trucios and Moon Jun Kwon; CRAN package
  • skfolio HierarchicalEqualRiskContribution — skfolio maintainers, documentation and BSD-3-Clause
  • Estimating the Number of Clusters in a Data Set via the Gap Statistic — Robert Tibshirani, Guenther Walther, Trevor Hastie.
  • Building Diversified Portfolios that Outperform Out of Sample — Marcos López de Prado.
  • Publication and data boundary

The rest of the Risk Allocation family#