Search code examples
pythonmachine-learningfacebook-prophet

Facebook Prophet: Providing different data sets to build a better model


My data frame looks like that. My goal is to predict event_id 3 based on data of event_id 1 & event_id 2

ds tickets_sold y event_id
3/12/19 90  90  1
3/13/19 40  130 1
3/14/19 13  143 1
3/15/19 8   151 1
3/16/19 13  164 1
3/17/19 14  178 1
3/20/19 10  188 1
3/20/19 15  203 1
3/20/19 13  216 1
3/21/19 6   222 1
3/22/19 11  233 1
3/23/19 12  245 1
3/12/19 30  30  2
3/13/19 23  53  2
3/14/19 43  96  2
3/15/19 24  120 2
3/16/19 3   123 2
3/17/19 5   128 2
3/20/19 3   131 2
3/20/19 25  156 2
3/20/19 64  220 2
3/21/19 6   226 2
3/22/19 4   230 2
3/23/19 63  293 2

I want to predict sales for the next 10 days of that data:

ds  tickets_sold y event_id
3/24/19 20  20  3
3/25/19 30  50  3
3/26/19 20  70  3
3/27/19 12  82  3
3/28/19 12  94  3
3/29/19 12  106 3
3/30/19 12  118 3

So far my model is that one. However, I am not telling the model that these are two separate events. However, it would be useful to consider all data from different events as they belong to the same organizer and therefore provide more information than just one event. Is that kind of fitting possible for Prophet?

# Load data
df = pd.read_csv('event_data_prophet.csv')
df.drop(columns=['tickets_sold'], inplace=True, axis=0)
df.head()

# 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.
df['cap'] = 500

# growth: String 'linear' or 'logistic' to specify a linear or logistic trend.
m = Prophet(growth='linear')
m.fit(df)

# periods is the amount of days that I look in the future
future = m.make_future_dataframe(periods=20)
future['cap'] = 500
future.tail()

forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()

fig1 = m.plot(forecast)

Solution

  • Start dates of events seem to cause peaks. You can use holidays for this by setting the starting date of each event as a holiday. This informs prophet about the events (and their peaks). I noticed event 1 and 2 are overlapping. I think you have multiple options here to deal with this. You need to ask yourself what the predictive value of each event is related to event3. You don't have too much data, that will be the main issue. If they have equal value, you could change the date of one event. For example 11 days earlier. The unequal value scenario could mean you drop 1 event.

    events = pd.DataFrame({
      'holiday': 'events',
      'ds': pd.to_datetime(['2019-03-24', '2019-03-12', '2019-03-01']),
      'lower_window': 0,
      'upper_window': 1,
    })
    
    m = Prophet(growth='linear', holidays=events)
    m.fit(df)
    

    Also I noticed you forecast on the cumsum. I think your events are stationary therefor prophet probably benefits from forecasting on the daily ticket sales rather than the cumsum.