This post is the part 3 of a three-part series on chi-squared distribution. In this post, we discuss the roles played by chi-squared distribution on experiments or random phenomena that result in measurements that are categorical rather than quantitative (part 2 deals with quantitative measurements). An introduction of the chi-squared distribution is found in part 1.
The chi-squared test discussed here is also referred to as Pearson’s chi-squared test, which was formulated by Karl Pearson in 1900. It can be used to assess three types of comparison on categorical variables – goodness of fit, homogeneity, and independence. As a result, we break up the discussion into 3 parts – part 3a (goodness of fit, this post), part 3b (test of homogeneity) and part 3c (test of independence).
Let’s look at the setting for Pearson’s goodness-of-fit test. Consider a random experiment consisting of a series of independent trials each of which results in exactly one of categories. We are interested in summarizing the counts of the trials that fall into the distinct categories. Some examples of such random experiments are:
- In rolling a die times, consider the counts of the faces of the die.
- Perform a series of experiments each of which is a toss of three coins. Summarize the experiments according to the number of heads, 0, 1, 2, and 3, that occur in each experiment.
- Blood donors can be classified into the blood types A, B, AB and O.
- Record the number of automobile accidents per week in a one-mile stretch of highway. Classify the weekly accident counts into the groupings 0, 1, 2, 3, 4 and 5+.
- A group of auto insurance policies are classified into the claim frequency rates of 0, 1, ,2, 3, 4+ accidents per year.
- Auto insurance claims are classified into various claim size groupings, e.g. under 1000, 1000 to 5000, 5000 to 10000 and 10000+.
- In auditing financial transactions in financial documents (accounting statements, expense reports etc), the leading digits of financial figures can be classified into 9 cells: 1, 2, 3, 4, 5, 6, 7, 8, and 9.
Each of the example can be referred to as a multinomial experiment. The characteristics of such an experiment are
- The experiment consists of performing identical trials that are independent.
- For each trial, the outcome falls into exactly one of categories or cells.
- The probability of the outcome of a trial falling into a particular cell is constant across all trials.
For cell , let be the probability of the outcome falling into cell . Of course, . We are interested in the joint random variables where is the number of trials whose outcomes fall into cell .
If (only two categories for each trial), then the experiment is a binomial experiment. Then one of the categories can be called success (with cell probability ) and the other is called failure (with cell probability ). If is the count of the successes, then has a binomial distribution with parameters and .
In general, the variables have a multinomial distribution. To be a little more precise, the random variables have a multinomial distribution with parameters and . Note that the last variable is deterministic since .
In the discussion here, the objective is to make inference on the cell probabilities . The hypotheses in the statistical test are expressed in terms of specific values of , . For example, the null hypothesis may be of the following form:
where are the hypothesized values of the cell probabilities. It is cumbersome to calculate the probabilities for the multinomial distribution. As a result, it would be difficult (if not impossible) to calculate the exact level of significance, which is the probability of type I error. Thus it is critical to use a test statistic that does not depend on the multinomial distribution. Fortunately this problem was solved by Karl Pearson. He formulated a test statistic that has an approximate chi-squared distribution.
The random variables discussed above have a multinomial distribution with parameters , respectively. Of course, each is the probability that the outcome of a trial falls into cell . The marginal distribution of each has a binomial distribution with parameters and with being the probability of success. Thus the expected value and the variance of are and . The following is the chi-squared test statistic.
The statistic defined in (1) was proposed by Karl Pearson in 1900. It is defined by summing the squares of the difference of the observed counts and the expected counts where each squared difference is normalized by the expected count (i.e. divided by the expected count). On one level, the test statistic in (1) seems intuitive since it involves all the deviations . If the observed values are close to the expected cell counts, then the test statistic in (1) would have a small value.
The chi-squared test statistic defined in (1) has an approximate chi-squared distribution when the number of trials is large. The proof of this fact will not be discussed here. We demonstrate with the case for .
The quantity inside the brackets in the last step is approximately normal according to the central limit theorem. Since the square of a normal distribution has a chi-squared distribution with one degree of freedom (see Part 1), the last step in the above derivation has an approximate chi-distribution with 1 df.
In order for the chi-squared distribution to provide an adequate approximation to the test statistic in (1), a rule of thumb requires that the expected cell counts are at least five. The null hypothesis to be tested is that the cell probabilities are certain specified values for . The following is the formal statement.
The hull hypothesis is to be tested against all possible alternatives. In other words, the alternative hypothesis is the statement that for at least one .
The chi-squared test statistic in (1) can be used for a goodness-of-fit test, i.e. to test how well a probability model fit the sample data, in other words, to test whether the observed data come from a hypothesized probability distribution. Example 2 below will test whether the Poisson model is a good fit for a set of claim frequency data.
Degrees of Freedom
Now that we have addressed the distribution for the test statistic in (1), we need to address two more issues. One is the direction of the hypothesis test (one-tailed or two-tailed). The second is the degrees of freedom. The direction of the test is easy to see. Note that the chi-squared test statistic in (1) is always a positive value. On the other hand, if the difference between observed cell counts and expected cell counts is large, the large difference would contradict the null hypothesis. Thus if the chi-squared statistic has a large value, we should reject the null hypothesis. So the correct test to use is the upper tailed chi-squared test.
The number of degrees of freedom is obtained by subtracting one from the cell count for each independent linear restriction placed on the cell probabilities. There is at least one linear restriction. The sum of all the cell probabilities must be 1. Thus the degrees of freedom must be the result of reducing by one at least one time. This means that the degrees of freedom of the chi-squared statistic in (1) is at most .
Furthermore, in the calculation for the specified cell probabilities, if there is any parameter that is unknown and is required to be estimated from the data, then there are further reductions in . If there is any unknown parameter that needs to be estimated from data, a maximum likelihood estimator (MLE) should be used. All these points will be demonstrated by the examples below.
If the value of the chi-squared statistic in (1) is “large”, we reject the null hypothesis stated in (2). By large we mean the value of the chi-squared statistic exceeds the critical value for the desired level of significance. The critical value is the upper tail in the chi-squared distribution (with the appropriate df) of area where is the desired level of significance (e.g. and or some other appropriate level). Instead of using critical value, the p-value approach can also be used. Critical value or p-value can be looked up using table or computed using software. For the examples below, chi-squared functions in Excel are used.
Suppose that we wish to test whether a given die is a fair die. We roll the die 240 times and the following table shows the results.
The null hypothesis
is tested against the alternative that at least one of the equalities is not true. This example is the simplest problem for testing cell probabilities. Since the specified values for the cells probabilities in are known, the degrees of freedom is one less than the cell count. Thus df = 5. The following is the chi-squared statistic based on the data and the null hypothesis.
At level of significance, the chi-squared critical value at df = 5 is . Since 3.65 < 11.07, the hypothesis that the die is fair is not rejected at . The p-value is . With such a large p-value, we also come to the conclusion that the null hypothesis is not rejected.
In all the examples, the critical values and the p-values are obtained by using the following functions in Excel.
=CHISQ.INV.RT(level of significance, df)
=1 – CHISQ.DIST(test statistic, df, TRUE)
We now give an example for the chi-squared goodness-of-fit test. The number of auto accident claims per year from 700 drivers are recorded by an insurance company. The claim frequency data is shown in the following table.
Test the hypothesis that the annual claim count for a driver has a Poisson distribution. Use . Assume that the claim frequency across the drivers in question are independent.
The hypothesized distribution of the annual claim frequency is a Poisson distribution with unknown mean . The MLE of the parameter is the sample mean, which in this case is .
Under the assumption that the claim frequency is Poisson with mean , the cell probabilities are calculated using .
Then the null hypothesis is:
The null hypothesis is tested against all alternatives. The following table shows the calculation of the chi-squared statistic.
The degrees of freedom of the chi-squared statistic is df = 5 – 1 -1 = 3. The first reduction of one is due to the linear restriction of all cell probabilities summing to 1. The second reduction is due to the fact that one unknown parameter has to be estimated using sample data. Using Excel, the critical value is . The p-value is . Thus the null hypothesis is not rejected at the level of significance .
For many data sets, especially for data sets with numbers that distribute across multiple orders of magnitude, the first digits occur according to the probability distribution indicated in below:
Probability for leading digit 1 = 0.301
Probability for leading digit 2 = 0.176
Probability for leading digit 3 = 0.125
Probability for leading digit 4 = 0.097
Probability for leading digit 5 = 0.079
Probability for leading digit 6 = 0.067
Probability for leading digit 7 = 0.058
Probability for leading digit 8 = 0.051
Probability for leading digit 9 = 0.046
This probability distribution was discovered by Simon Newcomb in 1881 and was rediscovered by physicist Frank Benford in 1938. Since then this distribution has become known as the Benford’s law. Thus in many data sets, the leading digit 1 occurs about 30% of the time. The data sets for which this law is applicable are demographic data (e.g. income data of a large population, census data such as populations of cities and counties) and scientific data. The law is also applicable in certain financial data, e.g. tax data, stock exchange data, corporate disbursement and sales data. Thus the Benford’s law is a great tool for forensic accounting and auditing.
The following shows the distribution of first digits in the population counts of all 3,143 counties in the United States (from US census data).
Count for leading digit 1 = 972
Count for leading digit 2 = 573
Count for leading digit 3 = 376
Count for leading digit 4 = 325
Count for leading digit 5 = 205
Count for leading digit 6 = 209
Count for leading digit 7 = 179
Count for leading digit 8 = 155
Count for leading digit 9 = 149
Use the chi-squared goodness-of-fit test to test the hypothesis that the leading digits in the county population data follow the Benford’s law. This example is also discussed in this blog post.
For further information and more examples on chi-squared test, please see the sources listed in the reference section.
- Moore D. S., McCabe G. P., Craig B. A., Introduction to the Practice of Statistics, 7th ed., W. H. Freeman and Company, New York, 2012
- Wackerly D. D., Mendenhall III W., Scheaffer R. L.,Mathematical Statistics with Applications, Thomson Learning, Inc, California, 2008