Difference in Proportions Confidence Interval
Arrays & HashingAuto-graded · 19 test casesLast 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 groupsuccess_control: the number of successes observed in the control groupn_treatment: the total number of participants in the treatment groupsuccess_treatment: the number of successes observed in the treatment group
Your function must return a dictionary containing:
lift: the point estimate of the difference in success rates (treatment rate minus control rate)standard_error: the standard error of the difference in proportions, computed using the unpooled variance formulalower_bound: the lower endpoint of the 95% confidence interval for the differenceupper_bound: the upper endpoint of the 95% confidence interval for the differenceis_statistically_significant: a boolean that isTrueif 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 > 0orupper_bound < 0.
Constraints
1 ≤ n_control, n_treatment ≤ 1,000,0000 ≤ success_control ≤ n_control0 ≤ success_treatment ≤ n_treatment- Use the critical value
z = 1.96for 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem asks us to perform statistical inference on the difference between two independent proportions. This is a standard technique in A/B testing and experimental analysis.
Step 1: Compute the success rates
For each group, divide the number of successes by the total number of participants to get the observed proportion:
p_control = success_control / n_controlp_treatment = success_treatment / n_treatment
Step 2: Calculate the point estimate (lift)
The lift is simply the difference between the treatment and control success rates:
lift = p_treatment - p_control
This tells us the observed effect size, but says nothing about uncertainty.
Step 3: Compute the standard error
Because the two groups are independent, their variances add. The variance of a proportion p estimated from n trials is p(1-p)/n. Therefore, the variance of the difference is:
Var(p_treatment - p_control) = p_treatment(1 - p_treatment)/n_treatment + p_control(1 - p_control)/n_control
The standard error is the square root of this variance. We use the unpooled formula, treating each group's variance separately, which is appropriate for constructing a confidence interval.
Step 4: Build the 95% confidence interval
Using the normal approximation, a 95% confidence interval is constructed as:
lift ± z * standard_error
where z = 1.96 is the critical value for a two-tailed test at the 5% significance level. This gives us:
lower_bound = lift - 1.96 * standard_errorupper_bound = lift + 1.96 * standard_error
Step 5: Assess statistical significance
The result is statistically significant at the 5% level if the confidence interval does not include zero. This happens when either both bounds are positive or both are negative:
is_statistically_significant = (lower_bound > 0) or (upper_bound < 0)
If the interval contains zero, we cannot rule out the possibility that there is no true difference between the groups.
Why this works
The normal approximation to the binomial distribution is valid when the number of successes and failures in each group is reasonably large (commonly, at least 5–10 each). Under this approximation, the difference in proportions is normally distributed with mean equal to the true difference and variance as computed above. The 95% confidence interval then has approximately 95% coverage in repeated sampling.
Complexity
- Time complexity: O(1) — a fixed number of arithmetic operations
- Space complexity: O(1) — a constant amount of storage
Solution
import math
def proportion_difference_ci(n_control, success_control, n_treatment, success_treatment):
p_control = success_control / n_control
p_treatment = success_treatment / n_treatment
lift = p_treatment - p_control
standard_error = math.sqrt(
p_control * (1 - p_control) / n_control + p_treatment * (1 - p_treatment) / n_treatment
)
margin_of_error = 1.96 * standard_error
lower_bound = lift - margin_of_error
upper_bound = lift + margin_of_error
return {
"lift": lift,
"standard_error": standard_error,
"lower_bound": lower_bound,
"upper_bound": upper_bound,
"is_statistically_significant": lower_bound > 0 or upper_bound < 0,
}