Page 1 of 1

dofor loop for different subsets of a data sample

Posted: Thu Jul 12, 2018 6:41 am
by sheriared
Dear Tom

I am trying to calculate information share for spot and futures prices for different periods, each containing 500 observations. So I need to divide the data sample into 2 subsamples, and I use dofor loop. Could you please check my dofor code to see if they are doing estimation for different non-overlapping periods?

Code: Select all

dofor i=1 to 2

OPEN DATA "C:\Users\lshenb\Documents\01-Work files\Research\A-study\Bitcoin\Price discovery\15min\SBX.xlsx"
DATA(FORMAT=xlsx,ORG=COLUMNS) (i-1)*500+1 (i-1)*500+500 Spot BTC XBT
set y1 = LOG(Spot)
set y2 = LOG(BTC)
set y3 = LOG(XBT)
sstats(mean) / (y1-y2)>>mean12 (y1-y3)>>mean13
equation(coeffs=||1.0,-1.0,-mean12||) eq12
# y1 y2 constant
equation(coeffs=||1.0,-1.0,-mean13||) eq13
# y1 y3 constant
system(model=vecm)
variables y1 y2 y3
lags 1 to 2
ect eq12 eq13
end(system)
*
estimate
*
compute alphaperp=%perp(%vecmalpha)
compute psi=tr(alphaperp)
*
* Compute Choleski factorization of sigma
*
compute f=%decomp(%sigma)
*
* Compute decomposition of long-run variance
*
compute split=psi*f
compute contrib=split.^2/%dot(split,split)
*
disp "Decomposition of Long-run variance" contrib
end dofor i
And my 2nd question: how do I combine vectors in RATS?
my 3rd question: how do I suppress output being printed?

Re: dofor loop for different subsets of a data sample

Posted: Thu Jul 12, 2018 8:26 am
by TomDoan
I would probably not do it that way. The only two things that actually depend upon the sample range are the SSTATS and the ESTIMATE. Read all the data first and do the set instructions outside the loop; loop around the rest and put your range calculations (i-1)*500+1 (i-1)*500+500 onto those two instructions.

I'm not sure what you mean by "combining vectors".

Use NOPRINT option on the ESTIMATE instruction (or almost anything else that produces output).

Re: dofor loop for different subsets of a data sample

Posted: Fri Jul 13, 2018 5:46 am
by sheriared
Thank you very much Tom.

I have changed my codes but I still want my y1 y2 and y3 to change in each iteration. There is an error message ## SX16. Missing Operand or Adjacent Operators
>>>>ats(mean) (y1-y2)>><<<< when I try to run this code. Could you please offer me some help on this?

Code: Select all

OPEN DATA "C:\Users\lshenb\Documents\01-Work files\Research\A-study\Bitcoin\Price discovery\15min\SBX.xlsx"
DATA(FORMAT=xlsx,ORG=COLUMNS) 2 6882 Spot BTC XBT
*note that the first entry of Spot, BTC and XBT are empty.
dofor i= 1 to 2
LOG  Spot (i-1)*500+2 (i-1)*500+501 y1 1
LOG  BTC (i-1)*500+2 (i-1)*500+501 y2 1
LOG  XBT (i-1)*500+2 (i-1)*500+501 y3 1

sstats(mean) (y1-y2)>>mean12 (y1-y3)>>mean13

equation(coeffs=||1.0,-1.0,-mean12||) eq12
# y1 y2 constant
equation(coeffs=||1.0,-1.0,-mean13||) eq13
# y1 y3 constant
system(model=vecm)
variables y1 y2 y3
lags 1 to 2
ect eq12 eq13
end(system)
*
estimate(NOPRINT)
*
compute alphaperp=%perp(%vecmalpha)
compute psi=tr(alphaperp)
*
* Compute Choleski factorization of sigma
*
compute f=%decomp(%sigma)
*
* Compute decomposition of long-run variance
*
compute split=psi*f
compute contrib=split.^2/%dot(split,split)
display contrib
end dofor i

Re: dofor loop for different subsets of a data sample

Posted: Fri Jul 13, 2018 7:11 am
by TomDoan
You're missing the range on the SSTATS:

sstats(mean) (i-1)*500+2 (i-1)*500+501 (y1-y2)>>mean12 (y1-y3)>>mean13

You're also not including the range on the ESTIMATE. Those are the only two places where the range is needed. The DATA and the LOG instructions should be outside the loop.