Announcement

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

  • Estimating pre-ranking betas from rolling regressions

    Dear all,

    I just started to use Stata, so I don't have much knowledge about it. I need to calculate the capm beta (market beta) for 524 different stock over a time period from 31.12.1991 to 30.12.2016.
    I have the daily stock and market excess returns xdr_ and xdrm_ for each date in the long format, as follows:

    In the first colum: variable date from 31.12.1991 to 30.12.2016
    In the second colum: Stock ids from 1 to 524
    And in the next colums the exess returns.

    I want to estimate the betas with a rolling regression of exess returns on market excess returns. The general formula for beta is given by beta=Corri,m(sd(i)/sd(m)) where sd(i) and sd(m) are the estimated volatilities for the stock and the market and corri,m is their correlation. I am looking for monthly betas as I need to rebalance later portfolios by the change of betas, that is why I set the window as 31.

    As I was looking for solutions in Stata help and Statalist forums I found this code:

    cd "\\cfs-student.student.uni-augsburg.de\homes\Eigene Dateien\International Finance and Banking"
    tsset stockss date
    foreach s of varlist stockss {
    rolling _b, window(31) saving(betas, replace): regress xdr_ xdrm_ if stockss == `s'
    }
    The problem is that our varibales include a lot of missing data, with which rolling is not working, so we converted all missing data to 0s. Now we are afraid to generate a big bias. Furthermore, we are not quite sure, if this formula is correct.

    Could you help me with myproplem and maybe suggest codes which could calculate the beta I am searching for? Or do Ihave to calculate the sd and corr sperately and calculate the beta with these values? If so, could you also help me with that? I tried long time to solve this problem, but I didnt succeed.

    Thank you so so much for your help in advance.

  • #2
    The problem is that our varibales include a lot of missing data, with which rolling is not working, so we converted all missing data to 0s. Now we are afraid to generate a big bias. Furthermore, we are not quite sure, if this formula is correct.
    Converting missing values to zero arbitrarily is a guaranteed recipe not just for bias but for garbage.

    In what sense is -rolling- "not working?" Are you getting error messages? If so, what do they say? Is -rolling- executing without error messages but producing unexpected results? In order to get help, you need at a minimum to show us what Stata is doing by pasting the Results window output (including the code that you used) into a code block (see FAQ #12) here on the forum. It would probably also be helpful to post an example of your data. (Use -dataex- to post the example data. FAQ #12 has information on how to get and use -dataex- as well.)

    Comment


    • #3
      Thank you for your fast answer.
      The problem is, that if I don't change the missing values to 0, the regression does start, but it only generates red "x", which I guess stands for non-calculated value. After that we started the regression with 0 instead of missing values, and now the rolling regression is generating black dots, meaning it generated values.
      The other general problem is that the regression takes too much time. We habe the 25 years daily dates and the 524 stocks and to generate the betas for one stock (with the wrongly used 0s) takes about 3 hours, so it would Stata weeks to finish the full rolling regression.

      Here is a small part of our data:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float date long stock float(dr_ d_rm_msci rf xdr_ xdrm smb hml mom liq)
      11687  1          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687  2    .006494 .00482 .018     -.011506 -.01318 .55 .12 -.69 -.05606072
      11687  3          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687  4    .038462 .00482 .018      .020462 -.01318 .55 .12 -.69 -.05606072
      11687  5          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687  6    .003649 .00482 .018     -.014351 -.01318 .55 .12 -.69 -.05606072
      11687  7          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687  8   -.004908 .00482 .018     -.022908 -.01318 .55 .12 -.69 -.05606072
      11687  9    .007042 .00482 .018     -.010958 -.01318 .55 .12 -.69 -.05606072
      11687 10   -.022058 .00482 .018     -.040058 -.01318 .55 .12 -.69 -.05606072
      11687 11    .003086 .00482 .018     -.014914 -.01318 .55 .12 -.69 -.05606072
      11687 12    -.02439 .00482 .018      -.04239 -.01318 .55 .12 -.69 -.05606072
      11687 13   -.002833 .00482 .018     -.020833 -.01318 .55 .12 -.69 -.05606072
      11687 14    -.01087 .00482 .018      -.02887 -.01318 .55 .12 -.69 -.05606072
      11687 15    .011086 .00482 .018  -.006913999 -.01318 .55 .12 -.69 -.05606072
      11687 16          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 17          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 18   -.021459 .00482 .018     -.039459 -.01318 .55 .12 -.69 -.05606072
      11687 19    .036585 .00482 .018      .018585 -.01318 .55 .12 -.69 -.05606072
      11687 20 .017582001 .00482 .018 -.0004179981 -.01318 .55 .12 -.69 -.05606072
      11687 21          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 22    .003883 .00482 .018     -.014117 -.01318 .55 .12 -.69 -.05606072
      11687 23          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 24    .019417 .00482 .018      .001417 -.01318 .55 .12 -.69 -.05606072
      11687 25   -.007042 .00482 .018     -.025042 -.01318 .55 .12 -.69 -.05606072
      11687 26          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 27   -.033898 .00482 .018     -.051898 -.01318 .55 .12 -.69 -.05606072
      11687 28          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 29    .007353 .00482 .018     -.010647 -.01318 .55 .12 -.69 -.05606072
      11687 30    .004107 .00482 .018     -.013893 -.01318 .55 .12 -.69 -.05606072
      11687 31    .009091 .00482 .018  -.008908999 -.01318 .55 .12 -.69 -.05606072
      11687 32    .020882 .00482 .018   .002882002 -.01318 .55 .12 -.69 -.05606072
      11687 33          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 34          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 35    .050725 .00482 .018   .032725003 -.01318 .55 .12 -.69 -.05606072
      11687 36          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 37          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 38    .014019 .00482 .018  -.003980999 -.01318 .55 .12 -.69 -.05606072
      11687 39    .004098 .00482 .018     -.013902 -.01318 .55 .12 -.69 -.05606072
      11687 40          0 .00482 .018        -.018 -.01318 .55 .12 -.69 -.05606072
      11687 41          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 42    .015874 .00482 .018  -.002125999 -.01318 .55 .12 -.69 -.05606072
      11687 43    .019031 .00482 .018  .0010310002 -.01318 .55 .12 -.69 -.05606072
      11687 44    .001333 .00482 .018     -.016667 -.01318 .55 .12 -.69 -.05606072
      11687 45    .023256 .00482 .018   .005255999 -.01318 .55 .12 -.69 -.05606072
      11687 46          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 47    .015385 .00482 .018     -.002615 -.01318 .55 .12 -.69 -.05606072
      11687 48    .057778 .00482 .018      .039778 -.01318 .55 .12 -.69 -.05606072
      11687 49          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 50          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 51          . .00482 .018            . -.01318 .55 .12 -.69 -.05606072
      11687 52     .04918 .00482 .018       .03118 -.01318 .55 .12 -.69 -.05606072
      11687 53   -.006277 .00482 .018     -.024277 -.01318 .55 .12 -.69 -.05606072
      11687 54      -.025 .00482 .018        -.043 -.01318 .55 .12 -.69 -.05606072
      11687 55    .002128 .00482 .018     -.015872 -.01318 .55 .12 -.69 -.05606072
      11687 56    .018587 .00482 .018  .0005870014 -.01318 .55 .12 -.69 -.05606072
      11687 57    .011742 .00482 .018  -.006257999 -.01318 .55 .12 -.69 -.05606072
      11687 58    .023622 .00482 .018   .005622001 -.01318 .55 .12 -.69 -.05606072
      11687 59    .012153 .00482 .018     -.005847 -.01318 .55 .12 -.69 -.05606072
      11687 60    .024391 .00482 .018   .006391002 -.01318 .55 .12 -.69 -.05606072
      end
      format %td date
      label values stock stockss
      label def stockss 1 "100", modify
      label def stockss 2 "101", modify
      label def stockss 3 "102", modify
      label def stockss 4 "103", modify
      label def stockss 5 "104", modify
      label def stockss 6 "105", modify
      label def stockss 7 "106", modify
      label def stockss 8 "107", modify
      label def stockss 9 "108", modify
      label def stockss 10 "109", modify
      label def stockss 11 "10_", modify
      label def stockss 12 "110", modify
      label def stockss 13 "111", modify
      label def stockss 14 "112", modify
      label def stockss 15 "113", modify
      label def stockss 16 "114", modify
      label def stockss 17 "115", modify
      label def stockss 18 "116", modify
      label def stockss 19 "117", modify
      label def stockss 20 "118", modify
      label def stockss 21 "119", modify
      label def stockss 22 "11_", modify
      label def stockss 23 "120", modify
      label def stockss 24 "121", modify
      label def stockss 25 "122", modify
      label def stockss 26 "123", modify
      label def stockss 27 "124", modify
      label def stockss 28 "125", modify
      label def stockss 29 "126", modify
      label def stockss 30 "127", modify
      label def stockss 31 "128", modify
      label def stockss 32 "129", modify
      label def stockss 33 "12_", modify
      label def stockss 34 "130", modify
      label def stockss 35 "131", modify
      label def stockss 36 "132", modify
      label def stockss 37 "133", modify
      label def stockss 38 "134", modify
      label def stockss 39 "135", modify
      label def stockss 40 "136", modify
      label def stockss 41 "137", modify
      label def stockss 42 "138", modify
      label def stockss 43 "139", modify
      label def stockss 44 "13_", modify
      label def stockss 45 "140", modify
      label def stockss 46 "141", modify
      label def stockss 47 "142", modify
      label def stockss 48 "143", modify
      label def stockss 49 "144", modify
      label def stockss 50 "145", modify
      label def stockss 51 "146", modify
      label def stockss 52 "147", modify
      label def stockss 53 "148", modify
      label def stockss 54 "149", modify
      label def stockss 55 "14_", modify
      label def stockss 56 "150", modify
      label def stockss 57 "151", modify
      label def stockss 58 "152", modify
      label def stockss 59 "153", modify
      label def stockss 60 "154", modify
      This is the code we enter on stata and the result we get. The red x are all over the sample.

      cd "\\cfs-student.student.uni-augsburg.de\homes\Eigene Dateien\International Finance and Banking"
      \\cfs-student.student.uni-augsburg.de\homes\Eigene Dateien\International Finance and Banking

      . tsset stock date
      panel variable: stock (unbalanced)
      time variable: date, 31dec1991 to 30dec2016
      delta: 1 day

      . foreach s of varlist stock {
      2. rolling _b, window(31) saving(betas, replace): regress xdr_ xdrm if sto
      > ck == `s'
      3. }
      (running regress on estimation sample)

      -> stock = 1

      Rolling replications (9102)
      1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5
      xxxxxxxxxxxx

      Can you help us with the code so that it runs faster? And What should we do with the missing values? Do you think the code is approriate for our purpose?

      Thank you very very much.

      Comment


      • #4
        First, -rolling- implicitly loops over values of the panel variable. So replace your entire -foreach- loop with just:

        Code:
        rolling _b, windows(31) saving(betas, replace): regress xdr_ xdrm
        No loop, no -if- on the regress. -rolling- handles that automatically. This alone will materially speed things up because you won't be repeating the same calculations many times!

        If that's still too slow, search this forum for other posts on rolling regressions by Robert Picard. His -rangestat- command is capable of doing this with blinding speed, but it requires some additional programming. He has illustrated it several times on this forum and you can modify his code to apply to your data.

        Comment


        • #5
          Thank you again for your reply. It makes sense that this regression will speed things up, I tried it out.
          However, I still have the problem of the missing values. If I dont generate 0s, the rolling regression only generates red x, meaning no values. Do you have an idea how I could pre-treat my missing data to get values?

          Comment


          • #6
            Zero and missing values have quite different meanings. In fact, one should never replace missing value with zero for regression purposes. The fact that you get xxxx is that your data has enough missing values which do not allow the regression to be estimated. Wait a minute! The data you have posed in the example will hardly support the code you have posted. Your code calls for varlist stocks, however, there is no variable with this name. There is a variable stock, which assumes values from 1 to 60. So the code you have posted should never work. Further, if you apply Clyde's code on your example data, it should generate all missing values because there are not enough observations per stock to justify regression.
            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