Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Re: RE: Avoiding loops (the while loop)


From   "Subhankar Nayak" <[email protected]>
To   <[email protected]>
Subject   st: Re: RE: Avoiding loops (the while loop)
Date   Sat, 26 Jun 2004 09:24:10 -0400

Hello Nick,

Thanks for your suggestions.

1) Yes, there are 21 variables. Sorry, it was a typo on my part.

2) I want to avoid loops because this is part of a simulation exercise, and
loops in stata (as in any other statistical package) is very slow. First, I
have to do month by month regression for 1000 months. Second, I have to
repeat this exercise for 1000 simulation rounds. Now, even with 1500MB
memory I am allocating, the whole exercise takes 8-10 hours... And I have
variations of these tests.

3) Let me try the code you are suggesting.

4) I am attaching my code below for perusal if necessary.

Thank you so much.
Subh

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

DATASET:

Variables Nobs
month  1000
ret1  1000
ret2  1000
ret3  1000
ret4  1000
ret5  1000
ret6  1000
ret7  1000
ret8  1000
ret9  1000
ret10  1000
pbeta1  1000
pbeta2  1000
pbeta3  1000
pbeta4  1000
pbeta5  1000
pbeta6  1000
pbeta7  1000
pbeta8  1000
pbeta9  1000
pbeta10  1000

Note: 1-10 denote 10 asset portfolios ("port") each month

GOAL:
1) Each month, collect the intercept and coefficient of regression of
returns ("ret") on postbetas ("pbeta")
2) Get the time-series average and t-statistic of this intercept and
coefficient


PART OF THE PROGRAM CODE:

* INITIAL DATA ANALYISIS
/* details skipped */

* RESHAPE THE DATASET
sort month
reshape long ret pbeta, i(month) j(port)
sort month port

* CROSS-SECTIONAL TEST 1: full-period cross-sectional regression
/* details skipped, works fine */

* CROSS-SECTIONAL TEST 2: stacked cross-sectional regression
/* details skipped, works fine */

* CROSS-SECTIONAL TEST 3: averaged monthly cross-sectional regressions
(Fama-French approach)
/* this is where I want to avoid the while loop, it gets very slow */

* Collect the monthly intercepts and slopes

quietly generate tslope = .
quietly generate tintcp = .
quietly summ month
local nummns = r(max)
sort month port
local imonth = 1
while `imonth' <= `nummns' {
quietly reg ret pbeta if month == `imonth'
sca e_coeff1 = _b[_con]
sca e_coeff2 = _b[pbeta]
quietly replace tintcp = e_coeff1 if month == `imonth'
quietly replace tslope = e_coeff2 if month == `imonth'
sca drop e_coeff1 e_coeff2
local imonth = `imonth' + 1
}

* Now test the time series of these intercepts and slopes

quietly replace tslope = . if port ~= 1
quietly replace tintcp = . if port ~= 1
quietly summ tintcp if tintcp ~= .
sca sl31 = r(mean)
quietly ttest tintcp = 0  if tintcp ~= .
sca ts31 = r(t)
sca pv31 = r(p)
quietly summ tslope if tslope ~= .
sca sl32 = r(mean)
quietly ttest tslope = 0  if tslope ~= .
sca ts32 = r(t)
sca pv32 = r(p)
drop tslope tintcp

/* REMAINING ANALYSIS FOLLOWS */



*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index