-
Notifications
You must be signed in to change notification settings - Fork 0
/
generative_model_estimation.py
249 lines (214 loc) · 8.31 KB
/
generative_model_estimation.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
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
# This module implements estimation of generative models used in the study
import numpy as np
from sklearn.mixture import GaussianMixture,BayesianGaussianMixture
from sdv.tabular import GaussianCopula, CTGAN, CopulaGAN, TVAE
import torch
from sklearn.model_selection import KFold
import math
from sdv.evaluation import evaluate
import awkde
from statsmodels.nonparametric.kernel_density import EstimatorSettings, KDEMultivariate
from sklearn.neighbors import KernelDensity
from sklearn.model_selection import GridSearchCV
import warnings
warnings.filterwarnings("ignore")
n_components_list = list(range(1, 5))
n_components_list.extend(list(range(5, 101, 5)))
def get_gmm_model(data, cv_rs):
"""Estimates GMM model
:param data: train dataset
:param cv_rs: random state for CV
:return gmm: generative model
"""
params = {'n_components': n_components_list,
'covariance_type': ['full', 'tied', 'diag', 'spherical']}
kf = KFold(n_splits=3, random_state=cv_rs, shuffle=True)
grid = GridSearchCV(GaussianMixture(random_state=42), params, cv=kf)
grid.fit(data)
gmm = grid.best_estimator_
return gmm
def get_bayesian_gmm_model(data, cv_rs):
"""Estimates Bayesian GMM model
:param data: train dataset
:param cv_rs: random state for CV
:return gmm: generative model
"""
params = {'n_components': list(range(1, 11, 1)),
'covariance_type': ['full', 'tied', 'diag', 'spherical'],
'weight_concentration_prior_type': ['dirichlet_process', 'dirichlet_distribution'],
'weight_concentration_prior': [0.001, 0.01, 0.1, 1, 10, 100, 1000, 5000, 10000]}
kf = KFold(n_splits=3, random_state=cv_rs, shuffle=True)
grid = GridSearchCV(BayesianGaussianMixture(random_state=42), params, cv=kf)
grid.fit(data)
gmm = grid.best_estimator_
return gmm
def get_copula_model(data):
"""Estimates Copula model
:param data: train dataset
:return model: generative model
"""
np.random.seed(42)
model = GaussianCopula(default_distribution="univariate")
model.fit(data)
return model
def get_ctgan_model(data, cv_rs):
"""Estimates CTGAN model
:param data: train dataset
:param cv_rs: random state for CV
:return model: generative model
"""
np.random.seed(42)
torch.manual_seed(42)
batch_size = math.ceil(len(data)/100)*100
skf = KFold(n_splits=3, shuffle=True, random_state=cv_rs)
res_metric = []
epoch_list = [500, 1000, 1500, 2000]
for epoch_v in epoch_list:
metric_list = []
for index in skf.split(data):
x_train, x_test = data.iloc[index[0]], data.iloc[index[1]]
model = CTGAN(epochs=epoch_v, batch_size=batch_size)
model.fit(x_train)
sub_metric_list = []
for i in range(10):
sampled_data = model.sample(len(x_test))
sub_metric_list.append(evaluate(sampled_data, x_test))
metric_list.append(np.mean(sub_metric_list))
res_metric.append(np.mean(metric_list))
opt_epoch = epoch_list[np.argmax(res_metric)]
model = CTGAN(epochs=opt_epoch, batch_size=batch_size)
model.fit(data)
return model
def get_copulagan_model(data, cv_rs):
"""Estimates CopulaGAN model
:param data: train dataset
:param cv_rs: random state for CV
:return model: generative model
"""
np.random.seed(42)
torch.manual_seed(42)
batch_size = math.ceil(len(data)/100)*100
skf = KFold(n_splits=3, shuffle=True, random_state=cv_rs)
res_metric = []
epoch_list = [500, 1000, 1500, 2000]
for epoch_v in epoch_list:
metric_list = []
for index in skf.split(data):
x_train, x_test = data.iloc[index[0]], data.iloc[index[1]]
model = CopulaGAN(epochs=epoch_v, batch_size=batch_size)
model.fit(x_train)
sub_metric_list = []
for i in range(10):
sampled_data = model.sample(len(x_test))
sub_metric_list.append(evaluate(sampled_data, x_test))
metric_list.append(np.mean(sub_metric_list))
res_metric.append(np.mean(metric_list))
opt_epoch = epoch_list[np.argmax(res_metric)]
model = CopulaGAN(epochs=opt_epoch, batch_size=batch_size)
model.fit(data)
return model
def get_tvae_model(data, cv_rs):
"""Estimates TVAE model
:param data: train dataset
:param cv_rs: random state for CV
:return model: generative model
"""
np.random.seed(42)
torch.manual_seed(42)
batch_size = math.ceil(len(data)/100)*100
skf = KFold(n_splits=3, shuffle=True, random_state=cv_rs)
res_metric = []
epoch_list = [500, 1000, 1500, 2000]
for epoch_v in epoch_list:
metric_list = []
for index in skf.split(data):
x_train, x_test = data.iloc[index[0]], data.iloc[index[1]]
model = TVAE(epochs=epoch_v, batch_size=batch_size)
model.fit(x_train)
sub_metric_list = []
for i in range(10):
sampled_data = model.sample(len(x_test))
sub_metric_list.append(evaluate(sampled_data, x_test))
metric_list.append(np.mean(sub_metric_list))
res_metric.append(np.mean(metric_list))
opt_epoch = epoch_list[np.argmax(res_metric)]
model = TVAE(epochs=opt_epoch, batch_size=batch_size)
model.fit(data)
return model
def sklearn_kde(data, cv_rs):
"""Estimates KDE (sklearn implementation)
:param data: train dataset
:param cv_rs: random state for CV
:return kde: generative model
"""
kf = KFold(n_splits=3, random_state=cv_rs, shuffle=True)
bw_range = np.arange(0.01, 1.01, 0.01)
params = {'bandwidth': bw_range}
grid = GridSearchCV(KernelDensity(kernel="gaussian"), params, cv=kf)
grid.fit(data)
if grid.best_estimator_.bandwidth == bw_range[0]:
bw_range = np.arange(0.0001, 0.01, 0.0001)
params = {'bandwidth': bw_range}
grid = GridSearchCV(KernelDensity(kernel="gaussian"), params, cv=kf)
grid.fit(data)
elif grid.best_estimator_.bandwidth == bw_range[-1]:
bw_range = np.arange(1.01, 2.01, 0.01)
params = {'bandwidth': bw_range}
grid = GridSearchCV(KernelDensity(kernel="gaussian"), params, cv=kf)
grid.fit(data)
kde = grid.best_estimator_
return kde
def awkde_kde(data, cv_rs):
"""Estimates AWKDE model
:param data: train dataset
:param cv_rs: random state for CV
:return kde: generative model
"""
kf = KFold(n_splits=3, random_state=cv_rs, shuffle=True)
params = {'alpha': [0.1, 0.3, 0.5, 0.7, 0.9]}
grid = GridSearchCV(awkde.GaussianKDE(), params, cv=kf)
grid.fit(data)
kde = grid.best_estimator_
return kde
def stats_kde(data, method, var_type, efficient=False):
"""Estimates KDE (Statsmodels)
:param data: train dataset
:param method: bandwidth selection method
:param var_type: type of variables
:param efficient: settings for bandwidth estimation
:return kde: generative model
"""
if efficient:
settings = EstimatorSettings(efficient=True, randomize=True)
kde = KDEMultivariate(data, var_type, bw=method, defaults=settings)
else:
kde = KDEMultivariate(data, var_type, bw=method)
return kde
def fit_model(gen_algorithm, data, cv_rs):
"""Calls a model estimation function
:param gen_algorithm: generative algorithm name
:param data: train dataset
:param cv_rs: random state for CV
:return model: generative model
"""
if gen_algorithm == "sklearn_kde":
model = sklearn_kde(data, cv_rs)
elif gen_algorithm == "awkde":
model = awkde_kde(data, cv_rs)
elif gen_algorithm in ["kde_cv_ml", "kde_cv_ls"]:
method_list = gen_algorithm.split("_")
method_name = method_list[1]+"_"+method_list[2]
model = stats_kde(data, method_name, "c"*data.shape[1])
elif gen_algorithm == "gmm":
model = get_gmm_model(data, cv_rs)
elif gen_algorithm == "bayesian_gmm":
model = get_bayesian_gmm_model(data, cv_rs)
elif gen_algorithm == "ctgan":
model = get_ctgan_model(data, cv_rs)
elif gen_algorithm == "copula":
model = get_copula_model(data)
elif gen_algorithm == "copulagan":
model = get_copulagan_model(data, cv_rs)
elif gen_algorithm == "tvae":
model = get_tvae_model(data, cv_rs)
return model