Challenge - 5 Problems
KS Test Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of KS test on two identical samples
What is the output of the Kolmogorov-Smirnov test when comparing two identical samples using scipy.stats.ks_2samp?
SciPy
from scipy.stats import ks_2samp sample = [1, 2, 3, 4, 5] result = ks_2samp(sample, sample) print((round(result.statistic, 2), round(result.pvalue, 2)))
Attempts:
2 left
💡 Hint
Think about what happens when two samples are exactly the same.
✗ Incorrect
When two samples are identical, the maximum difference between their cumulative distributions is zero, so the KS statistic is 0. The p-value is 1, indicating no evidence to reject the null hypothesis that samples come from the same distribution.
❓ data_output
intermediate1:30remaining
Number of items in KS test result object
How many attributes does the result object returned by scipy.stats.ks_2samp contain?
SciPy
from scipy.stats import ks_2samp sample1 = [1, 2, 3] sample2 = [4, 5, 6] result = ks_2samp(sample1, sample2) print(len(result._fields))
Attempts:
2 left
💡 Hint
Check the attributes of the namedtuple returned by ks_2samp.
✗ Incorrect
The ks_2samp function returns a namedtuple with two fields: statistic and pvalue.
🧠 Conceptual
advanced1:30remaining
Interpretation of KS test p-value
If the Kolmogorov-Smirnov test returns a p-value of 0.03 when comparing two samples, what does this mean?
Attempts:
2 left
💡 Hint
Recall what a p-value less than 0.05 usually indicates in hypothesis testing.
✗ Incorrect
A p-value of 0.03 means we reject the null hypothesis at the 5% level, suggesting the samples likely come from different distributions.
🔧 Debug
advanced2:00remaining
Identify error in KS test usage
What error will this code raise?
from scipy.stats import ks_2samp
sample1 = [1, 2, 3]
sample2 = 'abc'
result = ks_2samp(sample1, sample2)
print(result)
Attempts:
2 left
💡 Hint
Check what happens when comparing numeric and string data in ks_2samp.
✗ Incorrect
The ks_2samp function tries to compute differences between numeric values. Passing a string sample causes a TypeError when subtracting int and str.
🚀 Application
expert2:30remaining
Using KS test to compare sample to normal distribution
Which code correctly performs a Kolmogorov-Smirnov test to check if a sample comes from a normal distribution with mean 0 and std 1?
Attempts:
2 left
💡 Hint
Use kstest with distribution name and parameters to test against a theoretical distribution.
✗ Incorrect
The kstest function tests a sample against a given distribution. Passing 'norm' with args=(0,1) tests against standard normal. ks_2samp compares two samples, not sample vs distribution.