-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrd2d_illustration.py
More file actions
218 lines (188 loc) · 5.95 KB
/
Copy pathrd2d_illustration.py
File metadata and controls
218 lines (188 loc) · 5.95 KB
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
################################################################################
# RD2D Python Package
# Numerical Illustration
################################################################################
from __future__ import annotations
import os
import sys
from pathlib import Path
import numpy as np
import pandas as pd
if Path("rd2d").exists() and str(Path("rd2d").resolve()) not in sys.path:
sys.path.insert(0, str(Path("rd2d").resolve()))
from rd2d import rdbw2d, rdbw2d_dist, rd2d, rd2d_dist # noqa: E402
# Generate boundary evaluation points.
def make_eval_grid(neval: int = 40) -> pd.DataFrame:
half = int(np.ceil(neval / 2))
first = pd.DataFrame(
{
"x.1": np.zeros(half),
"x.2": 40 - np.arange(half) * 40 / half,
}
)
second = pd.DataFrame(
{
"x.1": np.arange(neval - half) * 56 / half,
"x.2": np.zeros(neval - half),
}
)
return pd.concat([first, second], ignore_index=True)
# Generate signed distances to each boundary evaluation point.
def make_signed_distances(
X: pd.DataFrame,
eval_points: pd.DataFrame,
assignment: np.ndarray,
) -> np.ndarray:
distance = np.column_stack(
[
np.sqrt(
(X["x.1"] - row["x.1"]) ** 2
+ (X["x.2"] - row["x.2"]) ** 2
)
for _, row in eval_points.iterrows()
]
)
return distance * (2 * assignment[:, None] - 1)
# Select displayed point rows and aggregate rows.
def display_table(table: pd.DataFrame, selected: list[int]) -> pd.DataFrame:
point_rows = table.iloc[selected]
aggregate_rows = table.loc[table.index.isin(["WBATE", "LBATE"])]
return pd.concat([point_rows, aggregate_rows])
# Run the illustration.
def main() -> None:
dat = pd.read_csv("rd2d_data.csv")
X = dat[["x.1", "x.2"]]
Y = dat["Y"]
A = dat["assignment"]
D = dat["fuzzy"]
Z = dat[["Z.1", "Z.2"]]
neval = int(os.getenv("RD2D_ILLUSTRATION_NEVAL", "40"))
repp = int(os.getenv("RD2D_ILLUSTRATION_REPP", "499"))
eval_points = make_eval_grid(neval)
distance = make_signed_distances(X, eval_points, A.to_numpy())
wbate_weights = np.ones(len(eval_points))
selected = [i - 1 for i in [1, 5, 10, 15, 21, 25, 30, 35, 40] if i <= neval]
# Location-based bandwidth selection with covariate adjustment.
bw_location = rdbw2d(Y, X, A, eval_points, covs_eff=Z, fitmethod="joint", masspoints="off")
print(bw_location.bws.iloc[selected])
# Location-based fuzzy estimation.
fit_location = rd2d(
Y,
X,
A,
eval_points,
fuzzy=D,
params_other="itt.0",
params_cov=["main", "itt", "fs", "itt.0"],
covs_eff=Z,
fitmethod="joint",
masspoints="off",
)
# Location-based fuzzy main effect.
summary_location_main = fit_location.summary(
output="main",
cbands="main",
repp=repp,
WBATE=wbate_weights,
LBATE=True,
)
print(display_table(summary_location_main.tables["main"], selected))
# Location-based reduced-form effect.
summary_location_itt = fit_location.summary(
output="itt",
cbands="itt",
repp=repp,
WBATE=wbate_weights,
LBATE=True,
)
print(display_table(summary_location_itt.tables["itt"], selected))
# Location-based first-stage effect.
summary_location_fs = fit_location.summary(
output="fs",
cbands="fs",
repp=repp,
WBATE=wbate_weights,
LBATE=True,
)
print(display_table(summary_location_fs.tables["fs"], selected))
# Location-based control-side reduced-form effect.
summary_location_itt0 = fit_location.summary(
output="itt.0",
cbands="itt.0",
repp=repp,
WBATE=wbate_weights,
LBATE=True,
)
print(display_table(summary_location_itt0.tables["itt.0"], selected))
# Distance-based bandwidth selection with covariate adjustment.
bw_distance = rdbw2d_dist(Y, distance, b=eval_points, covs_eff=Z, fitmethod="joint", masspoints="off")
print(bw_distance.bws.iloc[selected])
# Distance-based sharp estimation.
fit_distance = rd2d_dist(
Y,
distance,
b=eval_points,
covs_eff=Z,
fitmethod="joint",
masspoints="off",
cbands=True,
)
summary_distance = fit_distance.summary(
output="main",
cbands="main",
repp=repp,
)
print(display_table(summary_distance.tables["main"], selected))
# Distance-based fuzzy bandwidth selection.
bw_distance_fuzzy = rdbw2d_dist(
Y,
distance,
b=eval_points,
fuzzy=D,
covs_eff=Z,
fitmethod="joint",
bwparam="itt",
masspoints="off",
)
print(bw_distance_fuzzy.bws.iloc[selected])
# Distance-based fuzzy estimation.
fit_distance_fuzzy = rd2d_dist(
Y,
distance,
b=eval_points,
fuzzy=D,
covs_eff=Z,
fitmethod="joint",
bwparam="itt",
params_cov=["main", "itt", "fs"],
masspoints="off",
)
# Distance-based fuzzy main effect.
summary_distance_main = fit_distance_fuzzy.summary(
output="main",
cbands="main",
repp=repp,
WBATE=wbate_weights,
LBATE=True,
)
print(display_table(summary_distance_main.tables["main"], selected))
# Distance-based reduced-form effect.
summary_distance_itt = fit_distance_fuzzy.summary(
output="itt",
cbands="itt",
repp=repp,
WBATE=wbate_weights,
LBATE=True,
)
print(display_table(summary_distance_itt.tables["itt"], selected))
# Distance-based first-stage effect.
summary_distance_fs = fit_distance_fuzzy.summary(
output="fs",
cbands="fs",
repp=repp,
WBATE=wbate_weights,
LBATE=True,
)
print(display_table(summary_distance_fs.tables["fs"], selected))
if __name__ == "__main__":
main()