-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap_coefficients.py
325 lines (269 loc) · 9.13 KB
/
bootstrap_coefficients.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
import os
import numpy as np
import argparse
import csv
import logging
# SKLEARN modules
from sklearn.linear_model import Ridge
from sklearn.gaussian_process import GaussianProcessRegressor, kernels
from scipy.optimize import minimize
from src.utils import (
load_dataset,
create_folds,
standardize_data,
generate_data,
create_weight_matrix,
)
def main(args):
# Initialize the same random seed
np.random.seed(42)
# Create directory if it doesn't exist
if not os.path.exists(args.output_dir):
os.makedirs(args.output_dir)
# Initialize the logging
logging.basicConfig(
filename=os.path.join(args.output_dir, "output.log"),
filemode="w",
level=args.log_level,
format="%(asctime)s - %(levelname)s - %(message)s",
)
logging.info(args)
####
# Initialize the CSV to store the results
####
headers = ["sample", "train_score", "val_score"]
# Add the headers for the model coefficients
std_headers = []
mean_headers = []
if args.use_coords:
for i in range(21):
headers.append(f"beta_{i}")
std_headers.append(f"beta_{i}_std")
mean_headers.append(f"beta_{i}_mean")
else:
for i in range(19):
headers.append(f"beta_{i}")
std_headers.append(f"beta_{i}_std")
mean_headers.append(f"beta_{i}_mean")
headers.extend(["y_mean", "y_std", "model_intercept"])
# Create header in the CSV
with open(os.path.join(args.output_dir, "results.csv"), "w") as f:
writer = csv.writer(f)
writer.writerow(headers)
with open(os.path.join(args.output_dir, "scale.csv"), "w") as f:
writer = csv.writer(f)
writer.writerow(std_headers)
with open(os.path.join(args.output_dir, "shift.csv"), "w") as f:
writer = csv.writer(f)
writer.writerow(mean_headers)
####
# Get the dataset
####
data = load_dataset(args.data_dir, args.window_size, args.city)
# Create the folds
folds = create_folds(data, args.num_blocks)
for i in range(args.num_runs):
# Sample the data
sample_blocks = np.random.choice(
range(args.num_blocks), size=args.num_blocks, replace=True
)
held_out_blocks = np.setdiff1d(range(args.num_blocks), sample_blocks)
train_idx = np.where([f in sample_blocks for f in folds])[0]
val_idx = np.where([f in held_out_blocks for f in folds])[0]
X_train, y_train = generate_data(
data,
train_idx,
args.ndvi_ls,
args.albedo_ls,
args.window_size,
args.use_coords,
)
X_val, y_val = generate_data(
data,
val_idx,
args.ndvi_ls,
args.albedo_ls,
args.window_size,
args.use_coords,
)
# Normalize the data
(
X_train,
X_val,
y_train,
y_val,
y_mean,
y_std,
x_shift,
x_scale,
) = standardize_data(X_train, X_val, y_train, y_val, scaler=args.scaler)
if args.iterate:
# Create and fit the model
ridge_y = y_train
# iterating 5 times to stabilize the model
for _ in range(5):
lm = Ridge(alpha=args.l2_alpha, fit_intercept=False).fit(
X_train, ridge_y
)
# train_preds = y_train - lm.predict(X_train)
train_preds = lm.predict(X_train)
train_residuals = y_train - train_preds
sample_points, sample_residuals = generate_samples(
data.coords[train_idx], train_residuals, args.n_samples
)
gp = fit_gp(
sample_points,
sample_residuals,
args.gp_constant_1,
args.gp_length_scale,
args.gp_constant_2,
args.gp_sigma_0,
args.gp_noise,
)
U_train = gp.predict(data.coords[train_idx])
ridge_y = y_train - U_train
else:
# Create and fit the model
lm = Ridge(alpha=args.l2_alpha).fit(X_train, y_train)
# Calculate the train and validation score
train_score = lm.score(X_train, y_train)
val_score = lm.score(X_val, y_val)
# Record the results
row = [i, train_score, val_score]
row.extend(lm.coef_)
row.extend([y_mean, y_std])
row.append(lm.intercept_)
with open(os.path.join(args.output_dir, "results.csv"), "a") as f:
writer = csv.writer(f)
writer.writerow(row)
with open(os.path.join(args.output_dir, "scale.csv"), "a") as f:
writer = csv.writer(f)
writer.writerow(x_scale)
with open(os.path.join(args.output_dir, "shift.csv"), "a") as f:
writer = csv.writer(f)
writer.writerow(x_shift)
def fit_gp(points, residuals, constant_1, length_scale, constant_2, sigma_0, noise):
kernel = kernels.ConstantKernel(
constant_1, constant_value_bounds="fixed"
) * kernels.Matern(
length_scale=length_scale, nu=0.5, length_scale_bounds="fixed"
) + kernels.ConstantKernel(
constant_2, constant_value_bounds="fixed"
) * kernels.DotProduct(
sigma_0, sigma_0_bounds="fixed"
)
gpr = GaussianProcessRegressor(kernel=kernel, optimizer=optimizer, alpha=noise)
gpr.fit(points, residuals)
return gpr
def optimizer(obj_func, initial_theta, bounds):
opt_res = minimize(
obj_func,
initial_theta,
method="L-BFGS-B",
bounds=bounds,
jac=True,
options={"maxiter": 1000},
)
return opt_res.x, opt_res.fun
def generate_samples(coords, residuals, N):
sample_idx = np.random.choice(np.arange(coords.shape[0]), N, replace=False)
sample_points = coords[sample_idx, :]
sample_residuals = residuals[sample_idx]
return sample_points, sample_residuals
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Bootstrap experiment to retrieve posterior estimates of model coefficients."
)
# Create data arguments
parser.add_argument("--data_dir", type=str, help="Path to the data directory")
parser.add_argument(
"--window_size", type=int, default=25, help="Size of the window "
)
parser.add_argument("--use_coords", action="store_true", help="Use the coordinates")
# Create the model arguments
parser.add_argument(
"--l2_alpha", type=float, default=10, help="L2 regularization parameter"
)
# Create the training arguments
parser.add_argument(
"--num_blocks",
type=int,
default=50,
help="Number of folds to use for cross validation",
)
parser.add_argument(
"--num_runs", type=int, default=100, help="Number of bootstrap samples to draw."
)
parser.add_argument(
"--scaler", choices=["standard", "minmax"], default="standard", help="Scaler"
)
# Add the start and end ints for the NDVI length scale range
parser.add_argument(
"--ndvi_ls",
type=int,
help="The LS value to use for NDVI",
default=7,
)
# GP params
parser.add_argument(
"--n_samples", type=int, default=1000, help="Number of samples to draw"
)
# Add the GP arguments
parser.add_argument(
"--gp_constant_1",
type=float,
default=0.5,
help="The constant parameter for the Gaussian Process.",
)
parser.add_argument(
"--gp_constant_2",
type=float,
default=0.001,
help="The constant parameter for the Gaussian Process.",
)
# Add a term for the noise
parser.add_argument(
"--gp_noise",
type=float,
default=0.1,
help="The noise parameter for the Gaussian Process.",
)
parser.add_argument(
"--gp_length_scale",
type=float,
default=1000,
help="The length scale for the Gaussian Process.",
)
parser.add_argument(
"--gp_sigma_0",
type=float,
default=0.001,
help="The sigma_0 parameter for the Gaussian Process.",
)
# Add the start and end ints for the albedo length scale range
parser.add_argument(
"--albedo_ls",
type=int,
help="The LS value to use for Albedo",
default=1,
)
# Create the output arguments
parser.add_argument("--output_dir", type=str, help="Path to the output directory")
parser.add_argument("--log_level", type=str, default="INFO", help="Logging level")
# Create flag that determines whether to iterate between GP and Ridge
parser.add_argument(
"--iterate",
action="store_true",
help="Whether to iterate between GP and Ridge",
)
# Add an argument for the city name
parser.add_argument(
"--city",
type=str,
default="boston",
help="The city where the data is being collected from.",
)
# Parse the arguments
args = parser.parse_args()
print(args)
main(args)