Skip to content
Scenario4 min read

Instrumental Variables (IV)

In many scenarios, the treatment is not assigned randomly, and there are unobserved confounders that affect both the treatment and the outcome. In such cases, w...

Instrumental Variables (IV)

In many scenarios, the treatment is not assigned randomly, and there are unobserved confounders that affect both the treatment and the outcome. In such cases, we can use an Instrumental Variable (IV) to estimate the Local Average Treatment Effect (LATE).

Causal Assumptions

First-stage strength: instruments must be strongly correlated with the treatment.

Reduced form: instrument must affect the outcome only through the treatment.

Instrument balance by Z: instrument should be as good as random, independent of confounders.

Instrument propensity / predictability: instrument assignment should not be perfectly predictable from observed confounders.

Subgroup first-stage stability

Correct time order: X before Z, Z before D, D before Y: by design

Data

For the analysis you need data in pandas dataframe:

  • treatment column in binary format (1/0)
  • outcome column numeric format, measured after treatment time
  • instruments column in binary format (1/0), measured before treatment time
  • user_id column (Optional, but useful)
  • confounders columns (Optional, measured before instrument time, numeric format, used for causal assumption check)

We will use the generate_offer_iv_26 DGP from causalis

Result
user_idnet_revenue_90daccepted_offeroffer_eligibleagetenure_monthsannual_incomecredit_scoreapp_sessions_30dprior_spend_30d...g_z0g_z1iv_first_stageiv_reduced_formlate_xlatetau_linkg_d0g_d1cate
0030f648.2799550.00.042.039.15493931608.240102608.08.0135.193819...33.12552637.1101600.2540163.98463315.68655316.93934915.68655331.03264946.71920215.686553
103f5239.7191080.00.037.032.736592214134.071780727.04.0491.348038...80.78815188.0546190.3300437.26646822.01674116.93934922.01674172.53679094.55353122.016741
202ba649.7275050.00.055.021.02363895958.504507643.07.0189.244171...54.18332758.8711450.2957174.68781815.85235916.93934915.85235951.18930267.04166215.852359
302e1772.1554831.01.025.018.10501735715.886364617.011.0187.196966...66.77947174.9384080.3310568.15893724.64515416.93934924.64515457.81923782.46439124.645154
40443a33.3300210.00.019.062.996281107316.360040691.02.0113.817846...50.28517454.2462840.2861493.96111013.84281316.93934913.84281347.88492161.72773413.842813

5 rows × 30 columns

Result

IVCausalData(df=(20000, 16), treatment='accepted_offer', outcome='net_revenue_90d', instruments=['offer_eligible'], confounders=['age', 'tenure_months', 'annual_income', 'credit_score', 'app_sessions_30d', 'prior_spend_30d', 'savings_balance', 'premium_user', 'autopay_enabled', 'region_north', 'region_west', 'acquisition_paid'], user_id='user_id')

EDA

Result
treatmentcountmeanstdminp10p25medianp75p90max
001321538.49473025.145975-49.0671106.32296321.66592138.40699055.42939670.823578137.335543
11678573.70942327.678527-33.50828938.84181154.86086873.23546692.284659109.586879170.766350
Result

png

Check the confounders balance for the instrument

Are groups equal in terms of confounders? We check the balance of confounders across the instrument assignment (offer_eligible).

Result
confoundersmean_z_0mean_z_1abs_diffsmdks_pvalue
0credit_score659.763925693.25752733.4936020.5325810.00000
1annual_income84199.97112599330.82898115130.8578560.2865490.00000
2tenure_months28.49266632.0222613.5295950.1809590.00000
3autopay_enabled0.3572900.4446240.0873340.1789020.00000
4premium_user0.2415890.3137630.0721750.1616750.00000
5acquisition_paid0.4519630.3773120.074651-0.1519550.00000
6savings_balance7027.4524828913.4720891886.0196070.1518980.00000
7prior_spend_30d156.527119172.97994116.4528220.1108040.00000
8age37.69953338.6625810.9630480.0875520.00000
9app_sessions_30d7.2184117.5260220.3076100.0729120.00018
10region_north0.2087850.2316130.0228280.0551070.01101
11region_west0.2000930.1989250.001169-0.0029241.00000

Inference

Result

IIVM(status='fitted', n_folds=5, random_state=None)

Result
value
field
estimandLATE
modelIIVM
value19.1385 (ci_abs: 16.9702, 21.3068)
value_relativeNone
std_error1.1063
t_stat17.2999
alpha0.0500
p_value0.0000
is_significantTrue
outcomenet_revenue_90d
treatmentaccepted_offer
instrumentoffer_eligible
time2026-05-10

Diagnostics

Result
metricvalueflagthresholdmessage
0first_stage_effect0.282397GREENGREEN F >= 10; YELLOW 4 <= F < 10; RED F < 4 o...Coefficient on Z in D ~ 1 + Z + X.
1first_stage_se0.006291GREENGREEN when finite; RED when unavailable or inv...HC1 robust standard error for the Z coefficient.
2first_stage_pvalue0.0GREENGREEN when finite; RED when unavailable or inv...HC1 robust p-value for the Z coefficient.
3first_stage_f2015.048215GREENGREEN F >= 10; YELLOW 4 <= F < 10; RED F < 4 o...Robust first-stage Wald F for the single instr...
4partial_r20.095442GREENGREEN when finite; RED when unavailable or inv...Partial R-squared added by Z after controlling...
5orthogonal_first_stage0.281294GREENGREEN F >= 10; YELLOW 4 <= F < 10; RED F < 4 o...Denominator of the orthogonal LATE score.
6weak_iv_flagGREENGREENGREEN F >= 10; YELLOW 4 <= F < 10; RED F < 4 o...First stage is strong enough for IV inference.
Result
metricvalueflagthresholdmessage
0instrument_auc0.653257YELLOWGREEN <0.60; YELLOW 0.60-0.75; RED >0.75 by ma...How well covariates predict the instrument; hi...
1instrument_propensity_ks0.226303GREENGREEN <=0.30; YELLOW >0.30-0.40; RED >0.40Two-sample KS distance between Z=1 and Z=0 pro...
2instrument_ess_ratio0.813130GREENGREEN >=0.30; YELLOW 0.15-0.30; RED <0.15Minimum observed-arm IPW effective-sample-size...
Result

png

Result
metricvalueflagthresholdmessage
0reduced_form_effect1.076413e+01GREENGREEN when finite; RED when unavailable or inv...Coefficient on Z in Y ~ 1 + Z.
1reduced_form_se4.336784e-01GREENGREEN when finite; RED when unavailable or inv...HC1 robust standard error for the reduced-form...
2reduced_form_pvalue5.379896e-136GREENGREEN when finite; RED when unavailable or inv...HC1 robust p-value; non-significance is not a ...
3orthogonal_reduced_form5.383544e+00GREENGREEN when finite; RED when unavailable or inv...Numerator of the orthogonal LATE score.
4late_ratio_check1.913849e+01GREENGREEN when finite; RED when unavailable or inv...Orthogonal reduced form divided by orthogonal ...

Refutation

let's compare with oracle effect

Result

Ground truth LATE (oracle) is 16.939348789576744