Practice / Probability for ML

Chi-square tests: goodness of fit, independence and the chi-square distribution

Ten problems on chi-square tests: the chi-square distribution as a sum of squared standard normals with its mean, variance and the 5% cutoff for two degrees of freedom, a coin whose chi-square statistic is the square of its z score, a goodness-of-fit test of a die rolled 120 times, a classifier's predicted label distribution against a target mix, a Poisson fit to hourly error counts with an estimated rate and a merged tail cell, a 2 × 2 A/B table whose chi-square equals the two-proportion z squared, a 3 × 2 table of model versions and user ratings, its Pearson residuals and Cramér's V, how the statistic grows with the sample size when the proportions stay fixed, and the G-test as twice N times a KL divergence, with worked solutions and the mistakes of running the test on percentages instead of counts, forgetting to subtract a degree of freedom for an estimated parameter, using rc − 1 degrees of freedom for a contingency table, dividing by the observed counts instead of the expected ones and reading a large p-value as proof that the model fits.

Before you start

A chi-square test asks whether counts in categories are close enough to the counts a model predicts. The model might say a die is fair, a classifier should predict labels in a target mix, server errors arrive as a Poisson process, or two variables in a table are independent. For each category, the gap between the observed count OO and the expected count EE is squared and divided by EE, and the sum of these terms is compared with a chi-square distribution, the distribution of a sum of squared standard normals. The number of squared normals, the degrees of freedom, is the number of categories minus the constraints the expected counts were made to satisfy. These ten problems build the distribution, run goodness-of-fit and independence tests by hand, connect the 2×22 \times 2 test to the z test of an A/B experiment and the G-test to the KL divergence, and show what the statistic does as the sample grows.

  • The distribution. If Z1,…,ZkZ_1, \dots, Z_k are independent standard normals, Z12+⋯+Zk2Z_1^2 + \dots + Z_k^2 has a chi-square distribution with kk degrees of freedom, χk2\chi^2_k. Its mean is kk and its variance is 2k2k. For k=2k = 2 the upper tail is exactly Pr⁡(χ22>x)=e−x/2\Pr(\chi^2_2 > x) = e^{-x/2}.
  • Cutoffs at the 5%5\% level. 3.8413.841 for 11 degree of freedom, 5.9915.991 for 22, 7.8157.815 for 33 and 11.0711.07 for 55. The test is one-sided: only large values of the statistic count against H0H_0.
  • Pearson's statistic. X2=∑(O−E)2/EX^2 = \sum (O - E)^2/E, summed over all categories, with OO and EE as counts, never percentages. The expected count of a category is NN times its probability under H0H_0.
  • Goodness of fit. With kk categories, df=k−1−m\text{df} = k - 1 - m, where mm is the number of parameters estimated from the same data to compute the expected counts. The −1-1 is there because the expected counts must add up to NN.
  • Independence. For an r×cr \times c table, E=(row total)(column total)/NE = (\text{row total})(\text{column total})/N and df=(r−1)(c−1)\text{df} = (r - 1)(c - 1).
  • When the approximation holds. The chi-square distribution is a large-sample approximation. A common rule asks for every expected count to be at least 55; merge sparse categories, chosen before looking at the data, to get there.
  • Effect size and residuals. Cramér's V=X2/(N(min⁡(r,c)−1))V = \sqrt{X^2/(N(\min(r, c) - 1))} measures the strength of association on a 00 to 11 scale, whatever NN is. The Pearson residual (O−E)/E(O - E)/\sqrt E shows which cells drive the statistic; their squares add up to X2X^2.
  • G-test. G=2∑Oln⁡(O/E)G = 2\sum O\ln(O/E) is the likelihood-ratio statistic. It is close to X2X^2 when the counts are large, and it has the same degrees of freedom.

Builds on: Hypothesis tests and p-values: z and t tests, power and multiple comparisons, Discrete random variables: expectation, variance and the binomial, geometric and Poisson

