Hi Statalisters,
I could use your help with the following:
I have a panel of daily return data for all CRSP firm from 2002 till 2017 and I have to calculate an individual beta for each stock on a rolling basis.
That is, for a particular firm-year, I want to regress the firm's daily stock returns from the last 3 years on the daily market returns from the last 3 years.
For example, the beta of Apple's stock beta in 2006 would be a regression of Apple's returns from 01/01/2004 to 12/31/2006 on the market returns during this period.
The beta for 2007 would then include the returns from 01/01/2005 till 12/31/2007.
(perhaps it is relevant to know that my dataset period of interest is 2006 till 2017, I added back till 2002 because this variable and a few others I have to generate require more historical data.)
I tried replicating the -rangestat- code from:
https://www.statalist.org/forums/for...tas-for-stocks
https://www.statalist.org/forums/for...ing-stock-beta
It has been calculating for a while and hasn't finished. I can imagine that it would take a while, considering I have around 8.2 million observation and 3800 unique firms (permco).
However, it'd like to know for sure that the code works before I let it spend such a long time calculating.
Current code:
One problem I think I already have with this code is the fact that I have a count of the amount of trading days for each year (trading_days) which varies between 250 and 252.
So if I am using different, rolling periods of 3 years of daily data the number in the interval also changes continuously, right?
Secondly, I read in the help -rangestat- the following: ' Since this is panel data, we use the by(company) option to restrict calculations to observations within the same company group.'
Should I also be doing this since I have panel data?
I also want to reject the results for observations that did not have 3 years of past daily data available for the beta.
I know this is a lot, but I hope I have been clear in what I am looking for and can get some much needed help. My -dataex- is below
I could use your help with the following:
I have a panel of daily return data for all CRSP firm from 2002 till 2017 and I have to calculate an individual beta for each stock on a rolling basis.
That is, for a particular firm-year, I want to regress the firm's daily stock returns from the last 3 years on the daily market returns from the last 3 years.
For example, the beta of Apple's stock beta in 2006 would be a regression of Apple's returns from 01/01/2004 to 12/31/2006 on the market returns during this period.
The beta for 2007 would then include the returns from 01/01/2005 till 12/31/2007.
(perhaps it is relevant to know that my dataset period of interest is 2006 till 2017, I added back till 2002 because this variable and a few others I have to generate require more historical data.)
I tried replicating the -rangestat- code from:
https://www.statalist.org/forums/for...tas-for-stocks
https://www.statalist.org/forums/for...ing-stock-beta
It has been calculating for a while and hasn't finished. I can imagine that it would take a while, considering I have around 8.2 million observation and 3800 unique firms (permco).
However, it'd like to know for sure that the code works before I let it spend such a long time calculating.
Current code:
Code:
rangestat (reg) exret mktrf, by(permco) interval(bdate -755 0)
So if I am using different, rolling periods of 3 years of daily data the number in the interval also changes continuously, right?
Secondly, I read in the help -rangestat- the following: ' Since this is panel data, we use the by(company) option to restrict calculations to observations within the same company group.'
Should I also be doing this since I have panel data?
I also want to reject the results for observations that did not have 3 years of past daily data available for the beta.
I know this is a lot, but I hope I have been clear in what I am looking for and can get some much needed help. My -dataex- is below
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input long permco str32 comnam float(year bdate mktrf exret smb hml rf trading_days) 7 "APPLE COMPUTER INC" 2002 0 .31 . -.79 -.04 .01 252 7 "APPLE COMPUTER INC" 2002 1 .98 1.1845564 .56 -.34 .01 252 7 "APPLE COMPUTER INC" 2002 2 .71 .4554149 .22 .52 .01 252 7 "APPLE COMPUTER INC" 2002 3 -.63 -3.401615 -.49 .98 .01 252 7 "APPLE COMPUTER INC" 2002 4 -.25 -1.284458 1.2 -.41 .01 252 7 "APPLE COMPUTER INC" 2002 5 -.41 -4.3486876 -.04 .26 .01 252 7 "APPLE COMPUTER INC" 2002 6 .03 -1.9690182 .18 -.39 .01 252 7 "APPLE COMPUTER INC" 2002 7 -.86 -.861473 -.1 .06 .01 252 7 "APPLE COMPUTER INC" 2002 8 -.81 .4639364 -.8 .25 .01 252 7 "APPLE COMPUTER INC" 2002 9 .59 2.557241 -.43 .26 .01 252 7 "APPLE COMPUTER INC" 2002 10 -1.58 -4.342128 -.26 .66 .01 252 7 "APPLE COMPUTER INC" 2002 11 .93 7.853498 .2 -.65 .01 252 7 "APPLE COMPUTER INC" 2002 12 -.99 -1.3985977 -.48 .72 .01 252 7 "APPLE COMPUTER INC" 2002 13 -.75 -1.601306 -.28 .24 .01 252 7 "APPLE COMPUTER INC" 2002 14 .87 5.343646 .62 .02 .01 252 7 "APPLE COMPUTER INC" 2002 15 .4 .8119758 .12 .14 .01 252 7 "APPLE COMPUTER INC" 2002 16 .09 .1621951 -.3 .34 .01 252 7 "APPLE COMPUTER INC" 2002 17 .02 .0759865 .06 .74 .01 252 7 "APPLE COMPUTER INC" 2002 18 -2.51 -.8731938 1.32 .1 .01 252 7 "APPLE COMPUTER INC" 2002 19 1 4.316376 -.11 -.36 .01 252 7 "APPLE COMPUTER INC" 2002 20 1.34 2.571578 -.55 -.02 .01 252 7 "APPLE COMPUTER INC" 2002 21 -.64 -1.2719727 .46 -.36 .01 252 7 "APPLE COMPUTER INC" 2002 22 -2.4 3.7685866 .22 .78 .01 252 7 "APPLE COMPUTER INC" 2002 23 -.46 .3837028 .14 -.61 .01 252 7 "APPLE COMPUTER INC" 2002 24 -.74 -3.122784 -.84 .5 .01 252 7 "APPLE COMPUTER INC" 2002 25 -.35 -1.5211614 -.81 1.02 .01 252 7 "APPLE COMPUTER INC" 2002 26 1.53 -1.1273241 -.05 .37 .01 252 7 "APPLE COMPUTER INC" 2002 27 1.38 3.867241 -.35 -.17 .01 252 7 "APPLE COMPUTER INC" 2002 28 -.31 -1.0967504 .68 -.49 .01 252 7 "APPLE COMPUTER INC" 2002 29 .95 1.196777 -.12 .01 .01 252 7 "APPLE COMPUTER INC" 2002 30 -.26 -1.6629295 -.76 .38 .01 252 7 "APPLE COMPUTER INC" 2002 31 -1.06 -2.8968015 .61 .32 .01 252 7 "APPLE COMPUTER INC" 2002 32 -1.86 -5.514393 -.12 .83 .01 252 7 "APPLE COMPUTER INC" 2002 33 1.24 2.2195933 -.04 -.47 .01 252 7 "APPLE COMPUTER INC" 2002 34 -1.43 -7.31775 -.24 .88 .01 252 7 "APPLE COMPUTER INC" 2002 35 .67 5.597254 .16 .1 .01 252 7 "APPLE COMPUTER INC" 2002 36 1.65 4.588016 -1.28 .29 .01 252 7 "APPLE COMPUTER INC" 2002 37 .12 -.5997211 .6 .06 .01 252 7 "APPLE COMPUTER INC" 2002 38 .15 -7.508585 .03 .74 .01 252 7 "APPLE COMPUTER INC" 2002 39 -.29 -1.201028 -.43 .87 .01 252 7 "APPLE COMPUTER INC" 2002 40 2.21 7.745823 -.51 -.46 .01 252 7 "APPLE COMPUTER INC" 2002 41 2.02 3.5094254 -.01 -.26 .01 252 7 "APPLE COMPUTER INC" 2002 42 -.51 -3.188855 .55 .02 .01 252 7 "APPLE COMPUTER INC" 2002 43 1.38 2.2590008 -.09 .24 .01 252 7 "APPLE COMPUTER INC" 2002 44 -.4 1.269685 .6 -.01 .01 252 7 "APPLE COMPUTER INC" 2002 45 .66 1.1319402 .47 -.53 .01 252 7 "APPLE COMPUTER INC" 2002 46 .29 1.5990436 .08 0 .01 252 7 "APPLE COMPUTER INC" 2002 47 -.29 -1.3760324 -.15 .35 .01 252 7 "APPLE COMPUTER INC" 2002 48 -.89 -.9447743 .23 .23 .01 252 7 "APPLE COMPUTER INC" 2002 49 -.06 -.25529638 .36 .04 .01 252 end format %tbtrade_day1 bdate
Comment