Basic Prerequisites: 01 · From Zero (the state idea) and Calculus & Optimization (the chain rule).
1. Intuition & Practical Objective
A recurrent network is a state machine with shared weights: at every step it reads the input , combines it with its current memory , and produces a new memory . Because the same cell is applied at every time step, the model is compact and can in principle remember arbitrarily far back. In practice the original RNN could not - its memory decays geometrically with distance because the gradient used to train it vanishes (§3: at ). Long Short-Term Memory (LSTM) solved this with a second, additive memory channel - the cell state - gated by learned forget/input/output gates. That single change is why recurrence became usable for trading-length sequences at all.
The practical objective: understand (a) what a recurrent cell computes, (b) why the naive version fails, and (c) how the gates fix it - so you can read LSTM/GRU code and reason about what the model can and cannot learn from a financial series.
2. Mathematical Ground Truth & Derivations
2.1 The vanilla RNN and its unrolled graph
The model is a deep network whose depth equals the sequence length , with all layers sharing . Training is backpropagation through time (BPTT): unroll the graph and apply the chain rule.
2.2 Why the gradient vanishes (or explodes)
The gradient of any loss at time w.r.t. an early state is a product of per-step Jacobians:
Each factor is a matrix whose norm is at most . Therefore:
Because and the recurrence multiplies, the product of many factors decays geometrically - the network effectively cannot learn dependencies more than a handful of steps long. This is the Bengio–Simard–Frasconi (1994) result, and it is the single reason recurrence was considered impractical for long sequences until LSTM.
2.3 LSTM gates (Hochreiter & Schmidhuber 1997)
The fix is a second state whose update is additive, not multiplicative, and gated:
The crucial term is the cell update. The gradient along the cell path is
a product of gate values (not of weights times saturating derivatives). If the forget gate learns , the gradient passes essentially undamped across arbitrary distances - the constant-error-carousel (CEC). There is no penalty on this path; the gates can learn the memory horizon instead of having it imposed by the weight norm. The GRU (Cho et al. 2014) achieves a similar effect with two gates and a single state (§06).
A subtlety. The CEC solves the optimisation problem (training long-range dependencies), not the statistical problem (overfitting with little data). An LSTM can now fit a long memory; whether that memory generalises is a question about data, addressed in 05 · Failure Modes.
3. Computational Implementation - LSTM forward pass and the vanishing gradient
Stdlib + numpy. Part A runs a hand-weighted LSTM forward and prints every gate at each step; Part B measures the gradient magnitude for a vanilla RNN against the LSTM cell path .
Read it as the derivation in numbers. The vanilla RNN at loses seven orders of magnitude between () and () - the 20th-step dependency carries essentially zero gradient, so the cell can never learn it. The LSTM cell path with still transmits of the gradient at - orders of magnitude larger. That gap is the LSTM.
4. Failure Modes & First-Principles Breakdowns
- Vanishing/exploding gradients (the disease LSTM treats). A vanilla RNN cannot learn long-range structure; see the table. Exploding gradients (large , or the row’s transient growth) are handled separately by gradient clipping (Pascanu et al. 2013) - a hard cap on the gradient norm per step.
- Gates do not create signal. An LSTM can represent a long memory; with a low-SNR target it will happily fit spurious long memories that don’t generalise. Architecture fixes optimisation, not statistics (→ 05 · Failure Modes).
- BPTT is sequential and slow. Recurrence cannot be parallelised over time (step needs step ), which is both a training-time and, for high-frequency trading, an inference-latency problem - the motivation for the convolutional and attention alternatives in pages 03 and 06.
- Truncated BPTT biases the gradient. In practice you unroll only steps to fit memory; that reintroduces exactly the truncation the LSTM was meant to avoid, so the effective horizon is your chosen unroll length, not the architecture.
- Numerical saturating tanh/sigmoid. Early in training, gates sit near and the cell state can drift; input scaling (standardising features) matters as much here as in feed-forward nets.
5. References
- Hochreiter, S. & Schmidhuber, J. (1997), Long Short-Term Memory, Neural Computation 9(8):1735–1780
- Goodfellow, Bengio & Courville, Deep Learning, Ch. 10 (§10.2: the recurrence, BPTT, the vanishing-gradient discussion; the LSTM/GRU subsection)
- Bengio, Simard & Frasconi (1994), Learning Long-Term Dependencies with Gradient Descent is Difficult
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning, Ch. 11 (§11.4–11.5: back-propagation, weight decay, early stopping)
- Bai, Kolter & Koltun (2018), An Empirical Evaluation of Generic Convolutional and Recurrent Networks, arXiv:1803.01271
6. Connected Graph Bridges
- Back: 01 · From Zero · Index Hub
- Forward: 03 · Attention & Transformers (the non-recurrent route to long-range memory) · 04 · Autoencoders for Factors
- Latency reality: Low-Latency Systems Architecture (why sequential inference is expensive)
- Optimisation base: Calculus & Optimization