From 0ad8bfbd16d38e227066254a881d5a79265ab3b0 Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Tue, 5 Feb 2019 15:46:57 +0100 Subject: [PATCH] gui: clean up file --- matlab/+gui/+perfect-foresight/run.m | 222 ++++++++++++--------------- 1 file changed, 98 insertions(+), 124 deletions(-) diff --git a/matlab/+gui/+perfect-foresight/run.m b/matlab/+gui/+perfect-foresight/run.m index 4463c3ea5..44bae0293 100644 --- a/matlab/+gui/+perfect-foresight/run.m +++ b/matlab/+gui/+perfect-foresight/run.m @@ -11,7 +11,7 @@ function varargout = run(json) % varargout{1} [string] if desired, return output as JSON string % % SPECIAL REQUIREMENTS -% dynare must have been run with the option: json=compute +% none % Copyright (C) 2019 Dynare Team % @@ -32,6 +32,7 @@ function varargout = run(json) global M_ options_ oo_ ys0_ ex0_ +%% Check Inputs if nargin ~= 1 || ~ischar(json) error('function takes one string input argument') end @@ -40,14 +41,11 @@ if nargout > 1 error('function provides up to one output argument') end -%loading JSON -jm = loadjson(json,'SimplifyCell',0); +%% Read JSON +jm = loadjson(json, 'SimplifyCell', 0); -%We test if jsonload loads string or char -% jsl=length(class(jm.jsontest{1,1})); - -% INITVAL instructions -%we initialize exogenous shocks to zero and compute initial ss +%% INITVAL instructions +% initialize exogenous shocks to zero and compute initial steady state options_.initval_file = 0; oo_.exo_steady_state(1:jm.exonum) = 0; if M_.exo_nbr > 0 @@ -58,199 +56,175 @@ if M_.exo_det_nbr > 0 end steady; if nargout == 1 - data2json=struct(); - data2json.steady_state1=oo_.steady_state; + data2json = struct(); + data2json.steady_state1 = oo_.steady_state; end - -% ENDVAL instructions -%we initialize exogenous shocks to zero and compute final ss unless there is a permanent shock +%% ENDVAL instructions +% initialize exogenous shocks to zero and compute final ss unless there is a permanent shock ys0_= oo_.steady_state; ex0_ = oo_.exo_steady_state; -if jm.permanentshockexist==0 +if jm.permanentshockexist == 0 oo_.exo_steady_state(1:jm.exonum) = 0; else 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; - 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 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 steady; -savedpermanentSS=oo_.steady_state; +savedpermanentSS = oo_.steady_state; if nargout == 1 - data2json.steady_state2=oo_.steady_state; + data2json.steady_state2 = oo_.steady_state; end - -if jm.transitoryshockexist==1 - % SHOCKS instructions (for transitory shocks) +%% SHOCKS instructions (for transitory shocks) +if jm.transitoryshockexist == 1 for exotriter = 1:length(jm.shocksdescription) - currenttrshock=jm.shocksdescription(exotriter); - % disp(currenttrshock{1}.shockname); - % disp(class(currenttrshock{1}.shockstartperiod)); + currenttrshock = jm.shocksdescription(exotriter); 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 M_.exo_det_length = 0; end -if ((jm.nonanticipatedshockexist==1) || (jm.delayexist==1)) - - nonanticip=jm.nonanticipmatrix; - rowindex=1; - firstsimul=0; - - while nonanticip{rowindex}{1}>0 - +if jm.nonanticipatedshockexist == 1 || jm.delayexist == 1 + nonanticip = jm.nonanticipmatrix; + rowindex = 1; + firstsimul = 0; + while nonanticip{rowindex}{1} > 0 currentperiod=nonanticip{rowindex}{1}; - - if currentperiod==1 - %there are nonanticipated shocks to add at first period - if nonanticip{rowindex}{4}==0 - %this is a current nonanticipated shock + if currentperiod == 1 + % there are nonanticipated shocks to add at first period + if nonanticip{rowindex}{4} == 0 + % 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}) ]; 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}) ]; end - if nonanticip{rowindex+1}{1}~=currentperiod - %when we have tracked all first period shocks we can simulate + if nonanticip{rowindex+1}{1} ~= currentperiod + % when we have tracked all first period shocks we can simulate options_.periods = jm.simperiods; - yy=oo_.steady_state; + yy = oo_.steady_state; perfect_foresight_setup; - [rowexo,colexo]=size(oo_.exo_simul); + [rowexo, colexo] = size(oo_.exo_simul); perfect_foresight_solver; - if nonanticip{rowindex+1}{1}>0 - %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)))]; + if nonanticip{rowindex+1}{1} > 0 + % 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)))]; else - %... or if there are no more shocks we collect the whole path - yy=[yy,oo_.endo_simul(:,2:end)]; + % or if there are no more shocks we collect the whole path + yy = [yy oo_.endo_simul(:,2:end)]; end - ooexosaved=oo_.exo_simul; - firstsimul=1; + ooexosaved = oo_.exo_simul; + firstsimul = 1; end else - %currentperiod is larger than one: we first perform perfect foresight simulation with initial period 1 conditions - if firstsimul==0 - %Initializing the first simulation + % currentperiod is larger than one: we first perform perfect foresight simulation with initial period 1 conditions + if firstsimul == 0 + % Initializing the first simulation options_.periods = jm.simperiods; - yy=oo_.steady_state; + yy = oo_.steady_state; perfect_foresight_setup; - [rowexo,colexo]=size(oo_.exo_simul); + [rowexo, colexo] = size(oo_.exo_simul); 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 - yy=[yy,oo_.endo_simul(:,2:currentperiod)]; - ooexosaved=oo_.exo_simul; - firstsimul=1; + % 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)]; + ooexosaved = oo_.exo_simul; + firstsimul = 1; end - if nonanticip{rowindex}{3}==1 - %this is a permanent shock - oo_.exo_steady_state((nonanticip{rowindex}{2}+1)) = nonanticip{rowindex}{7}; + if nonanticip{rowindex}{3} == 1 + % permanent shock + oo_.exo_steady_state(nonanticip{rowindex}{2}+1) = nonanticip{rowindex}{7}; steady; - savedpermanentSS=oo_.steady_state; + savedpermanentSS = oo_.steady_state; if nargout == 1 - data2json.steady_state2=oo_.steady_state; + data2json.steady_state2 = oo_.steady_state; end - if nonanticip{rowindex}{4}==0 - %this is a current permanent nonanticipated shock - ooexosaved((currentperiod+1):end,(nonanticip{rowindex}{2}+1))=nonanticip{rowindex}{7}; + if nonanticip{rowindex}{4} == 0 + % current permanent nonanticipated shock + ooexosaved(currentperiod+1:end, nonanticip{rowindex}{2}+1) = nonanticip{rowindex}{7}; else - %this is a delayed permanent nonanticipated shock - ooexosaved((nonanticip{rowindex}{5}+1):end,(nonanticip{rowindex}{2}+1))=nonanticip{rowindex}{7}; + % delayed permanent nonanticipated shock + ooexosaved(nonanticip{rowindex}{5}+1:end, nonanticip{rowindex}{2}+1) = nonanticip{rowindex}{7}; end else - %this is not a permanent shock - %we add new shocks in the saved timepath with original time indexes - if nonanticip{rowindex}{4}==0 - %this is a single current nonanticipated shock - ooexosaved(currentperiod+1,(nonanticip{rowindex}{2}+1))=nonanticip{rowindex}{7}; - %oo_.exo_simul(2,(nonanticip{rowindex}{2}+1))=nonanticip{rowindex}{7}; + % not a permanent shock + % add new shocks in the saved timepath with original time indexes + if nonanticip{rowindex}{4} == 0 + % this is a single current nonanticipated shock + ooexosaved(currentperiod+1, nonanticip{rowindex}{2}+1) = nonanticip{rowindex}{7}; else - %this is a delayed nonanticipated shock - 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}; + % this is a delayed nonanticipated shock + ooexosaved(nonanticip{rowindex}{5}+1:nonanticip{rowindex}{6}+1, nonanticip{rowindex}{2}+1) = nonanticip{rowindex}{7}; end end - %we 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,:)]; + % copy only the necessary window in oo_.exo_simul + 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 there is a permanent shock we 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((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,:)]; + % if there is a permanent shock, fill with last value of ooexosaved + oo_.exo_simul = [oo_.exo_simul; ones(rowexo-size(oo_.exo_simul, 1), 1)*ooexosaved(end, :)]; else - %otherwise we 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((ooexolength+1):rowexo,:)=zeros(rowexo-ooexolength,colexo); - oo_.exo_simul=[oo_.exo_simul;zeros(rowexo-size(oo_.exo_simul, 1),colexo)]; + % otherwise fill with zeros + oo_.exo_simul = [oo_.exo_simul; zeros(rowexo-size(oo_.exo_simul, 1), colexo)]; end - if nonanticip{rowindex+1}{1}~=currentperiod - %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 we fill oo_.endo with finalSS - oo_.endo_simul=savedpermanentSS*ones(1,options_.periods+2); + if nonanticip{rowindex+1}{1} ~= currentperiod + % 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 + oo_.endo_simul = savedpermanentSS*ones(1, options_.periods+2); else - %no permanent shocks we fill oo_.endo with initialSS - oo_.endo_simul=oo_.steady_state*ones(1,options_.periods+2); + % no permanent shocks, fill oo_.endo with initialSS + oo_.endo_simul = oo_.steady_state*ones(1, options_.periods+2); end - %we need to change oo_.endo_simul first value that gives the initial state of the economy - oo_.endo_simul(:,1)=yy(:,end); - + % change oo_.endo_simul first value that gives the initial state of the economy + oo_.endo_simul(:, 1) = yy(:,end); perfect_foresight_solver; - - if nonanticip{rowindex+1}{1}>0 - %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)))]; + if nonanticip{rowindex+1}{1} > 0 + % 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)]; else - %... or if there are no more shocks we collect the whole path - yy=[yy,oo_.endo_simul(:,2:end)]; + % or if there are no more shocks we collect the whole path + yy = [yy oo_.endo_simul(:, 2:end)]; end end - end - - rowindex=rowindex+1; - end % while jm.nonanticipmatrix{rowindex}{1}>0 - - %we copy the endo path back - oo_.endo_simul=yy; - + rowindex = rowindex+1; + end + % copy the endo path back + oo_.endo_simul = yy; 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; perfect_foresight_setup; perfect_foresight_solver; end if nargout == 1 - plotlgt=length(oo_.endo_simul); - data2json.endosimul_length=plotlgt; - data2json.endo_names=char(M_.endo_names); - data2json.endo_nbr=M_.endo_nbr; + plotlgt = length(oo_.endo_simul); + data2json.endosimul_length = plotlgt; + data2json.endo_names = char(M_.endo_names); + data2json.endo_nbr = 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 - data2json.endo_simul.plotx=[0:1:plotlgt]; - varargout{1} = savejson('',data2json,''); + data2json.endo_simul.plotx = 0:plotlgt; + varargout{1} = savejson('', data2json, ''); end end