Algorithms to Live By · ch.2 · explore/exploit
🎰 Chapter 2 · The latest vs. the greatest

When to try something new

Every choice is a quiet bet between the favorite you already love and the untried thing that might be even better — or a total dud. The right call isn't about taste; it's about how much future you have left to enjoy the payoff.

Chapter 1 was a world of no second chances — see an option, decide forever. This chapter is its mirror image: a world where you can always come back. And that one change flips the whole question. It's no longer "should I commit?" but "should I stick with what I love, or risk something new?"

The surprise is that there's a real answer, and it doesn't depend on how adventurous you feel. It depends on the clock.

1The restaurant on your last night in town

You're hungry. There's the place down the street you already know you love — reliable, delicious, never let you down. And there's the new spot that just opened: it might be your new favorite, or it might be a forgettable mistake. Which do you pick?

Every choice like this is secretly split in two. One part is exploiting — using what you already know to get the best payoff right now. The other is exploring — spending this turn to gather information that could pay off later. The new restaurant is a worse bet tonight (you don't know if it's good) but a potentially great investment in every future dinner.

Here's the punchline that frames the entire chapter: the right balance between the two depends on how much future you have left to enjoy the payoff. This is the explore/exploit tradeoff — and once you see it, you'll see it everywhere.

🍽️
First night in a new city: explore — every meal teaches you something you'll use for years. Last night before you move away: exploit — go back to the one you love. Same person, same restaurants, opposite correct answer. The only thing that changed was the time left.

2A row of slot machines

Picture a row of slot machines. Each one pays out at some fixed rate you don't know — maybe this one wins 30% of the time, that one 70%, but the labels are hidden. You get a fixed number of pulls. Each pull gives you a reward (win or nothing) and a tiny bit of information about that machine. Your job: walk away with as much total reward as you can.

This is the multi-armed bandit — the name comes from a slot machine's lever, the "one-armed bandit," lined up into a row of arms. It's the cleanest possible model of the restaurant problem. Each machine is a dining option; each pull is a meal; the unknown payout rate is how much you'd enjoy it.

Why is it hard? Pull the arm that looks best so far and you exploit — but your read might be wrong, fooled by a lucky streak on a mediocre machine. Pull an untried arm and you explore — but that costs you a turn you could have spent on a known winner. There's no free lunch: every pull is either spent learning or spent earning, never quite both.

Interactive · you pull the arms Hidden win-rates · few pulls = noisy reads
Total pulls
0
Total reward
0
Your best guess
Truly best
hidden

3Seize the interval

This is the single most useful idea in the whole book, and it's almost embarrassingly simple: the answer changes with your time horizon — how many pulls you have left.

Lots of pulls remaining? Explore. Any information you dig up has plenty of time to pay you back. A wrong guess this turn is cheap when there are a thousand turns ahead. Down to your last few pulls? Exploit. There's no time left for a discovery to earn out, so cash in on the best thing you've found. The whole strategy collapses into one sentence:

"Explore when you'll have time to use the resulting knowledge; exploit when you're about to cash out."

It explains a lot of life. A young child is a pure explorer — tasting everything, poking everything — because their whole future is the payoff window. An elder reaching for the familiar isn't closed-minded; they're playing the late game correctly, exploiting a lifetime of accumulated knowledge. Move to a new town and you should try every restaurant; about to leave it, and you should revisit your favorites one last time.

The mistake almost everyone makes is treating the tradeoff as a personality trait — "I'm just not adventurous." It isn't. The same person should explore early and exploit late. If you feel restless trying new things when you're young and content with favorites when you're old, your gut is running the math correctly.

4Win-Stay, Lose-Shift

What's the simplest strategy that beats blind luck? Stick with an arm as long as it keeps paying. The moment it fails you, switch to another. That's Win-Stay, Lose-Shift — and it's about as simple as a rule can get while still being smart.

It does genuinely beat random choice, because it has a crude memory: a recent win is a weak signal that this arm is good, so it leans in. But "crude" is the operative word. It under-explores — one unlucky loss on your best machine and you abandon it. And it's nearly blind to degree: it can't really tell a 90%-arm from a 70%-arm, because both feel like "wins, mostly." A single failure triggers a switch either way.

Think of it as a warm-up. It captures the right instinct — pay attention to outcomes, don't just guess — but it throws away most of the information it collects. The smarter strategies coming up keep that information around and actually do arithmetic with it.

5The Gittins index

Now the heavy machinery. Imagine you could boil each arm down to a single number — call it its "play me" score — that already folds together two things: how good the arm looks based on what you've seen, plus the value of the information you'd gain by pulling it again. Then the optimal move is dead simple: always pull the arm with the highest score. That number is the Gittins index, and under the right assumptions it's provably optimal.

The "right assumptions" are that future rewards are discounted — a win tomorrow is worth a little less than a win today, the way money in a savings account is (geometric discounting). That's a natural fit for a world where you're not certain you'll even be around for the next pull.

The beautiful part is what the index does with uncertainty. An arm you've never tried gets an index higher than its raw odds would suggest — uncertainty itself is worth something, because an untried arm still might be the jackpot, and finding out has value. The index bakes in a kind of optimism: when in doubt, lean toward the thing you don't yet understand.

