Dear All,
I want to calculate monthly alphas by running the Carhart 4-factor model against monthly excess returns of funds. I am working with an unbalanced panel as not every fund is observed monthly over the whole period (e.g. funds who don't exist anymore). I have used the following Mata code written by Abraham Wolde-Tsadick earlier in this forum however this provides me with an alpha calculated from the monthly observations of for instance 2008, 2007, 2006 for the year 2008. This alpha is included in a column for all the observations of that particular fund in 2008.
I want to calculate my alpha on a monthly basis for a rolling window of 36 months. So the alpha of December 2012 is calculated over 12/2009 - 11/2012 and alpha of January 2013 over 01/2010 - 12/2012. I have tried to change the Mata code but couldn't get what I want.
This is the Mata code written by Abraham:
Could someone please help me to change this Mata code? Or maybe can help me with a Stata code which will not take so much time (I have large data set).
Thank you so much in advance!
I want to calculate monthly alphas by running the Carhart 4-factor model against monthly excess returns of funds. I am working with an unbalanced panel as not every fund is observed monthly over the whole period (e.g. funds who don't exist anymore). I have used the following Mata code written by Abraham Wolde-Tsadick earlier in this forum however this provides me with an alpha calculated from the monthly observations of for instance 2008, 2007, 2006 for the year 2008. This alpha is included in a column for all the observations of that particular fund in 2008.
I want to calculate my alpha on a monthly basis for a rolling window of 36 months. So the alpha of December 2012 is calculated over 12/2009 - 11/2012 and alpha of January 2013 over 01/2010 - 12/2012. I have tried to change the Mata code but couldn't get what I want.
This is the Mata code written by Abraham:
Code:
gen alpha = . gen b_mktrf = . gen b_smb = . gen b_hml = . gen b_umd = . mata mata clear st_view(crsp_fundno =.,.,"crsp_fundno") st_view(r_year=.,.,"r_year") st_view(VW_ExcRet_Gr=.,.,"VW_ExcRet_Gr") st_view(mktrf=.,.,"mktrf") st_view(smb=.,.,"smb") st_view(hml=.,.,"hml") st_view(umd=.,.,"umd") st_view(alpha=.,.,"alpha") st_view(b_mktrf=.,.,"b_mktrf") st_view(b_smb=.,.,"b_smb") st_view(b_hml=.,.,"b_hml") st_view(b_umd=.,.,"b_umd") p = panelsetup(crsp_fundno,1) f or (i=1; i<=rows(p); i++) { for (o=p[i,1]; o<=p[i,2]; o++) { y = VW_ExcRet_Gr[o,1] X = (mktrf[o,1], smb[o,1], hml[o,1], umd[o,1], 1) b = . for (t=p[i,1]; t<=p[i,2]; t++) {if (t != o & crsp_fundno[o,1] == crsp_fundno[t,1] & (r_year[o,1] - r_year[t,1] <= 2) & r_year[o,1] >= r_year[t,1] ) { y = y \ VW_ExcRet_Gr[t,1] X = X \ (mktrf[t,1], smb[t,1], hml[t,1], umd[t,1], 1) } } if (rows(y)>=6) { b = invsym(cross(X,X))*cross(X,y) alpha[o,1] = b[5,1] b_mktrf[o,1] = b[1,1] b_smb[o,1] = b[2,1] b_hml[o,1] = b[3,1] b_umd[o,1] = b[4,1] } } } end
Could someone please help me to change this Mata code? Or maybe can help me with a Stata code which will not take so much time (I have large data set).
Thank you so much in advance!
Comment