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_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_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_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()
# %% 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)