Problems

  1. ·

    Let Z1Z_1, Z2Z_2, Z3Z_3 be independent standard normals and Q=Z12+Z22+Z32Q = Z_1^2 + Z_2^2 + Z_3^2. Find E[Q]E[Q] and Var(Q)\text{Var}(Q). Then, using Pr⁡(χ22>x)=e−x/2\Pr(\chi^2_2 > x) = e^{-x/2}, find the 5%5\% cutoff for 22 degrees of freedom.

  2. ·

    A coin lands heads 6060 times in 100100 flips. Compute Pearson's statistic for H0:p=12H_0: p = \tfrac12 with the two categories heads and tails, compare it with the z statistic for the same test, and find the p-value.

  3. ··

    A die is rolled 120120 times, with these counts:

    face123456count251715232416\begin{array}{c|cccccc} \text{face} & 1 & 2 & 3 & 4 & 5 & 6 \\ \hline \text{count} & 25 & 17 & 15 & 23 & 24 & 16 \end{array}

    Test whether the die is fair at the 5%5\% level.

  4. ··

    A classifier should predict the labels cat, dog and bird in the mix 50%50\%, 30%30\% and 20%20\%. On 10001000 inputs it predicts cat 450450 times, dog 350350 times and bird 200200 times. Test whether its predicted mix matches the target, at the 5%5\% level.

  5. ···

    A service logs its errors per hour for 200200 hours: 8080 hours had 00 errors, 6464 had 11, 3636 had 22, 1616 had 33 and 44 had 44, with none higher. Test whether the counts follow a Poisson distribution, estimating its rate from the data, and merging the categories 33 and 44 or more so that every expected count is at least 55.

  6. ··

    In an A/B test, 200200 of 20002000 users convert with the old checkout and 240240 of 20002000 with the new one. Arrange the data as a 2×22 \times 2 table, run the chi-square test of independence and compare the statistic with the pooled two-proportion z statistic, z≈2.021z \approx 2.021.

  7. ··

    Three model versions are each shown to 100100 users, who give a thumbs up or a thumbs down:

    updownv16040v27525v34555\begin{array}{c|cc} & \text{up} & \text{down} \\ \hline \text{v1} & 60 & 40 \\ \text{v2} & 75 & 25 \\ \text{v3} & 45 & 55 \end{array}

    Test whether the rating is independent of the version, at the 5%5\% level.

  8. ··

    For the table of Problem 7, find the Pearson residuals (O−E)/E(O - E)/\sqrt E of every cell and Cramér's VV. Which cells drive the result?

  9. ··

    Keep the classifier's predicted proportions of Problem 4, 45%45\%, 35%35\% and 20%20\%, against the target 50%50\%, 30%30\%, 20%20\%, but vary the number of inputs NN. Write X2X^2 as a function of NN, find the smallest NN that gives significance at 5%5\%, and find X2X^2 at N=100,000N = 100{,}000.

  10. ···

    For the classifier of Problem 4, compute the G-statistic G=2∑Oln⁡(O/E)G = 2\sum O\ln(O/E), show that it equals 2N2N times the KL divergence from the target proportions to the observed ones, and compare it with Pearson's X2≈13.33X^2 \approx 13.33.

Worked solutions

Problem 1

Let Z1Z_1, Z2Z_2, Z3Z_3 be independent standard normals and Q=Z12+Z22+Z32Q = Z_1^2 + Z_2^2 + Z_3^2. Find E[Q]E[Q] and Var(Q)\text{Var}(Q). Then, using Pr⁡(χ22>x)=e−x/2\Pr(\chi^2_2 > x) = e^{-x/2}, find the 5%5\% cutoff for 22 degrees of freedom.

  1. E[Z2]=Var(Z)+(E[Z])2=1E[Z^2] = \text{Var}(Z) + (E[Z])^2 = 1A standard normal has mean 00 and variance 11.
  2. E[Z4]=3E[Z^4] = 3, so Var(Z2)=3−12=2\text{Var}(Z^2) = 3 - 1^2 = 2The fourth moment of a standard normal is 33.
  3. E[Q]=3E[Q] = 3 and Var(Q)=3⋅2=6\text{Var}(Q) = 3 \cdot 2 = 6Means add, and so do variances of independent terms.
  4. e−x/2=0.05e^{-x/2} = 0.05, so x=2ln⁡20x = 2\ln 20Set the upper tail equal to 0.050.05 and take logs.
  5. E[Q]=3E[Q] = 3, Var(Q)=6\text{Var}(Q) = 6, and the 5%5\% cutoff for χ22\chi^2_2 is 2ln⁡20≈5.9912\ln 20 \approx 5.991In general χk2\chi^2_k has mean kk and variance 2k2k, so a statistic close to its degrees of freedom is unremarkable.

Problem 2

A coin lands heads 6060 times in 100100 flips. Compute Pearson's statistic for H0:p=12H_0: p = \tfrac12 with the two categories heads and tails, compare it with the z statistic for the same test, and find the p-value.

  1. E=50E = 50 for heads and 5050 for tailsNpN p and N(1−p)N(1 - p).
  2. X2=10250+(−10)250=4X^2 = \dfrac{10^2}{50} + \dfrac{(-10)^2}{50} = 4Both gaps are 1010, in opposite directions.
  3. z=60−50100⋅0.25=2z = \dfrac{60 - 50}{\sqrt{100 \cdot 0.25}} = 2The binomial has standard deviation Np(1−p)=5\sqrt{Np(1 - p)} = 5.
  4. X2=z2X^2 = z^2, with df=2−1=1\text{df} = 2 - 1 = 1With two categories the two gaps are tied, so there is one squared normal, not two.
  5. X2=4=z2X^2 = 4 = z^2, df=1\text{df} = 1, p=Pr⁡(χ12>4)≈0.0455p = \Pr(\chi^2_1 > 4) \approx 0.0455The same p-value as the two-sided z test, and the cutoff 3.8413.841 is 1.9621.96^2.

Problem 3

A die is rolled 120120 times, with these counts:

face123456count251715232416\begin{array}{c|cccccc} \text{face} & 1 & 2 & 3 & 4 & 5 & 6 \\ \hline \text{count} & 25 & 17 & 15 & 23 & 24 & 16 \end{array}

Test whether the die is fair at the 5%5\% level.

  1. E=120/6=20E = 120/6 = 20 for every faceUnder H0H_0 each face has probability 16\tfrac16.
  2. O−E=5,−3,−5,3,4,−4O - E = 5, -3, -5, 3, 4, -4The gaps add up to 00, as they always must.
  3. ∑(O−E)2=25+9+25+9+16+16=100\sum(O - E)^2 = 25 + 9 + 25 + 9 + 16 + 16 = 100Square every gap.
  4. X2=100/20=5X^2 = 100/20 = 5, with df=6−1=5\text{df} = 6 - 1 = 5All the expected counts are equal, so divide once.
  5. X2=5<11.07X^2 = 5 < 11.07 and p≈0.416p \approx 0.416: no evidence that the die is unfairThe statistic is right at its mean of 55. That is not evidence that the die is fair, only that 120120 rolls cannot tell (Mistake 5).

Problem 4

A classifier should predict the labels cat, dog and bird in the mix 50%50\%, 30%30\% and 20%20\%. On 10001000 inputs it predicts cat 450450 times, dog 350350 times and bird 200200 times. Test whether its predicted mix matches the target, at the 5%5\% level.

  1. E=500,300,200E = 500, 300, 20010001000 times each target proportion.
  2. (−50)2500=5\dfrac{(-50)^2}{500} = 5 and 502300≈8.333\dfrac{50^2}{300} \approx 8.333Cat and dog; bird matches exactly and adds 00.
  3. X2=5+8.333+0≈13.33X^2 = 5 + 8.333 + 0 \approx 13.33The same gap of 5050 counts for more in the smaller category.
  4. df=3−1=2\text{df} = 3 - 1 = 2, and p=e−13.33/2≈0.0013p = e^{-13.33/2} \approx 0.0013The exact tail for 22 degrees of freedom.
  5. X2≈13.33>5.991X^2 \approx 13.33 > 5.991, p≈0.0013p \approx 0.0013: the predicted mix differs from the targetThe classifier over-predicts dog. Running the test on the percentages 45,35,2045, 35, 20 instead gives X2≈1.33X^2 \approx 1.33 and the opposite conclusion (Mistake 1).

Problem 5

