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_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_last_forecast(forecast, include_previous_forecasts=3) 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_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_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_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()
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, n_forecasts=24, changepoints_range=0.95, n_changepoints=30, weekly_seasonality=False, learning_rate=0.3, ) metrics = m.fit(df_hourly, freq='H') future = m.make_future_dataframe(df_hourly, n_historic_predictions=True) forecast = m.predict(future) fig = m.plot(forecast) plt.show() m = m.highlight_nth_step_ahead_of_each_forecast(24) fig = m.plot_last_forecast(forecast, include_previous_forecasts=10) plt.show()