# AutoReg

`D09-F02-A01` · Statistical Time Series → Forecast Models · archetype `record-transform` · difficulty 2/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/statistical-time-series/forecast-models/autoreg/
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 { forecastAutoReg } from "fintech-algorithms/statistical-time-series/forecast-models/autoreg";
```

## Signature

```ts
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

| Name | Type | Required | Notes |
| --- | --- | --- | --- |
| `values` | `number[]` | yes | Observation series in chronological order, oldest first. |
| `ar` | `number[]` | yes | Autoregressive coefficients, lag 1 first. |
| `intercept` | `number` | yes | Constant term. |
| `horizon` | `number` | yes | Steps 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

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

### Input

`values`:

```json
[10, 12, 11]
```

`ar`:

```json
[0.5]
```

`intercept`:

```json
5
```

`horizon`:

```json
2
```

### Call

```ts
forecastAutoReg(values, ar, intercept, horizon)
```

### Returns

object with 1 field: forecast

```json
{
  "forecast": [10.5, 10.25]
}
```

## 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/forecast-models/autoreg/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/statistical-time-series/forecast-models/autoreg/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
