Ejemplo n.º 1
0
    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()
Ejemplo n.º 2
0
    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()
Ejemplo n.º 3
0
    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()
Ejemplo n.º 4
0
    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()
Ejemplo n.º 5
0
    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()
Ejemplo n.º 6
0
 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()
Ejemplo n.º 7
0
    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()
Ejemplo n.º 8
0
 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()
Ejemplo n.º 9
0
    def test_air_data(self):
        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 self.plot:
            m.plot(forecast)
            m.plot_components(forecast)
            m.plot_parameters()
            plt.show()
Ejemplo n.º 10
0
 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()
Ejemplo n.º 11
0
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()