How to improve the forecasting performance
I am beginner with Prophet, my main goal is predict the daily freight number. I have tried to used GridSearch to find the best parameters. However the forecasting in test dataset is not good. Do anyone have some advises for me to improve. Thank you very much.
m_para = Prophet(changepoint_prior_scale=10,
changepoint_range= 1,
seasonality_mode = 'multiplicative',
seasonality_prior_scale=0.3,
weekly_seasonality=20,
yearly_seasonality=5,
daily_seasonality=25,
)
m_para.fit(train_data)
future = m_para.make_future_dataframe(periods=7,freq='D')
forecast_para = m_para.predict(future)
for col in ['yhat', 'yhat_lower', 'yhat_upper']:
forecast_para[col] = forecast_para[col].clip(lower=0.0)
fig = m_para.plot(forecast_para)
Prediction on test dataset, there are some gap between actual and predict values

Thanks for providing the visualizations. A couple of comments:
- The trend looks like it is overfit. You can actually see there is a yearly cycle in the trend, which is bad (we want the yearly cycle to be captured by the yearly seasonality component, not the trend). And generally the trend looks really erratic for a time series that doesn't seem to have to much trend change happening. This overfitting is a result of the
changepoint_prior_scale=10, which is a very large value for thechangepoint_prior_scale. I think the default of 0.05 would probably be better here, since it would produce a mostly flat trend and would push the fitting of seasonality into the yearly seasonality component where it belongs. The reason this is important is because any yearly cycle picked up in the trend component is not going to be repeated in future years, which will hurt your forecast performance. That's why by default the trend is given stronger regularization (smaller prior scale) than the seasonality. - The daily seasonality looks really bizarre - does this time series have daily data, or does it have hourly frequency (or something else finer-grained than daily)? Unless it's hourly data, you shouldn't be fitting daily seasonality; it means the cycle within a day, and it looks like this is daily data. So I'd remove the
daily_seasonality=25and let it use the default which is to disable it when the data frequency is daily or slower.
I think those changes alone should help to get a better forecast, in particular by getting the yearly seasonality to fit the cycle instead of the trend will help with the underprediction of yearly seasonality.
There is one more thing you could try after that. You can see there is some positive skew in the noise in this time series. Prophet assumes Gaussian noise. To better handle positive skew, you could try using a log transform. So, replace y with np.log(df['y']) before passing the data into prophet (or np.log(1 + df['y']) if 'y' has 0s). Then make the forecast, and invert the transform by taking np.exp(fcst['yhat']) to get the forecast in the original values. You'll want to use additive seasonality instead of multiplicative seasonality if you do this because the log transform will by nature produce multiplicative seasonality. That looks like it could work well on this time series.
@bletham Sorry for late reply. Thank you very much for your comments. Here is my update Prophet model
m = Prophet(seasonality_mode='additive',yearly_seasonality=5, weekly_seasonality=False, daily_seasonality=False ,interval_width=0.99, holidays_prior_scale=10, changepoint_prior_scale=5, seasonality_prior_scale=0.7, changepoint_range = 0.95, changepoints = ['2017-04-26','2017-07-12','2017-09-27','2018-01-29','2018-09-13','2019-03-28'], ) m.add_seasonality(name='weekly_on_season', period=5.5, fourier_order=80, condition_name='on_season',mode='additive') m.add_seasonality(name='weekly_off_season', period=5.5, fourier_order=20, condition_name='off_season',mode='additive') m.add_country_holidays(country_name='NO') m.add_regressor('last_week_count',mode='additive')
I found the value of Sunday is alway 0, so I eliminated it. We only have 6 days per week, but I realized that the period = 5.5 got better result than 6 (don't know why) The Wednesday and Thursday are alway higher than others. Therefore I used it as a seasonality
def is_season(ds): date = pd.to_datetime(ds) return (date.dayofweek == 2 or date.dayofweek == 3) data_freight['on_season'] = data_freight['ds'].apply(is_season) data_freight['off_season'] = ~data_freight['ds'].apply(is_season)
I also used the data of last week as addition regressor, because their trend is similar
Here is the results, it looks better

Should I need to add a custom yearly seasonality, what is yearly periods ? How to capture the peak of 20/6 and 27/6.
Thank you.
A couple more notes that I hope are helpful:
- The period for any of the seasonalities is always in days of real time, not data frequency. Prophet is a continuous-time model and does not care if you only have 5 data points per week or 6 or even if the data spacing is irregular. So the weekly seasonality should always be 7, because a weekly cycle is 7 days of calendar time independent of how many data points you have within that 7 day period.
- Fourier orders: The number of terms used to model a particular seasonality will be 2x the Fourier order. The default for weekly seasonality is 3, which means there are 6 parameters for modeling weekly seasonality. With daily data, there are only 7 output values for weekly seasonality (the amount of seasonality for each day), so 6 parameters is already a lot relative to that. The choices of 80 and 20 used here are much higher than is necessary, and mean that a total of 200 parameters are being used to model weekly seasonality. This will make the model much slower and is prone to numerical instabilities. I suspect that (once the period is corrected to 7) you will not see much benefit from increasing the Fourier order over 4 or 5 for weekly seasonality with daily data.
- I'd be pretty hesitant about having the changepoint_prior_scale larger than the seasonality_prior_scale for the reason described above about the trend capturing seasonal effects. Manually specifying the changepoints here is preventing that from happening, but I'd still suggest trying to lower changepoint_prior_scale and increase seasonality_prior_scale (maybe even something around swapping their values).
- The
yearly_seasonality=5means it is using a Fourier order of 5 for yearly seasonality. Yearly seasonality is often more complex than weekly seasonality and so typically benefits from a larger Fourier order. The default is 10, and especially here where you are seeing the yearly seasonality being underfit, I'd try increasing that to something in the 10-20 range. You won't need to add a custom yearly seasonality because, like weekly, the period doesn't depend on the data frequency and will always be 365.25 days.
Hello @bletham : I came across this post just today and would love to share some of my works.
The key finding is that: Great performance for test set, poor performance for future prediction. Prediction values are much higher than actual values for future set.
Time series data: 2020-01-01 to 2022-08-01. Training data set: 2020-01-01 to 2022-05-30. Test data set: 2022-06-01 to 2022-06-30. Future set: 2022-07-01 and more. (I also have actual data from 2022-07-01 to 2022-07-31, just want to further test model performance).
After tuning parameters, I got followings: changepoint_prior_scale = 0.5 seasonality_prior_scale = 1 holidays_prior_scale = 5
this set of parameters make RMSE = 0.20 (MAE 0.14) for test data set. Daily forecasted trend is almost in line with actual trend.
Yet when predicting future, the prediction values are much higher than actuals. See below, the green lines are future predictions.

Currently i can put a 'cap' in my df so that predictions won't grow too much. But is there anything else i can do? Really appreciate any inputs!!
model2 = Prophet(holidays=df_holiday,
seasonality_mode='additive',
changepoint_prior_scale=0.5,
seasonality_prior_scale=1,
holidays_prior_scale=5,
yearly_seasonality=True,
daily_seasonality=False,
weekly_seasonality=True,
interval_width=0.95)