💎
You don't need to derive the dynamic program behind it (it's gnarly). The takeaway is the shape of the answer: known payoff + a bonus for uncertainty, collapsed into one comparable number. A brand-new arm with a 50/50 prior can outrank a tested arm sitting at a solid 65% — because the new one carries the option value of being great.

6Regret, and being optimistic

The Gittins index is optimal, but it needs a discount rate and a precomputed table — awkward in practice. So let's change how we score ourselves. Define regret: how much worse you did than if a genie had told you the best arm up front and you'd pulled it every single time. Regret is the gap between your reward and the perfect-hindsight reward. A good strategy doesn't need zero regret — that's impossible while you're still learning — it just needs regret that grows slowly (sub-linearly): each pull adds less and less to the running total.

The standout strategy here is the Upper Confidence Bound (UCB). Instead of ranking arms by their observed average, rank them by the top of their confidence interval — their optimistic best-case. An arm you've barely tried has a wide interval, so its upper bound is high and it gets a look. An arm you've tested a hundred times and found mediocre has a tight interval sitting low, so it drops out of contention. It's the same instinct as Gittins, packaged as a rule you can compute on the fly: optimism in the face of uncertainty.

Contrast that with the workhorse of the real world, ε-greedy: most of the time pull the arm that looks best, but every so often (with probability ε) pull a random arm just to keep learning. It's simpler and dumber than UCB — it explores blindly rather than targeting the uncertain arms — but it's trivial to implement and it's absolutely everywhere, from ad servers to reinforcement-learning agents.

Interactive · optimism in the face of uncertainty Error bars shrink ∝ 1/√pulls · UCB picks the highest top
UCB will pull the arm with the highest upper bound.
📈
Watch the bars: untried arms start with sky-high upper bounds (forced exploration), so UCB tries them first. As pulls pile up, the intervals tighten, the optimistic best-cases of the losers fall below the true best, and the choice settles on the genuinely best arm — without anyone ever telling it which one that was.

7Where this shows up

Once you have the bandit lens, you can't stop spotting it:

  • A/B testing. The classic "split traffic 50/50, run for two weeks, ship the winner" is a crude bandit — it spends a fixed, large chunk on exploration even after one variant is clearly losing. Bandit-driven testing shifts traffic toward the winner as it learns, so fewer users get the worse experience.
  • Clinical trials. Here the ethics bite: every patient on the worse treatment is real regret. Adaptive trials use bandit logic to steer more patients toward the arm that's pulling ahead, trading some statistical tidiness for fewer people on the losing treatment.
  • Recommendations and news feeds. Show you the article it's confident you'll like (exploit) or a wildcard to learn your taste (explore)? Every ranking system is running a bandit on your attention.
  • Ad selection. Which ad to serve? The one with the best click-rate so far, or a fresh one that might do better? ε-greedy and UCB variants run billions of these decisions a day.
  • Reinforcement learning. An RL agent's core dilemma is explore/exploit — try a new action to learn the environment, or take the action it already believes is best. ε-greedy is the standard first tool reached for.
🔁
Notice the mirror to Chapter 1. Optimal stopping was the world of "no going back" — commit forever, never revisit. Bandits are the world of "you can always come back" — revisit freely, learn as you go. They're two halves of the same coin: how to act well when the future is uncertain and your time is finite.

8ε-greedy, in a dozen lines

ε-greedy is short enough to read in one breath. The shape is a fold over a stream of pulls: at each step you choose an arm (explore with probability ε, otherwise exploit the best-so-far), pull it against its hidden rate, and fold the reward back into your tallies. Flip between the languages — the structure is identical.

💡
The whole thing is a fold over a stream of pulls, carrying per-arm tallies — exactly the kind of accumulator a Scala/FP brain reaches for. And the only thing that separates ε-greedy from UCB is the choose function: swap "best observed rate, sometimes random" for "highest upper confidence bound" and the same skeleton becomes a smarter algorithm.

9Strategy showdown

Talk is cheap — let's race them. Below, four strategies play the same three-armed bandit thousands of times, and we plot their average cumulative regret: how far behind the perfect-hindsight player each one falls, pull by pull. Flat-ish is good. A line that keeps climbing steeply is a strategy that never stops paying for its ignorance.

Interactive · cumulative regret, Monte Carlo Lower line = less regret = smarter
300
300
Random
Win-Stay/Lose-Shift
ε-greedy (ε=.1)
UCB
🏁
The usual finish: Random's regret grows in a straight line — it never learns, so it pays the same toll every pull. UCB's curve bends flat as it homes in on the best arm. ε-greedy lands in between (it keeps wasting that ε fraction on random pulls forever). WSLS is mediocre — better than random, well short of the optimists.

10Check yourself

3 questions · instant feedback 0 / 3

11What's coming

Bandits were about which thing to do. The next chapter is about what order to put things in — and whether you should bother at all. It turns out your messy desk, a sports league, and a sorted array are the same problem wearing different clothes, and that sometimes the mathematically optimal amount of tidying is exactly none.

🔀
Up next · Chapter 3
Sorting
When is it worth putting things in order at all? Bubble sort vs. merge sort vs. not sorting, why your bookshelf and a sports league are the same problem, and the surprising cases where the optimal amount of sorting is zero.