Why Online Learning Matters
In traditional batch machine learning, algorithms train offline on a static dataset to produce a fixed model. However, real-world systems—such as ad click-through rate (CTR) prediction, financial portfolio management, real-time routing, and automated trading—operate in dynamic, non-stationary, or even adversarial environments. Data arrives sequentially as a stream, data distributions drift over time, and delayed offline retraining is far too slow. Online Learning solves this fundamental challenge by updating predictions incrementally round by round, learning directly from streaming feedback without storing historical data or making statistical assumptions about the underlying data-generating distribution.
Instead of optimizing for static statistical accuracy, Online Learning minimizes regret: the difference between the algorithm's cumulative loss and the cumulative loss of the best fixed action (or "expert") selected with the benefit of hindsight. The curriculum scope covers four cornerstone framework algorithms:
- Multiplicative Weights Update Algorithm (MWUA): A foundational decision-making framework that maintains a set of weights over $K$ experts, penalizing poor performers multiplicatively at each round.
- Hedge: The continuous, exponential generalization of MWUA for arbitrary real-valued loss vectors $\ell_t \in [0, 1]^K$, achieving optimal $O(\sqrt{T \ln K})$ regret against an adversarial opponent over $T$ rounds.
- Follow the Leader (FTL): A greedy strategy that picks the action minimizing cumulative past loss, which performs well in benign environments but fails catastrophically ($O(T)$ regret) under adversarial conditions.
- Follow the Regularized Leader (FTRL): A robust framework that stabilizes FTL by adding a convex regularizer $R(w)$ to the cumulative loss objective, uniting online gradient descent, mirror descent, and Hedge under a single unified mathematical principle.
After completing this lesson, you will be able to formulate online decision problems, compute per-round weight updates and regret bounds, implement production-ready Hedge and FTRL algorithms in Python, C++, and Java, and identify when to apply regularization to prevent adversarial oscillation.
Online Learning makes sequential decisions under streaming, dynamic, or adversarial feedback without assuming stationary distributions; by maintaining and multiplicatively updating a probability distribution over actions (Hedge) or solving regularized historical loss minimizations (FTRL), it guarantees sublinear regret $O(\sqrt{T})$, performing nearly as well as the single best fixed action chosen in hindsight.
Core Concepts and Terminology
Online Learning operates as an iterative game played between a learner (decision-maker) and an environment (which may be stochastic or malicious) over $T$ discrete time rounds $t = 1, 2, \dots, T$.
At each round $t$:
- The learner maintains a probability distribution $p_t = (p_{t,1}, p_{t,2}, \dots, p_{t,K})$ over $K$ candidate actions or experts, where $\sum_{i=1}^K p_{t,i} = 1$ and $p_{t,i} \ge 0$.
- The learner selects an action (or plays the distribution $p_t$).
- The environment simultaneously or reactively reveals a loss vector $\ell_t = (\ell_{t,1}, \ell_{t,2}, \dots, \ell_{t,K}) \in [0, 1]^K$.
- The learner incurs an expected loss $\langle p_t, \ell_t \rangle = \sum_{i=1}^K p_{t,i} \ell_{t,i}$.
- The learner updates its internal state (weights or cumulative losses) to produce $p_{t+1}$ for the next round.
The central performance metric is Cumulative Regret after $T$ rounds, defined as:
$$R_T = \sum_{t=1}^T \langle p_t, \ell_t \rangle - \min_{i \in \{1, \dots, K\}} \sum_{t=1}^T \ell_{t,i}$$
An online learning algorithm is considered successful if its regret is sublinear in $T$ (i.e., $R_T = o(T)$), which guarantees that the average per-round regret vanishes as $T \to \infty$: $\lim_{T \to \infty} \frac{R_T}{T} = 0$.
| Key Term | Algorithmic Role | Concrete Example |
|---|---|---|
| Expert / Action ($i \in \{1 \dots K\}$) | A base decision candidate or model strategy available to the learner. | In stock forecasting, Expert 1 = Moving Average, Expert 2 = Neural Net, Expert 3 = Momentum. |
| Probability Vector ($p_t$) | A mixture distribution over experts assigned by the algorithm at round $t$. | $p_t = [0.5, 0.3, 0.2]$ allocates 50% confidence to Expert 1, 30% to Expert 2, and 20% to Expert 3. |
| Loss Vector ($\ell_t \in [0,1]^K$) | Penalty vector assigned by the environment to each expert at round $t$. | $\ell_t = [0.1, 0.9, 0.4]$ indicates Expert 1 suffered low loss, while Expert 2 suffered high loss. |
| Learning Rate ($\eta > 0$) | Step-size parameter controlling weight adjustment aggressiveness. | $\eta = \sqrt{\frac{8 \ln K}{T}}$ balances quick adaptation with stability over $T$ rounds. |
| Cumulative Regret ($R_T$) | Difference between algorithm loss and best single expert loss in hindsight. | If algorithm lost 15.0 total units and best single expert lost 11.2, $R_T = 3.8$. |
| Regularizer ($R(w)$) | Penalty function added in FTRL to stabilize updates against adversarial swings. | Negative entropy $R(w) = \sum w_i \ln w_i$ yields Hedge; $L_2$ norm $R(w) = \frac{1}{2}\Vert{}w\Vert{}_2^2$ yields Online Gradient Descent. |
How Online Learning Works, Step by Step
The operational sequence for Hedge / Multiplicative Weights follows a systematic three-stage loop per round $t$:
- Normalize Weights to Probability Distribution: Given current unnormalized weights $w_{t} = (w_{t,1}, \dots, w_{t,K})$, compute total mass $W_t = \sum_{i=1}^K w_{t,i}$. Set $p_{t,i} = \frac{w_{t,i}}{W_t}$ for all $i \in \{1, \dots, K\}$.
- Commit Action & Observe Loss: Predict distribution $p_t$. Observe loss vector $\ell_t = (\ell_{t,1}, \dots, \ell_{t,K})$. * Compute suffered expected round loss $\ell_{\text{alg}, t} = \langle p_t, \ell_t \rangle$.
- Multiplicative Weight Decay: * For each expert $i$, penalize weight exponentially based on its individual loss $\ell_{t,i}$:
$$w_{t+1, i} = w_{t, i} \cdot \exp(-\eta \ell_{t,i})$$
- High-loss experts experience rapid exponential decay, shifting future distribution mass toward consistently low-loss experts.
flowchart TD
A[Start: Initialize weights w_1 = 1 for all K experts, η > 0] --> B[Loop Round t = 1 to T]
```
B --> C[Compute sum W_t = sum_i w_t,i]
C --> D[Normalize probabilities: p_t,i = w_t,i / W_t]
D --> E[Play distribution p_t]
E --> F[Observe environment loss vector ℓ_t]
F --> G[Incur expected algorithm loss: ⟨p_t, ℓ_t⟩]
G --> H[Accumulate total algorithm loss and expert losses]
H --> I{Algorithm Type?}
I -- Hedge / MWUA --> J[Update weights: w_{t+1,i} = w_{t,i} * exp\(-η * ℓ_{t,i}\)]
I -- FTL --> K[Set next action: argmin_i sum_{s=1}^t ℓ_{s,i}]
I -- FTRL --> L[Solve argmin_w \(sum_{s=1}^t ⟨w, ℓ_s⟩ + R\(w\)/\eta\)]
J --> M[Advance to Round t + 1]
K --> M
L --> M
M --> B
B -- Round T Finished --> N[Output Final Regret: R_T = Total Alg Loss - Min Expert Loss]
```
Worked Example with a Full Trace
Let us execute a complete step-by-step trace of the Hedge algorithm over $T = 4$ rounds with $K = 3$ experts and learning rate $\eta = 0.5$.
Setup:
- $K = 3$ experts ($E_1, E_2, E_3$).
- Initial weights: $w_1 = [1.0, 1.0, 1.0]$.
- Learning rate: $\eta = 0.5$.
- Sequence of loss vectors $\ell_t$:
- $t=1$: $\ell_1 = [0.0, 1.0, 0.5]$
- $t=2$: $\ell_2 = [1.0, 0.0, 0.5]$
- $t=3$: $\ell_3 = [0.2, 0.8, 0.0]$
- $t=4$: $\ell_4 = [0.0, 0.5, 1.0]$
Step-by-Step Calculation Trace
- Round 1:
- Unnormalized weights $w_1 = [1.0, 1.0, 1.0]$, sum $W_1 = 3.0$.
- Probabilities $p_1 = [0.3333, 0.3333, 0.3333]$.
- Loss vector $\ell_1 = [0.0, 1.0, 0.5]$.
- Algorithm loss $\langle p_1, \ell_1 \rangle = 0.3333(0.0) + 0.3333(1.0) + 0.3333(0.5) = 0.5000$.
- Weight updates ($w_{2,i} = w_{1,i} \cdot e^{-0.5 \cdot \ell_{1,i}}$):
- $w_{2,1} = 1.0 \cdot e^{0} = 1.0000$
- $w_{2,2} = 1.0 \cdot e^{-0.5} = 0.6065$
- $w_{2,3} = 1.0 \cdot e^{-0.25} = 0.7788$
- Sum $W_2 = 2.3853$.
- Round 2:
- Probabilities $p_2 = [1.0/2.3853, 0.6065/2.3853, 0.7788/2.3853] = [0.4192, 0.2543, 0.3265]$.
- Loss vector $\ell_2 = [1.0, 0.0, 0.5]$.
- Algorithm loss $\langle p_2, \ell_2 \rangle = 0.4192(1.0) + 0.2543(0.0) + 0.3265(0.5) = 0.5825$.
- Weight updates ($w_{3,i} = w_{2,i} \cdot e^{-0.5 \cdot \ell_{2,i}}$):
- $w_{3,1} = 1.0000 \cdot e^{-0.5} = 0.6065$
- $w_{3,2} = 0.6065 \cdot e^{0} = 0.6065$
- $w_{3,3} = 0.7788 \cdot e^{-0.25} = 0.6065$
- Sum $W_3 = 1.8196$. Notice all weights equalized!
- Round 3:
- Probabilities $p_3 = [0.3333, 0.3333, 0.3333]$.
- Loss vector $\ell_3 = [0.2, 0.8, 0.0]$.
- Algorithm loss $\langle p_3, \ell_3 \rangle = 0.3333(0.2 + 0.8 + 0.0) = 0.3333$.
- Weight updates:
- $w_{4,1} = 0.6065 \cdot e^{-0.1} = 0.5488$
- $w_{4,2} = 0.6065 \cdot e^{-0.4} = 0.4066$
- $w_{4,3} = 0.6065 \cdot e^{0} = 0.6065$
- Sum $W_4 = 1.5619$.
- Round 4:
- Probabilities $p_4 = [0.5488/1.5619, 0.4066/1.5619, 0.6065/1.5619] = [0.3514, 0.2603, 0.3883]$.
- Loss vector $\ell_4 = [0.0, 0.5, 1.0]$.
- Algorithm loss $\langle p_4, \ell_4 \rangle = 0.3514(0.0) + 0.2603(0.5) + 0.3883(1.0) = 0.5185$.
- Summary Totals:
- Cumulative Algorithm Loss: $0.5000 + 0.5825 + 0.3333 + 0.5185 = 1.9343$.
- Expert $E_1$ Cumulative Loss: $0.0 + 1.0 + 0.2 + 0.0 = 1.2000$.
- Expert $E_2$ Cumulative Loss: $1.0 + 0.0 + 0.8 + 0.5 = 2.3000$.
- Expert $E_3$ Cumulative Loss: $0.5 + 0.5 + 0.0 + 1.0 = 2.0000$.
- Best Expert in Hindsight: $E_1$ with loss $1.2000$.
- Final Regret $R_4 = 1.9343 - 1.2000 = 0.7343$.
Trace It by Hand
Verify your understanding of Hedge dynamics by performing the following manual step:
Suppose at round $t=2$, after observing loss $\ell_2 = [1.0, 0.0, 0.5]$, all weights $w_3$ unexpectedly equalized to $0.6065$.
- Question 1: Why did all three expert weights become identical at round 3 ($w_3 = [0.6065, 0.6065, 0.6065]$), resetting the distribution to uniform $p_3 = [1/3, 1/3, 1/3]$?
- Question 2: If $\eta$ were increased from $0.5$ to $2.0$, would Expert 1's probability at round $t=2$ be higher or lower than $0.4192$?
Answer Verification:
- Question 1: Cumulative losses after 2 rounds were $E_1 = 0+1=1.0$, $E_2 = 1+0=1.0$, and $E_3 = 0.5+0.5=1.0$. Because all three experts accumulated equal total loss ($1.0$), Hedge assigns them equal weight $1.0 \cdot e^{-0.5 \cdot 1.0} = e^{-0.5} \approx 0.6065$.
- Question 2: Higher. A larger $\eta$ penalizes high losses more heavily. At $t=1$, $E_2$ ($\ell_1=1.0$) and $E_3$ ($\ell_1=0.5$) would decay much faster relative to $E_1$ ($\ell_1=0.0$), concentrating more mass on $E_1$.
Implementation in Python, C++, and Java
The code below implements a production-ready Hedge simulation in Python, C++, and Java using identical logic, identical floating-point precision, and the exact 4-round reference input from the worked example.
Complexity, Correctness, and Invariants
Evaluating online learning algorithms involves analyzing per-round time overhead, space footprint, and theoretical regret bounds against adversarial inputs.
| Algorithm | Per-Round Time | Space Complexity | Regret Bound ($R_T$) | Environment Assumptions | Core Optimization Mechanism |
|---|---|---|---|---|---|
| MWUA (Binary) | $O(K)$ | $O(K)$ | $O(\sqrt{T \ln K})$ | Adversarial binary losses | Multiplicative penalty factor $(1 - \epsilon)$ |
| Hedge | $O(K)$ | $O(K)$ | $\sqrt{\frac{T \ln K}{2}}$ | Adversarial bounded losses $\ell_t \in [0,1]^K$ | Exponential weighting with learning rate $\eta$ |
| Follow the Leader (FTL) | $O(K)$ | $O(K)$ | $O(T)$ (Worst) / $O(\ln T)$ (Stochastic) | Requires i.i.d. / strongly convex losses | Unregularized historical loss minimization |
| FTRL (Entropy) | $O(K)$ | $O(K)$ | $O(\sqrt{T \ln K})$ | Adversarial continuous losses | FTL + Negative Entropy Regularizer (equiv. to Hedge) |
| FTRL ($L_2$ / OGD) | $O(D)$ | $O(D)$ | $O(G B \sqrt{T})$ | Adversarial convex losses over continuous domain $\mathbb{R}^D$ | FTL + $L_2$ Squared Norm Regularizer (Online Gradient Descent) |
Proof Idea & Potential Function Invariant: Hedge correctness is established using the potential function $W_t = \sum_{i=1}^K w_{t,i}$. By telescoping the ratio $\frac{W_{T+1}}{W_1}$ and using the inequalities $e^{-x} \le 1 - x + \frac{x^2}{2}$ (for $x \ge 0$) alongside $\ln(1-y) \le -y$, we upper-bound total algorithm loss by $\sum_{t=1}^T \langle p_t, \ell_t \rangle \le \sum_{t=1}^T \ell_{t,i^*} + \frac{\ln K}{\eta} + \frac{\eta T}{8}$. Setting the optimal tuning parameter $\eta = \sqrt{\frac{8 \ln K}{T}}$ proves that $R_T \le \sqrt{\frac{T \ln K}{2}} = O(\sqrt{T \ln K})$, guaranteeing sublinear regret against arbitrary adversarial loss sequences.
Variants, Pitfalls, and Optimizations
Why Follow the Leader (FTL) Fails Without Regularization
A classic pitfall in online decision making is relying on unregularized Follow the Leader (FTL). In adversarial settings, FTL suffers $O(T)$ linear regret due to extreme sensitivity to past losses.
Consider a 2-expert game over $T$ rounds where the adversary alternates loss vectors:
- Round 1: $\ell_1 = [0.5, 0.0] \implies$ FTL picks $E_2$ for Round 2.
- Round 2: $\ell_2 = [0.0, 1.0] \implies$ Cumulative losses $[0.5, 1.0] \implies$ FTL switches to $E_1$.
- Round 3: $\ell_3 = [1.0, 0.0] \implies$ Cumulative losses $[1.5, 1.0] \implies$ FTL switches back to $E_2$.
FTL gets "chased around" by the adversary, incurring maximum loss at every step ($R_T = \Omega(T)$). Adding regularization (FTRL / Hedge) stabilizes decisions, keeping predictions smooth and preventing single-round overreaction.
Top Implementation Pitfalls:
- Numerical Underflow in Exponential Weights: When cumulative losses grow large over thousands of rounds, $e^{-\eta \sum \ell_{\tau,i}}$ underflows to 0. Always maintain logarithmic weights $L_{t,i} = \sum_{\tau=1}^t \ell_{\tau,i}$ and perform stable softmax normalization using $w_{t,i} = \exp(-\eta (L_{t,i} - \min_j L_{t,j}))$.
- Fixed Learning Rate with Unknown $T$: Tuning $\eta = \sqrt{\frac{8 \ln K}{T}}$ requires knowing total horizon $T$ in advance. For unknown time horizons, use the Doubling Trick or self-tuning adaptive step-sizes $\eta_t = \sqrt{\frac{\ln K}{t}}$.
- Assuming Stationary Distributions: Applying Hedge assumes static best-expert targets. For dynamic environments where the best expert shifts over time, use Fixed Share or Discounted MWUA to enforce a minimum weight floor on all experts.
Practice Problems and Real-World Use
Online learning algorithms are core primitives across high-frequency finance, ad networks, recommendation engines, and optimization theory.
| Real-World Application / System | Operational Mechanism | Tested Core Concept | Difficulty |
|---|---|---|---|
| FTRL-Proximal in Ad-Tech (Google CTR) | Combines $L_1$ and $L_2$ regularization with online gradient descent to produce sparse, high-accuracy click prediction models on streaming queries. | FTRL, Sparsity, $L_1$ Regularization | Advanced |
| Universal Portfolios (Cover's Algorithm) | Rebalances asset allocation across $K$ stocks online without distributional assumptions using multiplicative portfolio updates. | MWUA / Hedge, Online Convex Optimization | Intermediate |
| Boosting & AdaBoost Foundation | AdaBoost maintains sample weights updated multiplicatively based on weak learner error rates—equivalent to Hedge playing game theory minimax. | MWUA, Minimax Equilibrium, Duality | Intermediate |
| Multi-Armed Bandits (EXP3 Algorithm) | Solves partial-feedback online learning where only the loss of the played action is observed using importance-weighted loss estimators in Hedge. | Partial Feedback, Importance Sampling | Advanced |
| Routing & Network Flow Management | Dynamically routes packet traffic across network paths by treating paths as experts to minimize latency under unpredictable congested spikes. | Online Shortest Paths, Regret Bounds | Intermediate |
| Linear Programming via MWUA | Solves large-scale LPs by converting hard linear constraints into exponential penalties, yielding fast $(1+\epsilon)$-approximation algorithms. | Algorithmic Game Theory, LP Relaxation | Advanced |
| Online Learning for Zero-Sum Games | Two Hedge algorithms playing against each other in zero-sum matrix games converge to Nash Equilibrium at rate $O(1/\sqrt{T})$. | Minimax Theorem, Self-Play Convergence | Hard |