Instrument-Based Estimator for a Randomized Encouragement Trial
Arrays & HashingAuto-graded · 18 test casesLast 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:
| Position | Name | Values | Meaning |
|---|---|---|---|
| 0 | assigned_to_prompt | 0 or 1 | Whether the user was randomly assigned to receive the encouragement prompt |
| 1 | installed | 0 or 1 | Whether the user actually installed the new feature |
| 2 | retained | 0 or 1 | Whether the user remained active for 30 days after the assignment date |
| 3 | prior_active_days | integer ≥ 0 | Number of active days in the 30-day window before the trial began |
Output format
Return a dictionary with exactly these eight keys:
| Key | Type | Definition |
|---|---|---|
"control_upgrade_rate" | float | Fraction of no-prompt users who installed the feature |
"offer_upgrade_rate" | float | Fraction of prompted users who installed the feature |
"control_retention_rate" | float | Fraction of no-prompt users who were retained |
"offer_retention_rate" | float | Fraction of prompted users who were retained |
"intent_to_treat" | float | Difference in retention rates: prompted minus no-prompt (i.e. offer_retention_rate − control_retention_rate) |
"first_stage_upgrade_effect" | float | Difference in install rates: prompted minus no-prompt (i.e. offer_upgrade_rate − control_upgrade_rate) |
"complier_average_causal_effect" | float or None | Wald/IV estimate: intent_to_treat / first_stage_upgrade_effect. Return None if first_stage_upgrade_effect is exactly zero |
"pre_activity_mean_difference" | float | Difference 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_promptis always 0 or 1). - If
first_stage_upgrade_effectis exactly0.0, set"complier_average_causal_effect"toNone. - 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The function implements three building blocks of a randomized encouragement (instrumental-variables) design.
Step 1 — split by arm
Partition the records on the first field (assigned_to_prompt). Every subsequent calculation is just arithmetic within each partition, so a single pass through the list is enough.
Step 2 — within-arm means
Compute four means independently:
- install rate in each arm →
control_upgrade_rate,offer_upgrade_rate - retention rate in each arm →
control_retention_rate,offer_retention_rate - mean prior active days in each arm → used for the balance check
Because arm assignment is random, these means are unbiased estimates of the corresponding population quantities.
Step 3 — the three causal statistics
Intent-to-treat (ITT): offer_retention_rate − control_retention_rate. This measures the effect of being offered the prompt on retention. Randomization makes it causal without any further adjustment, even though many prompted users never installed.
First stage: offer_upgrade_rate − control_upgrade_rate. This tells you how much the prompt actually moved install behavior. A strong first stage (large positive number) is needed for the next step to be precise.
Wald / IV estimate (LATE): ITT / first_stage. This rescales the ITT to isolate the effect of installing for the specific sub-population that installed because of the prompt (compliers). If first_stage is exactly zero the division is undefined, so return None.
Balance check: mean(prior_active_days, offer) − mean(prior_active_days, control). A value near zero confirms that randomization produced comparable arms on the pre-trial observable. This is a diagnostic, not a causal estimate.
Why this is correct
- No user-level selection bias enters any of the four arm means because assignment was random.
- The Wald estimator is the standard single-instrument IV estimator. Under the standard assumptions (relevance, exclusion, monotonicity, SUTVA) it recovers the local average treatment effect for compliers.
- The balance check uses the same mean-difference logic and requires no special handling.
Complexity
- Time: O(n) — one pass to split, then four O(n) mean computations.
- Space: O(n) — storing the two partitions.
Reference solution
from typing import Dict, List, Optional
def estimate_encouragement_trial(records: List[List[int]]) -> Dict[str, Optional[float]]:
# Separate records into control (assigned_to_prompt=0) and offer (assigned_to_prompt=1) arms.
groups = {0: [], 1: []}
for assigned, installed, retained, prior_active_days in records:
groups[assigned].append((installed, retained, prior_active_days))
control = groups[0]
offer = groups[1]
def mean(values):
return sum(values) / len(values)
control_upgrade_rate = mean([row[0] for row in control])
offer_upgrade_rate = mean([row[0] for row in offer])
control_retention_rate = mean([row[1] for row in control])
offer_retention_rate = mean([row[1] for row in offer])
# Balance check: difference in pre-trial activity between arms.
pre_activity_diff = (
mean([row[2] for row in offer]) - mean([row[2] for row in control])
)
# ITT: causal effect of being assigned the prompt on retention.
intent_to_treat = offer_retention_rate - control_retention_rate
# First stage: how much the prompt increased install rates.
first_stage = offer_upgrade_rate - control_upgrade_rate
# Wald/IV (LATE): causal effect of installing, for compliers only.
complier_effect = None if first_stage == 0 else intent_to_treat / first_stage
return {
"control_upgrade_rate": control_upgrade_rate,
"offer_upgrade_rate": offer_upgrade_rate,
"control_retention_rate": control_retention_rate,
"offer_retention_rate": offer_retention_rate,
"intent_to_treat": intent_to_treat,
"first_stage_upgrade_effect": first_stage,
"complier_average_causal_effect": complier_effect,
"pre_activity_mean_difference": pre_activity_diff,
}