Compute a two-sided permutation test p-value for a difference in means in SQL

Quick Overview

Write a SQL query that computes the empirical two-sided p-value of a permutation test for a difference in group means, using an observations table and a table of pre-generated label shuffles. It tests conditional aggregation per permutation, exact handling of ties, and safe division and rounding in PostgreSQL.

Compute a two-sided permutation test p-value for a difference in means in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `obs` has `obs_id`, numeric `value`, and `orig_group` (`'A'` or `'B'`). Seeded table `shuffles` (generated in advance with a fixed random seed) assigns, per `perm_id`, every `obs_id` a `label` (`'A'` or `'B'`). Compute the observed mean difference (mean A minus mean B by `orig_group`) and each permutation's mean difference by `label`, then return `p_value` rounded to 4 decimals: the fraction of permutations whose absolute difference is greater than or equal to the absolute observed difference. ```hint Exact ties The rule is "greater than or equal", so a permutation that lands exactly on the observed difference counts. Check that your comparison still sees that tie when the two means come from groups of different sizes. ``` ### Input Table(s) Table `obs`: | Column | Type | Meaning | |---|---|---| | `obs_id` | INT | Observation identifier | | `value` | INT | Measured value | | `orig_group` | VARCHAR(5) | Group the observation really belongs to, `'A'` or `'B'` | Primary key: `obs_id`. Table `shuffles`: | Column | Type | Meaning | |---|---|---| | `perm_id` | INT | Permutation identifier | | `obs_id` | INT | Observation, references `obs.obs_id` | | `label` | VARCHAR(5) | Group this permutation assigns to the observation, `'A'` or `'B'` | Primary key: (`perm_id`, `obs_id`). Every `perm_id` has exactly one row for every `obs_id`. ### Output Contract One row with one column: | Column | Type | Meaning | |---|---|---| | `p_value` | NUMERIC | (permutations with absolute difference at least the absolute observed difference) / (number of permutations), rounded to 4 decimal places | The result always has exactly one row, so no ordering is needed. ### Example Input `obs`: | obs_id | value | orig_group | |---|---|---| | 1 | 12 | A | | 2 | 15 | A | | 3 | 9 | A | | 4 | 20 | B | | 5 | 7 | B | | 6 | 14 | B | Input `shuffles`: | perm_id | obs_id | label | |---|---|---| | 1 | 1 | A | | 1 | 2 | A | | 1 | 3 | A | | 1 | 4 | B | | 1 | 5 | B | | 1 | 6 | B | | 2 | 1 | A | | 2 | 2 | A | | 2 | 3 | B | | 2 | 4 | A | | 2 | 5 | B | | 2 | 6 | B | | 3 | 1 | A | | 3 | 2 | B | | 3 | 3 | A | | 3 | 4 | B | | 3 | 5 | A | | 3 | 6 | B | | 4 | 1 | B | | 4 | 2 | A | | 4 | 3 | A | | 4 | 4 | B | | 4 | 5 | A | | 4 | 6 | B | | 5 | 1 | A | | 5 | 2 | B | | 5 | 3 | B | | 5 | 4 | A | | 5 | 5 | A | | 5 | 6 | B | | 6 | 1 | B | | 6 | 2 | B | | 6 | 3 | A | | 6 | 4 | A | | 6 | 5 | B | | 6 | 6 | A | | 7 | 1 | A | | 7 | 2 | A | | 7 | 3 | B | | 7 | 4 | B | | 7 | 5 | A | | 7 | 6 | B | | 8 | 1 | B | | 8 | 2 | A | | 8 | 3 | B | | 8 | 4 | A | | 8 | 5 | B | | 8 | 6 | A | | 9 | 1 | A | | 9 | 2 | B | | 9 | 3 | A | | 9 | 4 | A | | 9 | 5 | B | | 9 | 6 | B | | 10 | 1 | B | | 10 | 2 | A | | 10 | 3 | A | | 10 | 4 | A | | 10 | 5 | B | | 10 | 6 | B | Output: | p_value | |---| | 0.9000 | The observed means are 12 for A and about 13.667 for B, so the observed difference is about -1.667. Nine of the ten permutations have an absolute difference of at least 1.667 (permutations 1 and 9 tie it exactly). Only permutation 5, with a difference of about 0.333, falls short, so the p-value is 9 / 10. ### Constraints and Clarifications - `obs_id` is unique in `obs`, and (`perm_id`, `obs_id`) is unique in `shuffles`, so there are no duplicate rows to remove. - `value`, `orig_group` and `label` are never NULL, and the labels are exactly `'A'` and `'B'`. - The original grouping and every permutation have at least one A and one B, so every mean is defined. Group sizes in a permutation may differ from the original ones. - The denominator is the number of distinct `perm_id` values. The observed labeling is not added on top; it counts only if it appears in `shuffles`, as permutation 1 does in the example. - Ties count as extreme. Compare the absolute differences exactly, so a permutation whose absolute difference equals the observed one is counted. - Round only the final fraction, to 4 decimal places.

