# Hampel Bad-Tick Filter

`D01-F02-A02` · 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/hampel-bad-tick-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 { hampelFilter } from "fintech-algorithms/market-data-engineering/cleaning-and-validation/hampel-bad-tick-filter";
```

## Signature

```ts
hampelFilter(values, options)
```

Flags points that sit too far from a rolling median, measured in robust deviations rather than standard deviations — so one fat-finger print cannot inflate the very statistic used to detect it.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `values` | `number[]` | yes | Observation series in chronological order. |
| `options` | `{ windowRadius?: number; threshold?: number; scale?: number; minHistory?: number; mode?: "causal" \| "centred" }` | no | `windowRadius` is the half-width of the rolling window (default 3). `threshold` is how many scaled MADs count as an outlier. `scale` converts MAD to a standard-deviation equivalent (1.4826 for normal data). `minHistory` is the minimum sample before any judgement is made. `mode` chooses causal — history only, safe for live use — or centred, which sees future points and must never be used on a live feed. |

## Returns

`Verdict[]` · length same-as-input

One verdict per point with its median, deviation and the threshold applied. Returns one verdict per input row rather than throwing, so a single bad record cannot abort the batch — and cannot pass unnoticed either.

## Errors

- When windowRadius or threshold is not positive — throws

## Complexity

Time `O(n × windowRadius)`, 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
[100, 100.1, 99.9, 100, 100.1, 112]
```

Showing 6 of 17 elements.

`options`:

```json
{
  "windowRadius": 3,
  "threshold": 3,
  "scale": 1.4826,
  "minHistory": 3,
  "mode": "causal"
}
```

### Call

```ts
hampelFilter(values, options)
```

### Returns

array of 17 objects

```json
[
  {
    "index": 0,
    "value": 100,
    "mode": "causal",
    "windowStart": 0,
    "windowEnd": 0,
    "windowCount": 1,
    "median": 100,
    "mad": 0,
    "scaledMad": 0,
    "score": null,
    "threshold": 3,
    "flagged": false,
    "status": "insufficient_history",
    "lookaheadUsed": false
  },
  {
    "index": 1,
    "value": 100.1,
    "mode": "causal",
    "windowStart": 0,
    "windowEnd": 1,
    "windowCount": 2,
    "median": 100.05,
    "mad": 0.04999999999999716,
    "scaledMad": 0.07412999999999578,
    "score": null,
    "threshold": 3,
    "flagged": false,
    "status": "insufficient_history",
    "lookaheadUsed": false
  },
  {
    "index": 2,
    "value": 99.9,
    "mode": "causal",
    "windowStart": 0,
    "windowEnd": 2,
    "windowCount": 3,
    "median": 100,
    "mad": 0.09999999999999432,
    "scaledMad": 0.14825999999999157,
    "score": 0.6744907594765952,
    "threshold": 3,
    "flagged": false,
    "status": "eligible",
    "lookaheadUsed": false
  }
]
```

Showing 3 of 17 elements.

## 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/hampel-bad-tick-filter/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/market-data-engineering/cleaning-and-validation/hampel-bad-tick-filter/impl.ts
- Standalone repository: https://github.com/IslamBaraka90/Fintech-Hampel-Bad-Tick-Filter-Data-Quality-algorithm
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/market-data-engineering/llms.txt
