Algorithm Search⌘K

Algorithmresearch

Optimistic hedge

Also called optimistic multiplicative weights, OMWU.

Multiplicative weights that count the last loss twice as a prediction of the next. In games it reaches regret polylog(T) for every player.

Time
O(k T) worst
Space
O(k) extra
Where
T = the number of rounds; k = the number of actions
Properties
deterministicnot exactonlineiterative
Origin
Vasilis Syrgkanis, Alekh Agarwal, Haipeng Luo, Robert E. Schapire, Constantinos Daskalakis, Maxwell Fishelson, Noah Golowich, 2015

Optimistic Hedge is a variant of multiplicative weights for the expert problem. Plain Hedge weights each action by e−ηL(a)e^{-\eta L(a)}, where L(a)L(a) is the total past loss. Optimistic Hedge adds the last loss once more, as a guess that the next loss will look like the last one. Against an arbitrary adversary it keeps the O(Tlog⁡k)O(\sqrt{T \log k}) regret of Hedge. When the losses change slowly, its regret is much smaller, and in a game where every player uses it, the losses of each player change slowly because the others' strategies do. Daskalakis, Fishelson and Golowich (2021) proved that each of nn players then has regret O(nlog⁡klog⁡4T)O(n \log k \log^4 T), against Θ(T)\Theta(\sqrt{T}) for plain Hedge. By no-regret convergence to coarse correlated equilibria, the empirical distribution of play is then an O~(1/T)\tilde{O}(1/T) coarse correlated equilibrium. The general framework is no-regret dynamics; for two-player zero-sum games see no-regret convergence in zero-sum games.

In plain words

A learner picks among several actions every day and then learns what each action would have cost. The usual method keeps a score for each action and favours the actions that have cost little so far. The optimistic method also bets that tomorrow will look like today: it counts today's cost twice when it sets tomorrow's chances. If the costs really change slowly, this bet pays off, and the learner reacts a step earlier. If the bet fails, the extra term only costs a bounded amount per round. In a game where every player learns this way, each player's choices change slowly, so the costs that the others see also change slowly, and the bet is almost always right.

The problem

There are kk actions and TT rounds. In round tt the learner chooses a distribution xtx_t on the actions, an adversary who may know xtx_t chooses a loss vector ℓt∈[0,1]k\ell_t \in [0, 1]^k, and the learner pays xt⋅ℓtx_t \cdot \ell_t. The regret is

RegT=∑t=1Txt⋅ℓt−min⁡a∑t=1Tℓt(a).\text{Reg}_T = \sum_{t=1}^{T} x_t \cdot \ell_t - \min_{a} \sum_{t=1}^{T} \ell_t(a).

The goal is small regret against every adversary, and much smaller regret when the loss vectors change slowly, as they do in a game in which all players learn.

The algorithm

  1. OPTIMISTIC-HEDGE(k, eta, T) ▷ eta > 0 is the step size
  2. 1x ← the uniform distribution on the k actions
  3. 2m ← the zero vector ▷ the loss vector of the previous round
  4. 3for t ← 1 to T
  5. 4play x and observe the loss vector l
  6. 5for each action a
  7. 6x(a) ← x(a) · exp(-eta (2 l(a) - m(a)))
  8. 7divide each x(a) by the sum of all x(a)
  9. 8m ← l

After line L8 of round tt, the distribution is

xt+1(a)∝exp⁡(−η (Lt(a)+ℓt(a))),Lt=ℓ1+⋯+ℓt,x_{t+1}(a) \propto \exp\big(-\eta\,(L_t(a) + \ell_t(a))\big), \qquad L_t = \ell_1 + \cdots + \ell_t,

because the factors of line L6 telescope: the sum of 2ℓs−ℓs−12\ell_s - \ell_{s-1} over s≤ts \le t is Lt+ℓtL_t + \ell_t. Plain Hedge uses LtL_t alone. So optimistic Hedge is Hedge run on the cumulative loss plus a prediction ℓt\ell_t of the next loss. It is the entropy case of optimistic mirror descent and of optimistic follow the regularized leader, in the frame of online convex optimization. In code, the weights are better kept as logarithms, so that they do not underflow.

Example

Take k=2k = 2 actions, η=1\eta = 1, and the losses (1,0)(1, 0), (1,0)(1, 0), (0,1)(0, 1), (0,1)(0, 1). The table shows the distribution of optimistic Hedge, the exponent −(Lt−1+ℓt−1)-(L_{t-1} + \ell_{t-1}) that defines it, and the distribution of plain Hedge, rounded to four places.

round loss exponent optimistic xtx_t it pays Hedge xtx_t it pays
1 (1, 0) (0, 0) (0.5, 0.5) 0.5 (0.5, 0.5) 0.5
2 (1, 0) (-2, 0) (0.1192, 0.8808) 0.1192 (0.2689, 0.7311) 0.2689
3 (0, 1) (-3, 0) (0.0474, 0.9526) 0.9526 (0.1192, 0.8808) 0.8808
4 (0, 1) (-2, -2) (0.5, 0.5) 0.5 (0.2689, 0.7311) 0.7311

Each fixed action loses 2 in total. Optimistic Hedge loses 2.0718 and plain Hedge loses 2.3808. The optimistic learner moves away from action 1 faster in round 2, loses more in round 3 when the losses switch, and then returns to the middle in round 4, while Hedge is still behind.

Correctness

RVU bound. With ℓ0=0\ell_0 = 0, for every TT and every adversary,

RegT≤ln⁡kη+η∑t=1T∥ℓt−ℓt−1∥∞2−18η∑t=2T∥xt−xt−1∥12.\text{Reg}_T \le \frac{\ln k}{\eta} + \eta \sum_{t=1}^{T} \|\ell_t - \ell_{t-1}\|_\infty^2 - \frac{1}{8\eta} \sum_{t=2}^{T} \|x_t - x_{t-1}\|_1^2 .

