-
Notifications
You must be signed in to change notification settings - Fork 1
/
exp_data.py
169 lines (152 loc) · 5.7 KB
/
exp_data.py
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
import numpy as np
import pandas as pd
def meaningless_discrete_dataset_(num, treatment_effct,
confounder_n=2,
w_var=5,
eps=1e-4,
data_frame=True,
random_seed=2022,
instrument=False):
"""Generate a dataset where the treatment and outcome have some
confounders while the relation between the treatment and outcome
is linear. The treatment is an array of integers where each integer
indicates the treatment group assigned to the corresponding example.
The outcome is an array of float, i.e., we are building continuous
outcome.
Parameters
----------
num : int
The number of examples in the dataset.
confounder_n : int
The number of confounders of the treatment and outcome.
treatment_effct : list, optional. Defaults to None.
w_var : float, optional. Defaults to 0.5.
Variance of the confounder around its mean.
eps : float, optional. Defaults to 1e-4.
Noise level imposed to the data generating process.
data_frame : bool, optional. Defaults to True.
Return pandas.DataFrame if True.
random_seed : int, optional. Defaults to 2022.
instrument : bool, optional. Defaults to False.
Add instrument variables to the dataset if True.
Returns
----------
pandas.DataFrame, optional.
w_j's are confounders of outcome and treatment.
"""
np.random.seed(random_seed)
# Build treatment x which depends on the confounder w
x_num = len(treatment_effct)
w = [
np.random.normal(0, w_var*np.random.random_sample(), size=(num, 1))
for i in range(confounder_n)
]
w = np.concatenate(tuple(w), axis=1)
w_coef = np.random.rand(x_num, confounder_n)
x = w.dot(w_coef.T) + np.random.normal(0, eps, size=(num, x_num))
if instrument:
z = None
x = x.argmax(axis=1)
x_one_hot = np.eye(x_num)[x]
# Now we build the outcome y which depends on both x and w
x_coef = np.random.randn(1, confounder_n)
x_coef = np.concatenate(
(np.array(treatment_effct).reshape(1, -1), x_coef), axis=1
)
x_ = np.concatenate((x_one_hot, w), axis=1)
y = x_.dot(x_coef.T) + np.random.normal(0, eps, size=(num, 1))
# Return the dataset
if data_frame:
data_dict = {}
data_dict['treatment'] = x
if instrument:
data_dict['instrument'] = z
for i, j in enumerate(w.T):
data_dict[f'w_{i}'] = j
data_dict['outcome'] = y.reshape(num,)
data = pd.DataFrame(data_dict)
return data
else:
if instrument:
return (x, w, z, y)
else:
return (x, w, y)
def coupon_dataset(n_users, treatment_style='binary', with_income=False):
if with_income:
income = np.random.normal(500, scale=15, size=n_users)
gender = np.random.randint(0, 2, size=n_users)
coupon = gender * 20 + 110 + income / 50 \
+ np.random.normal(scale=5, size=n_users)
if treatment_style == 'binary':
coupon = (coupon > 120).astype(int)
amount = coupon * 150 + gender * 100 + 150 \
+ income / 5 + np.random.normal(size=n_users)
time_spent = coupon * 10 + amount / 10
df = pd.DataFrame({
'gender': gender,
'coupon': coupon,
'amount': amount,
'income': income,
'time_spent': time_spent,
})
else:
gender = np.random.randint(0, 2, size=n_users)
coupon = gender * 20 + 150 + np.random.normal(scale=5, size=n_users)
if treatment_style == 'binary':
coupon = (coupon > 150).astype(int)
amount = coupon * 30 + gender * 100 \
+ 150 + np.random.normal(size=n_users)
time_spent = coupon * 100 + amount / 10
df = pd.DataFrame({
'gender': gender,
'coupon': coupon,
'amount': amount,
'time_spent': time_spent,
})
return df
def meaningless_discrete_dataset(num, confounder_n,
treatment_effct=None,
prob=None,
w_var=0.5,
eps=1e-4,
coef_range=5e4,
data_frame=True,
random_seed=2022):
np.random.seed(random_seed)
samples = np.random.multinomial(num, prob)
# build treatment x with shape (num,), where the number of types
# of treatments is len(prob) and each treatment i is assigned a
# probability prob[i]
x = []
for i, sample in enumerate(samples):
x += [i for j in range(sample)]
np.random.shuffle(x)
# construct the confounder w
w = [
np.random.normal(0, w_var, size=(num,)) for i in range(confounder_n)
]
for i, w_ in enumerate(w, 1):
x = x + w_
x = np.round(x).astype(int)
for i, j in enumerate(x):
if j > len(prob) - 1:
x[i] = len(prob) - 1
elif j < 0:
x[i] = 0
# construct the outcome y
coef = np.random.randint(int(coef_range*eps), size=(confounder_n,))
y = np.random.normal(eps, size=(num,))
for i in range(len(y)):
y[i] = y[i] + treatment_effct[x[i]] * x[i]
for i, j in zip(coef, w):
y += i * j
if data_frame:
data_dict = {}
data_dict['treatment'] = x
for i, j in enumerate(w):
data_dict[f'w_{i}'] = j
data_dict['outcome'] = y
data = pd.DataFrame(data_dict)
return data, coef
else:
return (x, w, y, coef)