Hidden Markov Model
Install and import
npm install fintech-algorithmsimport { runFilter } from "fintech-algorithms/statistical-time-series/state-and-regime-models/hidden-markov-model";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.9183368993,
-0.5364890278,
-0.550167602,
-1.0221111597,
-0.9348142289,
-0.5355520167
]Showing 6 of 160 elements.
{
"transition": [
[0.94, 0.06],
[0.12, 0.88]
],
"means": [-0.15, 0.25],
"stds": [0.55, 1.45],
"initial": [0.8, 0.2]
}Call
runFilter(observations, config)Returns
array of 160 objects
[
{
"index": 0,
"predicted_state_0": 0.776,
"predicted_state_1": 0.22400000000000003,
"posterior_state_0": 0.8264612333826495,
"posterior_state_1": 0.17353876661735046,
"most_likely_state": 0,
"log_predictive_density": -1.3598732483743445
},
{
"index": 1,
"predicted_state_0": 0.7976982113737725,
"predicted_state_1": 0.20230178862622736,
"posterior_state_0": 0.9039212244809284,
"posterior_state_1": 0.09607877551907154,
"most_likely_state": 0,
"log_predictive_density": -0.6930121943884898
},
{
"index": 2,
"predicted_state_0": 0.8612154040743613,
"predicted_state_1": 0.13878459592563866,
"posterior_state_0": 0.9359801579550975,
"posterior_state_1": 0.0640198420449025,
"most_likely_state": 0,
"log_predictive_density": -0.6690356366768324
}
]Showing 3 of 160 elements.
Diagrams
Calculation flow
Hidden Markov Model 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 Tutorial on Hidden Markov Models and Selected Applications in Speech Recognition — Lawrence R. Rabiner
- Strictly Proper Scoring Rules, Prediction, and Estimation — Tilmann Gneiting and Adrian E. Raftery