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
