Sample Size per Group for a Two-Sided Two-Proportion Test (Unpooled Variance)
Quick Overview
Implement the per-group sample size for a two-sided two-proportion test using the unpooled normal-approximation formula, rounded up to an integer. It tests the two-sided critical value, the power quantile, the separate-variance term, and how this answer compares with the pooled formula.
Sample Size per Group for a Two-Sided Two-Proportion Test (Unpooled Variance)
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: medium
Interview Round: Other
Implement `sample_size_unpooled(p_control: float, p_treatment: float, alpha: float = 0.05, power: float = 0.8) -> int` in `solution.py`. Return the per-group sample size for a two-sided two-proportion test using the unpooled normal-approximation formula.
Requirements:
- `z_alpha = norm.ppf(1 - alpha/2)` (two-sided, not `1 - alpha`) and `z_beta = norm.ppf(power)`.
- `n = (z_alpha + z_beta)**2 * (p_control*(1-p_control) + p_treatment*(1-p_treatment)) / (p_treatment - p_control)**2`
- Unpooled variance only: no `p_bar` or `2*p_bar*(1-p_bar)` term.
- Return `math.ceil(n)` as a Python `int` (per group).
- Use `norm` from `scipy.stats` and `math`.
For example:
```python
sample_size_unpooled(0.10, 0.12) # 3839
```
With a 10% control rate, a 12% treatment rate and the default alpha and power, the formula gives about 3838.10, which rounds up to 3839 users in each group.
```hint One variance for both cases
Here a single variance expression sets both the rejection threshold and the power. Work out what that means for the order in which you add the z-values and square them.
```
```hint Rounding and types
Round up once, at the very end, and make sure the function returns a plain Python int.
```
### Constraints & Assumptions
- `p_treatment != p_control`, and both are proportions between 0 and 1.
- `alpha` and `power` are probabilities strictly between 0 and 1, with defaults 0.05 and 0.8.
- Both groups have the same size, and the function returns the size of one group.
### What a Strong Answer Covers
- The two-sided critical value and the power quantile, computed with `norm.ppf`.
- Adding the z-values before squaring, and using only the unpooled variance sum.
- Rounding up and returning a Python int for one group.
- Where the formula comes from, and how its answer compares with the pooled version.
### Follow-up Questions
- When would you use the pooled variance form instead?
- How would you extend this for a one-sided test?
- The control rate you plan with is itself an estimate. How would you account for uncertainty in it?
Overview: Implement the per-group sample size for a two-sided two-proportion test using the unpooled normal-approximation formula, rounded up to an integer. It tests the two-sided critical value, the power quantile, the separate-variance term, and how this answer compares with the pooled formula.
Implement sample_size_unpooled(p_control: float, p_treatment: float, alpha: float = 0.05, power: float = 0.8) -> int in solution.py. Return the per-group sample size for a two-sided two-proportion test using the unpooled normal-approximation formula.
Requirements:
z_alpha = norm.ppf(1 - alpha/2)
(two-sided, not
1 - alpha
) and
z_beta = norm.ppf(power)
.
Unpooled variance only: no
p_bar
or
2*p_bar*(1-p_bar)
term.
Return
math.ceil(n)
as a Python
int
(per group).
Use
norm
from
scipy.stats
and
math
.
For example:
sample_size_unpooled(0.10, 0.12) # 3839
With a 10% control rate, a 12% treatment rate and the default alpha and power, the formula gives about 3838.10, which rounds up to 3839 users in each group.
Constraints & Assumptions
p_treatment != p_control
, and both are proportions between 0 and 1.
alpha
and
power
are probabilities strictly between 0 and 1, with defaults 0.05 and 0.8.
Both groups have the same size, and the function returns the size of one group.
What a Strong Answer Covers Guidance
The two-sided critical value and the power quantile, computed with
norm.ppf
.
Adding the z-values before squaring, and using only the unpooled variance sum.
Rounding up and returning a Python int for one group.
Where the formula comes from, and how its answer compares with the pooled version.
Follow-up Questions Guidance
When would you use the pooled variance form instead?
How would you extend this for a one-sided test?
The control rate you plan with is itself an estimate. How would you account for uncertainty in it?