\setcounter{ExampleCounter}{1}
Remember, our goal now is to test how well data matches our expectation, specifically regarding how the data breaks down for a number of categories.

Let's go back to the M\&M example.  Suppose you opened a bag of 600 M\&Ms, separated them by color, and got the following counts for each category.
\begin{center}
\begin{tabular}{l l}
Color & Number\\
\hline
Blue & 212\\
Orange & 147\\
Green & 103\\
Red & 50\\
Yellow & 46\\
Brown & 42
\end{tabular}
\end{center}

You may already see evidence that the candies are not uniformly distributed, but how can we measure this?

\subsection{The Chi-Square Test Statistic}
The test statistic we use to measure how closely data matches our expected distribution is found by calculating how far off each category is, then combining all those errors:
\[\chi^2 = \sum \dfrac{(\textrm{Observed -- Expected})^2}{\textrm{Expected}}\]
Or more concisely,
\[\chi^2 = \sum \dfrac{(O - E)^2}{E}\]

Now, the larger this value, the more difference there is between what we expected and what we observe.  Therefore, the larger $\chi^2$ is, the less likely it is that our expected distribution was correct.
\vfill
\pagebreak

\subsection{The p Value}
The p value for this test will be the probability that $\chi^2$ could be at least as large as what we observe.  So if we get a large value for $\chi^2$, that probability will be relatively low (recall the shape of the $\chi^2$ distribution):
\begin{center}
\begin{tikzpicture}
  \begin{axis}[no markers, axis lines*=none,
  hide y axis,xtick={8},
  xticklabels={{$\chi^2$}},width=12cm, height=6cm]
    \addplot[domain=8:15,fill=cyan!40, draw=none]
      {chisquare(x,\grauliber)} \closedcycle;
    %\addplot[domain=0.01:\chiright]
      %{chisquare(x,\grauliber)} \closedcycle;
    \addplot[samples at={0.2,0.19,...,0}, thick]
      {chisquare(x,\grauliber)} -- (0,0);
    \addplot[samples=50, thick, smooth, domain=0.2:15]
      {chisquare(x,\grauliber)};
    \draw (0,0) -- (150,0);
    \draw [yshift=0.5cm, -latex](axis cs:8,0) -- node [fill=white] {$p$} (axis cs:15,0);
  \end{axis}
\end{tikzpicture}
\end{center}

\subsection{Testing Goodness of Fit}
Now that we have our test statistic ($\chi^2$) and we can calculate a p-value, the rest of the test is just like the others that we've done.
\begin{itemize}
\item The degrees of freedom for $\chi^2$ is based on the number of categories in the data (M\&M example: 6 categories).  If there are $k$ categories,
\[df = k - 1\]
\end{itemize}

\paragraph{Step 1:} State the hypotheses.

The null hypothesis will always be that the data fits whatever distribution we're assuming.  The alternate hypothesis will be that it does not.
\begin{align*}
H_0\ &:\ \textrm{The observed frequencies match the expected frequencies}\\
H_1\ &:\ \textrm{The observed frequencies do not match the expected frequencies}
\end{align*}
\pagebreak

\paragraph{Step 2:} Calculate the test statistic.
\[\chi^2 = \sum \dfrac{(O - E)^2}{E}\]

\paragraph{Step 3:} Calculate the p value (the area above the $\chi^2$ statistic:
\[\chi^2\texttt{cdf(value,1000000,df)}\]

\paragraph{Step 4:} Draw a conclusion.
\begin{itemize}
\item If p is smaller than $\alpha$, reject the null hypothesis -- the data doesn't match the expected distribution
\item If p is larger than $\alpha$, fail to reject the null hypothesis -- there's no evidence that the data doesn't match the expected distribution
\end{itemize}

\begin{example}{Work Absences}
A managers wants to know which days of the week her employees are absent in a five-day work week.  Most employers would like to believe that employees are absent equally during the week.  She tracked the next 60 absences and recorded on which day they occurred, and the results are shown in the table below.  Do the absences occur with equal frequencies during the work week?  Use a significance level of 0.05.

\begin{center}
\begin{tabular}{l c}
Day & Number of Absences\\
\hline
Monday & 15\\
Tuesday & 12\\
Wednesday & 9\\
Thursday & 9\\
Friday & 15
\end{tabular}
\end{center}

\paragraph{Step 1:} State the hypotheses.
\begin{align*}
H_0\ &:\ \textrm{Absences are equally distributed}\\
H_1\ &:\ \textrm{Absences are not equally distributed}
\end{align*}
\pagebreak

\paragraph{Step 2:} Calculate the test statistic.

Notice that since there are 5 days and a total of 60 absences, the expected number of absences each day is $60 / 5 = 12$.
\begin{center}
\begin{tabular}{l c c c}
Day & Observed Absences & Expected Absences & $O - E$\\
\hline
Monday & 15 & 12 & 3\\
Tuesday & 12 & 12 & 0\\
Wednesday & 9 & 12 & $-3$\\
Thursday & 9 & 12 & $-3$\\
Friday & 15 & 12 & 3
\end{tabular}
\end{center}

Then square each difference, divide by the expected number, and add the results:

\begin{align*}
\chi^2 &= \dfrac{3^2}{12} + \dfrac{0^2}{12} + \dfrac{(-3)^2}{12} + \dfrac{(-3)^2}{12} + \dfrac{3^2}{12}\\
&= \dfrac{1}{12}(9+9+9+9)\\
&= 3
\end{align*}

\begin{center}
\begin{tikzpicture}
  \begin{axis}[no markers, axis lines*=none,
  hide y axis,xtick={3},
  xticklabels={{$\chi^2=3$}},width=12cm, height=6cm]
    \addplot[domain=3:15,fill=cyan!40, draw=none]
      {chisquare(x,\grauliber)} \closedcycle;
    %\addplot[domain=0.01:\chiright]
      %{chisquare(x,\grauliber)} \closedcycle;
    \addplot[samples at={0.2,0.19,...,0}, thick]
      {chisquare(x,\grauliber)} -- (0,0);
    \addplot[samples=50, thick, smooth, domain=0.2:15]
      {chisquare(x,\grauliber)};
    \draw (0,0) -- (150,0);
    \draw [yshift=3cm, -latex](axis cs:3,0) -- node [fill=white] {$p$} (axis cs:15,0);
  \end{axis}
\end{tikzpicture}
\end{center}

\paragraph{Step 3:} Calculate the p value that corresponds to this area.  Since there are 5 categories (5 days of the work week),
\[df = 5 - 1 = 4\]
\begin{center}
$\chi^2$\texttt{cdf(3,1000000,4)} $= 0.5578$
\end{center}

\paragraph{Step 4:} Draw a conclusion.\\

Since $p > 0.05$, we fail to reject the null hypothesis, so there is not sufficient evidence that absences do not occur with equal frequency.
\end{example}
\pagebreak

\begin{example}{TV Ownership}
A study concluded that the number of TVs in American households is distributed as in the table below.
\begin{center}
\begin{tabular}{c c}
Number of TVs & Percent\\
\hline
0 & 10\\
1 & 16\\
2 & 55\\
3 & 11\\
4+ & 8
\end{tabular}
\end{center}

A new study investigates households on the west coast of the US, to see if the distribution there is similar to the entire country, or if it is unique.  A random sample of 600 west coast households yielded the following data.
\begin{center}
\begin{tabular}{c c}
Number of TVs & Frequency\\
\hline
0 & 66\\
1 & 119\\
2 & 340\\
3 & 60\\
4+ & 15
\end{tabular}
\end{center}

Using a significance level of 0.01, does it seem that the distribution for the west coast is different from the country as a whole?

\paragraph{Step 1:} State the hypotheses.
\begin{align*}
H_0\ &:\ \textrm{TV ownership follows the same distribution}\\
H_1\ &:\ \textrm{TV ownership follows a different distribution}
\end{align*}

\paragraph{Step 2:} Calculate the test statistic.

Note carefully that the first table above lists percentages, and the second lists frequencies.  Therefore, to calculate the expected frequency in each category, we need to multiply that percentage by 600 (our sample size).
\begin{center}
\begin{tabular}{l c c c}
Number of TVs & Observed Frequency & Expected Frequency & $O - E$\\
\hline
0 & 66 & 60 & 6\\
1 & 119 & 96 & 23\\
2 & 340 & 330 & 10\\
3 & 60 & 66 & $-6$\\
4+ & 15 & 48 & $-33$
\end{tabular}
\end{center}
\pagebreak

\begin{align*}
\chi^2 &= \dfrac{6^2}{60} + \dfrac{23^2}{96} + \dfrac{10^2}{330} + \dfrac{(-6)^2}{66} + \dfrac{(-33)^2}{48}\\
&= 29.65
\end{align*}

\begin{center}
\begin{tikzpicture}
  \begin{axis}[no markers, axis lines*=none,
  hide y axis,xtick={13},
  xticklabels={{$\chi^2=29.65$}},width=12cm, height=6cm]
    \addplot[domain=13:15,fill=cyan!40, draw=none]
      {chisquare(x,\grauliber)} \closedcycle;
    %\addplot[domain=0.01:\chiright]
      %{chisquare(x,\grauliber)} \closedcycle;
    \addplot[samples at={0.2,0.19,...,0}, thick]
      {chisquare(x,\grauliber)} -- (0,0);
    \addplot[samples=50, thick, smooth, domain=0.2:15]
      {chisquare(x,\grauliber)};
    \draw (0,0) -- (150,0);
    \draw [yshift=0.5cm, -latex](axis cs:13,0) -- node [fill=white] {$p$} (axis cs:15,0);
  \end{axis}
\end{tikzpicture}
\end{center}

\paragraph{Step 3:} Calculate the p value that corresponds to this area.  Since there are 5 categories (notice, not 600; that's the sample size),
\[df = 5 - 1 = 4\]
\begin{center}
$\chi^2$\texttt{cdf(29.65,1000000,4)} $= 0.000006$
\end{center}

\paragraph{Step 4:} Draw a conclusion.\\

Since $p < 0.05$, we reject the null hypothesis, so the distribution on the west coast \emph{is} different from the country as a whole.
\end{example}
\pagebreak

\begin{example}{Casino Game}
A new casino game involves rolling 3 dice, and winnings are based on the total number of sixes rolled.  A gambler played the game 100 times, with the following results.
\begin{center}
\begin{tabular}{c c}
Number of Sixes & Frequency\\
\hline
0 & 48\\
1 & 35\\
2 & 15\\
3 & 3
\end{tabular}
\end{center}

Should the casino ban this player for using rigged dice?

\paragraph{Step 1:} State the hypotheses.
\begin{align*}
H_0\ &:\ \textrm{The dice are fair}\\
H_1\ &:\ \textrm{The dice are rigged}
\end{align*}

\paragraph{Step 2:} Calculate the test statistic.

For fair dice, the probability of rolling a 6 is $1/6$, so we can build the following expected table by using the binomial distribution.
\begin{center}
\begin{tabular}{c c}
Number of Sixes & Probability\\
\hline
0 & 0.580\\
1 & 0.345\\
2 & 0.070\\
3 & 0.005
\end{tabular}
\end{center}

Again, the observed values are frequencies and the expected values are proportions, so we need to calculate the expected frequency using the sample size of 100.
\begin{center}
\begin{tabular}{l c c c}
Number of Sixes & Observed Frequency & Expected Frequency & $O - E$\\
\hline
0 & 48 & 58.0 & $-10$\\
1 & 35 & 34.5 & 0.5\\
2 & 15 & 7.0 & 8\\
3 & 3 & 0.5 & 2.5
\end{tabular}
\end{center}
\pagebreak

\begin{align*}
\chi^2 &= \dfrac{(-10)^2}{58} + \dfrac{0.5^2}{34.5} + \dfrac{8^2}{7} + \dfrac{2.5^2}{0.5}\\
&= 23.37
\end{align*}

\begin{center}
\begin{tikzpicture}
  \begin{axis}[no markers, axis lines*=none,
  hide y axis,xtick={13},
  xticklabels={{$\chi^2=23.37$}},width=12cm, height=6cm]
    \addplot[domain=13:15,fill=cyan!40, draw=none]
      {chisquare(x,\grauliber)} \closedcycle;
    %\addplot[domain=0.01:\chiright]
      %{chisquare(x,\grauliber)} \closedcycle;
    \addplot[samples at={0.2,0.19,...,0}, thick]
      {chisquare(x,\grauliber)} -- (0,0);
    \addplot[samples=50, thick, smooth, domain=0.2:15]
      {chisquare(x,\grauliber)};
    \draw (0,0) -- (150,0);
    \draw [yshift=0.5cm, -latex](axis cs:13,0) -- node [fill=white] {$p$} (axis cs:15,0);
  \end{axis}
\end{tikzpicture}
\end{center}

\paragraph{Step 3:} Calculate the p value that corresponds to this area.  Since there are 4 categories,
\[df = 4 - 1 = 3\]
\begin{center}
$\chi^2$\texttt{cdf(23.37,1000000,3)} $= 0.00003$
\end{center}

\paragraph{Step 4:} Draw a conclusion.\\

Since $p < 0.05$, we reject the null hypothesis, so the player is almost certainly using rigged dice.
\end{example}
\vfill
\pagebreak