-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfunctions.py
548 lines (419 loc) · 17.1 KB
/
functions.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
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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
""" Ignore Warnings """
def warn(*args, **kwargs):
pass
import warnings
warnings.warn = warn
""" Imports """
import numpy as np
import pandas as pd
import sobol_seq
from scipy.stats.distributions import entropy
import matplotlib.pylab as plt
import seaborn as sns
import numba
""" surrogate models """
# Xtreeme Gradient Boosted Decision Trees
from xgboost import XGBRegressor, XGBClassifier
# Gaussian Process Regression (Kriging)
# modified version of kriging to make a fair comparison with regard
# to the number of hyperparameter evaluations
from sklearn.gaussian_process import GaussianProcessRegressor
""" cross-validation
Cross validation is used in each of the rounds to approximate the selected
surrogate model over the data samples that are available.
The evaluated parameter combinations are randomly split into two sets. An
in-sample set and an out-of-sample set. The surrogate is trained and its
parameters are tuned to an in-sample set, while the out-of-sample performance
is measured (using a selected performance metric) on the out-of-sample set.
This out-of-sample performance is then used as a proxy for the performance
on the full space of unevaluated parameter combinations. In the case of the
proposed procedure, this full space is approximated by the randomly selected
pool.
"""
from sklearn.model_selection import cross_val_score, StratifiedKFold, KFold
from skopt import gp_minimize
""" performance metric """
# Mean Squared Error
from sklearn.metrics import mean_squared_error, f1_score
""" Defaults Algorithm Tuning Constants """
_N_EVALS = 10
_N_SPLITS = 5
_CALIBRATION_THRESHOLD = 1.00
""" Functions """
numba.jit()
def unique_rows(a):
a = np.ascontiguousarray(a)
unique_a = np.unique(a.view([('', a.dtype)] * a.shape[1]))
return unique_a.view(a.dtype).reshape((unique_a.shape[0], a.shape[1]))
numba.jit()
def evaluate_islands_on_set(parameter_combinations):
y = np.zeros(parameter_combinations.shape[0])
num_params = parameter_combinations.shape[1]
if num_params == 1:
for i, rho in enumerate(parameter_combinations):
gdp = island_abm(rho=rho,
_RNG_SEED=0)
y[i] = calibration_measure(gdp)
elif num_params == 2:
for i, (rho, alpha) in enumerate(parameter_combinations):
gdp = island_abm(rho=rho, alpha=alpha,
_RNG_SEED=0)
y[i] = calibration_measure(gdp)
elif num_params == 3:
for i, (rho, alpha, phi) in enumerate(parameter_combinations):
gdp = island_abm(rho=rho, alpha=alpha, phi=phi,
_RNG_SEED=0)
y[i] = calibration_measure(gdp)
elif num_params == 4:
for i, (rho, alpha, phi, pi) in enumerate(parameter_combinations):
gdp = island_abm(rho=rho, alpha=alpha, phi=phi,
pi=pi, _RNG_SEED=0)
y[i] = calibration_measure(gdp)
elif num_params == 5:
for i, (rho, alpha, phi, pi, eps) in enumerate(parameter_combinations):
gdp = island_abm(rho=rho, alpha=alpha, phi=phi,
pi=pi, eps=eps, _RNG_SEED=0)
y[i] = calibration_measure(gdp)
return y
numba.jit()
def island_abm(rho=0.01,
alpha=1.5,
phi=0.4,
pi=0.4,
eps=0.1,
lambda_param=1,
T=100,
N=50,
_RNG_SEED=0):
""" Islands growth model
Parameters
----------
rho :
alpha :
phi : float, required
eps :
lambda_param: (Default = 1)
T : int, required
The number of periods for the simulation
N : int, optional (Default = 50)
Number of firms
_RNG_SEED : int, optional (Default = 0)
Random number seen
Output
------
GDP : array, length = [,T]
Simulated GPD
"""
# Set random number seed
np.random.seed(_RNG_SEED)
T_2 = int(T / 2)
GDP = np.zeros((T, 1))
# Distributions
# Precompute random binomial draws
xy = np.random.binomial(1, pi, (T, T))
xy[T_2, T_2] = 1
# Containers
s = np.zeros((T, T))
A = np.ones((N, 6))
# Initializations
A[:, 1] = T_2
A[:, 2] = T_2
m = np.zeros((T, T))
m[T_2, T_2] = N
dest = np.zeros((N, 2))
""" Begin ABM Code """
for t in range(T):
w = np.zeros((N, N))
signal = np.zeros((N, N))
for i in range(N):
for j in range(N):
if i != j:
if A[j, 0] == 1:
w[i, j] = np.exp(-rho * (np.abs(A[j, 1] - A[i, 1]) + \
np.abs(A[j, 2] - A[i, 2])))
if np.random.rand() < w[i, j]:
signal[i, j] = s[int(A[j, 1]), int(A[j, 2])]
if A[i, 0] == 1:
A[i, 4] = s[int(A[i, 1]), int(A[i, 2])] * \
m[int(A[i, 1]), int(A[i, 2])] ** alpha
A[i, 3] = s[int(A[i, 1]), int(A[i, 2])]
if A[i, 0] == 3:
A[i, 4] = 0
rnd = np.random.rand()
if rnd <= 0.25:
A[i, 1] += 1
else:
if rnd <= 0.5:
A[i, 1] -= 1
else:
if rnd <= 0.75:
A[i, 2] += 1
else:
A[i, 2] -= 1
if xy[int(A[i, 1]), int(A[i, 2])] == 1:
A[i, 0] = 1
m[int(A[i, 1]), int(A[i, 2])] += 1
if m[int(A[i, 1]), int(A[i, 2])] == 1:
s[int(A[i, 1]), int(A[i, 2])] = \
(1 + int(np.random.poisson(lambda_param))) * \
(A[i, 1] + A[i, 2]) + phi * A[i, 5] + np.random.randn()
if (A[i, 0] == 1) and (np.random.rand() <= eps):
A[i, 0] = 3
A[i, 5] = A[i, 4]
m[int(A[i, 1]), int(A[i, 2])] -= 1
if t > T / 100:
if A[i, 0] == 2:
A[i, 4] = 0
if dest[i, 0] != A[i, 1]:
if dest[i, 0] > A[i, 1]:
A[i, 1] += 1
else:
A[i, 1] -= 1
else:
if dest[i, 1] != A[i, 2]:
if dest[i, 1] > A[i, 2]:
A[i, 2] += 1
else:
A[i, 2] -= 1
if (dest[i, 0] == A[i, 1]) and (dest[i, 1] == A[i, 2]):
A[i, 0] = 1
m[int(dest[i, 0]), int(dest[i, 1])] += 1
if A[i, 0] == 1:
best_sig = np.max(signal[i, :])
if best_sig > s[int(A[i, 1]), int(A[i, 2])]:
A[i, 0] = 2
A[i, 5] = A[i, 4]
m[int(A[i, 1]), int(A[i, 2])] -= 1
index = np.where(signal[i, :] == best_sig)[0]
if index.shape[0] > 1:
ind = int(index[int(np.random.uniform(0, len(index)))])
else:
ind = int(index)
dest[i, 0] = A[ind, 1]
dest[i, 1] = A[ind, 2]
GDP[t, 0] = np.sum(A[:, 4])
log_GDP = np.log(GDP)
return log_GDP
numba.jit()
def calibration_measure(log_GDP):
""" Calibration Measure
Input
-----
GDP : array, required, length = [,T]
Output
------
agdp : float
Average GDP growth rate
"""
T = log_GDP.shape[0]
log_GDP = log_GDP[~np.isinf(log_GDP)]
log_GDP = log_GDP[~np.isnan(log_GDP)]
if log_GDP.shape[0] > 0:
GDP_growth_rate = (log_GDP[-1] - log_GDP[0]) / T
else:
GDP_growth_rate = 0
return GDP_growth_rate
numba.jit()
def calibration_condition(average_GDP_growth_rate, threshold_condition):
return average_GDP_growth_rate >= threshold_condition
numba.jit()
def set_surrogate_as_gbt():
""" Set the surrogate model as Gradient Boosted Decision Trees
Helper function to set the surrogate model and parameter space
as Gradient Boosted Decision Trees.
For detail, see:
http://scikit-learn.org/stable/modules/generated/
sklearn.ensemble.GradientBoostingRegressor.html
Parameters
----------
None
Returns
-------
surrogate_model :
surrogate_parameter_space :
"""
surrogate_model = XGBRegressor(seed=0)
surrogate_parameter_space = [
(100, 1000), # n_estimators
(0.01, 1), # learning_rate
(10, 1000), # max_depth
(0.0, 1), # reg_alpha
(0.0, 1), # reg_lambda
(0.25, 1.0)] # subsample
return surrogate_model, surrogate_parameter_space
numba.jit()
def custom_metric_regression(y_hat, y):
return 'MSE', mean_squared_error(y.get_label(), y_hat)
numba.jit()
def custom_metric_binary(y_hat, y):
return 'MSE', f1_score(y.get_label(), y_hat, average='weighted')
numba.jit()
def fit_surrogate_model(X, y):
""" Fit a surrogate model to the X,y parameter combinations
Parameters
----------
surrogate_model :
X :
y :
Output
------
surrogate_model_fitted : A surrogate model fitted
"""
surrogate_model, surrogate_parameter_space = set_surrogate_as_gbt()
def objective(params):
n_estimators, learning_rate, max_depth, reg_alpha, \
reg_lambda, subsample = params
reg = XGBRegressor(n_estimators=n_estimators,
learning_rate=learning_rate,
max_depth=max_depth,
reg_alpha=reg_alpha,
reg_lambda=reg_lambda,
subsample=subsample,
seed=0)
kf = KFold(n_splits=_N_SPLITS, random_state=0, shuffle=True)
kf_cv = [(train, test) for train, test in kf.split(X, y)]
return -np.mean(cross_val_score(reg,
X, y,
cv=kf_cv,
n_jobs=1,
fit_params={'eval_metric': custom_metric_regression},
scoring="neg_mean_squared_error"))
# use Gradient Boosted Regression to optimize the Hyper-Parameters.
surrogate_model_tuned = gp_minimize(objective,
surrogate_parameter_space,
n_calls=_N_EVALS,
acq_func='gp_hedge',
n_jobs=-1,
random_state=0, verbose=9)
surrogate_model.set_params(n_estimators=surrogate_model_tuned.x[0],
learning_rate=surrogate_model_tuned.x[1],
max_depth=surrogate_model_tuned.x[2],
reg_alpha=surrogate_model_tuned.x[3],
reg_lambda=surrogate_model_tuned.x[4],
subsample=surrogate_model_tuned.x[5],
seed=0)
surrogate_model.fit(X, y, eval_metric=custom_metric_regression)
return surrogate_model
numba.jit()
def fit_entropy_classifier(X, y, calibration_threshold):
""" Fit a surrogate model to the X,y parameter combinations
Parameters
----------
surrogate_model :
X :
y :
Output
------
surrogate_model_fitted : A surrogate model fitted
"""
y_binary = calibration_condition(y, calibration_threshold)
_, surrogate_parameter_space = set_surrogate_as_gbt()
def objective(params):
n_estimators, learning_rate, max_depth, reg_alpha, \
reg_lambda, subsample = params
clf = XGBClassifier(n_estimators=n_estimators,
learning_rate=learning_rate,
max_depth=max_depth,
reg_alpha=reg_alpha,
reg_lambda=reg_lambda,
subsample=subsample,
seed=0,
objective="binary:logistic")
skf = StratifiedKFold(n_splits=_N_SPLITS, random_state=0, shuffle=True)
skf_cv = [(train, test) for train, test in skf.split(X, y_binary)]
return -np.mean(cross_val_score(clf,
X, y_binary,
cv=skf_cv,
n_jobs=1,
fit_params={'eval_metric': custom_metric_binary},
scoring="f1_weighted"))
# use Gradient Boosted Regression to optimize the Hyper-Parameters.
clf_tuned = gp_minimize(objective,
surrogate_parameter_space,
n_calls=_N_EVALS,
acq_func='gp_hedge',
n_jobs=-1,
random_state=0)
clf = XGBClassifier(n_estimators=clf_tuned.x[0],
learning_rate=clf_tuned.x[1],
max_depth=clf_tuned.x[2],
reg_alpha=clf_tuned.x[3],
reg_lambda=clf_tuned.x[4],
subsample=clf_tuned.x[5],
seed=0)
clf.fit(X, y_binary, eval_metric=custom_metric_binary)
return clf
numba.jit()
def get_sobol_samples(n_dimensions, samples, parameter_support):
"""
"""
# Get the range for the support
support_range = parameter_support[:, 1] - parameter_support[:, 0]
# Generate the Sobol samples
random_samples = sobol_seq.i4_sobol_generate(n_dimensions, samples)
# Compute the parameter mappings between the Sobol samples and supports
sobol_samples = np.vstack([
np.multiply(s, support_range) + parameter_support[:, 0]
for s in random_samples])
return sobol_samples
numba.jit()
def get_unirand_samples(n_dimensions, samples, parameter_support):
"""
"""
# Get the range for the support
support_range = parameter_support[:, 1] - parameter_support[:, 0]
# Generate the Sobol samples
random_samples = np.random.rand(n_dimensions,samples).T
# Compute the parameter mappings between the Sobol samples and supports
unirand_samples = np.vstack([
np.multiply(s, support_range) + parameter_support[:, 0]
for s in random_samples])
return unirand_samples
numba.jit()
def get_round_selections(evaluated_set_X, evaluated_set_y,
unevaluated_set_X,
predicted_positives, num_predicted_positives,
samples_to_select, calibration_threshold,
budget):
"""
"""
samples_to_select = np.min([abs(budget - evaluated_set_y.shape[0]),
samples_to_select]).astype(int)
if num_predicted_positives >= samples_to_select:
round_selections = int(samples_to_select)
selections = np.where(predicted_positives == True)[0]
selections = np.random.permutation(selections)[:round_selections]
elif num_predicted_positives <= samples_to_select:
# select all predicted positives
selections = np.where(predicted_positives == True)[0]
# select remainder according to entropy weighting
budget_shortfall = int(samples_to_select - num_predicted_positives)
selections = np.append(selections,
get_new_labels_entropy(evaluated_set_X, evaluated_set_y,
unevaluated_set_X,
calibration_threshold,
budget_shortfall))
else: # if we don't have any predicted positive calibrations
selections = get_new_labels_entropy(clf, unevaluated_set_X, samples_to_select)
to_be_evaluated = unevaluated_set_X[selections]
unevaluated_set_X = np.delete(unevaluated_set_X, selections, 0)
evaluated_set_X = np.vstack([evaluated_set_X, to_be_evaluated])
evaluated_set_y = np.append(evaluated_set_y, evaluate_islands_on_set(to_be_evaluated))
return evaluated_set_X, evaluated_set_y, unevaluated_set_X
numba.jit()
def get_new_labels_entropy(evaluated_set_X, evaluated_set_y,
unevaluated_X, calibration_threshold,
number_of_new_labels):
""" Get a set of parameter combinations according to their predicted label entropy
"""
clf = fit_entropy_classifier(evaluated_set_X, evaluated_set_y, calibration_threshold)
y_hat_probability = clf.predict_proba(unevaluated_X)
y_hat_entropy = np.array(map(entropy, y_hat_probability))
y_hat_entropy /= y_hat_entropy.sum()
unevaluated_X_size = unevaluated_X.shape[0]
selections = np.random.choice(a=unevaluated_X_size,
size=number_of_new_labels,
replace=False,
p=y_hat_entropy)
return selections
print ("Imported successfully")