Announcement

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

  • rolling window realized volatility

    Dear members,

    I am working with panel data and would like to calculate a 5-day rolling window realized volatility for each firm. Here is a simulated data for illustration:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float Date str45 firm int firm_id float Logreturn
    18536 "AAK" 1    0.0059992443
    18539 "AAK" 1  -.002986188
    18540 "AAK" 1    0.028037382
    18541 "AAK" 1   .0.0090909088
    18542 "AAK" 1   -0.029909907
    18543 "AAK" 1    0.021545317
    18546 "AAK" 1    0.002909088
    18547 "AAK" 1   -0.0090645393
    18548 "AAK" 1    0.021588003
    18536 "BBA" 2   -0.015042983
    18539 "BBA" 2    0.0061818208
    18540 "BBA" 2   -0.018070113
    18541 "BBA" 2    0.0095238099
    18542 "BBA" 2   -0.006256904
    18543 "BBA" 2   -0.018518519
    18546 "BBA" 2    0.0094339624
    18547 "BBA" 2   -0.02168224
    18548 "BBA" 2    0.0030569325
    
    end
    I tried using different codes with no luck. I keep getting errors for codes such as:

    Code:
    gen squared_return = Logreturn^2
    bysort firm_id (Date): egen realized_volatility = mean(squared_return), window(5)
    and also

    Code:
    by firm_id: gen vola5 = sqrt(rolling(sum) squared_return, window(5))
    I even tried a loop with no luck:
    Code:
    levelsof firm_id, local(firms)
    foreach firm of local firms {
    by firm_id: egen mean_squared_return = mean(squared_return)
    by firm_id: egen volat5_firm = sqrt(5 * mean_squared_return)
    replace volat5 = volat5_firm if firm_id == `firm'
    drop mean_squared_return volat5_firm
    }
    then i revered to the asrol code with no luck:
    Code:
    gen volat5 =.
    levelsof firm_id, local(firms)
    foreach firm of local firms {
    by firm_id: asrol volat5_firm = squared_return, window(5) start(Date) keep(firm_id)
    replace volat5 = volat5_firm if firm_id == `firm'
    drop volat5_firm
    }
    I then ran a simple code that worked:
    Code:
    bysort firm_id (Date): gen rvol5=sqrt(5*sum( Logreturn^2))
    but the does not produce the same realized volatility as if you would do it on excel:
    Date firm firm_id L. Return Realized Vol
    30-Sep AAK 1 0.005999244
    1-Oct AAK 1 -0.002981734
    4-Oct AAK 1 0.028037382 0.02764528
    5-Oct AAK 1 0.009090909 0.027082311
    6-Oct AAK 1 -0.029909907 0.051175779
    7-Oct AAK 1 0.021545317 0.046496322
    8-Oct AAK 1 0.002909088 0.045122254
    11-Oct AAK 1 -0.009064539 0.026717411
    12-Oct AAK 1 0.021588003 0.026756756
    30-Sep BBA 2 -0.015042983
    1-Oct BBA 2 0.006181821
    4-Oct BBA 2 -0.018070113 0.018688785
    5-Oct BBA 2 0.00952381 0.021297494
    6-Oct BBA 2 -0.006256904 0.019578963
    7-Oct BBA 2 -0.018518519 0.019880898
    8-Oct BBA 2 0.009433962 0.019814908
    11-Oct BBA 2 -0.02168224 0.024218233
    12-Oct BBA 2 0.003056933 0.023244435
    I would appreciate any guidance
    Last edited by Saad Al; 28 Jul 2023, 04:04.

  • #2
    The simplest way to calculate rolling statistics of any kind in Stata is with the -rangestat- command, by Robert Picard, Nick Cox, and Roberto Ferrer, available from SSC.

    But I can't say anything more specific to your problem. I don't know what "realized volatility" means, and Google turns up nothing by that name for me. From the code you have attempted, it looks like you have some kind of standard deviation in mind, but I'm not sure what it's the standard deviation of, and I can't tell whether you want your rolling window to start at the given observation, end there, or be centered around it, or related to the current observation in some other way. If you can't figure out how to get what you want with -rangestat-, please post back with a clear explanation of what "realized volatility" is in statistical terms and I'll try to help.

    Comment


    • #3
      My apologies for not explaining the realized volatility at the beginning. The realized volatility (RV) of an equity is nothing but the annualized standard deviation. So, for a one year (trading days) realized volatility, we begin by calculating the daily standard deviation as follows:
      Click image for larger version

