{"id":1046,"date":"2018-07-16T12:19:01","date_gmt":"2018-07-16T16:19:01","guid":{"rendered":"http:\/\/www.kaikaichen.com\/?p=1046"},"modified":"2018-07-20T23:29:56","modified_gmt":"2018-07-21T03:29:56","slug":"stata-commands-to-calculate-skewness","status":"publish","type":"post","link":"https:\/\/www.kaichen.work\/?p=1046","title":{"rendered":"Stata commands to calculate skewness"},"content":{"rendered":"<p>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\u00a0<code>sum var, detail<\/code>\u00a0reports.<\/p>\n<p><em><strong>Figure 1:<\/strong><\/em> Returns are stored in a row<\/p>\n<p><a href=\"http:\/\/www.kaikaichen.com\/wp-content\/uploads\/2018\/07\/ret_row.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1053 size-full\" src=\"http:\/\/www.kaikaichen.com\/wp-content\/uploads\/2018\/07\/ret_row.png\" alt=\"\" width=\"1710\" height=\"84\" srcset=\"https:\/\/www.kaichen.work\/wp-content\/uploads\/2018\/07\/ret_row.png 1710w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2018\/07\/ret_row-300x15.png 300w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2018\/07\/ret_row-768x38.png 768w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2018\/07\/ret_row-1024x50.png 1024w\" sizes=\"(max-width: 1710px) 100vw, 1710px\" \/><\/a><\/p>\n<p><em><strong>Figure 2:<\/strong><\/em> Returns are stored in a column<\/p>\n<p><a href=\"http:\/\/www.kaikaichen.com\/wp-content\/uploads\/2018\/07\/ret_column.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone wp-image-1052\" src=\"http:\/\/www.kaikaichen.com\/wp-content\/uploads\/2018\/07\/ret_column-193x300.png\" alt=\"\" width=\"103\" height=\"160\" srcset=\"https:\/\/www.kaichen.work\/wp-content\/uploads\/2018\/07\/ret_column-193x300.png 193w, https:\/\/www.kaichen.work\/wp-content\/uploads\/2018\/07\/ret_column.png 278w\" sizes=\"(max-width: 103px) 100vw, 103px\" \/><\/a><\/p>\n<p><em><strong>If returns are stored in a row<\/strong><\/em><\/p>\n<p>Stata does not provide a command to calculate the skewness in this situation.\u00a0The following Stata commands will do the job.<\/p>\n<pre class=\"lang:default decode:true \">egen ret_mean=rowmean(ret1-ret12)\r\negen n=rownonmiss(ret1-ret12)\r\n\r\nforeach v in ret1 ret2 ret3 ret4 ret5 ret6 ret7 ret8 ret9 ret10 ret11 ret12 {\r\n  ge `v'_m3=(`v'-ret_mean)^3\r\n}\r\negen m3=rowtotal(ret1_m3-ret12_m3), missing\r\nreplace m3=m3\/n\r\n\r\nforeach v in ret1 ret2 ret3 ret4 ret5 ret6 ret7 ret8 ret9 ret10 ret11 ret12 {\r\n  ge `v'_m2=(`v'-ret_mean)^2\r\n}\r\negen m2=rowtotal(ret1_m2-ret12_m2), missing\r\nreplace m2=m2\/n\r\n\r\nge ret_skew=m3*m2^(-3\/2)<\/pre>\n<p><em><strong>If<\/strong><strong> returns are stored in a column<\/strong><\/em><\/p>\n<p>Stata provides a command to calculate skewness in this situation (<code>egen<\/code>\u00a0and <code>skewness<\/code>). However, the computation is extremely slow if we have millions of observations. I would suggest calculating the skewness manually as follows:<\/p>\n<pre class=\"lang:default decode:true\">sort permno (add more variables here to identify a group)\r\n\r\nby permno: egen ret_mean=mean(ret)\r\nby permno: egen n=count(ret)\r\n\r\nge ret_m3=(ret-ret_mean)^3\r\nby permno: egen m3=total(ret_m3)\r\nreplace m3=m3\/n\r\n\r\nge ret_m2=(ret-ret_mean)^2\r\nby permno: egen m2=total(ret_m2)\r\nreplace m2=m2\/n\r\n\r\nge skewness=m3*m2^(-3\/2)<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/www.kaichen.work\/?p=1046\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1046"}],"collection":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1046"}],"version-history":[{"count":10,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1046\/revisions"}],"predecessor-version":[{"id":1068,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=\/wp\/v2\/posts\/1046\/revisions\/1068"}],"wp:attachment":[{"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1046"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1046"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kaichen.work\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1046"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}