-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_comparison.py
143 lines (99 loc) · 4.02 KB
/
model_comparison.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
"""
Script for computing scores of MDN, DTMC and GTGP models of transition density.
"""
import numpy as np
import grids
from utils import mdn_mean_log_likelihood
# Load data and choose a subset.
DT = 4.
DATA_DIR = f"data/GDP/{DT:.0f}day/"
# Set region limits.
global_ = np.array([[-180., 180.], [-90., 90.]])
A = np.array([[-50., -20.], [30., 50.]])
B = np.array([[145., 175.], [20., 40.]])
C = np.array([[-130., -100.], [-10., 10.]])
region = B
X0 = np.load(DATA_DIR + "X0_train.npy")
DX = np.load(DATA_DIR + "DX_train.npy")
# Get X1 from DX.
X1 = X0.copy() + DX.copy()
X1[(X1 > 180)[:, 0], 0] -= 360.
X1[(X1 < -180)[:, 0], 0] += 360.
# Extract subset.
subset_lon_0 = np.logical_and(X0[:, 0] > region[0, 0], X0[:, 0] < region[0, 1])
subset_lat_0 = np.logical_and(X0[:, 1] > region[1, 0], X0[:, 1] < region[1, 1])
subset_lon_1 = np.logical_and(X1[:, 0] > region[0, 0], X1[:, 0] < region[0, 1])
subset_lat_1 = np.logical_and(X1[:, 1] > region[1, 0], X1[:, 1] < region[1, 1])
subset = np.logical_and(
np.logical_and(
np.logical_and(
subset_lon_0, subset_lat_0), subset_lon_1), subset_lat_1)
X0 = X0[subset]
DX = DX[subset]
X0val = np.load(DATA_DIR + "X0_test.npy")
DXval = np.load(DATA_DIR + "DX_test.npy")
# Get X1val from DXval.
X1val = X0val.copy() + DXval.copy()
X1val[(X1val > 180)[:, 0], 0] -= 360.
X1val[(X1val < -180)[:, 0], 0] += 360.
subset_lon_0_val = np.logical_and(X0val[:, 0] > region[0, 0],
X0val[:, 0] < region[0, 1])
subset_lat_0_val = np.logical_and(X0val[:, 1] > region[1, 0],
X0val[:, 1] < region[1, 1])
subset_lon_1_val = np.logical_and(X1val[:, 0] > region[0, 0],
X1val[:, 0] < region[0, 1])
subset_lat_1_val = np.logical_and(X1val[:, 1] > region[1, 0],
X1val[:, 1] < region[1, 1])
subset_val = np.logical_and(
np.logical_and(
np.logical_and(
subset_lon_0_val,
subset_lat_0_val),
subset_lon_1_val),
subset_lat_1_val)
X0val = X0val[subset_val]
DXval = np.load(DATA_DIR + "DX_test.npy")[subset_val]
del X1, X1val
# Compute model scores.
def DTMC_score(res):
DTMC = grids.DTMC(n_x=int(np.ceil((region[0, 1] - region[0, 0]) / res)),
n_y=int(np.ceil((region[1, 1] - region[1, 0]) / res)),
xlims=region[0], ylims=region[1])
DTMC.fit(X0, DX)
return (DTMC.mean_log_likelihood(X0, DX),
DTMC.mean_log_likelihood(X0val, DXval))
ress = np.arange(5., 15.) # Global GTGP (7 gives best val. sco.)
# ress = [45., 60., 90., 180.] # Global DTMC (only 180 gives finite val. sco.)
# ress = np.logspace(np.log10(.25), np.log10(20), 15) # A
# ress = np.logspace(np.log10(.25), np.log10(20), 15) # B (DTMC:10, GTGP:2.5)
# ress = np.logspace(np.log10(1.5), np.log10(20), 12) # C (DTMC:10)
# ress = np.logspace(np.log10(1.5), np.log10(6), 12) # C (GTGP:4.11)
DTMC_mll = []
for i in range(len(ress)):
DTMC_mll.append(DTMC_score(ress[i]))
print(i + 1)
DTMC_mll = np.array(DTMC_mll)
def GTGP_score(res):
GTGP = grids.GTGP(n_x=int(np.ceil((region[0, 1] - region[0, 0]) / res)),
n_y=int(np.ceil((region[1, 1] - region[1, 0]) / res)),
xlims=region[0], ylims=region[1])
GTGP.fit(X0, DX)
return (GTGP.mean_log_likelihood(X0, DX),
GTGP.mean_log_likelihood(X0val, DXval))
GTGP_mll = []
for i in range(len(ress)):
GTGP_mll.append(GTGP_score(ress[i]))
print(i + 1)
print(GTGP_mll[-1])
GTGP_mll = np.array(GTGP_mll)
MDN_mll = mdn_mean_log_likelihood(X0val, DXval,
DT, 32,
block_size=40000)
# Evaluate DTMC with fixed resolution.
res = 1.
DTMC = grids.DTMC(n_x=int(np.ceil((region[0, 1] - region[0, 0]) / res)),
n_y=int(np.ceil((region[1, 1] - region[1, 0]) / res)),
xlims=region[0], ylims=region[1])
DTMC.fit(X0, DX)
DTMC_train = DTMC.mean_log_likelihood(X0, DX)
DTMC_test = DTMC.mean_log_likelihood(X0val, DXval)