Forecast Example

data-scienceinferencearchivedamazon-sagemaker-examplesreinforcement-learningmachine-learningawsexamplesdeep-learningsagemakerjupyter-notebooktrainingmlops

Time Series Modeling with Amazon Forecast and DeepAR on SageMaker - Amazon Forecast


This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.

This us-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable


Introduction

Amazon offers customers a multitude of time series prediction services, including DeepAR on SageMaker and the fully managed service Amazon Forecast. Both services are similar in some aspects, yet differ in others. This notebook series aims to highlight the similarities and differences between both services by demonstrating how each service is used as well as describing the features each service offers. As a result, both notebooks in the series will use the same dataset. We will consider a real use case using the Beijing Multi-Site Air-Quality Data Set which features hourly air pollutants data from 12 air-quality monitoring sites from March 1st, 2013 to February 28th, 2017, and is featured in the [1] academic paper. This particular notebook will focus on Amazon Forecast, and will:

  • Explain Amazon Forecast model options
  • Demonstrate how to train an AutoPredictor
  • Create inferences from Amazon Forecast model

One feature of Amazon Forecast is that the service can be used without any code. However, this notebook will outline how to use the service within a notebook format. Before you start, please note that training an Amazon Forecast may take several hours; this particular notebook took approximately 6 hours 30 minutes to complete. Also, make sure that your SageMaker Execution Role has the following policies:

  • AmazonForecastFullAccess
  • AmazonSageMakerFullAccess
  • IAMFullAccess

For convenience, here is an overview of the structure of this notebook:

  1. Introduction
  1. Data Preprocessing
  1. Dataset Group
  2. Datasets
  1. Predictors
  1. Forecast
  1. Resource Cleanup
  2. Next Steps

Preparation

[ ]
[ ]

To use Amazon Forecast, we're going to need to define an IAM role with the AmazonForecastFullAccess policy attached, as well as AmazonS3FullAccess:

[ ]
[ ]

All paths and resource names are defined below for a simple overview for where each resource will be located:

[ ]
[ ]

Data Preprocessing

This section prepares the dataset for use in Amazon Forecast. It will cover:

  • Target/Test dataset splitting
  • Target/Related time series splitting
  • S3 uploading

Data Import

This section will be demonstrating how to import data from an S3 bucket, but one can import their data whichever way is convenient. The data for this example will be imported from the sagemaker-sample-files S3 Bucket.

To communicate with S3 outside of our console, we'll use the Boto3 python3 library. More functionality between Boto3 and S3 can be found here: Boto3 Amazon S3 Examples

This particular dataset decompresses into a single folder named PRSA_Data_20130301-20170228. It contains 12 csv files, each containing air quality data for a single location. Each DataFrame will contain the following columns:

  • No: row number
  • year: year of data in this row
  • month: month of data in this row
  • day: day of data in this row
  • hour: hour of data in this row
  • PM2.5: PM2.5 concentration (ug/m^3)
  • PM10: PM10 concentration (ug/m^3)
  • SO2: SO2 concentration (ug/m^3)
  • NO2: NO2 concentration (ug/m^3)
  • CO: CO concentration (ug/m^3)
  • O3: O3 concentration (ug/m^3)
  • TEMP: temperature (degree Celsius)
  • PRES: pressure (hPa)
  • DEWP: dew point temperature (degree Celsius)
  • RAIN: precipitation (mm)
  • wd: wind direction
  • WSPM: wind speed (m/s)
  • station: name of the air-quality monitoring site

Citations

  • Dua, D. and Graff, C. (2019). UCI Machine Learning Repository [http://archive.ics.uci.edu/ml]. Irvine, CA: University of California, School of Information and Computer Science.
[ ]
[ ]
[ ]
[ ]

Data Visualization

For this example, we'll use the temperature, or TEMP column, as our target variable to predict on. Let's first take a look at what each of our time series looks like.

[ ]

Dataset Visual

Train/Test Split

Let's set the prediction horizon to the last 2 weeks of our time series. As a result, our test set should be the last 336 instances of each time series, as the frequency of our data is hourly.

[ ]

Target/Related Time Series Split

Amazon Forecast allows the use of a related time series, which contains other features that may increase the accuracy of our predictor. For simplicity, we'll use the PRES(pressure), RAIN(precipitation), and WSPM(wind speed) features for our related time series. More information can be found here: Related Time Series Datasets

[ ]

Upload to S3

[ ]
[ ]

Dataset Group

A dataset group is a container for all of our resources pertaining to one particular dataset. This includes target and related time series, predictors, and forecasts.

[ ]

Datasets

This section will go over:

  • Creating datasets
  • Creating schema
  • Importing datasets into dataset groups

Create Datasets

When importing datasets into Amazon Forecast, a schema for the target and/or related time series must be defined. This is a dictionary that describes the

[ ]
[ ]
[ ]
[ ]

Dataset Import

[ ]
[ ]
[ ]

Predictors

Predictor Options

Amazon Forecast offers six built-in algorithms:

  1. CNN-QR
  2. DeepAR+
  3. Prophet
  4. NPTS
  5. ARIMA
  6. ETS

Optimal use cases for each algorithm can be found here: Comparing Forecast Algorithms

In addition to multiple algorithms, Amazon Forecast offers three options for predictions:

  • Manual Selection - Manually select a single algorithm to apply to entire dataset
  • AutoML - Service finds and applies best-performing algorithm to entire dataset
  • AutoPredictor - Service runs all models and blends predictions with the goal of improving accuracy

Manual selection and AutoML are considered Legacy models, and new features will only be supported by the AutoPredictor model. As a result, the AutoPredictor model will be used in this notebook. More information on Amazon Forecast's AutoPredictor can be found here: Amazon Forecast AutoPredictor

AutoPredictor

This particular auto-predictor took approximately 6 hours 17 minutes to train.

[ ]
[ ]

Forecast

Create Forecast

[ ]
[ ]

Query Forecast

[ ]
[ ]

Forecast Results

The plot above shows the target values and 10%, 50%, and 90% quantiles. The 10% and 90% quantiles produce an 80% confidence interval.

Resource Cleanup

Let's clean up every resource we've created throughout this notebook. We'll have to delete our resources in a specific order:

  1. Delete Forecasts
  2. Delete Predictors
  3. Delete Dataset Imports
  4. Delete Datasets
  5. Delete Dataset Group
  6. Delete IAM Role
[ ]
[ ]
[ ]
[ ]
[ ]
[ ]

Next Steps

This notebook illustrates the features offered by Amazon Forecast, and is part of the Time Series Modeling with Amazon Forecast and DeepAR on SageMaker series. The notebook series aims to demonstrate how to use the Amazon Forecast and DeepAR on SageMaker time series modeling services as well as outline their features. Be sure to read the DeepAR on SageMaker example, and view a top-level comparison of both services in the README.

Notebook CI Test Results

This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.

This us-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This us-east-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This us-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ca-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This sa-east-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-west-3 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-central-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This eu-north-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-southeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-southeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-northeast-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-northeast-2 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable

This ap-south-1 badge failed to load. Check your device's internet connectivity, otherwise the service is currently unavailable