Hypothesis Tests
Chi-Square Goodness-of-Fit Statistic
Compute the chi-square statistic: χ² = Σ (obs − exp)² / exp, summed over k
categories. A single loop accumulates the per-category contributions using a
diff intermediate. Library: scipy.stats.chisquare(observed, f_exp=expected)
— snapshot shows statistic + pvalue; RESULT is the statistic only, matching
.statistic. Observed and expected must share the same total.
By hand
With the library
scipy.stats.chisquare(observed, f_exp=expected) returns statistic and
pvalue (df=k−1=3). The snapshot shows both; RESULT is the statistic.
observed = [6, 14, 10, 10]
expected = [10, 10, 10, 10]
k = len(observed)
chi2 = 0.0
for i in range(k):
diff = observed[i] - expected[i]
chi2 = chi2 + diff * diff / expected[i]
print('RESULT:', round(chi2, 4))
from scipy import stats
from dalib.display import set_display
set_display()
observed = [6, 14, 10, 10]
expected = [10, 10, 10, 10]
result = stats.chisquare(observed, f_exp=expected)
print('statistic:', round(float(result.statistic), 4))
print('pvalue:', round(float(result.pvalue), 4))
print('RESULT:', round(float(result.statistic), 4))
statistic: 3.2
pvalue: 0.3618
RESULT: 3.2
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 expected counts ≥ 5 per cell); 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.
- df = k−1 = 3 (one degree of freedom lost because observed counts sum to a fixed total). Under H0 (observed follows the expected distribution), χ² follows a chi-square distribution with df=3.
- Requirement: observed and expected must sum to the same total; if expected are proportions, scale them to counts first.
- Valid when each expected count ≥ 5 (normal approximation to Poisson holds). Here all expected=10 ✓.
- Cross-reference:
frequency-count(data-basics) for computing raw observed counts from raw data before passing them here.