Suppose we are going to calculate the skewness of 12 monthly returns. The 12 returns may be stored in a row (Figure 1) or in a column (Figure 2). This post discusses how to calculate the skewness in these two situations. Please note there are several formulae for skewness out there, which may yield different results. This post uses the formula that yields the same skewness as the Stata command sum var, detail
reports.
Figure 1: Returns are stored in a row
Figure 2: Returns are stored in a column
If returns are stored in a row
Stata does not provide a command to calculate the skewness in this situation. The following Stata commands will do the job.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
egen ret_mean=rowmean(ret1-ret12) egen n=rownonmiss(ret1-ret12) foreach v in ret1 ret2 ret3 ret4 ret5 ret6 ret7 ret8 ret9 ret10 ret11 ret12 { ge `v'_m3=(`v'-ret_mean)^3 } egen m3=rowtotal(ret1_m3-ret12_m3), missing replace m3=m3/n foreach v in ret1 ret2 ret3 ret4 ret5 ret6 ret7 ret8 ret9 ret10 ret11 ret12 { ge `v'_m2=(`v'-ret_mean)^2 } egen m2=rowtotal(ret1_m2-ret12_m2), missing replace m2=m2/n ge ret_skew=m3*m2^(-3/2) |
If returns are stored in a column
Stata provides a command to calculate skewness in this situation (egen
and skewness
). However, the computation is extremely slow if we have millions of observations. I would suggest calculating the skewness manually as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
sort permno (add more variables here to identify a group) by permno: egen ret_mean=mean(ret) by permno: egen n=count(ret) ge ret_m3=(ret-ret_mean)^3 by permno: egen m3=total(ret_m3) replace m3=m3/n ge ret_m2=(ret-ret_mean)^2 by permno: egen m2=total(ret_m2) replace m2=m2/n ge skewness=m3*m2^(-3/2) |