A service logs its errors per hour for 200200 hours: 8080 hours had 00 errors, 6464 had 11, 3636 had 22, 1616 had 33 and 44 had 44, with none higher. Test whether the counts follow a Poisson distribution, estimating its rate from the data, and merging the categories 33 and 44 or more so that every expected count is at least 55.

  1. λ^=(64+72+48+16)/200=1\hat\lambda = (64 + 72 + 48 + 16)/200 = 1The maximum-likelihood rate is the sample mean: total errors over hours.
  2. E=200e−1(1,1,12)≈73.58, 73.58, 36.79E = 200e^{-1}\big(1, 1, \tfrac12\big) \approx 73.58,\ 73.58,\ 36.79200Pr⁡(K=k)200\Pr(K = k) for k=0,1,2k = 0, 1, 2, with Pr⁡(K=k)=e−λλk/k!\Pr(K = k) = e^{-\lambda}\lambda^k/k!.
  3. E3+=200−183.94≈16.06E_{3+} = 200 - 183.94 \approx 16.06, with O3+=20O_{3+} = 20The cell 44 or more alone would expect only 3.803.80, so it is merged with 33.
  4. X2≈0.561+1.246+0.017+0.966≈2.79X^2 \approx 0.561 + 1.246 + 0.017 + 0.966 \approx 2.79The four terms (O−E)2/E(O - E)^2/E.
  5. X2≈2.79X^2 \approx 2.79 with df=4−1−1=2\text{df} = 4 - 1 - 1 = 2, so p≈0.248p \approx 0.248: the Poisson model is not rejectedOne degree of freedom goes to the total and one to the estimated λ\lambda. Forgetting the second gives p≈0.425p \approx 0.425 (Mistake 2).

Problem 6

In an A/B test, 200200 of 20002000 users convert with the old checkout and 240240 of 20002000 with the new one. Arrange the data as a 2×22 \times 2 table, run the chi-square test of independence and compare the statistic with the pooled two-proportion z statistic, z≈2.021z \approx 2.021.

  1. Rows old, new; columns converted, not: 200,1800200, 1800 and 240,1760240, 1760Row totals 20002000 and 20002000; column totals 440440 and 35603560.
  2. E=2000⋅440/4000=220E = 2000 \cdot 440/4000 = 220 converted per row, 17801780 notRow total times column total over NN.
  3. ∣O−E∣=20\lvert O - E\rvert = 20 in every cellIn a 2×22 \times 2 table all four gaps have the same size.
  4. X2=400(2220+21780)≈3.636+0.449X^2 = 400\big(\tfrac{2}{220} + \tfrac{2}{1780}\big) \approx 3.636 + 0.449Two cells divide by 220220 and two by 17801780.
  5. X2≈4.086X^2 \approx 4.086, df=1\text{df} = 1, p≈0.043p \approx 0.043; and z2=2.0212≈4.086z^2 = 2.021^2 \approx 4.086The chi-square test of a 2×22 \times 2 table is exactly the two-sided pooled z test, squared, and gives the same p-value. Using rc−1=3rc - 1 = 3 degrees of freedom gives p≈0.25p \approx 0.25 (Mistake 3).

Problem 7

Three model versions are each shown to 100100 users, who give a thumbs up or a thumbs down:

updownv16040v27525v34555\begin{array}{c|cc} & \text{up} & \text{down} \\ \hline \text{v1} & 60 & 40 \\ \text{v2} & 75 & 25 \\ \text{v3} & 45 & 55 \end{array}

Test whether the rating is independent of the version, at the 5%5\% level.

  1. Column totals 180180 up and 120120 down, N=300N = 300Each row total is 100100.
  2. E=100⋅180/300=60E = 100 \cdot 180/300 = 60 up and 4040 down in every rowUnder independence each version gets the overall 60%60\% rate.
  3. v1 adds 00; v2 and v3 each add 15260+15240\dfrac{15^2}{60} + \dfrac{15^2}{40}v2 is 1515 above in up, v3 1515 below.
  4. X2=2(3.75+5.625)=18.75X^2 = 2(3.75 + 5.625) = 18.75225/60=3.75225/60 = 3.75 and 225/40=5.625225/40 = 5.625.
  5. X2=18.75X^2 = 18.75 with df=(3−1)(2−1)=2\text{df} = (3 - 1)(2 - 1) = 2, so p=e−9.375≈8.5×10−5p = e^{-9.375} \approx 8.5 \times 10^{-5}: the rating depends on the versionFar beyond the cutoff 5.9915.991.

Problem 8

