# Median Absolute Deviation Outlier Filter

`D01-F02-A03` · Market Data Engineering → Cleaning and Validation · archetype `record-transform` · difficulty 3/5 · verification **contract**

Full page: https://docs.thefintechbuilder.com/market-data-engineering/cleaning-and-validation/median-absolute-deviation-outlier-filter/
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 { madOutliers } from "fintech-algorithms/market-data-engineering/cleaning-and-validation/median-absolute-deviation-outlier-filter";
```

## Signature

```ts
madOutliers(values, threshold, scale, minimumSamples)
```

Whole-sample outlier detection against the median absolute deviation. Where the Hampel filter is local and rolling, this judges every point against one global robust spread.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `values` | `number[]` | yes | The complete sample to evaluate. |
| `threshold` | `number` | yes | How many scaled MADs from the median a point may sit before it is flagged. · min: 0 |
| `scale` | `number` | yes | MAD-to-sigma conversion factor; 1.4826 makes the result comparable to a standard deviation under normality. · min: 0 |
| `minimumSamples` | `number` | yes | Below this count the function reports insufficient data rather than guessing from a handful of points. · min: 1, integer: true |

## Returns

`{ status, valid_count, median, raw_mad, scaled_mad, threshold, outliers }`

The decision plus every statistic behind it, so a surprising result can be checked rather than re-derived.

## Errors

- When threshold or scale is negative — throws

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

`values`:

```json
[10, 10.1, 9.9, 10.2, 10.1, 9.8]
```

Showing 6 of 13 elements.

`threshold`:

```json
3.5
```

`scale`:

```json
1.4825796886582654
```

`minimumSamples`:

```json
3
```

### Call

```ts
madOutliers(values, threshold, scale, minimumSamples)
```

### Returns

object with 9 fields: status, valid_count, minimum_samples, median, raw_mad, scaled_mad, scale, threshold, …

```json
{
  "status": "ok",
  "valid_count": 12,
  "minimum_samples": 3,
  "median": 10.05,
  "raw_mad": 0.09999999999999964,
  "scaled_mad": 0.148257968865826,
  "scale": 1.4825796886582654,
  "threshold": 3.5,
  "points": [
    {
      "index": 0,
      "value": 10,
      "score": 0.337250000000006,
      "outlier": false
    },
    {
      "index": 1,
      "value": 10.1,
      "score": 0.337249999999994,
      "outlier": false
    },
    {
      "index": 2,
      "value": 9.9,
      "score": 1.011750000000006,
      "outlier": false
    }
  ]
}
```

## 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.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-data-engineering/cleaning-and-validation/median-absolute-deviation-outlier-filter/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/cleaning-and-validation/median-absolute-deviation-outlier-filter/impl.ts
- Standalone repository: https://github.com/IslamBaraka90/Fintech-MAD-Median-Absolute-Deviation-Outlier-Filter-algorithm
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-data-engineering/llms.txt
