Announcement

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

  • How to construct 10 portfolios from 200 stocks

    I have 200 stock returns. I want to construct 10 portfolios, using delices sorted by the stock's volatility. Help on how to write a loop for this will highly be appreciated.

    Thank you

  • #2
    -help xtile-

    Comment


    • #3
      You can try the following
      Code:
      egen portfolios = xtile(volatility), nq(10)
      If you want to do that each year, then
      Code:
      bys year: egen portfolios = xtile(volatility), nq(10)
      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


      • #4
        I don't know what delices are. Note that Attaullah's solution requires download from SSC, i.e.

        Code:
        ssc inst egenmore

        Comment


        • #5
          Thanks for the quick response, I already checked xtile function and I have read comments on similar post in the forum. However, I can’t tell I still feel there is a mistake with the code I came up with.

          ***Firm is Firm1 to Firm200 daily stock return
          ***V is volatility of each stock hence V1 to V200
          ***I want to create 10 portfolios sorted on the volatility variable
          *** I use this code but the result I get is strange and am not sure if the portfolio is sorted by Firm1 on V1, Firm2 on V2 and so on

          ************************************************** ************************************
          sort Firm* V*
          foreach X of varlist Firm* {
          egen portfolio`X' = xtile(`X'), by (V*) nq(10)
          }

          ************************************************** ***********************************
          Thank you

          Comment


          • #6
            No, you can't do this in wide layout. The earlier responses assumed you had 200 observations each containing a firm and its volatility: that's the long layout. You have wide layout: 200 Firm variables and 200 volatility variables. So you have to go long:

            Code:
            gen long obs_no = _n
            reshape long Firm V, i(obs_no) j(_j)
            xtile decile = V, nq(10)
            More generally, most things in Stata are easier (sometimes even only possible) in long layout than wide. Having data like this arrayed as a separate variable for each firm and each volatility is a recipe for a difficult mess.

            Comment


            • #7
              The length of the data is 1000 daily data. When I try your code I get an error

              gen long obs_no = _n
              reshape long Firm1 V1, i(obs_no) j(_j)
              (note: j = 0)

              variable Firm1 already defined
              r(110);

              xtile decile = V1, nq(10)


              The data has Firm 1 to Firm 200 (returns) and then V1 to V200 (volatility of each firm)
              Date Firm 1 Firm 2 Firm 3 Firm 4 Firm 5 Firm 6
              4/01/2010 0.047164 -0.09991 0.007698 -0.03226 -0.03226 -0.00306
              5/01/2010 0.047063 -0.1054 -0.00847 -0.0435 -0.0435 -0.01777
              6/01/2010 0.042946 0.024919 0.006855 0.027033 0.027033 -0.00602
              7/01/2010 0.04271 -0.05712 0.023591 0.01817 0.01817 -0.03251
              8/01/2010 0.040565 -0.04292 -0.04417 -0.02588 -0.02588 -0.03642
              9/01/2010 0.037597 -0.00684 0.022761 -0.00361 -0.00361 -0.00493
              10/01/2010 0.03731 0.041008 -0.00167 0.013533 0.013533 0.00314
              11/01/2010 0.036982 0.037516 -0.02636 -0.01258 -0.01258 -0.03
              12/01/2010 0.036849 -0.03332 -0.01688 -0.02125 -0.02125 -0.01708
              13/01/2010 0.036478 -0.04201 0.000312 -0.02392 -0.02392 -0.01279
              14/01/2010 0.035534 -0.02668 0.000742 -0.00341 -0.00341 0.007816
              15/01/2010 0.033753 0.0432 -0.00299 -0.00527 -0.00527 0.005277
              16/01/2010 0.032578 0.023811 0.031447 0.013726 0.013726 0.009029
              17/01/2010 0.032346 -0.01103 -0.00341 0.001877 0.001877 -0.02545
              18/01/2010 0.031923 -0.04964 0.008454 0.003861 0.003861 -0.00261
              Snapshot of part of the data.
              So basically what I want to do is sort Firm1 on V1, sort Firm2 and V2 to Firm200 on V200. Then create 10 portfolios from that.

              Comment


              • #8
                Well, for starters you tried
                Code:
                reshape long Firm1 V1, i(obs_no) j(_j)
                but my advice was
                Code:
                reshape long Firm V, i(obs_no) j(_j)
                The absence of the 1's is important for this command to work properly. When working with code, you have to follow the details exactly.

                That said, now that I see something of your data, I no longer understand what you want to do. I don't understand what you mean by sorting Firm1 on V1, etc. Nor, given this layout of the data with daily information, do I understand what you mean by a portfolio based on deciles of volatility.

                By the way, in the future, it is best to post example data using the -dataex- command, which you can get by running -ssc install dataex-. It is easy to use, and the instructions are in its help file, -help dataex-. If I had understood what you are trying to do, I might have wanted to experiment with your data. But given the way it is posted, I really can't. For example, there is no way I can tell from this whether your Date variable is stored as a string or is a Stata internal format date (or, ugh!, some simple integer variable with value labels). Without knowing that, I could end up showing you code that would work in one of those situations but fail in your actual situation, thus wasting both of our times. So please use -dataex- from now on. It guarantees that anybody who wants to help you can construct a completely faithful replicate of your data with a simple copy/paste operation. (By the way, if you do post a data example in your response, please be sure to include some of the V variables as well.)

                Comment


                • #9
                  delices is a typo for deciles. I just didn't see that. I wasn't being ironic (on that occasion).

                  Comment

                  Working...
                  X