# \chi^2 Test
Keywords: Authored in PreTeXt

Skip to main content
\(\newcommand{\N}{\mathbb N} \newcommand{\Z}{\mathbb Z} \newcommand{\Q}{\mathbb Q} \newcommand{\R}{\mathbb R} \DeclareMathOperator{\Bin}{Bin} \DeclareMathOperator{\Geom}{Geom} \DeclareMathOperator{\Poiss}{Poiss} \DeclareMathOperator{\Exp}{Exp} \DeclareMathOperator{\Norm}{N} \DeclareMathOperator{\E}{E} \DeclareMathOperator{\Var}{Var} \DeclareMathOperator{\Cov}{Cov} \renewcommand{\L}{\mathcal{L}} \newcommand{\est}{\widehat} \newcommand{\lt}{<} \newcommand{\gt}{>} \newcommand{\amp}{&} \definecolor{fillinmathshade}{gray}{0.9} \newcommand{\fillinmath}[1]{\mathchoice{\colorbox{fillinmathshade}{$\displaystyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\textstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptstyle \phantom{\,#1\,}$}}{\colorbox{fillinmathshade}{$\scriptscriptstyle\phantom{\,#1\,}$}}} \)

# Section 5.4 \(\chi^2\) Test

In our previous hypothesis tests, we’ve asked about the correct value of some distribution parameter. But we might also be interested in whether a particular type of distribution fits the data in the first place. In this situation, we can try to measure the extent to which the observed data differs from what would be expected under a particular distribution.
🔗

## Definition 5.4.1.

Suppose \(O_1, \dotsc, O_n\) are observed data with expected values \(E_1, \dotsc, E_n\text{.}\) The \(\chi^2\) statistic is:
\begin{gather*} \chi^2 = \sum_{i=1}^n \frac{(O_i - E_i)^2}{E_i}. \end{gather*}
🔗
🔗
The expression \(O_i - E_i\) directly measures the difference between an observation and its expected value. However, if we simply summed up these differences, then one observation which is higher than its expected value and another that’s lower would have differences that cancel out. Squaring the term ensures that any difference contributes positively when summed together. And dividing by \(E_i\) ensures that the differences are expressed relative to the size of the expected value. A difference of \(0.1\) might sound like a small difference, but it would have a different interpretation if the expected value is \(100\) than if the expected value is \(0.01\text{.}\)
🔗
So the overall number \(\chi^2\) is designed to be a measurement of how much the observed data differs from expectation. It seems like \(\chi^2\) could be higher or lower simply as the result of the number of terms involved in the summation. How much difference is enough to reject the distribution which generated those expected values? It seems that it should depend on that number of terms.
🔗

## Definition 5.4.2.

If the data collected fall into \(n\) categories, then we will say that there are \(n - 1\) degrees of freedom.
🔗
🔗
Suppose our data comes from polling \(100\) respondents and arranging their responses into \(n\) categories. Then, if we know how many people fall into categories \(1, 2, \dotsc, n-1\text{,}\) we will automatically know how many people fall into the final category, simply because the total number of respondents is already known.
🔗
For each different number of degrees of freedom, there will be a different critical value of the \(\chi^2\) statistic, above which we’ll say that the data differs sufficiently from expectation for us to reject the distribution being tested. A table of critical \(\chi^2\) values is provided in Section B.1.
🔗

## Example 5.4.3.

Suppose \(100\) people are surveyed about their favorite color among four choices. The results are below. Is this data consistent with the hypothesis that each of the four colors is equally likely to be a favorite?
🔗

Color | Chosen as Favorite
--- | ---
Blue | \(37\)
Green | \(26\)
Purple | \(15\)
Red | \(22\)

In this case, if each color is equally likely to be a favorite, then we would expect \(25\) respondents to choose each color. So the \(\chi^2\) statistic is:
\begin{gather*} \chi^2 = \frac{(37 - 25)^2}{25} + \frac{(26 - 25)^2}{25} + \frac{(15 - 25)^2}{25} + \frac{(22 - 25)^2}{25} = 10.16. \end{gather*}
Since the data fall into four categories, the number of degrees of freedom is \(4 - 1 = 3\text{.}\) From the table in Table B.1.1, the critical value is \(7.815\text{.}\) Since \(10.16 \gt 7.815\text{,}\) our collected data is sufficiently different to reject the hypothesis of equal color preferences.
🔗
🔗
In the previous example, we’re testing an extrinsic hypothesis, i.e., a hypothesis made entirely without reference to the data. Knowing that we planned to poll \(100\) people, we could determine our expected values for this hypothesis before even conducting the survey. However, sometimes we need to use the collected data to estimate the value(s) of one or more parameters of a distribution. In such a situation, we say the hypothesis is intrinsic, and we adjust our calculation of degrees of freedom.
🔗

## Definition 5.4.4.

Suppose it takes \(m\) measurements to fully specify the collected data, and suppose there are \(k\) parameters that are estimated using the data. Then the number of degrees of freedom is \(m - k\)
🔗
🔗

## Example 5.4.5.

Is the data below consistent with a geometric distribution?
🔗

Number | Observation
--- | ---
\(1\) | \(59\)
\(2\) | \(21\)
\(3\) | \(11\)
\(4\) | \(8\)
\(5\) | \(1\)

In order to compare this data to a geometric distribution, we would need to know the value of the parameter \(\theta\) for the distribution. Based on the collected data, we could provide a maximum likelihood estimate:
\begin{align*} \widehat{\theta} \amp = \frac{59 + 21 + 11 + 8 + 1}{1 \cdot 59 + 2\cdot 21 + 3\cdot 11 + 4\cdot 8 + 5\cdot 1} \\ \amp = \frac{100}{171} \\ \amp \approx 0.58. \end{align*}
We can use Fact 2.1.14 with the parameter value \(0.58\) to generate expected values for our data. For example:
\begin{align*} g(2; 0.58) \amp = (1 - 0.58)^{1}(0.58) = 0.2436, \end{align*}
so we would expect \(0.2436 \times 100 = 24.36\) observations for the number \(2\text{.}\) The rest of the expected values are calculated similarly:
🔗

Number | Observation | Expected
--- | --- | ---
\(1\) | \(59\) | \(58\)
\(2\) | \(21\) | \(24.36\)
\(3\) | \(11\) | \(10.23\)
\(4\) | \(8\) | \(4.30\)
\(5\) | \(1\) | \(1.80\)

Now we can calculate the \(\chi^2\) statistic:
\begin{align*} \chi^2 \amp = \frac{(59 - 58)^2}{58} + \dotsb + \frac{(1 - 1.8)^2}{1.8} \approx 4.08. \end{align*}
With \(5\) categories, it takes \(4\) measurements to fully specify the data. Then, we used the data itself to estimate the value of one parameter, \(\widehat{\theta}\text{.}\) So the number of degrees of freedom is \(4 - 1 = 3\text{,}\) with critical \(\chi^2\) value \(7.815\text{.}\) Since \(4.08 \lt 7.815\text{,}\) our data is not sufficiently different from expected to reject the hypothesis of a geometric distribution.
🔗
🔗

## Exercises Exercises

### 1.

A store owner wants to determine how much shelf space to allocate to each of the drinks that they sell. They survey their customers about their favorite drinks. Is the data below consistent with the null hypothesis that each type of drink will be equally preferred?
🔗
Table 5.4.6.
🔗

drink type | water | soda | tea | coffee | energy drinks
--- | --- | --- | --- | --- | ---
favorite | 28 | 17 | 15 | 26 | 14

🔗

### 2.

A particular drug is administered in 100 independent trials. In each trial, the drug is administered to four people, and we count how many respond to the drug. The table below shows how many trials have each different count of people who respond to the drug.
🔗
Table 5.4.7.
🔗

# who respond to drug | 0 | 1 | 2 | 3 | 4
--- | --- | --- | --- | --- | ---
# of trials | 3 | 11 | 31 | 34 | 21

#### (a)

Is the data consistent with a binomial distribution with parameter \(p = 0.7\text{?}\)
🔗
🔗

#### (b)

What is the total number of people who have been administered the drug? What is the total number who have responded to it? What is the maximum likelihood estimation for the probability \(p\) that a person will respond to the drug?
🔗
🔗

#### (c)

Is the data consistent with a binomial distribution with the MLE value of \(p\text{?}\)
🔗
🔗
🔗
🔗
🔗
Prev Top Next