Goodness of fit evaluation helps us check if our data matches a specific expected pattern or distribution.
Goodness of fit evaluation in SciPy
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
SciPy
from scipy.stats import chisquare chisquare(f_obs, f_exp=None, ddof=0, axis=0)
f_obs is the observed frequency counts (your data).
f_exp is the expected frequency counts (if None, equal frequencies are assumed).
Examples
SciPy
from scipy.stats import chisquare observed = [20, 30, 50] result = chisquare(observed) print(result)
SciPy
from scipy.stats import chisquare observed = [25, 35, 40] expected = [30, 30, 40] result = chisquare(observed, expected) print(result)
Sample Program
This program tests if the candy colors are equally distributed using the chi-square test.
SciPy
from scipy.stats import chisquare # Observed counts of colors in a bag of candies observed_counts = [50, 30, 20] # Expected counts if colors are equally likely expected_counts = [100 / 3] * 3 # Perform chi-square goodness of fit test result = chisquare(f_obs=observed_counts, f_exp=expected_counts) print(f"Chi-square statistic: {result.statistic:.2f}") print(f"P-value: {result.pvalue:.4f}")
Important Notes
A low p-value (usually below 0.05) means the observed data does not fit the expected distribution well.
The chi-square test requires that expected frequencies are not too small (usually at least 5).
Summary
Goodness of fit tests check how well data matches an expected pattern.
Use scipy.stats.chisquare to perform the chi-square test easily.
Interpret the p-value to decide if the fit is good or not.
Practice
1. What does the chi-square goodness of fit test in
scipy.stats.chisquare primarily evaluate?easy
Solution
Step 1: Understand the purpose of chi-square test
The chi-square goodness of fit test compares observed data frequencies to expected frequencies to check if they match.Step 2: Identify what
This function calculates the chi-square statistic and p-value to evaluate the fit between observed and expected counts.scipy.stats.chisquaredoesFinal Answer:
How well observed data matches expected frequencies -> Option BQuick Check:
Goodness of fit = observed vs expected match [OK]
Hint: Chi-square tests observed vs expected frequencies [OK]
Common Mistakes:
- Confusing goodness of fit with correlation
- Thinking it measures mean differences
- Mixing variance analysis with goodness of fit
2. Which of the following is the correct way to import the chi-square goodness of fit test function from scipy?
easy
Solution
Step 1: Recall the module structure of scipy
The chi-square test function is inside the stats submodule of scipy.Step 2: Identify correct import syntax
The correct import isfrom scipy.stats import chisquareto directly access the function.Final Answer:
from scipy.stats import chisquare -> Option CQuick Check:
Correct import = from scipy.stats import chisquare [OK]
Hint: Import from scipy.stats for statistical tests [OK]
Common Mistakes:
- Trying to import chisquare directly from scipy
- Using incorrect module paths
- Using alias without import
3. What will be the output of the following code?
from scipy.stats import chisquare observed = [20, 30, 50] expected = [25, 25, 50] result = chisquare(f_obs=observed, f_exp=expected) print(round(result.statistic, 2), round(result.pvalue, 3))
medium
Solution
Step 1: Calculate chi-square statistic manually
Chi-square = sum((observed - expected)^2 / expected) = ((20-25)^2/25) + ((30-25)^2/25) + ((50-50)^2/50) = (25/25)+(25/25)+0 = 1+1+0 = 2.0Step 2: Interpret p-value from scipy output
Using scipy.stats.chisquare with these values gives a p-value around 0.368, indicating moderate fit.Final Answer:
2.0 0.368 -> Option AQuick Check:
Chi-square stat = 2.0, p-value ≈ 0.368 [OK]
Hint: Calculate chi-square stat then check p-value [OK]
Common Mistakes:
- Forgetting to square differences
- Dividing by wrong expected values
- Mixing up statistic and p-value
4. Identify the error in this code snippet for performing a chi-square goodness of fit test:
from scipy.stats import chisquare observed = [15, 25, 35] expected = [20, 20] result = chisquare(f_obs=observed, f_exp=expected) print(result)
medium
Solution
Step 1: Check input array lengths
The observed array has 3 elements, but expected has only 2 elements, which is invalid for chi-square test.Step 2: Understand scipy requirement
Both observed and expected arrays must be the same length to compare frequencies correctly.Final Answer:
Observed and expected arrays have different lengths -> Option AQuick Check:
Array length mismatch causes error [OK]
Hint: Observed and expected must be same length [OK]
Common Mistakes:
- Ignoring length mismatch
- Assuming expected must be integers
- Thinking import or print is the error
5. You have observed counts of [40, 35, 25] for three categories. You expect them to be equally likely. Using
scipy.stats.chisquare, what is the p-value indicating if the observed data fits the equal distribution? (Hint: expected counts are equal for all categories.)hard
Solution
Step 1: Calculate expected counts for equal distribution
Total counts = 40+35+25 = 100. Expected counts = [100/3, 100/3, 100/3] ≈ [33.33, 33.33, 33.33].Step 2: Perform chi-square test with scipy
Usingchisquare(f_obs=[40,35,25], f_exp=[33.33,33.33,33.33])gives a chi-square statistic ≈ 3.5 and p-value ≈ 0.174.Final Answer:
0.174 -> Option DQuick Check:
Unequal counts vs equal expected gives p-value ≈ 0.174 [OK]
Hint: Equal expected counts = total/number categories [OK]
Common Mistakes:
- Using observed counts as expected
- Not dividing total counts equally
- Misinterpreting p-value significance
