Mann-Whitney U Normal-Approximation Z-Test
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
The table `obs` holds integer observations `value` from two groups, `group_name` = `'A'` or `'B'`. Compute the Mann-Whitney U statistic for group A, using average (mid) ranks for ties, then its normal-approximation z-score. Return one row with `z_statistic`, `critical_value` and `decision`.
The z-score is
$$
z = \frac{U - n_1 n_2 / 2}{\sqrt{n_1 n_2 (n_1 + n_2 + 1) / 12}}
$$
where $n_1$ and $n_2$ are the sizes of groups A and B. The critical value is the two-tailed z stored in `critical_values` for `alpha` = 0.05, and the decision is `'reject'` when $|z|$ exceeds it, otherwise `'accept'`.
```hint Ranking with ties
Rank the two groups together. For a block of tied values, think about which window function gives the block's first position, and how far the block's average position sits above it.
```
### Input Table(s)
Table `obs` (no primary key; each row is one observation, and the same group and value can repeat):
| Column | Type | Meaning |
|---|---|---|
| group_name | VARCHAR(5) | Group label, `'A'` or `'B'` |
| value | INT | The observed value |
Table `critical_values` (primary key `alpha`):
| Column | Type | Meaning |
|---|---|---|
| alpha | DECIMAL(4,3) | Significance level |
| critical_value | DECIMAL(8,4) | Two-tailed standard normal cutoff for that level |
### Output Contract
- Exactly one row with the columns `z_statistic`, `critical_value`, `decision`, in that order. The result has a single row, so no ordering is needed.
- `z_statistic` is z rounded to 4 decimal places.
- `critical_value` is the stored value for `alpha` = 0.05, unchanged.
- `decision` is `'reject'` when the unrounded $|z|$ is strictly greater than `critical_value`, else `'accept'`.
### Example
`obs`:
| group_name | value |
|---|---|
| A | 10 |
| B | 5 |
| A | 4 |
| B | 18 |
| A | 4 |
| B | 18 |
| A | 8 |
| B | 5 |
| A | 7 |
| A | 10 |
| A | 4 |
| B | 14 |
| B | 15 |
`critical_values`:
| alpha | critical_value |
|---|---|
| 0.05 | 1.96 |
| 0.01 | 2.5758 |
Expected result:
| z_statistic | critical_value | decision |
|---|---|---|
| -1.8571 | 1.96 | accept |
Group A has 7 rows and group B has 6. In the pooled ranking the three 4s share rank 2, the two 5s share 4.5, the two 10s share 8.5 and the two 18s share 12.5, so group A's rank sum is 36 and U = 36 - 28 = 8. The mean is 21 and the standard deviation is 7, so z = -13/7, which rounds to -1.8571. Its absolute value is below 1.96, so the decision is `'accept'`.
### Constraints and Clarifications
- Rank all rows of both groups together in ascending order of `value`. Tied rows get the mean of the positions they occupy.
- U is group A's statistic: $U = R_1 - n_1(n_1 + 1)/2$, where $R_1$ is the sum of group A's ranks.
- Use the variance exactly as given, with no tie correction and no continuity correction.
- Duplicate rows are separate observations, and each one is ranked.
- `group_name` and `value` are never NULL, `group_name` is only `'A'` or `'B'`, and both groups have at least one row.
- `critical_values` has exactly one row with `alpha` = 0.05. Ignore the other rows.
- A z whose absolute value equals the critical value exactly is `'accept'`.
Overview: Compute the Mann-Whitney U statistic for one group in SQL using average ranks for tied values, then convert it to a normal-approximation z-score and a two-tailed decision at alpha 0.05. It tests tie-aware ranking with window functions, careful numeric division, and how the rank-sum test works.