Wilcoxon Signed-Rank W Statistic
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Table `pairs` holds one row per subject with a measurement taken before and after a treatment. Compute the Wilcoxon signed-rank statistic for the paired differences and test it against the critical values in `signed_rank_critical`. Return one row with `w_statistic`, `n_eff` and `decision`.
1. For each subject the difference is `after_val - before_val`. Drop subjects whose difference is 0.
2. Rank the absolute values of the remaining differences, from 1 for the smallest upward. Tied absolute values all receive the average of the positions they occupy (mid-ranks).
3. $W^+$ is the sum of the ranks of the positive differences, and $W^-$ is the sum of the ranks of the negative differences.
4. `w_statistic` is the smaller of $W^+$ and $W^-$, rounded to 1 decimal.
5. `n_eff` is the number of non-zero differences. Look up `critical_value` in the row of `signed_rank_critical` where `n = n_eff`.
6. `decision` is `'reject'` when `w_statistic <= critical_value`, and `'accept'` otherwise.
```hint Check the all-same-sign case
Test your query on data where every non-zero difference has the same sign, and look at what each of your rank sums returns.
```
### Input Table(s)
Table `pairs`
| Column | Type | Meaning |
|---|---|---|
| before_val | INTEGER | Measurement before the treatment |
| after_val | INTEGER | Measurement after the treatment |
Each row is one subject. The table has no key column.
Table `signed_rank_critical`
| Column | Type | Meaning |
|---|---|---|
| n | INTEGER | Number of non-zero differences |
| critical_value | INTEGER | Largest statistic that leads to rejection at this `n`; -1 means no statistic can reject |
Primary key: `n`.
### Output Contract
Exactly one row with these columns, in this order:
| Column | Meaning |
|---|---|
| w_statistic | The smaller of $W^+$ and $W^-$, rounded to 1 decimal |
| n_eff | Count of subjects with a non-zero difference |
| decision | `'reject'` or `'accept'`, by the rule above |
The result has one row, so it needs no ordering.
### Example
`pairs`:
| before_val | after_val |
|---|---|
| 3 | 3 |
| 7 | 5 |
| 1 | 11 |
| 1 | 5 |
| 1 | 10 |
| 2 | 7 |
| 5 | 9 |
| 6 | 6 |
| 6 | 7 |
| 9 | 5 |
`signed_rank_critical`:
| n | critical_value |
|---|---|
| 1 | -1 |
| 2 | -1 |
| 3 | -1 |
| 4 | -1 |
| 5 | -1 |
| 6 | 0 |
| 7 | 2 |
| 8 | 3 |
| 9 | 5 |
| 10 | 8 |
| 11 | 10 |
| 12 | 13 |
| 13 | 17 |
| 14 | 21 |
| 15 | 25 |
| 16 | 29 |
| 17 | 34 |
| 18 | 40 |
| 19 | 46 |
| 20 | 52 |
Expected output:
| w_statistic | n_eff | decision |
|---|---|---|
| 6.0 | 8 | accept |
The subjects (3, 3) and (6, 6) have a difference of 0 and drop out, which leaves 8 differences: -2, 10, 4, 9, 5, 4, 1 and -4. Their absolute values in order are 1, 2, 4, 4, 4, 5, 9, 10, and the three 4s occupy positions 3 to 5, so each gets rank 4. The positive differences 10, 4, 9, 5, 4 and 1 have ranks 8, 4, 7, 6, 4 and 1, so $W^+ = 30$. The negative differences -2 and -4 have ranks 2 and 4, so $W^- = 6$. The statistic is 6, the critical value for $n = 8$ is 3, and 6 is above 3, so the decision is accept.
### Constraints and Clarifications
- `before_val` and `after_val` are never NULL.
- At least one subject has a non-zero difference, and `n_eff` is at most 20. `signed_rank_critical` has exactly one row for each `n` from 1 to 20.
- Ties are decided on the absolute difference, whatever the sign, so a difference of 4 and a difference of -4 share a rank.
- When every non-zero difference has the same sign, the other rank sum is 0 and `w_statistic` is 0.
- The statistic is always a whole number or a whole number plus one half, so rounding it to 1 decimal never changes its value.
- `'accept'` is the label this output uses for failing to reject the null hypothesis.
Overview: Write a SQL query that computes the Wilcoxon signed-rank statistic for paired before and after measurements and compares it with a table of critical values. It tests handling zero differences, average ranks for tied magnitudes, splitting rank sums by sign and window functions.