0
0
SciPydata~10 mins

Wilcoxon signed-rank test in SciPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Wilcoxon signed-rank test
Start with paired data
Calculate differences
Remove zero differences
Rank absolute differences
Assign signs to ranks
Sum positive and negative ranks
Calculate test statistic W
Compare W to distribution
Output p-value and decision
The test compares paired samples by ranking their differences and checking if median difference is zero.
Execution Sample
SciPy
from scipy.stats import wilcoxon
x = [20, 22, 19, 24, 30]
y = [21, 21, 20, 23, 29]
stat, p = wilcoxon(x, y)
print(stat, p)
Runs Wilcoxon signed-rank test on two paired samples and prints test statistic and p-value.
Execution Table
StepActionCalculation/ConditionResult
1Calculate differencesdiff = x - y[ -1, 1, -1, 1, 1 ]
2Remove zerosNo zeros to remove[ -1, 1, -1, 1, 1 ]
3Absolute differencesabs_diff = [1, 1, 1, 1, 1][1, 1, 1, 1, 1]
4Rank absolute differencesAll equal, ranks = [3.0,3.0,3.0,3.0,3.0][3.0, 3.0, 3.0, 3.0, 3.0]
5Assign signs to ranksSigned ranks = [-3.0, +3.0, -3.0, +3.0, +3.0][-3.0, 3.0, -3.0, 3.0, 3.0]
6Sum positive ranksSum_pos = 3.0 + 3.0 + 3.09.0
7Sum negative ranksSum_neg = -3.0 + -3.0-6.0
8Test statistic WW = min(Sum_pos, abs(Sum_neg))6.0
9Calculate p-valueUsing Wilcoxon distributionp = 0.6875
10Decisionp > 0.05Fail to reject null hypothesis
💡 Test ends after p-value calculation and decision based on significance level
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8Final
diffN/A[-1, 1, -1, 1, 1][-1, 1, -1, 1, 1][-1, 1, -1, 1, 1][-1, 1, -1, 1, 1][-1, 1, -1, 1, 1][-1, 1, -1, 1, 1][-1, 1, -1, 1, 1][-1, 1, -1, 1, 1]
abs_diffN/AN/A[1, 1, 1, 1, 1][1, 1, 1, 1, 1][1, 1, 1, 1, 1][1, 1, 1, 1, 1][1, 1, 1, 1, 1][1, 1, 1, 1, 1][1, 1, 1, 1, 1]
ranksN/AN/AN/A[3.0, 3.0, 3.0, 3.0, 3.0][3.0, 3.0, 3.0, 3.0, 3.0][3.0, 3.0, 3.0, 3.0, 3.0][3.0, 3.0, 3.0, 3.0, 3.0][3.0, 3.0, 3.0, 3.0, 3.0][3.0, 3.0, 3.0, 3.0, 3.0]
signed_ranksN/AN/AN/AN/A[-3.0, 3.0, -3.0, 3.0, 3.0][-3.0, 3.0, -3.0, 3.0, 3.0][-3.0, 3.0, -3.0, 3.0, 3.0][-3.0, 3.0, -3.0, 3.0, 3.0][-3.0, 3.0, -3.0, 3.0, 3.0]
sum_posN/AN/AN/AN/AN/A9.09.09.09.0
sum_negN/AN/AN/AN/AN/AN/A-6.0-6.0-6.0
WN/AN/AN/AN/AN/AN/AN/A6.06.0
p-valueN/AN/AN/AN/AN/AN/AN/AN/A0.6875
Key Moments - 3 Insights
Why do we rank the absolute differences instead of the raw differences?
Ranking absolute differences (see Step 4 in execution_table) allows us to measure the size of differences ignoring direction, which is needed before assigning signs back.
What happens if some differences are zero?
Zero differences are removed before ranking (Step 2). They do not affect the test statistic because they show no change.
Why do we take the minimum of sum of positive and negative ranks as test statistic W?
The test statistic W (Step 8) is the smaller sum to measure how much ranks lean to one side, indicating if median difference is zero or not.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 5. What is the signed rank for the third difference?
A-3.0
B1.5
C0
D3.0
💡 Hint
Check the 'Assign signs to ranks' row in execution_table, third value in signed ranks array.
At which step does the test remove zero differences?
AStep 1
BStep 4
CStep 2
DStep 6
💡 Hint
Look for 'Remove zeros' action in execution_table.
If one difference was zero, how would the number of ranks change?
ARanks would increase by one
BRanks would decrease by one
CRanks would stay the same
DRanks would become zero
💡 Hint
Zero differences are removed before ranking (Step 2), so ranks count decreases.
Concept Snapshot
Wilcoxon signed-rank test compares paired samples.
Calculate differences, remove zeros.
Rank absolute differences.
Sum ranks by sign.
Test statistic W = smaller sum.
Use p-value to decide if median difference is zero.
Full Transcript
The Wilcoxon signed-rank test compares two related samples to check if their median difference is zero. First, it calculates the difference between paired values. Then it removes any zero differences because they don't affect the test. Next, it ranks the absolute values of these differences. After ranking, it assigns the original signs back to the ranks. The sums of positive and negative signed ranks are calculated. The test statistic W is the smaller of these sums. Finally, the test calculates a p-value from W to decide if the difference is statistically significant. If the p-value is greater than 0.05, we do not reject the null hypothesis, meaning no significant difference is found.