Bird
Raised Fist0
SciPydata~20 mins

Goodness of fit evaluation in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Goodness of Fit Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Chi-square test output interpretation

What is the output of the following code that performs a chi-square goodness of fit test?

SciPy
from scipy.stats import chisquare
observed = [20, 30, 50]
expected = [25, 25, 50]
stat, p = chisquare(f_obs=observed, f_exp=expected)
print(round(stat, 2), round(p, 3))
A1.0 0.607
B2.0 0.368
C5.0 0.082
D4.0 0.135
Attempts:
2 left
💡 Hint

Recall the chi-square formula: sum((observed - expected)^2 / expected).

data_output
intermediate
2:00remaining
Kolmogorov-Smirnov test result

Given the following code testing if data fits a normal distribution, what is the output?

SciPy
from scipy.stats import kstest, norm
import numpy as np
data = np.array([1.2, 2.3, 2.9, 3.1, 4.0])
stat, p = kstest(data, 'norm', args=(2.5, 1))
print(round(stat, 3), round(p, 3))
A0.317 0.123
B0.500 0.050
C0.317 0.876
D0.200 0.900
Attempts:
2 left
💡 Hint

Remember the KS test compares empirical and theoretical CDFs.

🔧 Debug
advanced
2:00remaining
Identify the error in chi-square test code

What error does the following code produce?

SciPy
from scipy.stats import chisquare
observed = [10, 20, 30]
expected = [15, 15]
stat, p = chisquare(f_obs=observed, f_exp=expected)
print(stat, p)
ANo error, outputs statistic and p-value
BIndexError: list index out of range
CValueError: operands could not be broadcast together
DTypeError: unsupported operand type(s)
Attempts:
2 left
💡 Hint

Check if observed and expected lists have the same length.

🚀 Application
advanced
2:00remaining
Choosing the right goodness of fit test

You have a dataset of 1000 values and want to check if it follows a uniform distribution. Which scipy test is most appropriate?

Ascipy.stats.kstest with 'uniform' distribution
Bscipy.stats.chisquare with observed frequencies and expected uniform frequencies
Cscipy.stats.ttest_1samp comparing data mean to uniform mean
Dscipy.stats.pearsonr between data and uniform samples
Attempts:
2 left
💡 Hint

Think about tests that compare sample distribution to a continuous theoretical distribution.

🧠 Conceptual
expert
2:00remaining
Interpreting p-values in goodness of fit tests

Which statement best describes the meaning of a p-value of 0.03 in a goodness of fit test?

AThere is a 3% chance the observed data fits the model perfectly
BThere is a 3% probability that the observed data would occur if the model is true
CThere is a 3% chance the null hypothesis is true
DThere is a 3% chance of observing data as extreme as this if the null hypothesis is true
Attempts:
2 left
💡 Hint

Recall the definition of p-value in hypothesis testing.

Practice

(1/5)
1. What does the chi-square goodness of fit test in scipy.stats.chisquare primarily evaluate?
easy
A. The mean difference between two samples
B. How well observed data matches expected frequencies
C. The correlation between two variables
D. The variance within a single dataset

Solution

  1. 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.
  2. Step 2: Identify what scipy.stats.chisquare does

    This function calculates the chi-square statistic and p-value to evaluate the fit between observed and expected counts.
  3. Final Answer:

    How well observed data matches expected frequencies -> Option B
  4. Quick 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
A. import scipy.chisquare
B. from scipy import chisquare
C. from scipy.stats import chisquare
D. import scipy.stats.chisquare as cs

Solution

  1. Step 1: Recall the module structure of scipy

    The chi-square test function is inside the stats submodule of scipy.
  2. Step 2: Identify correct import syntax

    The correct import is from scipy.stats import chisquare to directly access the function.
  3. Final Answer:

    from scipy.stats import chisquare -> Option C
  4. Quick 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
A. 2.0 0.368
B. 3.0 0.223
C. 0.5 0.778
D. 1.0 0.607

Solution

  1. 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.0
  2. Step 2: Interpret p-value from scipy output

    Using scipy.stats.chisquare with these values gives a p-value around 0.368, indicating moderate fit.
  3. Final Answer:

    2.0 0.368 -> Option A
  4. Quick 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
A. Observed and expected arrays have different lengths
B. chisquare function is not imported correctly
C. Expected frequencies must be integers
D. Missing p-value extraction from result

Solution

  1. 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.
  2. Step 2: Understand scipy requirement

    Both observed and expected arrays must be the same length to compare frequencies correctly.
  3. Final Answer:

    Observed and expected arrays have different lengths -> Option A
  4. Quick 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
A. 0.789
B. 0.223
C. 0.456
D. 0.174

Solution

  1. 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].
  2. Step 2: Perform chi-square test with scipy

    Using chisquare(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.
  3. Final Answer:

    0.174 -> Option D
  4. Quick 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