forked from IMFS-MMB/epi-mmb-code
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocp_epimmb.m
242 lines (217 loc) · 8.37 KB
/
ocp_epimmb.m
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
function result_mat=ocp_epimmb(modelname, macrovariablelist, inf_ini,dy_root)
% function starts
%====================================================================================================
% navigate to the root folder (if exists)
try
cd(ppp.path.root)
end
ppp.path.root = convertCharsToStrings(pwd);
ppp.path.working = fullfile(ppp.path.root, "working");
ppp.path.share.dyModules = fullfile(ppp.path.root, "shared", "dyModules") ;
%num_vars=10;
num_vars = length(macrovariablelist);
try
rmdir(ppp.path.working,'s')
end
mkdir working
maxhorizon = 700;
num_macrovar = length(macrovariablelist);
result_mat = NaN(num_macrovar,maxhorizon);
disp(modelname);
ppp.path.models = fullfile(ppp.path.root, "models");
ttt.path.model = fullfile(ppp.path.models, modelname);
ttt.path.modeldymodules = fullfile(ttt.path.model, "dyModules") ;
cd(ttt.path.model);
% copy everything to working folder
copyfile( ttt.path.model , ppp.path.working)
try
copyfile( ttt.path.modeldymodules , ppp.path.working)
end
%copyfile( ppp.path.share.dyModules, ppp.path.working)
cd(ppp.path.working)
%load json file to see how to make adjustments
fname = strcat(modelname,".json");
fid = fopen(fname);
raw = fread(fid,inf);
str = char(raw');
fclose(fid);
jcode = jsondecode(str);
%Remove because every model must be run with Dy 5.1
%{
if (jcode.Dynare_version ~= "0") && (jcode.Dynare_version ~= "??")
dy_path=dy_root+jcode.Dynare_version+"/matlab";
addpath(dy_path);
elseif jcode.Dynare_version == "??"
disp('Dynare path ?? Please check which is the correct version');
end
%}
if inf_ini(1)==1
helper=inf_ini(2);
save inf_ini.mat helper
else
helper=jcode.initial_value;
save inf_ini.mat helper
end
if isfile('modelmasterscript.m')
modelmasterscript
ress_load=load('simulated_results.mat');
ress_load_ss=load('simulated_results_ss.mat');
ress=nan(num_vars,size(ress_load.Susceptibles,1));
ress_ss=nan(num_vars,1);
ressnames=fieldnames(ress_load); %sorted list including SS
ress_ssName = fieldnames(ress_load_ss);
% retriving results from .mat
for iii = 1:length(fieldnames(ress_load))
ressName_tmp = char(ressnames(iii));
eval(strcat("ress(iii,:) = ress_load." , ressName_tmp, ";"))
ress_ssName_tmp = char(ress_ssName(iii));
eval(strcat("ress_ss(iii) = ress_load_ss." , ress_ssName_tmp, ";"))
end
% ress(1,:)=ress_load.Consumption;
% ress(2,:)=ress_load.Deaths;
% ress(3,:)=ress_load.Infected;
% ress(4,:)=ress_load.Labour;
% ress(5,:)=ress_load.Output;
% ress(6,:)=ress_load.Recovered;
% ress(7,:)=ress_load.Susceptibles;
% ress(8,:)=ress_load.Interest;
% ress(9,:)=ress_load.Inflation;
% ress(10,:)=ress_load.Investment;
%
% ress_ss(1)=ress_load_ss.Consumption_ss;
% ress_ss(2)=ress_load_ss.Deaths_ss;
% ress_ss(3)=ress_load_ss.Infected_ss;
% ress_ss(4)=ress_load_ss.Labour_ss;
% ress_ss(5)=ress_load_ss.Output_ss;
% ress_ss(6)=ress_load_ss.Recovered_ss;
% ress_ss(7)=ress_load_ss.Susceptibles_ss;
% ress_ss(8)=ress_load_ss.Interest_ss;
% ress_ss(9)=ress_load_ss.Inflation_ss;
% ress_ss(10)=ress_load_ss.Investment_ss;
else
eval(strcat("dynare ", modelname))
load simulated_results
end
disp("results")
if jcode.Code_type=="Dynare"
for ind_macrovar = 1: num_macrovar
macrovar = macrovariablelist(ind_macrovar);
series_pos = find(strcmp(results.M_.endo_names,macrovar));
if length(series_pos) > 0
series_level = results.oo_.endo_simul(series_pos,:);
series_ss = results.oo_.steady_state(series_pos,:);
if modelname=="LFA_22"
if series_ss == 0
if macrovar=="Susceptibles"| macrovar=="Infected"| macrovar=="Recovered"| macrovar=="Deaths"| macrovar=="Interest";
series = 100*series_level;
else
series = 100*series_level;
end
if series==zeros(1,length(series))
series=nan(1,length(series));
end
if length(series) < maxhorizon
series(end+1:maxhorizon)=nan;
end
result_mat(ind_macrovar,1:(maxhorizon-1)) = series(2:maxhorizon);
else
if macrovar=="Susceptibles"| macrovar=="Infected"| macrovar=="Recovered"| macrovar=="Deaths"| macrovar=="Interest";
series = 100*series_level;
else
series = 100*(series_level - series_ss)/series_ss;
end
if series==zeros(1,length(series))
series=nan(1,length(series));
end
if length(series) < maxhorizon
series(end+1:maxhorizon)=nan;
end
result_mat(ind_macrovar,1:(maxhorizon-1)) = series(2:maxhorizon);
end
else
if series_ss == 0
if macrovar=="Susceptibles"| macrovar=="Infected"| macrovar=="Recovered"| macrovar=="Deaths";
series = 100*series_level;
else
series = 100* series_level;
end
if series==zeros(1,length(series))
series=nan(1,length(series));
end
if length(series) < maxhorizon
series(end+1:maxhorizon)=nan;
end
result_mat(ind_macrovar,:) = series(1:maxhorizon);
else
if macrovar=="Susceptibles"| macrovar=="Infected"| macrovar=="Recovered"| macrovar=="Deaths";
series = 100*series_level;
else
series = 100*(series_level - series_ss)/series_ss;
end
if series==zeros(1,length(series))
series=nan(1,length(series));
end
if length(series) < maxhorizon
series(end+1:maxhorizon)=nan;
end
result_mat(ind_macrovar,:) = series(1:maxhorizon);
end
end
else
result_mat(ind_macrovar,:) = nan(1,maxhorizon);
end
end
elseif jcode.Code_type=="Matlab"
for ind_macrovar = 1: num_macrovar
macrovar = macrovariablelist(ind_macrovar);
series_pos = find(strcmp(ressnames,macrovar));
ss_pos = find(strcmp(ress_ssName,strcat(macrovar,"_ss")));
result_mat(ind_macrovar,:) = nan(1,maxhorizon);
if length(series_pos) > 0
%S.(F{idx})
series_level = ress(series_pos,:);
series_ss = ress_ss(ss_pos);
if exist('series_ss','var') == 1
if series_ss == 0;
if macrovar=="Susceptibles"| macrovar=="Infected"| macrovar=="Recovered"| macrovar=="Deaths";
series = 100*series_level;
else
series = 100* series_level;
end
if series==zeros(1,length(series))
series=nan(1,length(series));
end
if length(series) < maxhorizon
series(end+1:maxhorizon)=nan;
end
result_mat(ind_macrovar,:) = series(1:maxhorizon);
else
series = 100*(series_level - series_ss)/series_ss;
if series==zeros(1,length(series))
series=nan(1,length(series));
end
if length(series) < maxhorizon
series(end+1:maxhorizon)=nan;
end
result_mat(ind_macrovar,:) = series(1:maxhorizon);
end
end
else
result_mat(ind_macrovar,:) = nan(1,maxhorizon);
end
end
else
display("Invalid Code Type! Choose Matlab or Dynare or code it yourself!")
pause(4)
end
try
rmpath(dy_path);
end
if convertCharsToStrings(pwd) == ppp.path.working
cd(ppp.path.root)
try
rmdir(ppp.path.working,'s')
mkdir working
end
end
end