Rank-Biserial Effect Size from Mann-Whitney U
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Table `obs` records an integer `value` for `group_name` `'A'` or `'B'`. Compute the Mann-Whitney U for group A using average (mid) ranks for ties, then the rank-biserial correlation
$$
r = 1 - \frac{2U}{n_1 n_2}
$$
where $n_1$ and $n_2$ are the sizes of groups A and B. Return `u_statistic` rounded to 1 decimal and `rank_biserial` rounded to 4 decimals.
```hint Getting U first
U for group A comes from group A's sum of pooled ranks. Work out how a tied block's shared rank relates to what `RANK()` returns and the size of the block.
```
### 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 |
### Output Contract
- Exactly one row with the columns `u_statistic`, `rank_biserial`, in that order. The result has a single row, so no ordering is needed.
- `u_statistic` is group A's U, $R_1 - n_1(n_1 + 1)/2$ with $R_1$ the sum of group A's ranks, rounded to 1 decimal place.
- `rank_biserial` is ${1 - 2U/(n_1 n_2)}$ computed from the unrounded U, rounded to 4 decimal places.
### Example
`obs`:
| group_name | value |
|---|---|
| A | 12 |
| A | 11 |
| A | 7 |
| B | 4 |
| B | 8 |
| B | 15 |
| A | 6 |
| A | 4 |
| B | 11 |
| B | 10 |
| B | 8 |
| A | 4 |
Expected result:
| u_statistic | rank_biserial |
|---|---|
| 12.5 | 0.3056 |
Each group has 6 rows. In the pooled ranking the three 4s share rank 2 and the two 11s share 9.5, so group A's ranks are 2, 2, 4, 5, 9.5 and 11, which sum to 33.5. Then U = 33.5 - 21 = 12.5 and r = 1 - 25/36 = 11/36, which rounds to 0.3056.
### Constraints and Clarifications
- Rank all rows of both groups together in ascending order of `value`. Tied rows, in either group, get the mean of the positions they occupy.
- Use the formula as given. With this sign convention, a positive `rank_biserial` means group B tends to have the larger values.
- 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.
Overview: Write a SQL query that computes the Mann-Whitney U statistic for one group with average ranks for ties, then converts it into the rank-biserial correlation as an effect size. It tests tie-aware ranking with window functions, safe numeric division, and how to read the sign and size of a rank-based effect.