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

SARIMA/SARIMAX

Install and import#

bash
npm install fintech-algorithms
ts
import { forecastSARIMAX } from "fintech-algorithms/statistical-time-series/forecast-models/sarima-sarimax";

Signature#

forecastSARIMAX(values, exog, futureExog, beta, ar, ma, seasonalAr, seasonalMa, intercept, differenceOrder, seasonalDifferenceOrder, period, horizon)

Seasonal ARIMA with optional exogenous regressors. Future exogenous values must be supplied for the whole horizon — if you do not know them, the forecast is conditional on a guess, and that dependency is worth being explicit about.

Parameters#

NameTypeNotes
valuesnumber[]Observation series in chronological order, oldest first.
exognumber[][]In-sample exogenous regressors, one row per observation.
futureExognumber[][]Exogenous values over the forecast horizon. These are assumptions, not data.
betanumber[]Coefficients on the exogenous regressors.
arnumber[]Non-seasonal AR coefficients.
manumber[]Non-seasonal MA coefficients.
seasonalArnumber[]Seasonal AR coefficients.
seasonalManumber[]Seasonal MA coefficients.
interceptnumberConstant term.
differenceOrdernumberNon-seasonal differencing order.
min: 0 · integer: true
seasonalDifferenceOrdernumberSeasonal differencing order.
min: 0 · integer: true
periodnumberSeasonal period — 12 for monthly, 4 for quarterly, 5 for trading days in a week.
min: 1 · integer: true
horizonnumberSteps ahead.
min: 1 · integer: true

Returns#

{ forecast, fitted, residuals, state, transformed_forecast, residualized_forecast }

The final forecast plus the transformed and residualised intermediates.

Errors#

  • When futureExog is shorter than the horizon — throws

Complexity: time O(n × (p + q + P + Q)), 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#

values
[12, 15, 18, 21, 24, 27]

Showing 6 of 8 elements.

exog
[
  [1],
  [2],
  [3]
]

Showing 3 of 8 elements.

futureExog
[
  [9],
  [10]
]
beta
[2]
ar
[0.5]
ma
[]
seasonalAr
[0.25]
seasonalMa
[]
intercept
0
differenceOrder
1
seasonalDifferenceOrder
0
period
2
horizon
2

Call#

forecastSARIMAX(values, exog, futureExog, beta, ar, ma, seasonalAr, seasonalMa, intercept, differenceOrder, seasonalDifferenceOrder, period, horizon)

Returns#

object with 1 field: forecast

{
  "forecast": [35.625, 38.0625]
}

Diagrams#

SARIMA/SARIMAX — boundary failure
SARIMA/SARIMAX — diagnostic workbench
SARIMA/SARIMAX — family handoff
SARIMA/SARIMAX — forecast origin
SARIMA/SARIMAX — recursion anatomy

Calculation flow#

SARIMA/SARIMAX Calculation Flow
flowchart LR
    A["Finalized equally spaced training series"] --> B["Validate cutoff and frozen parameters"]
    B --> C["Build selected state or transformed series"]
    C --> D["Calculate horizon h conditional mean"]
    D --> E{"More horizons?"}
    E -- "yes" --> F["Append forecast and zero future innovation"]
    F --> D
    E -- "no" --> G["Publish path and state trace"]
    G --> H["Reveal holdout only for evaluation"]

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 Forecast Models family#