# Expected Shortfall Intuition

`D00-F11-A06` · Financial Mathematics, Statistics, and Data Foundations → Financial Risk and Performance Statistics · archetype `record-transform` · difficulty 1/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/foundations/financial-risk-and-performance-statistics/expected-shortfall-intuition/
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 { expectedShortfallIntuition } from "fintech-algorithms/foundations/financial-risk-and-performance-statistics/expected-shortfall-intuition";
```

## Signature

```ts
expectedShortfallIntuition(input)
```

Averages the losses that sit at or beyond the value-at-risk threshold, describing the tail rather than only its edge.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `input` | `D00Input` | yes | Reads `returns` and `benchmark`, two aligned non-empty lists of finite periodic returns, `frequency`, the number of periods per year, and `confidence`, the quantile level in the closed interval from zero to one. |

## Returns

`D00Output`

`expectedShortfall` is the mean of the tail losses, `valueAtRisk` is the threshold they were selected against, and `tailObservations` counts how many losses were at or above it.

## Errors

- When `returns` or `benchmark` is absent, empty, or holds a non-finite number — throws RangeError
- When `returns` and `benchmark` differ in length, or hold fewer than two observations — throws RangeError
- When `frequency` is zero or negative — throws RangeError
- When `confidence` is outside the closed interval from zero to one — throws RangeError
- When `confidence` is absent, or outside the range zero to one — the engine reads it for every topic from A04 onward, including those that never use it — throws RangeError

## Complexity

Time `O(n log n)`, 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

`input`:

```json
{
  "returns": [0.01, -0.02, 0.015, -0.01, 0.03],
  "benchmark": [0.008, -0.01, 0.012, -0.006, 0.02],
  "frequency": 252,
  "target": 0,
  "confidence": 0.8,
  "riskFree": 0.0001,
  "weights": [0.6, 0.4],
  "covarianceMatrix": [
    [0.04, 0.01],
    [0.01, 0.09]
  ]
}
```

### Call

```ts
expectedShortfallIntuition(input)
```

### Returns

object with 2 fields: expectedShortfall, valueAtRisk

```json
{
  "expectedShortfall": 0.02,
  "valueAtRisk": 0.012000000000000002
}
```

## 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.0.
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/foundations/financial-risk-and-performance-statistics/expected-shortfall-intuition/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/foundations/financial-risk-and-performance-statistics/expected-shortfall-intuition/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/foundations/llms.txt
