# Kalman Filter

`D09-F04-A01` · Statistical Time Series → State and Regime Models · archetype `record-transform` · difficulty 3/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/statistical-time-series/state-and-regime-models/kalman-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 { runFilter } from "fintech-algorithms/statistical-time-series/state-and-regime-models/kalman-filter";
```

## Signature

```ts
runFilter(observations, config)
```

Optimal recursive estimation of a hidden state from noisy observations in a linear Gaussian system. In markets the hidden state is often the thing you actually want — a fair value, a slowly moving beta — and the observation is a noisy proxy for it.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `observations` | `number[]` | yes | Noisy observations in chronological order. |
| `config` | `{ a: number; h: number; q: number; r: number; initial_mean: number; initial_variance: number }` | yes | `a` is the state transition and `h` the observation mapping. `q` and `r` are the process and observation noise variances — their **ratio** is what determines how quickly the filter trusts new data, so scaling both changes nothing. |

## Returns

`{ index, predicted_mean, predicted_variance, innovation, kalman_gain, filtered_mean, filtered_variance }[]`

Every intermediate per step, including the Kalman gain and the innovation. A filter that behaves oddly is diagnosed from the gain path, not from the output.

## Errors

- When r is not positive, or q or the initial variance is negative — throws

## Complexity

Time `O(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

`observations`:

```json
[0.1740371542, 0.7180296431, 0.0364260071]
```

`config`:

```json
{
  "a": 0.96,
  "h": 1,
  "q": 0.08,
  "r": 0.64,
  "initial_mean": 0,
  "initial_variance": 2
}
```

### Call

```ts
runFilter(observations, config)
```

### Returns

object with 1 field: 2

```json
{
  "2": {
    "index": 2,
    "predicted_mean": 0.376086443860437,
    "predicted_variance": 0.3451182399024169,
    "predicted_observation": 0.376086443860437,
    "innovation": -0.339660436760437,
    "innovation_variance": 0.985118239902417,
    "kalman_gain": 0.350331793609469,
    "filtered_mean": 0.25709259383197747,
    "filtered_variance": 0.22421234791006017
  }
}
```

## 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/statistical-time-series/state-and-regime-models/kalman-filter/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/statistical-time-series/state-and-regime-models/kalman-filter/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/statistical-time-series/llms.txt
