Announcement

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

  • Idiosyncratic Volatility - Rolling Window

    Hello everyone!

    I am trying to compute idiosyncratic volatility on a rolling basis of 24 months with monthly data. I have created a month/year variable called ymdate.

    This has been my code so far:

    bys perm : asreg Excess_USD_w MKT SMB HML, wind(ymdate 24)
    gen residuals = Excess_USD_w - _b_cons - _b_MKT*MKT - _b_SMB*SMB - _b_HML*HML
    bys perm: egen IVOL=sd (residuals)

    In the end, I want to look at each year. However, my min and max values are the same for multiple years, not just for two years as specified by my rolling window. In addition, my mean value just keep on increasing.

    Would anyone know what I have done wrong? Thank you very much in advance!


  • #2
    Why did no-one answer? I can only guess: No one understood the problem. There is no (simplified) data example to reproduce. Your code calculates a SD but you're reporting a problem that min and max value are the same. What min and max values? I read this, didn't understand it, and moved on. Someone else may know what you're implying.

    There is advice in #1 of https://www.statalist.org/forums/help#adviceextras Exactly the same question is just as difficult to understand. I am called snarky if I say these things, but you need to make the question clearer (please).

    You should also be explaining: asreg is from SSC. Some people will have read this, thought what's asreg? and bailed out.



    Comment


    • #3
      Nick Cox Thank you for your feedback. I completely understand where you are coming from. My question was not clearly formatted and not easy to understand. I apologise for that.

      I will try again:
      bys perm : asreg Excess_USD_w MKT SMB HML, wind(ymdate 24) min(24)
      gen residuals = Excess_USD_w - _b_cons - _b_MKT*MKT - _b_SMB*SMB - _b_HML*HML
      bys perm: egen IVOL=sd (residuals)

      However, for each perm, I then only have one IVOL instead of multiple since it is calculated on a rolling basis.

      Last edited by Kate Lussy; 02 May 2019, 09:46.

      Comment


      • #4
        Thanks for the courteous reply.

        The last line of your code says: for each distinct value of perm, calculate the SD of the variable residuals. There is therefore just one result for each perm (which will be repeated for all observations with the same perm).

        That's on all fours with this:

        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . egen sd = sd(mpg), by(foreign)
        
        . tabdisp foreign, c(sd)
        
        ----------------------
         Car type |         sd
        ----------+-----------
         Domestic |   4.743297
          Foreign |   6.611187
        ----------------------
        
        . tabstat mpg, by(foreign) s(sd)
        
        Summary for variables: mpg
             by categories of: foreign (Car type)
        
         foreign |        sd
        ---------+----------
        Domestic |  4.743297
         Foreign |  6.611187
        ---------+----------
           Total |  5.785503
        --------------------
        The variable sd just contains (in this example) the two standard deviations repeated again and again. Try it yourself and look at the results in the Data Editor.

        In your case, the egen statement knows nothing about rolling windows. It just acts as instructed. It doesn't inherit any information from previous commands.

        For SD in rolling windows, you need asrol or rangestat or something else. (Both programs are from SSC.)

        Comment


        • #5
          Nick Cox Thank you so much for your reply! I used asrol and it then worked. Thank you.

          If I can just add a follow-up question:

          I used
          bys perm : asreg Excess_USD_w MKT SMB HML, wind(ymdate 24) min(24) fit
          bys DSCode: asrol _residuals_IVOL, window(ymdate 24) stat(sd)

          and then I tried without min(24) but dropped the observations with <24 or missing observations:
          bys perm : asreg Excess_USD_w MKT SMB HML, wind(ymdate 24) fit
          drop if _Nobs<24
          drop if _Nobs==.
          bys DSCode: asrol _residuals, window(ymdate 24) stat(sd)

          In the first column in bold below, you can see the residuals for the first model with min(24) and in the second, you can see the ones without min(24).

          I do not understand how the values can be different if the residuals from the regression are the same, especially since I use the same asrol code.

          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str6 perm double Date float(ymdate _Nobs_IVOL) double(_residuals_IVOL sd24__residuals_IVOL) float _Nobs double(_residuals sd24__residuals)
          "13259U" 17773 583 24 -.1403431071139683 .0794470072288745 24 -.1403431071139683 .05630529367323475
          "13259U" 20605 676 24 -.03995034769645955 .08821927305506964 24 -.03995034769645955 .08821927305506964
          "13259U" 21062 691 24 -.016952248539619533 .05132739975065374 24 -.016952248539619533 .05132739975065374
          "13259U" 20055 658 24 -.027975145923394153 .04422284753576202 24 -.027975145923394153 .06084804960918507
          "13259U" 17955 589 24 -.16284934359722958 .13519852138938493 24 -.16284934359722958 .09121605721898479
          "13259U" 17987 590 24 .03113445673992493 .12793277777246342 24 .03113445673992493 .09098580667192922
          "13259U" 21364 701 24 -.03549751899229463 .04391010058380981 24 -.03549751899229463 .04391010058380981
          "13259U" 17713 581 24 .0011700504536449746 . 24 .0011700504536449746 .051216207911514045
          "13259U" 20544 674 24 -.017189055295395578 .08893939573162218 24 -.017189055295395578 .08893939573162218
          "13259U" 20088 659 24 -.05667560978987024 .04393982173139569 24 -.05667560978987024 .046161725363664606
          "13259U" 21123 693 24 -.06312729009045884 .053873742269379854 24 -.06312729009045884 .053873742269379854
          "13259U" 19782 649 24 .025366362336407447 .06195648451633075 24 .025366362336407447 .05910593353355297
          "13259U" 18017 591 24 .2069109235893152 .1364529037135318 24 .2069109235893152 .10134646028153407
          "13259U" 18109 594 24 -.07902266461773721 .13247690708903073 24 -.07902266461773721 .10807923366335198
          "13259U" 21243 697 24 .021792797487662198 .043880044737199475 24 .021792797487662198 .043880044737199475
          "13259U" 20208 663 24 .12933235648571376 .07710960653779467 24 .12933235648571376 .0682181879488554
          "13259U" 20573 675 24 -.03752806248933456 .08796515448146656 24 -.03752806248933456 .08796515448146656
          "13259U" 19754 648 24 -.06742241830117465 .08309743523092959 24 -.06742241830117465 .06021745296139699
          "13259U" 21488 705 24 -.009904806291036782 .043368855480332924 24 -.009904806291036782 .043368855480332924
          "13259U" 21396 702 24 .04278200815438645 .04443631771878796 24 .04278200815438645 .04443631771878796

          Thank you kindly in advance!
          Last edited by Kate Lussy; 02 May 2019, 11:23.

          Comment


          • #6
            That's mostly for Attaullah Shah I think. I think he might prefer that you phrase the example in terms of a dataset he can access or that you supply.

            Nevertheless I don't understand

            Code:
            drop if _residuals < 24
            I don't think you can mean what you say. If you want to ignore anything it's if the number of observations used is less than 24. Dropping _residuals < 24 looks like a way to throw most of your results away.

            Actually my guess is that you didn't type that at all. But if so, what did you type? You should copy and paste the code you really used.

            You still need to study #12 in the Statalist FAQ Advice. Code presented like this is harder to read than need be.

            Comment


            • #7
              Nick Cox You are completely right. I meant the _Nobs were dropped, not the residuals. I apologise for the misunderstanding and will take a look at #12 in the FAQ advice. Thank you!

              Comment


              • #8
                Just for reference, I have replied to your query on this thread, post 11
                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

                Working...
                X