IRF for multiple parameter values

Hi

I am currently simulating a model on Dynare using the following code below:

WealthShare4.mod (1.7 KB)

The code generates IRFs for my given calibration. However I would like the IRFs to display how the response variable changes as I vary a given parameter (PSI in the code). I tried the following:

rhos = 0.5:1.5;
first_time = 1;
for i=1:length(rhos)
if first_time
set_param_value(‘PSI’,rhos(i));
dynare WealthShare4.mod;
first_time = 0;
else
set_param_value(‘PSI’,rhos(i));
info = stoch_simul(var_list_);
if info;
disp('Computation fails for PSI = ’ num2str(PSI)]);
end;
end
end

However this does not work and I get a syntax error problem. Any thoughts?

It should be something along the lines of

dynare WealthShare4.mod;
rhos = 0.5:1.5;
first_time = 1;
for i=1:length(rhos)
    set_param_value('PSI',rhos(i));
    [info, oo_, options_, M_] = stoch_simul(M_, options_, oo_, var_list_);
    if info;
        disp(['Computation fails for PSI = ' num2str(PSI)]);
    end
end
1 Like

It looks like the code just ran the model separately for the two values of PSI. How could I generate a single IRF graph for a given variable that plots the impulse response for different levels of IES? Is this possible on Dynare? My first attempt was something like this:

dynare WealthShare4.mod;
load('WealthShare4_results.mat', 'oo_')
irf1=oo_.irfs;
save irf1

load irf1
dynare WealthShare3.mod noclearall
load('WealthShare3_results.mat', 'oo_')
irf2=oo_.irfs;
save irf2

load irf2
ending_cell={'g'};
for ii=1:length(ending_cell)

HOR=1:1:20;

var={'dws','e'};

figure for jj=1:length(var)
subplot(1,2,jj)
eval('irf1.' var{1,jj},ending_cell{1,ii}]);
eval('irf2.' var{1,jj},ending_cell{1,ii}]);

load irf2

end

But it doesn’t seem to work.

dynare WealthShare4.mod;

rhos=[0.5 0.6];
for ii=1:length(rho)
    set_param_value('PSI',rhos(ii));
    [info, oo_, options_, M_] = stoch_simul(M_, options_, oo_, var_list_);
    if ~info
        irf_cell{ii}=oo_.irfs;
    end
end

shock_ending_cell={'g'};
var_names={'dws','e'};
for shock_iter=1:length(shock_ending_cell)  
    figure('Name',shock_ending_cell{shock_iter})
    for var_iter=1:length(var_names)
        subplot(1,2,var_iter)       
        plot(1:options_.irf,irf_cell{1}.([var_names{var_iter},'_',shock_ending_cell{shock_iter}]))
        hold on
        plot(1:options_.irf,irf_cell{2}.([var_names{var_iter},'_',shock_ending_cell{shock_iter}]))
        title(var_names{var_iter})
    end    
end

should work together with

var ch cf cdiff ddiff df dh vh vf rh rf wh wf mh mf e ws dws rdiff;
varexo h f g;

parameters DELTA THETA PSI TAU_H TAU_F MU RHO;

DELTA=0.98;
PSI=0.01;
TAU_H=0.5;
TAU_F=1.5;
THETA=(1-2)/(1-1/0.01); 
MU=0.0015;
RHO=.9;

model;
vh       = DELTA^THETA * exp((-THETA/PSI)*dh(+1) + (THETA-1)*log((1+vh(+1))*exp(dh(+1))/vh) ) * (1+vh(+1))*exp(dh(+1));
vf       = DELTA^THETA * exp((-THETA/PSI)*df(+1) + (THETA-1)*log((1+vf(+1))*exp(df(+1))/vf) ) * (1+vf(+1))*exp(df(+1));
ch        = RHO * ch(-1) - h - TAU_H * g;
cf        = RHO * cf(-1) - f - TAU_F * g;
cdiff     = ch - cf;
wh       = vh * exp(ch);
wf       = vf * exp(cf);
ws       = log(wh) - log(wf);
dws       = ws - ws(-1);
dh        = ch - ch(-1);
df        = cf - cf(-1);
ddiff     = dh - df;
rh       = ((1+vh)*exp(dh))/vh(-1);
rf       = ((1+vf)*exp(df))/vf(-1);
rdiff    = rh - rf;
mh        = DELTA^THETA * exp((-THETA/PSI)*dh + (THETA-1)*log(rh));
mf       = DELTA^THETA * exp((-THETA/PSI)*df + (THETA-1)*log(rf));
e        = mf - mh;
end;

initval;
ch=0;
cf=0;
dh=0;
df=0;
dws=0; 

vh=100;
vf=100;
cdiff    = ch - cf;
ddiff    = dh - df;
rh       = ((1+vh)/vh)*exp(dh);
rf       = ((1+vf)/vf)*exp(df);
wh       = vh * exp(ch);
wf       = vf * exp(cf);
mh        = DELTA^THETA * exp((-THETA/PSI)*dh + (THETA-1)*log(rh));
mf        = DELTA^THETA * exp((-THETA/PSI)*df + (THETA-1)*log(rf));
e         = mf - mh;
ws        = log(wh) - log(wf);
rdiff        = rh - rf;
end;

shocks;
var h;
stderr 0.01;
var f;
stderr 0.01;
var g;
stderr 0.01;
end;

