liveBook · Manning

Chi-square statistic

As explained in Statistics Every Programmer Needs

A chi-square statistic is the sum of squared observed-minus-expected count differences divided by their expected counts.

The chi-square statistic measures how far observed counts depart from expected counts by summing squared differences divided by expected counts: χ² = Σ(Oᵢ − Eᵢ)²/Eᵢ. section 12.5.1

Interpret the calculation in a testing workflow:

  • Calculate one contribution for each category, then sum the contributions to obtain the test statistic. section 12.5.1
  • Compare the statistic with a p-value to decide whether to reject the null hypothesis at the 5% threshold. section 12.5.1

Leading digits in population counts

For 235 country and territory counts, the statistic is 7.193 and the p-value is 0.516. Because the p-value exceeds 5%, fail to reject the null hypothesis that the counts follow Benford’s law. Leading digits 5 and 7 contribute most to the statistic. section 12.5.1

>>> import scipy.stats as stats
>>> x2 = stats.chisquare(bf_populations.Counts, 
>>>                      bf_populations.Expected_Counts)
>>> print(x2)
Power_divergenceResult(statistic=7.192526063677557, pvalue=0.5160103482305225)

Compute the statistic and p-value directly with scipy.stats.chisquare using observed counts and expected counts. section 12.5.1

How Statistics Every Programmer Needs covers it

How deep it goes
Derives the formula, tabulates each category’s contribution, implements SciPy computation, and interprets the p-value.
Examples
Uses leading digits from 235 world population counts, a contribution table, and a scipy.stats output.
What it assumes
Assumes observed and expected category counts while defining how to compare them and interpret the resulting test.
What you can do after
Apply it via SciPy or manual calculation to evaluate distributional conformity and interpret statistical significance.
How it connects
Connects observed counts and expected counts with p-values, null hypotheses, Benford’s law, and degrees of freedom.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.