def test_seasons(self): log.info("testing: Seasonality") df = pd.read_csv(PEYTON_FILE, nrows=512) # m = NeuralProphet(n_lags=60, n_changepoints=10, n_forecasts=30, verbose=True) m = NeuralProphet( yearly_seasonality=8, weekly_seasonality=4, # daily_seasonality=False, seasonality_mode="additive", # seasonality_mode="multiplicative", seasonality_reg=1, epochs=EPOCHS, ) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=len(df), periods=365) forecast = m.predict(df=future) log.debug("SUM of yearly season params: {}".format( sum(abs(m.model.season_params["yearly"].data.numpy())))) log.debug("SUM of weekly season params: {}".format( sum(abs(m.model.season_params["weekly"].data.numpy())))) log.debug("season params: {}".format(m.model.season_params.items())) if self.plot: m.plot(forecast) # m.plot_components(forecast) m.plot_parameters() plt.show()
def test_custom_changepoints(self): log.info("testing: Custom Changepoints") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) dates = df["ds"][range(1, len(df) - 1, int(len(df) / 5.0))] dates_list = [str(d) for d in dates] dates_array = pd.to_datetime(dates_list).values log.debug("dates: {}".format(dates)) log.debug("dates_list: {}".format(dates_list)) log.debug("dates_array: {} {}".format(dates_array.dtype, dates_array)) for cp in [dates_list, dates_array]: m = NeuralProphet( changepoints=cp, yearly_seasonality=False, weekly_seasonality=False, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) # print(m.config_trend) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, periods=60, n_historic_predictions=60) forecast = m.predict(df=future) if self.plot: # m.plot(forecast) # m.plot_components(forecast) m.plot_parameters() plt.show()
def test_custom_seasons(self): log.info("testing: Custom Seasonality") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) # m = NeuralProphet(n_lags=60, n_changepoints=10, n_forecasts=30, verbose=True) other_seasons = False m = NeuralProphet( yearly_seasonality=other_seasons, weekly_seasonality=other_seasons, daily_seasonality=other_seasons, seasonality_mode="additive", # seasonality_mode="multiplicative", seasonality_reg=1, epochs=EPOCHS, batch_size=BATCH_SIZE, ) m = m.add_seasonality(name="quarterly", period=90, fourier_order=5) log.debug("seasonalities: {}".format(m.season_config.periods)) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=365, periods=365) forecast = m.predict(df=future) log.debug("season params: {}".format(m.model.season_params.items())) if self.plot: m.plot(forecast) # m.plot_components(forecast) m.plot_parameters() plt.show()
def test_future_reg(self): log.info("testing: Future Regressors") df = pd.read_csv(PEYTON_FILE) m = NeuralProphet( n_forecasts=1, n_lags=0, epochs=EPOCHS, ) df["A"] = df["y"].rolling(7, min_periods=1).mean() df["B"] = df["y"].rolling(30, min_periods=1).mean() m = m.add_future_regressor(name="A", regularization=0.5) m = m.add_future_regressor(name="B", mode="multiplicative", regularization=0.3) metrics_df = m.fit(df, freq="D") regressors_df = pd.DataFrame(data={ "A": df["A"][:50], "B": df["B"][:50] }) future = m.make_future_dataframe(df=df, regressors_df=regressors_df, n_historic_predictions=10, periods=50) forecast = m.predict(df=future) if self.plot: # print(forecast.to_string()) # m.plot_last_forecast(forecast, include_previous_forecasts=3) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_lag_reg_deep(self): log.info("testing: List of Lagged Regressors (deep)") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) m = NeuralProphet( n_forecasts=1, n_lags=14, num_hidden_layers=2, d_hidden=32, weekly_seasonality=False, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) df["A"] = df["y"].rolling(7, min_periods=1).mean() df["B"] = df["y"].rolling(15, min_periods=1).mean() df["C"] = df["y"].rolling(30, min_periods=1).mean() cols = [col for col in df.columns if col not in ["ds", "y"]] m = m.add_lagged_regressor(names=cols) m.highlight_nth_step_ahead_of_each_forecast(m.n_forecasts) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=365) forecast = m.predict(future) if self.plot: # print(forecast.to_string()) # m.plot_last_forecast(forecast, include_previous_forecasts=10) # m.plot(forecast) # m.plot_components(forecast) m.plot_parameters() plt.show()
def test_lag_reg(self): log.info("testing: Lagged Regressors") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) m = NeuralProphet( n_forecasts=2, n_lags=3, weekly_seasonality=False, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) df["A"] = df["y"].rolling(7, min_periods=1).mean() df["B"] = df["y"].rolling(30, min_periods=1).mean() m = m.add_lagged_regressor(names="A") m = m.add_lagged_regressor(names="B", only_last_value=True) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=10) forecast = m.predict(future) if self.plot: print(forecast.to_string()) m.plot_last_forecast(forecast, include_previous_forecasts=5) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_future_reg(self): log.info("testing: Future Regressors") df = pd.read_csv(PEYTON_FILE, nrows=NROWS + 50) m = NeuralProphet( epochs=EPOCHS, batch_size=BATCH_SIZE, ) df["A"] = df["y"].rolling(7, min_periods=1).mean() df["B"] = df["y"].rolling(30, min_periods=1).mean() regressors_df_future = pd.DataFrame(data={ "A": df["A"][-50:], "B": df["B"][-50:] }) df = df[:-50] m = m.add_future_regressor(name="A") m = m.add_future_regressor(name="B", mode="multiplicative") metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df=df, regressors_df=regressors_df_future, n_historic_predictions=10, periods=50) forecast = m.predict(df=future) if self.plot: m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_plot(self): log.info("testing: Plotting") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) m = NeuralProphet( n_forecasts=7, n_lags=14, epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics_df = m.fit(df, freq="D") m.highlight_nth_step_ahead_of_each_forecast(7) future = m.make_future_dataframe(df, n_historic_predictions=10) forecast = m.predict(future) m.plot(forecast) m.plot_last_forecast(forecast, include_previous_forecasts=10) m.plot_components(forecast) m.plot_parameters() m.highlight_nth_step_ahead_of_each_forecast(None) future = m.make_future_dataframe(df, n_historic_predictions=10) forecast = m.predict(future) m.plot(forecast) m.plot_last_forecast(forecast) m.plot_components(forecast) m.plot_parameters() if self.plot: plt.show()
def test_trend(self): log.info("testing: Trend") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) m = NeuralProphet( growth="linear", n_changepoints=10, changepoints_range=0.9, trend_reg=1, trend_reg_threshold=False, yearly_seasonality=False, weekly_seasonality=False, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) # print(m.config_trend) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, periods=60, n_historic_predictions=60) forecast = m.predict(df=future) if self.plot: m.plot(forecast) # m.plot_components(forecast) m.plot_parameters() plt.show()
def test_lag_reg(self): log.info("testing: Lagged Regressors") df = pd.read_csv(PEYTON_FILE) m = NeuralProphet( n_forecasts=3, n_lags=7, ar_sparsity=0.1, # num_hidden_layers=2, # d_hidden=64, # yearly_seasonality=False, # weekly_seasonality=False, # daily_seasonality=False, epochs=EPOCHS, ) if m.n_lags > 0: df["A"] = df["y"].rolling(7, min_periods=1).mean() df["B"] = df["y"].rolling(30, min_periods=1).mean() m = m.add_lagged_regressor(name="A") m = m.add_lagged_regressor(name="B", only_last_value=True) # m.highlight_nth_step_ahead_of_each_forecast(m.n_forecasts) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=365) forecast = m.predict(future) if self.plot: # print(forecast.to_string()) m.plot_last_forecast(forecast, include_previous_forecasts=10) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_ar_net(self): log.info("testing: AR-Net") df = pd.read_csv(PEYTON_FILE) m = NeuralProphet( n_forecasts=7, n_lags=14, # ar_sparsity=0.01, # num_hidden_layers=0, num_hidden_layers=2, d_hidden=64, # yearly_seasonality=False, # weekly_seasonality=False, # daily_seasonality=False, epochs=EPOCHS, ) m.highlight_nth_step_ahead_of_each_forecast(m.n_forecasts) metrics_df = m.fit(df, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df, n_historic_predictions=len(df) - m.n_lags) forecast = m.predict(df=future) if self.plot: m.plot_last_forecast(forecast, include_previous_forecasts=3) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_events(self): log.info("testing: Events") df = pd.read_csv(PEYTON_FILE)[-NROWS:] playoffs = pd.DataFrame( { "event": "playoff", "ds": pd.to_datetime( [ "2008-01-13", "2009-01-03", "2010-01-16", "2010-01-24", "2010-02-07", "2011-01-08", "2013-01-12", "2014-01-12", "2014-01-19", "2014-02-02", "2015-01-11", "2016-01-17", "2016-01-24", "2016-02-07", ] ), } ) superbowls = pd.DataFrame( { "event": "superbowl", "ds": pd.to_datetime(["2010-02-07", "2014-02-02", "2016-02-07"]), } ) events_df = pd.concat((playoffs, superbowls)) m = NeuralProphet( n_lags=2, n_forecasts=30, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) # set event windows m = m.add_events( ["superbowl", "playoff"], lower_window=-1, upper_window=1, mode="multiplicative", regularization=0.5 ) # add the country specific holidays m = m.add_country_holidays("US", mode="additive", regularization=0.5) history_df = m.create_df_with_events(df, events_df) metrics_df = m.fit(history_df, freq="D") future = m.make_future_dataframe(df=history_df, events_df=events_df, periods=30, n_historic_predictions=90) forecast = m.predict(df=future) log.debug("Event Parameters:: {}".format(m.model.event_params)) if self.plot: m.plot_components(forecast) m.plot(forecast) m.plot_parameters() plt.show()
def test_predict(self): log.info("testing: Predict") df = pd.read_csv(PEYTON_FILE, nrows=512) m = NeuralProphet( n_forecasts=3, n_lags=5, epochs=1, ) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, periods=None, n_historic_predictions=len(df) - m.n_lags) forecast = m.predict(future) if self.plot: m.plot_last_forecast(forecast, include_previous_forecasts=10) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_yosemite(self): log.info("TEST Yosemite Temps") df = pd.read_csv(YOS_FILE, nrows=NROWS) m = NeuralProphet( changepoints_range=0.95, n_changepoints=15, weekly_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics = m.fit(df, freq="5min") future = m.make_future_dataframe(df, periods=12 * 24, n_historic_predictions=12 * 24) forecast = m.predict(future) if self.plot: m.plot(forecast) m.plot_parameters() plt.show()
def test_air_data(self): log.info("TEST air_passengers.csv") df = pd.read_csv(AIR_FILE) m = NeuralProphet( n_changepoints=0, # trend_reg=1, yearly_seasonality=2, # seasonality_reg=1, # seasonality_mode="additive", seasonality_mode="multiplicative", ) metrics = m.fit(df, freq="MS") future = m.make_future_dataframe(df, periods=48, n_historic_predictions=len(df) - m.n_lags) forecast = m.predict(future) m.plot(forecast) # m.plot_components(forecast) m.plot_parameters() if self.plot: plt.show()
def test_seasons(): log.info("testing: Seasonality: additive") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) # m = NeuralProphet(n_lags=60, n_changepoints=10, n_forecasts=30, verbose=True) m = NeuralProphet( yearly_seasonality=8, weekly_seasonality=4, seasonality_mode="additive", seasonality_reg=1, epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, n_historic_predictions=365, periods=365) forecast = m.predict(df=future) log.debug("SUM of yearly season params: {}".format( sum(abs(m.model.season_params["yearly"].data.numpy())))) log.debug("SUM of weekly season params: {}".format( sum(abs(m.model.season_params["weekly"].data.numpy())))) log.debug("season params: {}".format(m.model.season_params.items())) if PLOT: m.plot(forecast) # m.plot_components(forecast) m.plot_parameters() plt.show() log.info("testing: Seasonality: multiplicative") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) # m = NeuralProphet(n_lags=60, n_changepoints=10, n_forecasts=30, verbose=True) m = NeuralProphet( yearly_seasonality=8, weekly_seasonality=4, seasonality_mode="multiplicative", epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, n_historic_predictions=365, periods=365) forecast = m.predict(df=future)
def test_no_trend(): log.info("testing: No-Trend") df = pd.read_csv(PEYTON_FILE, nrows=512) m = NeuralProphet( growth="off", yearly_seasonality=False, weekly_seasonality=False, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) # m.highlight_nth_step_ahead_of_each_forecast(m.n_forecasts) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, periods=60, n_historic_predictions=60) forecast = m.predict(df=future) if PLOT: m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_ar(self): log.info("testing: AR") df = pd.read_csv(PEYTON_FILE, nrows=NROWS) m = NeuralProphet( n_forecasts=7, n_lags=7, yearly_seasonality=False, epochs=EPOCHS, # batch_size=BATCH_SIZE, ) m.highlight_nth_step_ahead_of_each_forecast(m.n_forecasts) metrics_df = m.fit(df, freq="D") future = m.make_future_dataframe(df, n_historic_predictions=90) forecast = m.predict(df=future) if self.plot: m.plot_last_forecast(forecast, include_previous_forecasts=3) m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def test_air_data(): log.info("TEST air_passengers.csv") df = pd.read_csv(AIR_FILE) m = NeuralProphet( n_changepoints=0, yearly_seasonality=2, seasonality_mode="multiplicative", epochs=EPOCHS, batch_size=BATCH_SIZE, ) metrics = m.fit(df, freq="MS") future = m.make_future_dataframe(df, periods=48, n_historic_predictions=len(df) - m.n_lags) forecast = m.predict(future) if PLOT: m.plot(forecast) m.plot_components(forecast) m.plot_parameters() plt.show()
def time_pattern(): global target, daypara, df, df2, df_4pycaret, df_temp EPOCH = st.sidebar.slider("Epochs", 100, 1000) model = NeuralProphet( growth="linear", changepoints=None, n_changepoints=30, changepoints_range=0.95, trend_reg=0, trend_reg_threshold=False, yearly_seasonality="auto", weekly_seasonality=True, daily_seasonality="auto", seasonality_mode="additive", seasonality_reg=0, n_forecasts=30, n_lags=60, ##determines autoregression num_hidden_layers=0, d_hidden=None, ar_sparsity=None, learning_rate=None, epochs=EPOCH, loss_func="Huber", normalize="auto", impute_missing=True, ) metrics = model.fit(df2, validate_each_epoch=True, freq="D") future = model.make_future_dataframe(df2, periods=252, n_historic_predictions=len(df2)) with st.spinner("Training..."): forecast = model.predict(future) fig, ax = plt.subplots(1, 2, figsize=(17, 7)) ax[0].plot(metrics["MAE"], 'ob', linewidth=6, label="Training Loss") ax[0].plot(metrics["MAE_val"], '-r', linewidth=2, label="Validation Loss") ax[0].legend(loc='center right') ax[0].tick_params(axis='both', which='major') ax[0].set_xlabel("Epoch") ax[0].set_ylabel("Loss") ax[0].set_title("Model Loss (MAE)") ax[1].plot(metrics["SmoothL1Loss"], 'ob', linewidth=6, label="Training Loss") ax[1].plot(metrics["SmoothL1Loss_val"], '-r', linewidth=2, label="Validation Loss") ax[1].legend(loc='center right') ax[1].tick_params(axis='both', which='major') ax[1].set_xlabel("Epoch") ax[1].set_ylabel("Loss") ax[1].set_title("Model Loss (SmoothL1Loss)") st.subheader("Loss Check") st.pyplot() with st.spinner("Recognizing Time Pattern"): st.subheader("Time Pattern") model.plot_parameters() st.set_option('deprecation.showPyplotGlobalUse', False) st.pyplot()
weekly_seasonality=False, batch_size=64, epochs=10, learning_rate=1.0, ) metrics = m.fit(df, freq='5min') future = m.make_future_dataframe(df, n_historic_predictions=True) forecast = m.predict(future) fig = m.plot(forecast) plt.show() fig = m.plot(forecast[144:6 * 288]) plt.show() # fig_comp = m.plot_components(forecast) fig_param = m.plot_parameters() plt.show() ''' Larger forecast horizon¶ For predictions further into the future, you could reduce the resulution of the data. Using a 5-minute resolution may be useful for a high-resolution short-term forecast, but counter-productive for a long-term forecast. As we only have a limited amount of data (approx 2 months), we want to avoid over-specifying the model. As an example: If we set the model to forecast 24 hours into the future (nforecasts=24*12) based on the last day's temperatures (n_lags=24*12), the number of parameters of our AR component grows to 24*12*24*12 = 82,944. However, we only have about 2*30*24*12 = 17,280 samples in our dataset. The model would be overspecified. If we first downsample our data to hourly data, we reduce our dataset to 2*30*24=1440 and our model parameters to 24*24=576. Thus, we are able to fit the model. However, it would be better to collect more data. ''' df.loc[:, "ds"] = pd.to_datetime(df.loc[:, "ds"]) df_hourly = df.set_index('ds', drop=False).resample('H').mean().reset_index() m = NeuralProphet( n_lags=24,
def test_global_modeling_no_exogenous_variable(): ### GLOBAL MODELLING - NO EXOGENOUS VARIABLE log.info("Global Modeling - No exogenous variables") df = pd.read_csv(PEYTON_FILE, nrows=512) df1_0 = df.iloc[:128, :].copy(deep=True) df2_0 = df.iloc[128:256, :].copy(deep=True) df3_0 = df.iloc[256:384, :].copy(deep=True) df4_0 = df.iloc[384:, :].copy(deep=True) train_input = { 0: df1_0, 1: { "df1": df1_0, "df2": df2_0 }, 2: { "df1": df1_0, "df2": df2_0 } } test_input = {0: df3_0, 1: {"df1": df3_0}, 2: {"df1": df3_0, "df2": df4_0}} info_input = { 0: "Testing df train / df test - no events, no regressors", 1: "Testing dict df train / df test - no events, no regressors", 2: "Testing dict df train / dict df test - no events, no regressors", } for i in range(0, 3): log.info(info_input[i]) m = NeuralProphet(n_forecasts=2, n_lags=10, epochs=EPOCHS, batch_size=BATCH_SIZE) metrics = m.fit(train_input[i], freq="D") forecast = m.predict(df=test_input[i]) forecast_trend = m.predict_trend(df=test_input[i]) forecast_seasonal_componets = m.predict_seasonal_components( df=test_input[i]) if PLOT: forecast = forecast if isinstance(forecast, list) else [forecast] for key in forecast: fig1 = m.plot(forecast[key]) fig2 = m.plot(forecast[key]) with pytest.raises(ValueError): forecast = m.predict({"df4": df4_0}) log.info( "Error - dict with names not provided in the train dict (not in the data params dict)" ) with pytest.raises(ValueError): metrics = m.test({"df4": df4_0}) log.info( "Error - dict with names not provided in the train dict (not in the data params dict)" ) m = NeuralProphet( n_forecasts=2, n_lags=10, epochs=EPOCHS, batch_size=BATCH_SIZE, ) m.fit({"df1": df1_0, "df2": df2_0}, freq="D") with pytest.raises(ValueError): forecast = m.predict({"df4": df4_0}) # log.info("unknown_data_normalization was not set to True") with pytest.raises(ValueError): metrics = m.test({"df4": df4_0}) # log.info("unknown_data_normalization was not set to True") with pytest.raises(ValueError): forecast_trend = m.predict_trend({"df4": df4_0}) # log.info("unknown_data_normalization was not set to True") with pytest.raises(ValueError): forecast_seasonal_componets = m.predict_seasonal_components( {"df4": df4_0}) # log.info("unknown_data_normalization was not set to True") # Set unknown_data_normalization to True - now there should be no errors m.config_normalization.unknown_data_normalization = True forecast = m.predict({"df4": df4_0}) metrics = m.test({"df4": df4_0}) forecast_trend = m.predict_trend({"df4": df4_0}) forecast_seasonal_componets = m.predict_seasonal_components({"df4": df4_0}) m.plot_parameters(df_name="df1") m.plot_parameters()
# %% model_arima.summary() # %% Learning rates for ARNet for loss in mae_np: np.log(loss['SmoothL1Loss'][1:]).plot() # %% Error against lag final_loss = [loss['SmoothL1Loss'].iloc[-1] for loss in mae_np] plt.plot(final_loss) # %% model_nprophet_ar.model.ar_weights.shape mae_np[-1].plot() model_nprophet_ar.model.bias model_nprophet_ar = model_nprophet_ar.highlight_nth_step_ahead_of_each_forecast(1) fig_param = model_nprophet_ar.plot_parameters() model_nprophet_ar # %% fig = res.plot_diagnostics(fig=fig, lags=30) fig = res.plot_predict(720, 840)