-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME.qmd
202 lines (149 loc) · 4.83 KB
/
README.qmd
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
---
format: gfm
jupyter: python3
---
# TreatmentEffectRisk
This repository is an implementation of the Treatment Effect Risk: Bounds and Inference package, based on the replication by Kallus (2024).
# References
- Paper: [Treatment Effect Risk: Bounds and Inference](https://arxiv.org/abs/2201.05893)
- Original Repository: [CausalML/TreatmentEffectRisk](https://github.com/CausalML/TreatmentEffectRisk)
# License
This project is licensed under the MIT License - see the LICENSE file for details.
<!-- ## MAP
![](./original_pkg/functions.png) -->
# Contact
For questions or inquiries about the package, please contact [fr.jhonk@gmail.com].
# Usage
```{python}
# pip install TrER==0.1.41
```
```{python}
import numpy as np
import pandas as pd
url_csv = 'https://raw.githubusercontent.com/CausalML/TreatmentEffectRisk/main/data/behaghel.csv'
import warnings
warnings.filterwarnings("ignore")
```
```{python}
job = pd.read_csv(url_csv)
Xbin = [
'College_education', 'nivetude2', 'Vocational', 'High_school_dropout',
'Manager', 'Technician', 'Skilled_clerical_worker', 'Unskilled_clerical_worker',
'Skilled_blue_colar', 'Unskilled_blue_colar', 'Woman', 'Married', 'French',
'African', 'Other_Nationality', 'Paris_region', 'North', 'Other_regions',
'Employment_component_level_1', 'Employment_component_level_2',
'Employment_component_missing', 'Economic_Layoff', 'Personnal_Layoff',
'End_of_Fixed_Term_Contract', 'End_of_Temporary_Work', 'Other_reasons_of_unemployment',
'Statistical_risk_level_2', 'Statistical_risk_level_3', 'Other_Statistical_risk',
'Search_for_a_full_time_position', 'Sensitive_suburban_area', 'Insertion',
'Interim', 'Conseil'
]
Xnum = [
'age', 'Number_of_children', 'exper', 'salaire.num', 'mois_saisie_occ', 'ndem'
]
Xall = Xbin + Xnum
job_binary = job[(job['A_public'] == 1) | (job['A_private'] == 1)].copy()
job_binary['sw'] = job_binary['sw'] / job_binary['sw'].mean()
job_binary['A'] = job_binary['A_public']
job_binary['ipw'] = 1 / (
job_binary['A_standard'] * job_binary['sw'] * job_binary['A_standard'].mean() +
job_binary['A_private'] * job_binary['sw'] * job_binary['A_private'].mean() +
job_binary['A_public'] * job_binary['sw'] * job_binary['A_public'].mean()
)
```
## Imports
```{python}
from TrER.utils import make_cvgroup_balanced
from TrER.models import tau_predict, mu_calculate, var_calculate
from TrER.plots import plot_cvar_group, plot_cvar_groups_with_markers
from TrER.plots import plot_cvar, plot_CVAR_TE
from TrER.gen_cvar import cvar_tau, cvar_plugin, cvar_mate, cvar_bbound_mate, cvar_bbounded, prep_bbounds_ate, job_condvar_gen, cvar_bbounds_ate
```
## Pre-processing
```{python}
ps = np.arange(0.01, 1.01, 0.01)
bs = np.arange(0, 0.30, 0.05)
rhos = [-1, -0.5, 0, 0.5, 0.9, 0.95, 1]
np.random.seed(0)
K = 5
cvgroup = make_cvgroup_balanced(job_binary, K, 'A')
job_binary['y_ref'] = (2 * job_binary['A'] - 1) * job_binary['ipw'] * job_binary['Y']
# X and y for the model
X = job_binary[Xall] # Assuming Xall is a list of column names
y = job_binary['y_ref']
tau_pred = tau_predict(X, y, job_binary['sw'])
bad_c = ['age','Paris_region','African','High_school_dropout']
Xbad = job_binary[bad_c]
tau_pred_bad = tau_predict(Xbad, y, job_binary['sw'])
```
## Estimation
```{python}
y = job_binary['Y']
X = job_binary[Xall]
mu0, mu1 = mu_calculate(
job_binary, cvgroup, y, X
)
```
```{python}
job_binary['mu0'] = mu0
job_binary['mu1'] = mu1
var0, var1 = var_calculate(job_binary, job_binary[Xall], cvgroup)
job_binary['var0'] = var0
job_binary['var1'] = var1
job_binary['tau'] = tau_pred
job_binary['tau_bad'] = tau_pred_bad
```
## Calculations and plotting
```{python}
CVaR = cvar_tau(job_binary, ps)
plot_cvar(CVaR, rearrangement=False)
plot_cvar(CVaR, rearrangement=True)
```
```{python}
cvar_p = cvar_plugin(job_binary, ps)
plot_cvar(cvar_p)
```
```{python}
cvar_bad = cvar_tau(job_binary, ps, tau_col='tau_bad')
plot_cvar(cvar_bad, rearrangement=True)
```
```{python}
cvarmate = cvar_mate(job_binary, ps)
plot_cvar (cvarmate , rearrangement= True)
```
```{python}
cvar_bbound_mate_df = cvar_bbound_mate(job_binary, ps, bs)
plot_cvar_group(cvar_bbound_mate_df)
```
```{python}
df_bounded = cvar_bbounded(cvarmate, cvar_bbound_mate_df, bs)
plot_cvar_groups_with_markers(
df_bounded, "p", y = "CVaR"
)
```
```{python}
df_1, tot_var, cnd_1, cnd_2 = prep_bbounds_ate(job_binary)
sbound_mate = cvar_bbounds_ate (df_1, ps, rhos, tot_var)
```
```{python}
plot_cvar_group(
sbound_mate.query("p>0.25"),
group="rho",
y_label=r"${CVaR}_{\alpha}(\tau(X))-\bar\tau$",
)
```
```{python}
job_condvar = job_condvar_gen(cvarmate, sbound_mate, cnd_1, cnd_2)
plot_cvar_groups_with_markers(
job_condvar,
"p",
"CVaR",
main_group="rho",
cvar_se="CVaR_se",
y_label=r"${CVaR}_{\alpha}-\bar{\tau}$",
)
```
```{python}
plot_CVAR_TE(job_binary, ps)
```
<!---->