Moving Average (MA) models for univariate time series forecasting.

Classes

MA

class algorithms.timeseries.ma.MA(Regressor)

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:

  1. Estimate the series mean \mu and center the data.
  2. Obtain initial residual estimates using the Hannan-Rissanen method
(fit a high-order AR model to approximate residuals).
  1. Estimate the MA coefficients via OLS regression on lagged residuals.
  2. Refine parameters iteratively using conditional sum of squares (CSS).
  3. Forecast by applying MA coefficients to known past errors, with
future errors set to zero (their expected value).

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.
Moving average processes are always stationary, and they provide a way to model short-term shocks that persist for q periods.

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
the number of samples and q is the order.
  • Prediction: O(q) for each forecasted step.
When to use MA:
  • 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.
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

get_parameter_schema (cls) -> Dict[str, Dict[str, Any]]

Return JSON Schema for algorithm parameters.

get_capabilities (cls) -> List[str]

Return supported capabilities.

get_complexity (cls) -> str

Return complexity analysis.

get_references (cls) -> List[str]

Return academic citations.

fit (self, y: np.ndarray, _X: Optional[np.ndarray]=None) -> 'MA'

Fit the moving average model to data.

Parameters
y
np.ndarray of shape (n_samples,)
Time series values to fit.
_X
np.ndarray = None
Ignored. Present for API consistency with regressors.
Returns
self
MA
Fitted estimator.
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 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.