before and after hold the weekly spend of the same 40 customers before and after a loyalty-tier change, in the same customer order. What does this ttest_ind call compute, and what should the analyst use instead?
from scipy import stats
# row i of both arrays is the same customer
before = df["spend_before"].to_numpy() # 40 values
after = df["spend_after"].to_numpy() # 40 values
res = stats.ttest_ind(before, after)
print(res.statistic, res.pvalue)Show the answer and explanation
Correct answer: D, A pooled-variance Student t-test, since equal_var=True is the default, treating the 80 values as independent (df = 78); use stats.ttest_rel(before, after).
Explanation Statistical Tests
scipy.stats.ttest_ind(a, b) is the independent two-sample t-test, and its default is equal_var=True: the classic Student test with a pooled variance and df = n₁ + n₂ − 2 = 78. Passing equal_var=False switches to Welch's test (separate variances, Welch–Satterthwaite df). Neither version knows that row i of both arrays is the same customer.
| Call | Test | df here |
|---|---|---|
ttest_ind(a, b) | Student, pooled variance | 78 |
ttest_ind(a, b, equal_var=False) | Welch | ≤ 78 |
ttest_rel(a, b) | paired (one-sample t on a − b) | 39 |
For before/after data on the same customers, the right call is stats.ttest_rel(before, after). It needs equal-length arrays in matching order, and it removes the large customer-to-customer variation in spend from the standard error, which usually gives a much smaller SE and more power.
Why the distractors fail: R's t.test does default to Welch, but SciPy's ttest_ind defaults to Student, a classic trap when porting code. Welch fixes unequal variances, not dependence between measurements. ttest_ind never pairs values, even when the lengths match: its result depends only on each array's mean, variance and size, so shuffling one array would not change it.
Interview tip: when reviewing test code, check three things: paired versus independent design, the variance assumption (equal_var), and the alternative argument (two-sided by default).