Hypothesis Tests
One-Sample t Statistic
Compute the one-sample t statistic: t = (mean − mu0) / SE, where
SE = s/√n with s = sample std (ddof=1). Two loops: first accumulates the
mean, second accumulates the squared deviations for s. Library:
scipy.stats.ttest_1samp(sample, mu0) — snapshot shows both statistic and
pvalue; RESULT is the statistic only, matching .statistic.
By hand
Sample [4,7,2,9,5,9], n=6, mu0=4. Mean=6.0, s=√8≈2.8284, SE≈1.1547. t = (6.0−4.0)/1.1547 ≈ 1.7321 (= √3).
import math
sample = [4, 7, 2, 9, 5, 9]
mu0 = 4
n = len(sample)
total = 0.0
for v in sample:
total = total + v
mean = total / n
sq_diff = 0.0
for v in sample:
sq_diff = sq_diff + (v - mean) ** 2
s = math.sqrt(sq_diff / (n - 1))
se = s / math.sqrt(n)
t = (mean - mu0) / se
print('RESULT:', round(t, 4))
import math
1import math2sample = [4, 7, 2, 9, 5, 9]sample ← [4, 7, 2, 9, 5, 9]
1import math2sample = [4, 7, 2, 9, 5, 9]3mu0 = 4values this step[4, 7, 2, 9, 5, 9]samplemu0 ← 4
2sample = [4, 7, 2, 9, 5, 9]3mu0 = 44n = len(sample)values this step4mu0n ← 6
3mu0 = 44n = len(sample)5total = 0.0values this step6ntotal ← 0.0
4n = len(sample)5total = 0.06for v in sample:values this step0.0totalv ← 4, total ← 4.0
pass 1 of 65total = 0.06for v in sample:7 total = total + v8mean = total / nvalues this step4v0.0 → 4.0totalAll 6 passes — pass 1 is the card above pass vtotal1 4 0.0 → 4.0 2 4 → 7 4.0 → 11.0 3 7 → 2 11.0 → 13.0 4 2 → 9 13.0 → 22.0 5 9 → 5 22.0 → 27.0 6 5 → 9 27.0 → 36.0 for v in sample:
5total = 0.06for v in sample:7 total = total + vmean ← 6.0
7 total = total + v8mean = total / n9sq_diff = 0.0values this step6.0meansq_diff ← 0.0
8mean = total / n9sq_diff = 0.010for v in sample:values this step0.0sq_diffv ← 4, sq_diff ← 4.0
pass 1 of 69sq_diff = 0.010for v in sample:11 sq_diff = sq_diff + (v - mean) ** 212s = math.sqrt(sq_diff / (n - 1))values this step9 → 4v0.0 → 4.0sq_diffAll 6 passes — pass 1 is the card above pass vsq_diff1 9 → 4 0.0 → 4.0 2 4 → 7 4.0 → 5.0 3 7 → 2 5.0 → 21.0 4 2 → 9 21.0 → 30.0 5 9 → 5 30.0 → 31.0 6 5 → 9 31.0 → 40.0 for v in sample:
9sq_diff = 0.010for v in sample:11 sq_diff = sq_diff + (v - mean) ** 2s ← 2.8284271247461903
11 sq_diff = sq_diff + (v - mean) ** 212s = math.sqrt(sq_diff / (n - 1))13se = s / math.sqrt(n)values this step2.8284271247461903sse ← 1.1547005383792517
12s = math.sqrt(sq_diff / (n - 1))13se = s / math.sqrt(n)14t = (mean - mu0) / sevalues this step1.1547005383792517set ← 1.732050807568877
13se = s / math.sqrt(n)14t = (mean - mu0) / se15print('RESULT:', round(t, 4))values this step1.732050807568877tstdout ← RESULT: 1.7321
14t = (mean - mu0) / se15print('RESULT:', round(t, 4))values this stepRESULT: 1.7321stdout
With the library
scipy.stats.ttest_1samp(sample, mu0) returns a result object; .statistic
is the t value and .pvalue is the two-tailed p-value (snapshot only).
import numpy as np
from scipy import stats
from dalib.display import set_display
set_display()
sample = [4, 7, 2, 9, 5, 9]
mu0 = 4
n = len(sample)
mean = float(np.mean(sample))
s = float(np.std(sample, ddof=1))
se = float(s / np.sqrt(n))
result = stats.ttest_1samp(sample, mu0)
print('mean:', round(mean, 4))
print('se:', round(se, 6))
print('t_stat:', round(float(result.statistic), 4))
print('pvalue:', round(float(result.pvalue), 4))
print('RESULT:', round(float(result.statistic), 4))
mean: 6.0
se: 1.154701
t_stat: 1.7321
pvalue: 0.1438
RESULT: 1.7321
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 and approximate normality); 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 appears in the snapshot for context but is not the comparable between naive and library.
- df = n−1 = 5. Under H0: mu = mu0, t follows a t distribution with 5 df.
- Two-tailed p-value: probability of |T| ≥ |t| under H0. Here p≈0.1438, so no rejection at α=0.05.
- s uses ddof=1 (sample std); using ddof=0 would understate variability and inflate the statistic.
- Cross-reference:
standard-error(ch06) for the SE computation;z-scores(ch02) for the standardization idea the t statistic extends.