Syrgkanis, Agarwal, Luo and Schapire (2015) called a bound of this form, regret bounded by variation in utilities, the RVU property. The proof compares xtx_t with the iterate yt∝e−ηLt−1y_t \propto e^{-\eta L_{t-1}} of plain Hedge and uses that the negative entropy is 1-strongly convex in the ℓ1\ell_1 norm (Pinsker's inequality). Since ∥ℓt−ℓt−1∥∞≤1\|\ell_t - \ell_{t-1}\|_\infty \le 1, the regret is at most ln⁡k/η+ηT\ln k/\eta + \eta T against any adversary, which is 2Tln⁡k2\sqrt{T \ln k} for η=ln⁡k/T\eta = \sqrt{\ln k / T}.

Games. Let nn players each run optimistic Hedge with the same η≤1/(8(n−1))\eta \le 1/(\sqrt{8}(n-1)) on their expected losses, scaled to [0,1][0, 1]. A change of one opponent's distribution by dd in the ℓ1\ell_1 norm changes a player's loss vector by at most dd in the ∞\infty norm, so ∥ℓti−ℓt−1i∥∞2≤(n−1)∑j≠i∥xtj−xt−1j∥12\|\ell^i_t - \ell^i_{t-1}\|_\infty^2 \le (n-1)\sum_{j \ne i}\|x^j_t - x^j_{t-1}\|_1^2. Summing the RVU bounds over the players, the variation terms are cancelled by the movement terms, and

∑i=1nRegTi≤nln⁡kη+nη\sum_{i=1}^{n} \text{Reg}^i_T \le \frac{n \ln k}{\eta} + n\eta

for every TT. In a two-player zero-sum game the sum of the two regrets is TT times the duality gap of the average strategies, so the averages are an O(log⁡k/T)O(\log k / T) equilibrium. The full proofs are in the proof file.

The bound on the sum does not bound each regret in a general game. Syrgkanis et al. (2015) showed that each regret is O(T1/4)O(T^{1/4}) with a suitable step size, and Daskalakis, Fishelson and Golowich (2021, Theorem 3.1) showed that with η=1/(Cnlog⁡4T)\eta = 1/(C n \log^4 T), for a suitable constant CC, each player has regret O(nlog⁡klog⁡4T)O(n \log k \log^4 T). Their proof is long; it bounds the higher-order differences of the loss sequence and is not reproduced here.

Proof: Optimistic Hedge satisfies the RVU bound, and in self-play the sum of the regrets is at most n ln k / eta + n etafull

There are kk actions and loss vectors ℓ1,ℓ2,…∈[0,1]k\ell_1, \ell_2, \ldots \in [0, 1]^k, and ℓ0=0\ell_0 = 0. Write Lt=ℓ1+⋯+ℓtL_t = \ell_1 + \cdots + \ell_t and L0=0L_0 = 0. Let Δ\Delta be the simplex of distributions on the actions and R(x)=∑ax(a)ln⁡x(a)R(x) = \sum_a x(a) \ln x(a) the negative entropy, with 0ln⁡0=00 \ln 0 = 0. For a vector cc, let sm⁡(c)\operatorname{sm}(c) be the distribution with sm⁡(c)(a)∝e−c(a)\operatorname{sm}(c)(a) \propto e^{-c(a)}.

Claim 1 (closed form). The distribution xtx_t played in round tt is xt=sm⁡(η(Lt−1+ℓt−1))x_t = \operatorname{sm}(\eta(L_{t-1} + \ell_{t-1})).

Proof. By induction on tt. For t=1t = 1, line L1 gives the uniform distribution, which is sm⁡(0)\operatorname{sm}(0). Line L6 multiplies the weight of aa by e−η(2ℓt(a)−ℓt−1(a))e^{-\eta(2\ell_t(a) - \ell_{t-1}(a))} (line L8 makes m=ℓt−1m = \ell_{t-1}), and line L7 rescales all weights by the same factor, which does not change sm⁡\operatorname{sm}. The exponent becomes η(Lt−1+ℓt−1+2ℓt−ℓt−1)=η(Lt+ℓt)\eta(L_{t-1} + \ell_{t-1} + 2\ell_t - \ell_{t-1}) = \eta(L_t + \ell_t). □\square

Claim 2 (softmax is the regularized minimizer). For every vector cc, sm⁡(c)\operatorname{sm}(c) is the unique minimizer over Δ\Delta of φ(x)=⟨x,c⟩+R(x)\varphi(x) = \langle x, c \rangle + R(x), and for every y∈Δy \in \Delta, φ(y)−φ(sm⁡(c))≥12∥y−sm⁡(c)∥12\varphi(y) - \varphi(\operatorname{sm}(c)) \ge \frac{1}{2}\|y - \operatorname{sm}(c)\|_1^2.

Proof. Let x∗=sm⁡(c)x^* = \operatorname{sm}(c), so x∗(a)=e−c(a)/Zx^*(a) = e^{-c(a)}/Z with Z=∑be−c(b)Z = \sum_b e^{-c(b)}, and c(a)=−ln⁡x∗(a)−ln⁡Zc(a) = -\ln x^*(a) - \ln Z. Then for y∈Δy \in \Delta,

φ(y)=∑ay(a)(−ln⁡x∗(a)−ln⁡Z)+∑ay(a)ln⁡y(a)=KL(y ∥ x∗)−ln⁡Z,\varphi(y) = \sum_a y(a)\big(-\ln x^*(a) - \ln Z\big) + \sum_a y(a) \ln y(a) = \mathrm{KL}(y \,\|\, x^*) - \ln Z,

and φ(x∗)=−ln⁡Z\varphi(x^*) = -\ln Z. So φ(y)−φ(x∗)=KL(y ∥ x∗)≥12∥y−x∗∥12\varphi(y) - \varphi(x^*) = \mathrm{KL}(y \,\|\, x^*) \ge \frac{1}{2}\|y - x^*\|_1^2 by Pinsker's inequality, and the difference is 0 only for y=x∗y = x^*. □\square

Define the Hedge iterates yt=sm⁡(ηLt−1)y_t = \operatorname{sm}(\eta L_{t-1}), and the functions Φt(x)=η⟨x,Lt⟩+R(x)\Phi_t(x) = \eta \langle x, L_t \rangle + R(x) and Ψt(x)=η⟨x,Lt−1+ℓt−1⟩+R(x)\Psi_t(x) = \eta\langle x, L_{t-1} + \ell_{t-1} \rangle + R(x). By Claims 1 and 2, yt+1y_{t+1} minimizes Φt\Phi_t and xtx_t minimizes Ψt\Psi_t. Note Φt=Ψt+η⟨⋅,ℓt−ℓt−1⟩\Phi_t = \Psi_t + \eta\langle \cdot, \ell_t - \ell_{t-1}\rangle and Ψt=Φt−1+η⟨⋅,ℓt−1⟩\Psi_t = \Phi_{t-1} + \eta\langle \cdot, \ell_{t-1}\rangle.

Claim 3 (one step). For every t≥1t \ge 1,

η(⟨xt−yt+1,ℓt−1⟩+⟨yt+1,ℓt⟩)≤Φt(yt+1)−Φt−1(yt)−12∥xt−yt∥12−12∥yt+1−xt∥12.\eta\big(\langle x_t - y_{t+1}, \ell_{t-1} \rangle + \langle y_{t+1}, \ell_t \rangle\big) \le \Phi_t(y_{t+1}) - \Phi_{t-1}(y_t) - \tfrac{1}{2}\|x_t - y_t\|_1^2 - \tfrac{1}{2}\|y_{t+1} - x_t\|_1^2 .

Proof. By Claim 2 applied to Ψt\Psi_t with minimizer xtx_t, and to Φt−1\Phi_{t-1} with minimizer yty_t,

Φt(yt+1)=Ψt(yt+1)+η⟨yt+1,ℓt−ℓt−1⟩≥Ψt(xt)+12∥yt+1−xt∥12+η⟨yt+1,ℓt−ℓt−1⟩,\Phi_t(y_{t+1}) = \Psi_t(y_{t+1}) + \eta\langle y_{t+1}, \ell_t - \ell_{t-1} \rangle \ge \Psi_t(x_t) + \tfrac{1}{2}\|y_{t+1} - x_t\|_1^2 + \eta\langle y_{t+1}, \ell_t - \ell_{t-1} \rangle,
Ψt(xt)=Φt−1(xt)+η⟨xt,ℓt−1⟩≥Φt−1(yt)+12∥xt−yt∥12+η⟨xt,ℓt−1⟩.\Psi_t(x_t) = \Phi_{t-1}(x_t) + \eta\langle x_t, \ell_{t-1}\rangle \ge \Phi_{t-1}(y_t) + \tfrac{1}{2}\|x_t - y_t\|_1^2 + \eta\langle x_t, \ell_{t-1}\rangle.

Combine the two lines and rearrange; ⟨xt,ℓt−1⟩+⟨yt+1,ℓt−ℓt−1⟩\langle x_t, \ell_{t-1}\rangle + \langle y_{t+1}, \ell_t - \ell_{t-1}\rangle is the left side divided by η\eta. □\square

Claim 4 (the RVU bound). For every T≥1T \ge 1 and every action uu,

∑t=1T⟨xt,ℓt⟩−LT(u)≤ln⁡kη+η∑t=1T∥ℓt−ℓt−1∥∞2−18η∑t=2T∥xt−xt−1∥12,\sum_{t=1}^T \langle x_t, \ell_t \rangle - L_T(u) \le \frac{\ln k}{\eta} + \eta \sum_{t=1}^T \|\ell_t - \ell_{t-1}\|_\infty^2 - \frac{1}{8\eta}\sum_{t=2}^T \|x_t - x_{t-1}\|_1^2,

and also the regret is at most ln⁡k/η+(η/2)∑t=1T∥ℓt−ℓt−1∥∞2\ln k/\eta + (\eta/2)\sum_{t=1}^T \|\ell_t - \ell_{t-1}\|_\infty^2.

Proof. Split ⟨xt,ℓt⟩=⟨xt−yt+1,ℓt−ℓt−1⟩+⟨xt−yt+1,ℓt−1⟩+⟨yt+1,ℓt⟩\langle x_t, \ell_t \rangle = \langle x_t - y_{t+1}, \ell_t - \ell_{t-1}\rangle + \langle x_t - y_{t+1}, \ell_{t-1} \rangle + \langle y_{t+1}, \ell_t \rangle. Sum Claim 3 over t=1,…,Tt = 1, \ldots, T; the right side telescopes to ΦT(yT+1)−Φ0(y1)≤ΦT(eu)−min⁡R=ηLT(u)−min⁡R\Phi_T(y_{T+1}) - \Phi_0(y_1) \le \Phi_T(e_u) - \min R = \eta L_T(u) - \min R, where eue_u is the point mass on uu, R(eu)=0R(e_u) = 0, and Φ0=R\Phi_0 = R. Since min⁡ΔR=−ln⁡k\min_\Delta R = -\ln k,

∑t⟨xt,ℓt⟩−LT(u)≤ln⁡kη+∑t⟨xt−yt+1,ℓt−ℓt−1⟩−12η∑t(∥xt−yt∥12+∥yt+1−xt∥12).\sum_t \langle x_t, \ell_t \rangle - L_T(u) \le \frac{\ln k}{\eta} + \sum_t \langle x_t - y_{t+1}, \ell_t - \ell_{t-1}\rangle - \frac{1}{2\eta}\sum_t \big(\|x_t - y_t\|_1^2 + \|y_{t+1} - x_t\|_1^2\big).

By Hoelder, ⟨xt−yt+1,ℓt−ℓt−1⟩≤∥xt−yt+1∥1∥ℓt−ℓt−1∥∞\langle x_t - y_{t+1}, \ell_t - \ell_{t-1} \rangle \le \|x_t - y_{t+1}\|_1 \|\ell_t - \ell_{t-1}\|_\infty. With ab≤η2b2+12ηa2ab \le \frac{\eta}{2}b^2 + \frac{1}{2\eta}a^2 this gives the second bound at once. With ab≤ηb2+14ηa2ab \le \eta b^2 + \frac{1}{4\eta}a^2 instead, the regret is at most

ln⁡kη+η∑t∥ℓt−ℓt−1∥∞2−14η∑t=1T(∥xt−yt∥12+∥yt+1−xt∥12).\frac{\ln k}{\eta} + \eta\sum_t \|\ell_t - \ell_{t-1}\|_\infty^2 - \frac{1}{4\eta}\sum_{t=1}^T\big(\|x_t - y_t\|_1^2 + \|y_{t+1} - x_t\|_1^2\big).

For t≥2t \ge 2, ∥xt−xt−1∥12≤2∥xt−yt∥12+2∥yt−xt−1∥12\|x_t - x_{t-1}\|_1^2 \le 2\|x_t - y_t\|_1^2 + 2\|y_t - x_{t-1}\|_1^2, and summing over t=2,…,Tt = 2, \ldots, T uses each term of the last sum at most once. So the last sum is at least 12∑t=2T∥xt−xt−1∥12\frac{1}{2}\sum_{t=2}^T \|x_t - x_{t-1}\|_1^2, which gives the RVU bound. □\square

Claim 5 (adversarial losses). Against every adversary, the regret is at most ln⁡k/η+ηT/2\ln k / \eta + \eta T/2; for η=2ln⁡k/T\eta = \sqrt{2 \ln k / T} it is at most 2Tln⁡k\sqrt{2 T \ln k}.

Proof. The losses are in [0,1][0, 1], so ∥ℓt−ℓt−1∥∞≤1\|\ell_t - \ell_{t-1}\|_\infty \le 1. Use the second bound of Claim 4. □\square

Claim 6 (self-play). Let n≥2n \ge 2 players of a finite game, each with at most kk strategies, run optimistic Hedge with the same η≤1/(8(n−1))\eta \le 1/(\sqrt{8}(n-1)), where player ii gets the loss vector ℓti(a)=(Mi−ui(a,xt−i))/Δi\ell^i_t(a) = (M_i - u_i(a, x^{-i}_t))/\Delta_i, with MiM_i the largest utility, Δi>0\Delta_i > 0 the range of uiu_i, and xt−ix^{-i}_t the product of the other players' distributions. Then for every TT, ∑iRegTi≤nln⁡k/η+nη\sum_i \text{Reg}^i_T \le n \ln k/\eta + n\eta.

Proof. The losses are in [0,1][0, 1]. For a function ff with values in [0,1][0, 1] and two distributions p,qp, q, ∣Epf−Eqf∣≤∥p−q∥1|\mathbb{E}_p f - \mathbb{E}_q f| \le \|p - q\|_1. Change the opponents' distributions one at a time from round t−1t - 1 to round tt: each change of a product in one coordinate jj changes ℓi(a)\ell^i(a) by at most ∥xtj−xt−1j∥1\|x^j_t - x^j_{t-1}\|_1. So ∥ℓti−ℓt−1i∥∞≤∑j≠i∥xtj−xt−1j∥1\|\ell^i_t - \ell^i_{t-1}\|_\infty \le \sum_{j \ne i}\|x^j_t - x^j_{t-1}\|_1 for t≥2t \ge 2, and by the Cauchy-Schwarz inequality its square is at most (n−1)∑j≠i∥xtj−xt−1j∥12(n-1)\sum_{j \ne i}\|x^j_t - x^j_{t-1}\|_1^2. For t=1t = 1, ∥ℓ1i−ℓ0i∥∞2≤1\|\ell^i_1 - \ell^i_0\|_\infty^2 \le 1. Sum Claim 4 over the players:

∑iRegTi≤nln⁡kη+nη+∑t=2T(η(n−1)2−18η)∑j∥xtj−xt−1j∥12,\sum_i \text{Reg}^i_T \le \frac{n \ln k}{\eta} + n\eta + \sum_{t=2}^T \Big(\eta(n-1)^2 - \frac{1}{8\eta}\Big)\sum_j \|x^j_t - x^j_{t-1}\|_1^2,

since each jj appears for n−1n - 1 players ii. For η≤1/(8(n−1))\eta \le 1/(\sqrt{8}(n-1)) the bracket is at most 0. □\square

Claim 7 (zero-sum games). In a two-player game with u2=−u1u_2 = -u_1, the sum of the two regrets equals T/ΔT/\Delta times the duality gap max⁡au1(a,xˉ2)−min⁡bu1(xˉ1,b)\max_a u_1(a, \bar{x}^2) - \min_b u_1(\bar{x}^1, b) of the average distributions xˉi=1T∑txti\bar{x}^i = \frac{1}{T}\sum_t x^i_t, where Δ\Delta is the range of u1u_1. So the averages form a Δ(2ln⁡k/η+2η)/T\Delta(2\ln k/\eta + 2\eta)/T equilibrium.

Proof. Write u=u1u = u_1, with largest value MM and smallest value m=M−Δm = M - \Delta. Player 2 has M2=−mM_2 = -m. So Δ RegT1=∑t(M−u(xt1,xt2))−min⁡a∑t(M−u(a,xt2))=max⁡aTu(a,xˉ2)−∑tu(xt1,xt2)\Delta\,\text{Reg}^1_T = \sum_t (M - u(x^1_t, x^2_t)) - \min_a \sum_t (M - u(a, x^2_t)) = \max_a T u(a, \bar{x}^2) - \sum_t u(x^1_t, x^2_t), and Δ RegT2=∑tu(xt1,xt2)−min⁡bTu(xˉ1,b)\Delta\,\text{Reg}^2_T = \sum_t u(x^1_t, x^2_t) - \min_b T u(\bar{x}^1, b), by linearity of uu in each distribution. Add the two lines, and use Claim 6 with n=2n = 2. The duality gap is at least 0, because max⁡au(a,xˉ2)≥u(xˉ1,xˉ2)≥min⁡bu(xˉ1,b)\max_a u(a, \bar{x}^2) \ge u(\bar{x}^1, \bar{x}^2) \ge \min_b u(\bar{x}^1, b). □\square

Conclusion. Lines L1 to L8 play xt=sm⁡(η(Lt−1+ℓt−1))x_t = \operatorname{sm}(\eta(L_{t-1} + \ell_{t-1})) (Claim 1), the RVU bound holds after every round (Claim 4), the regret is O(Tlog⁡k)O(\sqrt{T \log k}) against any adversary (Claim 5), and in self-play with η≤1/(8(n−1))\eta \le 1/(\sqrt{8}(n-1)) the sum of the regrets stays bounded by nln⁡k/η+nηn \ln k/\eta + n\eta for all TT (Claim 6). ■\blacksquare

Complexity

Each round of lines L5 to L8 takes O(k)O(k) time, so TT rounds take O(kT)O(k T), the same as plain Hedge. The memory is O(k)O(k): the distribution and the last loss vector. In a game, computing the expected loss vectors usually dominates, at O(nkn)O(n k^n) per round for a game in normal form. For an ε\varepsilon-coarse correlated equilibrium, the number of rounds drops from O(log⁡k/ε2)O(\log k / \varepsilon^2) for plain Hedge to O~(nlog⁡k/ε)\tilde{O}(n \log k / \varepsilon), where the tilde hides factors polylogarithmic in 1/ε1/\varepsilon.

Properties

  • Robust. Against an arbitrary adversary the regret is O(Tlog⁡k)O(\sqrt{T \log k}), the same order as Hedge.
  • Adaptive to slow change. The regret depends on the total squared variation of the losses, not on TT. For a constant loss sequence it is at most ln⁡k/η+η\ln k / \eta + \eta.
  • Uncoupled. Each player uses only its own loss vectors; it does not need the payoffs of the others.
  • Last iterate. In two-player zero-sum games with a unique equilibrium, the distributions themselves converge to it for a small enough step size (Daskalakis and Panageas, 2019), while those of Hedge spiral away. In general games the last iterate need not converge.

Variants and improvements

  • Optimistic mirror descent and follow the regularized leader. Any strongly convex regularizer gives an RVU bound; the entropy gives Hedge, and the Euclidean norm gives optimistic online gradient descent.
  • Swap regret. Optimistic Hedge inside the copies of the Blum-Mansour reduction gives polylogarithmic swap regret in games, and so fast convergence to correlated equilibria (Anagnostides, Daskalakis, Farina, Fishelson, Golowich and Sandholm, 2022).
  • Other predictions. Line L6 uses ℓt−1\ell_{t-1} as the guess of ℓt\ell_t. Any other predictable guess fits the same analysis, with ∥ℓt−mt∥\|\ell_t - m_t\| in place of ∥ℓt−ℓt−1∥\|\ell_t - \ell_{t-1}\|.
  • Bandit feedback. With only the loss of the chosen action, as in the multi-armed bandit and Exp3, the variation of the losses cannot be observed directly, and these fast rates do not carry over in the same form.

When to use it

Use it in place of Hedge when all players of a game run the same kind of learner, for example to compute approximate coarse correlated equilibria, or zero-sum equilibria with an O(1/T)O(1/T) rate, by self-play. It costs nothing extra per round and keeps the worst-case guarantee. When the losses come from an adversary with no structure, it gives no gain over Hedge.

Pitfalls

  • Factor 2 in line L6. Using exp⁡(−η ℓ(a))\exp(-\eta\,\ell(a)) gives plain Hedge, and using exp⁡(−2η ℓ(a))\exp(-2\eta\,\ell(a)) without the +η m(a)+\eta\,m(a) gives Hedge with step 2η2\eta. The prediction must be added for the next round and removed in the round after, which is what 2ℓ−m2\ell - m does.
  • Forgetting line L8. If mm stays zero, the exponents grow by 2η ℓ2\eta\,\ell, again plain Hedge with step 2η2\eta.
  • Step size too large in games. The cancellation in the sum of regrets needs η≤1/(8(n−1))\eta \le 1/(\sqrt{8}(n-1)) in the form proved here. With a larger step, the variation terms of line L6 may not be absorbed, and the dynamics can oscillate.
  • Underflow in lines L6 and L7. After many rounds the weights x(a)x(a) become tiny and can underflow to 0, after which line L6 can never raise them again. Keep the logarithms of the weights and normalize against the largest one.

Implementations

The Python code keeps the logarithms of the weights and implements lines L1 to L8 in floating point. The tests check the closed form xt+1∝e−η(Lt+ℓt)x_{t+1} \propto e^{-\eta(L_t + \ell_t)}, the RVU bound and the simpler bound ln⁡k/η+(η/2)∑t∥ℓt−ℓt−1∥∞2\ln k/\eta + (\eta/2)\sum_t \|\ell_t - \ell_{t-1}\|_\infty^2 after many prefixes against random, adaptive, alternating and slowly drifting adversaries, the bound nln⁡k/η+nηn \ln k / \eta + n\eta on the sum of regrets in self-play in chicken, rock-paper-scissors and a random three-player game, the identity between the sum of regrets and the duality gap in random zero-sum games, and that in matching pennies the last iterate approaches the equilibrium while that of Hedge moves away.

Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.

agtgame.pyI0RawGitHub
# SPDX-License-Identifier: MIT
# Shared helper of the algorithmic game theory pages of https://algorithm.tamnd.com/. The authors are listed in prov/.
"""Finite games in normal form, with exact payoffs and exact equilibrium checks.

A game is a pair (sizes, pay). sizes is a tuple (k_1, ..., k_n) of the numbers of pure
strategies of the n players; the pure strategies of player i are 0, ..., k_i - 1. pay is a
dict that maps each profile, a tuple s = (s_1, ..., s_n), to a tuple of n Fraction
utilities, which each player maximizes. A cost game passes the utilities u = -c.

A mixed profile sigma is a list of n lists; sigma[i][a] is the probability, a Fraction,
that player i plays a. A distribution on profiles (for correlated and coarse correlated
equilibria) is a dict from profiles to Fraction; missing profiles have probability 0.

All checks are exact: there is no tolerance anywhere. There is no LP and no equilibrium
algorithm in this module.
"""

from fractions import Fraction
from itertools import product


def profiles(sizes):
    """Return the list of all pure profiles, in lexicographic order."""
    return list(product(*(range(k) for k in sizes)))


def from_bimatrix(A, B):
    """Return the game of the bimatrix (A, B): the row player gets A[r][c], the column player B[r][c]."""
    m, n = len(A), len(A[0])
    if len(B) != m or any(len(row) != n for row in A) or any(len(row) != n for row in B):
        raise ValueError("A and B must be m by n matrices of the same shape")
    pay = {(r, c): (Fraction(A[r][c]), Fraction(B[r][c])) for r in range(m) for c in range(n)}
    return (m, n), pay


def random_game(rng, sizes, lo, hi):
    """Return a game with integer utilities drawn uniformly from lo to hi (inclusive), as Fraction."""
    sizes = tuple(sizes)
    pay = {s: tuple(Fraction(rng.randint(lo, hi)) for _ in sizes) for s in profiles(sizes)}
    return sizes, pay


def _replace(s, i, a):
    return s[:i] + (a,) + s[i + 1:]


def _weight(sigma, s, skip=None):
    w = Fraction(1)
    for k, a in enumerate(s):
        if k != skip:
            w *= sigma[k][a]
            if w == 0:
                return w
    return w


def expected_utility(game, i, sigma):
    """Return u_i(sigma), the expected utility of player i when the players draw independently."""
    sizes, pay = game
    return sum((_weight(sigma, s) * pay[s][i] for s in profiles(sizes)), Fraction(0))


def deviation_payoffs(game, i, sigma):
    """Return the list of u_i(a, sigma_-i) for every pure strategy a of player i."""
    sizes, pay = game
    out = [Fraction(0)] * sizes[i]
    for s in profiles(sizes):
        w = _weight(sigma, s, skip=i)
        if w:
            out[s[i]] += w * pay[s][i]
    return out


def pure_best_responses(game, i, sigma):
    """Return the sorted list of the pure strategies of player i that maximize u_i(a, sigma_-i)."""
    d = deviation_payoffs(game, i, sigma)
    best = max(d)
    return [a for a, v in enumerate(d) if v == best]


def max_gain(game, sigma):
    """Return the largest gain of any player from a pure deviation: max_i (max_a u_i(a, sigma_-i) - u_i(sigma)).

    The result is never negative, because u_i(sigma) is an average of the deviation payoffs.
    """
    sizes, _ = game
    gain = Fraction(0)
    for i in range(len(sizes)):
        d = deviation_payoffs(game, i, sigma)
        u = sum((p * v for p, v in zip(sigma[i], d)), Fraction(0))
        gain = max(gain, max(d) - u)
    return gain


def is_distribution(p):
    """Return True if p (a sequence, or a dict of probabilities) is nonnegative and sums to exactly 1."""
    vals = list(p.values()) if isinstance(p, dict) else list(p)
    return all(v >= 0 for v in vals) and sum(vals, Fraction(0)) == 1


def _is_mixed_profile(game, sigma):
    sizes, _ = game
    return len(sigma) == len(sizes) and all(
        len(sigma[i]) == k and is_distribution(sigma[i]) for i, k in enumerate(sizes))


def is_nash(game, sigma):
    """Return True if sigma is a mixed Nash equilibrium: no pure deviation gains for any player."""
    return _is_mixed_profile(game, sigma) and max_gain(game, sigma) == 0


def is_epsilon_nash(game, sigma, eps):
    """Return True if no player gains more than eps by a pure deviation (the additive form)."""
    return _is_mixed_profile(game, sigma) and max_gain(game, sigma) <= eps


def _check_dist(game, dist):
    sizes, _ = game
    allowed = set(profiles(sizes))
    return all(s in allowed for s in dist) and is_distribution(dist)


def is_correlated(game, dist):
    """Return True if dist is a correlated equilibrium.

    For every player i and every pair a, b of its pure strategies, the sum over the profiles s
    with s_i = a of dist(s) (u_i(s) - u_i(b, s_-i)) is at least 0.
    """
    sizes, pay = game
    if not _check_dist(game, dist):
        return False
    for i, k in enumerate(sizes):
        for a in range(k):
            rows = [(q, s) for s, q in dist.items() if s[i] == a and q]
            for b in range(k):
                if b != a and sum((q * (pay[s][i] - pay[_replace(s, i, b)][i]) for q, s in rows), Fraction(0)) < 0:
                    return False
    return True


def is_coarse_correlated(game, dist):
    """Return True if dist is a coarse correlated equilibrium.

    For every player i and every pure strategy b, E_{s ~ dist} u_i(s) >= E_{s ~ dist} u_i(b, s_-i).
    """
    sizes, pay = game
    if not _check_dist(game, dist):
        return False
    for i, k in enumerate(sizes):
        here = sum((q * pay[s][i] for s, q in dist.items()), Fraction(0))
        for b in range(k):
            if sum((q * pay[_replace(s, i, b)][i] for s, q in dist.items()), Fraction(0)) > here:
                return False
    return True


def pure_nash_brute(game):
    """Return the list of all pure Nash equilibria, found by checking every profile."""
    sizes, pay = game
    out = []
    for s in profiles(sizes):
        if all(pay[s][i] >= pay[_replace(s, i, b)][i] for i in range(len(sizes)) for b in range(sizes[i])):
            out.append(s)
    return out
optimistic_hedge.py 103 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/optimistic-hedge/. The authors are listed in prov/259HJ8.jsonl.
"""Optimistic Hedge (optimistic multiplicative weights) on k actions with losses in [0, 1].

After it sees the loss vector l of a round, the learner multiplies the weight of action a by
exp(-eta (2 l(a) - m(a))), where m is the loss vector of the round before (0 before the
first round), and normalizes. This is Hedge on the cumulative losses plus one extra copy of
the last loss, used as a prediction of the next one. Arithmetic is in float.
"""

import math


class OptimisticHedge:
    """An online learner. Call distribution() to get the current x, then update(loss)."""

    def __init__(self, k, eta):
        if k < 1 or eta <= 0:
            raise ValueError("need k >= 1 and eta > 0")
        self.eta = eta
        self.x = [1.0 / k] * k  # @line L1
        self.z = [0.0] * k
        self.m = [0.0] * k  # @line L2

    def distribution(self):
        return list(self.x)

    def update(self, loss):
        eta = self.eta
        # The weights are kept as logarithms z(a), so that no weight underflows; x is the softmax of z.
        for a in range(len(self.z)):  # @line L5
            self.z[a] -= eta * (2 * loss[a] - self.m[a])  # @line L6
        top = max(self.z)
        w = [math.exp(e - top) for e in self.z]
        total = sum(w)
        self.x = [v / total for v in w]  # @line L7
        self.m = list(loss)  # @line L8


def play(k, T, eta, adversary):
    """Run optimistic Hedge for T rounds against adversary(t, x), which returns the loss vector
    of round t and may look at the current distribution x. Return the distributions and losses."""
    learner = OptimisticHedge(k, eta)
    xs, losses = [], []
    for t in range(T):  # @line L3
        x = learner.distribution()
        loss = adversary(t, x)  # @line L4
        learner.update(loss)
        xs.append(x)
        losses.append(list(loss))
    return xs, losses


def regret(xs, losses):
    """Return sum_t x_t . l_t minus the least total loss of a fixed action."""
    if not xs:
        return 0.0
    k = len(xs[0])
    mine = sum(sum(a * b for a, b in zip(x, l)) for x, l in zip(xs, losses))
    return mine - min(sum(l[a] for l in losses) for a in range(k))


def rvu_bound(xs, losses, eta):
    """Return ln k / eta + eta sum_t |l_t - l_{t-1}|_inf^2 - (1 / (8 eta)) sum_{t >= 2} |x_t - x_{t-1}|_1^2,
    with l_0 = 0: the RVU bound on the regret of optimistic Hedge."""
    k = len(xs[0])
    prev = [0.0] * k
    var = 0.0
    for l in losses:
        var += max(abs(a - b) for a, b in zip(l, prev)) ** 2
        prev = l
    move = sum(sum(abs(a - b) for a, b in zip(x, y)) ** 2 for x, y in zip(xs[1:], xs))
    return math.log(k) / eta + eta * var - move / (8 * eta)


def self_play(game, T, eta):
    """Let every player of a finite game (sizes, pay), as in the helper agtgame, run optimistic
    Hedge on its expected losses (max u_i - u_i(a, x_-i)) / range_i. Return the list of the
    profiles of distributions and the list of the loss vectors of each player."""
    sizes, pay = game
    n = len(sizes)
    hi = [float(max(pay[s][i] for s in pay)) for i in range(n)]
    span = [hi[i] - float(min(pay[s][i] for s in pay)) or 1.0 for i in range(n)]
    learners = [OptimisticHedge(k, eta) for k in sizes]
    payf = {s: [float(v) for v in u] for s, u in pay.items()}
    hist_x, hist_l = [], []
    for _ in range(T):
        xs = [L.distribution() for L in learners]
        losses = []
        for i in range(n):
            d = [0.0] * sizes[i]
            for s, u in payf.items():
                w = 1.0
                for j, a in enumerate(s):
                    if j != i:
                        w *= xs[j][a]
                d[s[i]] += w * u[i]
            losses.append([(hi[i] - v) / span[i] for v in d])
        for L, l in zip(learners, losses):
            L.update(l)
        hist_x.append(xs)
        hist_l.append(losses)
    return hist_x, hist_l
test_optimistic_hedge.py 128 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/optimistic-hedge/. The authors are listed in prov/259HJ8.jsonl.
"""Tests of optimistic Hedge: the closed form, the RVU bound at every round, the bound on the
sum of the regrets in self-play, the duality gap in zero-sum games, and the last iterate."""

import math
import random

import pytest

from agtgame import from_bimatrix, random_game
from optimistic_hedge import OptimisticHedge, play, regret, rvu_bound, self_play

CHICKEN = from_bimatrix([[0, 7], [2, 6]], [[0, 2], [7, 6]])
RPS = from_bimatrix([[0, -1, 1], [1, 0, -1], [-1, 1, 0]], [[0, 1, -1], [-1, 0, 1], [1, -1, 0]])


def softmax(v):
    top = max(v)
    w = [math.exp(a - top) for a in v]
    s = sum(w)
    return [a / s for a in w]


def test_closed_form_and_validation():
    """x_{t+1} is proportional to exp(-eta (L_t + l_t)), with L_t the cumulative loss."""
    rng = random.Random(4)
    k, eta = 4, 0.7
    learner = OptimisticHedge(k, eta)
    cum = [0.0] * k
    assert learner.distribution() == [0.25] * 4
    for _ in range(60):
        loss = [rng.random() for _ in range(k)]
        learner.update(loss)
        cum = [c + v for c, v in zip(cum, loss)]
        want = softmax([-eta * (c + v) for c, v in zip(cum, loss)])
        assert all(abs(a - b) < 1e-12 for a, b in zip(learner.distribution(), want))
    with pytest.raises(ValueError):
        OptimisticHedge(0, 1.0)
    with pytest.raises(ValueError):
        OptimisticHedge(3, -1.0)


def test_no_underflow_on_long_runs():
    xs, _ = play(3, 2000, 5.0, lambda t, x: [0.0, 1.0, 1.0])
    assert abs(sum(xs[-1]) - 1) < 1e-12 and xs[-1][0] > 0.999999


def adversaries(rng, k):
    yield "random", lambda t, x: [rng.random() for _ in range(k)]
    yield "adaptive", lambda t, x: [1.0 if a == max(range(k), key=lambda b: x[b]) else 0.0 for a in range(k)]
    yield "alternating", lambda t, x: [float((a + t) % 2) for a in range(k)]
    yield "slow drift", lambda t, x: [0.5 + 0.5 * math.sin(t / 50 + a) for a in range(k)]


def test_rvu_bound_at_every_round():
    rng = random.Random(99)
    for k in (1, 2, 3, 5):
        for eta in (0.05, 0.3, 1.0):
            for _, adv in adversaries(rng, k):
                xs, losses = play(k, 250, eta, adv)
                for T in range(1, 251, 17):
                    r = regret(xs[:T], losses[:T])
                    assert r <= rvu_bound(xs[:T], losses[:T], eta) + 1e-9
                    var = sum(max(abs(a - b) for a, b in zip(l, p)) ** 2
                              for l, p in zip(losses[:T], [[0.0] * k] + losses[:T - 1]))
                    assert r <= math.log(k) / eta + eta / 2 * var + 1e-9


def test_slowly_changing_losses_give_small_regret():
    """When the losses change slowly, the regret stays far below the sqrt(T) of plain Hedge."""
    xs, losses = play(2, 4000, 0.5, lambda t, x: [0.5 + 0.5 * math.sin(t / 300), 0.5 - 0.5 * math.sin(t / 300)])
    assert regret(xs, losses) <= math.log(2) / 0.5 + 0.5 * (1 + 4000 * (1 / 300) ** 2) + 1e-9
    assert regret(xs, losses) < 3


def test_sum_of_regrets_in_self_play():
    """With eta <= 1 / (sqrt 8 (n - 1)), the sum of the regrets is at most n ln k / eta + n eta for every T."""
    three = random_game(random.Random(6), (2, 3, 2), -3, 3)
    for game, T in ((CHICKEN, 600), (RPS, 600), (three, 200)):
        sizes = game[0]
        n, k = len(sizes), max(sizes)
        eta = 1 / (math.sqrt(8) * (n - 1))
        hist_x, hist_l = self_play(game, T, eta)
        for t in range(1, T + 1, 13):
            total = sum(regret([xs[i] for xs in hist_x[:t]], [ls[i] for ls in hist_l[:t]]) for i in range(n))
            assert total <= n * math.log(k) / eta + n * eta + 1e-9


def test_zero_sum_duality_gap():
    """In a zero-sum game the sum of the two regrets is T times the duality gap of the averages."""
    rng = random.Random(21)
    for _ in range(3):
        m = rng.randint(2, 4)
        A = [[rng.randint(-3, 3) for _ in range(m + 1)] for _ in range(m)]
        game = from_bimatrix(A, [[-v for v in row] for row in A])
        T, eta = 400, 1 / math.sqrt(8)
        hist_x, hist_l = self_play(game, T, eta)
        xbar = [sum(xs[0][r] for xs in hist_x) / T for r in range(m)]
        ybar = [sum(xs[1][c] for xs in hist_x) / T for c in range(m + 1)]
        best_col = min(sum(xbar[r] * A[r][c] for r in range(m)) for c in range(m + 1))
        best_row = max(sum(A[r][c] * ybar[c] for c in range(m + 1)) for r in range(m))
        gap = best_row - best_col
        span = max(max(row) for row in A) - min(min(row) for row in A) or 1
        r1 = regret([xs[0] for xs in hist_x], [ls[0] for ls in hist_l])
        r2 = regret([xs[1] for xs in hist_x], [ls[1] for ls in hist_l])
        assert abs((r1 + r2) * span / T - gap) < 1e-9
        assert 0 <= gap <= span * (2 * math.log(m + 1) / eta + 2 * eta) / T + 1e-9


def test_last_iterate_in_matching_pennies():
    """The last iterate of optimistic Hedge approaches the equilibrium; that of Hedge moves away.
    The row player wants to match: its loss for an action is the chance that the column differs."""
    row, col = OptimisticHedge(2, 0.1), OptimisticHedge(2, 0.1)
    row.z = [0.3, 0.0]
    row.x = softmax(row.z)
    zr, zc = [0.3, 0.0], [0.0, 0.0]
    for _ in range(3000):
        x, y = row.distribution(), col.distribution()
        row.update([y[1], y[0]])
        col.update([x[0], x[1]])
        hx, hy = softmax(zr), softmax(zc)
        zr = [zr[0] - 0.1 * hy[1], zr[1] - 0.1 * hy[0]]
        zc = [zc[0] - 0.1 * hx[0], zc[1] - 0.1 * hx[1]]
    x, y = row.distribution(), col.distribution()
    assert abs(x[0] - 0.5) + abs(y[0] - 0.5) < 0.01
    hx, hy = softmax(zr), softmax(zc)
    assert abs(hx[0] - 0.5) + abs(hy[0] - 0.5) > 0.5

History

Optimism in online learning goes back to Rakhlin and Sridharan (2013), who used the last loss as a prediction in mirror descent and showed fast convergence in zero-sum games; Daskalakis, Deckelbaum and Kim had earlier given uncoupled dynamics for zero-sum games that converge at a rate close to 1/T1/T. Syrgkanis, Agarwal, Luo and Schapire (2015) defined the RVU property and extended the fast rates to general games. Daskalakis, Fishelson and Golowich (2021) brought the individual regret of optimistic Hedge down to polylogarithmic in TT.

References

  1. Fast convergence of regularized learning in games, the RVU property, its proof for optimistic follow the regularized leader and optimistic Hedge, the bound on the sum of regrets, and individual regret O(T^(1/4))
  2. Near-optimal no-regret learning in general games, Theorem 3.1 (with step size 1/(C m log^4 T), each of m players has regret O(m log n_i log^4 T))
  3. Optimization, learning, and games with predictable sequences, optimistic mirror descent with the last loss as the prediction, and its use in zero-sum games
  4. Tim Roughgarden, Twenty Lectures on Algorithmic Game Theory, Cambridge University Press, 2016, Lecture 17 (no-regret dynamics and multiplicative weights, the baseline that this method improves)

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH1 sketch
  • a full proof of time.worst
  • a full proof of space.extra
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW2 basic
  • a scene preset for the example