# Matrix Operation In Dynare

Dear community,

Currently I am working on a project with Adaptive Learning. I have developed a simple Neo-Keynesian model as lab to test how far can I go with Dynare. I have so far been able to write most part of the code, but I am facing with the problem that I need to work with a matrix which needs to be inverted in order to use Recursive Least Square Learning. When I try to run my code I get the following message:

``````>> dynare nk_lab
Starting Dynare (version 5.4).
Calling Dynare with arguments: none
Starting preprocessing of the model file ...
ERROR: nk_lab.mod: line 123, col 5: syntax error, unexpected '['
``````

The problem goes directly to the line where I create the R matrix (this is a variance-covariance matrix)

``````% Build R Matrix

R = [R11, R12, R13, R14;
R21, R22, R23, R24;
R31, R32, R33, R34;
R41, R42, R43, R44];
``````

I need this matrix to be inverted so later I can continue with the algorithm, is there a way I could go along?

I would need to see the file.

Sure! I will leave the code extract because I am having troubles uploading it

``````var
y
i
pi

pi_AL

u
g
v

error

a
b
c
d

R11
R12
R13
R14

R21
R22
R23
R24

R31
R32
R33
R34

R41
R42
R43
R44
;

varexo
e_u
e_g
e_v
;

parameters
sigma
beta
eta
omega
kappa
phi_pi
phi_y

rho_u
rho_g
rho_v

theta
;

% Calibration
sigma = 1;
beta = 0.99;
eta = 1;
omega = 0.8;
kappa = (sigma + eta) * (1 - omega) * (1 - beta *omega) / omega;

phi_pi = 1.5;
phi_y = 0.125;

rho_u = 0.5;
rho_g = 0.5;
rho_v = 0.9;

theta = 0.05;

model;

%%% RATIONAL EXPECTATIONS %%%

% IS
y  = y(+1) - 1 / sigma * (i - pi(+1)) +  g;

% Phillip Curve
pi  = beta * pi(+1) + kappa * y + u;

% Monetary Rule
i = phi_y * y  + phi_pi * pi + v;

% Exogenous Inflation AR1
u = rho_u * u(-1) + e_u;

% Exogenous Demand AR1
g = rho_g * g(-1) + e_g;

% Exogenous Monetary AR1
v = rho_v * v(-1) + e_v;

% Variance Covariance Matrix Learning: R_{t} = R_{t-1} + theta *  (z_{t} * z_{t}' - R_{t-1}); Where z_{t} = [1; u_{t}; g_{t}; v_{t}]
R11 = R11(-1) + theta * (1 - R11(-1));
R12 = R12(-1) + theta * (u - R12(-1));
R13 = R13(-1) + theta * (g  - R13(-1));
R14 = R14(-1) + theta * (v - R14(-1));

R21 = R21(-1) + theta * (u - R21(-1));
R22 = R22(-1) + theta * (u^2 - R22(-1));
R23 = R23(-1) + theta * (u * g  - R23(-1));
R24 = R24(-1) + theta * (u * v - R24(-1));

R31 = R31(-1) + theta * (g - R31(-1));
R32 = R32(-1) + theta * (u * g - R32(-1));
R33 = R33(-1) + theta * (g^2  - R33(-1));
R34 = R34(-1) + theta * (g * v - R34(-1));

R41 = R41(-1) + theta * (v - R41(-1));
R42 = R42(-1) + theta * (u * v- R42(-1));
R43 = R43(-1) + theta * (g * v  - R43(-1));
R44 = R44(-1) + theta * (v^2 - R44(-1));

% Build R Matrix

R = [R11, R12, R13, R14;
R21, R22, R23, R24;
R31, R32, R33, R34;
R41, R42, R43, R44];

% Invert Matrix
R_inv = inv(R);

% forecast error: error_{t} = pi_{t} - a_{t-1} - b_{t-1} * u_{t} - c_{t-1} * g_{t} - d_{t-1} * v_{t}
error = pi - a(-1) - b(-1) * u - c(-1) * g - d(-1) * v;

% Recursive Least Square Learning: Zeta_{t} = Zeta_{t-1} + theta * R_{t}^{-1} * z_{t} * error_{t}; Where Zeta_{t} = [a_{t}; b_{t}; c_{t}; d_{t}]
a = a(-1) + theta *  (R_inv(1,1) * 1 * error + R_inv(1,2) * u * error + R_inv(1,3) * g * error + R_inv(1,4) *  v * error);
b = b(-1) + theta *  (R_inv(2,1) * 1 * error + R_inv(2,2) * u * error + R_inv(2,3) * g * error + R_inv(2,4) *  v * error);
c = c(-1) + theta *  (R_inv(3,1) * 1 * error + R_inv(3,2) * u * error + R_inv(3,3) * g * error + R_inv(3,4) *  v * error);
d = d(-1) + theta *  (R_inv(4,1) * 1 * error + R_inv(4,2) * u * error + R_inv(4,3) * g * error + R_inv(4,4) *  v * error);

% Actual Law Motion
pi_AL = a(-1) + b(-1) * u + c(-1) * g + d(-1) * v;

end;

shocks;
var e_u = 1;
var e_g = 1;
var e_v = 1;
end;

initval;
y = 0;
pi = 0;
i = 0;

u = 0;
g = 0;
v = 0;

error = 0;
a = 0;
b = 0;
c = 0;
d = 0;

R11 = 1;
R12 = 0;
R13 = 0;
R14 = 0;

R21 = 0;
R22 = 1;
R23 = 0;
R24 = 0;

R31 = 0;
R32 = 0;
R33 = 1;
R34 = 0;

R41 = 0;
R42 = 0;
R43 = 0;
R44 = 1;

end;

% Check Blanchard & Kahn conditions
check;