MATLAB: VAR(1) process – In an assignment A(I) = B, the number of elements in B and I must be the same.

econometricseconomicsmatrixstatisticsStatistics and Machine Learning Toolboxvectorautoregression

I am very new to Matlab and I have to simulate the following var(1) process for 100 periods, starting from g(0) and f(0) = 0
[Circular brackets will indicate time subscript]
[g(t); f(t)] = [0.7 0.2; 0.2 0.7]*[g(t-1); f(t-1)] + [u1(t); u2(t)]
E[u1(t).u2(t)] = sigma where sigma = [1 0.8; 0.8 1] for every period I am meant to take [u1(t); u2(t)] from a RNG. [i.e. mvnrnd(0, sigma)]
I cannot seem to get the loop to work even with hours of changing (I am new to all programming).
My code so far looks like this:
A = [0.7 0.2; 0.2 0.7];
sigma = [1 0.8; 0.8 1];
mu = [0 0; 0 0];
g0 = 0;
f0 = 0;
R = mvnrnd(mu, sigma);
X = [g0; f0];
X(1) = mu + R;
for t = 1:100
X(t+1) = A*X(t) + R;
end
From here, I obtain "In an assignment A(I) = B, the number of elements in B and I must be the same." Any guidance would be appreciated.

Best Answer

James - you should format your code so that it is kept distinct from your question and surrounding text. Just highlight the code and press the {} Code button. As well, since an error is generated, you should include the line of text that this error corresponds to.
In your case, the error is telling you that there is a problem with a matrix assignment - either too many elements are being assigned, or too few. Running your code, the error is raised at the line
X(1) = mu + R;
where X is a 2x1 matrix and both mu and R are 2x2. So the error message makes sense. As X should remain as a 2x1 (given your definition above), you can probably remove this line as you have already initialized X as
X = [g0; f0];
Now look at mu - this has been defined as a 2x2 matrix, but I think that it should have been defined as a 2x1 matrix of all zeros. This would ensure that R is 2x1 as well
R = mvnrnd(mu, sigma);
which again follows your problem definition. But again from above, [g(t); f(t)] = [0.7 0.2; 0.2 0.7][g(t-1); f(t-1)] + [u1(t); u2(t)]*, which implies that R should be calculated on each iteration of the for loop. So your code could be implemented as
A = [0.7 0.2; 0.2 0.7];
sigma = [1 0.8; 0.8 1];
mu = [0 ;0];
g0 = 0;
f0 = 0;
X = [g0; f0];
for t = 1:100
R = mvnrnd(mu, sigma);
X(:,t+1) = A*X(:,t) + R;
end
Note how we access column t of X as
X(:,t)
where we use the colon to say "get all rows of X". Similarly, we set all rows of column t+1 in the left hand side of the assignment.