Q: combining IRFs into subplotted figures & iterating?

Professor Pfeifer,

Thank you

Best

Hello,

I combined 4 IRFs in 4 subplots and it works with taylor rule (stoch_simul) and ramsey_policy but not with discretionary_policy (although I only changed the filenames in the code below and they all work on their own after editing discretionary_policy_1.m). Is this due to this bug or some failure by me?

Thank you in advance
ompD.mod (754 Bytes) ompDirfs.m (1.1 KB)
(ompD1/2/3 are just line 25 with “epsu(-1/-2/-3)”)

From what I can see, it’s indeed this bug. Did you try the unstable version?

I just tried the latest unstable version. The fix for above is implemented now but multiple IRF with discretionary_policy doesn’t work unfortunately.

What is the error message you are getting?

Like in the fixed single IRF case starting with an error msg of different dimensions (causing the others?).
This happens after the 2nd preprocessing (completed) / first figure:
"Unable to perform assignment because the size of the left side is 6-by-6 and the size of the right side is 5-by-5.

Error in discretionary_policy_engine (line 101)
H0(1:endo_nbr,1:endo_nbr)=H00;

Error in discretionary_policy_1 (line 106)
[H,G,info]=discretionary_policy_engine(A.lag,A.contemp,A.lead,B,W,M_.instr_id,beta,options_.dp.maxit,options_.discretionary_tol,options_.qz_criterium,Hold);

Error in stoch_simul (line 85)
[~,info,M_,options_,oo_] = discretionary_policy_1(options_.instruments,M_,options_,oo_);

Error in discretionary_policy (line 37)
[info, oo_, options_, M_] = stoch_simul(M_, options_, oo_, var_list);

Error in ompD1.driver (line 220)
[info, oo_, options_, M_] = discretionary_policy(M_, options_, oo_, var_list_);

Error in dynare (line 289)
evalin(‘base’,[fname ‘.driver’]) ;

Error in ompDirfs (line 7)
dynare ompD1.mod noclearall"

Discretionary policy employs persistent variables. You must not invoke the noclearall option.

Thanks alot Prof. Pfeifer!

You are working with perfect foresight. In that case, there is no oo_.irfs. You need to adjust your code to access the respective entries of oo_.endo_simul

Thank you for your reply. I have tried doing that in combinedmodel.m (690 Bytes) but still get the same error:

Error using eval
Unrecognized function or variable ‘mat1y_N_gap_epsA_N’.

Error in combinemodel (line 20)

  •   eval(['mat1' var{1,jj},ending_cell{1,ii}]);*
    

I would really appreciate your help with this.

Use

dynare model1
mat1 = oo_.endo_simul;
save model1_results.mat oo_ M_;

dynare model2
mat2 = oo_.endo_simul;
save model2_results.mat oo_ M_;

M1=load('model1_results.mat');
M2=load('model2_results.mat');

HOR=1:13;
var={'y_N_gap', 'pi_N', 'i'};
figure
for jj=1:length(var)
    subplot(2,2,jj)
    hold on
    plot(HOR,M1.oo_.endo_simul(strmatch(var{1,jj},M1.M_.endo_names,'exact'),M1.M_.maximum_lag+1:M1.M_.maximum_lag+13),'-k',...
        HOR,M2.oo_.endo_simul(strmatch(var{1,jj},M2.M_.endo_names,'exact'),M2.M_.maximum_lag+1:M2.M_.maximum_lag+13),'--r','LineWidth',2) ;
    title([var{1,jj}] )
end

It is working now, thank you so much for your help.

Dear Professor,
I have a problem with subplotting IRFs. I have two TANK models, one without idiosyncratic risk and the second with risk; and one shock that is a government spending shock. Here are my two models in Dynare:
tank_cw.mod (4.2 KB)
tankcw1.mod (1.3 KB)
from which I get the IRFs.
Now I have to compare the two models for each endogenous variable; therefore, I have to plot in the same chart the IRFs of labor hours in the two models, then in other chart the IRFs of consumption and so on…
I tried in the following way but nothing changed that is I get again the original IRFs as if I had not applied the following code:
Plotting.m (1.1 KB)

Thank you in advance

Use

clear,clc

% Run and collect dynare outputs

dynare tank_cw;
irf1 = oo_.irfs;
save tank_cw_results.mat irf1;

dynare tankcw1;
irf2 = oo_.irfs;
save tankcw1_results.mat irf2;

ending_cell={'_eps'};

