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

AutoReg

Install and import#

bash
npm install fintech-algorithms
ts
import { forecastAutoReg } from "fintech-algorithms/statistical-time-series/forecast-models/autoreg";

Signature#

forecastAutoReg(values, ar, intercept, horizon)

Forecasts from an autoregression with supplied coefficients. Estimation is deliberately separate: this evaluates a model you already have, which keeps the arithmetic checkable.

Parameters#

NameTypeNotes
valuesnumber[]Observation series in chronological order, oldest first.
arnumber[]Autoregressive coefficients, lag 1 first.
interceptnumberConstant term.
horizonnumberSteps ahead to forecast. Beyond a few steps an AR forecast converges to the unconditional mean.
min: 1 · integer: true

Returns#

{ forecast, fitted, residuals, state }

Forecasts with in-sample fitted values and residuals — the residuals are what Ljung-Box then tests.

Errors#

  • When fewer observations are supplied than the AR order requires — throws

Complexity: time O(n × p), 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
[10, 12, 11]
ar
[0.5]
intercept
5
horizon
2

Call#

forecastAutoReg(values, ar, intercept, horizon)

Returns#

object with 1 field: forecast

{
  "forecast": [10.5, 10.25]
}

Diagrams#

AutoReg — boundary failure
AutoReg — diagnostic workbench
AutoReg — family handoff
AutoReg — forecast origin
AutoReg — recursion anatomy

Calculation flow#

AutoReg 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#