16 — Deep Learning

RNNs for Time Series#

Deep LearningStatistical 1 min read
◆ The PatternSequence processing with recurrent memory

Recurrent Neural Networks process sequences step by step, carrying a hidden state that summarises the past. Each step updates the hidden state and optionally produces an output. The RNN learns temporal patterns through backpropagation through time (BPTT), though vanilla RNNs struggle with long-range dependencies.

ht = tanh(Whht−1 + Wxxt + b)
The hidden state ht is a compressed summary of all inputs seen so far. Vanishing gradients make plain RNNs forget early inputs.
// Interactive — RNN unrolled through time
Pattern bridge: RNN hidden states are a temporal version of embeddings — compressed representations of input context. The vanishing gradient problem connects to gradient dynamics in deep networks and the residual connections that solve it in transformers.
Worked example

Prices around 100 fed straight into a tanh unit with small random weights: 77% of the activations are saturated above 0.99, where the gradient is almost zero. Standardise the same prices first and 0% are. Neural forecasters need scaled inputs — usually per series — before anything else matters.

Where it misleads
  • Scaling with statistics from the whole series leaks the future into training; fit the scaler on the training window only.
  • RNNs need many related series to shine; on a single short series classical models often win (Hewamalage et al. 2021).
  • Recursive multi-step forecasting compounds errors; direct or sequence-to-sequence outputs are usually more stable.
In code
rng = np.random.default_rng(10)
prices = 100 + rng.normal(0, 5, 1_000)            # raw prices around 100
w = rng.normal(0, 0.1, 1_000)                      # small random input weights
saturated_raw = np.mean(np.abs(np.tanh(w * prices)) > 0.99)
z = (prices - prices.mean()) / prices.std()        # standardized
saturated_std = np.mean(np.abs(np.tanh(w * z)) > 0.99)
Python, in your browser — edit the code and run it again

Assumes import numpy as np and import pandas as pd. Each snippet simulates or makes up its own series, as the comments say.

The same idea with a library
# PyTorch — basic RNN for forecasting
import torch.nn as nn

class TSRNN(nn.Module):
    def __init__(self, input_dim, hidden_dim):
        super().__init__()
        self.rnn = nn.RNN(input_dim, hidden_dim, batch_first=True)
        self.fc = nn.Linear(hidden_dim, 1)
    def forward(self, x):
        out, _ = self.rnn(x)
        return self.fc(out[:, -1, :])
Sources
  • H. Hewamalage, C. Bergmeir & K. Bandara, “Recurrent Neural Networks for Time Series Forecasting: Current Status and Future Directions”, International Journal of Forecasting 37(1), 2021
  • D. Salinas, V. Flunkert, J. Gasthaus & T. Januschowski, “DeepAR: Probabilistic Forecasting with Autoregressive Recurrent Networks”, International Journal of Forecasting 36(3), 2020

Last reviewed October 2026 · How this site checks its content · Report a mistake

Linked from 1
← Previous
Changepoint Detection
Open in the full reader, with the topic sidebar