Why I can't replicate IRFs using the same codes with Dyanre

I have played with a toy model to replicate the IRFs using 2nd approximation. I have copied the codes from irf.m indynare but I can not replicate the same IRFs. Is there anything wrong?
I run the following code after the dynare finish the stochastic simulation.
I have debug the irf.m and stoch_simul.m, but nothings seems wrong.

iorder =2 ;
replic =50; %the default of irf calculation
long =options_.irf; %the default should be 40
drop = 100; %the default of dynare
i_exo_var =setdiff([1:M_.exo_nbr],find(diag(M_.Sigma_e)==0));

nxs = length(i_exo_var);

% set the path for the exogenous variables, column vector for each exog.
% variables
ex1=zeros(long +drop,M_.exo_nbr);
ex2=ex1;
%the following code is intercepted from dynare irf.m file
if M_.maximum_lag >=1
    temps = repmat(oo_.dr.ys,1,M_.maximum_lag);
else
    temps=zeros(M_.endo_nbr,1);
end
%get standard deviation of all shocks
chol_S = chol(M_.Sigma_e(i_exo_var,i_exo_var));
yy=0;
for j=1:replic
    ex1(:,i_exo_var)=randn(long+drop,nxs)*chol_S;
    ex2 = ex1;
    %add one standard deviation shock to technology shock eA which is 0.01,
    %totally four exog. variables, leaving the other three zero
    ex2(drop+1,:)=ex2(drop+1,:)+[0.01 0 0 0];
    y1=simult_(temps,oo_.dr,ex1,iorder);
    y2=simult_(temps,oo_.dr,ex2,iorder);
    yy=yy+(y2(:,M_.maximum_lag+drop+1:end)-y1(:,M_.maximum_lag+drop+1:end));
end
yy=yy/replic;
%display the results, not the same
 [oo_.irfs.pisharp_eA' yy(1,:)']

The results is:
-0.0158 -0.0161
-0.0141 -0.0140
-0.0125 -0.0125
-0.0111 -0.0113
-0.0101 -0.0104
-0.0091 -0.0095
-0.0084 -0.0087
-0.0077 -0.0081
-0.0070 -0.0076
-0.0065 -0.0070
-0.0059 -0.0064
-0.0054 -0.0059
-0.0050 -0.0053
-0.0046 -0.0049
-0.0042 -0.0045
-0.0039 -0.0041
-0.0036 -0.0038
-0.0034 -0.0035
-0.0032 -0.0031
-0.0030 -0.0028
-0.0028 -0.0027
-0.0026 -0.0024
-0.0024 -0.0022
-0.0022 -0.0021
-0.0021 -0.0020
-0.0019 -0.0018
-0.0018 -0.0017
-0.0017 -0.0016
-0.0016 -0.0014
-0.0014 -0.0013
-0.0014 -0.0012
-0.0012 -0.0011
-0.0011 -0.0010
-0.0010 -0.0009
-0.0009 -0.0008
-0.0009 -0.0008
-0.0008 -0.0007
-0.0007 -0.0006
-0.0007 -0.0006
-0.0006 -0.0005
A_NK_Model.rar (2.99 KB)

You are using different random numbers. Use

before your code.

[quote=“jpfeifer”]You are using different random numbers. Use

before your code.[/quote]

It works! Thanks very much!
BTW, are you a team member of Dynare? You are so familiar with it.

Judging from my name on the manual, I would say yes…

1 Like