MATLAB Answers

HELP FOR A LOOP FOR-END: In an assignment A(:) = B, the number of elements in A and B must be the same.

1 view (last 30 days)
Uccio
Uccio on 15 Jun 2019
Answered: Geoff Hayes on 18 Jun 2019
%reduced form 18 in BULLARD 2002
T=10;
sigma=0.157;
k=0.024;
beta=0.99;
po=0.35;
phipi=1.5;
phiz=0;
z=zeros(T,1)
pi=zeros(T,1)
y=[z pi] %vettore endogene
k=[1 2; 3 4] %non conosco gli elementi della matrice k, ma so che la sua dimensione, essendo uguale a quella di B (cioè n x n) è 2,2
rn=randn(T,1)
% k*rn %NON conformabili per il prodotto
intercept=ones(T,1)
z=[intercept,rn]; %vettore intercetta + slope, dimensione 10x2
csi=zeros(2,T); %vettore dei parametri da stimare tramite RLS
B2=[sigma 1-beta*phipi; k*sigma k+beta*(sigma+phiz)] %perchè non funziona quando il commando non è dato all'inizio?
B1=1/(sigma+phiz+k*phipi)
B=B1*B2
%Initial conditions
y(1,1)=2
y(1,2)=3
csi(1,1)=1
csi(2,1)=2
r=eye(2)
for t=2:T
y(t)=B*csi(1,t-1)+(B*csi(2,t-1)*po+k)*rn(t) %ALM, RHS è conformabile
csi(:,t)=csi(:,t-1)+t^(-1)*inv(rnew)*z(t-1)*(y(t)-csi(:,t-1)'*z(t-1))' %10.11, RHS è conformabile
rnew=r+t^(-1)*(z(t-1)*z(t-1)'-r); %10.12 size 2x2
r=rnew
end
Hi everyone,
I have a problem in my code. The error message shows up when I try to launch the loop: anyone can help me?

  2 Comments

Geoff Hayes
Geoff Hayes on 15 Jun 2019
Federico - when I run your code, I observe the following error
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
for the line
B2=[sigma 1-beta*phipi; k*sigma k+beta*(sigma+phiz)]
This is because k is a 2x2 matrix. You may have intended something like
B2=[sigma 1-beta*phipi; k*sigma ; k+beta*(sigma+phiz)]
which will ensure that B2 is a 5x2 matrix. Unfortunately, the next line of code will throw an error too
Error using /
Matrix dimensions must agree.
I recommend that you use the debugger to step through the code and verify that each line of code is doing exactly what you intend.
Uccio
Uccio on 15 Jun 2019
Thank you very much Geoff,
Actually I intended B2 to be a 2x2 matrix, so that B is a 2x2 matrix as well. For some reason, I obtain the error you said when the command for B is not specified in the first lines of the code. Could you try to run the following code please?
T=10;
sigma=0.157;
k=0.024;
beta=0.99;
po=0.35;
phipi=1.5;
phiz=0; %uso la calibrazione suggerita da Bullard e da Prof. Massaro
B2=[sigma 1-beta*phipi; k*sigma k+beta*(sigma+phiz)] %perchè non funziona quando il commando non è dato all'inizio?
B1=1/(sigma+phiz+k*phipi)
B=B1*B2
z=zeros(T,1)
pi=zeros(T,1)
y=[z pi] %vettore endogene
k=[1 2; 3 4] %non conosco gli elementi della matrice k, ma so che la sua dimensione, essendo uguale a quella di B (cioè n x n) è 2,2
rn=randn(T,1)
intercept=ones(T,1)
z=[intercept,rn]; %vettore intercetta + slope, dimensione 10x2
csi=zeros(2,T); %vettore dei parametri da stimare tramite RLS
%INITIAL CONDITIONS
y(1,1)=2
y(1,2)=3
csi(1,1)=1
csi(2,1)=2
r=eye(2) %chiedere al Prof.
for t=2:T
y(t)=B*csi(1,t-1)+(B*csi(2,t-1)*po+k)*rn(t) %ALM, RHS è conformabile
csi(:,t)=csi(:,t-1)+t^(-1)*inv(rnew)*z(t-1)*(y(t)-csi(:,t-1)'*z(t-1))' %10.11, RHS è conformabile
rnew=r+t^(-1)*(z(t-1)*z(t-1)'-r); %10.12 size 2x2
r=rnew
end

Sign in to comment.

Answers (1)

Geoff Hayes
Geoff Hayes on 18 Jun 2019
Federico - the line of code that generates the error is
y(t)=B*csi(1,t-1)+(B*csi(2,t-1)*po+k)*rn(t)
because you are trying to assign a 2x2 matrix (the right-hand side) to a 1x1 scalar (the left-hand side). What are you expecting y to be? A set of 2x2 matrices or are you hoping it to be a scalar? Your subsequent line of code seems to assume that y is a scalar where you refer to y(t). You may want to reconsider the above calculation.

  0 Comments

Sign in to comment.

Sign in to answer this question.