Autoregressive (AR) models for univariate time series forecasting.
Classes
Autoregressive (AR) model for univariate time series forecasting.
The \text{AR}(p) model predicts future values of a time series based on a linear combination of its own past values. The order p represents the number of lagged observations included in the model.
Constructor
__init__( self, order: int = 1, method: str = 'yule_walker', trend: str | None = 'c', )
Overview
The AR model works through the following steps:
- Select the model order p (number of lags to include).
- Estimate the autoregressive coefficients using Yule-Walker
- Compute the constant (intercept) term from the residuals.
- Generate forecasts by applying the fitted coefficients to the
Theory
The \text{AR}(p) process is defined as:
y_t = c + \sum_{i=1}^{p} \phi_i y_{t-i} + \epsilon_t
where:
- y_t: The value of the time series at time t.
- c: A constant (intercept) term.
- \phi_i: The autoregressive parameters (coefficients).
- p: The order of the model (number of lags).
- \epsilon_t: White noise error term at time t with
Parameters
order
int
= 1
The order :math:`p` of the AR model (number of lags).
method
{"yule_walker", "ols", "mle"}
= "yule_walker"
The method used to estimate the AR parameters:
- •
"yule_walker": Solves the Yule-Walker equations using
autocorrelations.
- •
"ols": Ordinary Least Squares estimation. - •
"mle": Simplified Maximum Likelihood Estimation.
trend
{"c", "ct", None}
= "c"
The trend component to include:
- •
"c": Include a constant term (intercept). - •
"ct": Include both a constant and a linear time trend. - •
None: No constant or trend.
Attributes
ar_params_
np.ndarray of shape (order,)
Fitted autoregressive coefficients :math:`(\phi_1, \phi_2, \dots, \phi_p)`.
const_
float
The fitted constant (intercept) term.
resid_
np.ndarray of shape (n_obs - order,)
The residuals (errors) from the fitted model.
sigma2_
float
The variance of the residuals (:math:`\sigma^2`).
n_obs_
int
The total number of observations used for fitting.
Notes
Complexity:
- Training: O(p^2 \cdot n) for Yule-Walker or O(p^2 \cdot n)
- Prediction: O(p) for each forecasted step.
- Stationary time series with autocorrelation structure
- Short-term forecasting where recent values are predictive
- Data with no significant moving average component
- When a simple, interpretable model is desired
References
Box2015
Box, G. E., Jenkins, G. M., Reinsel, G. C., & Ljung, G. M. (2015).
Time series analysis: forecasting and control.
John Wiley & Sons.
See Also
python
>>> import numpy as np
>>> from tuiml.algorithms.timeseries import AR
>>> # Generate a simple AR(1) process
>>> np.random.seed(42)
>>> n = 100
>>> y = np.zeros(n)
>>> for t in range(1, n):
... y[t] = 0.5 * y[t-1] + np.random.normal()
>>> model = AR(order=1)
>>> model.fit(y)
>>> # Forecast the next 3 steps
>>> forecast = model.predict(steps=3)
Methods
predict
(self, steps: int=1, _X: Optional[np.ndarray]=None) -> np.ndarray
predict
(self, steps: int=1, _X: Optional[np.ndarray]=None) -> np.ndarray
Forecast future values using the fitted AR model.
Parameters
steps
int
= 1
Number of future time steps to forecast.
_X
np.ndarray
= None
Ignored. Present for API consistency with regressors.
Returns
forecast
np.ndarray of shape (steps,)
Forecasted values.
fit_predict
(self, y: np.ndarray, steps: int=1) -> np.ndarray
fit_predict
(self, y: np.ndarray, steps: int=1) -> np.ndarray
Fit the model and forecast future values in one step.
Parameters
y
np.ndarray of shape (n_samples,)
Time series values to fit.
steps
int
= 1
Number of future time steps to forecast.
Returns
forecast
np.ndarray of shape (steps,)
Forecasted values.