Compute the pooled (equal-variance) two-sample t statistic: t = (mean1 − mean2) / (sp × √(1/n1 + 1/n2)), where sp = pooled std = √(((n1−1)s1² + (n2−1)s2²) / (n1+n2−2)). Four loops: two means, two squared-deviation sums. Library: scipy.stats.ttest_ind(s1, s2) with default equal_var=True (pooled) — snapshot shows statistic+pvalue; RESULT is the statistic only.

By hand

With the library

scipy.stats.ttest_ind(s1, s2) defaults to equal_var=True (pooled formula), matching the naive computation. The snapshot shows both statistic and pvalue; RESULT is the statistic.

naive.py
import math
s1 = [2, 4, 5, 7]
s2 = [1, 2, 3, 4]
n1 = len(s1)
n2 = len(s2)
total1 = 0.0
for v in s1:
    total1 = total1 + v
mean1 = total1 / n1
total2 = 0.0
for v in s2:
    total2 = total2 + v
mean2 = total2 / n2
sq1 = 0.0
for v in s1:
    sq1 = sq1 + (v - mean1) ** 2
sq2 = 0.0
for v in s2:
    sq2 = sq2 + (v - mean2) ** 2
sp = math.sqrt((sq1 + sq2) / (n1 + n2 - 2))
t = (mean1 - mean2) / (sp * math.sqrt(1 / n1 + 1 / n2))
print('RESULT:', round(t, 4))
library.py
import numpy as np
from scipy import stats
from dalib.display import set_display
set_display()

s1 = [2, 4, 5, 7]
s2 = [1, 2, 3, 4]
result = stats.ttest_ind(s1, s2)  # equal_var=True by default (pooled)
print('mean1:', round(float(np.mean(s1)), 4))
print('mean2:', round(float(np.mean(s2)), 4))
print('t_stat:', round(float(result.statistic), 4))
print('pvalue:', round(float(result.pvalue), 4))
print('RESULT:', round(float(result.statistic), 4))
mean1: 4.5
mean2: 2.5
t_stat: 1.633
pvalue: 0.1536
RESULT: 1.633

Honesty

This lesson shows the computation exactly, on a tiny pinned sample. The arithmetic is correct and reproducible, but with a sample this small the result is not a valid statistical finding — it demonstrates the mechanism, not evidence. Real inference needs an adequate sample size and assumption checks (e.g. independence, approximate normality, and equal variances for the pooled form); the p-value / interval here should be read as "how the formula is computed," not as a conclusion about a population.

Implementation notes

  • Design rule §7: RESULT = statistic only; p-value is snapshot-only.
  • Pooled assumption (equal_var=True): both groups share one population variance estimate, giving df = n1+n2−2 = 6.
  • Welch's t (equal_var=False) uses separate variance estimates and a fractional df (Welch-Satterthwaite). Prefer Welch when group variances differ; it's the safer default in practice.
  • Here sp=√3 cancels cleanly: t = 2√(2/3) = 2/√1.5 ≈ 1.633.
  • Cross-reference: one-sample-t-stat (this chapter) for the single-group version and the ddof=1 rule.