Quick Overview

Write a SQL query that computes a 95% bootstrap percentile confidence interval for the difference in medians between two groups, using a table of pre-drawn resamples. It tests window-function medians without PERCENTILE_CONT, pivoting per-group results into one row, CUME_DIST bounds, and reading significance from an interval.

Bootstrap Confidence Interval for Difference in Medians

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: hard

Interview Round: Other

The `obs` table holds `obs_id`, `group_name` (`'A'` or `'B'`) and `value`. The seeded `resamples` table lists three sampled `obs_id` values (drawn with replacement) per `boot_id` and `group_name`. For each `boot_id`, compute the median of group A minus the median of group B, then report a 95% percentile confidence interval over these differences. - `lower_ci` is the smallest difference whose `CUME_DIST` is at least 0.025, rounded to 2 decimals. - `upper_ci` is the largest difference whose `CUME_DIST` is at most 0.975, rounded to 2 decimals. - `decision` is `'significant'` when `lower_ci > 0` or `upper_ci < 0`, and `'not significant'` otherwise. `CUME_DIST` is computed over the per-replicate differences in ascending order. ```hint Work in stages A window function result can only be filtered in a later step. Build one median per boot_id and group, then one difference per boot_id, then the bounds. ``` ### Input Table(s) `obs` | Column | Type | Meaning | |---|---|---| | obs_id | INT | Observation id | | group_name | VARCHAR(5) | `'A'` or `'B'` | | value | INT | The observed value | Primary key: `obs_id`. `resamples` | Column | Type | Meaning | |---|---|---| | boot_id | INT | Bootstrap replicate id | | group_name | VARCHAR(5) | Group the draw belongs to | | slot | INT | Position of the draw within the replicate's group sample (1 to 3) | | obs_id | INT | The drawn observation; references `obs.obs_id` | Primary key: (`boot_id`, `group_name`, `slot`). ### Output Contract - Exactly one row with the columns `lower_ci`, `upper_ci`, `decision`, in that order. - `lower_ci` and `upper_ci` are rounded to 2 decimals. - `decision` is the exact text `significant` or `not significant`. - The result has a single row, so no ordering is needed. ### Example Input `obs`: | obs_id | group_name | value | |---|---|---| | 1 | A | 18 | | 2 | A | 22 | | 3 | A | 25 | | 4 | A | 30 | | 5 | A | 27 | | 6 | B | 12 | | 7 | B | 15 | | 8 | B | 11 | | 9 | B | 19 | | 10 | B | 14 | Input `resamples` has 90 rows: 15 replicates, each with slots 1 to 3 for group A and for group B. The rows for `boot_id` 1 are: | boot_id | group_name | slot | obs_id | |---|---|---|---| | 1 | A | 1 | 4 | | 1 | A | 2 | 3 | | 1 | A | 3 | 2 | | 1 | B | 1 | 8 | | 1 | B | 2 | 10 | | 1 | B | 3 | 7 | The full table, one line per replicate, with the drawn `obs_id` values listed in slot order 1, 2, 3: | boot_id | group A draws | group B draws | |---|---|---| | 1 | 4, 3, 2 | 8, 10, 7 | | 2 | 5, 2, 4 | 9, 7, 7 | | 3 | 3, 3, 3 | 6, 7, 10 | | 4 | 5, 3, 4 | 10, 9, 9 | | 5 | 2, 3, 5 | 6, 6, 8 | | 6 | 2, 1, 4 | 7, 6, 6 | | 7 | 3, 3, 4 | 10, 6, 6 | | 8 | 5, 4, 3 | 10, 9, 6 | | 9 | 4, 5, 2 | 6, 8, 8 | | 10 | 5, 2, 3 | 10, 7, 8 | | 11 | 1, 3, 5 | 10, 6, 10 | | 12 | 4, 5, 1 | 10, 8, 8 | | 13 | 2, 4, 2 | 7, 8, 10 | | 14 | 5, 2, 2 | 9, 8, 8 | | 15 | 2, 5, 5 | 10, 7, 7 | Expected output: | lower_ci | upper_ci | decision | |---|---|---| | 8.00 | 13.00 | significant | In replicate 1, group A draws the values 30, 25 and 22 (median 25) and group B draws 11, 14 and 15 (median 14), so the difference is 11. Sorted, the 15 differences are 8, 8, 10, 11, 11, 11, 11, 11, 12, 12, 13, 13, 13, 16, 16. The value 8 has `CUME_DIST` 2/15 (about 0.133), so it is the smallest difference at or above 0.025. The value 13 has 13/15 (about 0.867) and 16 has 1.0, so 13 is the largest at or below 0.975. Both bounds are above 0, so the result is significant. ### Constraints and Clarifications - Every `resamples.obs_id` exists in `obs`, and its `group_name` matches the row in `obs`. `value` is never NULL. - Every `boot_id` has draws for both A and B. - Draws are with replacement. A repeated `obs_id` inside one replicate (replicate 3 draws `obs_id` 3 three times for group A) counts once per draw. - Each replicate's group sample has three draws, so its median is the middle value. If a sample ever had an even number of draws, its median would be the average of the two middle values. - `CUME_DIST` of a difference is the share of replicates whose difference is less than or equal to it, so tied differences share the same value. - Make the decision from the unrounded bounds. Medians of integer values are multiples of 0.5, so rounding never changes the decision here. - The data guarantee that both bounds exist, that is, at least one difference has `CUME_DIST` at most 0.975.