For the table of Problem 7, find the Pearson residuals (O−E)/E(O - E)/\sqrt E of every cell and Cramér's VV. Which cells drive the result?

  1. v1: 00 and 00The first version matches the overall rate exactly.
  2. v2: 15/60≈1.9415/\sqrt{60} \approx 1.94 up, −15/40≈−2.37-15/\sqrt{40} \approx -2.37 downPositive residuals mark more counts than independence predicts.
  3. v3: −1.94-1.94 up, +2.37+2.37 downThe mirror image of v2.
  4. 1.942⋅2+2.372⋅2=7.5+11.25=18.751.94^2 \cdot 2 + 2.37^2 \cdot 2 = 7.5 + 11.25 = 18.75The squared residuals add up to X2X^2.
  5. Residuals 0,00, 0; 1.94,−2.371.94, -2.37; −1.94,2.37-1.94, 2.37, and V=18.75/300=0.25V = \sqrt{18.75/300} = 0.25The min⁡(r,c)−1\min(r, c) - 1 is 11 here. v2 is liked and v3 is disliked more than chance allows, and the down cells weigh more because their expected counts are smaller.

Problem 9

Keep the classifier's predicted proportions of Problem 4, 45%45\%, 35%35\% and 20%20\%, against the target 50%50\%, 30%30\%, 20%20\%, but vary the number of inputs NN. Write X2X^2 as a function of NN, find the smallest NN that gives significance at 5%5\%, and find X2X^2 at N=100,000N = 100{,}000.

  1. X2=∑(Np^−Np)2Np=N∑(p^−p)2pX^2 = \sum\dfrac{(N\hat p - Np)^2}{Np} = N\sum\dfrac{(\hat p - p)^2}{p}Every count is NN times a proportion.
  2. ∑(p^−p)2p=0.00250.5+0.00250.3≈0.01333\sum\dfrac{(\hat p - p)^2}{p} = \dfrac{0.0025}{0.5} + \dfrac{0.0025}{0.3} \approx 0.01333The bird term is 00.
  3. 0.01333N>5.9910.01333N > 5.991 when N>449.3N > 449.3The cutoff for 22 degrees of freedom.
  4. N=450N = 450 gives p≈0.0498p \approx 0.0498; N=449N = 449 gives p≈0.0501p \approx 0.0501The same mismatch flips from not significant to significant.
  5. X2=0.01333NX^2 = 0.01333N; the smallest significant NN is 450450; at N=100,000N = 100{,}000, X2≈1333X^2 \approx 1333X2X^2 grows like NN while the effect size w=X2/N≈0.115w = \sqrt{X^2/N} \approx 0.115 stays put. With enough data any mismatch, however small, is significant, so report the size of the effect, not only the p-value.

Problem 10

For the classifier of Problem 4, compute the G-statistic G=2∑Oln⁡(O/E)G = 2\sum O\ln(O/E), show that it equals 2N2N times the KL divergence from the target proportions to the observed ones, and compare it with Pearson's X2≈13.33X^2 \approx 13.33.

  1. 450ln⁡450500≈−47.41450\ln\tfrac{450}{500} \approx -47.41Cat: ln⁡0.9≈−0.1054\ln 0.9 \approx -0.1054.
  2. 350ln⁡350300≈53.95350\ln\tfrac{350}{300} \approx 53.95Dog: ln⁡76≈0.1542\ln\tfrac76 \approx 0.1542. Bird adds 200ln⁡1=0200\ln 1 = 0.
  3. G=2(53.95−47.41)≈13.08G = 2(53.95 - 47.41) \approx 13.08The likelihood-ratio statistic.
  4. KL(p^ ∥ p)=∑p^ln⁡(p^/p)≈0.006541\text{KL}(\hat p\,\Vert\,p) = \sum \hat p\ln(\hat p/p) \approx 0.006541Each term of GG divided by 2N2N, since O=Np^O = N\hat p and O/E=p^/pO/E = \hat p/p.
  5. G≈13.08=2000⋅0.006541G \approx 13.08 = 2000 \cdot 0.006541, close to X2≈13.33X^2 \approx 13.33, with p=e−G/2≈0.0014p = e^{-G/2} \approx 0.0014G=2N KL(p^ ∥ p)G = 2N\,\text{KL}(\hat p\,\Vert\,p) exactly, and X2X^2 is its second-order approximation, so the two agree when the observed and expected proportions are close.

Where this goes wrong

1. Percentages instead of counts