Name:	sample-standard-deviation.png
Views:	1
Size:	5.6 KB
ID:	1722182




      Where X is the log return per firm and X bar is the average/mean log returns per firm. If we multiply the daily standard deviation "S " by the square root of the window (say 252 trading days), we get the realized volatility of the stock for the past 252 days.

      In my case, my window is only 5 days, so we are multiplying the standard deviation of the first five observations of each firm by the square root of 5 and should get something similar to this:
      Date firm firm_id L. Return Realized Vol
      30-Sep AAK 1 0.005999244
      1-Oct AAK 1 -0.002981734
      4-Oct AAK 1 0.028037382
      5-Oct AAK 1 0.009090909
      6-Oct AAK 1 -0.029909907 0.04726612
      7-Oct AAK 1 0.021545317 0.051277327
      8-Oct AAK 1 0.002909088 0.050440216
      11-Oct AAK 1 -0.009064539 0.043677532
      12-Oct AAK 1 0.021588003 0.048804233
      30-Sep BBA 2 -0.015042983
      1-Oct BBA 2 0.006181821
      4-Oct BBA 2 -0.018070113
      5-Oct BBA 2 0.00952381
      6-Oct BBA 2 -0.006256904 0.027588189
      7-Oct BBA 2 -0.018518519 0.029372954
      8-Oct BBA 2 0.009433962 0.031108498
      11-Oct BBA 2 -0.02168224 0.03317864
      12-Oct BBA 2 0.003056933 0.029995836
      Obviously, I won't be able to have any realized volatilities for the first 5 observations of each firm since I need the first 5 observation to get the window rolling to calculate the standard deviation and thus, the realized volatility.
      I hope this helps.
      Last edited by Saad Al; 28 Jul 2023, 11:25.

      Comment


      • #4
        I would appreciate any help in calculating the rolling window realized volatility.
        Thank you

        Comment


        • #5
          Check ssc install asrol.
          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment


          • #6
            I am not an -asrol- user, but since its author, Ataullah Shah is a professor of finance, there is a good chance that it is designed specifically to calculate financial indices like yours. That said, if it does not serve your purposes, the standard deviation over a 5 day rolling lagging-interval for lreturn can be gotten from -rangestat- with
            Code:
            rangestat (sd) lreturn, by(firm_id) interval(date -5 -1)
            That said, these results do not match your variable realizedvol, even after multiplying them by sqrt(5). So there is some difference between what you describe and what you have calculated that I cannot pin down.

            I should also point out that your date variable does not mention any year. To use -rangestat- (and I would bet to use -asrol-, too) you need a full date variable, not just day and month.

            Correction: I see what you're doing. My calculation differs from yours in two ways. You are defining your five day lagging window as including the current date and extending back 4 days. Moreover, you are counting only days that appear in your data set (presumably these are the trading days), not all calendar days. When I change that, I do match your results:
            Code:
            replace date = date + "-2022"
            gen _date = daily(date, "DMY"), after(date)
            assert missing(date) == missing(_date)
            format _date %td
            drop date
            rename _date date
            by firm_id (date), sort: gen seq = _n // CREATE A TRADING DATE SEQUENCE
            
            rangestat (sd) lreturn, by(firm_id) interval(seq -4 0)
            by firm_id (seq), sort: gen wanted = lreturn_sd*sqrt(5) if _n >= 5
            Note: To work with your date variable, I assumed that the dates referred to 2022, for demonstration purposes. In your real data you will have to supply the correct calendar year in order to avoid problems with, for example, leap years.



            Last edited by Clyde Schechter; 02 Aug 2023, 10:29.

            Comment

            Working...
            X