Overview: Write a SQL query that computes a 95% bootstrap percentile confidence interval for the difference in medians between two groups, using a table of pre-drawn resamples. It tests window-function medians without PERCENTILE_CONT, pivoting per-group results into one row, CUME_DIST bounds, and reading significance from an interval.

The `obs` table holds `obs_id`, `group_name` ('A' or 'B') and `value`. The seeded `resamples` table lists three sampled `obs_id` values (drawn with replacement) per `boot_id` and `group_name`. For each `boot_id`, compute the median of group A minus the median of group B, then report a 95% percentile confidence interval over these differences. - `lower_ci` is the smallest difference whose `CUME_DIST` is at least 0.025, rounded to 2 decimals. - `upper_ci` is the largest difference whose `CUME_DIST` is at most 0.975, rounded to 2 decimals. - `decision` is 'significant' when `lower_ci > 0` or `upper_ci < 0`, and 'not significant' otherwise. `CUME_DIST` is computed over the per-replicate differences in ascending order. ### Input Tables `obs` | Column | Type | Meaning | |---|---|---| | obs_id | INTEGER | Observation id (primary key) | | group_name | VARCHAR(5) | 'A' or 'B' | | value | INTEGER | The observed value | `resamples` | Column | Type | Meaning | |---|---|---| | boot_id | INTEGER | Bootstrap replicate id | | group_name | VARCHAR(5) | Group the draw belongs to | | slot | INTEGER | Position of the draw within the replicate's group sample (1 to 3) | | obs_id | INTEGER | The drawn observation; references `obs.obs_id` | Primary key of `resamples`: (`boot_id`, `group_name`, `slot`). ### Output Contract - Exactly one row with the columns `lower_ci`, `upper_ci`, `decision`, in that order. - `lower_ci` and `upper_ci` are rounded to 2 decimals. - `decision` is the exact text `significant` or `not significant`. - The result has a single row, so no ordering is needed. ### Constraints and Clarifications - Every `resamples.obs_id` exists in `obs`, and its `group_name` matches the row in `obs`. `value` is never NULL. - Every `boot_id` has draws for both A and B. - Draws are with replacement. A repeated `obs_id` inside one replicate (replicate 3 of the sample data draws `obs_id` 3 three times for group A) counts once per draw. - Each replicate's group sample has three draws, so its median is the middle value. If a sample ever had an even number of draws, its median would be the average of the two middle values. - `CUME_DIST` of a difference is the share of replicates whose difference is less than or equal to it, so tied differences share the same value. - Make the decision from the unrounded bounds. Medians of integer values are multiples of 0.5, so rounding never changes the decision here. - The data guarantee that both bounds exist, that is, at least one difference has `CUME_DIST` at most 0.975. ### Example The sample data has 15 replicates. In replicate 1, group A draws `obs_id` 4, 3 and 2 (values 30, 25 and 22, median 25) and group B draws 8, 10 and 7 (values 11, 14 and 15, median 14), so the difference is 11. Sorted, the 15 differences are 8, 8, 10, 11, 11, 11, 11, 11, 12, 12, 13, 13, 13, 16, 16. The value 8 has `CUME_DIST` 2/15 (about 0.133), so it is the smallest difference at or above 0.025. The value 13 has 13/15 (about 0.867) and 16 has 1.0, so 13 is the largest at or below 0.975. Both bounds are above 0, so the expected output is `lower_ci` 8.00, `upper_ci` 13.00, `decision` significant.

