Slide 1

Slide 1 text

Exploratory Seminar #25 Time Series Forecasting with Prophet

Slide 2

Slide 2 text

EXPLORATORY

Slide 3

Slide 3 text

Kan Nishida CEO/co-founder Exploratory Summary Beginning of 2016, launched Exploratory, Inc. to democratize Data Science. Prior to Exploratory, Kan was a director of product development at Oracle leading teams for building various Data Science products in areas including Machine Learning, BI, Data Visualization, Mobile Analytics, Big Data, etc. While at Oracle, Kan also provided training and consulting services to help organizations transform with data. @KanAugust Speaker

Slide 4

Slide 4 text

Mission Democratize Data Science

Slide 5

Slide 5 text

Data Science is not just for Engineers and Statisticians. Exploratory makes it possible for Everyone to do Data Science. The Third Wave

Slide 6

Slide 6 text

First Wave Second Wave Third Wave Proprietary Open Source UI & Programming Programming 2016 2000 1976 Monetization Commoditization Democratization Statisticians Data Scientists Democratization of Data Science Algorithms Experience Tools Open Source UI & Automation Business Users Theme Users

Slide 7

Slide 7 text

Questions Communication Data Access Data Wrangling Visualization Analytics (Statistics / Machine Learning) Data Analysis Data Science Workflow

Slide 8

Slide 8 text

Questions Communication (Dashboard, Note, Slides) Data Access Data Wrangling Visualization Analytics (Statistics / Machine Learning) Data Analysis ExploratoryɹModern & Simple UI

Slide 9

Slide 9 text

Exploratory Seminar #25 Time Series Forecasting with Prophet

Slide 10

Slide 10 text

• Application of Supervised Training • Build a model to predict value from date, with past data as training data. • By feeding the model with future dates, it will produce forecasted values for the future dates. Time Series Forecasting

Slide 11

Slide 11 text

Builds a model that predicts a day’s value from past N days. Traditional Approach with Time Series Forecasting Day 1 Day 2 Day 3 Day 4 Day 2 Day 3 Day 4 Day 5 Day 3 Day 4 Day 5 Day 6

Slide 12

Slide 12 text

• Time interval between data has to be same throughout the data • Day with NA is not allowed • Seasonality with multiple periods (Week and Year) is hard to handle • Parameter tuning is hard and requires a forecasting expert level knowledge. Problems with Traditional Time Series Model

Slide 13

Slide 13 text

