Detect Simpson's Paradox in Pooled vs Per-Group Treatment Success Rates

Quick Overview

Write a pandas function that compares two treatments' success rates pooled and within each level of a candidate confounder, then flags the strata whose winner reverses the pooled result. It tests grouped rate calculations, exact output dtypes, and the reasoning behind Simpson's paradox.

Detect Simpson's Paradox in Pooled vs Per-Group Treatment Success Rates

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `simpsons_paradox_check(df, group, treat, outcome)`. `treat` is a treatment column with two values, `outcome` is a 0/1 success flag, and `group` is a candidate confounder. Return a pooled comparison row followed by one row per group. `rate_a` and `rate_b` are the success rates of the first and second treatment value in sorted order. The result must meet these requirements: - Columns exactly: `group_value` (object), `n` (int64, rows in the stratum), `rate_a`, `rate_b`, `diff` (float64), `winner` (object), `reverses_pooled` (bool). - The pooled row has `group_value = 'ALL'` and `reverses_pooled = False`. - `diff = rate_a - rate_b`. `winner` is the label of the treatment value with the higher rate (for example `'A'` or `'B'`), or `'tie'`. - On group rows, `reverses_pooled` is `True` when the group's `winner` differs from the pooled `winner`. - The pooled row comes first, then the groups by `group_value` ascending, then `reset_index(drop=True)`. ```hint One comparison, many slices The pooled row and each group row are the same calculation on different subsets of the frame. Write it once and apply it to each subset. ``` ```hint Where a reversal comes from Look at how each treatment's rows are spread across the groups. A treatment's pooled rate is a weighted average of its group rates, and the weights need not be the same for both treatments. ``` ### Constraints & Assumptions - The fixture is the kidney-stone data, where A wins in every stratum and B wins pooled. - The result is compared with `assert_frame_equal`, so dtypes and column order must match. - `treat` has exactly two distinct non-null values. ### What a Strong Answer Covers - One helper for the rates, difference and winner, applied to the full frame and to each group - The sorted-label convention for `rate_a` and `rate_b`, and `winner` reporting the actual treatment label - Row order, the `'ALL'` row and the exact dtypes - Why a treatment can win every stratum and still lose pooled, explained through how each arm's rows are distributed across the groups - What a missing treatment arm or an exact tie does to `winner` and `reverses_pooled` ### Follow-up Questions - How would you handle a group where one treatment value is absent? - How would you add a significance test to confirm the reversal is not noise? - When should you trust the stratified comparison over the pooled one, and when is stratifying the wrong move?

Overview: Write a pandas function that compares two treatments' success rates pooled and within each level of a candidate confounder, then flags the strata whose winner reverses the pooled result. It tests grouped rate calculations, exact output dtypes, and the reasoning behind Simpson's paradox.

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

Write simpsons_paradox_check(df, group, treat, outcome). treat is a treatment column with two values, outcome is a 0/1 success flag, and group is a candidate confounder. Return a pooled comparison row followed by one row per group. rate_a and rate_b are the success rates of the first and second treatment value in sorted order.

The result must meet these requirements:

  • Columns exactly: group_value (object), n (int64, rows in the stratum), rate_a , rate_b , diff (float64), winner (object), reverses_pooled (bool).
  • The pooled row has group_value = 'ALL' and reverses_pooled = False .
  • diff = rate_a - rate_b . winner is the label of the treatment value with the higher rate (for example 'A' or 'B' ), or 'tie' .
  • On group rows, reverses_pooled is True when the group's winner differs from the pooled winner .
  • The pooled row comes first, then the groups by group_value ascending, then reset_index(drop=True) .

Constraints & Assumptions

  • The fixture is the kidney-stone data, where A wins in every stratum and B wins pooled.
  • The result is compared with assert_frame_equal , so dtypes and column order must match.
  • treat has exactly two distinct non-null values.

What a Strong Answer Covers Guidance

  • One helper for the rates, difference and winner, applied to the full frame and to each group
  • The sorted-label convention for rate_a and rate_b , and winner reporting the actual treatment label
  • Row order, the 'ALL' row and the exact dtypes
  • Why a treatment can win every stratum and still lose pooled, explained through how each arm's rows are distributed across the groups
  • What a missing treatment arm or an exact tie does to winner and reverses_pooled

Follow-up Questions Guidance

  • How would you handle a group where one treatment value is absent?
  • How would you add a significance test to confirm the reversal is not noise?
  • When should you trust the stratified comparison over the pooled one, and when is stratifying the wrong move?