Announcement

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

  • #91
    This code works amazingly. Due to it, i feel relieved from burden of huge manual calculations. But i think it is not doing the calculations for HML correctly. Well you may please check and advise the correction please. The calculation of HML should be based on the following logic:

    HML = (SH+BH)/2 - (SL+BL)/2

    Where,

    SH = if june_mcap_group = 1 & march_mom_group = 3
    BH = if june_mcap_group = 2 & march_mom_group = 3

    and

    SL = if june_mcap_group = 1 & march_mom_group =1
    BL = if june_mcap_group = 2 & march_mom_group = 1




    Comment


    • #92
      The code for HML you have was based on a different definition. The new definition you propose here is more easily calculated with the data reduced to one observation per date and with the weighted mean returns in wide layout:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear*
      input int stock_id str52 stock float(date mdate rt mcap bmr)
       1 "3M India Ltd."                                  15430 506            .       .  .1984127
       1 "3M India Ltd."                                  15519 509  -.016724158  321.22         .
       1 "3M India Ltd."                                  15522 510   .007189263       .         .
       1 "3M India Ltd."                                  15523 510  -.007486157       .         .
       1 "3M India Ltd."                                  15524 510     .0349062       .         .
       1 "3M India Ltd."                                  15525 510           .1       .         .
       1 "3M India Ltd."                                  15526 510  -.019106355       .         .
       2 "A B B India Ltd."                               15430 506            .       . .37593985
       2 "A B B India Ltd."                               15519 509    .06520018 1139.01         .
       2 "A B B India Ltd."                               15522 510   .011906966       .         .
       2 "A B B India Ltd."                               15523 510  -.007905871       .         .
       2 "A B B India Ltd."                               15524 510    .02891033       .         .
       2 "A B B India Ltd."                               15525 510   .006123922       .         .
       2 "A B B India Ltd."                               15526 510    .00680274       .         .
       3 "A C C Ltd."                                     15430 506            .       .  .4672897
       3 "A C C Ltd."                                     15519 509    .02914508 2714.39         .
       3 "A C C Ltd."                                     15522 510    .01825053       .         .
       3 "A C C Ltd."                                     15523 510  -.008343671       .         .
       3 "A C C Ltd."                                     15524 510  -.005297543       .         .
       3 "A C C Ltd."                                     15525 510    -.0100251       .         .
       3 "A C C Ltd."                                     15526 510  -.002848082       .         .
       4 "A D C India Communications Ltd."                15430 506            .       .   .877193
       4 "A D C India Communications Ltd."                15519 509   -.00877193   38.98         .
       4 "A D C India Communications Ltd."                15522 510   .029498525       .         .
       4 "A D C India Communications Ltd."                15523 510 -.0017192152       .         .
       4 "A D C India Communications Ltd."                15524 510   .006888616       .         .
       4 "A D C India Communications Ltd."                15525 510   .021664785       .         .
       4 "A D C India Communications Ltd."                15526 510    .02957591       .         .
       5 "A G C Networks Ltd."                            15430 506            .       . .26595744
       5 "A G C Networks Ltd."                            15519 509    .02118376  233.32         .
       5 "A G C Networks Ltd."                            15522 510    -.0234289       .         .
       5 "A G C Networks Ltd."                            15523 510   .013744826       .         .
       5 "A G C Networks Ltd."                            15524 510 -.0003697619       .         .
       5 "A G C Networks Ltd."                            15525 510 -.0027127154       .         .
       5 "A G C Networks Ltd."                            15526 510   .031404562       .         .
       6 "Aarti Industries Ltd."                          15430 506            .       . 2.2727273
       6 "Aarti Industries Ltd."                          15519 509  .0036496315   80.09         .
       6 "Aarti Industries Ltd."                          15522 510    .03272724       .         .
       6 "Aarti Industries Ltd."                          15523 510   .007042247       .         .
       6 "Aarti Industries Ltd."                          15524 510            0       .         .
       6 "Aarti Industries Ltd."                          15525 510     -.013986       .         .
       6 "Aarti Industries Ltd."                          15526 510   .014184384       .         .
       7 "Aban Offshore Ltd."                             15430 506            .       .  2.564103
       7 "Aban Offshore Ltd."                             15519 509    .07301102   78.61         .
       7 "Aban Offshore Ltd."                             15522 510    .09995313       .         .
       7 "Aban Offshore Ltd."                             15523 510   -.04095567       .         .
       7 "Aban Offshore Ltd."                             15524 510    .05026695       .         .
       7 "Aban Offshore Ltd."                             15525 510    .04786104       .         .
       7 "Aban Offshore Ltd."                             15526 510     .0998383       .         .
       8 "Abbott India Ltd."                              15430 506            .       . .28818443
       8 "Abbott India Ltd."                              15519 509    .01531915  483.16         .
       8 "Abbott India Ltd."                              15522 510   -.01894382       .         .
       8 "Abbott India Ltd."                              15523 510  -.002563226       .         .
       8 "Abbott India Ltd."                              15524 510   .007366777       .         .
       8 "Abbott India Ltd."                              15525 510  -.003061204       .         .
       8 "Abbott India Ltd."                              15526 510  .0030706034       .         .
       9 "Accelya Solutions India Ltd."                   15430 506            .       .  .6578947
       9 "Accelya Solutions India Ltd."                   15519 509    .01359519   77.16         .
       9 "Accelya Solutions India Ltd."                   15522 510  -.012667637       .         .
       9 "Accelya Solutions India Ltd."                   15523 510  -.016603751       .         .
       9 "Accelya Solutions India Ltd."                   15524 510    .03376818       .         .
       9 "Accelya Solutions India Ltd."                   15525 510   .012620616       .         .
       9 "Accelya Solutions India Ltd."                   15526 510  -.006598196       .         .
      10 "Adani Enterprises Ltd."                         15430 506            .       . 1.5151515
      10 "Adani Enterprises Ltd."                         15519 509   -.01300107  401.15         .
      10 "Adani Enterprises Ltd."                         15522 510  -.021953877       .         .
      10 "Adani Enterprises Ltd."                         15523 510   .004489334       .         .
      10 "Adani Enterprises Ltd."                         15524 510   .001117344       .         .
      10 "Adani Enterprises Ltd."                         15525 510  .0044642813       .         .
      10 "Adani Enterprises Ltd."                         15526 510 -.0033333036       .         .
      11 "Aditya Birla Minacs I T Services Ltd. [Merged]" 15430 506            .       .  .2777778
      11 "Aditya Birla Minacs I T Services Ltd. [Merged]" 15519 509 -.0011016813  102.68         .
      11 "Aditya Birla Minacs I T Services Ltd. [Merged]" 15522 510  .0022059048       .         .
      11 "Aditya Birla Minacs I T Services Ltd. [Merged]" 15523 510  -.013206185       .         .
      11 "Aditya Birla Minacs I T Services Ltd. [Merged]" 15524 510   .035315987       .         .
      11 "Aditya Birla Minacs I T Services Ltd. [Merged]" 15525 510  -.005385996       .         .
      11 "Aditya Birla Minacs I T Services Ltd. [Merged]" 15526 510   -.00252712       .         .
      12 "Aditya Birla Nuvo Ltd. [Merged]"                15430 506            .       .  2.702703
      12 "Aditya Birla Nuvo Ltd. [Merged]"                15519 509   .023121985  594.88         .
      12 "Aditya Birla Nuvo Ltd. [Merged]"                15522 510  .0035409704       .         .
      12 "Aditya Birla Nuvo Ltd. [Merged]"                15523 510  -.024595296       .         .
      12 "Aditya Birla Nuvo Ltd. [Merged]"                15524 510   .009256333       .         .
      12 "Aditya Birla Nuvo Ltd. [Merged]"                15525 510   -.01265019       .         .
      12 "Aditya Birla Nuvo Ltd. [Merged]"                15526 510    .07997008       .         .
      13 "Ador Welding Ltd."                              15430 506            .       .  2.564103
      13 "Ador Welding Ltd."                              15519 509    .04238915   42.82         .
      13 "Ador Welding Ltd."                              15522 510   .007393744       .         .
      13 "Ador Welding Ltd."                              15523 510     .0568807       .         .
      13 "Ador Welding Ltd."                              15524 510    .14062497       .         .
      13 "Ador Welding Ltd."                              15525 510   -.05175035       .         .
      13 "Ador Welding Ltd."                              15526 510    .16372393       .         .
      14 "Aftek Ltd."                                     15430 506            .       .  .4132231
      14 "Aftek Ltd."                                     15519 509    .03029734  247.92         .
      14 "Aftek Ltd."                                     15522 510  -.007623924       .         .
      14 "Aftek Ltd."                                     15523 510  -.015913645       .         .
      14 "Aftek Ltd."                                     15524 510     .0401487       .         .
      14 "Aftek Ltd."                                     15525 510  -.025196567       .         .
      14 "Aftek Ltd."                                     15526 510   .016681941       .         .
      15 "Agro Dutch Inds. Ltd."                          15430 506            .       . 2.4390244
      15 "Agro Dutch Inds. Ltd."                          15519 509  -.004261864   54.54         .
      end
      format %td date
      format %tm mdate
      gen moy = month(dofm(mdate))
      gen year = year(dofm(mdate))
      //  CREATE A "FISCAL YEAR" RUNNING FROM JULY THROUGH SUBSEQUENT JUNE
      gen fyear = cond(moy > 6, year, year-1)
      frame put stock_id fyear mcap bmr, into(mcap_bmr_work)
      frame change mcap_bmr_work
      collapse (count) n_mcap = mcap n_bmr = bmr (firstnm) mcap bmr, by(stock_id fyear)
      assert n_mcap <= 1 & n_bmr <= 1 // VERIFY UNIQUE VALUE OF MCAP AND BMR
      replace fyear = fyear + 1 // CHANGE THE FYEAR TO WHICH THEY WILL APPLY
      frame change default
      rename (mcap bmr) orig=
      frlink m:1 stock_id fyear, frame(mcap_bmr_work)
      frget mcap bmr, from(mcap_bmr_work)
      frame drop mcap_bmr_work
      drop mcap_bmr_work
      egen byte representative = tag(stock_id fyear)
      
      //  MEDIAN SPLIT BASED ON JUNE VALUE OF mcap
      capture program drop one_year_median_split
      program define one_year_median_split
          xtile june_mcap_group = mcap, nq(2)
          exit
      end
      frame put stock_id fyear mcap if representative & !missing(mcap), into(median_split) // ***
      frame change median_split
      runby one_year_median_split, by(fyear)
      frame change default
      frlink m:1 stock_id fyear, frame(median_split stock_id fyear) // ***
      frget june_mcap_group, from(median_split)
      frame drop median_split
      drop median_split
      
      //  NOW SPLIT AT 30TH AND 70TH PERCENTILES OF bmr
      capture program drop one_year_three_groups
      program define one_year_three_groups
          if _N > = 3 {
              _pctile bmr, percentiles(30 70)
              gen cut = `r(r1)' in 1
              replace cut = `r(r2)' in 2
              xtile march_bmr_group = bmr, cutpoints(cut)
          }
          else {
              gen march_bmr_group = .
          }
          exit
      end
      frame put stock_id fyear bmr if representative & !missing(bmr), into(three_groups) // ***
      frame change three_groups
      runby one_year_three_groups, by(fyear) verbose
      frame change default
      frlink m:1 stock_id fyear, frame(three_groups stock_id fyear) // ***
      frget march_bmr_group, from(three_groups)
      frame drop three_groups
      drop three_groups
      
      capture program drop one_weighted_return
      program define one_weighted_return
          if !missing(june_mcap_group, march_bmr_group) {
              egen numerator = total(mcap*rt)
              egen denominator = total(mcap)
              gen vw_mean_rt = numerator/denominator
          }
          exit
      end
      drop if missing(june_mcap_group, march_bmr_group)
      runby one_weighted_return, by(date june_mcap_group march_bmr_group)
      
      collapse (first) vw_mean_rt, by(date june_mcap_group march_bmr_group)
      drop if missing(vw_mean_rt)
      keep date june_mcap_group march_bmr_group vw_mean_rt
      
      isid june_mcap_group march_bmr_group date, sort
      //  THE FOLLOWING LINES OF CODE ARE REMOVED AS THEY ARE BASED ON
      //  A PREVIOUS DEFINITION
      // by date june_mcap_group, sort: egen temp = mean(vw_mean_rt)
      // by date (june_mcap_group), sort: gen SMB = temp[1] - temp[_N]
      // drop temp
      //
      // by date march_bmr_group, sort: egen temp = mean(vw_mean_rt)
      // by date (march_bmr_group): gen HML = temp[1] - temp[_N]
      // drop temp
      
      label define june_mcap_group 1 "S" 2 "B"
      label define march_bmr_group 1 "L" 2 "M" 3 "H"
      label values june_mcap_group june_mcap_group
      label values march_bmr_group march_bmr_group
      decode june_mcap_group, gen (mcap_group)
      decode march_bmr_group, gen(bmr_group)
      drop june_mcap_group march_bmr_group
      egen groups = concat(mcap_group bmr_group)
      keep date groups vw_mean_rt
      rename vw_mean_rt =_
      reshape wide vw_mean_rt_, i(date) j(groups) string
      
      //  NEW DEFINITION OF HML
      gen HML = (vw_mean_rt_SH + vw_mean_rt_BH)/2 - (vw_mean_rt_SL + vw_mean_rt_BL)/2

      Comment


      • #93
        Fine. Got it. But if we were to modify this line of code to do the HML as per new definition then what to write instead of this line:

        Code:
         
         by date (march_bmr_group): gen HML = temp[1] - temp[_N]

        Comment


        • #94
          Look carefully at the code in #92. That line is commented out, so it no longer executes. If you want, you can just delete it and the other commented out commands. As you have seen, when I make changes to pre-existing code here I usually put the modified code in italics or bold, or color, or provide some other visual cue of the change. There is no simple similar way to show code that is to be eliminated. So I comment it out. This also has the advantage that if at some point you want to go back to the original way of calculating HML, you can remove the commenting and it is automatically restored.

          Comment


          • #95
            Yes got it. Many thanks indeed for this explanation.

            Comment


            • #96
              Please refer to code written in this thread at #57. Does this code conduct the two sorts, (first on mcap and then on mom) on monthly basis or once each year. How are the sorts handled really? If they are repeated each month irrespective of year, then how can we change the code to restrict sorts once a year from July month using sort variables observed in June month. Hope i made the point clear enough.

              Obviously value-weighting on lagged mcap is on monthly basis. That has no issues.

              Comment


              • #97
                The generic answer to this kind of question is to look at the -by()- options in -runby- or in -by ...- commands. In the particular situation you point to, we have the code -runby one_month_three_groups, by(mdate)-. So this is done on a monthly basis. If you want to do it on a yearly basis, you have to have a variable that gives the year, and replace -mdate- with the name of that year variable. To base that on values of variables from the June immediately preceding the start of a July-June fiscal year, you have to do a little dancing with the data. The technique is well illustrated by the code in #92, from the beginning down through the line -drop three_groups-.

                Looking more closely at the code in #92, if you want the value-weighted means to be calculated monthly, then the code there is not quite right. The lines
                Code:
                runby one_weighted_return, by(date june_mcap_group march_bmr_group)
                collapse (first) vw_mean_rt, by(date june_mcap_group march_bmr_group)
                should use the variable -mdate- instead of -date-.

                Obviously value-weighting on lagged mcap is on monthly basis. That has no issues.
                I'm glad it has no issues. But I want to react to the word "Obviously." This is not a finance/economics/econometrics forum, and I have no knowledge in any of those fields. I'm an epidemiologist who has a lot of experience using Stata, and I consider myself to be something of an expert in data management in Stata. I can't emphasize enough that none of your questions have anything in them that is obvious to me. I don't understand what most of the variables you are using are, and I have no idea what constitutes sensible use of those variables and what would be, in effect, analytic gibberish. In fact, it is fair to say that the only variable in any of these data sets that I understand is return. Everything else is, to me, a foreign language unrelated to any language I understand. The point of this little rant is to remind you that when you ask me for help on these analyses, you must never assume I understand anything about what you are doing, and your requests must be completely clear and explicit. If you leave anything to my imagination, the odds are I will get it wrong.

                Comment


                • #98
                  Well, I am fully aware of your observations to my second comment. At same time, i appreciate your knowledge which has benefited me a great deal.

                  Well, there is one more thing i want to ask you. In STATA, there is user written code for joint test of hypothesis called 'grstest2'. This test among other things generates a GRS test statistics. I am not able to figure out how can we get critical values of this GRS test statistics at 90%, 95%, 97.5%, 99% and 99.9% respectively, given that there are 25 LHS dependent variables having 228 monthly return observations. The following paper has reported the values but no source is given:

                  Size, Value and Momentum in International Stock Returns, (2012), Eugene F. Fama and Kenneth R. French, Journal of Financial Economics, Page 464

                  Some other paper defines this test-statistics as:

                  The statistic,under the null hypothesis of all regression intercepts are zero, has an F distribution with N and T-N-L degrees of freedom. Where T is the sample size, N is the number of LHS portfolios, L is the number of RHS factors.

                  In my case, N=25, T=228 and L ranges from 1-3 to 4.
                  Last edited by Sartaj Hussain; 29 Dec 2021, 12:27.

                  Comment


                  • #99
                    I'm afraid I can't help you with this test. I have never heard of it before.

                    If, however, all you need are the critical values of the F distribution with N and T-N-L df with N = 25, T = 228 - N - L df at the .9, .95, .975, .99, and .999 levels with L ranging from 1 to 4, the following Stata code will give you a table:

                    Code:
                    clear*
                    frame create critical_values int L float(level F_critical)
                    forvalues l = 1/4 {
                        foreach lvl of numlist .9 .95 .975 .99 .999 {
                            frame post critical_values (`l') (`lvl') (invF(25, 228-25-`l', `lvl'))
                        }
                    }
                    
                    frame change critical_values
                    tabdisp L level, c(F_critical)

                    Comment


                    • Thanks. This is what i need exactly.

                      Comment


                      • Please look at the command line first of the code in #11 of this thread. When i excute it after some time it was used lastly, the following error is encountered:

                        Code:
                        by mdate, sort: egen idiovol_decile = xtile (idiovol), nq(5)
                        unknown egen function xtile()
                        r(133);

                        Please resolve the same.

                        Comment


                        • In continuation to #101, the below code needs some modification: the deciles are to be made on previous month idiovol and weighted with previous month's mcap. I re-write the code and the dataset example below.

                          Code:
                          by mdate, sort: egen idiovol_decile = xtile (idiovol), nq(10)
                          
                          capture program drop one_weighted_return
                          program define one_weighted_return
                          if !missing(idiovol_decile){
                              egen numerator = total(mcap*rt)
                              egen denominator = total(mcap)
                              gen vw_mean_rt = numerator/denominator
                              }
                              exit
                          end
                          
                          runby one_weighted_return, by(mdate idiovol_decile)
                          
                          by mdate idiovol_decile, sort: egen ew_mean_rt = mean(rt)
                          
                          keep mdate idiovol_decile *_mean_rt
                          drop if missing(idiovol_decile, vw_mean_rt, ew_mean_rt)
                          
                          by mdate *_decile, sort: gen stock_count = _N
                          by mdate idiovol_decile, sort: keep if _n == 1
                          
                          gen str32 group = "_idiovol_d" + string(idiovol_d) + "_"
                          drop *_decile
                          reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string
                          Code:
                          * Example generated by -dataex-. For more info, type help dataex
                          clear
                          input int stock_id str56 stock float(rt mcap idiovol date mdate)
                          403 "Ruchi Soya Inds. Ltd."                                    .    61.48          . 15127 497
                          417 "Shaw Wallace & Co. Ltd. [Merged]"                         .   110.41  .12929256 15127 497
                          428 "Southern Petrochemical Inds. Corpn. Ltd."                 .    68.24  .09986409 15127 497
                           66 "Bharat Electronics Ltd."                                  .      492  .07359458 15127 497
                           77 "Blue Dart Express Ltd."                                   .   212.12  .10414287 15127 497
                          279 "Kajaria Ceramics Ltd."                                    .    33.85  .07913845 15127 497
                           16 "Ajanta Pharma Ltd."                                       .     69.8  .09283862 15127 497
                          119 "Cybertech Systems & Software Ltd."                        .    40.69  .19353513 15127 497
                           48 "B E M L Ltd."                                             .    65.59    .048544 15127 497
                          219 "Honda India Power Products Ltd."                          .    50.72  .14928423 15127 497
                           19 "Akzo Nobel India Ltd."                                    .   260.96   .0805758 15127 497
                          248 "Indo Rama Synthetics (India) Ltd."                        .   117.22          . 15127 497
                           68 "Bharat Heavy Electricals Ltd."                            .  4317.57  .09548052 15127 497
                          360 "P S L Ltd."                                               .    59.61   .0868574 15127 497
                          282 "Kanoria Chemicals & Inds. Ltd."                           .    41.58          . 15127 497
                          266 "Jaiprakash Industries Ltd. [Merged]"                      .    404.3  .08554617 15127 497
                          252 "Ineos Styrolution India Ltd."                             .    44.93  .14948723 15127 497
                          402 "Roofit Industries Ltd."                                   .   168.69   .0634334 15127 497
                          377 "Punjab Anand Lamp Inds. Ltd. [Merged]"                    .    84.66          . 15127 497
                          195 "H E G Ltd."                                               .   107.63  .07388717 15127 497
                          104 "Cholamandalam Financial Holdings Ltd."                    .   145.28   .0714538 15127 497
                          451 "Tamilnadu Petroproducts Ltd."                             .   175.44  .02623849 15127 497
                          184 "Grindwell Norton Ltd."                                    .    82.69          . 15127 497
                          415 "Schaeffler India Ltd."                                    .     56.5          . 15127 497
                          245 "Indian Overseas Bank"                                     .   400.32   .1396993 15127 497
                          480 "Ultramarine & Pigments Ltd."                              .    19.73   .1675484 15127 497
                          470 "Timex Group India Ltd."                                   .    57.56  .10671557 15127 497
                          414 "Satyam Computer Services Ltd. [Merged]"                   .  5361.33  .09352828 15127 497
                          455 "Tata Elxsi Ltd."                                          .   206.29  .12930584 15127 497
                          486 "Uttam Value Steels Ltd."                                  .    21.13   .6567888 15127 497
                          144 "Escorts Ltd."                                             .   457.95 .062986605 15127 497
                          168 "Geometric Ltd. [Merged]"                                  .    40.64  .18954584 15127 497
                           10 "Adani Enterprises Ltd."                                   .   621.52   .1543519 15127 497
                           47 "B A S F India Ltd."                                       .   157.97  .05339605 15127 497
                          411 "Sandvik Asia Pvt. Ltd."                                   .   131.41          . 15127 497
                           65 "Berger Paints India Ltd."                                 .   205.01  .07482278 15127 497
                           31 "Archies Ltd."                                             .    36.43   .1548937 15127 497
                          288 "Kobo Biotech Ltd."                                        .     8.14          . 15127 497
                          214 "Hindustan Unilever Ltd."                                  . 45189.22  .07393073 15127 497
                          268 "Jayant Agro-Organics Ltd."                                .     44.4  .11140443 15127 497
                          176 "Godrej Consumer Products Ltd."                            .   304.83          . 15127 497
                          270 "Jindal Saw Ltd."                                          .   326.84   .1415682 15127 497
                          175 "Godfrey Phillips India Ltd."                              .   347.22   .1246198 15127 497
                          426 "Smithkline Beecham Pharmaceuticals (India) Ltd. [Merged]" .   455.99  .07123204 15127 497
                          242 "Indian Aluminium Co. Ltd. [Merged]"                       .   625.79  .06581179 15127 497
                           78 "Blue Star Ltd."                                           .    77.02  .11294105 15127 497
                          259 "J B Chemicals & Pharmaceuticals Ltd."                     .   169.98  .17565393 15127 497
                          433 "Sterlite Industries (India) Ltd. [Merged]"                .   573.92  .05523154 15127 497
                          160 "Foseco India Ltd."                                        .    57.35   .2110898 15127 497
                          461 "Tata S S L Ltd. [Merged]"                                 .    62.13  .10355403 15127 497
                          443 "Suven Life Sciences Ltd."                                 .       51  .11138763 15127 497
                          141 "Emco Ltd."                                                .    21.48  .17326105 15127 497
                          347 "Nilkamal Ltd."                                            .    23.58          . 15127 497
                          293 "Kothari Products Ltd."                                    .    62.77          . 15127 497
                          466 "Television Eighteen India Ltd.[Merged]"                   .    74.63  .12593405 15127 497
                          420 "Shree Rama Multi-Tech Ltd."                               .   183.91  .22425704 15127 497
                            7 "Aban Offshore Ltd."                                       .    20.44  .12911019 15127 497
                          407 "S R F Ltd."                                               .    102.6  .12889893 15127 497
                          303 "M R F Ltd."                                               .   233.96  .07662546 15127 497
                          400 "Rhone-Poulenc (India) Ltd. [Merged]"                      .   199.35  .08890464 15127 497
                           37 "Asian Paints Ltd."                                        .  1604.65  .05083562 15127 497
                           55 "Balaji Telefilms Ltd."                                    .   216.47  .16558135 15127 497
                          170 "Gillette India Ltd."                                      .  1138.69  .08308157 15127 497
                          163 "G E Power India Ltd."                                     .   143.72  .07849518 15127 497
                          382 "R S Software (India) Ltd."                                .    32.46  .16159536 15127 497
                          348 "Nirma Ltd."                                               .  3141.55   .0540279 15127 497
                          167 "G T L Ltd."                                               .  1235.35   .2379354 15127 497
                          432 "Sterling Biotech Ltd."                                    .   129.72  .13534586 15127 497
                           29 "Apollo Tyres Ltd."                                        .   214.26   .1571892 15127 497
                          476 "Tourism Finance Corpn. Of India Ltd."                     .    52.25  .13786054 15127 497
                          206 "Himatsingka Seide Ltd."                                   .   166.13          . 15127 497
                           76 "Blow Plast Ltd. [Merged]"                                 .    23.52  .14285913 15127 497
                           57 "Balrampur Chini Mills Ltd."                               .   191.62  .04469626 15127 497
                          152 "Federal Bank Ltd."                                        .    95.77  .06886671 15127 497
                           38 "Assam Company India Ltd."                                 .    49.72  .15103628 15127 497
                          298 "Lakshmi Machine Works Ltd."                               .    86.93          . 15127 497
                          122 "D S Q Software Ltd."                                      .   285.54   .1664528 15127 497
                          251 "Indusind Bank Ltd."                                       .    207.2  .06942321 15127 497
                          516 "Zee Entertainment Enterprises Ltd."                       .  4912.93  .14013383 15127 497
                           79 "Bombay Burmah Trdg. Corpn. Ltd."                          .    66.98          . 15127 497
                          276 "K D L Biotech Ltd."                                       .    48.36  .13689928 15127 497
                           27 "Apeejay Tea Ltd. [Merged]"                                .     41.4          . 15127 497
                          473 "Titan Company Ltd."                                       .   179.67  .12048672 15127 497
                          481 "Unichem Laboratories Ltd."                                .    126.5  .07658487 15127 497
                          147 "Eveready Industries (India) Ltd."                         .    95.94  .16420387 15127 497
                          467 "Thermax Ltd."                                             .   101.49  .13225617 15127 497
                          337 "Nahar Poly Films Ltd."                                    .     58.4  .07295828 15127 497
                          300 "Linde India Ltd."                                         .    78.78   .1202313 15127 497
                          253 "Infomedia Press Ltd."                                     .    95.58  .17566204 15127 497
                          289 "Kochi Refineries Ltd. [Merged]"                           .   497.02  .08078612 15127 497
                          331 "Mukta Arts Ltd."                                          .   332.12   .1777349 15127 497
                           82 "Brabourne Enterprises Ltd. [Merged]"                      .    39.89  .06826832 15127 497
                          260 "J C T Ltd."                                               .     29.3  .21298572 15127 497
                          515 "Zandu Realty Ltd. [Merged]"                               .    79.23          . 15127 497
                          484 "United Breweries (Holdings) Ltd."                         .   360.18  .14550956 15127 497
                           20 "Alembic Ltd."                                             .    80.76  .10828427 15127 497
                          113 "Container Corpn. Of India Ltd."                           .   825.39          . 15127 497
                          367 "Pentasoft Technologies Ltd. [Merged]"                     .   134.03  .14360812 15127 497
                            4 "A D C India Communications Ltd."                          .    58.65  .22523586 15127 497
                          226 "I C I C I Ltd. [Merged]"                                  .  5654.49   .1056388 15127 497
                          end
                          format %td date
                          format %tm mdate

                          Comment


                          • Re #101: It is hard to understand why this would suddenly crop up when it worked previously. Perhaps you have moved to a different computer? the -xtile()- function is not part of official Stata's -egen-. It is from a user-written package (by Nick Cox, I think) -egenmore-. So try -ssc install egenmore- and then run the code again. I think that will solve the problem.

                            Re #102: The example data contains numerous stock_id's but only one month. So it isn't possible to test the code here as the previous month's mcap or idiovol is not available. But I think the following will do it:
                            Code:
                            by mdate, sort: egen idiovol_decile = xtile (idiovol), nq(10)
                            
                            capture program drop one_weighted_return
                            program define one_weighted_return
                            if !missing(idiovol_decile){
                                xtset stock_id mdate
                                egen numerator = total(L1.mcap*rt)
                                egen denominator = total(L1.mcap)
                                gen vw_mean_rt = numerator/denominator
                                drop numerator denominator
                                egen numerator = total(L1.idiovol*rt)
                                egen denominator = total(L1.idiovol)
                                gen ew_mean_rt = numerator/denominator
                                drop numerator denominator
                                }
                                exit
                            end
                            
                            runby one_weighted_return, by(mdate idiovol_decile) verbose
                            
                            keep mdate idiovol_decile *_mean_rt
                            drop if missing(idiovol_decile, vw_mean_rt, ew_mean_rt)
                            
                            by mdate *_decile, sort: gen stock_count = _N
                            by mdate idiovol_decile, sort: keep if _n == 1
                            
                            gen str32 group = "_idiovol_d" + string(idiovol_d) + "_"
                            drop *_decile
                            reshape wide  @stock_count  vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string
                            Note: With the example data, the -reshape- command will fail because there are no *stock_count variables in the example data.

                            Comment


                            • Yes, you are right, i had tried a new machine. What is the role of below part of code in #103.

                              Code:
                               egen numerator = total(L1.idiovol*rt)     egen denominator = total(L1.idiovol)     gen ew_mean_rt = numerator/denominator     drop numerator denominator
                              I think it is not what is needed. The portfolios or deciles weighted average returns should use only mcap as weight. For that i think this is sufficient:

                              Code:
                               egen numerator = total(L1.mcap*rt)     egen denominator = total(L1.mcap)     gen vw_mean_rt = numerator/denominator     drop numerator denominator
                              Here i think it is not properly understood. Actually, grouping of stocks into deciles ( from July of current year to June of next year) in the following part should use lagged idiovol and not current month's idiovol.

                              Code:
                               by mdate, sort: egen idiovol_decile = xtile (idiovol), nq(10)
                              Finally, this line of code is needed to calculate equal weighted returns:


                              Code:
                              by mdate idiovol_decile, sort: egen ew_mean_rt = mean(rt)
                              I append fresh data example for reference.

                              Code:
                              * Example generated by -dataex-. For more info, type help dataex
                              clear
                              input float(date mdate) int stock_id str18 stock float(rt mcap idiovol)
                              16982 557 1 "3M IN Equity"     .03979741 12935.15 .1011703
                              17013 558 1 "3M IN Equity"    -.03140462 12535.24 .0735412
                              17044 559 1 "3M IN Equity"     .01979822 12785.89 .0827738
                              17074 560 1 "3M IN Equity"     .14347082 14758.41 .1185964
                              17105 561 1 "3M IN Equity"     .03962137 15354.89  .083371
                              17135 562 1 "3M IN Equity"     .09906806 16953.97 .0820645
                              17166 563 1 "3M IN Equity"     .00860078 17100.42 .0466454
                              17197 564 1 "3M IN Equity"      .1170801 19224.46 .0865417
                              17225 565 1 "3M IN Equity"     .02308274 19673.37  .094335
                              17256 566 1 "3M IN Equity"    -.10203222 17765.06 .0797749
                              17286 567 1 "3M IN Equity"     .07274823 19105.61 .0691696
                              17317 568 1 "3M IN Equity"     .12631571 21677.99 .0807489
                              17347 569 1 "3M IN Equity"      .0753247 23373.96 .1245477
                              16982 557 2 "AACL IN Equity"   -.2388919 390.9465  .101902
                              17013 558 2 "AACL IN Equity"   .06368782  416.655 .1569841
                              17044 559 2 "AACL IN Equity"    .1752041   496.44 .1854097
                              17074 560 2 "AACL IN Equity"   .12095261  560.268 .1790345
                              17105 561 2 "AACL IN Equity"   .08489944  609.912 .1328724
                              17135 562 2 "AACL IN Equity"     .153943 711.4162  .176113
                              17166 563 2 "AACL IN Equity"   .06336961 757.9575 .1808397
                              17197 564 2 "AACL IN Equity"   .19202095  918.414  .160783
                              17225 565 2 "AACL IN Equity"  -.12146984 813.3637 .1425292
                              17256 566 2 "AACL IN Equity"  -.05373425 770.8117 .1655384
                              17286 567 2 "AACL IN Equity"    .1891032 931.2682 .1126103
                              17317 568 2 "AACL IN Equity"  -.00764457 924.1762 .1376465
                              17347 569 2 "AACL IN Equity"  -.02919915 897.5812 .0957982
                              16982 557 3 "AAVAS IN Equity"          .        .        .
                              17013 558 3 "AAVAS IN Equity"          .        .        .
                              17044 559 3 "AAVAS IN Equity"          .        .        .
                              17074 560 3 "AAVAS IN Equity"          .        .        .
                              17105 561 3 "AAVAS IN Equity"          .        .        .
                              17135 562 3 "AAVAS IN Equity"          .        .        .
                              17166 563 3 "AAVAS IN Equity"          .        .        .
                              17197 564 3 "AAVAS IN Equity"          .        .        .
                              17225 565 3 "AAVAS IN Equity"          .        .        .
                              17256 566 3 "AAVAS IN Equity"          .        .        .
                              17286 567 3 "AAVAS IN Equity"          .        .        .
                              17317 568 3 "AAVAS IN Equity"          .        .        .
                              17347 569 3 "AAVAS IN Equity"          .        .        .
                              16982 557 4 "ABB IN Equity"    .07374438 106034.8 .1117685
                              17013 558 4 "ABB IN Equity"   -.03083339 102815.9 .0502845
                              17044 559 4 "ABB IN Equity"    .14029822 118302.2 .0579322
                              17074 560 4 "ABB IN Equity"     .0459185 123860.5 .0314639
                              17105 561 4 "ABB IN Equity"    .19967006 151234.9 .0884278
                              17135 562 4 "ABB IN Equity"   -.01994549 148246.9 .0573419
                              17166 563 4 "ABB IN Equity"    .06173061 157687.5 .1035049
                              17197 564 4 "ABB IN Equity"   -.02732435 153436.6 .0359144
                              17225 565 4 "ABB IN Equity"    .00122632 153625.2 .0634785
                              17256 566 4 "ABB IN Equity"    -.0209452 150442.3 .0614537
                              17286 567 4 "ABB IN Equity"    .14099297 173220.4 .0487643
                              17317 568 4 "ABB IN Equity"    .11901111 195112.6 .0427221
                              17347 569 4 "ABB IN Equity"     .1726473 231880.8 .0962264
                              16982 557 5 "ABCAP IN Equity"          .        .        .
                              17013 558 5 "ABCAP IN Equity"          .        .        .
                              17044 559 5 "ABCAP IN Equity"          .        .        .
                              17074 560 5 "ABCAP IN Equity"          .        .        .
                              17105 561 5 "ABCAP IN Equity"          .        .        .
                              17135 562 5 "ABCAP IN Equity"          .        .        .
                              17166 563 5 "ABCAP IN Equity"          .        .        .
                              17197 564 5 "ABCAP IN Equity"          .        .        .
                              17225 565 5 "ABCAP IN Equity"          .        .        .
                              17256 566 5 "ABCAP IN Equity"          .        .        .
                              17286 567 5 "ABCAP IN Equity"          .        .        .
                              17317 568 5 "ABCAP IN Equity"          .        .        .
                              17347 569 5 "ABCAP IN Equity"          .        .        .
                              16982 557 6 "ABFRL IN Equity"          .        .        .
                              17013 558 6 "ABFRL IN Equity"          .        .        .
                              17044 559 6 "ABFRL IN Equity"          .        .        .
                              17074 560 6 "ABFRL IN Equity"          .        .        .
                              17105 561 6 "ABFRL IN Equity"          .        .        .
                              17135 562 6 "ABFRL IN Equity"          .        .        .
                              17166 563 6 "ABFRL IN Equity"          .        .        .
                              17197 564 6 "ABFRL IN Equity"          .        .        .
                              17225 565 6 "ABFRL IN Equity"          .        .        .
                              17256 566 6 "ABFRL IN Equity"          .        .        .
                              17286 567 6 "ABFRL IN Equity"          .        .        .
                              17317 568 6 "ABFRL IN Equity"          .        .        .
                              17347 569 6 "ABFRL IN Equity"          .        .        .
                              16982 557 7 "ACC IN Equity"    .02721828 146517.4 .0676918
                              17013 558 7 "ACC IN Equity"    .07357026 157703.1 .0454213
                              17044 559 7 "ACC IN Equity"    .07525434 170088.1 .0365174
                              17074 560 7 "ACC IN Equity"     .0880139 185736.8 .0399622
                              17105 561 7 "ACC IN Equity"   -.02719208 180798.9 .0250603
                              17135 562 7 "ACC IN Equity"     .1550959 211311.3 .0907192
                              17166 563 7 "ACC IN Equity"   -.03832475   203381  .050441
                              17197 564 7 "ACC IN Equity"   -.06281283 191098.7 .0747607
                              17225 565 7 "ACC IN Equity"   -.12333547 168945.6 .0732969
                              17256 566 7 "ACC IN Equity"   -.20440394 137713.1 .1034276
                              17286 567 7 "ACC IN Equity"    .13223848 157240.8 .0550011
                              17317 568 7 "ACC IN Equity"    .01378679 159423.7 .0678928
                              17347 569 7 "ACC IN Equity"     .0942572   175196 .1040733
                              16982 557 8 "ACEM IN Equity"   .06920837 135056.5  .065996
                              17013 558 8 "ACEM IN Equity"   .04953207 141914.6 .0528656
                              17044 559 8 "ACEM IN Equity"   .07243193 152668.8 .0521144
                              17074 560 8 "ACEM IN Equity"   .03927206   158839   .03884
                              17105 561 8 "ACEM IN Equity"   .00554727 159722.6 .0317309
                              17135 562 8 "ACEM IN Equity"    .2061489 196467.9 .0594034
                              17166 563 8 "ACEM IN Equity"  -.02347279 191909.9 .0548169
                              17197 564 8 "ACEM IN Equity"  -.02694612 187121.5 .0887978
                              17225 565 8 "ACEM IN Equity"  -.17030375 175648.6 .0635059
                              end
                              format %td date
                              format %tm mdate
                              Last edited by Sartaj Hussain; 17 Jun 2022, 11:50.

                              Comment


                              • What is the role of below part of code in #103.

                                Code:

                                egen numerator = total(L1.idiovol*rt) egen denominator = total(L1.idiovol) gen ew_mean_rt = numerator/denominator drop numerator denominator
                                I think it is not what is needed. The portfolios or deciles weighted average returns should use only mcap as weight. For that i think this is sufficient:

                                Code:

                                egen numerator = total(L1.mcap*rt) egen denominator = total(L1.mcap) gen vw_mean_rt = numerator/denominator drop numerator denominator
                                You are correct. I misread your request as calling for mcap weighted means and idiovol weighted means, but on re-reading I see that is not what you said.

                                The following code corrects the above, calculates the decile based on the previous (not current) month's idiovol, and also corrects other errors that became apparent with the updated example data that contains multiple months:

                                Code:
                                xtset stock_id mdate
                                gen lagged_idiovol = L1.idiovol
                                gen lagged_mcap = L1.mcap
                                by mdate, sort: egen idiovol_decile = xtile (lagged_idiovol), nq(10)
                                
                                capture program drop one_weighted_return
                                program define one_weighted_return
                                    if !missing(idiovol_decile){
                                        egen numerator = total(lagged_mcap*rt)
                                        egen denominator = total(lagged_mcap)
                                        gen vw_mean_rt = numerator/denominator
                                    }
                                    exit
                                end
                                
                                runby one_weighted_return, by(mdate idiovol_decile)
                                
                                keep mdate idiovol_decile *_mean_rt
                                drop if missing(idiovol_decile, vw_mean_rt) // NOTE: THERE IS NO ew_mean_rt VARIABLE
                                
                                by mdate *_decile, sort: gen stock_count = _N
                                by mdate idiovol_decile, sort: keep if _n == 1
                                
                                gen str32 group = "_idiovol_d" + string(idiovol_d) + "_"
                                drop *_decile
                                reshape wide  @stock_count  vw_mean_@rt, i(mdate) j(group) string

                                Comment

                                Working...
                                X