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_loader(): df = pd.read_csv(PEYTON_FILE, nrows=100) df["A"] = np.arange(len(df)) df["B"] = np.arange(len(df)) * 0.1 df1 = df[:50] df2 = df[50:] m = NeuralProphet( yearly_seasonality=True, weekly_seasonality=True, daily_seasonality=True, n_lags=3, n_forecasts=2, ) m.add_future_regressor("A") m.add_lagged_regressor("B") config_normalization = configure.Normalization("auto", False, True, False) df_dict = {"df1": df1.copy(), "df2": df2.copy()} config_normalization.init_data_params(df_dict, m.config_covar, m.regressors_config, m.events_config) m.config_normalization = config_normalization df_dict = m._normalize(df_dict) dataset = m._create_dataset(df_dict, predict_mode=False) loader = DataLoader(dataset, batch_size=min(1024, len(df)), shuffle=True, drop_last=False) for inputs, targets, meta in loader: assert set(meta["df_name"]) == set(df_dict.keys()) break
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_globaltimedataset(): df = pd.read_csv(PEYTON_FILE, nrows=100) df1 = df[:50] df2 = df[50:] m1 = NeuralProphet( yearly_seasonality=True, weekly_seasonality=True, daily_seasonality=True, ) m2 = NeuralProphet( n_lags=3, n_forecasts=2, ) m3 = NeuralProphet() # TODO m3.add_country_holidays("US") config_normalization = configure.Normalization("auto", False, True, False) for m in [m1, m2, m3]: df_dict = {"df1": df1.copy(), "df2": df2.copy()} config_normalization.init_data_params(df_dict, m.config_covar, m.regressors_config, m.events_config) m.config_normalization = config_normalization df_dict = m._normalize(df_dict) dataset = m._create_dataset(df_dict, predict_mode=False) dataset = m._create_dataset(df_dict, predict_mode=True) # lagged_regressors, future_regressors df4 = df.copy() df4["A"] = np.arange(len(df4)) df4["B"] = np.arange(len(df4)) * 0.1 m4 = NeuralProphet(n_lags=2) m4.add_future_regressor("A") m4.add_lagged_regressor("B") config_normalization = configure.Normalization("auto", False, True, False) for m in [m4]: df_dict = {"df4": df4.copy()} config_normalization.init_data_params(df_dict, m.config_covar, m.regressors_config, m.events_config) m.config_normalization = config_normalization df_dict = m._normalize(df_dict) dataset = m._create_dataset(df_dict, predict_mode=False) dataset = m._create_dataset(df_dict, predict_mode=True)
def test_add_lagged_regressors(self): NROWS = 512 EPOCHS = 3 BATCH_SIZE = 32 df = pd.read_csv(PEYTON_FILE, nrows=NROWS) 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() col_dict = { "1": "A", "2": ["B"], "3": ["A", "B", "C"], } for key, value in col_dict.items(): log.debug(value) if isinstance(value, list): feats = np.array(["ds", "y"] + value) else: feats = np.array(["ds", "y", value]) df1 = pd.DataFrame(df, columns=feats) cols = [col for col in df1.columns if col not in ["ds", "y"]] m = NeuralProphet( n_forecasts=1, n_lags=3, weekly_seasonality=False, daily_seasonality=False, epochs=EPOCHS, batch_size=BATCH_SIZE, ) m = m.add_lagged_regressor(names=cols) metrics_df = m.fit(df1, freq="D", validate_each_epoch=True) future = m.make_future_dataframe(df1, n_historic_predictions=365) ## Check if the future dataframe contains all the lagged regressors check = any(item in future.columns for item in cols) forecast = m.predict(future) log.debug(check)