Introduction to Chi-Square Test

Greetings, friends! Today, we're diving into a fascinating statistical test called the Chi-Square Test. It's a handy tool for assessing whether there are significant differences between observed and expected frequencies in one or more categories. This tool is often applied in health sciences, business, and market research.

Ready to unravel the secrets of the Chi-Square Test? Let's get started!

What is the Chi-Square Test?

Think of the Chi-Square Test as an investigator, determining if what we observe matches what we expect. Suppose you have a bag of different colored marbles, and you predict how many of each color you will pull out. The Chi-square test is the tool that can help determine if your observations match your expectations.

The Chi-Square Test assumes two things:

  1. Randomness: The data was randomly sampled.
  2. Adequacy: Each cell in the table contains at least five items, ensuring the test's validity.

Today, we'll learn about the Chi-Square Test in Python!

Understanding Chi-Square Test

The Chi-Square Test calculates a test statistic, denoted χ2\chi^2, which under the null hypothesis (our observed data matches the expected data) follows a chi-square distribution. This test statistic measures the divergence of the observed data from the expected one. The larger the Chi-Square Test statistic, the less likely the observed and expected data will match by chance.

A Bag of Marbles

We've documented the color of each marble drawn from a bag of marbles. Given a predicted distribution of marble colors, we want to know whether our observations match the predictions. Let's explore this situation in Python using the Chi-Square Test!

Python
import pandas as pd

# Observations
data = pd.DataFrame({
    'Color': ['Red', 'Blue', 'Green', 'Yellow', 'Purple'],
    'Observed': [30, 20, 15, 10, 25],
    'Expected': [20, 20, 20, 20, 20]
})

Here's our observed and expected color distribution for the marbles drawn.

Organizing Data

We prepare the observed and expected frequencies for our Chi-Square Test as follows:

# Prepare observed and expected frequencies
observed_frequencies = data['Observed']
expected_frequencies = data['Expected']

We select these from their respective columns, 'Observed' and 'Expected'.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal