Announcement

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

  • Volatility as the standard deviation of Earnings over the most recent 5 years: how to code ?

    Hi
    The firm-specific volatility of earnings,is calculated as the standard deviation of Earnings over the most recent 5 years. This should give a firm specific measure of earnings volatility.
    I think this is simple to code, but I am not sure how to construct a loop for that ?
    I would appreciate your help
    Thanks

  • #2
    Following the link to this thread from yesterday may help, especially post #5 from Robert Picard.

    Comment


    • #3
      Hi William
      I read the post you referred to. I am not sure how to use post 5 in my case.
      I have years and firms, and want to calculate volatility of earnings as described in my post. I read it again, but can't get it...
      Any help?

      Comment


      • #4
        Thanks William for the plug. On further thought, it is likely that when computing statistics on a rolling window, there may be a minimum requirement for observations. Here's an example of how to compute the standard deviation over a window of 5 years with a minimum requirement of 3 observations.

        Code:
        * ------------- data setup -------------
        set seed 1234
        clear
        
        * create firms and initial earnings
        set obs 1000
        gen firmid = _n
        gen earnings = 100 * runiform() 
        
        * for each firm, create 30 years of earnings
        expand 30
        bysort firmid: gen year = 1985 + _n
        
        * earning varies somewhat through time
        bysort firmid year: replace earnings = earnings + runiform() 
        
        * create some missing  values
        replace earnings = . if runiform() < .1
        
        * can also have some missing obs
        drop if runiform() < .1
        
        * Declare data to be panel data
        xtset firmid year
        
        * ------------- end of data setup -------------
        
        program drop _all
        program define tsrollstat
        
            version 9
            
            syntax varname, ///
                Generate(name) ///
                Window(integer) ///
                Minimum(integer) ///
                STAT(string) ///
                [ ///
                double ///
                ]
            
            local lastp = `window' - 1
        
            tsrevar L(1/`lastp').`varlist'
            local rollingvars `r(varlist)'
            
            qui egen `double' `generate' = row`stat'(`varlist' `rollingvars')
            
            tempvar n
            egen `n' = rownonmiss(`varlist' `rollingvars')
            qui replace `generate' = . if `n' < `minimum'
            
        end
        
        tsrollstat earnings, window(5) minimum(3) gen(volatility) stat(sd)

        Comment


        • #5
          Disclaimer, I know nothing about Finance and I have no idea if my previous post answers correctly the original question.

          Comment


          • #6
            Robert,
            Thanks a lot.
            I copied your tsrollstat program in a do file (or run it stata) starting from :
            program drop_all
            ....

            I then use your last line command:
            tsrollstat earnings, window(5) minimum(3) gen(volatility) stat(sd) I got the following error message: option / required r(198)

            Comment


            • #7
              No idea except that you must have missed some part when copying. I just copied from the browser the whole code segment (including the data setup) to a text file and saved it as a do-file and it runs fine on my computer.

              Comment


              • #8
                Hi again
                I did the same now as you. I ignored my dataset and used the full code you posted including the data setup. Unfortunately, I still get the same error message !
                What might be causing this?

                Comment


                • #9
                  I just copied the code segment in #4 above and pasted it directly into Stata's Command window with the result you report. Stata does not accept "///" as line continuation characters when you paste code directly into the Command window. You must paste it in a do-file and run the code from there.

                  Comment


                  • #10
                    Hello, Robert!

                    I have exactly the same issue but with a window of 10 years. Thanks heaps for the code - it works perfectly when I'm going through your example. However, when trying to replicate it to my data, it gives me an error message "time variable not set". I'm stuck ... how to deal with this?

                    Comment


                    • #11
                      The immediate answer is that you must tsset or xtset your data first.

                      Note that tsegen (SSC; and mentioned in many threads here) supersedes this program.

                      Comment


                      • #12
                        Thanks, Nick! Xtset it, all done! Enlightenment dawned Wink

                        Comment


                        • #13
                          Hello everyone (almost 9 years later), I have exactly the same question as Mike in #1 and I did everything what you guys said (e.g. using xtset) but I also still receive the error: option / required r(198);. Can someone help me with this?

                          Comment


                          • #14
                            See rangestat from SSC, which supersedes the solutions in this thread.

                            Comment

                            Working...
                            X