Mann-Whitney U test in SciPy - Time & Space Complexity
We want to understand how the time needed to run the Mann-Whitney U test changes as the size of the input data grows.
Specifically, how does the test's execution time increase when we have more data points?
Analyze the time complexity of the following code snippet.
import numpy as np
from scipy.stats import mannwhitneyu
data1 = np.random.rand(1000)
data2 = np.random.rand(1000)
stat, p = mannwhitneyu(data1, data2)
print(f"U statistic: {stat}, p-value: {p}")
This code runs the Mann-Whitney U test on two arrays of random numbers, each with 1000 elements, to compare their distributions.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing and ranking all combined elements from both input arrays.
- How many times: The algorithm processes all elements together, so it handles about n + m elements, where n and m are the sizes of the two arrays.
As the number of data points increases, the time to rank and compare grows faster than just adding more elements.
| Input Size (n = m) | Approx. Operations |
|---|---|
| 10 | About 20 elements ranked and compared |
| 100 | About 200 elements ranked and compared |
| 1000 | About 2000 elements ranked and compared |
Pattern observation: The work grows roughly proportional to the total number of elements times the log of that number, because sorting is involved.
Time Complexity: O((n + m) log(n + m))
This means the time to run the test grows a bit faster than just the total number of data points, mainly because it sorts all the data combined.
[X] Wrong: "The Mann-Whitney U test runs in linear time because it just compares elements once."
[OK] Correct: The test needs to sort or rank all combined data points, which takes more time than just one pass through the data.
Understanding how sorting affects the time needed for statistical tests like Mann-Whitney U helps you explain your approach clearly and shows you know how data size impacts performance.
"What if one input array is much smaller than the other? How would that affect the time complexity?"
Practice
Mann-Whitney U test in data analysis?Solution
Step 1: Understand the test purpose
The Mann-Whitney U test is a non-parametric test used to compare two independent groups.Step 2: Identify the key feature
It does not assume the data follows a normal distribution, unlike t-tests.Final Answer:
To compare two independent groups without assuming normal distribution -> Option BQuick Check:
Non-parametric comparison = C [OK]
- Confusing it with correlation tests
- Assuming it compares means only
- Using it for more than two groups
Solution
Step 1: Recall scipy.stats module functions
The Mann-Whitney U test function is namedmannwhitneyuand is insidescipy.stats.Step 2: Check correct import syntax
The correct Python import syntax isfrom scipy.stats import mannwhitneyu.Final Answer:
from scipy.stats import mannwhitneyu -> Option DQuick Check:
Correct import syntax = B [OK]
- Using wrong module or function name
- Incorrect import syntax
- Trying to import with 'import ... as' incorrectly
from scipy.stats import mannwhitneyu x = [1, 3, 5] y = [2, 4, 6] stat, p = mannwhitneyu(x, y) print(round(stat, 2), round(p, 3))
Solution
Step 1: Understand the data and test
Lists x and y have values [1,3,5] and [2,4,6]. Mann-Whitney U test compares ranks between these two groups.Step 2: Calculate Mann-Whitney U statistic and p-value
Using scipy, the U statistic is 3.0 and p-value is 0.5 (two-sided test by default).Final Answer:
3.0 0.5 -> Option AQuick Check:
U=3.0, p=0.5 from scipy output [OK]
- Mixing up U statistic with p-value
- Rounding incorrectly
- Assuming p-value is always small
from scipy.stats import mannwhitneyu x = [1, 2, 3] y = [4, 5, 6] stat, p = mannwhitneyu(x, y, alternative='greater') print(stat, p)
Solution
Step 1: Check function parameters
Themannwhitneyufunction accepts analternativeparameter with values 'two-sided', 'less', or 'greater'.Step 2: Verify code correctness
Usingalternative='greater'is valid and the function call is correct.Final Answer:
No error; code runs correctly -> Option CQuick Check:
Correct parameter usage = D [OK]
- Using wrong alternative string case
- Trying to use non-existent function names
- Assuming default alternative is 'greater'
a = [2, 3, 5, 7] and b = [1, 4, 6, 8]. You want to test if sample a tends to have higher values than b. Which scipy Mann-Whitney U test call is correct?Solution
Step 1: Define the hypothesis direction
We want to test ifatends to have higher values thanb, so the alternative hypothesis is 'greater'.Step 2: Choose correct function call
The call should bemannwhitneyu(a, b, alternative='greater')to test ifais stochastically greater thanb.Final Answer:
mannwhitneyu(a, b, alternative='greater') -> Option AQuick Check:
Test if a > b = A [OK]
- Swapping sample order and alternative incorrectly
- Using 'less' when testing for greater
- Choosing 'two-sided' when direction matters
Ready to prove it?
Pass the test and this pattern moves unknown