for ii=1:length(ending_cell)
    HOR=1:options_.irf;
    var={'n', 'nW', 'w', 'c', 'cC', 'cW', 'rn', 'r', 'pi', 'b', 'bC', 'bW', 't', 'g', 'd', 'csl', 'chl', 'bsl', 'bhl', 'ls'}; % variables to be plotted as called in Dynare
    fig(ii)= figure('Name',['Shock to',sprintf(ending_cell{1,ii})],'NumberTitle','off');
    % plot the model variables 
    for jj=1:length(var)
        subplot(5,5,jj)
        hold on
        plot(HOR,irf2.([var{1,jj},ending_cell{1,ii}]),HOR,irf1.([var{1,jj},ending_cell{1,ii}]),'LineWidth',1.3);      
        xlim([1 options_.irf]);
        hline = refline(0, 0);
        title([var{1,jj}] )
    end
    
    legend('Model1', 'Model2', 'AutoUpdate','off')
   
end
1 Like

Thank you very much! And in order to increase the dimension of the subplots, for example instead of having subplot (5,5,jj), having subplot (3,3,jj) and subplots(2,2,jj), how should I write the code?

A clean version would be

ending_cell={'_eps'};
var={'n', 'nW', 'w', 'c', 'cC', 'cW', 'rn', 'r', 'pi', 'b', 'bC', 'bW', 't', 'g', 'd', 'csl', 'chl', 'bsl', 'bhl', 'ls'}; % variables to be plotted as called in Dynare
HOR=1:options_.irf;
n_plots=3;
for ii=1:length(ending_cell)
    for jj=1:length(var)
        if mod(jj,n_plots^2)==1
            plot_iter=1;
            fig(ii)= figure('Name',['Shock to',sprintf(ending_cell{1,ii})],'NumberTitle','off');
        end
        subplot(n_plots,n_plots,plot_iter)
        hold on
        plot(HOR,irf2.([var{1,jj},ending_cell{1,ii}]),HOR,irf1.([var{1,jj},ending_cell{1,ii}]),'LineWidth',1.3);      
        xlim([1 options_.irf]);
        hline = refline(0, 0);
        title([var{1,jj}] )
        if plot_iter==n_plots^2 ||  jj==length(var)
            legend('Model1', 'Model2', 'AutoUpdate','off')
        end
        plot_iter=plot_iter+1;
    end
end

I am attempting to plot 5 IRFs on a single graph for a TFP shock and a monetary policy shock. I have modified the code from AITANAgf (reply 37/67 above) to allow for my 5 different models. First, you will need to run calibration4.m and model_run4.m, load the 5 .mod files, then run the jointgraphs.m file. The error message that I obtain is the following:
Reference to non-existent field ‘pit_eps_e’.
Error in jointgraphs (line 36)
eval([‘irf1.’ var{1,jj},ending_cell{1,ii}]);

Many thanks for your help!
model_run4.m (3.1 KB)
calibration4.m (2.5 KB)
thesisdynare4.mod (6.8 KB)
thesisdynarenu.mod (6.8 KB)
thesisdynaretheta.mod (6.8 KB)
thesisdynareeta.mod (6.8 KB)
thesisdynarejoint.mod (6.8 KB)
jointgraphs.m (1.4 KB)

You did not adjust the name of the shock:

ending_cell={'_e_z'};
% 'y', 'bt','ct', 'cu', 'hc', 'hz','hu', 'dp', 'q', 'qz', 'r', 'l', 'dy', pit mc c Y n u lfp w Z
for ii=1:length(ending_cell)
    HOR=1:options_.irf;
    var={ 'pit', 'mc','c', 'Y', 'n', 'u', 'lfp', 'w', 'Z'};
    figure
    for jj=1:length(var)
        subplot(3,3,jj)
        eval(['irf1.' var{1,jj},ending_cell{1,ii}]);
        eval(['irf2.' var{1,jj},ending_cell{1,ii}]);
        eval(['irf3.' var{1,jj},ending_cell{1,ii}]);
        eval(['irf4.' var{1,jj},ending_cell{1,ii}]);
        eval(['irf5.' var{1,jj},ending_cell{1,ii}]);
        hold on
        plot(HOR,[eval(['irf1.' var{1,jj},ending_cell{1,ii}])],'-k',HOR,[eval(['irf2.' var{1,jj},ending_cell{1,ii}])],'--r', HOR,[eval(['irf3.' var{1,jj},ending_cell{1,ii}])], ':m' ,'LineWidth',1) ;
        title([var{1,jj}] )
    end
end

Yes, that worked, thank you! Now, I am assuming that in the plot command on line 42, the ‘-k’, ‘—r’ and ‘:m’ arguments are changing the color/type of line for the IRF plot. After doing a quick search in the Dynare manual, I could not find information on the total possible plot colors and types. Where can I find this information?

Sorry for asking without checking thoroughly enough, I found the information. For those reading in the future, I found plot line types here: