Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: Estimate CAPM Beta, panel dataset
From
Sheldon Marker <[email protected]>
To
[email protected]
Subject
st: Estimate CAPM Beta, panel dataset
Date
Wed, 5 Jan 2011 02:17:03 +0000 (GMT)
Dear Stata-Listers,
I'm new to the list and got kind of a beginner questions or problem I guess so
please don't mind.
I have a panel data set with time series from industry segments and I try to
estimate the CAPM beta for a further calculation. The point is that I think my
code is correct but it is way to slow, so maybe you know what I did wrong that
it slows down so much (couple of hours).
The problem is not the CAPM regression at the end, but the loops to estimate the
weighted returns.
Help is greatly appreciated.
thanks,
Sheldon
Code:
* succeeding months
g seqmonth = ym(year,month)
sort seqmonth
*Calculate monthly return of the whole market
by seqmonth: egen mreturn_monthly = sum(return)
*Calculate monthly marketcap of the whole market
by seqmonth: egen mmcap_monthly = sum(mcap)
* Stores how many industry segments are in the dataset
g count_dummies = 0
* stores the return of each industry segment
g ireturn = 0
* stores mcap of each industry segment
g imcap = 0
* stores the weighted return of each industry segment
g adj_ireturn = 0
* gives me the interval over all months
levelsof seqmonth, local(monthly)
* loop over each month
foreach y of local monthly {
* loop over each industry segment.
/*There can be up to 113 segments (different data sets), so I need to check how
many I got in this dataset. I have one dummy for each segment that is in the
specific dataset:
e.g. idummy1, idummy2,…,idummy113. [dataset a]
e.g. idummy1, idummy2,…,idummy24. [dataset b]
*/
forvalues i = 1(1)113 {
* proof if segment exists in dataset
capture confirm variable idummy`i'
* if segment exists then:
if !_rc {
* get number of segments
replace count_dummies = `i' if seqmonth == `y'
* calculate return per month, per segment
egen help1 = sum(return) if seqmonth == `y' & idummy`i' == 1
replace ireturn = help1 if seqmonth == `y' & idummy`i' == 1
drop help1
* calculate mcap per month, per segment
egen help1 = sum(mcap) if seqmonth == `y' & idummy`i' == 1
replace imcap = help1 if seqmonth == `y' & idummy`i' == 1
drop help1
* calculate weighted return per month, per segment
replace adj_ireturn = ireturn * (imcap/mmcap_monthly) if seqmonth == `y' &
idummy`i' == 1
}
else {
}
}
* replace number of segments in dataset
egen count_max = max(count_dummies)
replace count_dummies = count_max if seqmonth == `y'
drop count_max
}
* use succeeding date for panel
qui g byte notmiss = price <.
qui replace seqdate = cond(notmiss, sum(notmiss),.)
drop notmiss
* set to panel
xtset icode seqdate,daily
* calculate CAPM beta via regression, with rolling window over 12 month
rollreg mreturn_monthly L(0/1).adj_ireturn, move(260) stub(mktM)
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/