FBProphet
Prophet is a procedure for forecasting time series data based on an additive model where non-linear trends are fit with yearly, weekly, and daily seasonality, plus holiday effects. It works best with time series that have strong seasonal effects and several seasons of historical data. Prophet is robust to missing data and shifts in the trend, and typically handles outliers well.
Trend Changepoints
By default, Prophet will automatically detect these changepoints and will allow the trend to adapt appropriately. However, if you wish to have finer control over this process, then there are several input arguments you can use.
Automatic changepoint detection in Prophet
Prophet detects changepoints by first specifying a large number of potential changepoints at which the rate is allowed to change.
# Python
from fbprophet.plot import add_changepoints_to_plot
fig = m.plot(forecast)
a = add_changepoints_to_plot(fig.gca(), m, forecast)
- By default, Prophet specifies 25 potential changepoints
- By default changepoints are only inferred for the first 80% of the time series
- We have a lot of places where the rate can possibly change
- Most of these changepoints go unused
n_changepoints
set number of potential changepoints, but this is better tuned by adjusting the regularization.changepoint_range
For example,m = Prophet(changepoint_range=0.9)
in Python
Adjusting trend flexibility
If the trend changes are being overfit (too much flexibility) or underfit (not enough flexibility), you can adjust the strength of the sparse prior using the input argument changepoint_prior_scale. Increasing it will make the trend more flexible:
# Python
m = Prophet(changepoint_prior_scale=0.5)
forecast = m.fit(df).predict(future)
fig = m.plot(forecast)
Seasonality, Holiday Effects, And Regressors
Built-in Country Holidays
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)
Fourier Order for Seasonalities
Seasonalities are estimated using a partial Fourier sum. See the paper for complete details, and this figure on Wikipedia for an illustration of how a partial Fourier sum can approximate an aribtrary periodic signal.
The default Fourier order for yearly seasonality is
10
. The default values are often appropriate, but they can be increased when the seasonality needs to fit higher-frequency changes, and generally be less smooth.
# Python
from fbprophet.plot import plot_yearly
m = Prophet(yearly_seasonality=20).fit(df)
a = plot_yearly(m)
Specifying Custom Seasonalities
Prophet will by default fit weekly and yearly seasonalities, if the time series is more than two cycles long. It will also fit daily seasonality for a sub-daily time series.
add_seasonality
: add other seasonalities (monthly, quarterly, hourly) The inputs to this function are a name
, the period
of the seasonality in days, and the Fourier order
for the seasonality.
# Python
m = Prophet(weekly_seasonality=False)
m.add_seasonality(name='monthly', period=30.5, fourier_order=5)
forecast = m.fit(df).predict(future)
fig = m.plot_components(forecast)
Prior scale for holidays and seasonality
If you find that the holidays are overfitting, you can adjust their prior scale to smooth them using the parameter
holidays_prior_scale
. By default this parameter is 10, which provides very little regularization. Reducing this parameter dampens holiday effects . Prior scales can be set separately for individual holidays by including a columnprior_scale
in the holidays dataframe.
Multiplicative Seasonality
By default Prophet fits additive seasonalities, meaning the effect of the seasonality is added to the trend to get the forecast.
Additive models : the different components affected the time series additively.
Data = Seasonal effect + Trend + Cyclical + Residual
Multiplicative model : Assuming that the seasonal and other effects act proportionally on the series
Data = (Seasonal effect) * Trend * Cyclical * Residual
Multiplicative models are equally easy to fit to data as Additive models, take logarithms of both sides of the model
log(Data) = log(SeasonalEffect * Trend * Cyclical * Residual)
= log(SeasonalEffect) + log(Trend) + log(Cyclical) + log(Residual)
To fit a multiplicative model, take logarithms of the data, then analyse the log data as before.
we use multiplicative models when the magnitude of the seasonal pattern in the data depends on the magnitude of the data. On other hand, in the additive model, the magnitude of seasonality does not change in relation to time.
Sometime additive seasonality does not work. Prophet can model multiplicative seasonality by setting seasonality_mode='multiplicative'
in the input arguments:
# Python
m = Prophet(seasonality_mode='multiplicative')
m.fit(df)
forecast = m.predict(future)
fig = m.plot(forecast)
Uncertainty Intervals
By default Prophet will return uncertainty intervals for the forecast
yhat
. There are several important assumptions behind these uncertainty intervals.
Uncertainty in the trend
The biggest source of uncertainty in the forecast is the potential for future trend changes. It’s impossible to know for sure, so we do the most reasonable thing we can, and we assume that the
future will see similar trend changes as the history
.
The width of the uncertainty intervals (by default 80%) can be set using the parameter interval_width
:
# Python
forecast = Prophet(interval_width=0.95).fit(df).predict(future)
Uncertainty in seasonality
By default Prophet will only return uncertainty in the trend and observation noise. To get
uncertainty in seasonality
, you must do full Bayesian sampling. This is done using the parametermcmc_samples
(which defaults to 0).
# Python
m = Prophet(mcmc_samples=300)
forecast = m.fit(df).predict(future)
This replaces the typical Maximum a Posteriori (MAP) estimation with Markov Chain Monte Carlo (MCMC) sampling.
You can access the raw posterior predictive samples in Python using the method m.predictive_samples(future)
Additional regressors
Multiple features for forecasting ? This is something that fits very easily into the modeling framework for Prophet but hasn’t yet been implemented. It is on the roadmap in #101 and so hopefully will be coming soon!
Troubleshooting
NameError: name 'go' is not defined
# solution pip install plotly