generated from alshedivat/al-folio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calibrate_demo.Rmd
454 lines (287 loc) · 15 KB
/
calibrate_demo.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
---
title: "Calibration demo"
output:
pdf_document: default
html_document: default
date: '2023-08-09'
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
---
title: "Calibration demo"
output:
html_document: default
pdf_document: default
date: '2023-08-09'
---
## Introduction
This document presents a demonstration of calibration methods for treatment effect predictors. We generate synthetic data with covariates, treatment assignments, and outcomes.
We explore two calibration methods: the Best Linear Predictor (BLP) and causal isotonic calibration.
The BLP method provides a linear calibration gaurantees by learning an optimal linear transformation of the original predictor, so that the linearly calibrated predictor cannot be improved by applying any linear transformation (i.e., scaling and shifting).
Isotonic calibration offers non-parametric (distribution-free) calibration gaurantees by (1) an optimal monotone transformation of the original predictor and (2) providing a calibrated predictor that cannot be improved by applying any transformation (linear or nonlinear).
## Data Generation
We begin by generating synthetic data for the demonstration. We set the seed for reproducibility and create variables for covariates, treatment assignments, potential outcomes, observed outcomes, and the conditional average treatment effect (CATE).
```{r}
# Set random seed for reproducibility
set.seed(12345)
n <- 2000
# Generate covariate W from a uniform distribution between -1 and 1
W <- runif(n, -1, 1)
# Calculate treatment assignment probabilities using logistic function
pi <- plogis(0.5 * W)
A <- rbinom(n, size = 1, pi)
# Define outcome regression functions and CATE
mu0 <- plogis(W)
mu1 <- plogis(1 + 2 * W)
cate <- mu1 - mu0
# Generate potential outcomes based on treatment assignment
Y0 <- rbinom(n, size = 1, mu0)
Y1 <- rbinom(n, size = 1, plogis(1 + 2 * W))
# Create observed outcomes based on treatment assignment
Y <- ifelse(A == 1, Y1, Y0)
```
## Initial predictor and calibration plot
We first create an initial predictor of the Individual Treatment Effect (ITE), denoted as tau.hat, which is a fixed function for simplicity.
```{r, echo = TRUE}
# use machine learning to obtain initial predictor of ITE Y_1 - Y_0
# for simplicity, we define our predictor tau.hat as a fixed function.
tau.hat <- plogis(1 + W ) - 0.45
```
We visualize the initial predictor, the Best Linear Predictor (BLP), and the true CATE as functions of the covariate.
```{r, echo = FALSE}
library(ggplot2)
# predictor is misspecified but not that badly
p <- ggplot(data = data.frame(tau.hat, cate), aes(x = tau.hat, y = cate)) +
geom_point(color = "blue") +
labs(x = "predictor", y = "cate") +
geom_smooth(method = 'lm', formula = y ~ x, color = "red") +
theme_minimal()
cor_tau <- cor(tau.hat, cate)
# Calculate the regression coefficients
fit <- lm(cate ~ tau.hat, data = data.frame(tau.hat, cate))
intercept <- coef(fit)[1]
slope <- coef(fit)[2]
tau.BLP.oracle <- intercept + tau.hat * slope
p <- ggplot(data = data.frame( tau.hat, tau.BLP.oracle, cate, W), aes(y = tau.hat, x = W)) +
geom_line(aes(color = "Original Predictor"), show.legend = TRUE, size = 1) +
labs(x = "Covariate", y = "Predictor") +
geom_line(aes(y = tau.BLP.oracle, color = "Best Linear Meta-Predictor"), show.legend = TRUE, size = 0.7, alpha = 1) +
geom_line(aes(y = cate, color = "True CATE"), show.legend = TRUE, size = 0.7, alpha = 1) +
scale_color_manual(
name = "Legend",
values = c("Original Predictor" = "blue", "Best Linear Meta-Predictor" = "red",
"True CATE" = "black")
) +
theme_minimal()
p
```
Now, lets make a calibrate plot. This is a scatter plot of the true CATE values vs the predicted CATE values.
```{r, echo = FALSE}
# plot best linear predictor (oracle)
p <- ggplot(data = data.frame( tau.hat, tau.BLP.oracle, cate), aes(y = tau.hat, x = cate)) +
geom_point(aes(color = "Original Predictor"), show.legend = TRUE) +
labs(y = "Predictor", x = "CATE") +
geom_point(aes(y = tau.BLP.oracle, color = "Best Linear Meta-Predictor"), show.legend = TRUE) +
scale_color_manual(
name = "Legend",
values = c("Original Predictor" = "blue", "Best Linear Meta-Predictor" = "red")
) +
theme_minimal()
# Add the slope and intercept to the plot
p <- p +
annotate(
"text", x = 0.05, y = max(tau.hat), label = paste("Slope:", round(slope, 2)),
hjust = 0, vjust = 1, color = "black"
) +
annotate(
"text", x = 0.05, y = max(tau.hat)- 0.05, label = paste("Intercept:", round(intercept, 2)),
hjust = 0, vjust = 0.9, color = "black"
) +
annotate(
"text", x = 0.05, y = max(tau.hat)- 0.1, label = paste("Correlation:", round(cor_tau, 2)),
hjust = 0, vjust = 0.9, color = "black"
)
# Display the plot
print(p)
```
## Linear calibration with Victor's BLP method
We apply the Best Linear Predictor (BLP) method to linearly calibrate the original predictor.
```{r}
# unbiased surrogate outcome for CATE/ITE
pseudo_outcome <- cate + (A/pi) * (Y - mu1) - ((1-A)/(1-pi)) * (Y - mu0)
# fit best linear predictor of tau.hat of the surrogate outcome
# provides estimat of BLP of ITE/CATE
fit <- lm(pseudo_outcome ~ tau.hat, data = data.frame(tau.hat, pseudo_outcome))
intercept <- coef(fit)[1]
slope <- coef(fit)[2]
# get linear calibrated predictor, i.e. BLP given tau.hat
tau.BLP.hat <- intercept + slope * tau.hat
cor_tau <- cor(tau.BLP.hat, cate)
# Calculate the regression coefficients
fit <- lm(cate ~ tau.hat, data = data.frame(tau.hat, cate))
intercept <- coef(fit)[1]
slope <- coef(fit)[2]
tau.BLP.oracle <- intercept + tau.hat * slope
```
We visualize the linearly calibrated predictor as a function of the covariate.
```{r, echo = FALSE}
tau.iso.oracle <- as.stepfun(isoreg(tau.hat, cate))(tau.hat)
p <- ggplot(data = data.frame(tau.BLP.hat, tau.hat, tau.BLP.oracle, W, cate), aes(x = W, y = tau.BLP.hat)) +
geom_line(aes(color = "Linearly Calibrated Predictor"), show.legend = TRUE, size = 1) +
labs(x = "Covariate", y = "Predictor") +
geom_line(aes(y = tau.BLP.oracle, color = "Best Linear Meta-Predictor"), show.legend = TRUE, alpha = 1, size = 0.7)+
geom_line(aes(y = tau.hat, color = "Original Predictor"), show.legend = TRUE, alpha = 1, size = 1)+
geom_line(aes(y = cate, color = "True CATE"), show.legend = TRUE, alpha = 1, size = 0.7)+
scale_color_manual(
name = "Legend",
values = c("Linearly Calibrated Predictor" = "red",
"Best Linear Meta-Predictor" = "#006633",
"Original Predictor" = "blue",
"True CATE" = "black")
) +
theme_minimal()
p
```
We see that the linearly calibrated predictor is a linear transformation of the original predictor.
```{r, echo = FALSE}
p <- ggplot(data = data.frame(tau.BLP.hat, tau.hat, tau.BLP.oracle), aes(x = tau.hat, y = tau.BLP.hat)) +
geom_line(aes(color = "Linearly Calibrated Predictor"), show.legend = TRUE, size = 1) +
labs(x = "Original Predictor", y = "Calibrated Predictor") +
geom_line(aes(y = tau.BLP.oracle, color = "Best Linear Meta-Predictor"), show.legend = TRUE, alpha = 1, size = 0.7)+
scale_color_manual(
name = "Legend",
values = c(
"Best Linear Meta-Predictor" = "#006633",
"Linearly Calibrated Predictor" = "red")
) +
theme_minimal()
p
```
### Check calibration of BLP-corrected predictor
Lets check the new calibration plot of the linearly calibrated predictor.
The following plot shows how the treatment effect predictions assigned to individuals (y-axis) varies as a function of the actual conditional average treatment effect of the individuals.
```{r, echo = FALSE}
cor_tau <- cor(tau.BLP.hat, cate)
# Calculate the regression coefficients
fit <- lm(cate ~ tau.BLP.hat, data = data.frame(tau.BLP.hat, cate))
intercept <- coef(fit)[1]
slope <- coef(fit)[2]
p <- ggplot(data = data.frame(tau.BLP.hat, tau.hat, tau.BLP.oracle, cate), aes(y = tau.BLP.hat, x = cate)) +
geom_point(aes(color = "Linearly Calibrated Predictor"), show.legend = TRUE, size = 1) +
labs(y = "Calibrated Predictor", x = "CATE") +
geom_point(aes(y = tau.BLP.oracle, color = "Best Linear Meta-Predictor"), show.legend = TRUE, alpha = 1, size = 0.7)+
scale_color_manual(
name = "Legend",
values = c("Linearly Calibrated Predictor" = "red",
"Best Linear Meta-Predictor" = "#006633")
) +
theme_minimal()
# Add the slope and intercept to the plot
p <- p +
annotate(
"text", x = 0.05, y = max(cate), label = paste("Slope:", round(slope, 2)),
hjust = 0, vjust = 1, color = "black"
) +
annotate(
"text", x = 0.05, y = max(cate)- 0.025, label = paste("Intercept:", round(intercept, 2)),
hjust = 0, vjust = 0.9, color = "black"
) +
annotate(
"text", x = 0.05, y = max(cate)- 0.05, label = paste("Correlation:", round(cor_tau, 2)),
hjust = 0, vjust = 0.9, color = "black"
)
# Display the plot
print(p)
```
We see now that the linearly calibrated predictor is well calibrated in a linear sense. The fit cannot be improved by fitting a linear model on top of the predictor.
We have correctly estimated the BLP.
However, we see that the BLP approach is unable to correct for the poor calibration (flattening) at the end regions. This is because BLP is parametric and can only calibrate by applying linear transformations to the predictor. A more nonparametric approach can correct for nonlinear transformation.
## Causal isotonic calibration
Next, we introduce causal isotonic calibration, a nonparametric method that can correct for any monotone transformations of the original predictor and provides distribution-free calibration gaurantees.
Since linear transformations (with positive slope) are monotone, this method is strictly more powerful than the BLP approach.
```{r}
# unbiased surrogate outcome for CATE/ITE
pseudo_outcome <- cate + (A/pi) * (Y - mu1) - ((1-A)/(1-pi)) * (Y - mu0)
# fit isotonic regression of pseudo outcome
tau.iso.hat <- as.stepfun(isoreg(tau.hat, pseudo_outcome))(tau.hat)
# note isotonic regression overfits at very end boundaries of tau.hat
# tmp fix: bound predictions into range of cate.
tau.iso.hat <- pmin(pmax(tau.iso.hat, min(cate)), max(cate))
```
We visualize the isotonic calibrated predictor as a function of the covariate.
```{r, echo = FALSE}
tau.iso.oracle <- as.stepfun(isoreg(tau.hat, cate))(tau.hat)
p <- ggplot(data = data.frame(tau.iso.hat, tau.hat, tau.iso.oracle, tau.BLP.oracle, W, cate), aes(x = W, y = tau.iso.hat)) +
geom_line(aes(color = "Isotonic Calibrated Predictor"), show.legend = TRUE, size = 1) +
labs(x = "Covariate", y = "Predictor") +
geom_line(aes(y = tau.iso.oracle, color = "Best Isotonic Meta-Predictor", alpha = 1), show.legend = TRUE, alpha = 1, size = 0.7) +
geom_line(aes(y = tau.BLP.oracle, color = "Best Linear Meta-Predictor"), show.legend = TRUE, alpha = 1, size = 0.7)+
geom_line(aes(y = tau.hat, color = "Original Predictor"), show.legend = TRUE, alpha = 1, size = 1)+
geom_line(aes(y = cate, color = "True CATE"), show.legend = TRUE, alpha = 1, size = 0.7)+
scale_color_manual(
name = "Legend",
values = c("Isotonic Calibrated Predictor" = "red",
"Best Isotonic Meta-Predictor" = "purple",
"Best Linear Meta-Predictor" = "#006633",
"Original Predictor" = "blue",
"True CATE" = "black")
) +
theme_minimal()
p
```
A benefit of isotonic calibration is that it automatically does data-driven heterogeneous treatment effect subgroup identification.
The piece-wise constant values of the calibrated predictor defines subgroups (or bins) of individuals. Individuals with a given bin have conditional treatment effects that are quantitatively similar (given only information provided by the original predictor). Moreover, the conditional treatment effect of a given subgroup is meaningfully different form those of other subgroups.
We see that the isotonic calibrated predictor is a monotone piece-wise constant transformation of the original predictor.
```{r, echo = FALSE}
tau.iso.oracle <- as.stepfun(isoreg(tau.hat, cate))(tau.hat)
p <- ggplot(data = data.frame(tau.iso.hat, tau.hat, tau.iso.oracle, tau.BLP.oracle), aes(x = tau.hat, y = tau.iso.hat)) +
geom_line(aes(color = "Isotonic Calibrated Predictor"), show.legend = TRUE, size = 1) +
labs(x = "Original Predictor", y = "Calibrated Predictor") +
geom_line(aes(y = tau.iso.oracle, color = "Best Isotonic Meta-Predictor", alpha = 1), show.legend = TRUE, alpha = 1, size = 0.7) +
geom_line(aes(y = tau.BLP.oracle, color = "Best Linear Meta-Predictor"), show.legend = TRUE, alpha = 1, size = 0.7)+
scale_color_manual(
name = "Legend",
values = c("Isotonic Calibrated Predictor" = "red",
"Best Isotonic Meta-Predictor" = "purple",
"Best Linear Meta-Predictor" = "#006633",
"Original Predictor" = "blue",
"True CATE" = "black")
) +
theme_minimal()
p
```
### Check calibration of isotonic-corrected predictor
Lets check the new calibration plot of the isotonic calibrated predictor.
The following plot shows how the treatment effect predictions assigned to individuals (y-axis) varies as a function of the actual conditional average treatment effect of the individuals.
```{r, echo = FALSE}
p <- ggplot(data = data.frame(tau.iso.hat, cate), aes(x = cate, y = tau.iso.hat)) +
geom_point(color = "blue") +
labs(x = "predictor", y = "cate") +
geom_smooth(method = 'lm', formula = y ~ x, color = "red") +
theme_minimal()
p <- ggplot(data = data.frame(tau.iso.hat, tau.hat, tau.iso.oracle, tau.BLP.oracle, cate), aes(y = tau.iso.hat, x = cate)) +
geom_point(aes(color = "Isotonic Calibrated Predictor"), show.legend = TRUE, size = 1) +
labs(x = "CATE", y = "Calibrated Predictor") +
geom_point(aes(y = tau.iso.oracle, color = "Best Isotonic Meta-Predictor", alpha = 1), show.legend = TRUE, alpha = 1, size = 0.7) +
geom_point(aes(y = tau.BLP.oracle, color = "Best Linear Meta-Predictor"), show.legend = TRUE, alpha = 1, size = 0.7)+
scale_color_manual(
name = "Legend",
values = c("Isotonic Calibrated Predictor" = "red",
"Best Isotonic Meta-Predictor" = "purple",
"Best Linear Meta-Predictor" = "#006633",
"Original Predictor" = "blue",
"True CATE" = "black")
) +
theme_minimal()
cor_tau <- cor(tau.iso.hat, cate)
# Calculate the regression coefficients
fit <- lm(cate ~ tau.iso.hat, data = data.frame(tau.iso.hat, cate))
intercept <- coef(fit)[1]
slope <- coef(fit)[2]
# Display the plot
print(p)
```
We see now that the linearly calibrated predictor is well calibrated in a linear sense. The fit cannot be improved by fitting a linear model on top of the predictor.
We have correctly estimated the BLP.
However, we see that the BLP approach is unable to correct for the poor calibration (flattening) at the end regions. This is because BLP is parametric and can only calibrate by applying linear transformations to the predictor. A more nonparametric approach can correct for nonlinear transformation.