Compute the power of a two-sided two-proportion test with statsmodels
Quick Overview
Write a Python function that returns the power of a two-sided two-proportion test for given conversion rates and group size, using the statsmodels arcsine effect size. It tests whether you can apply a pinned power-analysis convention exactly and explain what the resulting power means for an A/B test.
Compute the power of a two-sided two-proportion test with statsmodels
Role: Data Scientist
Category: Analytics & Experimentation
Difficulty: easy
Interview Round: Other
Implement `power_two_proportions` in `solution.py`. Given a control conversion rate, a treatment conversion rate and the number of users in each group, return the power (1 - beta) of a two-sided two-proportion test as a float between 0 and 1.
```python
def power_two_proportions(p_control: float, p_treatment: float,
n_per_group: int, alpha: float = 0.05) -> float:
...
```
The function must follow these rules:
- Compute `effect_size = proportion_effectsize(p_treatment, p_control)`. This is the arcsine effect size (Cohen's h), not a normal-approximation formula on the raw difference.
- Get the power from `NormalIndPower().solve_power(effect_size=effect_size, nobs1=n_per_group, alpha=alpha, power=None, ratio=1.0, alternative='two-sided')`.
- Return the value unrounded.
Example:
```python
power_two_proportions(0.10, 0.12, 2000)
# 0.5252812352177137
```
```hint Sanity check
With two equal groups of n users, the test statistic is centered near $h\sqrt{n/2}$. Compare that number with the two-sided critical value to predict roughly where the power should land.
```
### What a Strong Answer Covers
- Computes Cohen's h with treatment as the first argument and passes it to the solver with `power=None`.
- Treats `n_per_group` as the size of each arm, not the total.
- Returns a plain float without rounding.
- Explains what the number means for the experiment and why it sits near 0.5 in the example.
- Names the inputs that need care, such as equal proportions and a drop instead of a lift.
### Follow-up Questions
- How many users per group would the example test need to reach 80% power?
- Does the power change if the treatment rate falls from 10% to 8% instead of rising to 12%?
- Why does a textbook normal-approximation power formula give a slightly different number for the same inputs?
Overview: Write a Python function that returns the power of a two-sided two-proportion test for given conversion rates and group size, using the statsmodels arcsine effect size. It tests whether you can apply a pinned power-analysis convention exactly and explain what the resulting power means for an A/B test.
Implement power_two_proportions in solution.py. Given a control conversion rate, a treatment conversion rate and the number of users in each group, return the power (1 - beta) of a two-sided two-proportion test as a float between 0 and 1.
Compute
effect_size = proportion_effectsize(p_treatment, p_control)
. This is the arcsine effect size (Cohen's h), not a normal-approximation formula on the raw difference.
Get the power from
NormalIndPower().solve_power(effect_size=effect_size, nobs1=n_per_group, alpha=alpha, power=None, ratio=1.0, alternative='two-sided')
.