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

Trend, Seasonality, Cycles, and Remainder

Install and import#

bash
npm install fintech-algorithms
ts
import { trendSeasonalityCyclesAndRemainder } from "fintech-algorithms/foundations/financial-time-series-foundations/trend-seasonality-cycles-and-remainder";

Signature#

trendSeasonalityCyclesAndRemainder(input)

Separates a series into a rolling-mean trend, a seasonal difference taken period steps back, and the remainder left once the trend is removed.

Parameters#

NameTypeNotes
inputD00InputReads values, a non-empty list of finite numbers, timestamps of the same length, window for the trend average, and period for the seasonal difference.

Returns#

D00Output

trend is the rolling mean, null at positions with fewer than window observations behind them. seasonalDifference subtracts the value period positions earlier and is null for the first period entries. remainder is the value minus the trend, null wherever the trend is. period is echoed back.

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
  • When window is not an integer between one and the observation count — throws RangeError
  • When period is not an integer between one and the observation count minus one — 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#

trendSeasonalityCyclesAndRemainder(input)

Returns#

object with 2 fields: trend, seasonalDifference

{
  "trend": [null, null, 101, 102.33333333333333, 103.66666666666667, 105],
  "seasonalDifference": [null, null, null, 4, 4, 4]
}

Diagrams#

Trend, Seasonality, Cycles, and Remainder — article hero
Trend, Seasonality, Cycles, and Remainder — calculation ledger
Trend, Seasonality, Cycles, and Remainder — concept anatomy
Trend, Seasonality, Cycles, and Remainder — failure boundary
Trend, Seasonality, Cycles, and Remainder — method map
Trend, Seasonality, Cycles, and Remainder — 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#