-
Notifications
You must be signed in to change notification settings - Fork 0
/
figures_neoclassical_growth_robustness.py
271 lines (239 loc) · 7.9 KB
/
figures_neoclassical_growth_robustness.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
import jax.numpy as jnp
import matplotlib.pyplot as plt
import os
from new_neoclassical_growth_matern import neoclassical_growth_matern
#from neoclassical_growth_dae_matern import neoclassical_growth_dae_matern
from new_neoclassical_growth_neural import neoclassical_growth_neural
from mpl_toolkits.axes_grid1.inset_locator import (
zoomed_inset_axes,
mark_inset,
inset_axes,
)
fontsize = 14
ticksize = 14
figsize = (15, 10)
params = {
"font.family": "serif",
"figure.figsize": figsize,
"figure.dpi": 80,
"figure.edgecolor": "k",
"font.size": fontsize,
"axes.labelsize": fontsize,
"axes.titlesize": fontsize,
"xtick.labelsize": ticksize,
"ytick.labelsize": ticksize,
}
plt.rcParams.update(params)
## Plot given solution
def plot_neoclassical_growth(
sol,
output_path,
k_rel_error_ylim=(1e-6, 2 * 1e-2),
c_rel_error_ylim=(1e-6, 2 * 1e-2),
zoom=True,
zoom_loc=[50, 60],
):
t = sol["t_test"]
T = sol["t_train"].max()
c_hat = sol["c_test"]
k_hat = sol["k_test"]
c_benchmark = sol["c_benchmark"]
k_benchmark = sol["k_benchmark"]
k_rel_error = sol["k_rel_error"]
c_rel_error = sol["c_rel_error"]
# Plotting
plt.figure(figsize=(15, 10))
ax_capital = plt.subplot(2, 2, 1)
plt.plot(t, k_hat, color="k", label=r"$\hat{x}(t)$: Matérn Kernel Approximation")
plt.plot(
t, k_benchmark, linestyle="--", color="k", label=r"$x(t)$: Benchmark Solution"
)
plt.axvline(x=T, color="k", linestyle=":", label="Extrapolation/Interpolation")
plt.ylabel("Capital: $x(t)$")
plt.xlabel("Time")
plt.legend() # Show legend with labels
ax_rel_k = plt.subplot(2, 2, 2)
plt.plot(
t,
k_rel_error,
color="k",
label=r"$\varepsilon_x(t)$: Rel. Errors for $x(t)$, Matérn Kernel Approx.",
)
plt.axvline(x=T, color="k", linestyle=":", label="Extrapolation/Interpolation")
plt.yscale("log") # Set y-scale to logarithmic
plt.ylim(k_rel_error_ylim[0], k_rel_error_ylim[1])
plt.xlabel("Time")
plt.legend() # Show legend with labels
ax_consumption = plt.subplot(2, 2, 3)
plt.plot(t, c_hat, color="b", label=r"$\hat{y}(t)$: Matérn Kernel Approximation")
plt.plot(
t, c_benchmark, linestyle="--", color="b", label=r"$y(t)$: Benchmark Solution"
)
plt.axvline(x=T, color="k", linestyle=":", label="Extrapolation/Interpolation")
plt.ylabel("Consumption: $y(t)$")
plt.xlabel("Time")
plt.legend() # Show legend with labels
ax_rel_c = plt.subplot(2, 2, 4)
plt.plot(
t,
c_rel_error,
color="b",
label=r"$\varepsilon_y(t)$: Rel. Errors for $y(t)$, Matérn Kernel Approx.",
)
plt.axvline(x=T, color="k", linestyle=":", label="Extrapolation/Interpolation")
plt.yscale("log") # Set y-scale to logarithmic
plt.ylim(c_rel_error_ylim[0], c_rel_error_ylim[1])
plt.xlabel("Time")
plt.legend() # Show legend with labels
plt.tight_layout() # Adjust layout to prevent overlap
# Zoom in part of the plot
if zoom == True:
time_window = (
zoom_loc # Indices: The window on the x-axis that want to be zoomed in
)
ave_value = 0.5 * (
k_benchmark[time_window[0]] + k_benchmark[time_window[1]]
) # The average on the y-axis that want to be zoomed in
window_width = 0.01 * ave_value
axins = zoomed_inset_axes(
ax_capital,
3,
loc="center",
bbox_to_anchor=(0.5, 0.7, -0.3, -0.3),
bbox_transform=ax_capital.transAxes,
)
axins.plot(
t[time_window[0] - 1 : time_window[1] + 1],
k_hat[time_window[0] - 1 : time_window[1] + 1],
color="k",
)
axins.plot(
t[time_window[0] - 1 : time_window[1] + 1],
k_benchmark[time_window[0] - 1 : time_window[1] + 1],
linestyle="--",
color="k",
)
x1, x2, y1, y2 = (
t[time_window[0]],
t[time_window[1]],
ave_value - window_width,
ave_value + window_width,
)
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
plt.xticks(fontsize=8, visible=False)
plt.tick_params(
axis="x", which="both", bottom=False, top=False, labelbottom=False
)
plt.yticks(fontsize=8)
mark_inset(
ax_capital, axins, loc1=1, loc2=3, linewidth="0.7", ls="--", ec="0.5"
)
time_window = (
zoom_loc # Indices: The window on the x-axis that want to be zoomed in
)
ave_value = 0.5 * (
c_benchmark[time_window[0]] + c_benchmark[time_window[1]]
) # The average on the y-axis that want to be zoomed in
window_width = 0.01 * ave_value
axins = zoomed_inset_axes(
ax_consumption,
3,
loc="center",
bbox_to_anchor=(0.5, 0.7, -0.3, -0.3),
bbox_transform=ax_consumption.transAxes,
)
axins.plot(
t[time_window[0] - 1 : time_window[1] + 1],
c_hat[time_window[0] - 1 : time_window[1] + 1],
color="b",
)
axins.plot(
t[time_window[0] - 1 : time_window[1] + 1],
c_benchmark[time_window[0] - 1 : time_window[1] + 1],
linestyle="--",
color="b",
)
x1, x2, y1, y2 = (
t[time_window[0]],
t[time_window[1]],
ave_value - window_width,
ave_value + window_width,
)
axins.set_xlim(x1, x2)
axins.set_ylim(y1, y2)
plt.xticks(fontsize=8, visible=False)
plt.tick_params(
axis="x", which="both", bottom=False, top=False, labelbottom=False
)
plt.yticks(fontsize=8)
mark_inset(
ax_consumption, axins, loc1=1, loc2=3, linewidth="0.7", ls="--", ec="0.5"
)
plt.savefig(output_path, format="pdf")
# Plots with various parameters
sol = neoclassical_growth_matern(
train_points_list=[1.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0]
)
plot_neoclassical_growth(
sol,
"figures/neoclassical_growth_model_sparse.pdf",
c_rel_error_ylim=(1e-7, 2 * 1e-2),
zoom=True,
zoom_loc=[10, 20],
)
sol = neoclassical_growth_matern(train_T=10.0, train_points=11, test_T=15.0)
plot_neoclassical_growth(
sol,
"figures/neoclassical_growth_model_far_steady_state.pdf",
k_rel_error_ylim=(1e-4, 1e-1),
c_rel_error_ylim=(1e-4, 1e-1),
zoom=False,
)
sol = neoclassical_growth_matern(nu=1.5)
plot_neoclassical_growth(
sol,
"figures/neoclassical_growth_model_nu_1_5.pdf",
k_rel_error_ylim=(1e-7, 1e-2),
c_rel_error_ylim=(1e-7, 1e-2),
zoom=True,
zoom_loc=[75, 85],
)
sol = neoclassical_growth_matern(nu=2.5)
plot_neoclassical_growth(
sol,
"figures/neoclassical_growth_model_nu_2_5.pdf",
k_rel_error_ylim=(1e-7, 1e-2),
c_rel_error_ylim=(1e-8, 1e-2),
zoom=True,
zoom_loc=[75, 85],
)
sol = neoclassical_growth_matern(rho=2)
plot_neoclassical_growth(
sol,
"figures/neoclassical_growth_model_rho_2.pdf",
k_rel_error_ylim=(1e-7, 1e-2),
c_rel_error_ylim=(1e-7, 1e-2),
zoom=True,
zoom_loc=[75, 85],
)
sol = neoclassical_growth_matern(rho=20)
plot_neoclassical_growth(
sol,
"figures/neoclassical_growth_model_rho_20.pdf",
k_rel_error_ylim=(1e-7, 1e-2),
c_rel_error_ylim=(1e-7, 1e-2),
zoom=True,
zoom_loc=[75, 85],
)
"""
sol = neoclassical_growth_dae_matern()
plot_neoclassical_growth(
sol,
"figures/neoclassical_growth_model_dae.pdf",
k_rel_error_ylim=(1e-7, 1e-2),
c_rel_error_ylim=(1e-6, 1e-2),
zoom=True,
zoom_loc=[75, 85],
)
"""