Find the relative MDE for fixed traffic by inverting the pooled two-proportion sample size formula

Quick Overview

Write a function that inverts the pooled two-proportion sample-size formula with a root finder to get the relative minimum detectable effect for a fixed number of users per group. Tests power analysis, numerical root finding with brentq, and careful handling of conventions and edge cases.

Find the relative MDE for fixed traffic by inverting the pooled two-proportion sample size formula

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `mde_relative_pooled(p_control, n_per_group, alpha=0.05, power=0.8)` in `solution.py`. Given a baseline conversion rate and a fixed number of users per group, it inverts the pooled two-proportion sample-size formula below and returns the relative minimum detectable effect (MDE). Numerically find `p_treatment` in `(p_control, 0.5)` such that `n(p_control, p_treatment) == n_per_group`. ```python p_bar = (p1 + p2) / 2 n(p1, p2) = (norm.ppf(1-alpha/2)*sqrt(2*p_bar*(1-p_bar)) + norm.ppf(power)*sqrt(p1*(1-p1) + p2*(1-p2)))**2 / (p2-p1)**2 ``` Requirements: - Use `scipy.optimize.brentq` with the bracket `(p_control + 1e-12, 0.5)` and `xtol=1e-13`. - Return the relative lift `(p_treatment - p_control) / p_control` as an unrounded float. The absolute difference and `p_treatment` itself are both wrong return values. - Use the pooled convention shown above (no unpooled or arcsine variant), two-sided with `norm.ppf(1-alpha/2)`. - Search upward only (`p_treatment > p_control`). Example: ```python mde_relative_pooled(0.10, 20000) # -> 0.08562086348967116 ``` ```hint What to hand the root finder Look at how the required n behaves as `p2` moves from just above `p_control` up to 0.5. That tells you which function of `p2` crosses zero at the answer and why it crosses only once. ``` ```hint Check your answer Plug your `p_treatment` back into `n(p_control, p_treatment)`. For the example it should come back as 20000 to many decimal places. ``` ### Constraints & Assumptions - `0 < p_control < 0.5`, and `n_per_group` is large enough that a solution exists below 0.5 (a follow-up asks what happens otherwise). - `norm` is `scipy.stats.norm`, and `scipy` is available. - Defaults are `alpha = 0.05` (two-sided) and `power = 0.8`. ### What a Strong Answer Covers - Framing the inversion as a root of required n minus `n_per_group`, using the exact pooled formula. - Why the function changes sign exactly once in the bracket, and why the lower end is offset from `p_control`. - Returning the relative lift, unrounded, in the convention the task fixes. - Behavior at the edges: small `n_per_group`, `p_control` near 0.5, and the solver tolerance. ### Follow-up Questions - How would you extend this to detect downward effects too? - Why might `brentq` fail near the 0.5 endpoint, and how would you guard against it? - How would the answer change under the unpooled or arcsine conventions, and why does the convention need to be fixed in advance?

Overview: Write a function that inverts the pooled two-proportion sample-size formula with a root finder to get the relative minimum detectable effect for a fixed number of users per group. Tests power analysis, numerical root finding with brentq, and careful handling of conventions and edge cases.

Nov 24, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement mde_relative_pooled(p_control, n_per_group, alpha=0.05, power=0.8) in solution.py. Given a baseline conversion rate and a fixed number of users per group, it inverts the pooled two-proportion sample-size formula below and returns the relative minimum detectable effect (MDE). Numerically find p_treatment in (p_control, 0.5) such that n(p_control, p_treatment) == n_per_group.

p_bar = (p1 + p2) / 2
n(p1, p2) = (norm.ppf(1-alpha/2)*sqrt(2*p_bar*(1-p_bar))
             + norm.ppf(power)*sqrt(p1*(1-p1) + p2*(1-p2)))**2 / (p2-p1)**2

Requirements:

  • Use scipy.optimize.brentq with the bracket (p_control + 1e-12, 0.5) and xtol=1e-13 .
  • Return the relative lift (p_treatment - p_control) / p_control as an unrounded float. The absolute difference and p_treatment itself are both wrong return values.
  • Use the pooled convention shown above (no unpooled or arcsine variant), two-sided with norm.ppf(1-alpha/2) .
  • Search upward only ( p_treatment > p_control ).

Example:

mde_relative_pooled(0.10, 20000)  # -> 0.08562086348967116

Constraints & Assumptions

  • 0 < p_control < 0.5 , and n_per_group is large enough that a solution exists below 0.5 (a follow-up asks what happens otherwise).
  • norm is scipy.stats.norm , and scipy is available.
  • Defaults are alpha = 0.05 (two-sided) and power = 0.8 .

What a Strong Answer Covers Guidance

  • Framing the inversion as a root of required n minus n_per_group , using the exact pooled formula.
  • Why the function changes sign exactly once in the bracket, and why the lower end is offset from p_control .
  • Returning the relative lift, unrounded, in the convention the task fixes.
  • Behavior at the edges: small n_per_group , p_control near 0.5, and the solver tolerance.

Follow-up Questions Guidance

  • How would you extend this to detect downward effects too?
  • Why might brentq fail near the 0.5 endpoint, and how would you guard against it?
  • How would the answer change under the unpooled or arcsine conventions, and why does the convention need to be fixed in advance?