How does Prophet work? Part-2

Deepti Goyal
Analytics Vidhya
Published in
3 min readFeb 18, 2020

So till now, we have learned how to tune the Trend component of the prophet library ( you can check that here).

In this post, I am going to explain the Seasonality component and Holiday

Component-

Equation of the model is —

Let’s understand the seasonality component (s(t)) in little depth — It provides flexibility(Weekly, Yearly, daily seasonality change components) to the model.

This component uses Fourier Series —

Four partial sums (Fourier series) of lengths 1, 2, 3, and 4 terms. Showing how the approximation to a square wave improves as the number of terms increases

Seasonality is estimated using partial Fourier Sum. The Fourier order determines how quickly the seasonality can change (Default order for yearly seasonality is 10, for weekly seasonality order is 3). Increasing this Fourier order allows the seasonality to fit faster-changing cycles ( We need to be very careful while setting this parameter as it can lead to overfitting).

m = Prophet(weekly_seasonality=False)
m.add_seasonality(name='monthly', period=30.5, fourier_order=5)

We can use conditional seasonality also apart from weekly, yearly and daily seasonality Note:- The main reason to go for conditional seasonality is — the default weekly seasonality assumes the pattern of weekly seasonality is the same throughout the year.

There is another parameter which can be tuned —

seasonality_prior_scale -> It adjusts the extent to which the seasonality model will fit the data(Note: — similarly we also set similar kind of variable for individual holidays by using — prior_scale parameter).For more in-depth clarity, you can look into the official guide.

# Python
m = Prophet()
m.add_seasonality(
name='weekly', period=7, fourier_order=3, prior_scale=0.1)

Holiday Component (h(t)) —

The holidays for each country are provided by the holidays package in Python. You can use that or you can create your own dataframe for holidays — Note:- Remember your holiday should contain two parameters — holiday and ds

You can also include columns lower_window and upper_window which extend the holiday out to [lower_window, upper_window] days around the date. You can also include a column prior_scale to set the prior scale separately for each holiday.

Pass the dataframe as a parameter to the prophet as shown below

Holiday_dataframe = pd.DataFrame({
'holiday': 'superbowl',
'ds': pd.to_datetime(['2010-02-07', '2014-02-02', '2016-02-07']),
'lower_window': 0,
'upper_window': 1,
})
# Python
m = Prophet(holidays=Holiday_dataframe)
forecast = m.fit(df).predict(future)

You can use a built-in collection of country-specific holidays using the add_country_holidays method

# Python
m = Prophet(holidays=holidays)
m.add_country_holidays(country_name='US')
m.fit(df)

To see holidays —

# Python
m.train_holiday_names

Let’s see an example —

m = Prophet(mcmc_samples=1000, changepoint_prior_scale=0.07, seasonality_mode=’multiplicative’, \
yearly_seasonality=10, \
weekly_seasonality=True, \
daily_seasonality=False, changepoint_range=0.9)
m.add_seasonality(‘quarterly’, period=91.25, fourier_order=10, mode=’additive’)

In the above code, you can see the parameter mode. Based on your dataset you can define the parameter — either multiplicative or additive. You can set the parameter for each seasonality component separately.

mcmc_samples — This parameter is used to get the uncertainty in seasonality. It uses Bayesian sampling. Higher you set, more time it is going to take. Start with low(maybe 500) and then you can increase it based on how much time effective solution you want. (I prefer above 1000 — again it depends on the data size as it takes time)

(all other components of the above code are already discussed in part-1)

For more in-depth clarity, you can look into the official guide.

Link to my GitHub repository — https://github.com/DeeptiAgl?tab=repositories

Please do share your feedback and any questions if you have. I will try to answer them to the best of my knowledge.

--

--