Tables

obs(obs_id INTEGER, group_name VARCHAR(5), value INTEGER)

resamples(boot_id INTEGER, group_name VARCHAR(5), slot INTEGER, obs_id INTEGER)

Hints

  1. Work in stages. A window function result can only be filtered in a later step. Build one median per boot_id and group, then one difference per boot_id, then the bounds.
  2. Each resamples row is one draw. Look up its value in obs and keep every draw, so an obs_id drawn twice in one replicate counts twice.
GeneralData ScientisthardData Manipulation (SQL/Python)

Bootstrap Percentile Interval for a Difference in Medians

The obs table holds obs_id, group_name ('A' or 'B') and value. The seeded resamples table lists three sampled obs_id values (drawn with replacement) per boot_id and group_name. For each boot_id, compute the median of group A minus the median of group B, then report a 95% percentile confidence interval over these differences.

  • lower_ci is the smallest difference whose CUME_DIST is at least 0.025, rounded to 2 decimals.
  • upper_ci is the largest difference whose CUME_DIST is at most 0.975, rounded to 2 decimals.
  • decision is 'significant' when lower_ci > 0 or upper_ci < 0, and 'not significant' otherwise.

CUME_DIST is computed over the per-replicate differences in ascending order.

Input Tables

obs

ColumnTypeMeaning
obs_idINTEGERObservation id (primary key)
group_nameVARCHAR(5)'A' or 'B'
valueINTEGERThe observed value

resamples

ColumnTypeMeaning
boot_idINTEGERBootstrap replicate id
group_nameVARCHAR(5)Group the draw belongs to
slotINTEGERPosition of the draw within the replicate's group sample (1 to 3)
obs_idINTEGERThe drawn observation; references obs.obs_id

Primary key of resamples: (boot_id, group_name, slot).

Output Contract

  • Exactly one row with the columns lower_ci, upper_ci, decision, in that order.
  • lower_ci and upper_ci are rounded to 2 decimals.
  • decision is the exact text significant or not significant.
  • The result has a single row, so no ordering is needed.

Constraints and Clarifications

  • Every resamples.obs_id exists in obs, and its group_name matches the row in obs. value is never NULL.
  • Every boot_id has draws for both A and B.
  • Draws are with replacement. A repeated obs_id inside one replicate (replicate 3 of the sample data draws obs_id 3 three times for group A) counts once per draw.
  • Each replicate's group sample has three draws, so its median is the middle value. If a sample ever had an even number of draws, its median would be the average of the two middle values.
  • CUME_DIST of a difference is the share of replicates whose difference is less than or equal to it, so tied differences share the same value.
  • Make the decision from the unrounded bounds. Medians of integer values are multiples of 0.5, so rounding never changes the decision here.
  • The data guarantee that both bounds exist, that is, at least one difference has CUME_DIST at most 0.975.

Example

The sample data has 15 replicates. In replicate 1, group A draws obs_id 4, 3 and 2 (values 30, 25 and 22, median 25) and group B draws 8, 10 and 7 (values 11, 14 and 15, median 14), so the difference is 11. Sorted, the 15 differences are 8, 8, 10, 11, 11, 11, 11, 11, 12, 12, 13, 13, 13, 16, 16. The value 8 has CUME_DIST 2/15 (about 0.133), so it is the smallest difference at or above 0.025. The value 13 has 13/15 (about 0.867) and 16 has 1.0, so 13 is the largest at or below 0.975. Both bounds are above 0, so the expected output is lower_ci 8.00, upper_ci 13.00, decision significant.

Expected Output

Tables

obsresamples
obs
ColumnType
obs_idINTEGER
group_nameVARCHAR(5)
valueINTEGER
resamples
ColumnType
boot_idINTEGER
group_nameVARCHAR(5)
slotINTEGER
obs_idINTEGER

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions