Announcement

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

  • Rolling standard deviation

    Hey guys,

    I understand that there are dozens of threads on this particular question, but I haven't found a suitable answer. The data and variables I use are conventional, so I hope your answer would help others struggling with this issue too. The panel data is from CRSP Monthly files.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double permno float(month price return)
    10026 480  19.125            .
    10026 481 18.4375  -.036609747
    10026 482  19.875    .07507603
    10026 483 15.9375   -.22078784
    10026 484  14.875   -.06899287
    10026 485  17.875    .18372114
    10026 486  14.875   -.18372114
    10026 487  14.125   -.05173567
    10026 488 12.9375    -.0878162
    10026 489 13.4375    .03791923
    10026 490 14.8125    .09742212
    10026 491 16.8125    .12665124
    10026 492 15.3125   -.09345317
    10026 493  16.125    .05170137
    10026 494 16.8125     .0417518
    10026 495    20.6    .20316844
    10026 496   23.41    .12787218
    10026 497   22.15    -.0553258
    10026 498   23.48     .0583115
    10026 499   22.55   -.04041394
    10026 500   18.75    -.1845413
    10026 501   22.14    .16619214
    10026 502   23.93    .07774702
    10026 503   24.45    .02149733
    10026 504   29.99    .20423374
    10026 505   37.01     .2103241
    10026 506   37.38    .00994773
    10026 507   38.52    .03004177
    10026 508    36.2   -.06211846
    10026 509   44.96    .21671404
    10026 510   39.95    -.1181445
    10026 511    38.1   -.04741445
    10026 512   36.85  -.033358663
    10026 513      37  .0040623355
    10026 514    33.3   -.10536054
    10026 515   35.71    .06987336
    10026 516   26.53   -.29715455
    10026 517   28.93    .08660293
    10026 518   30.32    .04692842
    10026 519   31.75    .04608518
    10026 520   31.48 -.0085403165
    10026 521    31.4  -.002544528
    10026 522   33.88    .07601702
    10026 523   35.12   .035945836
    10026 524   35.78   .018618306
    10026 525   35.69  -.002518545
    10026 526      36   .008648439
    10026 527   37.76    .04773136
    10026 528    42.1     .1087974
    10026 529   47.26    .11561653
    10026 530   45.18   -.04500972
    10026 531   37.71   -.18072923
    10026 532   39.53    .04713456
    10026 533   40.83   .032357305
    10026 534   36.73    -.1058233
    10026 535   42.98    .15714103
    10026 536   42.88  -.002329339
    10026 537   44.23    .03099774
    10026 538   47.11   .063082024
    10026 539   49.03    .03994703
    10026 540   48.25  -.016036503
    10026 541   47.21  -.021790113
    10026 542   46.83  -.008081653
    10026 543   48.95    .04427532
    10026 544   49.36   .008341007
    10026 545   52.35    .05881152
    10026 546   58.84       .11687
    10026 547   59.96    .01885576
    10026 548    57.8  -.036688894
    10026 549   56.41  -.024342317
    10026 550   60.74    .07395604
    10026 551   59.41   -.02213993
    10026 552   30.28    -.6739751
    10026 553   33.95    .11440142
    10026 554   33.59  -.010660469
    10026 555   34.25   .019458156
    10026 556   32.35   -.05707259
    10026 557   33.07   .022012545
    10026 558   30.09   -.09443362
    10026 559   31.67    .05117697
    10026 560    31.1   -.01816203
    10026 561   33.41    .07164742
    10026 562   38.28    .13607228
    10026 563    41.4    .07835338
    10026 564   41.28  -.002902826
    10026 565   39.69   -.03927886
    10026 566   39.49  -.005051714
    10026 567   38.97  -.013255367
    10026 568   39.33   .009195482
    10026 569   37.74   -.04126705
    10026 570   34.45   -.09121156
    10026 571   37.55    .08616434
    10026 572   34.82   -.07548143
    10026 573   35.62    .02271532
    10026 574   32.38   -.09536627
    10026 575   31.28  -.034562044
    10026 576   25.01    -.2237032
    10026 577   24.65   -.01449887
    10026 578   27.47     .1083176
    10026 579   28.64    .04170984
    end
    format %tmMon_CCYY month
    Correct me if I am wrong. I have calculated the log returns using this formula: by permno (month), sort: generate return=ln(price)-ln(price[_n-1])

    For each firm in a given month I need to calculate the standard deviation of the stock's past 6 and 12 months return. Thank you very much in advance!

    Cheers

  • #2
    As you say, this is a common problem with many threads and your data are of standard kind. So, answers such as https://www.statalist.org/forums/for...rolling-window should be adaptable to your case.

    Comment


    • #3
      Thanks, Nick, for your prompt reply. However, I don't think that I fully understand your answer to that person's query. Would it be possible to explain again, please? What do you think about the code below?
      rolling SD = r(sd), window(6) step(1): summarize return

      Comment


      • #4
        Hi, Fanetti, I think Nick meant (please -ssc install rangestat- first):
        Code:
        rangestat (sd) return (obs) return, interval (month -5 0) by(permno) 
        // if necessary, perform the following:
        *replace return_sd = . if return_obs < 6
        Ho-Chuan (River) Huang
        Stata 19.0, MP(4)

        Comment


        • #5
          Thanks a lot, River. Much appreciated!

          Comment


          • #6
            I haven't seen a cogent argument as to why missing values before the first period deserve to be treated differently from those that are observed in the data so I would think that if you want to disregard results with fewer than 6 periods, you should use (count) and not (obs):

            Code:
            rangestat (sd) return (count) return, interval (month -5 0) by(permno)
            replace return_sd = . if return_count < 6

            Comment


            • #7
              Fanetti - I tried using asrol command (search this forum) and I aborted that after 4 hours and no results. I used the tsegen command (written by Robert and Nick) and it generated CORRECT results in about 30 seconds with 85,000 + observations. ssc install tsegen

              Comment


              • #8
                Dear Robert, Thanks for pointing out my mistake.
                Ho-Chuan (River) Huang
                Stata 19.0, MP(4)

                Comment

                Working...
                X