/
drivers.py
203 lines (145 loc) · 8.96 KB
/
drivers.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
import numpy as np
import pandas as pd
import numpy.matlib
from attrdict import AttrDict
import os, sys, torch, json
import ccpe, erm, model, utils
from data.benchmarks import synthetic, ohie, jobs
def run_hyperparam_exp(config, baselines, error_params, exp_name):
'''
Vary epochs and learning rate over different sample sizes
'''
exp_path = f'{config.log_dir}/{exp_name}/'
utils.write_file(json.dumps(config), exp_path, f'config.json')
po_results = []
for NS in config.sample_sizes:
for lr in [.001]:
for n_epochs in [30]:
print('==================================================================================')
print(f"NS: {NS}, LR: {lr}, epochs: {n_epochs}")
print('================================================================================== \n')
config.lr = lr
config.n_epochs = n_epochs
_, po_baseline_metrics = erm.run_model_comparison(config, baselines, error_params[0], NS)
for result in po_baseline_metrics:
result['lr'] = lr
result['n_epochs'] = n_epochs
po_results.extend(po_baseline_metrics)
po_df = pd.DataFrame(po_results)
utils.write_file(po_df, exp_path, f'runs={config.n_runs}_hyperparamexp_benchmark={config.benchmark.name}_samples={NS}_PO.csv')
return po_df
###########################################################
######## Risk minimmization experiments
###########################################################
def run_benchmark_risk_minimization_exp(config, baselines, param_configs, exp_name):
'''
Run each error parameter configuration over each benchmark environment. Keep sample size fixed.
'''
exp_path = f'{config.log_dir}/{exp_name}/'
utils.write_file(json.dumps(config), exp_path, f'config.json')
for error_params in param_configs:
te_results = []
po_results = []
for benchmark in config.benchmarks:
config.benchmark = benchmark
config.identification_pair = config['identification_pair']
for run_num in range(config.n_runs):
print('===============================================================================================================')
print(f"Benchmark: {config.benchmark.name}, RUN: {run_num}, alpha_0: {error_params.alpha_0}, alpha_1: {error_params.alpha_1}, beta_0: {error_params.beta_0}, beta_1: {error_params.beta_1}")
print('=============================================================================================================== \n')
te_baseline_metrics, po_baseline_metrics = erm.run_model_comparison(config, baselines, error_params)
te_results.extend(te_baseline_metrics)
po_results.extend(po_baseline_metrics)
po_df, te_df = pd.DataFrame(po_results), pd.DataFrame(te_results)
utils.write_file(po_df, exp_path, f'runs={config.n_runs}_epochs={config.n_epochs}_alpha={error_params.alpha_0}_beta={error_params.beta_0}_PO.csv')
utils.write_file(te_df, exp_path, f'runs={config.n_runs}_epochs={config.n_epochs}_alpha={error_params.alpha_0}_beta={error_params.beta_0}_TE.csv')
return po_df, te_df
def run_param_assumption_risk_minimization_exp(config, baselines, param_configs, exp_name):
'''
Run each error parameter configuration over each benchmark environment. Keep sample size fixed.
'''
exp_path = f'{config.log_dir}/{exp_name}/'
utils.write_file(json.dumps(config), exp_path, f'config.json')
for error_params in param_configs:
te_results = []
po_results = []
for benchmark in config.benchmarks:
config.benchmark = benchmark
for identification_pair in config.assumptions:
config.identification_pair = identification_pair
for run_num in range(config.n_runs):
print('===============================================================================================================')
print(f"Benchmark: {config.benchmark.name}, RUN: {run_num}, alpha_0: {error_params.alpha_0}, alpha_1: {error_params.alpha_1}, beta_0: {error_params.beta_0}, beta_1: {error_params.beta_1}")
print('=============================================================================================================== \n')
te_baseline_metrics, po_baseline_metrics = erm.run_model_comparison(config, baselines, error_params)
te_results.extend(te_baseline_metrics)
po_results.extend(po_baseline_metrics)
po_df, te_df = pd.DataFrame(po_results), pd.DataFrame(te_results)
utils.write_file(po_df, exp_path, f'runs={config.n_runs}_epochs={config.n_epochs}_alpha={error_params.alpha_0}_beta={error_params.beta_0}_PO.csv')
utils.write_file(te_df, exp_path, f'runs={config.n_runs}_epochs={config.n_epochs}_alpha={error_params.alpha_0}_beta={error_params.beta_0}_TE.csv')
return po_df, te_df
def run_risk_minimization_exp(config, baselines, param_configs, exp_name):
'''
Vary sample size for synthetic benchmark environment.
'''
exp_path = f'{config.log_dir}/{exp_name}/'
utils.write_file(json.dumps(config), exp_path, f'config.json')
for NS in config.sample_sizes:
te_results = []
po_results = []
config.benchmark.NS = NS
for error_params in param_configs:
for run_num in range(config.n_runs):
print('===============================================================================================================')
print(f"NS: {NS}, RUN: {run_num}, alpha_0: {error_params.alpha_0}, alpha_1: {error_params.alpha_1}, beta_0: {error_params.beta_0}, beta_1: {error_params.beta_1}")
print('=============================================================================================================== \n')
te_baseline_metrics, po_baseline_metrics = erm.run_model_comparison(config, baselines, error_params, NS)
te_results.extend(te_baseline_metrics)
po_results.extend(po_baseline_metrics)
po_df, te_df = pd.DataFrame(po_results), pd.DataFrame(te_results)
utils.write_file(po_df, exp_path, f'runs={config.n_runs}_epochs={config.n_epochs}_benchmark={config.benchmark.name}_samples={NS}_PO.csv')
utils.write_file(te_df, exp_path, f'runs={config.n_runs}_epochs={config.n_epochs}_benchmark={config.benchmark.name}_samples={NS}_TE.csv')
return po_df, te_df
###########################################################
######## Parameter estimation experiments
###########################################################
def run_ccpe_exp(config, error_param_configs, sample_sizes, do=0):
results = []
for error_params in error_param_configs:
for NS in sample_sizes:
config.benchmark.update({'NS': NS})
for RUN in range(config.n_runs):
X_train, X_test, Y_train, Y_test = loader.get_benchmark(config.benchmark, error_params)
split_ix = int(X.shape[0]*config.train_test_ratio)
dataset = {
'X_train': X[:split_ix],
'Y_train': Y[:split_ix],
'X_test': X[split_ix:],
'Y_test': Y[split_ix:]
}
alpha_hat, beta_hat = ccpe_multiestimate(dataset, do, config)
results.append({
'NS': NS,
'benchmark': config.benchmark.name,
'alpha': error_params[f'alpha_{do}'],
'beta': error_params[f'beta_{do}'],
'alpha_hat': alpha_hat,
'beta_hat': beta_hat,
'alpha_error': error_params[f'alpha_{do}'] - alpha_hat,
'beta_error': error_params[f'beta_{do}'] - beta_hat
})
ccpe_results = pd.DataFrame(results)
path = f'{config.log_dir}/{exp_name}/'
utils.write_file(ccpe_results, path, f'{config.log_dir}/parameter_estimation_runs={config.n_runs}_epochs={config.n_epochs}_benchmark={config.benchmark.name}.csv')
return ccpe_results
if __name__ == '__main__':
exp_type, exp_name = sys.argv[1], sys.argv[2]
config = AttrDict(json.load(open(f'configs/{exp_name}.json')))
if exp_type == 'erm':
run_risk_minimization_exp(config, config.baselines, config.error_params, exp_name)
if exp_type == 'erm_hyperparam':
run_hyperparam_exp(config, config.baselines, config.error_params, exp_name)
if exp_type == 'erm_experimental':
run_benchmark_risk_minimization_exp(config, config.baselines, config.error_params, exp_name)
if exp_type == 'erm_experimental_assumption':
run_param_assumption_risk_minimization_exp(config, config.baselines, config.error_params, exp_name)