Theta method for univariate time series forecasting.
Classes
Theta method for univariate forecasting by curvature decomposition.
__init__( self, theta: float = 2.0, alpha: float | None = None, season_length: int | None = None, seasonal: str = 'mul', seasonality_test: bool = True, )
Overview
- Optionally deseasonalise the series with a classical decomposition
season_length is given and a seasonality test fires.
- Fit an ordinary least-squares line a + b t to the
- Build the theta line
- Extrapolate the \theta = 0 line by simple linear
- Combine the two extrapolations with weights 1/\theta and
Theory
A theta line rescales the second differences of the series,
so \theta = 0 removes all curvature (a straight line) and \theta > 1 amplifies it. The solution of that difference equation with the two boundary conditions that minimise the squared deviation from the data is
with a, b the OLS intercept and slope of y on t = 1, \dots, n. The combined forecast is
where \ell_n(Z_{\theta}) is the SES level of the theta line.
Equivalence with SES plus drift. Hyndman and Billah (2003) showed that for \theta = 2 and equal weights the method is exactly simple exponential smoothing with a drift of b / 2:
where \ell_n is the SES level of the original series initialised at \ell_0 = y_1. This implementation reproduces that identity to machine precision.
Parameters
theta
theta=2 gives the classic Theta method.
alpha
None it is chosen on a deterministic grid by minimising the in-sample sum of squared one-step errors of the theta line.
season_length
None (or 1) no seasonal adjustment is attempted.
seasonal
"mul" falls back to "add" when the series is not strictly positive.
seasonality_test
True, seasonal adjustment is applied only if the autocorrelation at lag season_length is significant at the 90% level.
Attributes
alpha_
intercept_
slope_
level_
drift_
seasonal_indices_
season_length.
is_seasonal_
seasonal_mode_
"mul" or "add".
fitted_values_
resid_
n_obs_
Notes
Complexity:
-
Training: O(n) for a fixed
alpha, O(gn) when
alpha is optimised over a grid of g values.
- Prediction: O(h).
- Short and medium series where a robust, low-variance benchmark is
- Series with a clear local trend that should be damped rather than
-
Monthly or quarterly business data, combined with
season_length. - As a baseline against which ARIMA or exponential smoothing is judged.
References
See Also
>>> import numpy as np
>>> from tuiml.algorithms.timeseries.theta import ThetaForecaster
>>> y = np.arange(1.0, 21.0)
>>> model = ThetaForecaster(theta=2.0, alpha=0.3).fit(y)
>>> forecast = model.predict(steps=3)
>>> forecast.shape
(3,)
>>> bool(np.all(np.diff(forecast) > 0))
True
Methods
fit
(self, y: np.ndarray, X: Optional[np.ndarray]=None) -> 'ThetaForecaster'
fit
(self, y: np.ndarray, X: Optional[np.ndarray]=None) -> 'ThetaForecaster'
Fit the Theta model to a univariate series.
Parameters
y
X
Returns
self
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 of the series.
Parameters
steps
X
Returns
forecast