# Bayesian Change-Point Detection

`D09-F04-A06` · Statistical Time Series → State and Regime Models · archetype `record-transform` · difficulty 4/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/statistical-time-series/state-and-regime-models/bayesian-change-point-detection/
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/bayesian-change-point-detection";
```

## Signature

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

Online detection of structural breaks by tracking the posterior over run length — how long since the last change. Unlike a regime model it does not need the number of regimes specified in advance.

## Parameters

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `observations` | `number[]` | yes | Observations in chronological order. |
| `config` | `{ hazard: number; observation_variance: number; prior_mean: number; prior_variance: number }` | yes | `hazard` is the prior probability of a change at any step — its reciprocal is the expected run length, which is the more intuitive way to set it. The priors describe beliefs about a segment's mean before seeing data. |

## Returns

`{ …per-step run-length posterior }[]`

The run-length distribution per observation. Being online, an apparent change point can be revised by later data — the posterior shows that, a hard list of breaks would not.

## Errors

- When hazard falls outside 0…1, or a variance is not positive — throws

## Complexity

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

## 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.3275053489, 0.2452664583, 0.224748597]
```

`config`:

```json
{
  "hazard": 0.025,
  "observation_variance": 0.36,
  "prior_mean": 0,
  "prior_variance": 4
}
```

### Call

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

### Returns

object with 1 field: 2

```json
{
  "2": {
    "index": 2,
    "change_probability": 0.00942784148480789,
    "map_run_length": 3,
    "expected_run_length": 2.9241612404581354,
    "active_hypotheses": 4,
    "map_mean": 0.04611964608414239,
    "log_predictive_density": -0.6857585614486436
  }
}
```

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