5.3.14MLOps & Deployment

A - B testing for models

2,131 words10 min readdifficulty · medium

WHY do we even A/B test models?

The core danger we defend against: seeing a difference that is just random luck. If B looks 2% better on 100 users, that could easily be noise. Statistics tells us how many users we need and how confident we can be.

WHAT are the pieces?

Figure — A - B testing for models

HOW do we decide? — Derive the test from scratch

Suppose the metric is a conversion rate (each user either converts or not → a Bernoulli trial). Group A has nAn_A users with sample conversion rate p^A\hat p_A; group B has nBn_B, p^B\hat p_B.

Step 1 — Model one user. Each user in group A converts with unknown probability pAp_A. A single conversion XiBernoulli(pA)X_i \sim \text{Bernoulli}(p_A) has mean pAp_A and variance pA(1pA)p_A(1-p_A).

Why this step? Because our raw data are yes/no outcomes; Bernoulli is the honest model of a coin-flip event.

Step 2 — Average over users. The sample rate is p^A=1nAXi\hat p_A = \frac1{n_A}\sum X_i. By linearity, E[p^A]=pA\mathbb E[\hat p_A] = p_A. Because the users are independent, Var(p^A)=pA(1pA)nA.\operatorname{Var}(\hat p_A) = \frac{p_A(1-p_A)}{n_A}.

Why this step? Variance of a sum of independent variables adds, and dividing by nAn_A scales variance by 1/nA21/n_A^2 — this is why more users shrink noise.

Step 3 — Difference of two groups. We care about p^Bp^A\hat p_B - \hat p_A. For independent groups variances add: Var(p^Bp^A)=pA(1pA)nA+pB(1pB)nB.\operatorname{Var}(\hat p_B - \hat p_A) = \frac{p_A(1-p_A)}{n_A} + \frac{p_B(1-p_B)}{n_B}.

Step 4 — Standardize (Central Limit Theorem). For large nn, p^Bp^A\hat p_B - \hat p_A is approximately Normal. Under H0H_0 the true difference is 00, so the test statistic is

Why pooled? Under the null hypothesis they have the same pp, so our best estimate of that shared pp uses all the data. This gives the most accurate standard error for the "no difference" world.

Step 5 — Decide. Compute the two-sided p-value =2(1Φ(z))= 2\,(1-\Phi(|z|)). If p-value<αp\text{-value} < \alpha, reject H0H_0 → B is significantly different. Combine with a confidence interval on the lift to judge practical significance.

Deriving the sample size (WHY the experiment takes so long)

To detect a lift δ=pBpA\delta = p_B - p_A (the MDE) with significance α\alpha and power 1β1-\beta, we need the signal δ\delta to stand out above the noise. The classic per-group formula:

Read it as intuition: nn grows when you demand more confidence (zz terms up), when the metric is noisier (variance up), and explodes as δ0\delta \to 0 (tiny effects need huge samples — the 1/δ21/\delta^2).

Worked examples

Common mistakes (Steel-manned)

Recall Feynman: explain to a 12-year-old

Imagine two lemonade stands on the same street. Stand A uses Grandma's recipe, Stand B uses your new recipe. You send random kids to each stand and count how many come back to buy again. If way more come back to B — but only after lots of kids visited both — you can trust the new recipe is really tastier and not just that a few thirsty kids happened to pick B. The "lots of kids" part is the math: small crowds can fool you by luck, big crowds tell the truth.

Flashcards

What is the control group in a model A/B test?
The currently-deployed baseline model (A), against which the new candidate (B) is compared.
Why randomize by user rather than by request?
To give each user a consistent experience and keep observations independent; per-request splitting mixes arms for one user and correlates outcomes.
Why use the pooled rate p^\hat p in the two-proportion z-test?
Under H0H_0 both groups share the same true conversion rate, so pooling all data gives the best estimate of the common pp for the standard error.
State the two-proportion z statistic.
z=p^Bp^Ap^(1p^)(1/nA+1/nB)z=\dfrac{\hat p_B-\hat p_A}{\sqrt{\hat p(1-\hat p)(1/n_A+1/n_B)}} with p^\hat p the pooled rate.
How does required sample size scale with the minimum detectable effect δ\delta?
As 1/δ21/\delta^2 — halving the effect you want to detect roughly quadruples the needed sample.
What is statistical power?
1β1-\beta, the probability of correctly detecting a true effect of the specified size; usually targeted at 0.80.
Why is 'peeking and stopping early' a problem?
Repeated significance checks each add a false-positive chance, inflating the overall Type-I error above α\alpha; use fixed samples or sequential methods.
What are guardrail metrics?
Secondary metrics (latency, error rate, revenue) that must not regress even if the primary OEC improves.
Significant but tiny lift — ship or not?
Not automatically; check practical significance via the effect size / confidence interval and guardrails, since significance ≠ importance.
What is an A/A test used for?
A sanity check that splits identical traffic to the same model, validating that the experiment pipeline yields no false differences.

Connections

Concept Map

insufficient for

splits traffic between

splits traffic between

randomizes on

measures

provides

guards against

controlled by

modeled as

averaged gives

difference standardized via

yields

compared to

Offline metrics frozen history

A B Testing

Control A baseline

Treatment B candidate

Randomization unit user

OEC business metric

Causal evidence B improves metric

Random luck noise

Statistics H0 alpha power

Bernoulli conversion per user

Sample rate p-hat with variance

Central Limit Theorem

Test statistic vs H0

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, A/B testing ka matlab hai — apna purana model (A, yani control) aur naya model (B, yani treatment) dono ko live traffic pe chalao, lekin users ko randomly baant do. Fir dekho ki B se business metric (jaise conversion rate ya click) sach mein badha ya sirf luck tha. Offline test set pe AUC accha hona kaafi nahi hota, kyunki real duniya me users alag behave karte hain aur jo metric paisa deta hai wo aksar training loss se alag hota hai.

Core statistics simple hai: har user ek coin-flip jaisa hai (convert hua ya nahi = Bernoulli). Jitne zyada users, utna noise kam hota hai — variance p(1p)/np(1-p)/n hoti hai, isliye nn badhne pe error shrink hota hai. Do groups ka difference nikaal ke usko standard error se divide karo, wo z statistic ban jaata hai. Agar p-value <0.05< 0.05, tab bolo ki B genuinely alag hai.

Ek important cheez: sample size pehle hi calculate karo, kyunki chhota effect (δ\delta) detect karne ke liye 1/δ21/\delta^2 ke hisaab se bahut saare users chahiye. Aur peeking mat karo — roz roz check karke jaise hi p<0.05 dikhe stop kar dena galti hai, kyunki har baar dekhne se false-positive chance badhta hai. Randomization hamesha user ke basis pe karo, request pe nahi, warna ek hi user A aur B dono me ghusega.

Aur haan, sirf significant hona kaafi nahi — guardrail metrics (latency, error rate, revenue) bhi check karo. Agar conversion 3% badha par latency 80ms se 300ms ho gayi, to B ship mat karo. Yaad rakho: statistical significance ka matlab "sach me change hua" hai, "important change hua" nahi.

Go deeper — visual, from zero

Test yourself — MLOps & Deployment

Connections