Pipelines can get complicated at times. Poland 2022.

# How to handle autoregressive model over multiple time series?

## Introduction

Autoregressive models are among the “more difficult” to implement when it comes to data preparation. As the models are expected to yield daily or hourly predictions, the datasets themselves are expected to grow and expand with every new observation requiring the models to undergo regular retraining. Consequently, handling the data becomes a highly dynamic process where one has to be extra careful to ensure that no data leakage is present at any given point.

Surprisingly, but unfortunately, most tutorials on autoregression put way more emphasis on the models and algorithms, while not focusing that much on the data preparation side of things. Since their goal is to explain the autoregression concept (and they do it so well, for example: Machine Learning Mastery or article 1, article 2 from Towards Data Science), handing of the data preparation is limited to a single uni- or multi-variate time series. Tensorflow.org provides a more extensive explanation, although their approach is not easily extendable to cases of multiple multi-variate time series.

In this article, I would like to show you, how you can take advantage of the factory pattern to encapsulate the logic at various levels of abstraction without losing the flexibility in your code. For demonstration, I will use the same dataset (Jena Climate Dataset) as used in the Tensorflow’s tutorial. However, to complicate the matter a bit, I will create a few copies of the data with random perturbations to mimic a situation where our model’s task is to predict the weather at several geo-locations. Also, it is not uncommon to see time series datasets containing observations that “come and go” in random intervals. To gain as many training examples from the dataset as possible, I will also account for this fact.

## Making the data a bit more realistic…

For simplicity, I will skip the part that deals with the feature generation and focus on the T (degC) column. Let’s assume that our model’s task would be to predict the mean temperatures in several cities given first, last, min, and max temperature. Naturally, you could extend the same reasoning to all the columns in the dataset or choose a different target (e.g. predicting the mean temperature from the past mean temperatures). It’s all up to you. What I focus on here, however, is to build the training examples from multiple multi-dimensional series with special care for avoiding data leakage and “production-like” nature where new observations are expected to come each day.

### Fetching data

Let’s begin with fetching the data and resampling it to daily intervals.

Having created the [first, last, min, max] columns, let’s use the candlestick to display the data.

### Mocking the case

Now, it’s time to mock a more realistic scenario in which the data availability is not ideal.

After this… modification, our data looks like this:

Isn’t that more how data behaves in reality? As you can see, depending on the city, our availability is different. Especially for London, our choices for the sequences’ lengths may be limited.

## Generating samples

In autoregression, it is a common approach to use a rolling window that would slide along the time dimension and divide it into history and future sub-sequences. As the window moves forward (here, with a step size of one day), each time a new (X, y) pair is obtained. These pairs are then fed into the model for training.

When defining windows two considerations need to be taken into account. First of all, the history part of the window must never enter time regions where data is not supposed to yet be observed. Otherwise, data leakage is more than guaranteed! Conversely, the future part of the window can enter them. However, for the training, validation, and test subsets, we should ensure that the data exists so that predicted values can be compared with true values. During the inference, it is different. Technically, it is not possible to observe tomorrow’s numbers today. But as the sun raises again, we can compare the predictions.

Secondly, the time “horizon” is going to constantly shift forward. For models’ training, this means our historical dataset grows and so should the time horizons be pushed to the future as well.

As you can see, the whole situation is quite dynamic. Unless designed with sense, the code can quickly lose transparency and lead to errors.

## Solution

The key to solving the problem is to delegate responsibility to different classes. For example, splitting the series into training and testing parts should not be a part of the rolling window logic. Similarly, once the window is rolling it is only supposed to generate data samples without needing to care about the dates.

Thinking about it, we can discriminate the following chain of responsibilities.

