Kolmogorov-Smirnov test in SciPy - Time & Space Complexity
We want to understand how the time needed to run the Kolmogorov-Smirnov test changes as the size of the data grows.
How does the test's execution time increase when we have more data points?
Analyze the time complexity of the following code snippet.
from scipy import stats
# Sample data arrays
sample1 = [1.2, 2.3, 3.1, 4.7, 5.5]
sample2 = [1.0, 2.5, 3.0, 4.8, 5.2]
# Perform Kolmogorov-Smirnov test
result = stats.ks_2samp(sample1, sample2)
print(result)
This code compares two samples to check if they come from the same distribution using the Kolmogorov-Smirnov test.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Sorting both input arrays and comparing their cumulative distributions.
- How many times: Each array is traversed multiple times during sorting and comparison, roughly proportional to the size of the arrays.
As the number of data points increases, the time to sort and compare grows faster than just adding more points.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 30 operations |
| 100 | About 700 operations |
| 1000 | About 10,000 operations |
Pattern observation: The operations grow a bit faster than the input size, roughly like n log n, because of sorting.
Time Complexity: O(n log n)
This means the time to run the test grows a bit faster than the number of data points, mainly due to sorting.
[X] Wrong: "The test runs in linear time because it just compares values."
[OK] Correct: The test needs to sort the data first, which takes more time than just comparing values one by one.
Understanding how sorting affects time helps you explain why some tests take longer with bigger data, showing you know how algorithms work behind the scenes.
"What if the input arrays were already sorted? How would the time complexity change?"
Practice
scipy.stats.ks_2samp function in data analysis?Solution
Step 1: Understand the function purpose
ks_2sampis designed to compare two independent samples.Step 2: Identify what it tests
It tests if the two samples come from the same continuous distribution.Final Answer:
To compare two samples and check if they come from the same distribution -> Option DQuick Check:
Kolmogorov-Smirnov test compares distributions [OK]
- Confusing KS test with mean calculation
- Thinking KS test performs regression
- Assuming KS test generates random data
Solution
Step 1: Recall correct import syntax
Functions inside scipy.stats are imported usingfrom scipy.stats import function_name.Step 2: Match the function name
The function for KS two-sample test isks_2samp, so the correct import isfrom scipy.stats import ks_2samp.Final Answer:
from scipy.stats import ks_2samp -> Option CQuick Check:
Correct import uses 'from scipy.stats import' [OK]
- Using 'import scipy.ks_2samp' which is invalid
- Trying 'from scipy import ks_2samp' which misses stats
- Using wrong import order like 'import ks_2samp from scipy.stats'
from scipy.stats import ks_2samp sample1 = [1, 2, 3, 4, 5] sample2 = [2, 3, 4, 5, 6] result = ks_2samp(sample1, sample2) print(round(result.pvalue, 3))
Solution
Step 1: Understand the samples and test
Samples are close but not identical; KS test compares their distributions.Step 2: Run the KS test and check p-value
Running the code shows p-value approximately 0.999, indicating no strong evidence to reject similarity.Final Answer:
0.999 -> Option AQuick Check:
Close samples give very high p-value ~0.999 [OK]
- Assuming p-value is always very small for different samples
- Confusing p-value with test statistic
- Rounding errors leading to wrong choice
from scipy.stats import ks_2samp sample1 = [1, 2, 3] sample2 = [4, 5, 6] result = ks_2samp(sample1) print(result.statistic)
Solution
Step 1: Check function call parameters
ks_2samprequires two sample arguments, but only one is given.Step 2: Identify the error cause
Missing second sample causes a TypeError because the function expects two inputs.Final Answer:
Missing second sample argument in ks_2samp call -> Option BQuick Check:
ks_2samp needs two samples [OK]
- Forgetting the second sample argument
- Thinking print causes error
- Assuming integer samples cause error
Solution
Step 1: Understand the data and goal
We want to check if two samples (wait times) differ in distribution, not just means.Step 2: Choose appropriate test
ks_2sampcompares full distributions without assuming normality, ideal for wait times.Step 3: Evaluate other options
Mean subtraction ignores distribution shape; regression is unrelated;ttest_indassumes normality which may not hold.Final Answer:
Useks_2sampto compare the two samples' distributions -> Option AQuick Check:
KS test compares distributions best for this case [OK]
- Using mean difference which ignores distribution shape
- Applying t-test without checking normality
- Using regression for distribution comparison
Ready to prove it?
Pass the test and this pattern moves unknown
