Announcement

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

  • Daily stock return volatility

    Hi,

    I have data on daily stock prices for -240 through -40 days prior the issue date form crsp. I am trying to calculate daily stock return volatility for each firm for each issue date. I have calculated the daily returns form the prices but can't find a command that would calculate daily stock return volatility for each firm for each issue date. I also tried using the following I found in one of the forums as suggested by Clyde Schechter but the volatility column has all values missing. Can any one please help?

    format date1 %d
    xtset permno date
    // CALCULATE SOME RUNNING TOTALS OF RET AND RET^2
    by permno (date1), sort: gen sum_ret = sum(ret)
    by permno (date1): gen sum_ret_sq = sum(ret^2)
    // AND A RUNNING COUNT OF NON MISSING OBSERVATIONS
    by permno (date1): gen int n_obs = sum(!missing(ret))

    // NOW CALCULATE RUNNING STANDARD DEVIATIONS
    gen variance = (L1.sum_ret_sq - L18.sum_ret_sq)/(L1.n_obs-L18.n_obs) /// - ((L1.sum_ret - L18.sum_ret)/(L1.n_obs-L18.n_obs))^2


    My data looks like as follows:
    permco price date
    10104 15.98 25-Nov-08
    10104 16.14 26-Nov-08
    10104 16.09 28-Nov-08
    10104 15.47 1-Dec-08
    10104 15.79 2-Dec-08
    10104 16.13 3-Dec-08
    10104 15.44 4-Dec-08
    10104 16.32 5-Dec-08
    10104 17.04 8-Dec-08
    10104 16.94 9-Dec-08
    10104 17.39 10-Dec-08
    10104 16.46 11-Dec-08
    10104 16.84 12-Dec-08
    10104 16.45 15-Dec-08
    10104 17.27 16-Dec-08
    10104 16.74 17-Dec-08
    10104 16.61 18-Dec-08
    10104 17.78 19-Dec-08
    Last edited by Zainab Mehmood; 06 May 2016, 17:03. Reason: Clyde Schechter

  • #2
    I think that the reference to Clyde's code can be found here.

    So I assume that you are trying to calculate a standard deviation over a rolling window of time. For such computations, I suggest that you use either tsegen or rangestat, both from SSC. To install them, type in Stata's Command window:

    Code:
    ssc install tsegen
    ssc install rangestat
    Once installed, you can type help tsegen and help rangestat to learn more about them. For smaller windows of time, tsegen is a bit more efficient. The syntax of rangestat is more flexible and can calculate multiple statistics over the same time window. Here's an example that calculates the standard deviation of your sample data over a 6 day window using Clyde's method (adjusted to calculate the variance with n-1) as well as with tsegen and rangestat:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int permco float price str9 date
    10104 15.98 "25-Nov-08"
    10104 16.14 "26-Nov-08"
    10104 16.09 "28-Nov-08"
    10104 15.47 "1-Dec-08" 
    10104 15.79 "2-Dec-08" 
    10104 16.13 "3-Dec-08" 
    10104 15.44 "4-Dec-08" 
    10104 16.32 "5-Dec-08" 
    10104 17.04 "8-Dec-08" 
    10104 16.94 "9-Dec-08" 
    10104 17.39 "10-Dec-08"
    10104 16.46 "11-Dec-08"
    10104 16.84 "12-Dec-08"
    10104 16.45 "15-Dec-08"
    10104 17.27 "16-Dec-08"
    10104 16.74 "17-Dec-08"
    10104 16.61 "18-Dec-08"
    10104 17.78 "19-Dec-08"
    end
    
    * convert string date to Stata numeric date
    gen ndate = daily(date,"DM20Y")
    format %td ndate
    
    xtset permco ndate
    
    * use method suggested by Clyde, adjusted to divide by (n-1)
    * http://www.statalist.org/forums/forum/general-stata-discussion/general/881052-compute-standard-deviation-using-past-two-year-monthly-return-data
    by permco (ndate), sort: gen double sum_price = sum(price)
    by permco (ndate): gen double sum_price_sq = sum(price^2)
    by permco (ndate): gen int n_obs = sum(!missing(price))
    gen long n = L1.n_obs-L7.n_obs
    gen double variance = ((L1.sum_price_sq - L7.sum_price_sq) /// 
        - ((L1.sum_price - L7.sum_price))^2 / n) / (n - 1)
    gen double sd = sqrt(variance)
    
    * Best approach is to use -tsegen- (from SSC) if the window is short
    tsegen double pricesd = rowsd(L(1/6).price)
    tsegen nsd = rownonmiss(L(1/6).price)
    
    * With longer time windows, use -rangestat- (from SSC)
    rangestat (sd) price (count) price, interval(ndate -6 -1)
    
    * spot check for the 8th observation
    sum price if inrange(ndate, ndate[8]-6, ndate[8]-1)
    list price n-price_count in 8

    Comment


    • #3
      Hey guys,

      referring to Robert's advice to use -rangestat- for longer windows in #2: Is there a way to calculate the moving standard deviations in Mata (to speed things up) and then use the -rangestat- command?

      Many thanks in advance.

      Comment


      • #4
        I don't understand the question. rangestat (SSC) is using Mata to calculate rolling SD in the code you cite. I would be interested to hear if you can do it faster yourself.

        Comment


        • #5
          Okay, perfect! Didn't know that. When using -rangestat- to calculate, say, rolling linear regressions, one has to tell Mata that it should execute a regression model. So I was guessing one has to do something similar with standard deviations. But it's even better this way!

          Comment


          • #6
            In essence rangestat works in two ways: it applies existing Mata functions or it applies a supplied user-written Mata function. See Remarks in the help file and look at the code to learn more.

            Comment

            Working...
            X