# MANUAL creation of IRF function

Hi,
this is my code. In the first part is requested to solve a partial equilibrium model. The model works, but in the second part of the exercise is required to create our own impulse response graphs and to compare them with the ones created authomatically by the software. I don’t know exactly how to do it: i’ve part of the code to use but i’m not able to use the data from the policy function to generate matrices in which taking values to plot. the problem is probably more theoretical than a codes’ problem.
Can someone help me?
Thank you

``````// MARKET FOR ENGINEERS

/********
*Model in levels*
********/

var s w N P ;

varexo e u;          // e=epsilon s u=epsilon d

parameters a0 a1 d0 d1 beta delta ;

a0 = 10;
a1 = 1;
d0 = 1000;
d1 = 1;
beta = 0.99;
delta = 0.02;

model(linear);

P=(1-delta)*beta*P(+1)+(1-delta)^(5)*beta^(5)*w(+5);
s=a0+a1*P+e;
N=(1-delta)*N(-1)+s(-5);
N=d0-d1*w+u;

end;

// the model is already loglinearized so there is no need of writing the initial values of steady state.

check;

shocks;
var e; stderr 1;
var u; stderr 1;

end;

stoch_simul(periods=500 ,order=1);

//ordering variables s w N P

p_s = 1;
p_w = 2;
p_N = 3;
p_P = 4;

//

% state space representation: S(t) = A*S(t-1) + B*e(t),
% X(t) = C*S(t-1) + D*e(t);

C = [oo_.dr.ghx(oo_.dr.inv_order_var(p_s),:);
oo_.dr.ghx(oo_.dr.inv_order_var(p_w),:);
oo_.dr.ghx(oo_.dr.inv_order_var(p_N),:);
oo_.dr.ghx(oo_.dr.inv_order_var(p_P),:)];
D = [oo_.dr.ghu(oo_.dr.inv_order_var(p_s),:);
oo_.dr.ghu(oo_.dr.inv_order_var(p_w),:);
oo_.dr.ghu(oo_.dr.inv_order_var(p_N),:);
oo_.dr.ghu(oo_.dr.inv_order_var(p_P),:)];
H = 500;

Xirf = zeros(4,H);

Xirf(:,1) = D;
for j = 2:H

Xirf(:,j) = C;
end;``````

You need to identify the states in Dynare’s decision rule matrices and then simulate forward. Dynare does this in it’s `simult_.m`
function. If you do not want to use the function directly, you need the respective part of it:

``` k2 = dr.kstate(find(dr.kstate(:,2) <= M_.maximum_lag+1),[1 2]); k2 = k2(:,1)+(M_.maximum_lag+1-k2(:,2))*endo_nbr; order_var = dr.order_var; epsilon = dr.ghu*transpose(ex_); for i = 2:iter+M_.maximum_lag yhat = y_(order_var(k2),i-1); y_(order_var,i) = dr.ghx*yhat + epsilon(:,i-1); end y_ = bsxfun(@plus,y_,dr.ys); ```

Is it better for use to use another code used during lessons.
we should in practice first order our variables

``````p_s=1
p_w=2
p_N=3
p_P=4``````

then we should create our matrices A, B,C,D based on the state space representation

[code]A = [oo_.dr.ghx(oo_.dr.inv_order_var(p_s),:);
oo_.dr.ghx(oo_.dr.inv_order_var(p_N),:)];

B =[oo_.dr.ghu(oo_.dr.inv_order_var(p_s),:);
oo_.dr.ghu(oo_.dr.inv_order_var(p_N),:)];

``````  C =[oo_.dr.ghx(oo_.dr.inv_order_var(p_w),:);
oo_.dr.ghx(oo_.dr.inv_order_var(p_P),:)];[/code]
``````

Nb this is just an example…
and then we should use the matrices to create the series to plot

```Sirf = zeros(2,H); 4 Xirf = zeros(6,H); 5 6 Sirf(:,1) = B*sigmae; 7 Xirf(:,1) = D*sigmae; 8 9 for j = 2:H 10 Sirf(:,j) = A*Sirf(:,j−1); ```
Is always an example…
The problem is that, given the model, we should have 12 variables, 6 states and 6 controls;
the states are s(-1)…s(-5) and N(-1), since we have fixed K=5, and the same for the controls( w(+1), etc). How can we say to dynare to use these variable?
Should we order all the 12 variables even if declared just four?
And then, in the matrixes, dynare “reads” s(-1) as a function or an an element of a vector, that for this reason cannot be negative.
During the construction of Xirf and Sirf it complaints about problems with matrixes dimension, and we think it’s because of the wrong construction of A B, C D.
We are not able to find example with variables indexed at times following t+1 or previous to t-1. Probably is not such a big problem but we don’t know how to proceed.
Thank you

In that case you need to introduce auxiliary variables for lag bigger than 1 (essentially a companion form). That way, Dynare will not introduce auxiliary variables itself, which can be hard to access.