Get the "Applied Data Science Edge"!

The ViralML School

Fundamental Market Analysis with Python - Find Your Own Answers On What Is Going on in the Financial Markets

Web Work

Python Web Work - Prototyping Guide for Maker

Use HTML5 Templates, Serve Dynamic Content, Build Machine Learning Web Apps, Grow Audiences & Conquer the World!

Hot off the Press!

The Little Book of Fundamental Market Indicators

My New Book: "The Little Book of Fundamental Analysis: Hands-On Market Analysis with Python" is Out!


Sign up for my newsletter and get my free intro class:

Forecasting Demand, Finding Sales Data - Facebook Prophet, Google Trends & Python

Introduction

Let's talk about forecasting demand, this is as old as money and commerce. Rainy season, you stock up on umbrellas; winter, winter coats, etc. I'll walk you through a simple example using open-source FBProphet in Python, one of the most powerful forecasting engines and also one of the easiest to use (once you manage to install it).



If you liked it, please share it:

Code

Forecasting Demand - Quick Intro to FBProphet
In [124]:
from IPython.display import Image
Image(filename='Forecasting-Demand.png')
Out[124]:
In [125]:
# Image(filename='fb-prophet.png')

Forecasting Online Interest and Demand

Let's talk about forecasting demand, this is as old as money and commerce. Rainy seasons you stock up on umbrellas, for winter, winter coats, etc. It is also critical today, where so many stores around the world are closed due to covid19 and want an online presence, a virtual store to mirror Amazon and the likes. And most products aren't as straightforward as selling umbrellas.

I'll walk you through a simple example using open-source FBProphet in Python, one of the most powerful forecasting engines and also one of the easiest to use (once you manage to install it). I'll also show you external data sources that work great as proxy values to forecast demand when you want to understand products you don't carry or you don't have enough data stored.

To properly forecast demand you need enough data to teach a model the different cycles and season around that product, preferably a few of those cycles. FBProphet recommends 3 years of data so the model can understand the different holidays, peak sales, low sales, etc.

Let me show you an example using anonymized data from a Kaggle competition the "Store Item Demand Forecasting Challenge"

Open Source 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"

"It works best with time series that have strong seasonal effects and several seasons of historical data."

"Prophet is used in many applications across Facebook for producing reliable forecasts for planning and goal setting. We’ve found it to perform better than any other approach in the majority of cases."

FBProphet Quick Start Guide

https://facebook.github.io/prophet/docs/quick_start.html

Installing FB Prophet:

!pip3 install fbprophet --user


!pip3 install holidays==0.9.12

Kaggle data

https://www.kaggle.com/c/demand-forecasting-kernels-only/data

In [94]:
import time
import matplotlib
import matplotlib.pyplot as plt
import seaborn as sns
import statsmodels.api as sm
import io, base64, os, json, re, glob
import datetime
from datetime import timedelta
import pandas as pd
import pydata_google_auth
import numpy as np

from fbprophet import Prophet
import statsmodels.api as sm
In [102]:
df_raw = pd.read_csv('demand-forecasting-kernels-only/train.csv', 
                     low_memory=False, 
                     parse_dates=['date'], 
                     index_col=['date'])
In [103]:
df_raw = df_raw.sort_values('date', ascending=True)
print(df_raw.shape)
df_raw.head()
(913000, 3)
Out[103]:
store item sales
date
2013-01-01 1 1 13
2013-01-01 7 12 26
2013-01-01 7 46 27
2013-01-01 8 12 54
2013-01-01 9 12 35

Store 1 - All Sales

In [104]:
plt.subplots(1, figsize = (16, 5))
plt.grid()
plt.title('Store 1 - All Sales')
for item in set(df_raw[df_raw['store'] == 1]['item']):
    plt.plot(df_raw[(df_raw['store'] == 1) & 
                    (df_raw['item'] == item)]['sales'].rolling(window=50).mean())