liveBook · Manning

Chi-square test

As explained in Statistics Every Programmer Needs

A chi-square test is a statistical hypothesis test that compares observed categorical counts with expected counts to assess whether their discrepancy is statistically significant.

A chi-square goodness-of-fit test compares observed categorical counts with expected counts and evaluates whether their discrepancy is statistically significant. For Benford’s law, the null hypothesis states that leading-digit counts follow the Benford distribution. section 12.5 section 12.5.1

Apply the test by following these steps:

  • Create expected counts by multiplying each expected proportion by the total observed count. section 12.5.1
  • Compute the chi-square statistic by summing (observed count − expected count)² / expected count across categories. section 12.5.1
  • Compare the p-value with the 5% threshold: reject conformity when p < 0.05; otherwise, fail to reject the null hypothesis. section 12.5.1
  • Check that the record count is sufficient before treating the result as meaningful. section 12.5.1
>>> bf_populations = \
>>>     bf_populations.assign(Expected_Counts = \
>>>                           bf_populations.Expected * \
>>>                           sum(bf_populations.Counts))
>>> print(bf_populations)
             Counts     Found  Expected  Expected_Counts
First_1_Dig                                             
1                70  0.297872  0.301030        70.742049
2                37  0.157447  0.176091        41.381446
3                30  0.127660  0.124939        29.360603
4                21  0.089362  0.096910        22.773853
5                25  0.106383  0.079181        18.607593
6                19  0.080851  0.066947        15.732496
7                 7  0.029787  0.057992        13.628108
8                14  0.059574  0.051153        12.020843
9                12  0.051064  0.045757        10.753010

Builds expected leading-digit counts from proportions and the observed total before running the test. section 12.5.1

World population leading digits

For 235 population counts, the test produces a statistic of 7.193 and a p-value of 0.516. Because the p-value exceeds 5%, the result is not statistically significant, so the null hypothesis is not rejected. section 12.5.1

How Statistics Every Programmer Needs covers it

How deep it goes
Derives expected counts, the chi-square statistic, degrees of freedom, and p-value interpretation, while requiring sufficient records.
Examples
Uses leading digits in world population counts, with tabulated observed counts, expected counts, and category contributions.
What it assumes
Requires observed categorical counts, expected proportions, a derived expected-count variable, and a sufficient record count.
What you can do after
Diagnose whether categorical counts significantly depart from an expected distribution using Python’s SciPy implementation.
How it connects
Connects observed counts, expected distribution, null hypothesis, p-value, and Benford’s law.

Also explained in

Compare how 3 books explain chi-square test →

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.