Repository: ourownstory/neural_prophet

Issue:
Static Trend does not follow required forecasting pattern
**Source Code:**

```
import pandas as pd
from neuralprophet import NeuralProphet, set_log_level, set_random_seed
set_log_level("ERROR")

data_location = "https://raw.githubusercontent.com/ourownstory/neuralprophet-data/main/datasets/"
file = "energy/SF_hospital_load.csv"
data_df = pd.read_csv(data_location + file)

m = NeuralProphet()
train_df, test_df = m.split_df(data_df, freq="H", valid_p=1.0 / 5)

quantile_lo, quantile_hi = 0.05, 0.95
quantiles = [quantile_lo, quantile_hi]
n_lags = 3 * 24
n_forecasts = 24

m = NeuralProphet(
    growth="off",
    yearly_seasonality=False,
    weekly_seasonality=False,
    daily_seasonality=False,
    n_forecasts = n_forecasts,
    n_lags=n_lags,
    ar_reg=1,
    learning_rate=0.01,
    quantiles=quantiles,
)

random_seed = 0
set_random_seed(random_seed)
metrics = m.fit(train_df, freq="H")

forecast = m.predict(test_df)
```

**Error:**

```
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
Input In [1], in <cell line: 34>()
     31 metrics = m.fit(train_df, freq="H")
     32 metrics.tail(1)
---> 34 forecast = m.predict(test_df)

File ~.../neuralprophet/forecaster.py:940, in NeuralProphet.predict(self, df, decompose, raw)
    938         fcst = fcst[:-1]
    939 else:
--> 940     fcst = self._reshape_raw_predictions_to_forecst_df(df_i, predicted, components)
    941     if periods_added[df_name] > 0:
    942         fcst = fcst[: -periods_added[df_name]]

File ~.../neuralprophet/forecaster.py:3169, in NeuralProphet._reshape_raw_predictions_to_forecst_df(self, df, predicted, components)
   3167 for j in range(len(self.config_train.quantiles)):
   3168     forecast_0 = components[comp][0, :, j]
-> 3169     forecast_rest = components[comp][1:, self.n_forecasts - 1, j]
   3170     yhat = np.concatenate(([np.NaN] * self.max_lags, forecast_0, forecast_rest))
   3171     if j == 0:  # temporary condition to add only the median component
   3172         # add yhat into dataframe, using df_forecast indexing

IndexError: index 23 is out of bounds for axis 1 with size 1
```

Is it because I used the empty `m = NeuralProphet()` to split to data to train/test?


Discussion:
This is definitely a bug. By testing somethings out I found out that whenever we run NP with Autoregression `n_lags>0` and multi-step-ahead forecasts `n_forecasts>1` for some reason, NP always tries to fit a trend. If the user, however, opts to not fit a trend `growth='off'`, we get this error. We get this error also if we do not add any quantiles! Does this have anything to do with trend modularization @karl-richter ?
Update on this: The `StaticTrend()` class (called when `growth='off`) in `static.py` does not follow the forecasting pattern of all the other components. 
Example: PiecewiseLinear() (when growth not off) forecasts for every sample, and ends up with a shape of `(batch_size, n_forecasts, quantiles)`. Static trend is in shape `(batch_size, 1, quantiles)`. Although the static trend is constant, we need it to be in the same shape as the other components. Can we change this @karl-richter ? :) 
I got the same issue while using the global local model with n_forecasts > 1 and n_lags > 1 as @judussoari mentioned. Have added a workaround https://github.com/Terabyte17/neural_prophet/blob/main/neuralprophet/forecaster.py#L3265 here for my own use case, so this might be helpful till this gets resolved.
> Update on this: The `StaticTrend()` class (called when `growth='off`) in `static.py` does not follow the forecasting pattern of all the other components. Example: PiecewiseLinear() (when growth not off) forecasts for every sample, and ends up with a shape of `(batch_size, n_forecasts, quantiles)`. Static trend is in shape `(batch_size, 1, quantiles)`. Although the static trend is constant, we need it to be in the same shape as the other components. Can we change this @karl-richter ? :)

Has this Issue been addressed yet? @karl-richter @judussoari

Resolving patch:
diff --git a/neuralprophet/components/trend/static.py b/neuralprophet/components/trend/static.py
--- a/neuralprophet/components/trend/static.py
+++ b/neuralprophet/components/trend/static.py
@@ -28,7 +28,7 @@ def forward(self, t, meta):
             torch.Tensor
                 Trend component, same dimensions as input t
         """
-        return self.bias.unsqueeze(dim=0).repeat(t.shape[0], 1, 1)
+        return self.bias.unsqueeze(dim=0).repeat(t.shape[0], self.n_forecasts, 1)
 
     @property
     def get_trend_deltas(self):


Test patch: