Login
📚 Python for Introductory Statistics
Chapters ▾

13.1 Goodness of Fit Test

Watch demo video

Categorical or qualitative data can be organized by frequency distribution of a list of each category and the corresponding counts or relative frequencies.

A Chi-square goodness-of-fit is used to test whether a frequency distribution follows a given specific distribution.

Ho: The random variable follows a certain given distribution or (the probability for each category, Pi, as given is true).

Ha: The random variable does not follow the distribution in the null hypothesis

The Chi-square test statistic is given as:

χ 2 = i = 1 k ( O i E i ) 2 E i

Where

Oi is the observed value for category i

Ei=nPi is the expected value for category i assuming Ho is true.n is sample size.

df = k 1

For large sample sizes χ2 is approximately follows the chi-square probability distribution

Note: In order to perfrom the Chi-Square goodness of fit, we assume:

For chi-square goodness of fit test, use the following syntax with observed frequency stored in obs and expected frequency stored in exp:

from scipy import stats
stats.chisquare(obs,exp)

returns Power_divergenceResult(statistic, pvalue)

Example: A store manager claims that a plain M&M bag of candies contains an equal number of each of the six colors: brown, yellow, red, blue, orange, and green. The counts for each color of a randomly selected bag of plain M&M is given below. Test the store managers claim at 5% level of significance.

colorfrequency
brown57
yellow64
red54
blue75
orange86
green64

The above table contains the observed (Oi) frequencies. We need to find the expected frequencies before we call the function. If the manager's claim is true there will be equal number of candies for each color. We need the total number of candies and divide it to 6 to get the expected frequencies.

obs = [57,64,54,75,86,64]
sum(obs)   #total number of candies in the bag
Show expected output
400

If the null is true, we expect an equal number in each category. That is, Pi=16 for all i. Ei=nPi=400/6

colorobservedexpected
brown57400/6
yellow64400/6
red54400/6
blue75400/6
orange86400/6
green64400/6
exp = [400/6,400/6,400/6,400/6,400/6,400/6]
obs = [57,64,54,75,86,64]
from scipy import stats

obs = [57,64,54,75,86,64]
exp = [400/6,400/6,400/6,400/6,400/6,400/6]

stats.chisquare(obs,exp)
Show expected output
Power_divergenceResult(statistic=10.67, pvalue=0.05833003695944934)

Interpretation: The Chi-square test statistic is equal to 10.67. At the p-value is not less than 0.05. We would fail to reject the null hypothesis.

Example: The number of sales of electric vehicles (EV) in the USA is given in the table below. Test a claim that the distribution of sales is uniform with α=0.05.

BrandSales
Ford2400000
Toyota2200000
Chevrolet2020000
Honda1500000
import numpy as np
from scipy import stats

obs = [2400000,2200000,2020000,1500000]  # observed frequency
sum = np.sum(obs)                        # sum of observed freq   
exp = [sum/4,sum/4,sum/4,sum/4]          # expected freq

stats.chisquare(obs,exp)
Show expected output
Power_divergenceResult(statistic=220098.52216748768, pvalue=0.0)

Interpretation: The chi-square test statistics is 220098.52216748768 and the p-value 0.0. Reject the null hypothesis. There is enough evidence to conclude that the distribution is not uniform.

Adapted from Python for Introductory Statistics, by Simon Aman (Truman College, City Colleges of Chicago), licensed under CC BY 4.0. Changes were made: reformatted as an accessible XYZ web edition with live in-browser code cells. License: CC-BY-4.0.