Forecasting with DeepAR: for busy people

Kekayan
3 min readOct 11, 2021

--

Photo by petr sidorov on Unsplash

In this blog, I will explore time series forecasting using the DeepAR model developed by researchers at Amazon. This will be a code first guide to train time-series data to forecast.

DeepAR forecasting algorithm is a supervised learning algorithm for forecasting time series using recurrent neural networks (RNN).DeepAR can be trained on one or more related time series to predict future horizons given the preceding context. Since It’s a probabilistic model It will predict single best values for time index, the model parametrizes a parametric probability distribution for each time index. In this blog, We will use the GluonTS library’s DeepAR implementation.

Setup

In addition to pandas , numpy we need mxnet and gluonts. Let’s install those first by executing the pip command.

  • If you are using CPU
pip install mxnet gluonts ujson
  • If you have GPU access then Install the GPU version of the mxnet.
pip install mxnet-cu101 gluonts ujson # GPU

Prepare the Dataset

We will start with downloading our data on International airline passengers from Kaggle.

Let’s import our data and prepare for training

imports the libraries
Load the data to pandas dataframe

The data is about International airline passenger’s monthly counts in thousands starts from 1949 and ends in 1960. Since the last row has some noisy data, I will remove it and set the time stamp as the index.

Now let’s visualize our data over time.

Before passing the data to the model, We have to format our data which our library understands, Let’s do that now for training and test data. We need to create a ListDataset object. It should have at least a target variable, a start timestamp, and a frequency (min, H, M).

The training data needs to contain the train data only, But the test data needs to contain both train and test data.

Train and Test data split

Model

Now we will create our DeepAR model instance by creating an object of DeepAREstimater . GluonTs uses Estimators to encapsulate model and predictor in other words Estimators represents a model that can be trained to yield predictor after training.

Let’s Intializise our DeepAR model with a few hyperparameters and callbacks for Learning Rate Scheduling & model Averaging.

Few important parameters,

  • context_length — The number of time-points that the model gets to see before making the prediction.
  • prediction_length — The number of time-steps that the model is trained to predict, also called the forecast horizon.
  • freq — The granularity of the time series in the dataset.
  • num_cells — The number of cells to use in each hidden layer of the RNN.
  • num_layers — The number of hidden layers in the RNN.

You can check the full list of hyperparameters here.

Evaluate

Now we can use the predictor to forecast our test data.

You will see the probabilistic forecast from the following graphs. The model provides an estimate of how confident the model is, and allows downstream decisions based on these forecasts to account for this uncertainty.

Let’s check metrics such as MSE, MASE, symmetric MAPE, RMSE using Evaluator from gluonTs.

happy forecasting :)

--

--

Responses (3)