Statistical Tests interview question

StatisticsStatistical TestsPythoneasy

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?

python
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)
  1. AA Welch t-test, because equal_var=False is the default, as in R's t.test; it is valid here because Welch needs neither equal variances nor equal sample sizes.
  2. BA Student t-test with df = 78; passing equal_var=False fixes it, because Welch's adjusted degrees of freedom absorb the correlation between the paired values.
  3. CA paired t-test, because ttest_ind pairs the two arrays element-wise when they have equal length, so the result already matches ttest_rel with df = 39.
  4. DA 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).
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.

CallTestdf here
ttest_ind(a, b)Student, pooled variance78
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).

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Statistics questions

  1. A pricing team fits the demand model below on weekly store data. How should the coefficient on ln(price) be interpreted?
  2. Two analysts plot kernel density estimates (KDEs) of the same 5,000 basket values from a grocery chain, one with a bandwidth of 10 € and one with 2 €. The two…
  3. A telecom emails a satisfaction survey to a random sample of the customers who have an email address on file (about 70% of all customers). About 12% respond,…
  4. A product team wants to correlate customers' satisfaction rating (1–5 stars) with their number of support contacts last quarter. What are the requirements for…
  5. Crashes on an inference cluster are modelled as a Poisson process, so the 50 observed times between crashes are i.i.d. exponential with rate λ. Their mean is…