# Hidden Markov Model

`D09-F04-A04` · 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/hidden-markov-model/
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/hidden-markov-model";
```

## Signature

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

Infers a discrete hidden regime from observations. Where the Kalman filter tracks a continuous state, this asks which of a small number of *regimes* the market is in — and returns a probability rather than a label.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `observations` | `number[]` | yes | Observations in chronological order. |
| `config` | `{ transition: number[][]; means: number[]; stds: number[]; initial: number[] }` | yes | `transition` holds the regime switching probabilities, each row summing to 1. `means` and `stds` are the emission parameters per regime. Regime **order is arbitrary** — label switching means 'regime 0' has no intrinsic meaning across fits. |

## Returns

`{ …per-step filtered probabilities }[]`

Filtered regime probabilities per observation. Probabilities, not a hard classification: a 55/45 split is a genuinely uncertain moment and rounding it to a label discards that.

## Errors

- When a transition row does not sum to 1, or a standard deviation is not positive — throws

## Complexity

Time `O(n × states²)`, space `O(n × states)`.

## 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.9183368993, -0.5364890278, -0.550167602]
```

`config`:

```json
{
  "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

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

### Returns

object with 1 field: 2

```json
{
  "2": {
    "index": 2,
    "predicted_state_0": 0.8612154040743613,
    "predicted_state_1": 0.13878459592563866,
    "posterior_state_0": 0.9359801579550974,
    "posterior_state_1": 0.0640198420449025,
    "most_likely_state": 0,
    "log_predictive_density": -0.6690356366768324
  }
}
```

## 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/hidden-markov-model/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/statistical-time-series/state-and-regime-models/hidden-markov-model/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
