# Hierarchical Equal Risk Contribution

`D14-F02-A05` · Portfolio Construction → Risk Allocation · archetype `record-transform` · difficulty 5/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/portfolio-construction/risk-allocation/hierarchical-equal-risk-contribution/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

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

## Signature

```ts
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

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `assetIds` | `ordered string array` | yes | identifier |
| `covariance` | ``N x N` numeric matrix` | yes | return squared for one horizon |
| `clusterCount` | `integer` | yes | count |

## Worked example

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`:

```json
["A", "B", "C", "D"]
```

`covariance`:

```json
[
  [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`:

```json
2
```

### Call

```ts
hierarchicalEqualRiskContributionWeights(assetIds, covariance, clusterCount)
```

### Returns

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

```json
{
  "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.

## Verification and provenance

Tier: **contract**.

The module loads, the entry point is callable and its declared signature matches the compiled code. The example below is real captured output, but no independently published figure asserts the numbers.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.2.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/portfolio-construction/risk-allocation/hierarchical-equal-risk-contribution/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/portfolio-construction/risk-allocation/hierarchical-equal-risk-contribution/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/portfolio-construction/llms.txt