1. First, it is useful to have some configuration file(s) that will store the parameters. Usually, I would have two separate files such as development.ini, and production.ini and a get_config function.
2. Once the constants are loaded, let’s use a TimeSplitter class to calculate time intervals and give us boundary dates. These dates will define the beginning and the end of the training, test, and validation parts.
3. The dates essentially tell where the rolling windows are supposed to operate. However, as mentioned earlier, we don’t want them to manage the time. The rolling windows are only going to generate samples, and this is where the factory pattern comes in. Let’s have a WindowFactory class to consume the boundaries and create Window objects.
4. Finally, depending on the need, we may construct different kinds of Windows (e.g. with or without a stepsize, an averaging window, etc.). Still, once the factory class has translated the dates into indices, each Window is only going to be given a time series chunk to roll over and generate the (X, y)-pairs.

### Constants

Before we begin, let’s agree on a few parameters.

#### development.ini

To clarify the convention: horizon defines the ultimate “blinding” of the dataset. It is supposed be “today”, beyond which no observation exists yet. In production, this parameter is expected to be updated daily. Then, the training, validation, and test sets are defined over the intervals $t_\text{training} \in [-\infty, h - 91], t_\text{validation} \in [h - 90, h - 31]$ and $t_\text{test} \in [h - 30, h]$, respectively. Finally, in this scenario, Windows are expected to support models that render predictions 7 days into the future, knowing the last 21 days.

I will leave implementing get_config… Once we read the data, the next step is:

### Time Splitter

As mentioned earlier, the only goal of the TimeSplitter is to figure out the boundaries. With additional logic, we can introduce assertions, warnings, or errors against situations resulting in ill-conditioned results. Also, we could replace the if-else statement with an Enum class, limiting the choices to only a few predefined options. This would go a bit beyond the scope of this article, so let’s move on to implementing the factory class.

### Window Factory

Now, it is time for the factory class. Once the date time boundaries are calculated, we expect the class to use the information to trim the series and spawn the window.

There are several ways to implement the class. The core functionality is provided by the .create method. It expects a time series as a pd.DataFrame and its goal is to correctly slice the input data. Here, we take advantage of the fact that indices in our dataframes are pd.DateTimeIndex objects. However, if we worked with plain numpy, the idea would be the same.

The method can also be extended to support different kinds of windows if needed. In our version, we use only one type of Window, so no other input arguments are necessary.

Finally, we introduce the __repr__ method to display the “capability” of the factory in terms of the interval and window shape. It is purely for the sake of debugging and readability.

### The Window

So how is the Window going to be defined? After all, the date time and indexing management are already done, all we need is an object that produces samples.

The .get_generator method is self-explanatory. It gives off slices of the time series by rolling along the time dimension with a step size of one day. Here, the __len__ method is used to express the number of samples the Window will produce. For example, if the series contains 5 records, and the window’s width is 2, it should roll exactly 4 times (hence + 1 in the end).

But wait!

The generator does not yield (X, y)-pairs, right? Correct.

Formulating pairs is easy, though. With slight modification, we can redefine the generator method:

### tf.data version

If you choose to work with Keras, however, you may find it useful to replace the generator with tf.data.Dataset object that contains the pair inside and pass that into the model. To achieve that, let’s make the Window object callable by adding one more method.

The downside of using this method is the fact that we create a lazy loading mechanism, just to consume the data eagerly. On the other hand, using tf.data provides us with a lot of useful functions (e.g. prefetch, shuffle, batch or concatenate), which makes the data easier to handle later.

## The whole flow

Let’s revisit our solution and create the full training, test, and validation datasets with help of the implemented logic.

## Conclusions

And this is it! Once we have loaded the constants and split the dates using TimeSplitter, we create three factories for each dataset respectively. The samples are then generated and collected using tf.data.Datasets objects. However we choose to parametrize the code, the process stays the same, although if the datasets are too narrow (or the Window’s width too large), we may end up having only a few samples.

To verify the approach, you can assert the total number of samples.

This code can certainly be improved. However, as you can see, this approach is simple to use and the code should be easy to maintain. It is also adaptable to any generic multivariate time series. It all depends on how you envision the (X, y)-pairs to look like (e.g. scalars, vectors, tensors, …)? Still, by applying this simple pattern, you can almost completely separate the data preparation code from the actual model, allowing you to iterate quickly over your ideas without worrying about data leakage to creeping up in your results.