You are on page 1of 38

AW S A c a d e m y M a c h i n e L e a r n i n g F o u n d a t i o n s

Module 4: Introducing Forecasting

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Module overview

Sections Demonstrations
1. Forecasting overview • Creating a forecast with Amazon
Forecast
2. Processing time series data
3. Using Amazon Forecast Guided Lab
4. Guided lab • Creating a Forecast with Amazon
Forecast
5. Module wrap-up

Knowledge check
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 2
Module objectives

At the end of this module, you should be able to:


• Describe the business problems solved by using Amazon Forecast
• Describe the challenges of working with time series data
• List the steps that are required to create a forecast by using Amazon Forecast
• Use Amazon Forecast to make a prediction

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 3
Module 4: Introducing Forecasting

Section 1: Forecasting overview

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Overview of forecasting

• Predicting future values that are


based on historical data
• Can be either univariate or
multivariate
• Common patterns – Trending data Seasonal data
• Trends: Patterns that increase,
decrease, or are stagnant
• Seasonal: Pattern that is based on
seasons
• Cyclical: Other repeating patterns
• Irregular: Patterns that might appear Cyclical data Irregular data
to be random
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 5
Forecasting use cases

Sales and demand forecasts Inventory projections

Energy consumption Weather forecasts


© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 6
Module 4: Introducing Forecasting

Section 2: Processing time series data

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Time series data
Unit Sales Product #21232
Time series data
is captured in
sequence over time

Related data informs


the time series data—
for example, price or
promotions

Metadata might also be


needed to explain
predictions—for
example, brand name or Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
category
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 8
Time and date challenges

Incomplete and varying timestamps yyyy-mm-dd HH:MM:SS Includes time

yyyy-dd-mm Year, day, month

yyyy-mm-dd Year, month, day

yyyy-mm No day

ss:s Seconds

mm-dd No year

UTC, local, and time zones:


Is the time in UTC format?
2020-06-02T13:15:30Z

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 9
Time series handling: Missing data
Forward Fill Moving Average
Last known == missing value Avg(previous) == missing value

Backward Fill Interpolation


Next value == missing value Linear
Spline
Danger! Lookahead risk! Polynomial

Note: Zero is sometimes the perfect fill value


© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 10
Time series handling: Downsampling

yyyy-mm-dd HH:MM:SS yyyy-mm-dd

InvoiceDate Item Quantity


2009-12-01 07:45:00 21232 24 InvoiceDate Item Quantity
2009-12-01 10:06:00 21232 36 Mean/ 2009-12-01 21232 60
2010-12-08 14:21:00 21232 17 Sum 2010-12-08 21232 26
2010-12-08 13:11:00 21232 9 2010-12-09 21232 44
2010-12-09 18:28:00 21232 44

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 11
Time series handling: Upsampling
yyyy-mm-dd yyyy-mm-dd HH:MM:SS

InvoiceDate Item Quantity


InvoiceDate Item Quantity 2009-12-01 ??:??:?? 21232 ??
2009-12-01 21232 60
?
2009-12-01 ??:??:?? 21232 ??
2010-12-08 21232 26 2010-12-08 ??:??:?? 21232 ??
2010-12-09 21232 44 2010-12-08 ??:??:?? 21232 ??
2010-12-09 ??:??:?? 21232 44

Reasons to upsample:
• Match different time series
• Irregular time series
• Knowledge of domain
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 12
Time series handling: Smoothing data

outliers
Smoothing function

Why are you smoothing? How does smoothing


• Data preparation affect your outcome?
• Visualization • Cleaner data to model
• Model compatibility
• Production improvements

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 13
Seasonality

• Seasonality frequency
• Hourly, daily, quarterly, yearly
• Spring, summer, fall, winter
• Major holiday sales, winter
holiday season

• Incorporating holidays
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 14
Time series correlations

This chart is originally from Tyler Vigen: Spurious Correlations


© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 15
Stationarity, trends, and autocorrelation

• Stationarity
• How stable is the system?
Autocorrelation?
• Does the past inform the future?
• Trends
• Correlation issues
• Autocorrelation
• How points in time are linearly
related
1 2 3 4 5 6 7 8 9 10 11 12 1 2 3 4 5 6 7 8 9 10 11 12

• Influences algorithm choice Trend

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 16
Using pandas for time series data

• Time-aware index
dataframe['2010-01-04']
dataframe['2010-02':'2010-03']
dataframe['weekday_name'] = dataframe.index.weekday_name

• GroupBy and resampling operations


dataframe.groupby('StockCode')
dataframe.groupby('StockCode').resample('D').sum()

• Autocorrelation
dataframe['Quantity'].autocorr()

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 17
Time series algorithms

• Autoregressive Integrated Moving


