How to create time series sum of matrices using loop
Show older comments
I'm very much a beginner when it comes to matlab and have been having issues trying to write a loop for the sum of matrices. I'm trying to estimate x_{t+j} = c [I + B + ..B^j], where c is a 12x1 matrix and B is a 12x12 matrix. So essentially I want it so that at x_{0} = c [I], at x_{1} = c + Bc, at x_{2} = c + Bc +B^2c, and so on.
I've been working hours on this and have not gotten anywhere.
>> Y = [UNRATE1(2:end), Inflation2(2:end),FEDFUNDS(2:end)];
>> spec = vgxset('n',3,'nAR',4,'Constant',true);
>> [EstSpec, EstStdErrors, logL, W] = vgxvarx(spec,Y);
>> vgxdisp(EstSpec)
Model : 3-D VAR(4) with Additive Constant
Conditional mean is AR-stable and is MA-invertible
a Constant:
0.182955
0.0944877
0.162639
AR(1) Autoregression Matrix:
1.02021 -0.005937 -0.0342805
-0.0371862 1.34661 0.0482451
-0.235275 0.142802 1.39328
AR(2) Autoregression Matrix:
0.0551479 -0.00832073 0.0236625
-0.0201467 -0.344195 0.00396517
0.145578 -0.200587 -0.567446
AR(3) Autoregression Matrix:
-0.0954256 0.0108807 -0.0376391
0.00402947 -0.028162 -0.0478039
-0.0293905 0.0746669 0.110649
AR(4) Autoregression Matrix:
-0.0160549 0.0200511 0.04516
0.0414149 0.00443558 0.00522856
0.0971691 0.0160857 0.0361896
Q Innovations Covariance:
0.074462 -0.0022237 -0.0111057
-0.0022237 0.0850203 0.00722063
-0.0111057 0.00722063 0.20472
B1=[1.02021 -0.005937 -0.0342805;-0.0371862 1.34661 0.0482451; -0.235275 0.142802 1.39328];
B2=[0.0441479 -0.00832 0.0236625;-0.0201467 -0.344195 0.00396516;0.145578 -0.200587 -0.567446];
B3=[-0.0954256 0.0108807 -0.0376391;0.00402947 -0.028162 -0.0478039;-0.0293905 0.0746669 0.110649];
B4=[-0.0160549 0.0200511 0.04516; 0.0414149 0.00443558 0.00522856;0.0971691 0.0160857 0.0361896];
B = [B1 B2 B3 B4; eye(9) zeros(9,3)];
c = [0.182955;0.0944877;0.162639;0;0;0;0;0;0;0;0;0];
B
B =
Columns 1 through 8
1.0202 -0.0059 -0.0343 0.0441 -0.0083 0.0237 -0.0954 0.0109
-0.0372 1.3466 0.0482 -0.0201 -0.3442 0.0040 0.0040 -0.0282
-0.2353 0.1428 1.3933 0.1456 -0.2006 -0.5674 -0.0294 0.0747
1.0000 0 0 0 0 0 0 0
0 1.0000 0 0 0 0 0 0
0 0 1.0000 0 0 0 0 0
0 0 0 1.0000 0 0 0 0
0 0 0 0 1.0000 0 0 0
0 0 0 0 0 1.0000 0 0
0 0 0 0 0 0 1.0000 0
0 0 0 0 0 0 0 1.0000
0 0 0 0 0 0 0 0
Columns 9 through 12
-0.0376 -0.0161 0.0201 0.0452
-0.0478 0.0414 0.0044 0.0052
0.1106 0.0972 0.0161 0.0362
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
1.0000 0 0 0
t = 0:1:750;
C = cell(750,1);
C2=C;
for k = 1:750;
C{k}=B^k;
end
for j = 2:750;
C2{j}=C2{j}+C2{j-1};
end
However, the code for C2 just gives me a 750x1 with empty cells. How do I make it so that the first row of C2 is the first row of C, the second row of C2 is the first row plus the second row of C, and so on?
Answers (1)
Geoff Hayes
on 22 Feb 2017
real334 - look closely at your code
C = cell(750,1);
C2=C;
for k = 1:750;
C{k}=B^k;
end
for j = 2:750;
C2{j}=C2{j}+C2{j-1};
end
C2 is initialized as C which is an empty 750x1 cell array. Your first for loop then populates C but the second one does not make use of C at all and instead just makes use of C2
C2{j}=C2{j}+C2{j-1};
Since x_{k} = c(I + B + B^2 + ... + B^k) (using your example), then I suspect that you want to do something like
X = cell(750,1);
X{1} = (eye(12) + B)*c;
for k=2:750
X{k} = X{k-1} + (B^k)*c;
end
In the above, I'm guessing that your 1 or I is the identity matrix (so eye(12)). I haven't tested the above, but I think that it could work or at least provide a hint of how to proceed.
2 Comments
real334
on 22 Feb 2017
Geoff Hayes
on 24 Feb 2017
How is x initialized? Or are you using he first loop to calculate this new x?
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!