Announcement

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

  • Moving 12 month Standard Deviations

    Dear STATA users,

    I want to compute 12-month moving standard deviations for price returns. For example, if I am looking at the moving average, I can use this command:

    tssmooth ma moveave1 = r, window(12).


    I kindly request that someone help me replicate the same for standard deviation. Instead of computing the moving average, I am looking at the moving standard deviation. Please.




    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float r
     .069388516
      .08126382
      .27004573
      .12472465
     -.18312235
      -.3020063
      .03815682
      .11805111
     -.06872444
       -.152516
       .0386655
      .08108934
       .0379653
      .15982857
     .063930154
     -.02768949
      -.0860794
    -.009729363
      .13283129
      .09999108
     -.16191234
    end





  • #2
    You can install rangestat for the same, while the command for the 12 month rolling standard deviation would be "rangestat (count) price_return (sd) price_return , interval( Month -11 0) by (companies)", in case if you have multiple countries whose rolling standard deviation you want to calculate. The command count will give the number of observation used to calculate the standard deviation whereas sd stands for standard deviation.

    Comment


    • #3
      Thank you so much Itesh. Is there a way I can have that count constant at 12 months?

      Comment


      • #4
        Hi Itesh. A clarification on what I am looking to achieve. I start by finding sd for the first 12 observations (in 1/12) then (2/13) then (3/14) in that order.

        Comment


        • #5
          So suppose there is a dataset of 30 point what the above mentioned command will do is it, take a fixed window of 12 observation, so the first window would be 1-12, then it will move one unit up so the second window would be 2-13 and will continue till the end in similar way (19-30) . The count command lets you know the number of observation used for calculating the standard deviation, so if the count is 2, it means it has used 2 observation similarly it will show how many observation it has used, for your case values having count of 12 are relevant.

          Comment


          • #6
            It works but the count variable generated has different values.

            Comment


            • #7
              You will have incomplete windows whatever you do: if you use interval(date 0 11) you will have at least an incomplete window at the end of each panel. By all means ignore incomplete windows if you do not want to cite their results.

              This is to flag that rangestat is from SSC, which should be mentioned in this thread.

              Comment


              • #8
                Thank you so much, Nick and Itesh for your support. This worked:

                gen id = _n
                rangestat (count) r (mean) r (sd) r , interval( id -11 0)

                Comment

                Working...
                X