The predicted mix is reported as percentages, and percentages look like the natural thing to compare.

  1. Observed 45,35,2045, 35, 20 and expected 50,30,2050, 30, 20, in percentRight so far as a description of Problem 4.
  2. “X2=2550+2530+0X^2 = \dfrac{25}{50} + \dfrac{25}{30} + 0.”The statistic needs counts. Rescaling every count by cc rescales X2X^2 by cc, so percentages behave as if NN were 100100.
  3. X2≈1.33X^2 \approx 1.33, p≈0.51p \approx 0.51: the classifier matches the targetThe counts out of 10001000 give X2≈13.33X^2 \approx 13.33 and p≈0.0013p \approx 0.0013. The percentages threw away a factor of 1010 in the sample size.

2. Not subtracting a degree of freedom for an estimated parameter

The Poisson test of Problem 5 has four categories, and the rule "categories minus one" is remembered on its own.

  1. X2≈2.79X^2 \approx 2.79 over 44 categoriesRight so far: Problem 5.
  2. “df=4−1=3\text{df} = 4 - 1 = 3.”The rate λ^=1\hat\lambda = 1 was estimated from the same counts, which pulls the expected counts toward the observed ones and removes one more degree of freedom.
  3. p=Pr⁡(χ32>2.79)≈0.425p = \Pr(\chi^2_3 > 2.79) \approx 0.425The right reference is χ22\chi^2_2, with p≈0.248p \approx 0.248 and cutoff 5.9915.991 instead of 7.8157.815. Too many degrees of freedom makes every fit look better than it is.

3. rc − 1 degrees of freedom for a contingency table

A 2×22 \times 2 table has four cells, so four minus one looks like the count of free cells.

  1. X2≈4.086X^2 \approx 4.086 for the A/B tableRight so far: Problem 6.
  2. “df=4−1=3\text{df} = 4 - 1 = 3.”Under independence the expected counts must match both the row totals and the column totals. Once one cell of a 2×22 \times 2 table is fixed with all totals known, the other three follow, so df=(2−1)(2−1)=1\text{df} = (2 - 1)(2 - 1) = 1.
  3. p=Pr⁡(χ32>4.086)≈0.25p = \Pr(\chi^2_3 > 4.086) \approx 0.25, not significantAgainst the cutoff 7.8157.815 the result vanishes. With 11 degree of freedom the cutoff is 3.8413.841 and p≈0.043p \approx 0.043, the same as the z test.

4. Dividing by the observed counts

The formula is remembered as "squared difference over the count", and the observed count is the one on the page.

  1. O−E=5,−3,−5,3,4,−4O - E = 5, -3, -5, 3, 4, -4 for the dieRight so far: Problem 3.
  2. “X2=∑(O−E)2/OX^2 = \sum(O - E)^2/O.”Pearson's statistic divides by EE: under H0H_0 a count has variance about EE, so (O−E)/E(O - E)/\sqrt E is roughly a standard normal. Dividing by OO gives a different statistic, Neyman's, which only agrees with Pearson's when every count is large, and is infinite whenever a category has no observations.
  3. X2=2525+917+2515X^2 = \tfrac{25}{25} + \tfrac{9}{17} + \tfrac{25}{15} +923+1624+1616+ \tfrac{9}{23} + \tfrac{16}{24} + \tfrac{16}{16} ≈5.25\approx 5.25The right value is 100/20=5100/20 = 5. The gap is small here because the counts are near 2020; with a sparse category it can be huge.

5. A large p-value read as proof that the model fits

The die of Problem 3 passed its test easily, so it is declared fair.

  1. X2=5X^2 = 5, p≈0.416p \approx 0.416Right so far: Problem 3.
  2. “The test shows the die is fair.”A test can only fail to find a difference; whether it would have found one depends on its power.
  3. The die is fairA die that shows 11 a fifth of the time, and each other face 16%16\% of the time, gives 120120 rolls a noncentrality of only 120∑(p1−p0)2/p0=0.96120\sum(p_1 - p_0)^2/p_0 = 0.96, and the test rejects it only about 10%10\% of the time. Ninety percent of the time such a loaded die passes. Keeping H0H_0 means "not shown to be wrong", not "shown to be right".

Print this set: chi-square-tests-goodness-of-fit-and-independence.pdf (problems, answers, and worked solutions on separate pages).

Read the guide: Chi-square tests: compare counts with expected counts, divide each squared gap by E, and get the degrees of freedom right