def fallback(mase_all, smape_all, train, test, m): naive = NaiveSeasonal(K=m) naive.fit(train) forecast = naive.predict(len(test)) mase_all.append(np.vstack([ mase_m4(train, test, forecast, m=m), ])) smape_all.append(np.vstack([ smape_m4(test, forecast), ]))
def test_predict_ensemble_local_models(self): naive = NaiveSeasonal(K=5) theta = Theta() naive_ensemble = NaiveEnsembleModel([naive, theta]) naive_ensemble.fit(self.series1 + self.series2) forecast_naive_ensemble = naive_ensemble.predict(5) naive.fit(self.series1 + self.series2) theta.fit(self.series1 + self.series2) forecast_mean = 0.5 * naive.predict(5) + 0.5 * theta.predict(5) self.assertTrue( np.array_equal(forecast_naive_ensemble.values(), forecast_mean.values()))
def test_forecasting_residuals(self): model = NaiveSeasonal(K=1) # test zero residuals constant_ts = ct(length=20) residuals = model.residuals(constant_ts) np.testing.assert_almost_equal(residuals.univariate_values(), np.zeros(len(residuals))) # test constant, positive residuals linear_ts = lt(length=20) residuals = model.residuals(linear_ts) np.testing.assert_almost_equal(np.diff(residuals.univariate_values()), np.zeros(len(residuals) - 1)) np.testing.assert_array_less(np.zeros(len(residuals)), residuals.univariate_values())
def naive2_groe(ts: TimeSeries, n: int, m: int): """ Return the prediction of the naive2 baseline """ # It will be better to use R functions ts_des = ts seasonOut = 1 if m > 1: if check_seasonality(ts, m=int(m), max_lag=2 * m): _, season = extract_trend_and_seasonality(ts, m, model=ModelMode.MULTIPLICATIVE) ts_des = remove_from_series(ts, season, model=ModelMode.MULTIPLICATIVE) seasonOut = season[-m:].shift(m) seasonOut = seasonOut.append_values(seasonOut.values())[:n] naive2 = NaiveSeasonal(K=1) naive2.fit(ts_des) return naive2.predict(n) * seasonOut
def test_fit_univar_ts_with_covariates_for_local_models(self): naive = NaiveEnsembleModel( [NaiveDrift(), NaiveSeasonal(), Theta(), ExponentialSmoothing()]) with self.assertRaises(ValueError): naive.fit(self.series1, self.series2)
def test_input_models_local_models(self): with self.assertRaises(ValueError): NaiveEnsembleModel([]) with self.assertRaises(ValueError): NaiveEnsembleModel( [NaiveDrift, NaiveSeasonal, Theta, ExponentialSmoothing]) with self.assertRaises(ValueError): NaiveEnsembleModel( [NaiveDrift(), NaiveSeasonal, Theta(), ExponentialSmoothing()]) NaiveEnsembleModel( [NaiveDrift(), NaiveSeasonal(), Theta(), ExponentialSmoothing()])
m = int(info_dataset.Frequency[cat[0] + "1"]) for train, test in _build_tqdm_iterator(zip(ts_train, ts_test), verbose=True): train_des = train seasonOut = 1 if m > 1: if check_seasonality(train, m=m, max_lag=2 * m): _, season = extract_trend_and_seasonality( train, m, model=ModelMode.MULTIPLICATIVE) train_des = remove_from_series( train, season, model=ModelMode.MULTIPLICATIVE) seasonOut = season[-m:].shift(m) seasonOut = seasonOut.append_values(seasonOut.values()) seasonOut = seasonOut[:len(test)] naive = NaiveDrift() naive2 = NaiveSeasonal(K=1) naiveSeason = NaiveSeasonal(K=m) ses = ExponentialSmoothing(trend=None, seasonal=None, seasonal_periods=m) holt = ExponentialSmoothing(seasonal=None, damped=False, trend='additive', seasonal_periods=m) damp = ExponentialSmoothing(seasonal=None, damped=True, trend='additive', seasonal_periods=m) naive.fit(train) naive2.fit(train_des) naiveSeason.fit(train)
(ExponentialSmoothing(), 5.6), (ARIMA(12, 2, 1), 10), (ARIMA(1, 1, 1), 40), (StatsForecastAutoARIMA(period=12), 4.8), (Croston(version="classic"), 34), (Croston(version="tsb", alpha_d=0.1, alpha_p=0.1), 34), (Theta(), 11.3), (Theta(1), 20.2), (Theta(-1), 9.8), (FourTheta(1), 20.2), (FourTheta(-1), 9.8), (FourTheta(trend_mode=TrendMode.EXPONENTIAL), 5.5), (FourTheta(model_mode=ModelMode.MULTIPLICATIVE), 11.4), (FourTheta(season_mode=SeasonalityMode.ADDITIVE), 14.2), (FFT(trend="poly"), 11.4), (NaiveSeasonal(), 32.4), (KalmanForecaster(dim_x=3), 17.0), ] if TORCH_AVAILABLE: models += [ (LinearRegressionModel(lags=12), 11.0), (RandomForest(lags=12, n_estimators=200, max_depth=3), 15.5), ] # forecasting models with exogenous variables support multivariate_models = [ (VARIMA(1, 0, 0), 55.6), (VARIMA(1, 1, 1), 57.0), (KalmanForecaster(dim_x=30), 30.0), ]
smape_all = [] m = int(info_dataset.Frequency[freq[0] + '1']) for train, test in _build_tqdm_iterator(zip(ts_train, ts_test), verbose=True): # remove seasonality train_des = train seasonOut = 1 season = constant_timeseries(length=len(train), freq=train.freq_str, start_ts=train.start_time()) if m > 1: if check_seasonality(train, m=m, max_lag=2 * m): pass _, season = extract_trend_and_seasonality(train, m, model=ModelMode.MULTIPLICATIVE) train_des = remove_from_series(train, season, model=ModelMode.MULTIPLICATIVE) seasonOut = season[-m:].shift(m) seasonOut = seasonOut.append_values(seasonOut.values())[:len(test)] # model selection naiveSeason = NaiveSeasonal(K=m) naive2 = NaiveSeasonal(K=1) ses = ExponentialSmoothing(trend=None, seasonal=None, seasonal_periods=m) holt = ExponentialSmoothing(seasonal=None, damped=False, trend='additive', seasonal_periods=m) damp = ExponentialSmoothing(seasonal=None, damped=True, trend='additive', seasonal_periods=m) fourtheta = FourTheta.select_best_model(train, [1, 2, 3], m) theta = Theta(theta=0, season_mode=SeasonalityMode.MULTIPLICATIVE, seasonality_period=m) models_simple = [naiveSeason, theta, fourtheta] models_des = [naive2, ses, holt, damp] # Linear Regression (with constraints) def train_pred(id_start=None, id_end=None): for m in models_simple:
def get_local_models(self): return [NaiveDrift(), NaiveSeasonal(5), NaiveSeasonal(10)]
def test_call_predict_local_models(self): naive_ensemble = NaiveEnsembleModel([NaiveSeasonal(), Theta()]) with self.assertRaises(Exception): naive_ensemble.predict(5) naive_ensemble.fit(self.series1) naive_ensemble.predict(5)
seasonOut = 1 season = constant_timeseries(length=len(train), freq=train.freq_str(), start_ts=train.start_time()) if m > 1: if check_seasonality(train, m=m, max_lag=2 * m): pass _, season = extract_trend_and_seasonality( train, m, model=ModelMode.MULTIPLICATIVE) train_des = remove_from_series( train, season, model=ModelMode.MULTIPLICATIVE) seasonOut = season[-m:].shift(m) seasonOut = seasonOut.append_values( seasonOut.values())[:len(test)] # model selection naiveSeason = NaiveSeasonal(K=m) naive2 = NaiveSeasonal(K=1) ses = ExponentialSmoothing(trend=None, seasonal=None, seasonal_periods=m) holt = ExponentialSmoothing(seasonal=None, damped=False, trend='additive', seasonal_periods=m) damp = ExponentialSmoothing(seasonal=None, damped=True, trend='additive', seasonal_periods=m) fourtheta = FourTheta.select_best_model(train, [1, 2, 3], m) theta = Theta(theta=0,