Sub topic 10
Balancing exploration and exploitation
This chapter assumes you have read the earlier chapters on signals, engagement prediction, and score combination. You should be comfortable with predicted probabilities between 0 and 1, and with short Python functions.
The problem: a ranker only learns from what it shows
A feed ranker learns from user reactions. A user can only react to a post that was shown. If the system always shows the post with the highest predicted score, it collects data only about posts it already believes are good. Its beliefs about everything else never get tested.
This is the exploration versus exploitation trade-off.
- Exploitation means showing the post the model currently expects to perform best. This gives the best expected result right now.
- Exploration means showing something the model is unsure about, to learn how it performs. This costs some short-term quality and can pay off later.
The previous chapter on cold start described why new posts and new users have little data. Exploration is the mechanism that fixes that. Without it, a new post with no history gets a low or uncertain score, is never shown, and so never collects the history that would raise the score.
Why pure exploitation fails
Three failure modes appear when a feed only exploits.
- Stuck estimates. Suppose a new creator's post has a predicted like probability of 0.02 because the model has no data. If the post is never shown, the estimate stays at 0.02 even if the true rate is 0.30.
- Stale models. Models are trained on logged data. If the logs contain only posts the old ranker chose, the next model learns the old ranker's preferences. This is a form of selection bias.
- Narrowing. A user who liked two cooking videos is shown more cooking videos. The system never learns whether they would also enjoy woodworking, so their feed narrows. The next chapter covers this feedback effect in depth.
Epsilon-greedy: the simplest approach
The simplest exploration rule is epsilon-greedy. Pick a small number epsilon, such as 0.1. For each feed slot, with probability epsilon show a post from an exploration pool. Otherwise show the top-ranked post.
import random
def pick_slot(ranked_post, explore_pool, epsilon=0.1, rng=random):
"""With probability epsilon, show a post from the exploration pool instead."""
if explore_pool and rng.random() < epsilon:
return rng.choice(explore_pool)
return ranked_postPoints to notice in this function:
rng.random()returns a number from 0 up to but not including 1. Withepsilon=0.1, about 10% of calls take the exploration branch.- The
explore_pool andcheck handles an empty pool. Without it,rng.choice([])would raise anIndexError. - Passing
rngas a parameter lets you test the function with a seeded generator and get repeatable results.
You can check the behavior with a quick simulation:
import random
def pick_slot(ranked_post, explore_pool, epsilon=0.1, rng=random):
if explore_pool and rng.random() < epsilon:
return rng.choice(explore_pool)
return ranked_post
rng = random.Random(42)
counts = {"top": 0, "explore": 0}
for _ in range(10_000):
choice = pick_slot("top", ["new1", "new2", "new3"], epsilon=0.1, rng=rng)
counts["top" if choice == "top" else "explore"] += 1
print(counts)The explore count will be close to 1,000 out of 10,000.
Limits of epsilon-greedy
Epsilon-greedy is easy to build and explain, but it is wasteful in two ways.
- It explores uniformly. A post that has already been shown 10,000 times and performed badly is as likely to be picked as a post that has never been shown.
- It never stops. After the system is confident about a post, 10% of slots still go to random picks.
Real systems usually improve on this in one of two ways.
Using uncertainty to guide exploration
A better idea is to explore where the model is least sure. Two common methods follow this idea.
Upper confidence bound
An upper confidence bound (UCB) approach adds a bonus to a post's score that is larger when there is less data about it. A simple form is:
ucb_score = estimated_value + c * sqrt(1 / (impressions + 1))An impression is one time a post is shown to a user. A post with 5 impressions gets a big bonus. A post with 50,000 impressions gets almost none. As data accumulates, the bonus shrinks and the estimate dominates. The constant c controls how strongly the system favors the unknown.
import math
def ucb_score(estimated_value, impressions, c=0.5):
return estimated_value + c * math.sqrt(1 / (impressions + 1))
print(round(ucb_score(0.10, 4), 3)) # new post, few impressions
print(round(ucb_score(0.12, 50_000), 3)) # established postThe new post scores about 0.324, which beats the established post at about 0.122, even though its estimated value is lower. That is the intended effect: the system gives the new post a chance.
Thompson sampling
Thompson sampling keeps a probability distribution over each post's true engagement rate, draws one random sample from each distribution, and shows the post with the highest sample. A post with little data has a wide distribution, so it sometimes draws a high value and gets shown. A post with lots of data has a narrow distribution close to its true rate.
For a like rate, a Beta distribution is a standard choice. It is defined by a count of successes (likes) and failures (impressions without a like).
import random
def thompson_pick(stats, rng=random):
"""stats maps post id to (likes, impressions). Return the id to show."""
best_id, best_draw = None, -1.0
for post_id, (likes, impressions) in stats.items():
misses = impressions - likes
draw = rng.betavariate(likes + 1, misses + 1)
if draw > best_draw:
best_id, best_draw = post_id, draw
return best_id
stats = {"old": (300, 1000), "new": (1, 5)}
rng = random.Random(7)
shown = [thompson_pick(stats, rng) for _ in range(1000)]
print(shown.count("new"))The + 1 on both parameters starts each post with a flat prior, which means no initial opinion. The "new" post has a wide distribution, so it wins a meaningful share of draws even though the "old" post has a higher observed rate of 30%.
Where exploration happens in the pipeline
Exploration does not have to happen at the final slot. It can enter at several stages, and the choice affects cost and risk.
| Stage | How exploration is added | Risk |
|---|---|---|
| Candidate generation | A quota of candidates comes from new or under-shown content | Low, because ranking and filters still apply |
| Scoring | An uncertainty bonus such as UCB is added to scores | Medium, because it changes the ordering directly |
| Slot selection | Epsilon-greedy replaces some slots | Higher, because it bypasses ranking for those slots |
Exploration should come after integrity filtering. A candidate that fails a policy check must never be shown just because the system wants data about it. The usual design is to build the exploration pool from posts that have already passed the filters, then let the exploration rule choose among them.
Practical rules for new content
Platforms commonly give new posts a limited test audience. A typical pattern looks like this:
- Show the post to a small sample of users, chosen because their past behavior suggests relevance.
- Measure engagement and negative signals, such as hides or reports, in that sample.
- If results are strong relative to similar posts, widen the audience. If they are weak, stop.
This resembles a sequence of small bets. Because the first sample is small, the measured rate is noisy. A post that gets 3 likes from 10 impressions has an observed rate of 30%, but the true rate could be much lower or higher. Using a confidence-aware method such as UCB or Thompson sampling avoids over-reacting to small samples.
Costs, edge cases, and gotchas
- Exploration costs engagement. Each exploratory slot has a lower expected value than the top-ranked post. Teams usually measure this cost in an experiment and cap the exploration rate.
- Exploration must be logged. To train unbiased models later, the system should record the probability with which each post was chosen. This allows corrections such as inverse propensity weighting. Without it, exploratory data cannot be separated from ranked data.
- Exploration is not uniform across users. A new user has little data, so more exploration helps. A long-time user has a clearer profile, so aggressive exploration may feel random to them.
- Newness is not the only axis. Exploration can also test new topics for an existing user, not only new posts.
- Non-stationary tastes. User interests and trends change. An estimate that was accurate last month may be wrong now. Continuing some exploration is how the system notices the change.
- Gaming. If creators learn that new posts get a test audience, some may post many low-effort items to farm exposure. Rate limits per author and the integrity filters from earlier chapters limit this.
How this connects to the next stage
Exploration reduces one cause of narrowing: the system learning only from what it already shows. It does not remove feedback loops entirely. Early engagement on a post still shapes how widely it is shown afterward. The next chapter examines how those loops form and amplify.
Exercise
A new post has 4 impressions and an estimated like rate of 0.10. An established post has 50,000 impressions and an estimated like rate of 0.12. Using ucb_score with c=0.5, which post scores higher, and why does that help the system?
Show answer
The new post scores about 0.10 + 0.5 sqrt(1/5) ≈ 0.324. The established post scores about 0.12 + 0.5 sqrt(1/50001) ≈ 0.122. The new post wins because its small impression count gives it a large uncertainty bonus. This gets it shown, which produces data that moves its estimate toward its true rate.
Exercise
Name two weaknesses of epsilon-greedy exploration, and name one method that addresses them.
Show answer
It explores uniformly, so it wastes slots on posts already known to perform badly, and it keeps exploring at the same rate even after the system is confident. Upper confidence bound and Thompson sampling both direct exploration toward posts with the most uncertainty, and the effect shrinks as data accumulates.
Exercise
Why should exploration candidates pass integrity filters before entering the exploration pool, and why should the system log the probability of choosing each exploratory post?
Show answer
Filters protect users from policy-violating content, and exploration is only meant to collect data on acceptable posts. Logging the selection probability lets later training correct for the fact that exploratory posts were chosen differently from ranked posts, for example with inverse propensity weighting. Without it, the logged data is biased.