Facebook Prophet: Saturating Forecasts

from fbprophet import Prophet
import pandas as pd
import logging
logging.getLogger('fbprophet').setLevel(logging.ERROR)
import warnings
warnings.filterwarnings("ignore")
# Plotly needs to be explicitly added so that Prophet as access to ggsave.
libraries <- c("prophet", "plotly")
lapply(libraries, require, character.only = TRUE)

Forecasting Growth

By default, Prophet uses a linear model for its forecast. When forecasting growth, there is usually some maximum achievable point: total market size, total population size, etc. This is called the carrying capacity, and the forecast should saturate at this point.

Prophet allows you to make forecasts using a logistic growth trend model, with a specified carrying capacity. We illustrate this with the log number of page visits to the R (programming language) page on Wikipedia:

example_wp_log_R.csv
df <- read.csv(
example_wp_log_R.csv
)
df = pd.read_csv(
example_wp_log_R.csv
)

We must specify the carrying capacity in a column ("cap"). Here we will assume a particular value, but this would usually be set using data or expertise about the market size.

df$cap <- 8.5
df['cap'] = 8.5

The important things to note are that ("cap") must be specified for every row in the dataframe, and that it does not have to be constant. If the market size is growing, then ("cap") can be an increasing sequence.

We then fit the model as before, except pass in an additional argument to specify logistic growth:

m <- prophet(df, growth = 'logistic')
m = Prophet(growth='logistic')
m.fit(df)
<fbprophet.fo...x7fca1df7a160>

We make a dataframe for future predictions as before, except we must also specify the capacity in the future. Here we keep capacity constant at the same value as in the history, and forecast 3 years into the future:

future <- make_future_dataframe(m, periods = 1826)
future$cap <- 8.5
fcst <- predict(m, future)
plot(m, fcst)
future = m.make_future_dataframe(periods=1826)
future['cap'] = 8.5
fcst = m.predict(future)
fig = m.plot(fcst)
fig

The logistic function has an implicit minimum of 0, and will saturate at 0 the same way that it saturates at the capacity. It is possible to also specify a different saturating minimum.

Saturating Minimum

The logistic growth model can also handle a saturating minimum, which is specified with a column ("floor") in the same way as the ("cap") column specifies the maximum:

df$y <- 10 - df$y
df$cap <- 6
df$floor <- 1.5
future$cap <- 6
future$floor <- 1.5
m <- prophet(df, growth = 'logistic')
fcst <- predict(m, future)
plot(m, fcst)
# forecast2 <- plot(m, fcst)
# ggsave("/results/forecast2.png", forecast2)
df['y'] = 10 - df['y']
df['cap'] = 6
df['floor'] = 1.5
future['cap'] = 6
future['floor'] = 1.5
m = Prophet(growth='logistic')
m.fit(df)
fcst = m.predict(future)
fig = m.plot(fcst)
fig

To use a logistic growth trend with a saturating minimum, a maximum capacity must also be specified.