Chi-square goodness of fit python


A Chi-Square Goodness of Fit Test is used to determine whether or not a categorical variable follows a hypothesized distribution.

This tutorial explains how to perform a Chi-Square Goodness of Fit Test in Python.

Example: Chi-Square Goodness of Fit Test in Python

A shop owner claims that an equal number of customers come into his shop each weekday. To test this hypothesis, a researcher records the number of customers that come into the shop in a given week and finds the following:

  • Monday: 50 customers
  • Tuesday: 60 customers
  • Wednesday: 40 customers
  • Thursday: 47 customers
  • Friday: 53 customers

Use the following steps to perform a Chi-Square goodness of fit test in Python to determine if the data is consistent with the shop owner’s claim.

Step 1: Create the data.

First, we will create two arrays to hold our observed and expected number of customers for each day:

expected = [50, 50, 50, 50, 50]
observed = [50, 60, 40, 47, 53]

Step 2: Perform the Chi-Square Goodness of Fit Test.

Next, we can perform the Chi-Square Goodness of Fit Test using the chisquare function from the SciPy library, which uses the following syntax:

chisquare(f_obs, f_exp) 

where:

  • f_obs: An array of observed counts.
  • f_exp: An array of expected counts. By default, each category is assumed to be equally likely.

The following code shows how to use this function in our specific example:

import scipy.stats as stats

#perform Chi-Square Goodness of Fit Test
stats.chisquare(f_obs=observed, f_exp=expected)

(statistic=4.36, pvalue=0.35947)

The Chi-Square test statistic is found to be 4.36 and the corresponding p-value is 0.35947.

Note that the p-value corresponds to a Chi-Square value with n-1 degrees of freedom (dof), where n is the number of different categories. In this case, dof = 5-1 = 4. You can use the Chi-Square to P Value Calculator to confirm that the p-value that corresponds to X2 = 4.36 with dof = 4 is 0.35947.

Recall that a Chi-Square Goodness of Fit Test uses the following null and alternative hypotheses:

  • H0: (null hypothesis) A variable follows a hypothesized distribution.
  • H1: (alternative hypothesis) A variable does not follow a hypothesized distribution.

Since the p-value (.35947) is not less than 0.05, we fail to reject the null hypothesis. This means we do not have sufficient evidence to say that the true distribution of customers is different from the distribution that the shop owner claimed.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we are going to see how to Perform a Chi-Square Goodness of Fit Test in Python

    The Chi-Square Goodness of fit test is a non-parametric statistical hypothesis test that’s used to determine how considerably the observed value of an event differs from the expected value. it helps us check whether a variable comes from a certain distribution or if a sample represents a population. The observed probability distribution is compared with the expected probability distribution. 

    Chi-square goodness of fit python

    null hypothesis:  A variable has a predetermined distribution.

    Alternative hypotheses: A variable deviates from the expected distribution.

    Example 1: Using stats.chisquare() function

    In this approach we use stats.chisquare() method from the scipy.stats module which helps us determine chi-square goodness of fit statistic and p-value. 

    Syntax: stats.chisquare(f_obs, f_exp)

    parameters:

    • f_obs : this parameter contains an array of observed values.
    • f_exp : this parameter contains an array of expected values.

    In the below example we also use the stats.ppf() method which takes the parameters level of significance and degrees of freedom as input and gives us the value of chi-square critical value. if chi_square_ value > critical value, the null hypothesis is rejected. if chi_square_ value <= critical value, the null hypothesis is accepted. in the below example chi_square value is 5.0127344877344875 and the critical value is 12.591587243743977. As chi_square_ value <=, critical_value null hypothesis is accepted and the alternative hypothesis is rejected.

    Python3

    import scipy.stats as stats

    import numpy as np

    observed_data = [8, 6, 10, 7, 8, 11, 9]

    expected_data = [9, 8, 11, 8, 10, 7, 6]

    chi_square_test_statistic, p_value = stats.chisquare(

        observed_data, expected_data)

    print('chi_square_test_statistic is : ' +

          str(chi_square_test_statistic))

    print('p_value : ' + str(p_value))

    print(stats.chi2.ppf(1-0.05, df=6))

    Output:

    chi_square_test_statistic is : 5.0127344877344875
    p_value : 0.542180861413329
    12.591587243743977

    Example 2: Determining chi-square test statistic by implementing formula

    In this approach, we directly implement the formula. we can see that we get the same values of chi_square. 

    Python3

    import scipy.stats as stats

    import numpy as np

    observed_data = [8, 6, 10, 7, 8, 11, 9]

    expected_data = [9, 8, 11, 8, 10, 7, 6]

    chi_square_test_statistic1 = 0

    for i in range(len(observed_data)):

        chi_square_test_statistic1 = chi_square_test_statistic1 + \

            (np.square(observed_data[i]-expected_data[i]))/expected_data[i]

    print('chi square value determined by formula : ' +

          str(chi_square_test_statistic1))

    print(stats.chi2.ppf(1-0.05, df=6))

    Output:

    chi square value determined by formula : 5.0127344877344875
    12.591587243743977

    How do you calculate goodness

    chisquare() function. In this approach we use stats. chisquare() method from the scipy. stats module which helps us determine chi-square goodness of fit statistic and p-value.

    How do you do a chi

    How to perform the chi-square goodness of fit test.
    Step 1: Calculate the expected frequencies. ... .
    Step 2: Calculate chi-square. ... .
    Step 3: Find the critical chi-square value. ... .
    Step 4: Compare the chi-square value to the critical value. ... .
    Step 5: Decide whether the reject the null hypothesis..

    How do you calculate chi

    The Pearson's chi-squared test for independence can be calculated in Python using the chi2_contingency() SciPy function. The function takes an array as input representing the contingency table for the two categorical variables.

    What does chi

    What is the Chi-square goodness of fit test? The Chi-square goodness of fit test is a statistical hypothesis test used to determine whether a variable is likely to come from a specified distribution or not. It is often used to evaluate whether sample data is representative of the full population.