Average (ARIMA) ARIMA
• DeepAR+
• Exponential Smoothing (ETS)
• Non-Parametric Time Series (NPTS) Prophet DeepAR+

• Prophet Algorithm

NPTS ETS

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 18
• Time series data is sequenced
Section 2 key
• Time challenges –
takeaways • Different formats
• Missing data
• Seasonality
• Correlations
• The pandas library offers support for
time series data
• With Amazon Forecast, you can
choose between five algorithms

19 © 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Module 4: Introducing Forecasting

Section 3: Using Amazon Forecast

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Amazon Forecast workflow
New data, retraining Deploy
model
Business problem

Generate forecasts
Problem Tune Yes
formulation model

Meets
Collect and Evaluate Feature Select and Evaluate
business
label data data engineering train model model
goal?

Import, evaluate, and clean your data Train a predictor No

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 21
Amazon Forecast overview

AWS Management
Historical data Console

Amazon Forecast Predictors

Related data CSV file

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 22
Supported domains

• Retail • Web traffic


• Inventory planning • Metrics
• Amazon EC2 capacity • Custom
• Work force

Amazon Forecast

Training Domain-specific
Predictor
data schema

Forecast

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 23
Retail forecasting example

• Time series data


• Transactional sales data
• Timestamp, item, quantity
• Metadata
• Category, item color
• Item, metadata
• Related data
• Time series
• In-stock data
• Promotion data
• Timestamp, item, price
© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 24
Web traffic forecast example

• Time series data


• Webpage ID
• Page views per month
• Timestamp
• Related and metadata
• Page category
• Geographic identifier

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 25
Selecting an Amazon Forecast algorithm

You can select from the following


list of algorithms:
• ARIMA Predictor
• DeepAR+
• ETS
• NPTS Model
• Prophet
Algorithm

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 26
Evaluating your forecast: Back testing

Predictor accuracy metrics are based on back testing.


Forecast horizon
Start date End date

Training

Testing

Time

Back test window offset


Backtest window 1 Training Testing

Backtest window 2 Training Testing

Backtest window 3 Training Testing

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 27
Evaluation metrics: wQuantileLoss

• Quantiles determined for 10%, 50%, and 90% quantiles


• wQuantileLoss is the average error for each quantile in a set –
• Works best for models with greater variability in the errors

60
Demand
P10 forecast
40
P50 forecast
P90 forecast 20

-20

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 28
Root mean square error (RMSE)
RMSE is the square of the errors.

Actual
Test Results Test result Prediction Deltas
1 2 4 2
Error is the Actual result Prediction
2 4 8 4
30 difference 3 8 12 4
between the 28 4 16 28 12
25 actual
target value and RMSE 6.708204
20 the forecasted
value 16
15
12
10
8 8 Works best for a
5 model where most
4 4
2 errors are of similar
0 size
1 2 3 4

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 29
Model accuracy example

Web retailer of shoes wants to predict how often it


will be unable to fill orders for AnyCompany
brand shoes.

Amazon Forecast predicts demand of 1,000 pairs


per month P10 = 880
• P10: 10% of the time, fewer than 880 pairs will be ordered P50 = 1050 Forecast = 1000
• P50: 50% of the time, fewer than 1,050 pairs will be ordered P90 = 1200
• P90: 90% of the time, fewer than 1,200 pairs will be ordered

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 30
Demonstration:
Creating a forecast
with Amazon
Forecast

31 © 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
• You can use Amazon Forecast for
Section 3 key time series data
takeaways • Schemas are specific for domains
• Data can include –
• Time series data
• Metadata
• Related data
• Data is split into training and testing
data by accounting for time
• Use RMSE and wQuantileLoss
metrics to evaluate model
32 © 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Module 4 –
Guided Lab:
Creating a Forecast
with Amazon
Forecast

33 © 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Module 4: Introducing Forecasting

Module wrap-up

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Module summary

In summary, in this module you learned how to:


• Describe the business problems solved by using Amazon Forecast
• Describe the challenges of working with time series data
• List the steps that are required to create a forecast by using Amazon Forecast
• Use Amazon Forecast to make a prediction

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 35
Complete the knowledge check

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 36
Additional resources

• Amazon Forecast documentation


• Amazon Forecast product page
• How to not use machine learning for time series forecasting
• Time series forecasting principles Amazon Forecast whitepaper

© 2020, Amazon Web Services, Inc. or its Affiliates. All rights reserved. 37
Thank you

© 2021 Amazon Web Services, Inc. or its affiliates. All rights reserved. This work may not be reproduced or redistributed, in whole or in part, without prior written permission from Amazon
Web Services, Inc. Commercial copying, lending, or selling is prohibited. Corrections, feedback, or other questions? Contact us at https://support.aws.amazon.com/#/contacts/aws-training.
All trademarks are the property of their owners.

You might also like