resid;
steady;
check;

stoch_simul(order=3, periods=0,simul_replic=100, irf=20) dws e;```

Thanks for the help, this code seems to work. However I had one last question. I would like to change the sub plot titles which currently appear as variable names. I tried the following:

dynare WealthShare4.mod;

rhos=[0.5 1.5 2.5];
for ii=1:length(rhos)
    set_param_value('PSI',rhos(ii));
    [info, oo_, options_, M_] = stoch_simul(M_, options_, oo_, var_list_);
    if ~info
        irf_cell{ii}=oo_.irfs;
    end
end

shock_ending_cell={'g'};
var_names={'dws','rdiff','e'};
for shock_iter=1:length(shock_ending_cell)  
    figure('Name',shock_ending_cell{shock_iter})
    for var_iter=1:length(var_names)
        subplot(2,2,var_iter)       
        plot(1:options_.irf,irf_cell{1}.([var_names{var_iter},'_',shock_ending_cell{shock_iter}]))
        legend({'$\psi=0.5$','$\psi=1.5$','$\psi=2.5$'},'Interpreter','latex')
title('$\tilde{W}_{t}$', 'Interpreter','latex', 'fontweight', 'bold', 'Units', 'normalized', 'Position', [0.5, 1.05, 0]);
        hold on
        plot(1:options_.irf,irf_cell{2}.([var_names{var_iter},'_',shock_ending_cell{shock_iter}]))
        legend({'$\psi=0.5$','$\psi=1.5$','$\psi=2.5$'},'Interpreter','latex')
  title('$\mathcal{E}_{t}$', 'Interpreter','latex', 'fontweight', 'bold', 'Units', 'normalized', 'Position', [0.5, 1.05, 0]);
    end    
end

When I do this, it just changes the titles of both subplots to the last title in the code. Do you have any idea on how I can change the subplot titles in this loop to reflect the two corresponding titles I’ve written in latex code?

I would guess you are looking for

shock_ending_cell={'g'};
var_names={'dws','rdiff','e'};
var_title={'$\tilde{W}_{t}$','$R$','$\mathcal{E}_{t}$'};
for shock_iter=1:length(shock_ending_cell)  
    figure('Name',shock_ending_cell{shock_iter})
    for var_iter=1:length(var_names)
        subplot(2,2,var_iter)       
        for par_iter=1:size(irf_cell,2)
            hold on
            plot(1:options_.irf,irf_cell{par_iter}.([var_names{var_iter},'_',shock_ending_cell{shock_iter}]))
        end
        legend({'$\psi=0.5$','$\psi=1.5$','$\psi=2.5$'},'Interpreter','latex')
        title(var_title{var_iter}, 'Interpreter','latex', 'fontweight', 'bold', 'Units', 'normalized', 'Position', [0.5, 1.05, 0]);
    end    
end

Sorry to bother about this again. I added this to my code and I get the following error:

"
Error using plot
Vectors must be the same length.

Error in IES (line 21)
plot(1:options_.irf,irf_cell{par_iter}.([var_names{var_iter},’_’,shock_ending_cell{shock_iter}]))"

Any ideas?

Always provide the full files.

dynare WealthShare4.mod;

rhos=[0.5 1.5 2.5];
for ii=1:length(rhos)
    set_param_value('PSI',rhos(ii));
    [info, oo_, options_, M_] = stoch_simul(M_, options_, oo_, var_list_);
    if ~info
        irf_cell{ii}=oo_.irfs;
    end
end

shock_ending_cell={'g'};
var_names={'dws','rdiff','e'};
var_title={'$\tilde{W}_{t}$','$R$','$\mathcal{E}_{t}$'};
for shock_iter=1:length(shock_ending_cell)  
    figure('Name',shock_ending_cell{shock_iter})
    for var_iter=1:length(var_names)
        subplot(2,2,var_iter)       
        for par_iter=1:size(irf_cell,2)
            hold on
            plot(1:options_.irf,irf_cell{par_iter}.([var_names{var_iter},'_',shock_ending_cell{shock_iter}]))
        end
        legend({'$\psi=0.5$','$\psi=1.5$','$\psi=2.5$'},'Interpreter','latex')
        title(var_title{var_iter}, 'Interpreter','latex', 'fontweight', 'bold', 'Units', 'normalized', 'Position', [0.5, 1.05, 0]);
    end    
end

This was the code that generated the error above.

Please also provide the mod-file.

IES.m (926 Bytes)

This is the mod file.

That was only the m-file.

OpenEconomy1.mod (2.7 KB)

Sorry this is the mod file referred to in the m-file code.

In Dynare 4.6.4 I can run your code without problems.

This is interesting because I tried a modification of this code and it gives me this error message now:

Error using plot
Vectors must be the same length.

Error in CESComparativeStatics (line 21)
plot(1:options_.irf,irf_cell{par_iter}.([var_names{var_iter},’_’,shock_ending_cell{shock_iter}]))

CES Comparative Statics.m (915 Bytes)
CES.mod (3.2 KB)

I’ve uploaded both the m file and the Dynare mod files.

Which Dynare version are you using? I cannot reproduce the problem.

This is Dynare 4.6.4

Again, I cannot reproduce the problem. Are you sure you provided the same files you are executing? Try downloading the posted files into a separate folder and then running them.