Scan candidate columns for Simpson's paradox reversals of a two-arm winner in pandas

Quick Overview

Write a pandas function that checks each candidate column for Simpson's paradox by asking whether splitting the data by it reverses which of two treatments has the higher success rate. It tests grouped rate tables, strict tie handling and exact output dtypes.

Scan candidate columns for Simpson's paradox reversals of a two-arm winner in pandas

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Write `confounder_reversal_scan(df, treat, outcome, candidates)`. `treat` is a column holding a two-valued label and `outcome` is a 0/1 column. The winner, whether pooled over all rows or within one level of a column, is the label with the strictly higher `outcome` mean; equal means are a tie. Return one row per candidate column reporting whether splitting the data by that column reverses the pooled winner. Requirements: - Columns: `candidate` (object), `n_levels` (int64), `pooled_winner` (object, the same on every row), `n_levels_reversed` (int64), `pct_levels_reversed` (float64), `is_confounder` (bool). - `n_levels` is the number of distinct levels. `n_levels_reversed` is the number of levels whose winner differs from `pooled_winner`. Tied levels are not reversed. - `pct_levels_reversed = n_levels_reversed / n_levels * 100`. - `is_confounder` is `True` only when every level reverses the pooled winner. - Sort by `pct_levels_reversed` descending, break ties by `candidate` ascending, then `reset_index(drop=True)`. Example: ```python # treat in {"A", "B"}, outcome in {0, 1} # Pooled: A wins overall. out = confounder_reversal_scan(df, "treat", "outcome", ["dept", "region", "color"]) # candidate n_levels pooled_winner n_levels_reversed pct_levels_reversed is_confounder # 0 dept 3 A 3 100.0 True # 1 region 4 A 2 50.0 False # 2 color 2 A 0 0.0 False ``` ```hint One winner rule everywhere Write one small helper that takes the two rates and returns the winning label or a tie, and use it for the pooled data and for every level. Decide how a tie is represented before you count reversals. ``` ```hint Per-level rate table Grouping by the candidate and the treatment, then unstacking the treatment, gives one row per level with both rates side by side. ``` ```hint Strictly higher The rule says strictly higher. Check whether a tolerance-based comparison gives the same answer on two rates that differ only slightly. ``` ### Constraints & Assumptions - `treat` has exactly two labels, and the pooled rates are not tied. - Every level of every candidate contains both treatment labels. ### What a Strong Answer Covers - The pooled winner is computed once from all rows, using the strict comparison rule. - Per-level winners come from a grouped rate table, and tied levels are not counted as reversed. - The output has the six columns, the required dtypes and the two-key sort. - The connection to Simpson's paradox and what a reversal in every level means. - Complexity and edge cases such as a level missing one arm or a candidate with no non-null levels. ### Follow-up Questions - How would you weight reversals by group size instead of counting levels equally? - Rates can flip on noise alone. How would you guard against spurious reversals? - A candidate reverses the winner in every level. Does that prove it is a confounder, and what else would you check?

Overview: Write a pandas function that checks each candidate column for Simpson's paradox by asking whether splitting the data by it reverses which of two treatments has the higher success rate. It tests grouped rate tables, strict tie handling and exact output dtypes.

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

Write confounder_reversal_scan(df, treat, outcome, candidates). treat is a column holding a two-valued label and outcome is a 0/1 column. The winner, whether pooled over all rows or within one level of a column, is the label with the strictly higher outcome mean; equal means are a tie. Return one row per candidate column reporting whether splitting the data by that column reverses the pooled winner.

Requirements:

  • Columns: candidate (object), n_levels (int64), pooled_winner (object, the same on every row), n_levels_reversed (int64), pct_levels_reversed (float64), is_confounder (bool).
  • n_levels is the number of distinct levels. n_levels_reversed is the number of levels whose winner differs from pooled_winner . Tied levels are not reversed.
  • pct_levels_reversed = n_levels_reversed / n_levels * 100 .
  • is_confounder is True only when every level reverses the pooled winner.
  • Sort by pct_levels_reversed descending, break ties by candidate ascending, then reset_index(drop=True) .

Example:

# treat in {"A", "B"}, outcome in {0, 1}
# Pooled: A wins overall.
out = confounder_reversal_scan(df, "treat", "outcome", ["dept", "region", "color"])
#   candidate  n_levels pooled_winner  n_levels_reversed  pct_levels_reversed  is_confounder
# 0      dept         3             A                  3                100.0           True
# 1    region         4             A                  2                 50.0          False
# 2     color         2             A                  0                  0.0          False

Constraints & Assumptions

  • treat has exactly two labels, and the pooled rates are not tied.
  • Every level of every candidate contains both treatment labels.

What a Strong Answer Covers Guidance

  • The pooled winner is computed once from all rows, using the strict comparison rule.
  • Per-level winners come from a grouped rate table, and tied levels are not counted as reversed.
  • The output has the six columns, the required dtypes and the two-key sort.
  • The connection to Simpson's paradox and what a reversal in every level means.
  • Complexity and edge cases such as a level missing one arm or a candidate with no non-null levels.

Follow-up Questions Guidance

  • How would you weight reversals by group size instead of counting levels equally?
  • Rates can flip on noise alone. How would you guard against spurious reversals?
  • A candidate reverses the winner in every level. Does that prove it is a confounder, and what else would you check?