Compute Type II error (beta) at several significance levels for a two-proportion test

Quick Overview

Write a Python function that computes the Type II error rate of a two-sided two-proportion test at several significance levels for a fixed sample size, using statsmodels power tools. It tests power analysis, the arcsine effect size, and how alpha and beta trade off when n is held constant.

Compute Type II error (beta) at several significance levels for a two-proportion test

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `beta_by_alpha(p_control, p_treatment, n_per_group, alphas)` in `solution.py`. For each significance level in `alphas`, compute the power of a two-sided two-proportion test at a fixed sample size and return the Type II error rate `beta = 1 - power`. Requirements: - `effect_size = proportion_effectsize(p_treatment, p_control)` (the arcsine convention). - `power = NormalIndPower().solve_power(effect_size=effect_size, nobs1=n_per_group, alpha=alpha, power=None, ratio=1.0, alternative='two-sided')`. - Store `1 - power` (beta). Storing the power itself is wrong. - Return a dict keyed by the exact alpha floats from `alphas`, unchanged and in the same order, with exactly `len(alphas)` entries. - Values are unrounded floats. Example: ```python beta_by_alpha(0.10, 0.12, 4000, [0.01, 0.05, 0.10]) # -> {0.01: 0.387616..., 0.05: 0.183686..., 0.10: 0.111891...} ``` ```hint Which argument is unknown `solve_power` solves for whichever argument you pass as `None`. Work out which one that is here, and what you need to do with the value it returns. ``` ```hint Sanity check the direction Before trusting your output, ask which way beta should move as alpha grows while the sample size and effect size stay fixed. ``` ### What a Strong Answer Covers - Computes the effect size once with the specified argument order and keeps it fixed across alphas - Calls `solve_power` with power as the unknown, the fixed per-group n, equal allocation and a two-sided alternative - Stores beta unrounded, keyed by the original alpha values in input order - Explains why beta falls as alpha rises at fixed n and effect size - Mentions edge cases such as equal proportions or repeated alpha values ### Follow-up Questions - Why does beta decrease as alpha increases at fixed n? - How would you instead solve for the n that hits a target power? - If you doubled `n_per_group`, how would the beta values change, and would the ordering across alphas still hold?

Overview: Write a Python function that computes the Type II error rate of a two-sided two-proportion test at several significance levels for a fixed sample size, using statsmodels power tools. It tests power analysis, the arcsine effect size, and how alpha and beta trade off when n is held constant.

Nov 24, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement beta_by_alpha(p_control, p_treatment, n_per_group, alphas) in solution.py. For each significance level in alphas, compute the power of a two-sided two-proportion test at a fixed sample size and return the Type II error rate beta = 1 - power.

Requirements:

  • effect_size = proportion_effectsize(p_treatment, p_control) (the arcsine convention).
  • power = NormalIndPower().solve_power(effect_size=effect_size, nobs1=n_per_group, alpha=alpha, power=None, ratio=1.0, alternative='two-sided') .
  • Store 1 - power (beta). Storing the power itself is wrong.
  • Return a dict keyed by the exact alpha floats from alphas , unchanged and in the same order, with exactly len(alphas) entries.
  • Values are unrounded floats.

Example:

beta_by_alpha(0.10, 0.12, 4000, [0.01, 0.05, 0.10])
# -> {0.01: 0.387616..., 0.05: 0.183686..., 0.10: 0.111891...}

What a Strong Answer Covers Guidance

  • Computes the effect size once with the specified argument order and keeps it fixed across alphas
  • Calls solve_power with power as the unknown, the fixed per-group n, equal allocation and a two-sided alternative
  • Stores beta unrounded, keyed by the original alpha values in input order
  • Explains why beta falls as alpha rises at fixed n and effect size
  • Mentions edge cases such as equal proportions or repeated alpha values

Follow-up Questions Guidance

  • Why does beta decrease as alpha increases at fixed n?
  • How would you instead solve for the n that hits a target power?
  • If you doubled n_per_group , how would the beta values change, and would the ordering across alphas still hold?