store the results in the loop

Use this forum to post questions about syntax problems or general programming issues. Questions on implementing a particular aspect of econometrics should go in "Econometrics Issues" below.
sam
Posts: 5
Joined: Thu Jan 07, 2016 3:30 pm

store the results in the loop

Unread post by sam »

Dear Tom,
I am simulating 20000 results for VIRF (with three series system). I successfully simulate the 20,000 random shocks from t distribution, but when I tried to get the simulated VIRF, instead of getting 20,000 unique VIRFs for each variance and covariance, I received 2580 VIRFs with the same value for each variance and covariance.

I tried to modify the code many times, and still cannot get it right. I think I should create a storage variable, but I don't know if it is the right thing to do. Can you help me to look at the code below? Am I missing something?

Thank you so much!

FYI,
I am studying 3 series.
The number of observation for each series is 2580.
hvec0 is a 6 by 1vector,
%%vech_a, %%vech_b are 6 by 6 matrix.
The value of shock0 is the value for the 20,000th simulated shock
Yours,
Sam.


set randshock 1 20000 = %rant(3.14)
compute nstep=1
do j = 1,20000

compute shock0 = %fill(6,1,1)*randshock(j)

do step=1,2
if step==1
compute hvec0=%%vech_a*shock0
else
compute hvec0=(%%vech_a+%%vech_b+%%vech_d)*hvec0
end do step
Set ban081 = hvec0(1,1)
Set ban082 = hvec0(2,1)
Set ban083 = hvec0(3,1)
Set ban084 = hvec0(4,1)
Set ban085 = hvec0(5,1)
Set ban086 = hvec0(6,1)
end do j
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: store the results in the loop

Unread post by TomDoan »

I assume the intention is to save a separate value for each draw into ban081,...,ban086.

outside the loop:

clear(length=20000) ban081 ban082 ban083 ban084 ban085 ban086

inside the loop

compute ban081(j)=hvec0(1,1)
compute ban082(j)=hveco(2,1)
etc.

The incorrect

set ban081 = hvec0(1,1)

sets all elements of ban081 to the one value (which is what you were seeing).
sam
Posts: 5
Joined: Thu Jan 07, 2016 3:30 pm

Re: store the results in the loop

Unread post by sam »

Dear Tom,

Thank you so much for your help, which saved my life! THANK YOU! I got the results now.
I have an extended question. "clear (length=)" will give me a "container" for storing the simulation results as a series.
However, if I want to store the results as a matrix, which command will allow me to create the "container" for a matrix?

Thank you so much!

Yours,
Sam.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: store the results in the loop

Unread post by TomDoan »

outside the loop

dec rect ban08(20000,6)

inside the loop

compute ban08(j,1)=hvec0(1,1)
compute ban08(j,2)=hvec0(2,1)
etc.

Note, it's a good idea to put the 20000 into a variable (like NDRAWS) and write everything else in terms of NDRAWS so you can change it just once.
sam
Posts: 5
Joined: Thu Jan 07, 2016 3:30 pm

Re: store the results in the loop

Unread post by sam »

Thank you so so much, Tom!!!! Happy holidays! :D :D :D
Yours,
Sam.
Post Reply