Moving Average (MA) models for univariate time series forecasting.
Classes
Moving Average (MA) model for univariate time series forecasting.
The \text{MA}(q) model predicts the next value in a time series as a linear combination of past forecast errors (residuals). Unlike the autoregressive model, it uses errors instead of past values.
Constructor
__init__( self, order: int = 1, method: str = 'hannan_rissanen', max_iter: int = 100, )
Overview
The MA model works through the following steps:
- Estimate the series mean \mu and center the data.
- Obtain initial residual estimates using the Hannan-Rissanen method
- Estimate the MA coefficients via OLS regression on lagged residuals.
- Refine parameters iteratively using conditional sum of squares (CSS).
- Forecast by applying MA coefficients to known past errors, with
Theory
The \text{MA}(q) process is defined as:
y_t = \mu + \epsilon_t + \theta_1 \epsilon_{t-1} + \theta_2 \epsilon_{t-2} + \dots + \theta_q \epsilon_{t-q}
where:
- y_t: The value of the time series at time t.
- \mu: The mean of the series.
- \theta_i: The moving average parameters (coefficients).
- q: The order of the model (number of lagged errors).
- \epsilon_t: The white noise error term at time t.
Parameters
order
int
= 1
The order :math:`q` of the MA model (number of lagged errors).
method
{"hannan_rissanen", "css", "mle"}
= "hannan_rissanen"
The method used to estimate the MA parameters:
- •
"hannan_rissanen": Two-step regression for initial estimates. - •
"css": Conditional Sum of Squares estimation. - •
"mle": Simplified Maximum Likelihood Estimation.
max_iter
int
= 100
Maximum iterations for iterative estimation methods.
Attributes
ma_params_
np.ndarray of shape (order,)
Fitted moving average coefficients :math:`(\\theta_1, \\theta_2, \dots, \\theta_q)`.
mu_
float
The estimated mean of the series.
resid_
np.ndarray of shape (n_obs,)
The estimated residuals 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(q \cdot n \cdot \text{max\_iter}) where n is
- Prediction: O(q) for each forecasted step.
- Stationary time series driven by short-term shocks
- When the autocorrelation function (ACF) cuts off after lag q
- Data where past errors are more informative than past values
- Modeling noise structure in residuals from other models
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 MA
>>> # Generate a simple MA(1) process
>>> np.random.seed(42)
>>> n = 100
>>> eps = np.random.normal(size=n)
>>> y = 10.0 + eps[1:] + 0.6 * eps[:-1]
>>> model = MA(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 MA 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.