• A ‘curve fitting’ algorithm to build time series forcasting models. • Designed for ease of use without expert knowledge on time series forecasting or statistics. • Built by Data Scientists (Sean J. Taylor & co.) at Facebook and open sourced. (https:// facebook.github.io/prophet) Prophet Sean J. Taylor @seanjtaylor

Slide 14

Slide 14 text

Build a model by finding a best smooth line which can be represented as sum of the following components. • Overall growth trend • Seasonality - Yearly, Weekly, Daily, etc. • Holiday effects - X’mas, New Year, July 4th, etc. • External Predictors Prophet - Additive Model

Slide 15

Slide 15 text

Trend

Slide 16

Slide 16 text

Trend

Slide 17

Slide 17 text

Yearly

Slide 18

Slide 18 text

Trend + Yearly

Slide 19

Slide 19 text

Weekly

Slide 20

Slide 20 text

Trend + Yearly + Weekly

Slide 21

Slide 21 text

21 Trend + Yearly + Weekly

Slide 22

Slide 22 text

22 Holiday Effect

Slide 23

Slide 23 text

23 Trend + Yearly + Weekly + Holiday

Slide 24

Slide 24 text

• Can handle uneven time intervals. • Can handle dates with NA. • Can handle multiple seasonality components (e.g. Yearly, Weekly, etc.). • Works well by default. • Can improve it by using easy-to-interpret parameters with business domain knowledge. Benefit of Prophet Approach

Slide 25

Slide 25 text

Let’s do it!

Slide 26

Slide 26 text

Sales Data

Slide 27

Slide 27 text

Weekly Sales Trend

Slide 28

Slide 28 text

Build a Forecasting Model with Prophet

Slide 29

Slide 29 text

Assign Order Date to Date/Time and Sales to Value.

Slide 30

Slide 30 text

The blue line is the actual data (Sales), and the orange line is the forecasted data.

Slide 31

Slide 31 text

The last area is the forecasted period where there is no actual data.

Slide 32

Slide 32 text

The default is 10 units, in this case, that is 10 weeks.

Slide 33

Slide 33 text

Set the Forecasting Period to 52 weeks.

Slide 34

Slide 34 text

Under the Trend tab, you can see the overall trend that is used by the model. The blue line is the actual (Sales) data, and the green line is the trend.

Slide 35

Slide 35 text

The vertical light green bars are Change Points where the trend changes.

Slide 36

Slide 36 text

Under the Yearly tab you can see the Yearly Seasonality.

Slide 37

Slide 37 text

Every year, the sales doesn’t pick up until June, then it goes down in July.

Slide 38

Slide 38 text

Weekly tab shows up only when the data is daily or more granular levels (hour, minutes, etc.)

Slide 39

Slide 39 text

Every week, the sales are low on Sunday and Monday, and the rest of the week is high.

Slide 40

Slide 40 text

Data Pre-Processing NA Data Handling

Slide 41

Slide 41 text

Somehow, the weekly seasonality doesn’t repeat exactly the same…

Slide 42

Slide 42 text

Somehow, the weekly seasonality doesn’t repeat exactly the same…

Slide 43

Slide 43 text

There are NA for some dates. You can impute NA as part of the Data Preprocessing.

Slide 44

Slide 44 text

No content

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

Under the Importance tab, you can see which seasonality has more effect on the forecasting outcome.

Slide 47

Slide 47 text

47 Evaluating Forecasting Model

Slide 48

Slide 48 text

• Keep the latest part of past data as test data • Forecast the test data period with the data before the test data period • Estimate the forecast Backtesting

Slide 49

Slide 49 text

Split the data into two sections. 49 Training Period Test Period

Slide 50

Slide 50 text

50 Training Period Test Period Build a model with the training data and forecast for the test period. Compare the forecasted values against the actual values in the test period.

Slide 51

Slide 51 text

51 Assign Order Date and select ‘Floor to Month’ Assign ‘Sales’ to Value and select ‘Sum’.

Slide 52

Slide 52 text

52 Set Test Mode to TRUE, type 12 (months) for the Test Period.

Slide 53

Slide 53 text

53 Training Period Test Period 12 Months

Slide 54

Slide 54 text

Since the forecasting model is built based on the training data, the forecasted values (Orange) and the actual values (Blue) are usually very close. 54

Slide 55

Slide 55 text

55 Evaluate the model based on how much difference there is between the forecasted values and the actual values in the Test period.

Slide 56

Slide 56 text

Evaluation Metrics under Summary tab 56

Slide 57

Slide 57 text

• MAE (Mean Absolute Error) : Mean of absolute differences between actual value and forecasted value. • RMSE (Root Mean Square Error) : Root of mean of squares of difference between actual value and forecasted value. • MAPE (Mean Absolute Percentage Error) : Mean of absolute differences in percentage of actual value. • MASE (Mean Absolute Scaled Error) : Divide MAE of the forecasting model by MAE of a naive forecasting model (a simple model that always forecasts one previous value). Metrics to Evaluate Forecasting Model

Slide 58

Slide 58 text

Root
 Mean
 Square
 Error Square the Errors (Difference between the actual and the forecasted), take the mean, and root the mean. 58 RMSE (Root Mean Square Error)

Slide 59

Slide 59 text


 22 + 22 + 22 + 42 
 4 4 + 4 + 4 + 16 
 4 7 = 2.65 59 RMSE (Root Mean Square Error) 2 2 4 2 = =

Slide 60

Slide 60 text

Mean
 Absolute
 Error Absolute values of the Errors (Difference between the actual and the forecasted) and take the mean. 60 MAE (Mean Absolute Error)

Slide 61

Slide 61 text


 2 + 2 + 2 + 4 
 4 61 = 2.5 MAE (Mean Absolute Error) 2 2 4 2

Slide 62

Slide 62 text

Mean
 Absolute
 Percentage
 Error Absolute Percentage differences between the actual and the forecasted and take the mean. 62 MAPE (Mean Absolute Percentage Error)

Slide 63

Slide 63 text

63 12 13 16 11 Take the actual values. MAPE (Mean Absolute Percentage Error)

Slide 64

Slide 64 text

64 12 13 16 11 MAPE (Mean Absolute Percentage Error) 2 2 4 2 Take the errors.

Slide 65

Slide 65 text

65 100 100 100 100 MAPE (Mean Absolute Percentage Error) 16.6% 15.4% 25% 18.2% Calculate the ratio of the errors against the actual values. If it’s negative make it positive. (Absolute)

Slide 66

Slide 66 text

66 100 100 100 100 MAPE (Mean Absolute Percentage Error) 16.6% 15.4% 25% 18.2% 
 16.6 + 15.4 + 18.2 + 25 
 4 Take the mean. = 18.8%

Slide 67

Slide 67 text

67 Mean
 Absolute
 Scaled
 Error Scale the MAE so that it can be compared among data sets with different variability. MASE (Mean Absolute Scaled Error)

Slide 68

Slide 68 text

68 MAE in the test period / MAE of a ‘naive’ prediction model in the training period MASE - How to Scale

Slide 69

Slide 69 text

69 Use the Previous Period Values as the Forecasted Values Naive Forecasting

Slide 70

Slide 70 text

70 MAE in Training Period

Slide 71

Slide 71 text

71 MAE in the test period MAE of a ‘naive’ prediction model in the training period MASE = MAE in the test period / MAE of a ‘naive’ prediction model in the training period

Slide 72

Slide 72 text

72 Seasonality Mode Additive vs. Multiplicative

Slide 73

Slide 73 text

73 The difference between the actual line and the forecasted line becomes wider as the time progresses.

Slide 74

Slide 74 text

What’s happening? 74 • This model is assuming that the seasonal variability stays the constant. • But, as Sales increases maybe the seasonal variability increases, too. • This model might not be able to keep up with the actual growth of Sales. Can we model the seasonality that also grows as the Sales grows?

Slide 75

Slide 75 text

Additive Growth Multiplicative Growth Number of Employees at a very stable company Amazon’s Sales Trend Different Types of Growth Models 75

Slide 76

Slide 76 text

76 The effect is additive The effect is multiplicative Additive Multiplicative Seasonality Mode Grows 20% in February Grows $20,000 in February The growth is constant regardless of the previous value The growth rate is constant, which means it grows bigger when the previous value big.

Slide 77

Slide 77 text

Switch Seasonality Mode to ‘Multiplicative’ 77

Slide 78

Slide 78 text

The difference between the actual and the forecasted looks similar even as the time progresses.

Slide 79

Slide 79 text

79 Additive Model Multiplicative Model

Slide 80

Slide 80 text

80 Seasonality Effect is constant throughout the years Seasonality Effect is growing as the Sales grows Additive Model Multiplicative Model

Slide 81

Slide 81 text

81 Additive Model Multiplicative Model Seasonality Effect is constant throughout the years Seasonality Effect is growing as the Sales grows

Slide 82

Slide 82 text

82 Building Forecasting Models for Monthly Sales Additive Model Multiplicative Model

Slide 83

Slide 83 text

83 The Additive Seasonality model is not keeping up with the actual Sales growth while the Multiplicative model is performing better. Additive Model Multiplicative Model

Slide 84

Slide 84 text

84 Additive Model Multiplicative Model All Evaluation Metrics suggest that the multiplicative seasonality model performs better than the additive model.

Slide 85

Slide 85 text

Compare the Models among Markets with Repeat By

Slide 86

Slide 86 text

86 To compare among Markets, select ‘Market’ for Repeat By and build multiple forecasting models.

Slide 87

Slide 87 text

87

Slide 88

Slide 88 text

• RMSE and MAE indicate that the errors are smaller for Africa compared to Asia Pacific. • Can we conclude that the model for Africa is better? 88

Slide 89

Slide 89 text

89 Sales amounts are bigger in Asia Pacific than in Africa in general, hence it’s expected that RMSE and MAE are bigger for Asia Pacific.

Slide 90

Slide 90 text

• MAPE is calculated as the percentage of the errors compared to the actual values. • It is more useful when we want to compare the two without considering the scale of the actual values. • MAPE is smaller for Asia Pacific, which means we have a better model for Asia Pacific than for Africa. 90

Slide 91

Slide 91 text

91 • The ratio of MAE in Test Period against the naive prediction model. • Given it’s a ratio, we can use it to compare among the models regardless of the scale of the actual values. • One advantage of MASE over MAPE is that MAPE can be unreliable when the actual values are close to 0 or have positive and negative values together where MASE doesn’t get bothered by such cases. • The MASE suggests that the model is fitting better for Asia Pacific and USCA compared to other regions.

Slide 92

Slide 92 text

External Predictors

Slide 93

Slide 93 text

If we know some variables are correlated with Sales and we can get to know the future values of such variables, can’t we forecast better?

Slide 94

Slide 94 text

Sales and Sales Comp. look correlated to one another.

Slide 95

Slide 95 text

Sales and Marketing look correlated to one another, too.

Slide 96

Slide 96 text

Sales and Discount (Avg) don’t look correlated to one another.

Slide 97

Slide 97 text

Marketing Let’s say we can control how much we will be spending on the marketing in the next 3 months. Can we make such information built into the forecasting model? Example

Slide 98

Slide 98 text

Weather Let’s say our Sales is usually impacted by the weather and we can forecast the temperature or whether it will rain or not for the next 10 days. Can we use such information built into the forecasting model? Example

Slide 99

Slide 99 text

You can assign variables as ‘External Predictors (Extra Regressors) and build forecasting models. Prophet investigate if the external predictor (Marketing) is useful to build a better model forecast the target variable (Sales) and calculate the coefficient of the predictor variable. External Predictors

Slide 100

Slide 100 text

A forecasting model to forecast Sales based on the Trend, Yearly Seasonality with Test Mode.

Slide 101

Slide 101 text

Metrics to evaluate the base model.

Slide 102

Slide 102 text

A forecasting model to forecast Sales based on the Trend, Yearly Seasonality, and Sales Comp.

Slide 103

Slide 103 text

Model Evaluation Metrics Base Model Base Model with Sales Comp.

Slide 104

Slide 104 text

A forecasting model to forecast Sales based on the Trend, Yearly Seasonality, and Marketing.

Slide 105

Slide 105 text

Base Model with Sales Comp. Base Model with Marketing

Slide 106

Slide 106 text

How about all of them together? Sales Comp., Marketing, and Discount

Slide 107

Slide 107 text

With Sales Comp., Marketing, Discount

Slide 108

Slide 108 text

Base Model with Sales Comp. Base Model with Sales Comp., Marketing, and Dicount The forecasting model quality has improved.

Slide 109

Slide 109 text

Under the Effects tab, you can see how each of the seasonality and the predictor variables are effecting on the forecasted values.

Slide 110

Slide 110 text

Under the Importance tab, you can comparesee how each of the seasonality and the predictor variables are effecting on the forecasted values.

Slide 111

Slide 111 text

Advanced Topics

Slide 112

Slide 112 text

Holiday Effect

Slide 113

Slide 113 text

113 • Holiday might effect the measures like sales, number of orders, etc. • Special events like conferences, black friday, sports events, etc. might effect the measures. Holiday Effect

Slide 114

Slide 114 text

114 Unique Page Views of ”Apple Worldwide Developers Conference” (WWDC) at Wikipedia Data

Slide 115

Slide 115 text

115 Apple Worldwide Developers Conference (WWDC) dates Data

Slide 116

Slide 116 text

116 Data Future dates.

Slide 117

Slide 117 text

• Set ‘Day’ for Date / Time • Set 365 as Forecasting Time Period Forecast without Considering Holiday Effect 117

Slide 118

Slide 118 text

118 These WWDC dates are not forecasted well by the model.

Slide 119

Slide 119 text

Add WWDC Dates by Join

Slide 120

Slide 120 text

Select ‘Full join’ as the join type because we want to bring the dates that don’t exist in the current data frame but does exist in the target data frame. Add WWDC Dates by Join 120

Slide 121

Slide 121 text

121

Slide 122

Slide 122 text

Select Holiday Column 122 These WWDC dates are not forecasted well by the model.

Slide 123

Slide 123 text

123 Future conference dates are better forecasted by incorporating the holiday effect.

Slide 124

Slide 124 text

124 Holidays Tab Green line is the Holiday effect.

Slide 125

Slide 125 text

125

Slide 126

Slide 126 text

Capacity

Slide 127

Slide 127 text

127 Capacity - Upper / Lower Limit

Slide 128

Slide 128 text

128 You can set Upper / Lower Limit values. • Sales / Demand ForecastingɿWe know it won’t exceed a certain limit. • Conversion RateɿIt never exceed 100%. Prophet can take those limits into account. Capacity

Slide 129

Slide 129 text

129 You can set the lower limit when the upper limit is set. • The default value is 0. • You can set a negative value as well. Lower Limit

Slide 130

Slide 130 text

No content

Slide 131

Slide 131 text

131

Slide 132

Slide 132 text

Trend Line 132 Trend line becomes a logistic curve instead of a linear line when Upper/Lower Limits are set.

Slide 133

Slide 133 text

Q & A

Slide 134

Slide 134 text

Information Email [email protected] Website https://exploratory.io Twitter @KanAugust Training https://exploratory.io/training

Slide 135

Slide 135 text

EXPLORATORY