KatInterview

Difference in Proportions Confidence Interval

Arrays & HashingAuto-graded · 19 test cases
MediumAuto-gradedArrays & Hashingstatisticshypothesis-testingconfidence-intervals

Last updated

Tests understanding of statistical inference by computing a confidence interval for the difference between two independent proportions.

You are evaluating the results of a randomized experiment comparing two independent groups. Each group has a certain number of participants, and within each group, a certain number experienced a success event (such as a conversion, click, or other binary outcome).

Your task is to quantify the difference in success rates between the two groups and assess the statistical uncertainty around that difference.

Task

Write a function that takes four parameters:

  • n_control: the total number of participants in the control group
  • success_control: the number of successes observed in the control group
  • n_treatment: the total number of participants in the treatment group
  • success_treatment: the number of successes observed in the treatment group

Your function must return a dictionary containing:

  1. lift: the point estimate of the difference in success rates (treatment rate minus control rate)
  2. standard_error: the standard error of the difference in proportions, computed using the unpooled variance formula
  3. lower_bound: the lower endpoint of the 95% confidence interval for the difference
  4. upper_bound: the upper endpoint of the 95% confidence interval for the difference
  5. is_statistically_significant: a boolean that is True if and only if the 95% confidence interval does not contain zero (indicating statistical significance at the 5% level)

Formulas

Let p_control = success_control / n_control and p_treatment = success_treatment / n_treatment.

  • Point estimate (lift): p_treatment - p_control
  • Standard error (unpooled): sqrt(p_control * (1 - p_control) / n_control + p_treatment * (1 - p_treatment) / n_treatment)
  • 95% Confidence interval: lift ± 1.96 * standard_error
  • Statistical significance: The interval does not contain zero if lower_bound > 0 or upper_bound < 0.

Constraints

  • 1 ≤ n_control, n_treatment ≤ 1,000,000
  • 0 ≤ success_control ≤ n_control
  • 0 ≤ success_treatment ≤ n_treatment
  • Use the critical value z = 1.96 for the 95% confidence level
  • Return all numeric values as floats with full precision (no rounding)

Examples

Example 1

Input: proportion_difference_ci(10000, 500, 10000, 560)

Output: {'lift': 0.005999999999999998, 'standard_error': 0.003168027777655998, 'lower_bound': -0.0002093344442057575, 'upper_bound': 0.012209334444205754, 'is_statistically_significant': False}

Hints

Hint 1

The standard error formula adds the variances of two independent proportions, not the standard errors themselves.

Hint 2

Statistical significance means the confidence interval excludes zero entirely — either both bounds are positive or both are negative.

Hint 3

Use the unpooled standard error, which treats each group's variance separately, rather than assuming a common proportion.