Minimum Detectable Effect vs Observed Conversion Lift in SQL

Quick Overview

Given per-user conversion rows for control and treatment and a one-row parameter table, write a SQL query that computes the minimum detectable effect from the observed arm sizes, the observed conversion lift, and a flag for whether the lift exceeds the MDE. It tests power analysis formulas and careful numeric division in PostgreSQL.

Minimum Detectable Effect vs Observed Conversion Lift in SQL

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `trials` has one row per user in a conversion test, with the user's arm in `group_name` (`'control'` or `'treatment'`) and `converted` (1 or 0). The single-row table `test_params` holds `baseline_rate`, `z_alpha` and `z_beta`: the baseline conversion rate and the z-values for the chosen significance level and power. Return one row with `mde`, `observed_lift` and `detectable`: - `se = sqrt(baseline_rate * (1 - baseline_rate) * (1/n_control + 1/n_treatment))`, using the observed size of each arm. - `mde = (z_alpha + z_beta) * se`, rounded to 4 decimals. - `observed_lift = p_treatment - p_control`, where each rate is the arm's conversions divided by its users, rounded to 4 decimals. - `detectable` is 1 when `|observed_lift| > mde`, else 0. ```hint Watch the division The arm sizes and conversion counts are integers. Check that every rate and every `1/n` term is computed in decimal arithmetic. ``` ### Input Table(s) Table `trials` (no key column; each row is one user): | Column | Type | Meaning | |---|---|---| | `group_name` | VARCHAR(20) | Arm: `'control'` or `'treatment'` | | `converted` | SMALLINT | 1 if the user converted, 0 otherwise | Table `test_params` (exactly one row): | Column | Type | Meaning | |---|---|---| | `baseline_rate` | NUMERIC(4,3) | Baseline conversion rate used in the variance term | | `z_alpha` | NUMERIC(4,2) | z-value for the significance level | | `z_beta` | NUMERIC(4,2) | z-value for the target power | ### Output Contract Exactly one row with these columns, in this order: | Column | Definition | Rounding | |---|---|---| | `mde` | `(z_alpha + z_beta) * se` | 4 decimals | | `observed_lift` | `p_treatment - p_control` | 4 decimals | | `detectable` | 1 if `abs(observed_lift) > mde`, else 0 | integer | With $p_0$ the baseline rate and $n_c$, $n_t$ the arm sizes: $$ SE = \sqrt{p_0 (1 - p_0) \left(\frac{1}{n_c} + \frac{1}{n_t}\right)}, \qquad MDE = (z_\alpha + z_\beta) \cdot SE $$ `detectable` compares the unrounded lift and MDE, and the inequality is strict, so a lift exactly equal to the MDE gives 0. The result is a single row, so no ordering is needed. ### Example Input `trials`: | group_name | converted | |---|---| | treatment | 1 | | treatment | 0 | | control | 0 | | treatment | 1 | | control | 0 | | control | 0 | | treatment | 0 | | treatment | 1 | | treatment | 1 | | control | 0 | | treatment | 1 | | control | 0 | | control | 0 | | treatment | 1 | | control | 0 | | treatment | 1 | | control | 0 | | control | 0 | Input `test_params`: | baseline_rate | z_alpha | z_beta | |---|---|---| | 0.300 | 1.96 | 0.84 | Expected output: | mde | observed_lift | detectable | |---|---|---| | 0.6049 | 0.7778 | 1 | Each arm has 9 users. Treatment has 7 conversions (rate 7/9 ≈ 0.7778) and control has none, so the observed lift is 0.7778. The standard error is $\sqrt{0.3 \cdot 0.7 \cdot (2/9)} \approx 0.21602$, so the MDE is 2.80 × 0.21602 ≈ 0.6049. The absolute lift is larger than the MDE, so `detectable` is 1. ### Constraints and Clarifications - Each arm has at least one user, and `converted` is never NULL. - `group_name` only takes the values `'control'` and `'treatment'`. - `test_params` has exactly one row. - The variance term uses `baseline_rate` for both arms. The observed rates enter only `observed_lift`. - `observed_lift` keeps its sign (treatment minus control). Only the `detectable` comparison uses its absolute value.

Overview: Given per-user conversion rows for control and treatment and a one-row parameter table, write a SQL query that computes the minimum detectable effect from the observed arm sizes, the observed conversion lift, and a flag for whether the lift exceeds the MDE. It tests power analysis formulas and careful numeric division in PostgreSQL.

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

Table trials has one row per user in a conversion test, with the user's arm in group_name ('control' or 'treatment') and converted (1 or 0). The single-row table test_params holds baseline_rate, z_alpha and z_beta: the baseline conversion rate and the z-values for the chosen significance level and power. Return one row with mde, observed_lift and detectable:

  • se = sqrt(baseline_rate * (1 - baseline_rate) * (1/n_control + 1/n_treatment)) , using the observed size of each arm.
  • mde = (z_alpha + z_beta) * se , rounded to 4 decimals.
  • observed_lift = p_treatment - p_control , where each rate is the arm's conversions divided by its users, rounded to 4 decimals.
  • detectable is 1 when |observed_lift| > mde , else 0.

Input Table(s)

Table trials (no key column; each row is one user):

ColumnTypeMeaning
group_nameVARCHAR(20)Arm: 'control' or 'treatment'
convertedSMALLINT1 if the user converted, 0 otherwise

Table test_params (exactly one row):

ColumnTypeMeaning
baseline_rateNUMERIC(4,3)Baseline conversion rate used in the variance term
z_alphaNUMERIC(4,2)z-value for the significance level
z_betaNUMERIC(4,2)z-value for the target power

Output Contract

Exactly one row with these columns, in this order:

ColumnDefinitionRounding
mde(z_alpha + z_beta) * se4 decimals
observed_liftp_treatment - p_control4 decimals
detectable1 if abs(observed_lift) > mde, else 0integer

With p0p_0 the baseline rate and ncn_c, ntn_t the arm sizes:

SE=p0(1−p0)(1nc+1nt),MDE=(zα+zβ)⋅SESE = \sqrt{p_0 (1 - p_0) \left(\frac{1}{n_c} + \frac{1}{n_t}\right)}, \qquad MDE = (z_\alpha + z_\beta) \cdot SE

detectable compares the unrounded lift and MDE, and the inequality is strict, so a lift exactly equal to the MDE gives 0. The result is a single row, so no ordering is needed.

Example

Input trials:

group_nameconverted
treatment1
treatment0
control0
treatment1
control0
control0
treatment0
treatment1
treatment1
control0
treatment1
control0
control0
treatment1
control0
treatment1
control0
control0

Input test_params:

baseline_ratez_alphaz_beta
0.3001.960.84

Expected output:

mdeobserved_liftdetectable
0.60490.77781

Each arm has 9 users. Treatment has 7 conversions (rate 7/9 ≈ 0.7778) and control has none, so the observed lift is 0.7778. The standard error is 0.3⋅0.7⋅(2/9)≈0.21602\sqrt{0.3 \cdot 0.7 \cdot (2/9)} \approx 0.21602, so the MDE is 2.80 × 0.21602 ≈ 0.6049. The absolute lift is larger than the MDE, so detectable is 1.

Constraints and Clarifications

  • Each arm has at least one user, and converted is never NULL.
  • group_name only takes the values 'control' and 'treatment' .
  • test_params has exactly one row.
  • The variance term uses baseline_rate for both arms. The observed rates enter only observed_lift .
  • observed_lift keeps its sign (treatment minus control). Only the detectable comparison uses its absolute value.