Announcement

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

  • how to calculate a 10-period standard deviation

    Hello everyone,

    I was wondering whether anyone can help me with the following. I'm trying to retrieve the standard deviation of the variable "Price" over the last 10 periods. It should be calculated on a rolling basis (10-period). I came up with the following command, but I am wondering whether it is correct:
    Code:
     rangestat (count) n=Price (sd) sd = Price, interval (N -9 0)

    Dataex code:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(Price N)
    52.22  1
    52.78  2
    53.02  3
    53.67  4
    53.67  5
    53.74  6
    53.45  7
    53.72  8
    53.39  9
    52.51 10
    52.32 11
    51.45 12
     51.6 13
    52.43 14
    52.47 15
    52.91 16
    52.07 17
    53.12 18
    52.77 19
    52.73 20
    52.09 21
    53.19 22
    53.73 23
    53.87 24
    53.85 25
    53.88 26
    54.08 27
    54.14 28
     54.5 29
     54.3 30
     54.4 31
    54.16 32
    end
    Thank you in advance!

  • #2
    Looks fine to me. As the help for rangestat (from SSC) emphasises, you can always check what you've done directly.

    For example, with your helpful dataex output,

    Code:
    . rangestat (count) n=Price (sd) sd = Price, interval (N -9 0)
    
    .
    . l in 8/10
    
         +-----------------------------+
         | Price    N    n          sd |
         |-----------------------------|
      8. | 53.72    8    8   .55797925 |
      9. | 53.39    9    9   .52314201 |
     10. | 52.51   10   10     .552249 |
         +-----------------------------+
    
    .
    . su Price in 1/8
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
           Price |          8    53.28375    .5579793      52.22      53.74
    
    .
    . su Price in 1/9
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
           Price |          9    53.29556     .523142      52.22      53.74
    
    .
    . su Price in 1/10
    
        Variable |        Obs        Mean    Std. dev.       Min        Max
    -------------+---------------------------------------------------------
           Price |         10      53.217     .552249      52.22      53.74
    Last edited by Nick Cox; 20 Oct 2021, 09:03.

    Comment


    • #3
      Looks good to me. I applied your rangestat command to your data, then used egen to calculate the standard deviation of observations 10-19. The rangestat value for observation 19 agrees with the egen value.
      Code:
      . rangestat (count) n=Price (sd) sd = Price, interval (N -9 0)
      
      . egen double example = sd(Price) if inrange(N,10,19)
      (22 missing values generated)
      
      . list if N==19
      
           +-----------------------------------------+
           | Price    N    n          sd     example |
           |-----------------------------------------|
       19. | 52.77   19   10   .53529323   .53529323 |
           +-----------------------------------------+
      That's the advantage to rangestat - it saves you having to write a loop doing egen repeatedly.

      Comment

      Working...
      X