Qu

ar_model

Builtin index · Statistics & ML

result = ar_model(x, order)

Input: x, order · Output: result

What it is

Fits an autoregressive AR(p) model by Yule-Walker to a length-N numeric vector x, using the integer lag order order (this is p).

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = ar_model(x, order)
x vectornot documented yet
order vectornot documented yet
coef vectora length-p vector of AR coefficients
order —p back
mean scalarthe scalar series mean subtracted before fitting
history —the last p values of x, used to seed forecasting
fitted vectora length-N vector of in-sample fitted values
residuals vectora length-N vector, x minus fitted
Returns a fitted model Record with fields coef (a length-p vector of AR coefficients), order (p back), mean (the scalar series mean subtracted before fitting), history (the last p values of x, used to seed forecasting), fitted (a length-N vector of in-sample fitted values), and residuals (a length-N vector, x minus fitted).

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

x = zeros(500)
x[0] = randn(seed = 21)
x[1] = randn(seed = 22)
for t = 2 to 499
    x[t] = 0.6 * x[t - 1] - 0.3 * x[t - 2] + 0.1 * randn()
end for
m = ar_model(x, 2)
print("generating: [0.6, -0.3]")
print("recovered:  {round(m.coef, 3)}")

future = m.predict(30)
plot(460 to 499, x[460:499], color = "#0f172a", label = "measured")
plot(500 to 529, future, color = "#e11d48", width = 2, label = "forecast")
legend()
title("AR(2), 30 steps ahead")
output
generating: [0.6, -0.3]
recovered:  [0.489, -0.153]
· plot: figure 4 (40 points)
· plot: figure 5 (30 points)
-6 -4 -2 0 2 4 6 0 50 100 150 200 250 300 350 400 450 500 550 x=-1.058, y=-0.826 x=0.594, y=0.636 x=0.88, y=1.305 x=-0.379, y=-0.489 x=0.152, y=1.215 x=1.407, y=0.754 x=0.231, y=-0.942 x=-1.291, y=-0.12 x=-0.085, y=-0.627 x=0.304, y=-0.101 x=0.037, y=0.004 x=-0.705, y=-0.429 x=0.241, y=-0.559 x=-0.582, y=-1.167 x=-0.277, y=-0.685 x=-1.636, y=-1.342 x=-0.865, y=-0.799 x=0.131, y=0.029 x=-1.581, y=-1.773 x=-2.048, y=-2.111 x=2.947, y=2.32 x=-0.456, y=-1.875 x=0.908, y=0.097 x=0.85, y=1.1 x=0.517, y=0.032 x=0.003, y=0.246 x=-0.201, y=0.212 x=0.466, y=-0.074 x=0.426, y=1.096 x=-0.066, y=-0.164 x=1.021, y=1.029 x=0.113, y=0.035 x=-0.998, y=-0.494 x=-0.066, y=-0.991 x=2.823, y=0.731 x=-0.046, y=-0.746 x=-0.029, y=-0.9 x=-0.031, y=0.196 x=-0.616, y=0.539 x=-1.349, y=-0.313 x=-1.085, y=-1.65 x=-1.452, y=-1.608 x=0.973, y=0.867 x=0.737, y=0.242 x=-0.185, y=0.468 x=1.175, y=1.485 x=-1.836, y=-0.822 x=-0.157, y=-0.076 x=0.407, y=0.307 x=-1.365, y=-1.022 x=-0.847, y=1.143 x=-0.762, y=0.021 x=-1.044, y=-0.505 x=0.478, y=1.123 x=0.316, y=-0.102 x=0.965, y=0.825 x=-0.751, y=-0.957 x=-0.988, y=-0.033 x=-1.22, y=-0.385 x=-0.681, y=-0.432 x=-0.621, y=0.256 x=0.918, y=0.744 x=1.182, y=-0.477 x=-0.039, y=0.425 x=0.585, y=-0.031 x=0.297, y=-0.535 x=0.112, y=-0.357 x=0.039, y=0.8 x=0.16, y=0.201 x=1.261, y=-0.955 x=0.95, y=0.238 x=-0.013, y=0.393 x=-1.577, y=-0.343 x=-0.67, y=-0.101 x=-0.631, y=-1.302 x=-0.84, y=-0.949 x=1.692, y=0.99 x=0.389, y=0.473 x=-0.261, y=-0.509 x=-0.491, y=-0.033 x=0.634, y=0.806 x=-0.342, y=0.622 x=-1.674, y=-1.63 x=0.039, y=-0.203 x=0.175, y=-0.688 x=0.206, y=0.293 x=1.405, y=0.994 x=-0.644, y=-0.672 x=-0.623, y=-0.7 x=1.569, y=0.384 x=-0.533, y=-0.154 x=0.682, y=-0.014 x=1.089, y=1.13 x=0.342, y=0.984 x=1.161, y=0.991 x=-0.232, y=0.289 x=-0.607, y=-0.737 x=0.295, y=0.07 x=0.275, y=0.25 x=-1.141, y=-0.456 x=0.461, y=0.554 x=-0.671, y=-0.906 x=0.513, y=1.154 x=0.499, y=-0.224 x=1.753, y=1.474 x=0.103, y=0.61 x=-0.351, y=0.258 x=0.553, y=-0.788 x=0.298, y=0.904 x=0.472, y=0.843 x=-0.94, y=-0.939 x=-1.062, y=-1.629 x=0.467, y=0.806 x=-0.653, y=-1.083 x=1.172, y=1.15 x=0.206, y=0.15 x=-1.077, y=-0.827 x=0.153, y=-0.987 x=0.202, y=0.439 x=0.011, y=-0.385 x=-0.67, y=0.029 x=1.367, y=0.502 x=0.955, y=0.531 x=0.352, y=0.238 x=1.543, y=0.703 x=-1.029, y=-0.474 x=0.104, y=0.508 x=0.877, y=-0.209 x=-1.366, y=-1.921 x=-1.583, y=-1.15 x=1.748, y=0.901 x=0.359, y=0.563 x=0.309, y=-0.002 x=1.322, y=1.929 x=-0.57, y=-0.79 x=-0.857, y=0.127 x=0.177, y=-0.37 x=-1.161, y=-1.563 x=0.371, y=1.281 x=-0.811, y=-1.39 x=0.168, y=1.089 x=-0.599, y=0.823 x=-0.452, y=-1.067 x=-1.241, y=-1.344 x=-0.711, y=-0.72 x=-0.233, y=-0.53 x=-0.344, y=0.761 x=1.152, y=0.27 x=0.989, y=0.896 x=0.308, y=0.67 x=-1.851, y=-1.723 x=0.866, y=0.926 x=-0.586, y=0.32 x=-0.204, y=-0.76 x=1.617, y=1.863 x=-0.897, y=-1.095 x=1.375, y=0.119 x=0.027, y=0.911 x=-0.268, y=-0.958 x=-0.58, y=-0.581 x=1.263, y=0.222 x=0.021, y=0.109 x=0.228, y=-0.664 x=0.002, y=-0.26 x=-0.301, y=0.237 x=0.646, y=0.863 x=0.272, y=-1.038 x=-0.432, y=-0.686 x=-0.123, y=1.22 x=0.844, y=1.163 x=-1.237, y=-1.157 x=2, y=1.132 x=-0.836, y=0.309 x=-0.332, y=-0.738 x=0.17, y=0.198 x=-0.327, y=0.587 x=-1.737, y=-1.131 x=-0.054, y=0.877 x=0.317, y=0.75 x=-1.406, y=-1.905 x=0.04, y=0.092 x=1.456, y=0.878 x=0.373, y=0.985 x=-0.912, y=-0.551 x=-0.104, y=0.042 x=0.349, y=0.549 x=1.098, y=0.24 x=0.588, y=-0.011 x=-0.164, y=-0.854 x=0.235, y=-0.679 x=-0.713, y=0.139 x=0.252, y=-0.404 x=-0.029, y=-0.281 x=-0.988, y=-0.818 x=0.223, y=-0.99 x=-0.591, y=0.839 x=-0.36, y=-0.088 x=-0.006, y=-0.156 x=-0.054, y=-0.158 x=0.644, y=-0.111 x=-3, y=-4.018 x=-2.75, y=-6.388 x=-2.5, y=-5.262 x=-2.25, y=0.293 x=-2, y=1.587 x=-1.75, y=1.194 x=-1.5, y=1.829 x=-1.25, y=2.719 x=-1, y=-0.145 x=-0.75, y=0.779 x=-0.5, y=-0.837 x=-0.25, y=2.101 x=0, y=0.378 x=0.25, y=1.148 x=0.5, y=1.019 x=0.75, y=-3.978 x=1, y=-3.731 x=1.25, y=-0.191 x=1.5, y=-1.178 x=1.75, y=-2.55 x=2, y=-0.452 x=2.25, y=4.657 x=2.5, y=0.591 x=2.75, y=5.418 x=3, y=6.826 AR(2), 30 steps ahead a b measured cubic fit measured forecast

from statistics-ml

How it works

ar_model(x, order) — classical AR(p) autoregressive time-series model, fit via the Yule-Walker equations rather than a fresh lagged-design-matrix least-squares fit: solves the (order, order) symmetric Toeplitz system built directly from x's own autocorrelation (reusing the existing acf free function — acf's already-normalized rho[k] values are exactly what the Yule-Walker normal equations are stated in terms of, so there was real solver code here to reuse rather than reinvent, per this task's own framing). Returns a kind="ar" model — see ar_fit_model's own doc comment for the fitted fields and the equations themselves, and the "predict" builtin arm below for .predict(n_ahead) (a forecast horizon, NOT a regression Xnew — an AR model forecasts its own future from its own fitted history, it has no "new X" to evaluate against, so this is special-cased there exactly like q_learning/ kalman's own non-regression predict meanings are).

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.