Announcement

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

  • Calculating Standard Deviation with Rangestat

    Hey all,

    I have the following problem. I would like to calculate rolling standard deviation of the past returns of the 36 months in a panel data.I tried using he following code:
    rangestat (sd) ret, interval (datem -36 -2) by (permno)

    However, when looking at the output that Stata has generated, I can see that the standard deviation is calculated even if the number of observations equals two.

    I would like to ask for your recommendation how to indicate that I would like Stata to generate the standard deviation when the number of observations equals one.


    Thank you in advance!

    Nick Cox

  • #2
    Stata uses #observations - 1 in the denominator when calculating SD. So, when you have 1 observation the denominator becomes zero, so end of story. SD is reported as missing.

    summarize does this, and rangestat (from SSC, as you are asked to explain) follows suit.

    Except that you could use #observations in the denominator and then the SD of one value becomes identically 0 and you can convert the other SDs by the appropriate factor.


    Code:
    . sysuse auto, clear
    (1978 Automobile Data)
    
    . set obs 75
    number of observations (_N) was 74, now 75
    
    . replace foreign = 2 in L 
    (1 real change made)
    
    . label def origin 2 Extraterrestrial, modify
    
    . replace mpg = 666 in L
    (1 real change made)
    
    . su mpg in L
    
        Variable |        Obs        Mean    Std. Dev.       Min        Max
    -------------+---------------------------------------------------------
             mpg |          1         666           .        666        666
    
    . rangestat (sd) mpg , int(foreign 0 0)
    
    . tabdisp foreign, c(mpg_sd) 
    
    -----------------------------
            Car type |  sd of mpg
    -----------------+-----------
            Domestic |  4.7432972
             Foreign |  6.6111869
    Extraterrestrial |           
    -----------------------------
    
    . 
    . rangestat (count) mpg_n=mpg , int(foreign 0 0)
    
    . 
    . gen mpg_sd2 = cond(mpg_n == 1, 0, mpg_sd * sqrt(mpg_n - 1) / sqrt(mpg_n)) 
    
    . 
    . tabdisp foreign, c(mpg_sd*)  
    
    -----------------------------------------
            Car type |  sd of mpg     mpg_sd2
    -----------------+-----------------------
            Domestic |  4.7432972    4.697467
             Foreign |  6.6111869    6.459185
    Extraterrestrial |                      0
    -----------------------------------------

    Comment

    Working...
    X