Sub topic 10
Choosing content: rules, next best action and bandits
This chapter assumes you know what segments and content templates are, and that you can read basic Python. Once a customer has a segment and a template, something must decide which content to show. That step is called decisioning.
Three ways to decide
- Fixed rules map a condition to content. For example, "if the segment is lapsed, show the basket reminder". Rules are easy to explain and audit. They do not learn.
- Next best action (NBA) scores several candidate actions for one customer and picks the best one. Scores usually combine a propensity model with business value and eligibility rules, such as consent or contact limits.
- Bandits pick content adaptively. They send variants, watch the results, and shift traffic toward the winners while still exploring.
| Approach | Learns from results | Explainability | Best when |
|---|---|---|---|
| Rules | No | High | Logic is stable or mandated |
| Next best action | Through retrained models | Medium | Many actions compete for one slot |
| Bandit | Continuously | Medium to low | Variants are similar and results arrive quickly |
Always filter first
Apply eligibility before any scoring. Remove actions the customer has not consented to, products out of stock, and offers they already redeemed. A model that ranks an ineligible action first is a defect, not a clever choice.
Rules as the baseline
Start with rules. They give you a working system and a baseline to beat. Keep them in order, with the most specific first, and always include a default.
Next best action in practice
For each customer, compute score = P(response) × value, then choose the highest score among eligible actions. Using expected value stops a cheap, likely action from always beating a rarer but more valuable one. Add contact-frequency caps so the top action is not repeated daily.
Bandits and epsilon-greedy
An A/B test splits traffic evenly until it ends. A bandit changes the split as it learns. The simplest version is epsilon-greedy. With probability epsilon it explores by choosing a random variant. Otherwise it exploits by choosing the variant with the best click rate so far.
import random
def choose_variant(stats, epsilon=0.1):
"""stats maps variant name to {'sends': int, 'clicks': int}."""
if random.random() < epsilon:
return random.choice(list(stats))
return max(stats, key=lambda v: stats[v]["clicks"] / max(stats[v]["sends"], 1))
stats = {
"discount_banner": {"sends": 200, "clicks": 18},
"new_arrivals": {"sends": 180, "clicks": 25},
}
print(choose_variant(stats))Here discount_banner has a 9.0% click rate and new_arrivals has about 13.9%. About 90% of the time the function returns new_arrivals. The other 10% it picks randomly, so the estimates keep improving. The max(..., 1) guard avoids dividing by zero for a new variant.
Common errors
- No holdout. A bandit optimises clicks, not incremental impact. Keep a control group that gets no personalisation, and measure lift against it.
- Optimising the wrong metric. Clicks can rise while revenue or unsubscribes get worse. Choose a reward that matches the business goal.
- Starting without exploration data. A variant with one send and one click shows a 100% rate. Require a minimum number of sends before trusting a rate.
- Reward delay. If purchases arrive days after sends, the bandit acts on stale data. Use a faster proxy or update on a schedule.
- Unlogged decisions. Record which variant was shown, why, and the probability of choosing it. Without this you cannot audit or analyse later.
Exercise
A new variant has been sent twice and received two clicks. Why can a plain epsilon-greedy bandit over-favour it, and what is one fix?
Show answer
Its observed click rate is 100%, which is based on too little data, so the greedy step keeps choosing it. A fix is to require a minimum number of sends before a variant can win the greedy step. Until then, treat it as an exploration candidate. Another fix is to smooth the rate, for example by adding prior sends and clicks.