KatInterview

Instrument-Based Estimator for a Randomized Encouragement Trial

Arrays & HashingAuto-graded · 18 test cases
MediumAuto-gradedArrays & Hashingcausal-inferencea-b-testinginstrumental-variables

Last updated

Tests whether a candidate can implement ITT, first-stage, and IV/Wald (LATE) estimators from a randomized encouragement design, including a balance check on pre-treatment activity.

Background

A product team wants to measure the true causal effect of a new software feature on user retention. Because installing the feature requires a manual step, users are not forced to adopt it. Instead, a random subset of the user base is selected to receive a prominent in-app prompt encouraging them to install the update; the remaining users see no such prompt.

Your job is to process the resulting per-user trial data and compute a set of statistics that together tell the causal story: how much did the prompt change install behavior, how much did it change retention, and — crucially — what was the effect of actually installing the feature for the users whose behavior the prompt changed?

Task

Write a function that accepts a list of per-user records and returns a dictionary with exactly the keys described below.

Input format

Each record is a list of four integers in this order:

PositionNameValuesMeaning
0assigned_to_prompt0 or 1Whether the user was randomly assigned to receive the encouragement prompt
1installed0 or 1Whether the user actually installed the new feature
2retained0 or 1Whether the user remained active for 30 days after the assignment date
3prior_active_daysinteger ≥ 0Number of active days in the 30-day window before the trial began

Output format

Return a dictionary with exactly these eight keys:

KeyTypeDefinition
"control_upgrade_rate"floatFraction of no-prompt users who installed the feature
"offer_upgrade_rate"floatFraction of prompted users who installed the feature
"control_retention_rate"floatFraction of no-prompt users who were retained
"offer_retention_rate"floatFraction of prompted users who were retained
"intent_to_treat"floatDifference in retention rates: prompted minus no-prompt (i.e. offer_retention_rate − control_retention_rate)
"first_stage_upgrade_effect"floatDifference in install rates: prompted minus no-prompt (i.e. offer_upgrade_rate − control_upgrade_rate)
"complier_average_causal_effect"float or NoneWald/IV estimate: intent_to_treat / first_stage_upgrade_effect. Return None if first_stage_upgrade_effect is exactly zero
"pre_activity_mean_difference"floatDifference in mean prior active days: prompted group minus no-prompt group. Used to check whether randomization produced balanced arms

Edge-case rules

  • Every record belongs to exactly one arm (assigned_to_prompt is always 0 or 1).
  • If first_stage_upgrade_effect is exactly 0.0, set "complier_average_causal_effect" to None.
  • All other divisions are safe (each arm will contain at least one user in every test case).

Constraints

  • 2 ≤ total number of records ≤ 10 000
  • Each record contains exactly 4 elements
  • assigned_to_prompt ∈ {0, 1}
  • installed ∈ {0, 1}
  • retained ∈ {0, 1}
  • prior_active_days ≥ 0

Examples

Example 1

Input: estimate_encouragement_trial([[0, 0, 0, 5], [0, 0, 1, 7], [0, 1, 1, 6], [1, 1, 1, 6], [1, 1, 1, 8], [1, 0, 0, 5]])

Output: {'control_upgrade_rate': 0.3333333333333333, 'offer_upgrade_rate': 0.6666666666666666, 'control_retention_rate': 0.6666666666666666, 'offer_retention_rate': 0.6666666666666666, 'intent_to_treat': 0, 'first_stage_upgrade_effect': 0.3333333333333333, 'complier_average_causal_effect': 0, 'pre_activity_mean_difference': 0.33333333333333304}

Example 2

Input: estimate_encouragement_trial([[0, 0, 0, 2], [0, 0, 1, 3], [0, 1, 1, 4], [1, 1, 1, 3], [1, 0, 0, 2], [1, 1, 0, 5], [1, 1, 1, 4]])

Output: {'control_upgrade_rate': 0.3333333333333333, 'offer_upgrade_rate': 0.75, 'control_retention_rate': 0.6666666666666666, 'offer_retention_rate': 0.5, 'intent_to_treat': -0.16666666666666663, 'first_stage_upgrade_effect': 0.4166666666666667, 'complier_average_causal_effect': -0.3999999999999999, 'pre_activity_mean_difference': 0.5}

Example 3

Input: estimate_encouragement_trial([[0, 0, 0, 0], [0, 0, 1, 1], [1, 1, 1, 0], [1, 0, 0, 1]])

Output: {'control_upgrade_rate': 0, 'offer_upgrade_rate': 0.5, 'control_retention_rate': 0.5, 'offer_retention_rate': 0.5, 'intent_to_treat': 0, 'first_stage_upgrade_effect': 0.5, 'complier_average_causal_effect': 0, 'pre_activity_mean_difference': 0}

Hints

Hint 1

Split the records into two groups by the first field (assigned_to_prompt = 0 vs 1), then compute simple arithmetic means within each group independently.

Hint 2

The intent-to-treat and first-stage effects are both plain differences of means — retention rate difference and install rate difference respectively. Compute those two numbers first; the Wald estimate is just their ratio.

Hint 3

For the balance check, compute the mean of prior_active_days in each arm and subtract: prompted minus control. A value close to zero suggests randomization worked; the function should return this number regardless of whether it is close to zero.