def test_untrained_models(self): model = NaiveDrift() _ = NaiveEnsembleModel([model]) # trained models should raise error model.fit(self.series1) with self.assertRaises(ValueError): NaiveEnsembleModel([model])
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()])
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_backtest_forecasting(self): linear_series = lt(length=50) linear_series_multi = linear_series.stack(linear_series) # univariate model + univariate series score = NaiveDrift().backtest(linear_series, start=pd.Timestamp('20000201'), forecast_horizon=3, metric=r2_score) self.assertEqual(score, 1.0) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, start=pd.Timestamp('20000217'), forecast_horizon=3) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, start=pd.Timestamp('20000217'), forecast_horizon=3, overlap_end=False) NaiveDrift().backtest(linear_series, start=pd.Timestamp('20000216'), forecast_horizon=3) NaiveDrift().backtest(linear_series, start=pd.Timestamp('20000217'), forecast_horizon=3, overlap_end=True) # Using forecast_horizon default value NaiveDrift().backtest(linear_series, start=pd.Timestamp('20000216')) NaiveDrift().backtest(linear_series, start=pd.Timestamp('20000217'), overlap_end=True) # Using an int or float value for start NaiveDrift().backtest(linear_series, start=30) NaiveDrift().backtest(linear_series, start=0.7, overlap_end=True) # Using invalid start and/or forecast_horizon values with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, start=0.7, forecast_horizon=-1) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, start=-0.7, forecast_horizon=1) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, start=100) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, start=1.2) with self.assertRaises(TypeError): NaiveDrift().backtest(linear_series, start='wrong type') with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, start=49, forecast_horizon=2, overlap_end=False) # univariate model + multivariate series with self.assertRaises(AssertionError): NaiveDrift().backtest(linear_series_multi, start=pd.Timestamp('20000201'), forecast_horizon=3) # multivariate model + univariate series if TORCH_AVAILABLE: tcn_model = TCNModel(input_chunk_length=12, output_chunk_length=1, batch_size=1, n_epochs=1) pred = tcn_model.historical_forecasts( linear_series, start=pd.Timestamp('20000125'), forecast_horizon=3, verbose=False, last_points_only=True) self.assertEqual(pred.width, 1) self.assertEqual(pred.end_time(), linear_series.end_time()) # multivariate model + multivariate series with self.assertRaises(ValueError): tcn_model.backtest(linear_series_multi, start=pd.Timestamp('20000125'), forecast_horizon=3, verbose=False) tcn_model = TCNModel(input_chunk_length=12, output_chunk_length=3, batch_size=1, n_epochs=1) pred = tcn_model.historical_forecasts( linear_series_multi, start=pd.Timestamp('20000125'), forecast_horizon=3, verbose=False, last_points_only=True) self.assertEqual(pred.width, 2) self.assertEqual(pred.end_time(), linear_series.end_time())
smape_all = [] 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)
def test_backtest_forecasting(self): linear_series = lt(length=50) linear_series_int = TimeSeries.from_values(linear_series.values()) linear_series_multi = linear_series.stack(linear_series) # univariate model + univariate series score = NaiveDrift().backtest( linear_series, start=pd.Timestamp("20000201"), forecast_horizon=3, metric=r2_score, ) self.assertEqual(score, 1.0) # very large train length should not affect the backtest score = NaiveDrift().backtest( linear_series, train_length=10000, start=pd.Timestamp("20000201"), forecast_horizon=3, metric=r2_score, ) self.assertEqual(score, 1.0) # window of size 2 is too small for naive drift with self.assertRaises(ValueError): NaiveDrift().backtest( linear_series, train_length=2, start=pd.Timestamp("20000201"), forecast_horizon=3, metric=r2_score, ) # test that it also works for time series that are not Datetime-indexed score = NaiveDrift().backtest( linear_series_int, start=0.7, forecast_horizon=3, metric=r2_score ) self.assertEqual(score, 1.0) with self.assertRaises(ValueError): NaiveDrift().backtest( linear_series, start=pd.Timestamp("20000217"), forecast_horizon=3, overlap_end=False, ) NaiveDrift().backtest( linear_series, start=pd.Timestamp("20000216"), forecast_horizon=3 ) NaiveDrift().backtest( linear_series, start=pd.Timestamp("20000217"), forecast_horizon=3, overlap_end=True, ) # Using forecast_horizon default value NaiveDrift().backtest(linear_series, start=pd.Timestamp("20000216")) NaiveDrift().backtest( linear_series, start=pd.Timestamp("20000217"), overlap_end=True ) # Using an int or float value for start NaiveDrift().backtest(linear_series, start=30) NaiveDrift().backtest(linear_series, start=0.7, overlap_end=True) # Set custom train window length NaiveDrift().backtest(linear_series, train_length=10, start=30) # Using invalid start and/or forecast_horizon values with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, start=0.7, forecast_horizon=-1) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, start=-0.7, forecast_horizon=1) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, start=100) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, start=1.2) with self.assertRaises(TypeError): NaiveDrift().backtest(linear_series, start="wrong type") with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, train_length=0, start=0.5) with self.assertRaises(TypeError): NaiveDrift().backtest(linear_series, train_length=1.2, start=0.5) with self.assertRaises(TypeError): NaiveDrift().backtest(linear_series, train_length="wrong type", start=0.5) with self.assertRaises(ValueError): NaiveDrift().backtest( linear_series, start=49, forecast_horizon=2, overlap_end=False ) # univariate model + multivariate series with self.assertRaises(AssertionError): NaiveDrift().backtest( linear_series_multi, start=pd.Timestamp("20000201"), forecast_horizon=3 ) # multivariate model + univariate series if TORCH_AVAILABLE: tcn_model = TCNModel( input_chunk_length=12, output_chunk_length=1, batch_size=1, n_epochs=1 ) pred = tcn_model.historical_forecasts( linear_series, start=pd.Timestamp("20000125"), forecast_horizon=3, verbose=False, last_points_only=True, ) self.assertEqual(pred.width, 1) self.assertEqual(pred.end_time(), linear_series.end_time()) # multivariate model + multivariate series with self.assertRaises(ValueError): tcn_model.backtest( linear_series_multi, start=pd.Timestamp("20000125"), forecast_horizon=3, verbose=False, ) tcn_model = TCNModel( input_chunk_length=12, output_chunk_length=3, batch_size=1, n_epochs=1 ) pred = tcn_model.historical_forecasts( linear_series_multi, start=pd.Timestamp("20000125"), forecast_horizon=3, verbose=False, last_points_only=True, ) self.assertEqual(pred.width, 2) self.assertEqual(pred.end_time(), linear_series.end_time())
def test_backtest_forecasting(self): linear_series = lt(length=50) linear_series_multi = linear_series.stack(linear_series) # univariate model + univariate series pred = NaiveDrift().backtest(linear_series, None, pd.Timestamp('20000201'), 3) self.assertEqual(r2_score(pred, linear_series), 1.0) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, None, start=pd.Timestamp('20000217'), forecast_horizon=3) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, None, start=pd.Timestamp('20000217'), forecast_horizon=3, trim_to_series=True) NaiveDrift().backtest(linear_series, None, start=pd.Timestamp('20000216'), forecast_horizon=3) NaiveDrift().backtest(linear_series, None, pd.Timestamp('20000217'), forecast_horizon=3, trim_to_series=False) # Using forecast_horizon default value NaiveDrift().backtest(linear_series, None, start=pd.Timestamp('20000216')) NaiveDrift().backtest(linear_series, None, pd.Timestamp('20000217'), trim_to_series=False) # Using an int or float value for start NaiveDrift().backtest(linear_series, None, start=30) NaiveDrift().backtest(linear_series, None, start=0.7, trim_to_series=False) # Using invalid start and/or forecast_horizon values with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, None, start=0.7, forecast_horizon=-1) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, None, 0.7, -1) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, None, start=100) with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, None, start=1.2) with self.assertRaises(TypeError): NaiveDrift().backtest(linear_series, None, start='wrong type') with self.assertRaises(ValueError): NaiveDrift().backtest(linear_series, None, start=49, forecast_horizon=2, trim_to_series=True) # univariate model + multivariate series with self.assertRaises(AssertionError): NaiveDrift().backtest(linear_series_multi, None, pd.Timestamp('20000201'), 3) # multivariate model + univariate series if TORCH_AVAILABLE: tcn_model = TCNModel(batch_size=1, n_epochs=1) pred = tcn_model.backtest(linear_series, None, pd.Timestamp('20000125'), 3, verbose=False) self.assertEqual(pred.width, 1) # multivariate model + multivariate series with self.assertRaises(ValueError): tcn_model.backtest(linear_series_multi, None, pd.Timestamp('20000125'), 3, verbose=False) tcn_model = TCNModel(batch_size=1, n_epochs=1, input_size=2, output_length=3) with self.assertRaises(ValueError): tcn_model.backtest(linear_series_multi, None, pd.Timestamp('20000125'), 3, verbose=False, use_full_output_length=False) pred = tcn_model.backtest(linear_series_multi, linear_series_multi[['0']], pd.Timestamp('20000125'), 1, verbose=False, use_full_output_length=True) self.assertEqual(pred.width, 1) pred = tcn_model.backtest(linear_series_multi, linear_series_multi[['1']], pd.Timestamp('20000125'), 3, verbose=False, use_full_output_length=True) self.assertEqual(pred.width, 1) tcn_model = TCNModel(batch_size=1, n_epochs=1, input_size=2, output_length=3, output_size=2) pred = tcn_model.backtest(linear_series_multi, linear_series_multi, pd.Timestamp('20000125'), 3, verbose=False, use_full_output_length=True) self.assertEqual(pred.width, 2)
weight = st.number_input("Today's weight:", min_value=50.0, max_value=750.0, format="%4.1f", step=.1) st.write("Echoing weight: ", weight) sqlStoreWeight(todaysDate, weight) #weightsDF = pd.DataFrame({'date':['7-17-2020','7-18-2020','7-19-2020','7-20-2020','7-21-2020','7-22-2020'], # 'weight':[170.8, 170.4, 170.8, 170.6, 170.6, 745]}) weightsDF = sqlGetWeightsDF() # Would passing todaysEXPTDate here be helpful? if weightsDF.date.size > 30: # Remember, size is rows*cols, so just get the size of a series or the index of the DF glModel = AutoARIMA() else: glModel = NaiveDrift() series = TimeSeries.from_dataframe(weightsDF, time_col='date', value_cols='weight') train, val = series.split_after(pd.Timestamp(todaysDate - dt.timedelta(days=3))) trainTheModel(train) next3daysDF = predict_next(4) print(next3daysDF[0]) plotYmax = weightsDF['weight'].max() + 0.5 plotYmin = weightsDF['weight'].min() - 0.5
def get_local_models(self): return [NaiveDrift(), NaiveSeasonal(5), NaiveSeasonal(10)]
def test_input_models_mixed(self): with self.assertRaises(ValueError): NaiveEnsembleModel([NaiveDrift(), Theta(), RNNModel(12)])