# Top-N Index Contribution

`D04-F05-A01` · Market Breadth and Internals → Concentration and Diffusion · archetype `record-transform` · difficulty 3/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/market-breadth-and-internals/concentration-and-diffusion/top-n-index-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 { calculate } from "fintech-algorithms/market-breadth-and-internals/concentration-and-diffusion/top-n-index-contribution";
```

## Signature

```ts
calculate(rows, topN)
```

How much of an index's move came from its largest N members. This is the arithmetic behind 'seven stocks are holding up the market' — a claim that is usually asserted and rarely measured.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `rows` | `Row[]` | yes | Constituents with weight, return and sector. Rows carry a `ready` flag; a row that is not ready is excluded rather than treated as zero, because a missing count and a count of zero mean opposite things about market breadth. |
| `topN` | `number` | yes | How many leading contributors to attribute separately. · min: 1, integer: true |

## Returns

`{ status, top_n, top_contribution, total_contribution, share, leaders }`

The leaders' contribution, the total, and the share — plus the leaders themselves, so the claim can be named rather than gestured at.

## Errors

- When topN exceeds the number of ready constituents — reported as a status rather than thrown

## Complexity

Time `O(n log n)`, space `O(n)`.

## 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

`rows`:

```json
[
  {
    "security_id": "S01",
    "weight": 0.17821782178217824,
    "return": 0.065,
    "sector": "Tech",
    "ready": true
  },
  {
    "security_id": "S02",
    "weight": 0.13861386138613863,
    "return": 0.041,
    "sector": "Financials",
    "ready": true
  },
  {
    "security_id": "S03",
    "weight": 0.10891089108910892,
    "return": 0.028,
    "sector": "Industrials",
    "ready": true
  }
]
```

Showing 3 of 24 elements.

`topN`:

```json
5
```

### Call

```ts
calculate(rows, topN)
```

### Returns

object with 6 fields: status, top_n, top_contribution, total_contribution, share, leaders

```json
{
  "status": "resolved",
  "top_n": 5,
  "top_contribution": 0.02225742574257426,
  "total_contribution": 0.021676237623762376,
  "share": 1.0268122230850045,
  "leaders": [
    {
      "security_id": "S01",
      "contribution": 0.011584158415841586
    },
    {
      "security_id": "S02",
      "contribution": 0.005683168316831684
    },
    {
      "security_id": "S03",
      "contribution": 0.00304950495049505
    }
  ]
}
```

## Verification and provenance

Tier: **verified** (via row-fixture).

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/market-breadth-and-internals/concentration-and-diffusion/top-n-index-contribution/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-breadth-and-internals/concentration-and-diffusion/top-n-index-contribution/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-breadth-and-internals/llms.txt
