Extended Kalman Filter
Install and import#
npm install fintech-algorithmsimport { runFilter } from "fintech-algorithms/statistical-time-series/state-and-regime-models/extended-kalman-filter";Signature#
runFilter(observations, config)The Kalman filter for non-linear systems, linearised at each step. The approximation is local, so strong non-linearity can make it diverge — quietly, while still producing plausible-looking numbers.
Parameters#
| Name | Type | Notes |
|---|---|---|
observations | number[] | Noisy observations. |
config | { a, b, c, q, r, initial_mean, initial_variance } | a, b and c parameterise the non-linear transition and observation functions; q and r are the noise variances. |
Returns#
{ …per-step estimates }[]
Per-step predicted and filtered estimates with the gain, as for the linear filter.
Errors#
- When r is not positive — throws
Complexity: time O(n),
space O(n).
Worked example#
verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.
Input#
[0.1740564489, 0.7180957323, 0.0365723689]{
"a": 0.9,
"b": 0.22,
"c": 0.04,
"q": 0.06,
"r": 0.49,
"initial_mean": 0,
"initial_variance": 1.5
}Call#
runFilter(observations, config)Returns#
object with 1 field: 2
{
"2": {
"index": 2,
"transition_jacobian": 1.0979025313193456,
"measurement_jacobian": 1.040234775932769,
"predicted_mean": 0.5029346991596126,
"predicted_variance": 0.36799793432466277,
"predicted_observation": 0.5130524316243634,
"innovation": -0.47648006272436344,
"innovation_variance": 0.8882062919307445,
"kalman_gain": 0.4309857431023305,
"filtered_mean": 0.2975785852529078,
"filtered_variance": 0.20301476071185573
}
}Diagrams#
Calculation flow#
Extended Kalman Filter Causal Update Flow
flowchart LR
A["Filtered state at t-1"] --> B["Predict state at t"]
B --> C["Read observation available at t"]
C --> D["Compute evidence or innovation"]
D --> E["Normalize or gain-weight update"]
E --> F["Filtered state at t"]
F --> G["Publish diagnostics"]
F --> A
How it works#
This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.
References#
- An Introduction to the Kalman Filter — Greg Welch and Gary Bishop
- A New Approach to Linear Filtering and Prediction Problems — R. E. Kalman
- Bayesian Filtering and Smoothing — Simo Särkkä and Lennart Svensson
- Time Series Analysis by State Space Methods — statsmodels developers