> For the complete documentation index, see [llms.txt](https://docs.algenta.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.algenta.ai/machine-learning/ml-arima.md).

# AR/MA estimation (ARIMA building blocks)

Durbin-Levinson AR estimation, innovations MA fitting and ARIMA forecasting building blocks.

`arima` fits AR(p) and MA(q) models in pure Mojo with zero Python imports — replacing `statsmodels.tsa` (AutoReg / ARIMA Yule-Walker fitting, innovations MA estimation, conditional-sum-of-squares likelihood). It complements `timeseries_stats`, which provides ACF/PACF diagnostics only; this module actually estimates coefficients. Model convention matches statsmodels (positive θ on lagged innovations), with the sample mean μ handled internally.

## The modules

| Module             | What it does                                                                                                                                                                                         |
| ------------------ | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `arima`            | fits AR(p) and MA(q) coefficients in pure Mojo — Yule-Walker/Durbin-Levinson AR estimation, innovations-algorithm MA estimation, CSS log-likelihood scoring, and order-d differencing/undifferencing |
| `timeseries_stats` | ACF/PACF and ADF diagnostics plus rolling mean/std, exponential smoothing and Holt linear trend — describes a series, does not fit AR/MA coefficients                                                |

## Functions

| Function                                      | What it does                                                                                                                           |
| --------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
| `ar_fit_yule_walker(series, p)`               | Fit AR(p) by Yule-Walker: Durbin-Levinson on the sample ACF (series de-meaned internally). Returns `ARFit` (phi, sigma2).              |
| `durbin_levinson(autocov, p)`                 | Durbin-Levinson recursion solving the order-p Yule-Walker equations on given autocovariances γ(0..p).                                  |
| `ar_forecast(series, phi, steps)`             | h-step-ahead recursive AR forecasts; decays toward the sample mean for stationary φ.                                                   |
| `ma_fit_innovations(series, q, max_iter)`     | Fit MA(q) via the innovations algorithm on the sample ACF. Returns `MAFit` (theta, sigma2).                                            |
| `innovations_algorithm(autocov, q, max_iter)` | Innovations recursion (Brockwell & Davis) on given autocovariances; θ estimates converge to the MA(q) coefficients as iterations grow. |
| `arma_loglik_css(series, phi, theta)`         | Conditional-sum-of-squares Gaussian log-likelihood at the profile MLE variance. Higher is better; compare fits with equal p.           |
| `difference(series, order)`                   | Order-d differencing — the ARIMA "I" step.                                                                                             |
| `undifference(diffed, initial)`               | Exact inverse of `difference` given the first d values of the original series.                                                         |

## Usage guidance

* **Deterministic**: the same series always produces bit-identical fits (asserted directly in the tests).
* Keep `max_iter` modest for the innovations algorithm — it is O(M³); 20–50 is typical and estimates stabilize quickly. `ma_fit_innovations` is sub-millisecond for M ≤ \~60 at any n ≤ 10k; do not pass `max_iter ~ n`.
* Degenerate inputs never trap: empty series or p/q ≤ 0 yield empty coefficient lists; an all-equal series (γ(0) ≈ 0) yields zero coefficients with sigma2 = 0.
* Round-trip identity: `undifference(difference(x, d), x[0:d]) == x` — exact (verified bit-for-bit on integer-valued floats for d = 1..3).

## Verification

Tests pin hand-derived references, not library snapshots:

* Hand-solved Yule-Walker at p = 2 on γ = \[1, 0.5, 0.2]: φ = (0.5333333333333333, −0.06666666666666667) = (8/15, −1/15) and sigma2 = 0.7466666666666666 = 11.2/15, matched to 1e-12.
* Hand-run innovations recursion on the exact MA(1) ACF (θ = 0.5): θ₁ = 42/85 = 0.49411764705882355 and sigma2 = 341/340 at M = 3 to 1e-12; at M = 30 the estimate converges to θ = 0.5, sigma2 = 1 within 1e-3.
* Seeded synthetic recovery (n = 2000): AR(1) with φ = 0.7 is recovered within 0.05 (overfit p = 2 gives |φ₂| < 0.08, the near-zero lag-2 PACF); MA(1) with θ = 0.5 is recovered within 0.08; both innovation variances land within 0.15 of the true 1.0.
* CSS log-likelihood matches hand-computed values −3.825292490936347 (AR(1)) and −3.806951651082006 (ARMA(1,1)) to 1e-9, and the true φ = 0.7 beats φ = 0 on the synthetic AR(1) data.

**Next:** promote what you trained with [statistical model comparison](/machine-learning/ml-model-comparison.md), or browse the [engine catalog](/engine-runtime/catalog.md).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.algenta.ai/machine-learning/ml-arima.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
