Forecasting Univariate Time Series

Univariate forecasting uses the historical values of a single variable to predict its future values. In deep learning, we implement this by training recurrent networks on sliding context windows extracted from the time series.


Dataset Windowing and Preparation

Before feeding a time series to a network, we must restructure it into input-target pairs using a sliding window.

Sliding Window Technique

Given a sequence [x_1, x_2, ..., x_t], we define a history window of size L. The input at step i is the slice [x_i, ..., x_{i+L-1}], and the target is the subsequent value x_{i+L}.

Scale and Stationarity Adjustments

Time series must be scaled (e.g., using MinMaxScaler) to prevent features from destabilizing training. If the series exhibits trend, we stabilize the mean by taking differences before scaling.

LSTM Forecasting Model in PyTorch

We build a forecasting model by feeding the hidden state of an LSTM at the final step of the window into a linear output layer.

Forecasting Network Code

This architecture maps a history window to a single continuous prediction value.

<pre><code class="language-python">import torch import torch.nn as nn class UnivariateLSTM(nn.Module): def __init__(self, hidden_size=32): super().__init__() self.lstm = nn.LSTM(input_size=1, hidden_size=hidden_size, batch_first=True) self.linear = nn.Linear(hidden_size, 1) def forward(self, x): # x shape: [batch, seq_len, 1] out, (h_n, c_n) = self.lstm(x) # Extract the hidden state from the last time step last_step = out[:, -1, :] return self.linear(last_step) # Shape: [batch, 1] model = UnivariateLSTM() x_sample = torch.randn(4, 24, 1) # batch=4, window=24 hours, input_dim=1 print(model(x_sample).shape) # torch.Size([4, 1])</pre>

Autoregressive vs. Direct Multi-Step

To forecast multiple steps into the future, we can use Autoregressive forecasting (predicting one step, appending the prediction to the input window, and predicting the next step) or Direct forecasting (adjusting the output layer to project a vector of size H corresponding to the entire horizon).