gui: clean up file

time-shift
Houtan Bastani 2019-02-05 15:46:57 +01:00
parent ce3be98aa0
commit 0ad8bfbd16
No known key found for this signature in database
GPG Key ID: 000094FB955BE169
1 changed files with 98 additions and 124 deletions

View File

@ -11,7 +11,7 @@ function varargout = run(json)
% varargout{1} [string] if desired, return output as JSON string % varargout{1} [string] if desired, return output as JSON string
% %
% SPECIAL REQUIREMENTS % SPECIAL REQUIREMENTS
% dynare must have been run with the option: json=compute % none
% Copyright (C) 2019 Dynare Team % Copyright (C) 2019 Dynare Team
% %
@ -32,6 +32,7 @@ function varargout = run(json)
global M_ options_ oo_ ys0_ ex0_ global M_ options_ oo_ ys0_ ex0_
%% Check Inputs
if nargin ~= 1 || ~ischar(json) if nargin ~= 1 || ~ischar(json)
error('function takes one string input argument') error('function takes one string input argument')
end end
@ -40,14 +41,11 @@ if nargout > 1
error('function provides up to one output argument') error('function provides up to one output argument')
end end
%loading JSON %% Read JSON
jm = loadjson(json,'SimplifyCell',0); jm = loadjson(json, 'SimplifyCell', 0);
%We test if jsonload loads string or char %% INITVAL instructions
% jsl=length(class(jm.jsontest{1,1})); % initialize exogenous shocks to zero and compute initial steady state
% INITVAL instructions
%we initialize exogenous shocks to zero and compute initial ss
options_.initval_file = 0; options_.initval_file = 0;
oo_.exo_steady_state(1:jm.exonum) = 0; oo_.exo_steady_state(1:jm.exonum) = 0;
if M_.exo_nbr > 0 if M_.exo_nbr > 0
@ -58,199 +56,175 @@ if M_.exo_det_nbr > 0
end end
steady; steady;
if nargout == 1 if nargout == 1
data2json=struct(); data2json = struct();
data2json.steady_state1=oo_.steady_state; data2json.steady_state1 = oo_.steady_state;
end end
%% ENDVAL instructions
% ENDVAL instructions % initialize exogenous shocks to zero and compute final ss unless there is a permanent shock
%we initialize exogenous shocks to zero and compute final ss unless there is a permanent shock
ys0_= oo_.steady_state; ys0_= oo_.steady_state;
ex0_ = oo_.exo_steady_state; ex0_ = oo_.exo_steady_state;
if jm.permanentshockexist==0 if jm.permanentshockexist == 0
oo_.exo_steady_state(1:jm.exonum) = 0; oo_.exo_steady_state(1:jm.exonum) = 0;
else else
for exoiter = 1:length(jm.permanentshocksdescription) for exoiter = 1:length(jm.permanentshocksdescription)
currentshock=jm.permanentshocksdescription(exoiter); currentshock = jm.permanentshocksdescription(exoiter);
oo_.exo_steady_state(currentshock{1}.shockindex+1) = currentshock{1}.shockvalue; oo_.exo_steady_state(currentshock{1}.shockindex+1) = currentshock{1}.shockvalue;
if (currentshock{1}.shockstartperiod)>1 if currentshock{1}.shockstartperiod > 1
%in case the permanent shock does not start at the initial period, we add a shocks block to mask the unnecessary periods %in case the permanent shock does not start at the initial period, we add a shocks block to mask the unnecessary periods
M_.det_shocks = [ M_.det_shocks;struct('exo_det',0,'exo_id',(currentshock{1}.shockindex+1),'multiplicative',0,'periods',1:(currentshock{1}.shockstartperiod-1),'value',0.0) ]; M_.det_shocks = [ M_.det_shocks;struct('exo_det',0,'exo_id',(currentshock{1}.shockindex+1),'multiplicative',0,'periods',1:(currentshock{1}.shockstartperiod-1),'value',0.0) ];
end end
end end
end end
steady; steady;
savedpermanentSS=oo_.steady_state; savedpermanentSS = oo_.steady_state;
if nargout == 1 if nargout == 1
data2json.steady_state2=oo_.steady_state; data2json.steady_state2 = oo_.steady_state;
end end
%% SHOCKS instructions (for transitory shocks)
if jm.transitoryshockexist==1 if jm.transitoryshockexist == 1
% SHOCKS instructions (for transitory shocks)
for exotriter = 1:length(jm.shocksdescription) for exotriter = 1:length(jm.shocksdescription)
currenttrshock=jm.shocksdescription(exotriter); currenttrshock = jm.shocksdescription(exotriter);
% disp(currenttrshock{1}.shockname);
% disp(class(currenttrshock{1}.shockstartperiod));
M_.det_shocks = [ M_.det_shocks;struct('exo_det',0,'exo_id',(currenttrshock{1}.shockindex+1),'multiplicative',0,'periods',currenttrshock{1}.shockstartperiod:currenttrshock{1}.shockendperiod,'value',currenttrshock{1}.shockvalue) ]; M_.det_shocks = [ M_.det_shocks;struct('exo_det',0,'exo_id',(currenttrshock{1}.shockindex+1),'multiplicative',0,'periods',currenttrshock{1}.shockstartperiod:currenttrshock{1}.shockendperiod,'value',currenttrshock{1}.shockvalue) ];
%oo_.exo_steady_state(str2num(currentshock{1}.shockindex)+1) = currentshock{1}.shockvalue;
end end
M_.exo_det_length = 0; M_.exo_det_length = 0;
end end
if ((jm.nonanticipatedshockexist==1) || (jm.delayexist==1)) if jm.nonanticipatedshockexist == 1 || jm.delayexist == 1
nonanticip = jm.nonanticipmatrix;
nonanticip=jm.nonanticipmatrix; rowindex = 1;
rowindex=1; firstsimul = 0;
firstsimul=0; while nonanticip{rowindex}{1} > 0
while nonanticip{rowindex}{1}>0
currentperiod=nonanticip{rowindex}{1}; currentperiod=nonanticip{rowindex}{1};
if currentperiod == 1
if currentperiod==1 % there are nonanticipated shocks to add at first period
%there are nonanticipated shocks to add at first period if nonanticip{rowindex}{4} == 0
if nonanticip{rowindex}{4}==0 % this is a current nonanticipated shock
%this is a current nonanticipated shock
M_.det_shocks = [ M_.det_shocks;struct('exo_det',0,'exo_id',(nonanticip{rowindex}{2}+1),'multiplicative',0,'periods',1:1,'value',nonanticip{rowindex}{7}) ]; M_.det_shocks = [ M_.det_shocks;struct('exo_det',0,'exo_id',(nonanticip{rowindex}{2}+1),'multiplicative',0,'periods',1:1,'value',nonanticip{rowindex}{7}) ];
else else
%this is a delayed nonanticipated shock % this is a delayed nonanticipated shock
M_.det_shocks = [ M_.det_shocks;struct('exo_det',0,'exo_id',(nonanticip{rowindex}{2}+1),'multiplicative',0,'periods',(nonanticip{rowindex}{5}):(nonanticip{rowindex}{6}),'value',nonanticip{rowindex}{7}) ]; M_.det_shocks = [ M_.det_shocks;struct('exo_det',0,'exo_id',(nonanticip{rowindex}{2}+1),'multiplicative',0,'periods',(nonanticip{rowindex}{5}):(nonanticip{rowindex}{6}),'value',nonanticip{rowindex}{7}) ];
end end
if nonanticip{rowindex+1}{1}~=currentperiod if nonanticip{rowindex+1}{1} ~= currentperiod
%when we have tracked all first period shocks we can simulate % when we have tracked all first period shocks we can simulate
options_.periods = jm.simperiods; options_.periods = jm.simperiods;
yy=oo_.steady_state; yy = oo_.steady_state;
perfect_foresight_setup; perfect_foresight_setup;
[rowexo,colexo]=size(oo_.exo_simul); [rowexo, colexo] = size(oo_.exo_simul);
perfect_foresight_solver; perfect_foresight_solver;
if nonanticip{rowindex+1}{1}>0 if nonanticip{rowindex+1}{1} > 0
%we collect all the path from ooendo period 1 to just before the next shock... % we collect all the path from ooendo period 1 to just before the next shock...
yy=[yy,oo_.endo_simul(:,2:(2+(nonanticip{rowindex+1}{1}-currentperiod-1)))]; yy = [yy oo_.endo_simul(:,2:(2+(nonanticip{rowindex+1}{1}-currentperiod-1)))];
else else
%... or if there are no more shocks we collect the whole path % or if there are no more shocks we collect the whole path
yy=[yy,oo_.endo_simul(:,2:end)]; yy = [yy oo_.endo_simul(:,2:end)];
end end
ooexosaved=oo_.exo_simul; ooexosaved = oo_.exo_simul;
firstsimul=1; firstsimul = 1;
end end
else else
%currentperiod is larger than one: we first perform perfect foresight simulation with initial period 1 conditions % currentperiod is larger than one: we first perform perfect foresight simulation with initial period 1 conditions
if firstsimul==0 if firstsimul == 0
%Initializing the first simulation % Initializing the first simulation
options_.periods = jm.simperiods; options_.periods = jm.simperiods;
yy=oo_.steady_state; yy = oo_.steady_state;
perfect_foresight_setup; perfect_foresight_setup;
[rowexo,colexo]=size(oo_.exo_simul); [rowexo, colexo] = size(oo_.exo_simul);
perfect_foresight_solver; perfect_foresight_solver;
%In this because there is at least one shock we did not consider yet in the first period, we only save the path from the beginning up the period just before the current % In this because there is at least one shock we did not consider yet in the first period, we only save the path from the beginning up the period just before the current
yy=[yy,oo_.endo_simul(:,2:currentperiod)]; yy = [yy oo_.endo_simul(:,2:currentperiod)];
ooexosaved=oo_.exo_simul; ooexosaved = oo_.exo_simul;
firstsimul=1; firstsimul = 1;
end end
if nonanticip{rowindex}{3}==1 if nonanticip{rowindex}{3} == 1
%this is a permanent shock % permanent shock
oo_.exo_steady_state((nonanticip{rowindex}{2}+1)) = nonanticip{rowindex}{7}; oo_.exo_steady_state(nonanticip{rowindex}{2}+1) = nonanticip{rowindex}{7};
steady; steady;
savedpermanentSS=oo_.steady_state; savedpermanentSS = oo_.steady_state;
if nargout == 1 if nargout == 1
data2json.steady_state2=oo_.steady_state; data2json.steady_state2 = oo_.steady_state;
end end
if nonanticip{rowindex}{4}==0 if nonanticip{rowindex}{4} == 0
%this is a current permanent nonanticipated shock % current permanent nonanticipated shock
ooexosaved((currentperiod+1):end,(nonanticip{rowindex}{2}+1))=nonanticip{rowindex}{7}; ooexosaved(currentperiod+1:end, nonanticip{rowindex}{2}+1) = nonanticip{rowindex}{7};
else else
%this is a delayed permanent nonanticipated shock % delayed permanent nonanticipated shock
ooexosaved((nonanticip{rowindex}{5}+1):end,(nonanticip{rowindex}{2}+1))=nonanticip{rowindex}{7}; ooexosaved(nonanticip{rowindex}{5}+1:end, nonanticip{rowindex}{2}+1) = nonanticip{rowindex}{7};
end end
else else
%this is not a permanent shock % not a permanent shock
%we add new shocks in the saved timepath with original time indexes % add new shocks in the saved timepath with original time indexes
if nonanticip{rowindex}{4}==0 if nonanticip{rowindex}{4} == 0
%this is a single current nonanticipated shock % this is a single current nonanticipated shock
ooexosaved(currentperiod+1,(nonanticip{rowindex}{2}+1))=nonanticip{rowindex}{7}; ooexosaved(currentperiod+1, nonanticip{rowindex}{2}+1) = nonanticip{rowindex}{7};
%oo_.exo_simul(2,(nonanticip{rowindex}{2}+1))=nonanticip{rowindex}{7};
else else
%this is a delayed nonanticipated shock % this is a delayed nonanticipated shock
ooexosaved((nonanticip{rowindex}{5}+1):(nonanticip{rowindex}{6}+1),(nonanticip{rowindex}{2}+1))=nonanticip{rowindex}{7}; ooexosaved(nonanticip{rowindex}{5}+1:nonanticip{rowindex}{6}+1, nonanticip{rowindex}{2}+1) = nonanticip{rowindex}{7};
%oo_.exo_simul((nonanticip{rowindex}{5}+1):(nonanticip{rowindex}{6}+1),(nonanticip{rowindex}{2}+1))=nonanticip{rowindex}{7};
end end
end end
%we copy only the necessary window in oo_.exo_simul % copy only the necessary window in oo_.exo_simul
%oo_.exo_simul=ooexosaved((currentperiod+1):end,:); oo_.exo_simul = [zeros(1, colexo); ooexosaved(currentperiod+1:end, :)];
oo_.exo_simul=[zeros(1,colexo);ooexosaved((currentperiod+1):end,:)];
%we fill oo_.exo_simul until it has the correct size depending on of there are permanent shocks or not % fill oo_.exo_simul until it has the correct size depending on of there are permanent shocks or not
if jm.permanentshockexist==1 if jm.permanentshockexist==1
%if there is a permanent shock we fill with last value of ooexosaved % if there is a permanent shock, fill with last value of ooexosaved
%oo_.exo_simul((length(ooexosaved((currentperiod+1):end,:))+1):rowexo,:)=ones(rowexo-(length(ooexosaved((currentperiod+1):end,:))+1)+1,1)*ooexosaved(end,:); oo_.exo_simul = [oo_.exo_simul; ones(rowexo-size(oo_.exo_simul, 1), 1)*ooexosaved(end, :)];
%oo_.exo_simul((ooexolength+1):rowexo,:)=ones(rowexo-ooexolength,1)*ooexosaved(end,:);
oo_.exo_simul=[oo_.exo_simul;ones(rowexo-size(oo_.exo_simul, 1),1)*ooexosaved(end,:)];
else else
%otherwise we fill with zeros % otherwise fill with zeros
%oo_.exo_simul((length(ooexosaved((currentperiod+1):end,:))+1):rowexo,:)=zeros(rowexo-(length(ooexosaved((currentperiod+1):end,:))+1)+1,colexo); oo_.exo_simul = [oo_.exo_simul; zeros(rowexo-size(oo_.exo_simul, 1), colexo)];
%oo_.exo_simul((ooexolength+1):rowexo,:)=zeros(rowexo-ooexolength,colexo);
oo_.exo_simul=[oo_.exo_simul;zeros(rowexo-size(oo_.exo_simul, 1),colexo)];
end end
if nonanticip{rowindex+1}{1}~=currentperiod if nonanticip{rowindex+1}{1} ~= currentperiod
%when we have tracked all the non-anticipated/delayed shocks for the current period, we can simulate % when we have tracked all the non-anticipated/delayed shocks for the current period, we can simulate
if jm.permanentshockexist == 1
% if there are permanent shocks, fill oo_.endo with finalSS
if jm.permanentshockexist==1 oo_.endo_simul = savedpermanentSS*ones(1, options_.periods+2);
%if there are permanent shocks we fill oo_.endo with finalSS
oo_.endo_simul=savedpermanentSS*ones(1,options_.periods+2);
else else
%no permanent shocks we fill oo_.endo with initialSS % no permanent shocks, fill oo_.endo with initialSS
oo_.endo_simul=oo_.steady_state*ones(1,options_.periods+2); oo_.endo_simul = oo_.steady_state*ones(1, options_.periods+2);
end end
%we need to change oo_.endo_simul first value that gives the initial state of the economy % change oo_.endo_simul first value that gives the initial state of the economy
oo_.endo_simul(:,1)=yy(:,end); oo_.endo_simul(:, 1) = yy(:,end);
perfect_foresight_solver; perfect_foresight_solver;
if nonanticip{rowindex+1}{1} > 0
if nonanticip{rowindex+1}{1}>0 % collect all the path from ooendo period 1 to just before the next shock...
%we collect all the path from ooendo period 1 to just before the next shock... yy = [yy oo_.endo_simul(:, 2:2+nonanticip{rowindex+1}{1}-currentperiod-1)];
yy=[yy,oo_.endo_simul(:,2:(2+(nonanticip{rowindex+1}{1}-currentperiod-1)))];
else else
%... or if there are no more shocks we collect the whole path % or if there are no more shocks we collect the whole path
yy=[yy,oo_.endo_simul(:,2:end)]; yy = [yy oo_.endo_simul(:, 2:end)];
end end
end end
end end
rowindex = rowindex+1;
rowindex=rowindex+1; end
end % while jm.nonanticipmatrix{rowindex}{1}>0 % copy the endo path back
oo_.endo_simul = yy;
%we copy the endo path back
oo_.endo_simul=yy;
else else
%if there are no unanticipated shocks we perform the simulation % if there are no unanticipated shocks we perform the simulation
options_.periods = jm.simperiods; options_.periods = jm.simperiods;
perfect_foresight_setup; perfect_foresight_setup;
perfect_foresight_solver; perfect_foresight_solver;
end end
if nargout == 1 if nargout == 1
plotlgt=length(oo_.endo_simul); plotlgt = length(oo_.endo_simul);
data2json.endosimul_length=plotlgt; data2json.endosimul_length = plotlgt;
data2json.endo_names=char(M_.endo_names); data2json.endo_names = char(M_.endo_names);
data2json.endo_nbr=M_.endo_nbr; data2json.endo_nbr = M_.endo_nbr;
for nendo = 1:M_.endo_nbr for nendo = 1:M_.endo_nbr
data2json.endo_simul.(strtrim(char(M_.endo_names(nendo,:))))=oo_.endo_simul(nendo,:); data2json.endo_simul.(strtrim(char(M_.endo_names(nendo, :)))) = oo_.endo_simul(nendo, :);
end end
data2json.endo_simul.plotx=[0:1:plotlgt]; data2json.endo_simul.plotx = 0:plotlgt;
varargout{1} = savejson('',data2json,''); varargout{1} = savejson('', data2json, '');
end end
end end