← Master Index
Vol. 08 Module 8.1 Lecture

Time Series

Recurrent Networks

How This Lesson Fits the Module & Engineering Practice

Sequence data is the broad category. Time series is one of its most important engineering forms because events are indexed by real time, sampling intervals matter, and forecasting mistakes can have operational consequences.

RNNs became popular in time-dependent tasks because they can consume measurements step by step and maintain a running state. This lecture shows how sequential modeling applies to demand prediction, capacity planning, anomaly detection, and control pipelines, while also clarifying why time-aware validation is essential.

Learning Objectives

By the end of this lesson, students should be able to:

  • Define time series data and explain how it differs from generic sequence data.
  • Identify forecasting, classification, and anomaly detection tasks on temporal signals.
  • Construct sliding windows for RNN-based time series modeling.
  • Describe common tensor shapes for univariate and multivariate temporal batches.
  • Recognize temporal leakage and evaluation mistakes unique to forecasting.
  • Build a small PyTorch RNN for next-step prediction.
Definition

A time series is a sequence of observations indexed by time, where each measurement is associated with a temporal position such as a timestamp or fixed interval.

What Makes Time Series Different?

A time series has all the properties of a sequence, but it adds temporal direction and operational causality. The future cannot influence the past. That sounds obvious, but many modeling mistakes come from accidentally violating that rule during feature construction or data splitting.

Time series also introduces domain concepts such as trend, seasonality, lag, drift, and irregular sampling. Even when using neural networks, those concepts still matter because they shape what the model must remember.

PatternDescriptionExample
TrendLong-term upward or downward directionGrowing energy demand
SeasonalityRepeating cycleDaily traffic peaks
Lag dependencePast values affect current valuesSensor inertia
DriftStatistics change over timeMachine wear

Common Time Series Tasks

Forecasting

  • Predict one or more future values.
  • Often uses sliding windows.
  • Example: next-hour demand.

Classification

  • Assign a label to a whole series.
  • Example: normal vs faulty machine cycle.
  • Final hidden state is often useful.

Anomaly Detection

  • Flag unusual temporal behavior.
  • Can use errors, thresholds, or labels.
  • Example: sudden equipment spike.

Sliding Window Construction

Most neural time series pipelines convert a long signal into many supervised training samples. A window of the last T steps becomes the input, and one future step or horizon becomes the target.

1. Window

Select past values t-T through t-1.

2. Predict

Use them to estimate t or a future horizon.

3. Slide

Move one step forward and repeat.

4. Batch

Train many windows in parallel.

PyTorch Example: Next-Step Forecasting

The following model consumes a window of univariate values and predicts the next value. This is not production forecasting code, but it captures the core sequence mechanics clearly.

import torch from torch import nn class ForecastRNN(nn.Module): def __init__(self, input_size=1, hidden_size=16): super().__init__() self.rnn = nn.RNN(input_size=input_size, hidden_size=hidden_size, batch_first=True) self.head = nn.Linear(hidden_size, 1) def forward(self, x): output, h_n = self.rnn(x) last_hidden = output[:, -1, :] return self.head(last_hidden) model = ForecastRNN() x = torch.randn(8, 12, 1) # batch of 8 windows, 12 time steps each y_hat = model(x) print(y_hat.shape) # torch.Size([8, 1])

Understanding Temporal Shapes

SettingTypical shapeMeaning
Univariate window batch(B, T, 1)One value per time step
Multivariate window batch(B, T, F)F features per time step
One-step target(B, 1)Forecast a single next value
Per-step labels(B, T, C)Classify each position

Evaluation Must Respect Time

In standard supervised learning, random train/validation splits are common. In time series, they are often wrong. If the validation set contains earlier examples than the training set, or if future statistics leak backward into preprocessing, you get an unrealistically optimistic estimate.

Good Practice

  • Split by time, not random shuffle.
  • Fit scalers on training data only.
  • Use rolling or walk-forward validation.

Failure Modes

  • Future leakage during normalization.
  • Using target-derived features.
  • Ignoring regime changes and drift.
Common Mistake

Many beginners randomize individual time points before training. That destroys the very structure the model is meant to learn. Shuffle windows across samples if appropriate, but do not scramble the order inside each window.

Misconception

“Neural networks remove the need for temporal reasoning.” In reality, neural models still depend heavily on correct windowing, leakage control, normalization strategy, and evaluation design.

Bridge to Hidden State and Memory

In forecasting and temporal classification, the network must decide what to retain from the recent past. That is exactly the role of the hidden state. Once the sequence definition is clear, the next question becomes: what information can the model actually carry forward?

Later in the curriculum, when Volume 09 moves into text sequences such as tokenization, the same principle returns: current meaning depends on earlier context.

Knowledge Check

  1. Short Answer: What makes a time series more specific than a generic sequence? Answer: It is explicitly indexed by time.
  2. True/False: Random train/validation splitting is always safe for forecasting. Answer: False.
  3. Multiple Choice: Which pattern means repeating cycles? (a) drift, (b) seasonality, (c) padding. Answer: (b).
  4. Short Answer: What is a sliding window in time series modeling? Answer: A fixed-length chunk of past observations used to predict a future value or horizon.
  5. True/False: All time series tasks are forecasting tasks. Answer: False.
  6. Multiple Choice: A univariate batch for an RNN is commonly shaped as: (a) (B, T, 1), (b) (1, B, T), (c) (T, 1, B). Answer: (a).
  7. Short Answer: Name one common evaluation practice for time series. Answer: Walk-forward validation or chronological train/validation splits.
  8. True/False: Shuffling the order of values inside each window preserves temporal meaning. Answer: False.
  9. Multiple Choice: Which next lecture explains the model memory that carries temporal context? (a) Hidden State, (b) Pooling, (c) Quantization. Answer: (a).
  10. Short Answer: Why is leakage especially serious in time series? Answer: Because using future information creates unrealistically strong validation results that cannot exist in deployment.

Key Takeaways

  • Time series is a time-indexed subtype of sequence data with strong causality constraints.
  • Neural forecasting usually relies on sliding windows and careful tensor shaping.
  • Temporal leakage is one of the biggest practical risks in evaluation.
  • RNNs can model forecasting, classification, and anomaly detection on sequential signals.
  • Next, Hidden State explains how recurrent models carry memory forward.
Trainer’s Guide

Hands-on idea: Have students convert one long CSV column into overlapping windows and targets, then print the resulting tensor shapes before training.

Discussion prompt: Ask why a validation score can become misleading if a scaler is fitted on the full dataset instead of training data only.

Recap: Time series adds strict temporal direction, leakage risks, and forecasting structure to the broader sequence modeling story. Continue with Hidden State.