Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Calculating monthly skewness of a variable based on the previous 252 days

    Dear all,

    I have panel data comprising of firms and daily stock returns. The returns of the firms are called pch. The firm_month is a group(firm month) variable. I have declared my data as panel data: tsset firmn day. (firmn is just the encoded firm variable and is omitted for brevity).

    Every month, I want to calculate the skewness of the variable pch based on the previous 252 days. To give you an idea, I show below how the variable skewness want to look like. For example for the firm de0007459331 in month 102, the skewness is calculated by taking into account the pch in day 2121 and in the previous 251 days.

    After doing my research, I found that these 2 links are the closest to what I want to do, but the questions posed are not answered explicitly.
    http://www.stata.com/statalist/archi.../msg00825.html
    http://www.statalist.org/forums/foru...ndows-in-panel

    The command that I tried is:
    Code:
    rolling pch, window(252) stepsize(21): egen skewness = skew(pch)
    But apart from its efficiency, I doubt also about the syntax.

    A simple one line command that does this is welcome, because I want to understand the intuition.
    But because I have a large dataset, any answers that use Mata or rangestat and thus make the process more efficient would be more than welcome.

    Below, I show you an example of my dataset of a firm for two months (21 trading days each that may contain also missings).

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str14 firm float(day month firm_month pch skewness)
    "de0007459331" 2101 101 19253 -.0261 .04761905
    "de0007459331" 2102 101 19253      0 .04761905
    "de0007459331" 2103 101 19253      0 .04761905
    "de0007459331" 2104 101 19253 -.0327 .04761905
    "de0007459331" 2105 101 19253      0 .04761905
    "de0007459331" 2106 101 19253      0 .04761905
    "de0007459331" 2107 101 19253  .0246 .04761905
    "de0007459331" 2108 101 19253      0 .04761905
    "de0007459331" 2109 101 19253  .0285 .04761905
    "de0007459331" 2110 101 19253  .0015 .04761905
    "de0007459331" 2111 101 19253      0 .04761905
    "de0007459331" 2112 101 19253  .0204 .04761905
    "de0007459331" 2113 101 19253 -.0186 .04761905
    "de0007459331" 2114 101 19253 -.0029 .04761905
    "de0007459331" 2115 101 19253 -.0073 .04761905
    "de0007459331" 2116 101 19253 -.0441 .04761905
    "de0007459331" 2117 101 19253 -.0154 .04761905
    "de0007459331" 2118 101 19253  .0469 .04761905
    "de0007459331" 2119 101 19253  .0149 .04761905
    "de0007459331" 2120 101 19253  .0147 .04761905
    "de0007459331" 2121 101 19253      0 .04761905
    
    "de0007459331" 2122 102 19254 -.0145 .05823654
    "de0007459331" 2123 102 19254  .0221 .05823654
    "de0007459331" 2124 102 19254 -.0115 .05823654
    "de0007459331" 2125 102 19254 -.0218 .05823654
    "de0007459331" 2126 102 19254  -.003 .05823654
    "de0007459331" 2127 102 19254      0 .05823654
    "de0007459331" 2128 102 19254 -.0075 .05823654
    "de0007459331" 2129 102 19254 -.0226 .05823654
    "de0007459331" 2130 102 19254      0 .05823654
    "de0007459331" 2131 102 19254  .0231 .05823654
    "de0007459331" 2132 102 19254 -.0075 .05823654
    "de0007459331" 2133 102 19254      0 .05823654
    "de0007459331" 2134 102 19254  -.003 .05823654
    "de0007459331" 2135 102 19254      0 .05823654
    "de0007459331" 2136 102 19254  .0015 .05823654
    "de0007459331" 2137 102 19254 -.0061 .05823654
    "de0007459331" 2138 102 19254  .0076 .05823654
    "de0007459331" 2139 102 19254 -.0061 .05823654
    "de0007459331" 2140 102 19254  .0061 .05823654
    "de0007459331" 2141 102 19254      0 .05823654
    "de0007459331" 2142 102 19254 -.0061 .05823654
    end

    Thank you in advance for your help,
    Dimitris Chlorokostas

  • #2
    Using egen won't work here. Where is rolling expected to put the result, namely an entire variable each time?

    Previous posts indicated how to do this In the past, namely to call up summarize, detail but now you can just write your own Mata function to use with rangestat (SSC; Robert Picard and friends).

    Or find one somewhere. Fortunately I could find one. As certain cookery programs on television used to say, here's one I prepared earlier.

    Reference is http://www.stata-journal.com/sjpdf.h...iclenum=st0204 Make sure you cite it: it has more jokes than any other paper I know on skewness and kurtosis.

    Here is a really silly example with different variables and intervals but the principles carry over.

    Code:
    webuse grunfeld , clear 
    
    mata 
    
    mata clear
    real rowvector rsskew(real vector X) {
        real vector dev 
        dev = X :- mean(X)
        return(mean(dev:^3) / (mean(dev:^2)):^(3/2)) 
    } 
    
    end 
    
    rangestat (rsskew) invest, interval(year -9 0) by(company) 
    
    rangestat (mean) invest (median) invest (sd) invest, interval(year -9 0) by(company) 
    
    gen alt = (invest_mean - invest_median) / invest_sd
    I threw in as an extra a not very well known measure that is rediscovered intermittently. (mean - median) / sd is pretty easy to explain and think about and has the perhaps surprising property that its value falls within [-1, 1]. So, it might be a little less sensitive to outliers.

    Comment


    • #3
      Can we calculate co-skewness (each year for each ID) with the help of this command?

      Comment


      • #4
        Please look again at #2. rangestat could calculate skewness given the Mata code written to do that. In fact, in rangestat as currently available on SSC

        Code:
        *! 1.1.1                        09may2017
        skewness can be calculated without extra code.

        You need to provide extra code for co-skewness yourself, however.

        Comment

        Working...
        X