Plan an A/B test end to end: per-arm sample sizes and duration for a two-proportion test
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `design_experiment(p_control, relative_mde, ratio, daily_visitors, exposure_fraction, alpha=0.05, power=0.8)` in `solution.py`. It plans a two-arm proportion experiment using `statsmodels`. The MDE is relative, the effect size uses the arcsine convention, and the test is two-sided. Group 1 is control and `ratio` = treatment / control.
The procedure is pinned:
1. `p_treatment = p_control * (1 + relative_mde)`
2. `h = proportion_effectsize(p_treatment, p_control)`
3. `n1 = NormalIndPower().solve_power(effect_size=h, nobs1=None, alpha=alpha, power=power, ratio=ratio, alternative='two-sided')`
4. `n_control = math.ceil(n1)`, `n_treatment = math.ceil(n1 * ratio)` (round the product; `ceil(n1) * ratio` gives a different number), `total = n_control + n_treatment`
5. `days = math.ceil(total / (daily_visitors * exposure_fraction))`
Return a dict with exactly the keys `n_control`, `n_treatment`, `total` and `days`, all with `int` values.
Example:
```python
design_experiment(0.10, 0.10, 2.0, 20000, 0.4)
# -> {'n_control': 11059, 'n_treatment': 22117, 'total': 33176, 'days': 5}
```
```hint Which arm is nobs1
Pin down which arm `nobs1` refers to and how `ratio` is defined before you derive the size of the other arm.
```
```hint Rounding order
Try both rounding orders for the treatment arm on the example. Only one reproduces the expected output.
```
### What a Strong Answer Covers
- Derives the treatment rate as a relative lift and computes the arcsine effect size with the specified argument order
- Solves for the control arm with the given ratio and a two-sided alternative, and knows which arm `nobs1` is
- Rounds each arm in the pinned way and explains why it differs from scaling the rounded control size
- Converts the total into days using only exposed traffic, rounding up
- Handles inputs that make the plan undefined, such as a zero lift, a treatment rate above 1 or no exposed traffic
### Follow-up Questions
- How does using `ratio` != 1 change the required total sample size?
- Why use the arcsine (`proportion_effectsize`) convention instead of a raw difference in proportions?
- If the experiment must run in whole weeks to cover weekly seasonality, how would you change the duration calculation?
- How would the plan change if the primary metric were revenue per visitor instead of a conversion rate?
Overview: Build a Python A/B test planner that turns a baseline conversion rate, a relative minimum detectable effect, an allocation ratio and daily traffic into per-arm sample sizes and a run length. It tests power analysis with statsmodels, unequal allocation, the arcsine effect size and careful rounding.