fintech-algorithms
Using a coding agent? Give it the skill: npx skills add IslamBaraka90/Fintech-Algorithms-Library What it does →

Stationarity and Differencing Intuition

Install and import#

bash
npm install fintech-algorithms
ts
import { stationarityAndDifferencingIntuition } from "fintech-algorithms/foundations/financial-time-series-foundations/stationarity-and-differencing-intuition";

Signature#

stationarityAndDifferencingIntuition(input)

Compares the average level of the second half of a series with the first half, and reports the first differences, as a rough read on whether the level drifts.

Parameters#

NameTypeNotes
inputD00InputReads values, a non-empty list of finite numbers, timestamps of the same length, and stationarityTolerance, the size of mean shift still treated as flat.

Returns#

D00Output

meanShift is the second-half mean minus the first-half mean, firstDifferences the consecutive changes, differenceMean their average, and levelLikelyStationary is true when the absolute mean shift is within stationarityTolerance.

Errors#

  • When values is absent, empty, or holds a non-finite number — throws RangeError
  • When timestamps and values have different lengths — throws RangeError
  • When the series holds fewer than two observations — throws RangeError

Complexity: time O(n^2), space O(n).

Worked example#

verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

Input#

input
{
  "timestamps": [
    "2025-01-01T00:00:00Z",
    "2025-01-02T00:00:00Z",
    "2025-01-03T00:00:00Z",
    "2025-01-04T00:00:00Z",
    "2025-01-05T00:00:00Z",
    "2025-01-06T00:00:00Z"
  ],
  "values": [100, 102, 101, 104, 106, 105],
  "lag": 1,
  "window": 3,
  "resampleSize": 2,
  "period": 3,
  "stationarityTolerance": 3,
  "alpha": 0.4,
  "splitIndex": 4
}

Call#

stationarityAndDifferencingIntuition(input)

Returns#

object with 2 fields: meanShift, firstDifferences

{
  "meanShift": 4,
  "firstDifferences": [2, -1, 3, 2, -1]
}

Diagrams#

Stationarity and Differencing Intuition — article hero
Stationarity and Differencing Intuition — calculation ledger
Stationarity and Differencing Intuition — concept anatomy
Stationarity and Differencing Intuition — failure boundary
Stationarity and Differencing Intuition — method map
Stationarity and Differencing Intuition — scenario contrast

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.

Read the article →

References#

The rest of the Financial Time-Series Foundations family#