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.
Code
Input
Output
Description
result = ar_model(x, order)
xvectornot documented yet
ordervectornot documented yet
coefvectora length-p vector of AR coefficients
order—p back
meanscalarthe scalar series mean subtracted before fitting
history—the last p values of x, used to seed forecasting
fittedvectora length-N vector of in-sample fitted values
residualsvectora 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.
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.