Announcement

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

  • Creating a variable that measures volatility of another variable

    Hi all,

    I am writing my Bachelor thesis which is about how total payout policy (dividends + share repurchases) is affected during a crisis period in which I will be comparing the Financial Crisis to the COVID crisis. Sample includes all firms in the economy excluding some based on their SIC codes (utilities & financial companies).

    I am trying to create a variable which measures the volatility (the standard deviation) of Cash Flow and Percentage Returns in the last 2 years, at each observation, for each company in the data set.

    OIBD is Operating Income Before Depreciation, taken as Cash Flow. For example the data looks like the following:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str6 datacqtr str70 Company float PercentageReturns double OIBD
    "2000Q3" "AAR CORP"                              . 14.565
    "2000Q4" "AAR CORP"                     -.07777778 15.974
    "2001Q1" "AAR CORP"                       .3108434 17.247
    "2001Q2" "AAR CORP"                     .029411735 16.581
    "2001Q3" "AAR CORP"                      .21857142 10.469
    "2001Q4" "AAR CORP"                      -.5222743  5.513
    "2002Q1" "AAR CORP"                      -.0920245  5.722
    "2002Q2" "AAR CORP"                      .54594594  5.503
    "2002Q3" "AAR CORP"                       -.458042  3.967
    "2002Q4" "AAR CORP"                      -.1129032 10.646
    "2003Q1" "AAR CORP"                      -.1909091 12.037
    "2003Q2" "AAR CORP"                     .011235998  4.095
    "2003Q3" "AAR CORP"                            .78      .
    "2003Q4" "AAR CORP"                       .3058676      .
    "2004Q1" "AAR CORP"                      .23518164      .
    "2004Q2" "AAR CORP"                     -.25851393      .
    "2004Q3" "AAR CORP"                      .11691024  13.27
    "2004Q4" "AAR CORP"                      .27757013 15.754
    "2005Q1" "AAR CORP"                     -.18434528  16.36
    "2005Q2" "AAR CORP"                       .4385651  16.39
    "2005Q3" "AAR CORP"                       .0972568 16.872
    "2005Q4" "AAR CORP"                       .1897727 22.416
    "2006Q1" "AAR CORP"                      .20869146 25.406
    "2006Q2" "AAR CORP"                     -.04859737 28.198
    "2000Q2" "ADC TELECOMMUNICATIONS INC"            .  216.5
    "2000Q3" "ADC TELECOMMUNICATIONS INC"     -.490313  211.7
    "2000Q4" "ADC TELECOMMUNICATIONS INC"    -.3187135  119.2
    "2001Q1" "ADC TELECOMMUNICATIONS INC"    -.4842918 -112.6
    "2001Q2" "ADC TELECOMMUNICATIONS INC"    -.3462051  -18.1
    "2001Q3" "ADC TELECOMMUNICATIONS INC"  -.073319726  -44.1
    "2001Q4" "ADC TELECOMMUNICATIONS INC"    .07252742  -32.8
    "2002Q1" "ADC TELECOMMUNICATIONS INC"    -.2028689  -56.1
    "2002Q2" "ADC TELECOMMUNICATIONS INC"    -.5372751  -75.2
    "2002Q3" "ADC TELECOMMUNICATIONS INC"    -.1222222  -28.3
    "2002Q4" "ADC TELECOMMUNICATIONS INC"     .4556962      .
    "2003Q1" "ADC TELECOMMUNICATIONS INC"    .03478263      .
    "2003Q2" "ADC TELECOMMUNICATIONS INC"   -.07983198      .
    "2003Q3" "ADC TELECOMMUNICATIONS INC"    .17351595      .
    "2003Q4" "ADC TELECOMMUNICATIONS INC"     .3680934   20.1
    "2004Q1" "ADC TELECOMMUNICATIONS INC"   -.28896475   18.4
    "2004Q2" "ADC TELECOMMUNICATIONS INC"         -.04   14.6
    "2004Q3" "ADC TELECOMMUNICATIONS INC"    -.0791667   24.8
    "2004Q4" "ADC TELECOMMUNICATIONS INC"     .1628959   19.9
    "2005Q1" "ADC TELECOMMUNICATIONS INC"   -.11673149   50.6
    "2005Q2" "ADC TELECOMMUNICATIONS INC"     10.51542   48.7
    "2005Q3" "ADC TELECOMMUNICATIONS INC"    -.3324407   46.6
    "2000Q2" "ALPHARMA INC  -CL A"                   . 45.206
    "2000Q3" "ALPHARMA INC  -CL A"         -.018072288  57.76
    "2000Q4" "ALPHARMA INC  -CL A"           -.2822086 45.056
    "2001Q1" "ALPHARMA INC  -CL A"           -.2537892 64.883
    "2001Q2" "ALPHARMA INC  -CL A"          -.16768484 41.608
    "2001Q3" "ALPHARMA INC  -CL A"           .05688073 42.673
    "2001Q4" "ALPHARMA INC  -CL A"           -.0815972 14.437
    "2002Q1" "ALPHARMA INC  -CL A"           -.4593573  42.98
    "2002Q2" "ALPHARMA INC  -CL A"           .18741257 55.277
    "2002Q3" "ALPHARMA INC  -CL A"            -.434629 67.887
    "2002Q4" "ALPHARMA INC  -CL A"           .24062496 64.814
    "2003Q1" "ALPHARMA INC  -CL A"            .5054576 51.283
    "2003Q2" "ALPHARMA INC  -CL A"           .20468485 56.636
    "2003Q3" "ALPHARMA INC  -CL A"          -.13888891 41.629
    "2003Q4" "ALPHARMA INC  -CL A"           .08064514   51.2
    "2004Q1" "ALPHARMA INC  -CL A"         -.024378126 27.139
    "2004Q2" "ALPHARMA INC  -CL A"           .04436509 37.934
    "2004Q3" "ALPHARMA INC  -CL A"          -.10693358 24.741
    "2004Q4" "ALPHARMA INC  -CL A"          -.07326412 73.202
    "2005Q1" "ALPHARMA INC  -CL A"          -.27315637 41.863
    "2005Q2" "ALPHARMA INC  -CL A"             .174513  45.63
    "2005Q3" "ALPHARMA INC  -CL A"            .7187284 45.606
    "2005Q4" "ALPHARMA INC  -CL A"           .14636104  54.09
    "2000Q2" "UNITED DOMINION INDUSTRIES"            . 81.687
    "2000Q3" "UNITED DOMINION INDUSTRIES"     .3933823 72.141
    "2000Q4" "UNITED DOMINION INDUSTRIES"    -.4854881 70.606
    "2000Q2" "AMC ENTERTAINMENT INC -OLD"            . 25.335
    "2000Q3" "AMC ENTERTAINMENT INC -OLD"    -.6410257 31.057
    "2000Q4" "AMC ENTERTAINMENT INC -OLD"    1.2857143 30.602
    "2001Q1" "AMC ENTERTAINMENT INC -OLD"          .77 15.828
    "2001Q2" "AMC ENTERTAINMENT INC -OLD"     .8361582  30.46
    "2001Q3" "AMC ENTERTAINMENT INC -OLD"    -.1923077 56.678
    "2001Q4" "AMC ENTERTAINMENT INC -OLD"    .14285715 29.244
    "2002Q1" "AMC ENTERTAINMENT INC -OLD"    .13666667 37.766
    "2002Q2" "AMC ENTERTAINMENT INC -OLD"    .04105569 42.161
    "2002Q3" "AMC ENTERTAINMENT INC -OLD"    -.4788732 58.649
    "2002Q4" "AMC ENTERTAINMENT INC -OLD"    .19594593  49.05
    "2003Q1" "AMC ENTERTAINMENT INC -OLD"  -.020339025 51.148
    "2003Q2" "AMC ENTERTAINMENT INC -OLD"     .3194925 67.401
    "2003Q3" "AMC ENTERTAINMENT INC -OLD"     .1713287 55.713
    "2003Q4" "AMC ENTERTAINMENT INC -OLD"    .13507466 70.532
    "2004Q1" "AMC ENTERTAINMENT INC -OLD"   .009204468 37.528
    "2004Q2" "AMC ENTERTAINMENT INC -OLD"  .0013029067  76.58
    "2004Q3" "AMC ENTERTAINMENT INC -OLD"    .24528304 47.761
    "2000Q2" "AMERICAN AIRLINES GROUP INC"           .    811
    "2000Q3" "AMERICAN AIRLINES GROUP INC"   .23640662    879
    "2000Q4" "AMERICAN AIRLINES GROUP INC"   .19885278    393
    "2001Q1" "AMERICAN AIRLINES GROUP INC"  -.10379586    309
    "2001Q2" "AMERICAN AIRLINES GROUP INC"   .02875857    277
    "2001Q3" "AMERICAN AIRLINES GROUP INC"   -.4702464   -367
    "2001Q4" "AMERICAN AIRLINES GROUP INC"    .1650993   -675
    "2002Q1" "AMERICAN AIRLINES GROUP INC"   .18430497   -388
    "2002Q2" "AMERICAN AIRLINES GROUP INC"   -.3616055   -263
    "2002Q3" "AMERICAN AIRLINES GROUP INC"   -.7520759   -273
    end
    My struggle comes from finding the code that measures the standard deviation and how to specifically tell STATA to not calculate the standard deviation once a new company begins in the dataset.

    I would really appreciate any sort of help.

    Thank you,
    Viktor Christian


  • #2
    You should re-read the last two threads you started

    https://www.statalist.org/forums/for...e-last-5-years

    https://www.statalist.org/forums/for...s-observations

    as they touched on almost all of what you need to know.

    1. Your string date variable is not fit for purpose and needs to be converted to a numeric quarterly date variable. From previous threads you may have one already.

    2. rangestat from SSC can compute SD within a moving window.

    3. Dealing with companies separately is handled by using by(Company) as an option within a rangestat call.

    Comment


    • #3
      Nick Cox Hi, I didn't know rangestat also deals with SD.

      Is the following coding appropriate in this case?

      gen qdate = yq(FiscalY, FiscalQ)
      format qdate %tq

      rangestat (sd) OIBD (SD) OIBD, int(qdate -5 -1) by(Company)

      Comment


      • #4
        It's a good idea to look at the help files of commands you use, even if in this case reading the whole thing is really not needed.

        5 quarters is not 2 years and I doubt that you mean

        (sd) OIBD (SD) OIBD

        which at best will get the same result twice and at worst will be rejected as a syntax error. Otherwise this looks to be moving in the right direction.

        Comment


        • #5
          Thank you again for the guidance, this -rangestat- command is quite magical (for the record I put 5 just for the sake of an example, I know 5 quarters is not 2 years haha but now I've made a fool of myself).

          Finally, when I run the code:

          rangestat (sd) OIBD (SD) OIBD, int(qdate -8 -1) by(Company)

          it yields some results but it also offers the following error code:
          <istmt>: 3499 SD() not found

          Does this originate from the fact that the date variables aren't fully consistent between each company? (eg some have data for 2020 and some don't)

          Comment


          • #6
            No; this is general across Stata, which is almost always case-sensitive. rangestat supports sd but not SD.

            We (I am second author of the command) could have supported variations in spellings (here capitalization) but here we chose not to. You need to read the fine help to find out the syntax.

            There can be exceptions. A programmer can choose to support variations in syntax. I am working on something in which there is a documented varlabels option and an undocumented variablelabels option. It would be reasonable for an experienced user to guess either way or to use the command intermittently and only half-remember the syntax. Also, that could be me, using my own command months or years after I wrote it.

            I once sat by a very experienced Stata programmer who just wrote a new command on the fly because he couldn't be bothered to search for an existing command and knew that if one existed he would have to look at the help to find out the syntax, which he would probably dislike, and it was a lot easier to write a new one. That session he wrote about ten new commands and threw them all away at the end. But they were all very short! It would more often be insane not to look for existing code.

            Comment

            Working...
            X