Compare Pooled CTR and Mean Per-User CTR by Variant in pandas
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `naive_vs_pooled_ctr(df)` for a DataFrame with one row per user and the columns `variant`, `impressions` and `clicks`. For each `variant`, compute two click-through rates:
- `pooled_ctr` = `sum(clicks) / sum(impressions)`
- `mean_user_ctr` = the mean over users of `clicks / impressions`
Requirements:
- Return a DataFrame with columns `['variant', 'pooled_ctr', 'mean_user_ctr']`, one row per variant.
- Sort by `variant` ascending and reset the index.
- Both metric columns are float64.
For example:
```python
import pandas as pd
df = pd.DataFrame({
"variant": ["control", "control", "treatment", "treatment"],
"impressions": [10, 100, 20, 40],
"clicks": [1, 5, 3, 6],
})
naive_vs_pooled_ctr(df)
# variant pooled_ctr mean_user_ctr
# 0 control 0.054545 0.075
# 1 treatment 0.150000 0.150
```
In control, user A has 1 click over 10 impressions and user B has 5 over 100. The pooled CTR is (1 + 5) / (10 + 100) = 0.0545..., and the mean user CTR is the mean of 0.10 and 0.05, which is 0.075. In treatment both users have a rate of 0.15, so the two metrics agree.
```hint Order of operations
One metric divides after summing and the other divides before averaging. Decide which per-user column you need before you group.
```
```hint Keeping rows aligned
Compute both metrics from the same grouping so each variant's two numbers end up on the same row.
```
### Constraints & Assumptions
- One row per user, so each user belongs to exactly one variant.
- `impressions` is greater than 0 on every row, so every per-user ratio is defined.
- `clicks` and `impressions` are integer counts.
### What a Strong Answer Covers
- Computing the pooled rate as a ratio of sums and the user mean as a mean of per-user ratios, within each variant.
- Meeting the output contract: column order, one row per variant, sorted by variant, a fresh index, float64 metrics.
- Explaining why the two numbers diverge in terms of how each one weights users.
- When the two metrics coincide, and what a gap between them says about the data.
### Follow-up Questions
- Which metric should drive the A/B decision, and why?
- How would you compute a valid confidence interval for the difference in pooled CTR between variants?
- In one test the pooled CTR goes up while the mean user CTR goes down. What could cause that, and how would you investigate?
Overview: Write a pandas function that reports, for each experiment variant, both the pooled click-through rate and the average of per-user click-through rates. It tests groupby aggregation order, how each metric weights users, and why a ratio metric needs a user-level variance estimate.