Kalman Filter
Install and import
npm install fintech-algorithmsimport { runFilter } from "fintech-algorithms/statistical-time-series/state-and-regime-models/kalman-filter";Signature
runFilter(observations, config)Worked example
executed 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
[
0.1740371542,
0.7180296431,
0.0364260071,
-0.3580458703,
0.3552038047,
0.4311906307
]Showing 6 of 160 elements.
{
"a": 0.96,
"h": 1,
"q": 0.08,
"r": 0.64,
"initial_mean": 0,
"initial_variance": 2
}Call
runFilter(observations, config)Returns
array of 160 objects
[
{
"index": 0,
"predicted_mean": 0,
"predicted_variance": 1.9232,
"predicted_observation": 0,
"innovation": 0.1740371542,
"innovation_variance": 2.5632,
"kalman_gain": 0.7503121098626716,
"filtered_mean": 0.1305821843622971,
"filtered_variance": 0.4801997503121099
},
{
"index": 1,
"predicted_mean": 0.12535889698780522,
"predicted_variance": 0.5225520898876405,
"predicted_observation": 0.12535889698780522,
"innovation": 0.5926707461121947,
"innovation_variance": 1.1625520898876405,
"kalman_gain": 0.4494870332546945,
"filtered_mean": 0.3917567123546219,
"filtered_variance": 0.28767170128300446
},
{
"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
}
]Showing 3 of 160 elements.
Diagrams
Calculation flow
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
- A New Approach to Linear Filtering and Prediction Problems — R. E. Kalman
- An Introduction to the Kalman Filter — Greg Welch and Gary Bishop
- Bayesian Filtering and Smoothing — Simo Särkkä and Lennart Svensson
- Time Series Analysis by State Space Methods — statsmodels developers