Assumptions:- Each day t we observe N_t users ~ Normal(mean, var) rounded to int, independent across days.- Each user has context x sampled from given context distribution.- For context x, model A/B have known conversion probabilities pA(x), pB(x).- Reward r(conversion, x) is given (can be constant per conversion or context-dependent).- Contextual bandit uses Thompson Sampling with Beta priors per context bucket (discretize continuous contexts).- Horizon T days, many Monte Carlo simulation runs M to estimate expectation and CI.Pseudocode (high-level):python
# Inputs: mean, var, context_dist, pA(x), pB(x), reward(x, outcome),
# T, M, context_buckets, prior_alpha=1, prior_beta=1
def simulate_one_run(strategy):
total_reward = 0
for t in range(1, T+1):
N = max(0, int(normal(mean, var)))
for i in range(N):
x = sample(context_dist)
bucket = discretize(x)
if strategy == "A": prob = pA(x)
elif strategy == "B": prob = pB(x)
else: # contextual bandit (Thompson Sampling)
# draw theta_A ~ Beta(alphaA[b], betaA[b]), theta_B similarly
thetaA = beta_sample(alphaA[bucket], betaA[bucket])
thetaB = beta_sample(alphaB[bucket], betaB[bucket])
choose = "A" if thetaA*expected_reward_A(bucket) > thetaB*expected_reward_B(bucket) else "B"
prob = pA(x) if choose=="A" else pB(x)
outcome = bernoulli(prob)
total_reward += reward(x, outcome)
if strategy == "bandit":
# update posterior for chosen arm in bucket
if outcome==1: increment chosen alpha else increment chosen beta
return total_reward
# Monte Carlo
results = {s: [] for s in ["A","B","bandit"]}
for m in range(M):
for s in ["A","B","bandit"]:
results[s].append(simulate_one_run(s))
# Compute expected regret relative to oracle (choose per-context optimal arm each user)
oracle_rewards = simulate_oracle_expected_reward_over_runs(M) # same procedure but always pick max p*reward
expected_regret = {s: mean(oracle_rewards - results[s])}
confidence_intervals = compute_CI(expected_regret)
Key points / choices:- Discretization of context trades bias vs variance; for high-dim contexts use learned contextual models (e.g., logistic bandit).- Use daily traffic sampling to capture variability and compute distribution of cumulative regret.- Use sufficiently large M (e.g., 1k) and T to stabilize estimates.How to use results to guide rollout:- Compare expected cumulative regret and its CI across strategies. If always-A (or B) has lower regret and narrow CI, prefer immediate rollout.- If bandit has slightly higher expected regret short-term but lower long-term regret (crossing curves), prefer bandit for phased rollout where learning is acceptable.- Use Value of Perfect Information: compute expected loss from choosing wrong model; if loss > cost of exploration, run bandit in production for a fixed fraction of traffic.- Report metrics: mean regret, median, 90% CI, probability bandit outperforms best static, and time-to-domination (day when bandit surpasses static).Edge considerations:- Nonstationarity: repeat simulation with drift scenarios.- Delayed rewards: model conversion delay in simulation and update posteriors only when outcomes observed.