# Iterative Cap Redistribution

`D03-F02-A07` · Index and Benchmark Engineering → Weighting and Capping · archetype `record-transform` · difficulty 4/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/index-and-benchmark-engineering/weighting-and-capping/iterative-cap-redistribution/
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 { calculate } from "fintech-algorithms/index-and-benchmark-engineering/weighting-and-capping/iterative-cap-redistribution";
```

## Signature

```ts
calculate(data)
```

The redistribution engine behind every capped index. Cap the offenders, spread the excess across the rest in proportion, repeat until nothing breaches — the loop is necessary because each pass can create new breaches.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `data` | `{ ids: string[]; rawWeights: number[]; cap: number; tolerance: number; maxIterations: number }` | yes | `tolerance` is the convergence threshold and `maxIterations` the bound that keeps a pathological input from looping forever. |

## Returns

`{ ids, weights, cap, iterations, maxWeight, weightSum }`

Converged weights with the iteration count. `weightSum` should be 1 to within tolerance — if it is not, the loop hit its bound.

## Errors

- When cap × count < 1, which makes compliance impossible — throws

## Complexity

Time `O(n × iterations)`, space `O(n)`.

## Worked example

This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

### Input

`data`:

```json
{
  "ids": ["A", "B", "C", "D", "E", "F"],
  "rawWeights": [0.42, 0.22, 0.14, 0.1, 0.07, 0.05],
  "cap": 0.25,
  "tolerance": 1e-10,
  "maxIterations": 100
}
```

### Call

```ts
calculate(data)
```

### Returns

object with 6 fields: ids, weights, cap, iterations, maxWeight, weightSum

```json
{
  "ids": ["A", "B", "C", "D", "E", "F"],
  "weights": [0.25, 0.25, 0.194444, 0.138889, 0.097222, 0.069444],
  "cap": 0.25,
  "iterations": 3,
  "maxWeight": 0.25,
  "weightSum": 1
}
```

## Verification and provenance

Tier: **verified** (via input-expected).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

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.1.
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/index-and-benchmark-engineering/weighting-and-capping/iterative-cap-redistribution/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/index-and-benchmark-engineering/weighting-and-capping/iterative-cap-redistribution/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/index-and-benchmark-engineering/llms.txt