Overview: Write a SQL query that computes the empirical two-sided p-value of a permutation test for a difference in group means, using an observations table and a table of pre-generated label shuffles. It tests conditional aggregation per permutation, exact handling of ties, and safe division and rounding in PostgreSQL.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Table obs has obs_id, numeric value, and orig_group ('A' or 'B'). Seeded table shuffles (generated in advance with a fixed random seed) assigns, per perm_id, every obs_id a label ('A' or 'B'). Compute the observed mean difference (mean A minus mean B by orig_group) and each permutation's mean difference by label, then return p_value rounded to 4 decimals: the fraction of permutations whose absolute difference is greater than or equal to the absolute observed difference.

Input Table(s)

Table obs:

ColumnTypeMeaning
obs_idINTObservation identifier
valueINTMeasured value
orig_groupVARCHAR(5)Group the observation really belongs to, 'A' or 'B'

Primary key: obs_id.

Table shuffles:

ColumnTypeMeaning
perm_idINTPermutation identifier
obs_idINTObservation, references obs.obs_id
labelVARCHAR(5)Group this permutation assigns to the observation, 'A' or 'B'

Primary key: (perm_id, obs_id). Every perm_id has exactly one row for every obs_id.

Output Contract

One row with one column:

ColumnTypeMeaning
p_valueNUMERIC(permutations with absolute difference at least the absolute observed difference) / (number of permutations), rounded to 4 decimal places

The result always has exactly one row, so no ordering is needed.

Example

Input obs:

obs_idvalueorig_group
112A
215A
39A
420B
57B
614B

Input shuffles:

perm_idobs_idlabel
11A
12A
13A
14B
15B
16B
21A
22A
23B
24A
25B
26B
31A
32B
33A
34B
35A
36B
41B
42A
43A
44B
45A
46B
51A
52B
53B
54A
55A
56B
61B
62B
63A
64A
65B
66A
71A
72A
73B
74B
75A
76B
81B
82A
83B
84A
85B
86A
91A
92B
93A
94A
95B
96B
101B
102A
103A
104A
105B
106B

Output:

p_value
0.9000

The observed means are 12 for A and about 13.667 for B, so the observed difference is about -1.667. Nine of the ten permutations have an absolute difference of at least 1.667 (permutations 1 and 9 tie it exactly). Only permutation 5, with a difference of about 0.333, falls short, so the p-value is 9 / 10.

Constraints and Clarifications

  • obs_id is unique in obs , and ( perm_id , obs_id ) is unique in shuffles , so there are no duplicate rows to remove.
  • value , orig_group and label are never NULL, and the labels are exactly 'A' and 'B' .
  • The original grouping and every permutation have at least one A and one B, so every mean is defined. Group sizes in a permutation may differ from the original ones.
  • The denominator is the number of distinct perm_id values. The observed labeling is not added on top; it counts only if it appears in shuffles , as permutation 1 does in the example.
  • Ties count as extreme. Compare the absolute differences exactly, so a permutation whose absolute difference equals the observed one is counted.
  • Round only the final fraction, to 4 decimal places.