Page 1 of 1

loop inside a loop

Posted: Sun Nov 04, 2012 10:19 pm
by xswz
Hello Tom,

I posted a question about reading series from excel using loops in VAR forum session and thanks for your help. I want log-difference for each series, I have so far successfully read series in logs by do loop, but I just could not do difference, because RATS wont recognize
"set statepermits(count) = log(i{0})-log(i{0}){1}" I also tried other methods, but always gives error...please help.

declare vector[series] statepermits(51)
smpl 1975:1 2007:4
compute count=0
dofor i = AK to WY
compute count=count+1
set statepermits(count) = log(i{0}) * I want do first difference after taking logs
*stats statepermits(count)
end dofor i


Another question is that I want to compute IRFs using loop inside a loop. I have a [rect] B(i) and D(i)for each state i, and a horizon j of 24 periods. I want to compute B(i)^j * D(i) over all states and over all horizon. I have a problem defining the format of newly generated matrix..

My code which does state (2) only, works fine.

declare rect zero
compute zero=%zeros(4,2)
compute k1=B11(2)~B12(2)
compute k2=zero~B22
compute B2=k1~~k2

compute d1=D11(2)~D12(2)
compute d2=zero~D22
compute D0=d1~~d2

declare vect[rect] D(24)
compute total=%zeros(6,6)

do i=1,24
compute D(i)=B2^i*D0
compute total=total+D(i)
compute irf(i) =D(i)(1,3)
end do i

display irf %dims(irf) %maxvalue(irf) %avg(irf)


when I have tried using loop inside a loop....

declare vect[rect] k1(51) B2(51) d1(51) D0(i)
do i=1,51
compute k1(i)=B11(i)~B12(i)
compute k2=zero~B22
compute B2(i)=k1(i)~~k2

compute d1(i)=D11(i)~D12(i)
compute d2=zero~D22
compute D0(i)=d1(i)~~d2 **********so far I think it is fine

declare vect[vect[rect]] D(51,24) ******start to be a mess..
compute total=%zeros(6,6)
do j=1,24
compute D(i,j)=B2(i)^j*D0(i)
compute total=total+D(i,j)
end do j
end do i

Re: loop inside a loop

Posted: Mon Nov 05, 2012 9:35 am
by TomDoan
xswz wrote:Hello Tom,

I posted a question about reading series from excel using loops in VAR forum session and thanks for your help. I want log-difference for each series, I have so far successfully read series in logs by do loop, but I just could not do difference, because RATS wont recognize
"set statepermits(count) = log(i{0})-log(i{0}){1}" I also tried other methods, but always gives error...please help.
i{0} is for the current value of series <<i>> (referenced indirectly); for the lag, you just need i{1}. So you want

set statepermits(count) = log(i{0})-log(i{1})
xswz wrote: Another question is that I want to compute IRFs using loop inside a loop. I have a [rect] B(i) and D(i)for each state i, and a horizon j of 24 periods. I want to compute B(i)^j * D(i) over all states and over all horizon. I have a problem defining the format of newly generated matrix..

My code which does state (2) only, works fine.

declare rect zero
compute zero=%zeros(4,2)
compute k1=B11(2)~B12(2)
compute k2=zero~B22
compute B2=k1~~k2

compute d1=D11(2)~D12(2)
compute d2=zero~D22
compute D0=d1~~d2

declare vect[rect] D(24)
compute total=%zeros(6,6)

do i=1,24
compute D(i)=B2^i*D0
compute total=total+D(i)
compute irf(i) =D(i)(1,3)
end do i

display irf %dims(irf) %maxvalue(irf) %avg(irf)


when I have tried using loop inside a loop....

declare vect[rect] k1(51) B2(51) d1(51) D0(i)
do i=1,51
compute k1(i)=B11(i)~B12(i)
compute k2=zero~B22
compute B2(i)=k1(i)~~k2

compute d1(i)=D11(i)~D12(i)
compute d2=zero~D22
compute D0(i)=d1(i)~~d2 **********so far I think it is fine

declare vect[vect[rect]] D(51,24) ******start to be a mess..
compute total=%zeros(6,6)
do j=1,24
compute D(i,j)=B2(i)^j*D0(i)
compute total=total+D(i,j)
end do j
end do i
What is D supposed to be? A VECT[VECT[RECT]] should be one-dimensional on the outside, followed by another one-dimensional aggregation of two-dimensional arrays. Is the outer one regimes, followed by horizons, followed by variables x variables? It looks like you're trying to do it as a RECT[RECT] instead (which would also work under the circumstances).