mojolearn.ExponentialSmoothing#
- class mojolearn.ExponentialSmoothing(endog, *, seasonal='additive', seasonal_periods=2, start_periods=2, ts_num=1, eps=0.00224, initialization_method='estimated')[source]#
Bases:
objectHolt-Winters exponential smoothing for a batch of seasonal series.
Fits level, trend and season with additive or multiplicative seasonality, following
cuml.tsa.ExponentialSmoothing. As in cuML (and unlike scikit-learn), the data goes to the constructor asendog, fit() takes no arguments, and forecast() produces forecasts. predict(), save() and load() are additions: a saved model forecasts and predicts without refitting, also on a CPU-only install.The default
initialization_method="estimated"differs from cuML: it estimates the initial level, trend and all seasonal states jointly with the smoothing parameters by minimizing the SSE over all points (statsmodels’ definition)."heuristic"(alias"cuml") reproduces cuML’s fit: initial states from a moving-average decomposition of the firststart_periodsseasons, then BFGS over the smoothing parameters only. The heuristic fit is faster and usually less accurate.Computation is float32; float64 input is converted (and copied). Non-finite values are refused with the series and position, and multiplicative seasonality needs strictly positive data. The numeric mode is the process default (see
mojolearn.set_numeric_mode()).- Parameters:
endog (array-like of shape (ts_num, n) or (n,)) – The series, one per row; 1-D is one series.
nmust be at leaststart_periods * seasonal_periods.seasonal ({“additive”, “add”, “multiplicative”, “mul”}, default “additive”) – Seasonal component type.
seasonal_periods (int, default 2) – Season length, at least 2.
start_periods (int, default 2) – Seasons used to seed the initial states, between 2 and
seasonal_periods.ts_num (int, default 1) – Number of series; must match
endog.eps (float, default 2.24e-3) – Optimizer tolerance, greater than 0 (cuML’s default).
initialization_method ({“estimated”, “heuristic”, “cuml”}, default “estimated”) – How initial states are found, as described above.
- Variables:
season_ (level_, trend_,) – Fitted components.
sse_ (Array of shape (ts_num,), float32) – Sum of squared errors: over all
npoints under"estimated", over pointsseasonal_periodston - 1under"heuristic"(cuML’s SSE).gamma_ (alpha_, beta_,) – Fitted smoothing parameters.
n_iter_ (Array of shape (ts_num,), int32) – Optimizer iterations.
criterion_ (Array of shape (ts_num,), int32) – Why the optimizer stopped. Under
"heuristic"(BFGS): 0 iteration limit, 1 parameter change below tolerance, 2 error change below tolerance, 3 gradient norm below tolerance. Under"estimated"(Levenberg-Marquardt): 0 iteration limit, 1 no step lowers the SSE, 2 relative SSE decrease below 1e-6.
Notes
cuML’s
verboseandoutput_typeare not accepted. Results are float32mojolearn.Arrayobjects. When the BFGS line search hits its iteration limit, the lowest-loss trial point is kept rather than the last one, so such fits can differ from cuML’s.Examples
>>> import numpy as np, mojolearn >>> t = np.arange(96, dtype=np.float32) >>> y = 20 + 0.1 * t + 3 * np.sin(2 * np.pi * t / 12).astype(np.float32) >>> model = mojolearn.ExponentialSmoothing(y, seasonal_periods=12).fit() >>> np.asarray(model.forecast(12)).shape (12,)
- __init__(endog, *, seasonal='additive', seasonal_periods=2, start_periods=2, ts_num=1, eps=0.00224, initialization_method='estimated')[source]#
Methods
fit()Fit level, trend, season and the smoothing parameters.
forecast([h, index])Forecast
hpoints per series.get_level([index])Fitted level component.
get_season([index])Fitted season component.
get_trend([index])Fitted trend component.
load(path)Load a model written by save().
predict([start, end, index])One-step-ahead predictions at times
starttoend - 1.save(path)Save the fitted model to an
.npzfile.score([index])Sum of squared errors of the fit, as cuML's
scorereturns.- fit()[source]#
Fit level, trend, season and the smoothing parameters.
- Returns:
self
- Raises:
ValueError – If a constraint is violated:
ts_num >= 1,seasonal_periods >= 2,start_periods >= 2,seasonal_periods >= start_periods,eps > 0,n >= start_periods * seasonal_periods; or for non-finite (or, multiplicative, non-positive) data.
- forecast(h=1, index=None)[source]#
Forecast
hpoints per series.- Parameters:
h (int, default 1) – Forecast horizon, greater than 0.
index (int or None, default None) – Return only this series.
- Returns:
Array, float32 – Shapes follow cuML:
(h, ts_num)whenindexis None andts_num > 1;(h,)whents_num == 1or for a givenindex.
- predict(start=0, end=None, index=None)[source]#
One-step-ahead predictions at times
starttoend - 1.A time
t < nis in sample: the prediction ofendog[t]from the fitted state before it was observed. Timest < 2 * seasonal_periodsare NaN, because the fit does not keep the state they need. A timet >= nis a forecast, sopredict(n, n + h)equalsforecast(h).- Parameters:
start (int, default 0) – First time index, at least 0.
end (int or None, default None) – Index to stop before; None means
n. Must be greater thanstart.index (int or None, default None) – Return only this series.
- Returns:
Array, float32 – Shapes as for forecast(), with
end - startsteps.
- save(path)[source]#
Save the fitted model to an
.npzfile.The fitted components, smoothing parameters, diagnostics and settings are written;
endogis not. A loaded model forecasts, predicts and exposes the fitted attributes without refitting, including on a CPU-only install (ExponentialSmoothing.load(path)ormojolearn.host_model(path)).- Parameters:
path (str or path-like) – Destination file.
- classmethod load(path)[source]#
Load a model written by save().
- Parameters:
path (str or path-like) – File written by save().
- Returns:
ExponentialSmoothing – A fitted model that forecasts and predicts; it does not refit.
- score(index=None)[source]#
Sum of squared errors of the fit, as cuML’s
scorereturns.- Parameters:
index (int or None, default None) – None returns every series’ SSE; an integer returns that series’ SSE.
- Returns:
Array of shape (ts_num,) or float32
- get_level(index=None)[source]#
Fitted level component.
- Parameters:
index (int or None, default None) – Return only this series.
- Returns:
Array, float32 –
(n - seasonal_periods, ts_num)whenindexis None andts_num > 1; 1-D for one series or a givenindex.