/** Compute variance decompositions for horizons 1 to 10 **/

vd1 = ir1.^2;

vd1 = 100*vd1./sumc(vd1');

vd2 = ir1.^2 + ir2.^2;

vd2 = 100*vd2./sumc(vd2');

vd3 = ir1.^2 + ir2.^2 + ir3.^2;

vd3 = 100*vd3./sumc(vd3');

vd4 = ir1.^2 + ir2.^2 + ir3.^2 + ir4.^2;

vd4 = 100*vd4./sumc(vd4');

vd5 = ir1.^2 + ir2.^2 + ir3.^2 + ir4.^2 + ir5.^2;

vd5 = 100*vd5./sumc(vd5');

vd6 = ir1.^2 + ir2.^2 + ir3.^2 + ir4.^2 + ir5.^2 + ir6.^2;

vd6 = 100*vd6./sumc(vd6');

vd7 = ir1.^2 + ir2.^2 + ir3.^2 + ir4.^2 + ir5.^2 + ir6.^2 + ir7.^2;

vd7 = 100*vd7./sumc(vd7');

vd8 = ir1.^2 + ir2.^2 + ir3.^2 + ir4.^2 + ir5.^2 + ir6.^2 + ir7.^2 + ir8.^2;

vd8 = 100*vd8./sumc(vd8');

vd9 = ir1.^2 + ir2.^2 + ir3.^2 + ir4.^2 + ir5.^2 + ir6.^2 + ir7.^2 + ir8.^2 + ir9.^2;

vd9 = 100*vd9./sumc(vd9');

vd10 = ir1.^2 + ir2.^2 + ir3.^2 + ir4.^2 + ir5.^2 + ir6.^2 + ir7.^2 + ir8.^2 + ir9.^2 + ir10.^2;

vd10 = 100*vd10./sumc(vd10');

------------------------------------------------------------------------------------------------------------------

then I want to sum

sumc(diag(vd1*vd1') to sumc(diag(vd10*vd10')

so I use do until.

tr=0;

i=1;

do until i>10;

tr = tr + sumc(diag(vd[i]*vd[i]'));

i=i+1;

endo;

but error occur...

please help me.

## 5 Answers

0

You are getting an error, because inside of your `do loop` you are trying to index into a matrix named `vd`. However, that matrix does not exist. There is a way to loop through global variables like you are wanting to do, but I think we can make the code much shorter and simpler if we know a few things about the data. Do all of the `ir` matrices have the same dimensions? What are they?

The simplest, though not most elegant, solution is to simply replace your `do loop` with this:

tr=0; i=1; do until i>10; tmp = varget("vd"$+ntos(i)); tr = tr + sumc(diag(tmp*tmp')); i=i+1; endo;

One step better would be to simplify by using a `for loop` in place of the `do loop` like this:

for i(1, 10, 1); tmp = varget("vd"$+ntos(i)); tr = tr + sumc(diag(tmp*tmp')); endfor;

But if you answer the question from above about the sizes of these variables, we can make a version that will be more elegant and you will not have to hand enter each of the terms. You will appreciate this if you change your model to include more.

0

I am user gauss version 10.

and I think the ntos(i) is for gauss version 14.

would you revise your coding for gauss 10 version?

It'd be very appreciate that.

0

I resolve the problem using cvtos(ftocv(i, 0, 0));

thnaks you.

0

and all of the `ir` matrices have the same dimensions.

please let me know how I convert my code more elegan.

0

Yes, GAUSS 10 does not have the function `ntos`. For older versions of GAUSS replace:

tmp = varget("vd"$+ntos(i));

with:

tmp = varget("vd"$+cvtos(ftocv(i, 1, 0)));

## Your Answer

## 5 Answers

You are getting an error, because inside of your `do loop` you are trying to index into a matrix named `vd`. However, that matrix does not exist. There is a way to loop through global variables like you are wanting to do, but I think we can make the code much shorter and simpler if we know a few things about the data. Do all of the `ir` matrices have the same dimensions? What are they?

The simplest, though not most elegant, solution is to simply replace your `do loop` with this:

tr=0; i=1; do until i>10; tmp = varget("vd"$+ntos(i)); tr = tr + sumc(diag(tmp*tmp')); i=i+1; endo;

One step better would be to simplify by using a `for loop` in place of the `do loop` like this:

for i(1, 10, 1); tmp = varget("vd"$+ntos(i)); tr = tr + sumc(diag(tmp*tmp')); endfor;

But if you answer the question from above about the sizes of these variables, we can make a version that will be more elegant and you will not have to hand enter each of the terms. You will appreciate this if you change your model to include more.

I am user gauss version 10.

and I think the ntos(i) is for gauss version 14.

would you revise your coding for gauss 10 version?

It'd be very appreciate that.

I resolve the problem using cvtos(ftocv(i, 0, 0));

thnaks you.

and all of the `ir` matrices have the same dimensions.

please let me know how I convert my code more elegan.

Yes, GAUSS 10 does not have the function `ntos`. For older versions of GAUSS replace:

tmp = varget("vd"$+ntos(i));

with:

tmp = varget("vd"$+cvtos(ftocv(i, 1, 0)));