Wilcoxon Rank-Sum W Statistic
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Two independent samples are stored in `sample_a` (the smaller sample) and `sample_b`. Run a two-sided Wilcoxon rank-sum test against a table of critical values.
Pool both samples and rank every value together, from 1 for the smallest to $N$ for the largest. Tied values all receive the average of the positions they occupy (mid-ranks). The statistic $W$ is the sum of the ranks that belong to `sample_a`. Then look up the row of `critical_values` whose `n1` is the size of `sample_a` and whose `n2` is the size of `sample_b`. The decision is `'reject'` when `w_statistic <= w_lower` or `w_statistic >= w_upper`, and `'accept'` otherwise.
```hint Average ranks
Window functions can give you both the first position a tie group occupies and the number of rows in that group. The average position follows from those two numbers.
```
### Input Table(s)
Table `sample_a`
| Column | Type | Meaning |
|---|---|---|
| value | INTEGER | One observation from the first sample |
Table `sample_b`
| Column | Type | Meaning |
|---|---|---|
| value | INTEGER | One observation from the second sample |
Neither sample table has a key. Repeated values are allowed and every row is one observation.
Table `critical_values`
| Column | Type | Meaning |
|---|---|---|
| n1 | INTEGER | Size of `sample_a` |
| n2 | INTEGER | Size of `sample_b` |
| w_lower | NUMERIC(8,1) | Reject when $W$ is at or below this value |
| w_upper | NUMERIC(8,1) | Reject when $W$ is at or above this value |
Primary key: (`n1`, `n2`).
### Output Contract
Exactly one row with these columns, in this order:
| Column | Meaning |
|---|---|
| w_statistic | $W$, rounded to 1 decimal |
| w_lower | `w_lower` from the matching `critical_values` row |
| w_upper | `w_upper` from the matching `critical_values` row |
| decision | `'reject'` or `'accept'`, by the rule above |
The result has one row, so it needs no ordering.
### Example
`sample_a`:
| value |
|---|
| 11 |
| 9 |
| 4 |
| 11 |
| 16 |
| 10 |
`sample_b`:
| value |
|---|
| 6 |
| 7 |
| 6 |
| 18 |
| 5 |
| 19 |
`critical_values`:
| n1 | n2 | w_lower | w_upper |
|---|---|---|---|
| 6 | 6 | 26.0 | 52.0 |
| 8 | 12 | 58.0 | 110.0 |
Expected output:
| w_statistic | w_lower | w_upper | decision |
|---|---|---|---|
| 41.0 | 26.0 | 52.0 | accept |
The pooled values in order are 4, 5, 6, 6, 7, 9, 10, 11, 11, 16, 18, 19. The two 6s occupy positions 3 and 4, so each gets 3.5, and the two 11s occupy positions 8 and 9, so each gets 8.5. The `sample_a` values 4, 9, 10, 11, 11 and 16 have ranks 1, 6, 7, 8.5, 8.5 and 10, which sum to 41. Both samples have 6 rows, the bounds for (6, 6) are 26 and 52, and 41 lies strictly between them, so the decision is accept.
### Constraints and Clarifications
- Neither sample contains NULLs, and each has at least one row.
- `sample_a` never has more rows than `sample_b`.
- `critical_values` always has exactly one row for the observed pair of sizes.
- Equal values are ties whether they come from the same sample or from different samples, and they all share one average rank.
- $W$ 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 runs a two-sided Wilcoxon rank-sum test: pool two samples, give tied values their average rank, sum the first sample's ranks and compare the total with tabulated critical values. It tests window functions, tie handling and joining on computed sample sizes.