Compute a Welch confidence interval for the difference in means between treatment and control

Quick Overview

Implement a two-sided Welch confidence interval for the difference in means between treatment and control using NumPy and SciPy. It tests unpooled standard errors, Welch-Satterthwaite degrees of freedom, t critical values and the variance and sign mistakes that come up in A/B test analysis.

Compute a Welch confidence interval for the difference in means between treatment and control

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: medium

Interview Round: Other

Implement `welch_ci_diff_means(control, treatment, alpha=0.05)`. Return a two-sided `(1 - alpha)` Welch (unequal-variance) confidence interval for the difference in means (treatment minus control) as a `(lower, upper)` tuple. Requirements: - Compute sample variances with `var(ddof=1)`, not `ddof=0`. The standard error is `se = sqrt(s_c2/n_c + s_t2/n_t)`. - Use the Welch-Satterthwaite degrees of freedom, not the pooled `n_c + n_t - 2`. - Use the critical value `t.ppf(1 - alpha/2, df)` (`scipy.stats.t.ppf`). - Compute `diff = treatment.mean() - control.mean()`. - Return `(diff - tcrit*se, diff + tcrit*se)`. Example: ```text control = [10, 12, 9, 11, 13, 8, 10, 12, 11, 9] treatment = [12, 14, 11, 13, 15, 10, 13, 14, 12, 11] alpha = 0.05 Output: (0.5144237786013404, 3.4855762213986594) ``` Both groups have sample variance 2.5 and 10 observations, so `se = sqrt(0.5)`, about 0.7071, and the Welch degrees of freedom come out to exactly 18. The mean difference is 12.5 - 10.5 = 2.0, and the 0.975 quantile of t with 18 degrees of freedom is about 2.1009, so the interval is 2.0 plus or minus 1.4856. ```hint Reuse the variance terms The Welch-Satterthwaite formula is built from the same two per-group quantities, variance divided by size, that go into the standard error. Compute those once. ``` ```hint A sanity check With equal group sizes and equal sample variances, the Welch degrees of freedom should equal `n_c + n_t - 2`. Test your code on that case. ``` ### Constraints & Assumptions - Inputs are 1-D `np.ndarray` arrays. - `numpy` and `scipy` are available. ### What a Strong Answer Covers - Separate sample variances with `ddof=1` and the unpooled standard error. - The Welch-Satterthwaite degrees of freedom, and why they are usually fractional. - The two-sided critical value at `1 - alpha/2` and the treatment-minus-control sign convention. - A check against the example, plus degenerate inputs such as a group with one observation or zero variance. ### Follow-up Questions - When would the pooled-variance (Student's) t interval be preferred over Welch? - How would you adapt this for a difference in proportions instead of means? - What would you change if the metric were heavily skewed, such as revenue per user?

Overview: Implement a two-sided Welch confidence interval for the difference in means between treatment and control using NumPy and SciPy. It tests unpooled standard errors, Welch-Satterthwaite degrees of freedom, t critical values and the variance and sign mistakes that come up in A/B test analysis.

PracHub logo
General
Nov 25, 2026
mediumData ScientistOtherAnalytics & Experimentation
0
0

Implement welch_ci_diff_means(control, treatment, alpha=0.05). Return a two-sided (1 - alpha) Welch (unequal-variance) confidence interval for the difference in means (treatment minus control) as a (lower, upper) tuple.

Requirements:

  • Compute sample variances with var(ddof=1) , not ddof=0 . The standard error is se = sqrt(s_c2/n_c + s_t2/n_t) .
  • Use the Welch-Satterthwaite degrees of freedom, not the pooled n_c + n_t - 2 .
  • Use the critical value t.ppf(1 - alpha/2, df) ( scipy.stats.t.ppf ).
  • Compute diff = treatment.mean() - control.mean() .
  • Return (diff - tcrit*se, diff + tcrit*se) .

Example:

control   = [10, 12, 9, 11, 13, 8, 10, 12, 11, 9]
treatment = [12, 14, 11, 13, 15, 10, 13, 14, 12, 11]
alpha     = 0.05
Output: (0.5144237786013404, 3.4855762213986594)

Both groups have sample variance 2.5 and 10 observations, so se = sqrt(0.5), about 0.7071, and the Welch degrees of freedom come out to exactly 18. The mean difference is 12.5 - 10.5 = 2.0, and the 0.975 quantile of t with 18 degrees of freedom is about 2.1009, so the interval is 2.0 plus or minus 1.4856.

Constraints & Assumptions

  • Inputs are 1-D np.ndarray arrays.
  • numpy and scipy are available.

What a Strong Answer Covers Guidance

  • Separate sample variances with ddof=1 and the unpooled standard error.
  • The Welch-Satterthwaite degrees of freedom, and why they are usually fractional.
  • The two-sided critical value at 1 - alpha/2 and the treatment-minus-control sign convention.
  • A check against the example, plus degenerate inputs such as a group with one observation or zero variance.

Follow-up Questions Guidance

  • When would the pooled-variance (Student's) t interval be preferred over Welch?
  • How would you adapt this for a difference in proportions instead of means?
  • What would you change if the metric were heavily skewed, such as revenue per user?