Markov-Switching Autoregression
Install and import
npm install fintech-algorithmsimport { runFilter } from "fintech-algorithms/statistical-time-series/state-and-regime-models/markov-switching-autoregression";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.0710447869,
0.2119198541,
0.4036856432,
0.1288063984,
0.0072781565,
0.2691322581
]Showing 6 of 160 elements.
{
"transition": [
[0.95, 0.05],
[0.1, 0.9]
],
"intercepts": [0.12, -0.18],
"phis": [0.72, -0.3],
"stds": [0.35, 1.05],
"initial": [0.85, 0.15]
}Call
runFilter(observations, config)Returns
array of 160 objects
[
{
"index": 0,
"regime_0_forecast": 0.4285714285714285,
"regime_1_forecast": -0.13846153846153844,
"predicted_state_0": 0.8225,
"predicted_state_1": 0.17750000000000002,
"posterior_state_0": 0.8341333456896518,
"posterior_state_1": 0.1658666543103482,
"most_likely_state": 0,
"next_mixture_forecast": 0.025391734996519682,
"log_predictive_density": -0.9020034995504373
},
{
"index": 1,
"regime_0_forecast": 0.068847753432,
"regime_1_forecast": -0.15868656393,
"predicted_state_0": 0.809013343836204,
"predicted_state_1": 0.19098665616379595,
"posterior_state_0": 0.9255985027572549,
"posterior_state_1": 0.0744014972427451,
"most_likely_state": 0,
"next_mixture_forecast": 0.2141318776960829,
"log_predictive_density": -0.0872910496699388
},
{
"index": 2,
"regime_0_forecast": 0.272582294952,
"regime_1_forecast": -0.24357595623,
"predicted_state_0": 0.8867587273436666,
"predicted_state_1": 0.11324127265633334,
"posterior_state_0": 0.9636140438968227,
"posterior_state_1": 0.03638595610317734,
"most_likely_state": 0,
"next_mixture_forecast": 0.3530523573178951,
"log_predictive_density": -0.02238977910202937
}
]Showing 3 of 160 elements.
Diagrams
Calculation flow
Markov-Switching Autoregression 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 the Economic Analysis of Nonstationary Time Series and the Business Cycle — James D. Hamilton
- MarkovAutoregression — statsmodels developers
- Strictly Proper Scoring Rules, Prediction, and Estimation — Tilmann Gneiting and Adrian E. Raftery