-
Notifications
You must be signed in to change notification settings - Fork 1
/
cross_validate_function_fits.py
469 lines (435 loc) · 15.3 KB
/
cross_validate_function_fits.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
#!/usr/bin/env python
# ~*~ coding: utf8 ~*~
"""Find best correlation functions based on cross-validation on a single tower.
I will likely need to generalize this to include cross-validation
across towers at some point. I may limit that to only a subset of the
correlation functions.
"""
from __future__ import division, print_function
import datetime
import itertools
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.optimize
import xarray
import flux_correlation_function_fits
from correlation_function_fits import (
GLOBAL_DICT,
CorrelationPart,
PartForm,
get_full_expression,
get_full_parameter_list,
get_weighted_fit_expression,
is_valid_combination,
)
from correlation_utils import get_autocorrelation_stats
# Time constants
HOURS_PER_DAY = 24
DAYS_PER_WEEK = 7
DAYS_PER_FORTNIGHT = 14
DAYS_PER_YEAR = 365.2425
DAYS_PER_DECADE = 10 * DAYS_PER_YEAR
HOURS_PER_YEAR = HOURS_PER_DAY * DAYS_PER_YEAR
# Need to check this separately for each half of the data
N_YEARS_DATA = 2
REQUIRED_DATA_FRAC = 0.8
def has_enough_data(da):
"""Check whether there is enough data for a good analysis.
Parameters
----------
da: xarray.DataArray
Returns
-------
bool
"""
time_index_name = [name for name in da.dims if "time" in name.lower()][0]
time_index = da.indexes[time_index_name]
if len(time_index) < 1:
print("No data")
return False
if time_index[-1] - time_index[0] < datetime.timedelta(
days=N_YEARS_DATA * DAYS_PER_YEAR
):
print("< 2 years")
return False
if (
da.count(time_index_name).values[()]
< REQUIRED_DATA_FRAC * N_YEARS_DATA * HOURS_PER_YEAR
):
print("Missing data")
return False
return True
def timedelta_index_to_floats(index):
"""Turn a TimedeltaIndex into an array of floats.
Parameters
----------
index: pd.TimedeltaIndex
Returns
-------
np.ndarray
"""
# Timedeltas can be negative, so the underlying type should be a
# signed integer.
lag_times = index.values.astype("m8[h]").astype("i8")
lag_times -= lag_times[0]
lag_times = lag_times.astype("f4") / 24
return lag_times.astype(np.float32)
############################################################
# Set initial values and bounds for the parameters
STARTING_PARAMS = dict(
daily_coef=0.2,
daily_coef1=0.7,
daily_coef2=0.3,
daily_width=0.5,
daily_timescale=60, # fortnights
dm_width=0.8,
dm_coef1=0.3,
dm_coef2=+0.1,
ann_coef1=+0.4,
ann_coef2=+0.3,
ann_coef=0.1,
ann_width=0.3,
ann_timescale=3.0, # decades
resid_coef=0.05,
resid_timescale=2.0, # fortnights
ec_coef=0.7,
ec_timescale=2.0, # hours
)
PARAM_LOWER_BOUNDS = dict(
daily_coef=-10,
daily_coef1=-10,
daily_coef2=-10,
daily_width=0,
daily_timescale=0, # fortnights
dm_width=0,
dm_coef1=-10,
dm_coef2=-10,
ann_coef1=-10,
ann_coef2=-10,
ann_coef=-10,
ann_width=0,
ann_timescale=0, # decades
resid_coef=-10,
resid_timescale=0.0, # fortnights
ec_coef=-10,
ec_timescale=0.0, # hours
)
PARAM_UPPER_BOUNDS = dict(
daily_coef=10,
daily_coef1=10,
daily_coef2=10,
daily_width=10,
daily_timescale=500, # fortnights
dm_width=10,
dm_coef1=10,
dm_coef2=10,
ann_coef1=10,
ann_coef2=10,
ann_coef=10,
ann_width=10,
ann_timescale=4, # decades
resid_coef=10,
resid_timescale=500.0, # fortnights
ec_coef=10,
ec_timescale=1000.0, # hours
)
# Convert initial values and bounds to float32
for coef, val in STARTING_PARAMS.items():
STARTING_PARAMS[coef] = np.float32(val)
for coef, val in PARAM_LOWER_BOUNDS.items():
PARAM_LOWER_BOUNDS[coef] = np.float32(val)
for coef, val in PARAM_UPPER_BOUNDS.items():
PARAM_UPPER_BOUNDS[coef] = np.float32(val)
############################################################
# Read in data
AMERIFLUX_MINUS_CASA_DATA = xarray.open_dataset(
"ameriflux-and-casa-matching-data.nc4"
)
############################################################
# Set up data frames for results
COEF_DATA = pd.DataFrame(
index=pd.MultiIndex.from_product(
[
AMERIFLUX_MINUS_CASA_DATA.indexes["site"],
[
"_".join(
[
"{0:s}{1:s}".format(
part.get_short_name(),
form.get_short_name(),
)
for part, form in zip(CorrelationPart, forms)
]
)
for forms in itertools.product(PartForm, PartForm, PartForm)
if is_valid_combination(*forms)
],
],
names=["Site", "Correlation Function"],
),
columns=[
"daily_coef",
"daily_timescale",
"daily_coef1",
"daily_coef2",
"daily_width",
"dm_coef1",
"dm_coef2",
"dm_width",
"ann_coef",
"ann_timescale",
"ann_coef1",
"ann_coef2",
"ann_width",
"resid_coef",
"resid_timescale",
"ec_coef",
"ec_timescale",
],
dtype=np.float32,
)
COEF_VAR_DATA = COEF_DATA.copy()
CORRELATION_FIT_ERROR = pd.DataFrame(
index=COEF_DATA.index,
columns=pd.MultiIndex.from_product(
[
["function_optimized", "other_function"],
["weighted_error_in_sample", "weighted_error_out_of_sample"],
],
names=["which_function", "which_data"],
),
dtype=np.float32,
)
CORRELATION_FIT_ERROR.iloc[:, :] = np.inf
for site_name in AMERIFLUX_MINUS_CASA_DATA.indexes["site"]:
print(site_name, flush=True)
# Pull out non-missing site data, so I can get a decent idea of
# what has enough data I can use.
site_data = (
AMERIFLUX_MINUS_CASA_DATA["flux_difference"]
.sel(site=site_name)
.dropna("time")
.sortby("time")
)
# Split data in two, so we have separate training and validation
# data sets.
first_half = site_data.isel(time=slice(None, len(site_data) // 2))
second_half = site_data.isel(time=slice(len(site_data) // 2, None))
if not (has_enough_data(first_half) and has_enough_data(second_half)):
print("Not enough data. Skipping:", site_name)
continue
# Resample to an hour, so acf/count_pairs can work (they assume
# regularly spaced data, and I need the .freq attribute to make
# that happen)
first_half.resample(time="1H").first()
second_half.resample(time="1H").first()
for train_data, validation_data in itertools.permutations(
[first_half, second_half]
):
print("New train/val split")
corr_data_train = get_autocorrelation_stats(
train_data.to_dataframe()["flux_difference"].resample("1H").first()
)
corr_data_train = corr_data_train[corr_data_train["pair_counts"] > 0]
corr_data_validate = get_autocorrelation_stats(
validation_data.to_dataframe()["flux_difference"].resample("1H").first()
)
corr_data_validate = corr_data_validate[corr_data_validate["pair_counts"] > 0]
acf_lags_train = timedelta_index_to_floats(corr_data_train.index)
acf_lags_validate = timedelta_index_to_floats(corr_data_validate.index)
acf_weights_train = 1.0 / np.sqrt(corr_data_train["pair_counts"])
acf_weights_validate = 1.0 / np.sqrt(corr_data_validate["pair_counts"])
for forms in itertools.product(PartForm, PartForm, PartForm):
if not is_valid_combination(*forms):
continue
# Get function to optimize
func_short_name = "_".join(
[
"{0:s}{1:s}".format(
part.get_short_name(),
form.get_short_name(),
)
for part, form in zip(CorrelationPart, forms)
]
)
print(func_short_name, flush=True)
name_to_optimize = "{fun_name}_fit_loop".format(fun_name=func_short_name)
fun_to_optimize = getattr(flux_correlation_function_fits, name_to_optimize)
fun_to_check = getattr(
flux_correlation_function_fits,
"{fun_name}_fit_ne".format(fun_name=func_short_name),
)
# Set up parameters
parameter_list = get_full_parameter_list(*forms)
starting_params = np.array(
[STARTING_PARAMS[param] for param in parameter_list],
dtype=np.float32,
)
lower_bounds = np.array(
[PARAM_LOWER_BOUNDS[param] for param in parameter_list],
dtype=np.float32,
)
upper_bounds = np.array(
[PARAM_UPPER_BOUNDS[param] for param in parameter_list],
dtype=np.float32,
)
# Try the optimization
# Use curve_fit to fine-tune
curve_and_deriv = getattr(
flux_correlation_function_fits,
"{fun_name:s}_curve_loop".format(fun_name=func_short_name),
)
def curve_deriv(tdata, *params):
return curve_and_deriv(tdata, *params)[1]
try:
opt_params, param_cov = scipy.optimize.curve_fit(
getattr(
flux_correlation_function_fits,
"{fun_name:s}_curve_ne".format(fun_name=func_short_name),
),
acf_lags_train.astype(np.float32),
corr_data_train["acf"].astype(np.float32).values,
starting_params.astype(np.float32),
acf_weights_train.astype(np.float32),
bounds=(
lower_bounds.astype(np.float32),
upper_bounds.astype(np.float32),
),
jac=curve_deriv,
)
except (RuntimeError, ValueError) as err:
print(err, "Curve fit failed, next function", sep="\n")
continue
print(opt_params)
# If this fit's cross-validation score is worse than the
# currently-stored one, don't bother recording.
if (
fun_to_optimize(
opt_params,
acf_lags_validate,
corr_data_validate["acf"].astype(np.float32).values,
corr_data_validate["pair_counts"].astype(np.float32).values,
)[0]
> CORRELATION_FIT_ERROR.loc[
(site_name, func_short_name),
("function_optimized", "weighted_error_out_of_sample"),
]
):
continue
# Otherwise, save the results
COEF_DATA.loc[(site_name, func_short_name), parameter_list] = opt_params
COEF_VAR_DATA.loc[(site_name, func_short_name), parameter_list] = np.diag(
param_cov
)
CORRELATION_FIT_ERROR.loc[
(site_name, func_short_name),
("function_optimized", "weighted_error_in_sample"),
] = fun_to_optimize(
opt_params,
acf_lags_train,
corr_data_train["acf"].astype(np.float32).values,
corr_data_train["pair_counts"].astype(np.float32).values,
)[
0
]
CORRELATION_FIT_ERROR.loc[
(site_name, func_short_name),
("function_optimized", "weighted_error_out_of_sample"),
] = fun_to_optimize(
opt_params,
acf_lags_validate,
corr_data_validate["acf"].astype(np.float32).values,
corr_data_validate["pair_counts"].astype(np.float32).values,
)[
0
]
CORRELATION_FIT_ERROR.loc[
(site_name, func_short_name),
("other_function", "weighted_error_in_sample"),
] = fun_to_check(
opt_params,
acf_lags_train,
corr_data_train["acf"].astype(np.float32).values,
corr_data_train["pair_counts"].astype(np.float32).values,
)
CORRELATION_FIT_ERROR.loc[
(site_name, func_short_name),
("other_function", "weighted_error_out_of_sample"),
] = fun_to_check(
opt_params,
acf_lags_validate,
corr_data_validate["acf"].astype(np.float32).values,
corr_data_validate["pair_counts"].astype(np.float32).values,
)
break
# Done fits, make plots.
fig, axes = plt.subplots(4, 2, sharey=True, sharex=True, figsize=(6.5, 5))
fig.suptitle("Correlation fit for {site:s}".format(site=site_name))
axes[0, 0].set_title("Training data")
axes[0, 1].set_title("Validation data")
axes[0, 0].set_ylabel("Empirical\nCorrelogram")
for ax in axes[1:, 0]:
ax.set_ylabel("Fitted\nCorrelogram")
axes[0, 0].plot(acf_lags_train, corr_data_train["acf"])
axes[0, 1].plot(acf_lags_validate, corr_data_validate["acf"])
axes[0, 0].set_ylim(-1, 1)
max_lag = max(acf_lags_train[-1], acf_lags_validate[-1])
axes[0, 0].set_xlim(0, max_lag)
xticks = np.arange(0, max_lag, 365)
for ax in axes.flat:
ax.set_xticks(xticks)
for ax in axes[-1, :]:
ax.set_xlabel("Time difference (years)")
ax.set_xticklabels(range(len(xticks)))
sorted_fits = (
CORRELATION_FIT_ERROR.loc[
(site_name, slice(None)), ("function_optimized", slice(None))
]
.sort_values(("function_optimized", "weighted_error_out_of_sample"))
.dropna(how="all")
)
print(sorted_fits.iloc[:3, :])
for i, fun_name in enumerate(sorted_fits.iloc[:3, :].index.get_level_values(1), 1):
print(fun_name)
curve_fun = getattr(
flux_correlation_function_fits,
"{fun_name:s}_curve_ne".format(fun_name=fun_name),
)
axes[i, 0].plot(
acf_lags_train,
curve_fun(
acf_lags_train,
**COEF_DATA.loc[(site_name, fun_name)].dropna(),
),
)
axes[i, 1].plot(
acf_lags_validate,
curve_fun(
acf_lags_validate,
**COEF_DATA.loc[(site_name, fun_name)].dropna(),
),
)
axes[i, 0].set_ylabel(fun_name)
fig.tight_layout()
fig.subplots_adjust(top=0.88)
fig.savefig(
"{site_name:s}-cross-validation-function-fits.png".format(site_name=site_name)
)
xticks_short = pd.timedelta_range(start=0, freq="7D", periods=7)
xtick_labels = ["{n:d} days".format(n=i * 7) for i in range(len(xticks_short))]
axes[0, 0].set_xlim(xticks_short[[0, -1]].values.astype(float))
for ax in axes.flat:
ax.set_xticks(xticks_short.values.astype(float))
for ax in axes[-1, :]:
ax.set_xticklabels(xtick_labels)
ax.set_xlabel("Time difference (days)")
fig.savefig(
"{site_name:s}-cross-validation-function-fits-short.png".format(
site_name=site_name
)
)
plt.close(fig)
COEF_DATA.to_csv("coefficient-data-loop.csv")
COEF_VAR_DATA.to_csv("coefficient-variance-data-loop.csv")
CORRELATION_FIT_ERROR.to_csv("correlation-fit-error-loop.csv")