The Experts Problem and Weighted Majority
The last section of the course is about an algorithm that looks unrelated to linear programming: a rule for combining advice. Each day experts predict whether it will rain tomorrow; you make your own prediction after seeing theirs; then the truth is revealed. You want to make not many more mistakes than the best expert in hindsight, without knowing in advance who that is, and without any assumption about how the weather or the experts behave: the sequence may be chosen by an adversary. The weighted majority algorithm (Littlestone and Warmuth, 1994) achieves this by keeping a weight per expert and shrinking the weights of those who err. Its multiplicative update, generalized in the next lesson, turns out to be a fast approximate solver for zero-sum games and LPs.
Warm-up: the halving algorithm
Suppose one expert is perfect. Predict with the majority of the experts not yet caught in a mistake, and discard every expert who errs. Each of your mistakes means that at least half of the remaining candidates were wrong and are discarded, so after mistakes at most candidates remain. The perfect expert is never discarded, so :
import random
rng = random.Random(4)
n, T = 64, 200
perfect = rng.randrange(n)
alive = set(range(n))
mistakes = 0
for t in range(T):
truth = rng.randrange(2)
preds = [truth if i == perfect else rng.randrange(2) for i in range(n)]
votes = sum(preds[i] for i in alive)
guess = 1 if 2 * votes >= len(alive) else 0
mistakes += guess != truth
alive = {i for i in alive if preds[i] == truth}
print(f"{n} experts, one perfect: {mistakes} mistakes in {T} rounds, bound log2(n) = 6; candidates left: {len(alive)}")64 experts, one perfect: 1 mistakes in 200 rounds, bound log2(n) = 6; candidates left: 1
Here the other experts guess at random, so most of them are discarded in the first rounds and the algorithm errs only once; is the worst case, forced by experts who are wrong in a coordinated way.
Weighted majority
Without a perfect expert, discarding is too harsh: the best expert may err a few times and would be thrown out. Instead, multiply the weight of each erring expert by .
WEIGHTED-MAJORITY(eps) -- 0 < eps <= 1/2
1 w_i = 1 for every expert i
2 for t = 1 .. T
3 predict 1 if the total weight of experts predicting 1 is at least half of the total weight, else 0
4 observe the outcome
5 for every expert i that was wrong: w_i = (1 - eps) * w_iTheorem. Let be the number of mistakes of expert after rounds and those of weighted majority. For every ,
Proof. Follow the potential , with . When the algorithm errs, experts holding at least half of the weight were wrong, and their weight shrinks by a factor , so . After mistakes, . On the other hand, expert alone has weight . Taking logarithms of and using and for :
The factor 2 cannot be removed by any deterministic algorithm: with two experts who always disagree, an adversary who sees the algorithm's prediction (it is deterministic, so the adversary can compute it) makes it wrong in every round, while one of the two experts is right at least half the time. The remedy is randomization: predict 1 with probability equal to the weight fraction of experts predicting 1 (randomized weighted majority). Then the expected number of mistakes is at most , which the next lesson proves in a more general form.
The program pits both algorithms against 10 experts of different quality on random outcomes, and against the two-expert adversary.
import math
import random
def run(n, T, eps, expert_pred, outcome_rule, rng):
w = [1.0] * n
det_mistakes, rand_expected = 0, 0.0
expert_mistakes = [0] * n
for t in range(T):
preds = expert_pred(t)
W = sum(w)
w1 = sum(wi for wi, p in zip(w, preds) if p == 1)
guess = 1 if w1 >= W / 2 else 0
truth = outcome_rule(t, guess)
det_mistakes += guess != truth
rand_expected += (w1 / W) if truth == 0 else (1 - w1 / W) # chance the randomized version errs
for i in range(n):
if preds[i] != truth:
expert_mistakes[i] += 1
w[i] *= 1 - eps
return det_mistakes, rand_expected, min(expert_mistakes)
rng = random.Random(1994)
n, T, eps = 10, 1000, 0.1
truths = [rng.randrange(2) for _ in range(T)]
error_rates = [0.1 + 0.04 * i for i in range(n)] # expert i is wrong with rate 0.1 + 0.04 i
table = [[truths[t] ^ (rng.random() < error_rates[i]) for i in range(n)] for t in range(T)]
M, R, m = run(n, T, eps, lambda t: table[t], lambda t, g: truths[t], rng)
print(f"random outcomes: best expert {m}, weighted majority {M}, randomized WM expected {R:.1f}")
print(f" bounds: 2(1+eps)m + 2 ln n / eps = {2 * (1 + eps) * m + 2 * math.log(n) / eps:.1f},"
f" (1+eps)m + ln n / eps = {(1 + eps) * m + math.log(n) / eps:.1f}")
M, R, m = run(2, T, eps, lambda t: [0, 1], lambda t, g: 1 - g, rng) # the adversary inverts the prediction
print(f"adversary vs deterministic WM: best expert {m}, weighted majority {M}, randomized WM expected {R:.1f}")random outcomes: best expert 104, weighted majority 89, randomized WM expected 127.9
bounds: 2(1+eps)m + 2 ln n / eps = 274.9, (1+eps)m + ln n / eps = 137.4
adversary vs deterministic WM: best expert 500, weighted majority 1000, randomized WM expected 513.2
On random data the deterministic vote even beats the best expert (89 against 104 mistakes): the experts' errors are independent, and a weighted vote cancels many of them. The randomized version, which follows a single expert's opinion in proportion to the weights, makes more mistakes here, but stays inside its bound. Against the adversary, the deterministic algorithm errs in every one of the 1000 rounds, twice the best expert's 500, exactly the factor-2 barrier, while the randomized version's expectation, 513.2, is within the bound . (In this simulation the adversary reacts to the deterministic prediction only. Against the randomized algorithm an adversary can only choose outcomes in advance, or react to the weights, not to the coin flips.)
In C++
Deterministic weighted majority on the same kind of input, generated with a seeded Mersenne Twister so the run is reproducible.
#include <cmath>
#include <cstdio>
#include <random>
#include <vector>
int main() {
const int n = 10, T = 1000;
const double eps = 0.1;
std::mt19937 rng(1994);
std::vector<double> w(n, 1.0);
std::vector<int> mistakes(n, 0);
int alg = 0;
for (int t = 0; t < T; t++) {
int truth = rng() & 1;
std::vector<int> pred(n);
for (int i = 0; i < n; i++) pred[i] = truth ^ ((rng() >> 8) / 16777216.0 < 0.1 + 0.04 * i);
double W = 0, W1 = 0;
for (int i = 0; i < n; i++) { W += w[i]; if (pred[i]) W1 += w[i]; }
int guess = W1 >= W / 2;
alg += guess != truth;
for (int i = 0; i < n; i++) if (pred[i] != truth) { mistakes[i]++; w[i] *= 1 - eps; }
}
int best = mistakes[0];
for (int i = 1; i < n; i++) if (mistakes[i] < best) best = mistakes[i];
std::printf("best expert %d mistakes, weighted majority %d, bound %.1f\n", best, alg,
2 * (1 + eps) * best + 2 * std::log(n) / eps);
}best expert 79 mistakes, weighted majority 72, bound 219.9
The C++ run draws its own random sequence (the library generator differs from Python's), so the numbers differ from the Python run, but the picture is the same.
Common mistakes
- Confusing the benchmark. The guarantee is relative to the best single expert over the whole sequence, chosen in hindsight. It says nothing about experts who are good on some days and bad on others; a benchmark that may switch experts needs other algorithms (for example "fixed share" variants that never let a weight fall too low).
- Choosing too large or too small. The additive term favours a large , the multiplicative term a small one. With rounds known in advance, balances them (next lesson); without it, a "doubling trick" restarts with a smaller whenever doubles.
- Forgetting the adversary model. The randomized bound assumes the outcome of a round does not depend on the algorithm's coin flip in that round.
Test yourself
With a perfect expert among , how many mistakes can the halving algorithm make?
- At most
- At most 1
Which potential does the weighted majority proof follow?
- The number of mistakes
- The total weight , which shrinks by a factor at each mistake of the algorithm
- The weight of the worst expert
- The entropy of the outcomes
Why can no deterministic algorithm beat the factor 2 in the mistake bound?
- Because experts lie
- With two experts that always disagree, an adversary that knows the deterministic prediction makes every prediction wrong, while one expert is right at least half the time
- Because is too large
- It can, with