Announcement

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

  • ASROL Version update: Improvements in the calculation of geometric mean and products in a rolling window and over groups in Stata

    Version 4.5.1 dated December 11, 2018, of asrol is now available on SSC (thanks to Kit Baum as usual). This version improves the calculation of geometric mean and products. A detailed blog post with relevant examples can be found on my website here. Below is an excerpt from the asrol help file concerning the new changes.


    This version of asrol improves the calculation of the product of values and the geometric mean. Since both the statistics involve the multiplication of values in a given window, the presence of missing values and zeros present a challenge to getting the desired results. Following are the defaults in asrol to deal with missing values and zeros:

    7.1 : Missing values are ignored when calculating the product or the geometric mean of values.

    7.2 : To be consistent with Stata's default for geometric mean calculations, (see ameans), the default in asrol is to ignore zeros and negative number. So
    Code:
    the geometric mean of 0,2,4,6 is 3.6342412, that is [2 * 4 * 6]^(1/3).
    And
    Code:
    the geometric mean of 0,-2,4,6 is 4.8989795,
    that is [4 * 6]^(1/2).

    7.3 : Zeros are considered when calculating the product of values. So
    Code:
    the product of 0,2,4,6 is 0


    Two variations are possible when we want to treat zeros differently. These are discussed below:

    7.4 Option ignorezero: This option can be used to ignore zeros when calculating the product of values. Therefore, when the zero is ignored,
    Code:
    the product of 0,2,4,6 is 48


    7.5 Option add(#): This option adds a constant # to each value in the range before calculating the product or the geometric mean. Once the required statistic is calculated, then the constant is subtracted back. So using option add(1), t
    Code:
    the product of 0,.2,.4,.6 is 1.6880001
    =  [1+0 * 1+.2 * 1+.4 * 1+.6] - 1
    and the
    Code:
    geometric mean is .280434
     =  [(1+0 * 1+.2 * 1+.4 * 1+.6)^(1/4)] - 1.
    The Stata's ameans command calculates three types of means, including the geometric mean. The difference between asrol' gmean function and the Stata ameans command lies in the treatment of option add(#). ameans does not subtract the constant # from the results, whereas asrol does.
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Associate Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    www.FinTechProfessor.com
    If you use MS Word, do check my asdoc program that easily sends Stata output to MS Word

  • #2
    Hi, I have just started using asrol, which is a very useful tool. I had the following question. I am using asrol to calculated stock returns compounded daily, and asrol allows me to pick a window starting before a specific observation. E.g. if the date is February 20, 2019, I can set the window to 3 and use the rolling product (which is what I need in my case) for February 18-February20. Is there any way to increase the window to after the date, so e.g. make the window February 18-February 22.
    Below is the code I am using (cusip is the company identifier, date is the trading day, ret is the daily stock return; I calculate oneplus to later use for compounding)
    sort cusip date
    by cusip: gen number=_n
    gen oneplus=1+ret
    by cusip: asrol oneplus, window(number 3) stat(product)


    Comment


    • #3
      The current version of asrol does not support a forward-looking window. I have a plan to include it in its future update. Since your data has stock returns, I assume that your data can be declared as panel data or time series data. IF so, then you can utilize the forward operator of time series/panel data. First, we shall find the returns for a window of 5 observations and then forward the values by two periods.

      Since you have not posted an example data set, I am posting first a dataset that will be used in my example code
      Code:
      clear
      input float(cusip date ret)
      1005 20211  .01271792
      1005 20212  .04037379
      1005 20213    .020725
      1005 20214  .03062747
      1005 20215   .0201812
      1005 20216  .03498518
      1005 20217  .03957188
      1005 20218 .020718904
      1005 20219 .011318724
      1005 20220  .04568697
      1005 20221 .012142092
      1005 20222  .03731414
      1005 20223 .017001197
      1005 20224  .04407655
      1005 20225 .035473723
      1005 20226 .014186862
      1005 20227 .025026076
      1005 20228 .014784273
      1005 20229  .03554296
      1005 20230 .016812455
      1005 20231  .03768982
      1005 20232  .02161423
      1005 20233 .017827658
      1005 20234  .04664527
      1005 20235  .03440519
      1005 20236  .02711067
      1005 20237 .034615275
      1005 20238  .04154405
      1005 20239 .008618532
      1005 20240 .014368876
      1005 20241 .023795227
      1005 20242 .032901995
      1005 20243  .04341258
      1005 20244 .034236886
      1005 20245  .04127531
      1005 20246 .009568072
      1005 20247  .04056266
      1005 20248  .03183281
      1005 20249 .023167025
      1005 20250  .04614547
      1005 20251  .02151072
      1005 20252  .01955298
      1005 20253  .03252285
      1005 20254  .01447975
      1005 20255 .027213907
      1005 20256  .02810522
      1005 20257  .03841366
      1005 20258  .02586935
      1005 20259 .016199369
      1005 20260  .03890731
      end
      format %td date
      
      * The new variable
      * Find 5 periods rolling window product of returns, adding the value of 1 before finding the product, the same is deducted back once the product is found.
      bys cusip : asrol ret, stat(product) add(1) wind(date 5) gen(cret)
      
      * Declare the data as panel data
      tsset cusip date
      
      * Forward the values by two periods
      gen cretm_lag = F2.cret
      
      
           +-----------------------------------------------------+
           | cusip        date        ret        cret   cretm_~g |
           |-----------------------------------------------------|
        1. |  1005   03may2015   .0127179   .01271792   .0754412 |
        2. |  1005   04may2015   .0403738   .05360518   .1083792 |
        3. |  1005   05may2015    .020725   .07544115   .1307476 |
        4. |  1005   06may2015   .0306275   .10837919   .1556101 |
        5. |  1005   07may2015   .0201812   .13074761   .1547193 |
           |-----------------------------------------------------|
        6. |  1005   08may2015   .0349852   .15561007   .1547124 |
        7. |  1005   09may2015   .0395719   .15471933    .133079 |
        8. |  1005   10may2015   .0207189   .15471244   .1614073 |
        9. |  1005   11may2015   .0113187   .13307897    .135774 |
       10. |  1005   12may2015    .045687   .16140732   .1333073 |
           +-----------------------------------------------------+
      Last edited by Attaullah Shah; 26 Feb 2019, 00:17.
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Associate Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      www.FinTechProfessor.com
      If you use MS Word, do check my asdoc program that easily sends Stata output to MS Word

      Comment

      Working...
      X