Announcement

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

  • #46
    The grouping only is to be done once in a year. While as the calculation of vw_mean_rt is to be done by month and not by fyear. So, there won't be any repeated values in that case.
    Ah, that is different from what I had understood. So some of the earlier code needs a slight revision, too.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float stock_id str51 stock float(mdate rt mcap mom) long obs_no
    1 "3I Infotech Ltd."           582  .0838774  1300.66 -.12633932   1
    1 "3I Infotech Ltd."           583  .0435588  1410.03  -.2648224   2
    1 "3I Infotech Ltd."           584 -.3898757  1471.64 -.17417905   3
    1 "3I Infotech Ltd."           585  -.389374   898.22  -.1374322   4
    1 "3I Infotech Ltd."           586 -.2276519    548.5  -.4467936   5
    1 "3I Infotech Ltd."           587  .1929012   423.63  -.8273377   6
    1 "3I Infotech Ltd."           588 -.0776197   505.35 -1.1370238   7
    1 "3I Infotech Ltd."           589 -.1879383   466.13  -.7739682   8
    1 "3I Infotech Ltd."           590  .1226252   378.52  -.8983014   9
    1 "3I Infotech Ltd."           591  .4138462   424.94  -.8540121  10
    1 "3I Infotech Ltd."           592  .5984765    600.8 -1.0945559  11
    1 "3I Infotech Ltd."           593 -.0251872   960.37 -.58259636  12
    1 "3I Infotech Ltd."           594  .0838774  1300.66 -.12633932   1
    1 "3I Infotech Ltd."           595  .0435588  1410.03  -.2648224   2
    1 "3I Infotech Ltd."           596 -.3898757  1471.64 -.17417905   3
    1 "3I Infotech Ltd."           597  -.389374   898.22  -.1374322   4
    1 "3I Infotech Ltd."           598 -.2276519    548.5  -.4467936   5
    1 "3I Infotech Ltd."           599  .1929012   423.63  -.8273377   6
    1 "3I Infotech Ltd."           600 -.0776197   505.35 -1.1370238   7
    1 "3I Infotech Ltd."           601 -.1879383   466.13  -.7739682   8
    1 "3I Infotech Ltd."           602  .1226252   378.52  -.8983014   9
    1 "3I Infotech Ltd."           603  .4138462   424.94  -.8540121  10
    1 "3I Infotech Ltd."           604  .5984765    600.8 -1.0945559  11
    1 "3I Infotech Ltd."           605 -.0251872   960.37 -.58259636  12
    2 "3M India Ltd."              582 -.0403587  1884.08 -.04965548  13
    2 "3M India Ltd."              583  .0123053  1808.04 -.00929656  14
    2 "3M India Ltd."              584 -.1137713  1830.29  -.0061362  15
    2 "3M India Ltd."              585 -.3538788  1622.06 -.03710564  16
    2 "3M India Ltd."              586 -.0153705  1048.05  -.3864366  17
    2 "3M India Ltd."              587  .0108619  1031.94  -.6253247  18
    2 "3M India Ltd."              588  .0210583  1043.15  -.8057244  19
    2 "3M India Ltd."              589 -.0546272  1065.11  -.6232147  20
    2 "3M India Ltd."              590 -.0166135  1006.93  -.6206292  21
    2 "3M India Ltd."              591  .1656428    990.2  -.6131397  22
    2 "3M India Ltd."              592  .2114972  1154.22  -.6534325  23
    2 "3M India Ltd."              593   .035205  1398.33 -.48089325  24
    2 "3M India Ltd."              594 -.0403587  1884.08 -.04965548  13
    2 "3M India Ltd."              595  .0123053  1808.04 -.00929656  14
    2 "3M India Ltd."              596 -.1137713  1830.29  -.0061362  15
    2 "3M India Ltd."              597 -.3538788  1622.06 -.03710564  16
    2 "3M India Ltd."              598 -.0153705  1048.05  -.3864366  17
    2 "3M India Ltd."              599  .0108619  1031.94  -.6253247  18
    2 "3M India Ltd."              600  .0210583  1043.15  -.8057244  19
    2 "3M India Ltd."              601 -.0546272  1065.11  -.6232147  20
    2 "3M India Ltd."              602 -.0166135  1006.93  -.6206292  21
    2 "3M India Ltd."              603  .1656428    990.2  -.6131397  22
    2 "3M India Ltd."              604  .2114972  1154.22  -.6534325  23
    2 "3M India Ltd."              605   .035205  1398.33 -.48089325  24
    3 "63 Moons Technologies Ltd." 582  -.142147  7708.22  -.4747278  25
    3 "63 Moons Technologies Ltd." 583 -.0119002  6612.52 -.34828845  26
    3 "63 Moons Technologies Ltd." 584 -.2733497  6533.83  -.4611148  27
    3 "63 Moons Technologies Ltd." 585 -.5062092  4747.81  -.5940032  28
    3 "63 Moons Technologies Ltd." 586  .1500146  2344.42  -.8046051  29
    3 "63 Moons Technologies Ltd." 587 -.1507828  2696.12 -1.2564824  30
    3 "63 Moons Technologies Ltd." 588 -.0406813  2289.59 -1.1768951  31
    3 "63 Moons Technologies Ltd." 589 -.1151034  2196.45 -1.1431617  32
    3 "63 Moons Technologies Ltd." 590  .4634089  1943.63 -1.1361648  33
    3 "63 Moons Technologies Ltd." 591  .1415551  2844.33 -1.0393578  34
    3 "63 Moons Technologies Ltd." 592   1.04409  3246.96  -.6623359  35
    3 "63 Moons Technologies Ltd." 593 -.0962323  6637.07 -.51756567  36
    3 "63 Moons Technologies Ltd." 594  -.142147  7708.22  -.4747278  25
    3 "63 Moons Technologies Ltd." 595 -.0119002  6612.52 -.34828845  26
    3 "63 Moons Technologies Ltd." 596 -.2733497  6533.83  -.4611148  27
    3 "63 Moons Technologies Ltd." 597 -.5062092  4747.81  -.5940032  28
    3 "63 Moons Technologies Ltd." 598  .1500146  2344.42  -.8046051  29
    3 "63 Moons Technologies Ltd." 599 -.1507828  2696.12 -1.2564824  30
    3 "63 Moons Technologies Ltd." 600 -.0406813  2289.59 -1.1768951  31
    3 "63 Moons Technologies Ltd." 601 -.1151034  2196.45 -1.1431617  32
    3 "63 Moons Technologies Ltd." 602  .4634089  1943.63 -1.1361648  33
    3 "63 Moons Technologies Ltd." 603  .1415551  2844.33 -1.0393578  34
    3 "63 Moons Technologies Ltd." 604   1.04409  3246.96  -.6623359  35
    3 "63 Moons Technologies Ltd." 605 -.0962323  6637.07 -.51756567  36
    4 "A B B India Ltd."           582 -.0510167 17196.36  .01307075  37
    4 "A B B India Ltd."           583   .140761 16319.06 -.24201564  38
    4 "A B B India Ltd."           584 -.1065452 18616.15  -.2728126  39
    4 "A B B India Ltd."           585 -.3175564 16632.69 -.28264767  40
    4 "A B B India Ltd."           586 -.1824886 11350.87  -.6069741  41
    4 "A B B India Ltd."           587  .0355104  9279.47  -.9133573  42
    4 "A B B India Ltd."           588    .05811  9608.99 -1.0667948  43
    4 "A B B India Ltd."           589 -.2337432 10167.36  -.7721182  44
    4 "A B B India Ltd."           590  .1625187  7790.81   -.745961  45
    4 "A B B India Ltd."           591  .1402668  9056.96  -.9950995  46
    4 "A B B India Ltd."           592  .3331282 10327.35  -.8049828  47
    4 "A B B India Ltd."           593  .1960904 13767.69  -.5617028  48
    4 "A B B India Ltd."           594 -.0510167 17196.36  .01307075  37
    4 "A B B India Ltd."           595   .140761 16319.06 -.24201564  38
    4 "A B B India Ltd."           596 -.1065452 18616.15  -.2728126  39
    4 "A B B India Ltd."           597 -.3175564 16632.69 -.28264767  40
    4 "A B B India Ltd."           598 -.1824886 11350.87  -.6069741  41
    4 "A B B India Ltd."           599  .0355104  9279.47  -.9133573  42
    4 "A B B India Ltd."           600    .05811  9608.99 -1.0667948  43
    4 "A B B India Ltd."           601 -.2337432 10167.36  -.7721182  44
    4 "A B B India Ltd."           602  .1625187  7790.81   -.745961  45
    4 "A B B India Ltd."           603  .1402668  9056.96  -.9950995  46
    4 "A B B India Ltd."           604  .3331282 10327.35  -.8049828  47
    4 "A B B India Ltd."           605  .1960904 13767.69  -.5617028  48
    5 "A B G Shipyard Ltd."        582  .0023809  1710.97   .4182359  49
    5 "A B G Shipyard Ltd."        583   .076158  1715.05  -.3066918  50
    5 "A B G Shipyard Ltd."        584 -.1761622  1845.66  -.2124559  51
    5 "A B G Shipyard Ltd."        585 -.6138647  1520.52  -.3513617  52
    5 "A B G Shipyard Ltd."        586 -.1548136   587.13  -.7107053  53
    5 "A B G Shipyard Ltd."        587  .3689071   496.23 -1.6249357  54
    5 "A B G Shipyard Ltd."        588 -.4745127    679.3 -1.7767352  55
    5 "A B G Shipyard Ltd."        589  .1875892   356.96 -1.1264615  56
    5 "A B G Shipyard Ltd."        590 -.0054054   423.92 -1.5662667  57
    5 "A B G Shipyard Ltd."        591  .3864734   421.63  -1.424076  58
    5 "A B G Shipyard Ltd."        592  .9551393   584.58 -1.2780082  59
    5 "A B G Shipyard Ltd."        593  -.113611  1142.94  -.7924519  60
    5 "A B G Shipyard Ltd."        594  .0023809  1710.97   .4182359  49
    5 "A B G Shipyard Ltd."        595   .076158  1715.05  -.3066918  50
    5 "A B G Shipyard Ltd."        596 -.1761622  1845.66  -.2124559  51
    5 "A B G Shipyard Ltd."        597 -.6138647  1520.52  -.3513617  52
    5 "A B G Shipyard Ltd."        598 -.1548136   587.13  -.7107053  53
    5 "A B G Shipyard Ltd."        599  .3689071   496.23 -1.6249357  54
    5 "A B G Shipyard Ltd."        600 -.4745127    679.3 -1.7767352  55
    5 "A B G Shipyard Ltd."        601  .1875892   356.96 -1.1264615  56
    5 "A B G Shipyard Ltd."        602 -.0054054   423.92 -1.5662667  57
    5 "A B G Shipyard Ltd."        603  .3864734   421.63  -1.424076  58
    5 "A B G Shipyard Ltd."        604  .9551393   584.58 -1.2780082  59
    5 "A B G Shipyard Ltd."        605  -.113611  1142.94  -.7924519  60
    6 "A C C Ltd."                 582  .1182775  9804.72 -.27696294  61
    6 "A C C Ltd."                 583 -.0387643 10964.48  -.6211684  62
    6 "A C C Ltd."                 584  .0890234 10539.54  -.5085044  63
    6 "A C C Ltd."                 585 -.1933296  11477.8  -.6682932  64
    6 "A C C Ltd."                 586 -.1766315  9258.83  -.4810188  65
    6 "A C C Ltd."                 587  .1763692  7623.52  -.6854388  66
    6 "A C C Ltd."                 588  .0563926  8969.31  -.8024532  67
    6 "A C C Ltd."                 589  .0692284  9475.17  -.3900176  68
    6 "A C C Ltd."                 590  .0682661 10131.12   -.346466  69
    6 "A C C Ltd."                 591  .1324026 10822.84   -.319372  70
    6 "A C C Ltd."                 592  .1990812 12255.82  -.1694572  71
    6 "A C C Ltd."                 593 -.0180076 14696.14  .09212224  72
    6 "A C C Ltd."                 594  .1182775  9804.72 -.27696294  61
    6 "A C C Ltd."                 595 -.0387643 10964.48  -.6211684  62
    6 "A C C Ltd."                 596  .0890234 10539.54  -.5085044  63
    6 "A C C Ltd."                 597 -.1933296  11477.8  -.6682932  64
    6 "A C C Ltd."                 598 -.1766315  9258.83  -.4810188  65
    6 "A C C Ltd."                 599  .1763692  7623.52  -.6854388  66
    6 "A C C Ltd."                 600  .0563926  8969.31  -.8024532  67
    6 "A C C Ltd."                 601  .0692284  9475.17  -.3900176  68
    6 "A C C Ltd."                 602  .0682661 10131.12   -.346466  69
    6 "A C C Ltd."                 603  .1324026 10822.84   -.319372  70
    6 "A C C Ltd."                 604  .1990812 12255.82  -.1694572  71
    6 "A C C Ltd."                 605 -.0180076 14696.14  .09212224  72
    7 "A I A Engineering Ltd."     582  .0162214  2363.88 -.06549466  73
    7 "A I A Engineering Ltd."     583  .2260563  2402.23 -.13198939  74
    7 "A I A Engineering Ltd."     584 -.1385219  2945.27 -.04077616  75
    7 "A I A Engineering Ltd."     585 -.4228988  2537.28  .24033707  76
    7 "A I A Engineering Ltd."     586 -.1980103  1464.27 -.00271862  77
    7 "A I A Engineering Ltd."     587  .0892358  1174.33  -.4690253  78
    7 "A I A Engineering Ltd."     588 -.1620132  1279.12  -.7592906  79
    7 "A I A Engineering Ltd."     589   .018413  1071.89  -.5515879  80
    7 "A I A Engineering Ltd."     590  .0714593  1091.62  -.8733688  81
    7 "A I A Engineering Ltd."     591   .359984  1169.63  -.7269815  82
    7 "A I A Engineering Ltd."     592  .4254062  1590.68  -.7309081  83
    7 "A I A Engineering Ltd."     593 -.0580311  2267.36 -.33348125  84
    7 "A I A Engineering Ltd."     594  .0162214  2363.88 -.06549466  73
    7 "A I A Engineering Ltd."     595  .2260563  2402.23 -.13198939  74
    7 "A I A Engineering Ltd."     596 -.1385219  2945.27 -.04077616  75
    7 "A I A Engineering Ltd."     597 -.4228988  2537.28  .24033707  76
    7 "A I A Engineering Ltd."     598 -.1980103  1464.27 -.00271862  77
    7 "A I A Engineering Ltd."     599  .0892358  1174.33  -.4690253  78
    7 "A I A Engineering Ltd."     600 -.1620132  1279.12  -.7592906  79
    7 "A I A Engineering Ltd."     601   .018413  1071.89  -.5515879  80
    7 "A I A Engineering Ltd."     602  .0714593  1091.62  -.8733688  81
    7 "A I A Engineering Ltd."     603   .359984  1169.63  -.7269815  82
    7 "A I A Engineering Ltd."     604  .4254062  1590.68  -.7309081  83
    7 "A I A Engineering Ltd."     605 -.0580311  2267.36 -.33348125  84
    8 "Aban Offshore Ltd."         582 -.1301212 10872.86  .46470425  85
    8 "Aban Offshore Ltd."         583 -.1686682  9458.07  .18721692  86
    8 "Aban Offshore Ltd."         584 -.0145899  7862.79  .08449736  87
    8 "Aban Offshore Ltd."         585  -.611874   7748.9  -.2840065  88
    8 "Aban Offshore Ltd."         586 -.1288336  3007.55  -.6351557  89
    8 "Aban Offshore Ltd."         587 -.0308757  2620.08 -1.2225515  90
    8 "Aban Offshore Ltd."         588 -.2791425  2539.18 -1.4337457  91
    8 "Aban Offshore Ltd."         589 -.3478934  1830.39   -1.20281  92
    8 "Aban Offshore Ltd."         590  .2657165  1193.61  -1.608876  93
    8 "Aban Offshore Ltd."         591  .0192669  1510.77  -1.692288  94
    8 "Aban Offshore Ltd."         592  1.222536  1539.88 -1.5530345  95
    8 "Aban Offshore Ltd."         593 -.0119843  3422.43 -1.7080214  96
    8 "Aban Offshore Ltd."         594 -.1301212 10872.86  .46470425  85
    8 "Aban Offshore Ltd."         595 -.1686682  9458.07  .18721692  86
    8 "Aban Offshore Ltd."         596 -.0145899  7862.79  .08449736  87
    8 "Aban Offshore Ltd."         597  -.611874   7748.9  -.2840065  88
    8 "Aban Offshore Ltd."         598 -.1288336  3007.55  -.6351557  89
    8 "Aban Offshore Ltd."         599 -.0308757  2620.08 -1.2225515  90
    8 "Aban Offshore Ltd."         600 -.2791425  2539.18 -1.4337457  91
    8 "Aban Offshore Ltd."         601 -.3478934  1830.39   -1.20281  92
    8 "Aban Offshore Ltd."         602  .2657165  1193.61  -1.608876  93
    8 "Aban Offshore Ltd."         603  .0192669  1510.77  -1.692288  94
    8 "Aban Offshore Ltd."         604  1.222536  1539.88 -1.5530345  95
    8 "Aban Offshore Ltd."         605 -.0119843  3422.43 -1.7080214  96
    9 "Abbott India Ltd."          582  .0186034   774.07  .03810834  97
    9 "Abbott India Ltd."          583  .0295522   745.03    .040532  98
    9 "Abbott India Ltd."          584 -.1340703   767.04 -.03217566  99
    9 "Abbott India Ltd."          585 -.0898703   664.21 -.00843637 100
    9 "Abbott India Ltd."          594  .0186034   774.07  .03810834  97
    9 "Abbott India Ltd."          595  .0295522   745.03    .040532  98
    9 "Abbott India Ltd."          596 -.1340703   767.04 -.03217566  99
    9 "Abbott India Ltd."          597 -.0898703   664.21 -.00843637 100
    end
    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)
    
    //  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 year mcap if moy == 6, into(median_split)
    frame change median_split
    runby one_year_median_split, by(year)
    frame change default
    frlink m:1 stock_id fyear, frame(median_split stock_id year)
    frget june_mcap_group, from(median_split)
    frame drop median_split
    drop median_split
    
    //  NOW SPLIT AT 30TH AND 70TH PERCENTILES OF MOM
    capture program drop one_year_three_groups
    program define one_year_three_groups
        _pctile mom, percentiles(30 70)
        gen cut = `r(r1)' in 1
        replace cut = `r(r2)' in 2
        xtile march_mom_group = mom, cutpoints(cut)
        exit
    end
    frame put stock_id year mom if moy == 3, into(three_groups)
    frame change three_groups
    runby one_year_three_groups, by(year) verbose
    frame change default
    frlink m:1 stock_id fyear, frame(three_groups stock_id year)
    frget march_mom_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_mom_group) {
            egen numerator = total(mcap*rt)
            egen denominator = total(mcap)
            gen vw_mean_rt = numerator/denominator
        }
        exit
    end
    
    runby one_weighted_return, by(mdate june_mcap_group march_mom_group)
    
    collapse (first) vw_mean_rt, by(mdate june_mcap_group march_mom_group)
    drop if missing(vw_mean_rt)
    keep mdate june_mcap_group march_mom_group vw_mean_rt
    
    isid june_mcap_group march_mom_group mdate, sort
    by mdate june_mcap_group, sort: egen temp = mean(vw_mean_rt)
    by mdate (june_mcap_group), sort: gen SMB = temp[1] - temp[_N]
    drop temp
    
    by mdate march_mom_group, sort: egen temp = mean(vw_mean_rt)
    by mdate (march_mom_group): gen HML = temp[1] - temp[_N]
    drop temp
    
    //  AND IF YOU WANT TO REDUCE TO ONE OBSERVATION PER MONTH
    label define june_mcap_group 1 "S" 2 "B"
    label define march_mom_group 1 "L" 2 "M" 3 "H"
    label values june_mcap_group june_mcap_group
    label values march_mom_group march_mom_group
    decode june_mcap_group, gen (mcap_group)
    decode march_mom_group, gen(mom_group)
    drop june_mcap_group march_mom_group
    egen groups = concat(mcap_group mom_group)
    keep mdate groups SMB HML vw_mean_rt
    rename vw_mean_rt =_
    reshape wide vw_mean_rt_, i(mdate) j(groups) string
    It dawns on me that it might be convenient to have as your end result a data set with just one observation per month and containing the values of weighted mean rt in each of the 6 groups and the values of SMB and HML. So that is what everything following the last comment does. If that's not where you're going, you can omit that part--all of the real calculation is done before that--this is just rearranging the data.

    Also, in order to have some results data to work with, I took your original data example and just duplicated every observation, changing the dates so that there would now be two July-June years worth of data. You don't need to do anything like that with your real data set. The -dataex- part is just to provide something to demo the code.

    Comment


    • #47
      This is fine. I will go through this and respond if need be. Plenty of gratitude is due to you. Well, i just wanted to know what does code do if (mcap) or (mom) values are not available for a certain rt observation in some year. As it is a cross sectional data, there are some stocks or firms for which (rt) may not be consistently available throughout.

      Comment


      • #48
        When rt is not available, that observation is not included in any of the 6 groups. Those observations also turn up with missing values of weighted mean rt, and so are excluded from the final calculations.

        Correction: When rt is not available, but mom and mcap are, the observation is still included in the 6 groups, but contributes nothing to the calculation of the weighted mean rt for the groups. In effect, these observations contribute to the definitions of the 6 groups, but do not contribute to the calculation of the groups' weighted mean rt.
        Last edited by Clyde Schechter; 13 Aug 2021, 12:59.

        Comment


        • #49
          That is not the answer i look for. Please read my # 47 once again. My point was if grouping variable, i.e. mcap or mom is missing for any rt.

          Comment


          • #50
            Oh, sorry. If mcap or mom is missing, then it cannot belong to any quantile of mcap (resp. mom). Consequently the mcap (resp. mom) group variable will be missing for such an observation. These will then turn up as "extra" mcap or mom groups (and possibly an extra group where both mcap and mom are missing.) Such groups will have their own weighted mean rt calculated, and they will appear in the final results. I imagine that such results are meaningless, so you might want to drop them with
            Code:
            drop if missing(june_mcap_group, march_mom_group)
            just before the -runby one_weighted_return, by(mdate june_mcap_group march_mom_group)- command.

            Comment


            • #51
              Your efforts have been extremely helpful. Really appreciate that.

              Now, I require modification in the code #46 again to construct Carhart's Momentum Factor. I append a fresh data example for the same. The following are the changes that have to be incorporated in the code #46 to enable it calculation of monthly momentum factor.

              1. For each month, beginning from July onwards all the return observations (rt) will be divided into two groups based on the median mcap of the month.

              2. Then for each month again, all the return observations (rt) will be independently divided into three groups following given percentiles (30, 70) based on the (mom) value observed in the same (current) month.

              3. Drop if sort variable, i.e. mcap and or mom is missing for any (rt) observation in any month.

              4. Next weighted average portfolio returns for 2*3=6 portfolios will be generated for each month using mcap of the month (t-1) as the weight.

              This will lead to generation 6 portfolio series of returns which can be defined as:

              SL = if june_mcap_group = 1 & march_mom_group =1
              SN = if june_mcap_group = 1 & march_mom_group = 2
              SW = if june_mcap_group = 1 & march_mom_group = 3
              BL = if june_mcap_group = 2 & march_mom_group = 1
              BN = if june_mcap_group = 2 & march_mom_group = 2
              BW = if june_mcap_group = 2 & march_mom_group = 3

              Where L = Loser, N = Neutral and W = Winner and S = Small and B = Big

              The monthly WML factor is the simple average of return of the winner minus loser return for small and big stocks and can be expressed as:

              WML = (WMLS - WMLB)/2 where

              WMLS = SW – SL
              WMLB = BW - BL

              Code:
              * Example generated by -dataex-. For more info, type help dataex
              clear
              input int s_id str51 stock float(mdate mcap rt) double mom
              1 "3I Infotech Ltd."           545   470.09 -.065294996                   .
              1 "3I Infotech Ltd."           546   549.01   .16801436                   .
              1 "3I Infotech Ltd."           547   699.99   .27500007                   .
              1 "3I Infotech Ltd."           548   698.51  -.00301666                   .
              1 "3I Infotech Ltd."           549   601.55  -.13888049                   .
              1 "3I Infotech Ltd."           550   738.93   .22839074                   .
              1 "3I Infotech Ltd."           551  1010.53    .3669909                   .
              1 "3I Infotech Ltd."           552   958.49  -.05283535                   .
              1 "3I Infotech Ltd."           553   899.97  -.06274164    .799376051640138
              1 "3I Infotech Ltd."           554   968.86    .0763701   .7366344144102186
              1 "3I Infotech Ltd."           555   997.06   .02835866   .8130045195575804
              1 "3I Infotech Ltd."           556   908.57  -.08997016   .8203556870575994
              1 "3I Infotech Ltd."           557   777.76  -.14414413   .7956805264111608
              2 "3M India Ltd."              545    712.8  -.09877507     .54090945282951
              2 "3M India Ltd."              546   894.67   .25515607  .42538299271836877
              2 "3M India Ltd."              547   902.44  .008687941   .6465656221844256
              2 "3M India Ltd."              548   986.82   .09349648   .5197590035386384
              2 "3M India Ltd."              549   822.35  -.16666667   .6575795379467309
              2 "3M India Ltd."              550    875.3   .06438356   .4235710916109383
              2 "3M India Ltd."              551   960.46   .09729727  .39042139472439885
              2 "3M India Ltd."              552  1036.39   .07905234  .49402921460568905
              2 "3M India Ltd."              553  1463.28    .4119021   .6128379311412573
              2 "3M India Ltd."              554  1601.78   .09465344   1.102471573278308
              2 "3M India Ltd."              555  1658.61   .03548066  1.1210935469716787
              2 "3M India Ltd."              556  1258.14  -.24145074    .874668138101697
              2 "3M India Ltd."              557  1294.07    .0285625    .731992470100522
              3 "63 Moons Technologies Ltd." 545  2144.56    .4980019  2.2004850818775594
              3 "63 Moons Technologies Ltd." 546  3518.73    .6407757  2.5002797706983984
              3 "63 Moons Technologies Ltd." 547   4135.3    .1752236  3.1227679229341447
              3 "63 Moons Technologies Ltd." 548  5797.74    .4020114   3.049011943396181
              3 "63 Moons Technologies Ltd." 549  4319.92  -.25489604  3.4575592558830976
              3 "63 Moons Technologies Ltd." 550   5414.2   .25331095  2.6309527438133955
              3 "63 Moons Technologies Ltd." 551   5722.7   .05698027  2.5242804270237684
              3 "63 Moons Technologies Ltd." 552  5700.92  -.00380663  2.2790137957781553
              3 "63 Moons Technologies Ltd." 553  5642.17 -.010305678   1.908627156401053
              3 "63 Moons Technologies Ltd." 554  7521.35    .3330603   1.983931994298473
              3 "63 Moons Technologies Ltd." 555  8167.18   .08586641  2.2980780007783324
              3 "63 Moons Technologies Ltd." 556  7217.25  -.11631107  2.1762221979442984
              3 "63 Moons Technologies Ltd." 557  5280.41  -.26836184  1.5619092511478812
              4 "A B B India Ltd."           545  5611.55  .015804194   .7466999449534342
              4 "A B B India Ltd."           546  6206.58   .10603835   .7079776012105867
              4 "A B B India Ltd."           547  6752.88   .08801939   .7378308259649202
              4 "A B B India Ltd."           548  7246.42  .073084936   .8390909320442006
              4 "A B B India Ltd."           549  7111.43 -.018627895   .8987574781058356
              4 "A B B India Ltd."           550  8219.08    .1557555   .6292119099525735
              4 "A B B India Ltd."           551  8175.21 -.005336917   .7683041988639161
              4 "A B B India Ltd."           552 10731.46    .3126831   .7612664164043963
              4 "A B B India Ltd."           553 10782.75  .004778608   .7509755552746356
              4 "A B B India Ltd."           554 12403.63   .15032235   .8586632814258337
              4 "A B B India Ltd."           555 13202.32   .06439103   .9622068498283625
              4 "A B B India Ltd."           556  9843.57    -.254406   .9469126258045435
              4 "A B B India Ltd."           557 10578.47   .07465776   .6767024267464876
              5 "A B G Shipyard Ltd."        545        .           .                   .
              5 "A B G Shipyard Ltd."        546        .           .                   .
              5 "A B G Shipyard Ltd."        547        .           .                   .
              5 "A B G Shipyard Ltd."        548        .           .                   .
              5 "A B G Shipyard Ltd."        549        .           .                   .
              5 "A B G Shipyard Ltd."        550        .           .                   .
              5 "A B G Shipyard Ltd."        551  1465.27           .                   .
              5 "A B G Shipyard Ltd."        552  1805.94   .23249346                   .
              5 "A B G Shipyard Ltd."        553   1795.5 -.005780313                   .
              5 "A B G Shipyard Ltd."        554  1924.33   .07175266                   .
              5 "A B G Shipyard Ltd."        555  2053.68  .067213535                   .
              5 "A B G Shipyard Ltd."        556     1547   -.2467146                   .
              5 "A B G Shipyard Ltd."        557  1365.47  -.11734692                   .
              6 "A C C Ltd."                 545  6762.05   .00318551   .4747412820579484
              6 "A C C Ltd."                 546  7689.84   .13720563  .48784412222448736
              6 "A C C Ltd."                 547  8724.77   .11332173   .5013018675381318
              6 "A C C Ltd."                 548  8921.02    .0183927   .6233515670755878
              6 "A C C Ltd."                 549   8176.4  -.08671113    .681834120885469
              6 "A C C Ltd."                 550  9377.73   .14404495  .47568515373859555
              6 "A C C Ltd."                 551  9856.43    .0493027   .4387676609912887
              6 "A C C Ltd."                 552 10611.45   .07263195   .4330069056013599
              6 "A C C Ltd."                 553 11660.56    .0954625   .4786345331231132
              6 "A C C Ltd."                 554 14578.24    .2459773   .5919444119790569
              6 "A C C Ltd."                 555 18927.52   .29587013   .8384766201488674
              6 "A C C Ltd."                 556 14245.38  -.24780466  1.0886839772574604
              6 "A C C Ltd."                 557 14650.79   .02826792   .8376938104629517
              7 "A G C Networks Ltd."        545   524.71  -.00351405   .6459528106497601
              7 "A G C Networks Ltd."        546   505.35  -.03683815   .4364063054090366
              7 "A G C Networks Ltd."        547   525.78   .04038753  .11753652838524431
              7 "A G C Networks Ltd."        548   501.58  -.04602057   .0747908599441871
              7 "A G C Networks Ltd."        549   521.51   .03972758   .2141401789849624
              7 "A G C Networks Ltd."        550   623.84    .1962336  .16645275277551264
              7 "A G C Networks Ltd."        551   699.07   .12055676    .421804366982542
              7 "A G C Networks Ltd."        552   584.42  -.16398585  .34621750412043184
              7 "A G C Networks Ltd."        553    640.5   .09595712  .07116988894995302
              7 "A G C Networks Ltd."        554   651.74  .017555542   .1780335953226313
              7 "A G C Networks Ltd."        555   603.99 -.073247455    .196853679837659
              7 "A G C Networks Ltd."        556   459.73   -.2388538  .18681207788176835
              7 "A G C Networks Ltd."        557   407.43   -.1137461 -.04852765426039696
              8 "A I A Engineering Ltd."     545        .           .                   .
              8 "A I A Engineering Ltd."     546        .           .                   .
              8 "A I A Engineering Ltd."     547        .           .                   .
              8 "A I A Engineering Ltd."     548        .           .                   .
              8 "A I A Engineering Ltd."     549        .           .                   .
              8 "A I A Engineering Ltd."     550        .           .                   .
              8 "A I A Engineering Ltd."     551    957.7           .                   .
              8 "A I A Engineering Ltd."     552   1101.6   .15025525                   .
              8 "A I A Engineering Ltd."     553  1032.63 -.062610954                   .
              end
              format %tm mdate
              .
              Last edited by Sartaj Hussain; 17 Aug 2021, 10:38.

              Comment


              • #52
                This is very similar to the last problem. Really it seems the only new wrinkle is the calculation of the weighted averages using the preceding months mcap as weight. Why don't you try doing this on your own first. I'll be happy to troubleshoot your code if you can't get it working.

                Comment


                • #53
                  Thanks for encouraging me. I tried. I could do what you called wrinkle part. Hope that is changed correctly. The construction of WML factor is a bit complicate for me. Well, i would learn it gradually. So for now i request you to help me update that. The second part of code in #46 which is for SMB and HML generation needs modification to allow calculation of WML instead as per definition in #51.

                  Code:
                  by mdate, sort: egen mcap_quantile = xtile(mcap), nq(2)
                  _pctile mom, percentiles(30 70)
                  gen cutoffs = r(r1) in 1
                  replace cutoffs = r(r2) in 2
                  xtile mom_quantile = mom, cutpoints(cutoffs)
                  
                  capture program drop one_weighted_return
                  program define one_weighted_return
                      if !missing(mcap_quantile, mom_quantile) {
                          egen numerator = total(mcap[_n-1]*rt)
                          egen denominator = total(mcap[_n-1])
                          gen vw_mean_rt = numerator/denominator
                      }
                      exit
                  end
                  
                  runby one_weighted_return, by(mdate mcap_quantile mom_quantile)

                  Comment


                  • #54
                    I think this gets you there. Note that your code for the mom_quantile did not do separate quantiles for each month. I assume that was a mistake, since in #51 you do say it should be done for each month. So I've fixed that in the code.

                    Code:
                    by mdate, sort: egen mcap_quantile = xtile(mcap), nq(2)
                    
                    //  NOW SPLIT AT 30TH AND 70TH PERCENTILES OF MOM
                    capture program drop one_month_three_groups
                    program define one_month_three_groups
                        _pctile mom, percentiles(30 70)
                        gen cut = `r(r1)' in 1
                        replace cut = `r(r2)' in 2
                        xtile mom_quantile = mom, cutpoints(cut)
                        exit
                    end
                    runby one_month_three_groups, by(mdate)
                    
                    capture program drop one_weighted_return
                    program define one_weighted_return
                        if !missing(mcap_quantile, mom_quantile) {
                            egen numerator = total(mcap[_n-1]*rt)
                            egen denominator = total(mcap[_n-1])
                            gen vw_mean_rt = numerator/denominator
                        }
                        exit
                    end
                    
                    runby one_weighted_return, by(mdate mcap_quantile mom_quantile)
                    
                    collapse (firstnm) vw_mean_rt, by(mdate mcap_quantile mom_quantile)
                    drop if missing(mcap_quantile, mom_quantile, vw_mean_rt)
                    
                    local mcap_groups S B
                    local mom_groups L N W
                    
                    forvalues mcap = 1/2 {
                        forvalues mom = 1/3 {
                            local varname `:word `mcap' of `mcap_groups''`:word `mom' of `mom_groups''
                            by mdate, sort: egen `varname' = max(cond(mcap_quantile == `mcap' & mom_quantile == `mom', ///
                                vw_mean_rt, .))
                        }
                    }
                    
                    gen WMLS = SW - SL
                    gen WMLB = BW - BL
                    gen WML = (WMLS - WMLB)/2
                    Note: in the example data, you have a lot of months, but very little data for each month. In fact, when I tried this code out with your data, there were no results because there weren't enough observations in any month to produce 6 groups. Ultimately for testing, I changed the mdate variable to be constant, so the code was tested on a data example artificially constrained to a single month. I have tried to write the code so that if all 6 groups are not computable for a given month, you will still get sensible results when possible (and missing values when not). But not having a good data set to fully test that out, I'm not 100% certain. If you will need help with more variations on this theme, in future data examples please provide data from just a few months (2 or 3 is plenty) but plenty of complete observations in each month (at least enough to actually create 6 groups, if possible.)

                    Comment


                    • #55
                      The code in #54 works though but doesn't give desired output.
                      Last edited by Sartaj Hussain; 17 Aug 2021, 12:42.

                      Comment


                      • #56
                        The code gives the following out put on another year's dataset. But i require WML and six portfolio returns by mdate. Which means one observation per month. As was in case of #46 for SMB and HML.

                        Would this command be appropriate:

                        Code:
                        collapse (mean) SL SN SW BL BN BW WMLS WMLB WML, by(mdate)
                        One more thing, how did overall code calculate value-weighted returns for June month when no previous month's mcap value is available. It should produce VW returns from July month onwards essentially.

                        Code:
                        * Example generated by -dataex-. For more info, type help dataex
                        clear
                        input float mcap_quantile byte mom_quantile float(vw_mean_rt SL SN SW BL BN BW WMLS)
                        1 1     .18162575   .18162575   .15991524   .13493839   .18182664     .10177067   .13974959  -.04668736
                        1 2     .15991524   .18162575   .15991524   .13493839   .18182664     .10177067   .13974959  -.04668736
                        1 3     .13493839   .18162575   .15991524   .13493839   .18182664     .10177067   .13974959  -.04668736
                        2 1     .18182664   .18162575   .15991524   .13493839   .18182664     .10177067   .13974959  -.04668736
                        2 2     .10177067   .18162575   .15991524   .13493839   .18182664     .10177067   .13974959  -.04668736
                        2 3     .13974959   .18162575   .15991524   .13493839   .18182664     .10177067   .13974959  -.04668736
                        1 1    .003006551  .003006551   .05560442   .05401698    .0872187     .10933877     .113911   .05101043
                        1 2     .05560442  .003006551   .05560442   .05401698    .0872187     .10933877     .113911   .05101043
                        1 3     .05401698  .003006551   .05560442   .05401698    .0872187     .10933877     .113911   .05101043
                        2 1      .0872187  .003006551   .05560442   .05401698    .0872187     .10933877     .113911   .05101043
                        2 2     .10933877  .003006551   .05560442   .05401698    .0872187     .10933877     .113911   .05101043
                        2 3       .113911  .003006551   .05560442   .05401698    .0872187     .10933877     .113911   .05101043
                        1 1     .08941895   .08941895    .1209528   .09186868   .06838574     .18334517   .16065025  .002449736
                        1 2      .1209528   .08941895    .1209528   .09186868   .06838574     .18334517   .16065025  .002449736
                        1 3     .09186868   .08941895    .1209528   .09186868   .06838574     .18334517   .16065025  .002449736
                        2 1     .06838574   .08941895    .1209528   .09186868   .06838574     .18334517   .16065025  .002449736
                        2 2     .18334517   .08941895    .1209528   .09186868   .06838574     .18334517   .16065025  .002449736
                        2 3     .16065025   .08941895    .1209528   .09186868   .06838574     .18334517   .16065025  .002449736
                        1 1    -.04294977  -.04294977  .018216224  -.03738491  .011502678    .006788128   .06976413  .005564854
                        1 2    .018216224  -.04294977  .018216224  -.03738491  .011502678    .006788128   .06976413  .005564854
                        1 3    -.03738491  -.04294977  .018216224  -.03738491  .011502678    .006788128   .06976413  .005564854
                        2 1    .011502678  -.04294977  .018216224  -.03738491  .011502678    .006788128   .06976413  .005564854
                        2 2    .006788128  -.04294977  .018216224  -.03738491  .011502678    .006788128   .06976413  .005564854
                        2 3     .06976413  -.04294977  .018216224  -.03738491  .011502678    .006788128   .06976413  .005564854
                        1 1    -.02038215  -.02038215   .05371788  .019211235   .10063282      .1603558   .06252578   .03959338
                        1 2     .05371788  -.02038215   .05371788  .019211235   .10063282      .1603558   .06252578   .03959338
                        1 3    .019211235  -.02038215   .05371788  .019211235   .10063282      .1603558   .06252578   .03959338
                        2 1     .10063282  -.02038215   .05371788  .019211235   .10063282      .1603558   .06252578   .03959338
                        2 2      .1603558  -.02038215   .05371788  .019211235   .10063282      .1603558   .06252578   .03959338
                        2 3     .06252578  -.02038215   .05371788  .019211235   .10063282      .1603558   .06252578   .03959338
                        1 1     .24138413   .24138413   .20109846    .2464077    .1054305     .09409977    .0659551  .005023569
                        1 2     .20109846   .24138413   .20109846    .2464077    .1054305     .09409977    .0659551  .005023569
                        1 3      .2464077   .24138413   .20109846    .2464077    .1054305     .09409977    .0659551  .005023569
                        2 1      .1054305   .24138413   .20109846    .2464077    .1054305     .09409977    .0659551  .005023569
                        2 2     .09409977   .24138413   .20109846    .2464077    .1054305     .09409977    .0659551  .005023569
                        2 3      .0659551   .24138413   .20109846    .2464077    .1054305     .09409977    .0659551  .005023569
                        1 1     .29357228   .29357228    .2159555    .2052518   .22087845     .18677033   .15517655  -.08832046
                        1 2      .2159555   .29357228    .2159555    .2052518   .22087845     .18677033   .15517655  -.08832046
                        1 3      .2052518   .29357228    .2159555    .2052518   .22087845     .18677033   .15517655  -.08832046
                        2 1     .22087845   .29357228    .2159555    .2052518   .22087845     .18677033   .15517655  -.08832046
                        2 2     .18677033   .29357228    .2159555    .2052518   .22087845     .18677033   .15517655  -.08832046
                        2 3     .15517655   .29357228    .2159555    .2052518   .22087845     .18677033   .15517655  -.08832046
                        1 1     -.1734792   -.1734792  -.16970883  -.19935976  -.08141438    -.07433158  -.06509113  -.02588056
                        1 2    -.16970883   -.1734792  -.16970883  -.19935976  -.08141438    -.07433158  -.06509113  -.02588056
                        1 3    -.19935976   -.1734792  -.16970883  -.19935976  -.08141438    -.07433158  -.06509113  -.02588056
                        2 1    -.08141438   -.1734792  -.16970883  -.19935976  -.08141438    -.07433158  -.06509113  -.02588056
                        2 2    -.07433158   -.1734792  -.16970883  -.19935976  -.08141438    -.07433158  -.06509113  -.02588056
                        2 3    -.06509113   -.1734792  -.16970883  -.19935976  -.08141438    -.07433158  -.06509113  -.02588056
                        1 1      -.079728    -.079728   -.0824271   -.0781909 .0031145704 -.00006382787  .034985196 .0015370995
                        1 2     -.0824271    -.079728   -.0824271   -.0781909 .0031145704 -.00006382787  .034985196 .0015370995
                        1 3     -.0781909    -.079728   -.0824271   -.0781909 .0031145704 -.00006382787  .034985196 .0015370995
                        2 1   .0031145704    -.079728   -.0824271   -.0781909 .0031145704 -.00006382787  .034985196 .0015370995
                        2 2 -.00006382787    -.079728   -.0824271   -.0781909 .0031145704 -.00006382787  .034985196 .0015370995
                        2 3    .034985196    -.079728   -.0824271   -.0781909 .0031145704 -.00006382787  .034985196 .0015370995
                        1 1     -.0766945   -.0766945  -.05777332   -.0479508  -.03299457  -.0011578099 -.016780023  .028743707
                        1 2    -.05777332   -.0766945  -.05777332   -.0479508  -.03299457  -.0011578099 -.016780023  .028743707
                        1 3     -.0479508   -.0766945  -.05777332   -.0479508  -.03299457  -.0011578099 -.016780023  .028743707
                        2 1    -.03299457   -.0766945  -.05777332   -.0479508  -.03299457  -.0011578099 -.016780023  .028743707
                        2 2  -.0011578099   -.0766945  -.05777332   -.0479508  -.03299457  -.0011578099 -.016780023  .028743707
                        2 3   -.016780023   -.0766945  -.05777332   -.0479508  -.03299457  -.0011578099 -.016780023  .028743707
                        1 1     .09062248   .09062248   .12176552   .10786302   .09307132     .10926148   .05060379   .01724054
                        1 2     .12176552   .09062248   .12176552   .10786302   .09307132     .10926148   .05060379   .01724054
                        1 3     .10786302   .09062248   .12176552   .10786302   .09307132     .10926148   .05060379   .01724054
                        2 1     .09307132   .09062248   .12176552   .10786302   .09307132     .10926148   .05060379   .01724054
                        2 2     .10926148   .09062248   .12176552   .10786302   .09307132     .10926148   .05060379   .01724054
                        2 3     .05060379   .09062248   .12176552   .10786302   .09307132     .10926148   .05060379   .01724054
                        1 1    -.12639274  -.12639274  -.12974228  -.17925207  -.12537986     -.1507314  -.13518386  -.05285934
                        1 2    -.12974228  -.12639274  -.12974228  -.17925207  -.12537986     -.1507314  -.13518386  -.05285934
                        1 3    -.17925207  -.12639274  -.12974228  -.17925207  -.12537986     -.1507314  -.13518386  -.05285934
                        2 1    -.12537986  -.12639274  -.12974228  -.17925207  -.12537986     -.1507314  -.13518386  -.05285934
                        2 2     -.1507314  -.12639274  -.12974228  -.17925207  -.12537986     -.1507314  -.13518386  -.05285934
                        2 3    -.13518386  -.12639274  -.12974228  -.17925207  -.12537986     -.1507314  -.13518386  -.05285934
                        1 1   -.014002712 -.014002712 .0042334497 -.003627675 .0044410713     .05831722  .005285408  .010375038
                        1 2   .0042334497 -.014002712 .0042334497 -.003627675 .0044410713     .05831722  .005285408  .010375038
                        1 3   -.003627675 -.014002712 .0042334497 -.003627675 .0044410713     .05831722  .005285408  .010375038
                        2 1   .0044410713 -.014002712 .0042334497 -.003627675 .0044410713     .05831722  .005285408  .010375038
                        2 2     .05831722 -.014002712 .0042334497 -.003627675 .0044410713     .05831722  .005285408  .010375038
                        2 3    .005285408 -.014002712 .0042334497 -.003627675 .0044410713     .05831722  .005285408  .010375038
                        end
                        ----------------
                        Last edited by Sartaj Hussain; 17 Aug 2021, 13:00.

                        Comment


                        • #57
                          Sorry, yes. To reduce to one observation per month, your proposed -collapse- command will do the trick.

                          One more thing, how did overall code calculate value-weighted returns for June month when no previous month's mcap value is available. It should produce VW returns from July month onwards essentially.
                          Good thing you asked. I hadn't noticed before, but the code for the weighted average is wrong. Since it is run separately for each month, mcap[_n-1] does not refer to the previous month. It refers to the previous firm in the current month. So the code needs some fixing. Here we go:
                          Code:
                          by mdate, sort: egen mcap_quantile = xtile(mcap), nq(2)
                          
                          //  NOW SPLIT AT 30TH AND 70TH PERCENTILES OF MOM
                          capture program drop one_month_three_groups
                          program define one_month_three_groups
                              _pctile mom, percentiles(30 70)
                              gen cut = `r(r1)' in 1
                              replace cut = `r(r2)' in 2
                              xtile mom_quantile = mom, cutpoints(cut)
                              exit
                          end
                          runby one_month_three_groups, by(mdate)
                          
                          capture program drop one_weighted_return
                          program define one_weighted_return
                              if !missing(mcap_quantile, mom_quantile) {
                                  egen numerator = total(lagged_mcap*rt)
                                  egen denominator = total(lagged_mcap)
                                  gen vw_mean_rt = numerator/denominator
                              }
                              exit
                          end
                          
                          xtset s_id mdate
                          gen lagged_mcap = L1.mcap
                          runby one_weighted_return, by(mdate mcap_quantile mom_quantile)
                          
                          collapse (firstnm) vw_mean_rt, by(mdate mcap_quantile mom_quantile)
                          drop if missing(mcap_quantile, mom_quantile, vw_mean_rt)
                          
                          local mcap_groups S B
                          local mom_groups L N W
                          
                          forvalues mcap = 1/2 {
                              forvalues mom = 1/3 {
                                  local varname `:word `mcap' of `mcap_groups''`:word `mom' of `mom_groups''
                                  by mdate, sort: egen `varname' = max(cond(mcap_quantile == `mcap' & mom_quantile == `mom', ///
                                      vw_mean_rt, .))
                              }
                          }
                          
                          gen WMLS = SW - SL
                          gen WMLB = BW - BL
                          gen WML = (WMLS - WMLB)/2
                          This code will do the weighted averages in the way you expect: if the previous month has no mcap value (or does not exist at all in the data set) that observation will be omitted from the calculation of the weighted mean.
                          Last edited by Clyde Schechter; 17 Aug 2021, 13:22.

                          Comment


                          • #58
                            The code in #57 works nicely. But in #46 we did no separate collapse command at the end to reduce to one observation per month. Like #46, can't we make it in-built for code #57 also that it does reduce to one observation for WML and others per month without having to write collapse....... at the end.

                            Comment


                            • #59
                              The reason there was no need for a collapse command at the end is because there was a series of -decode- commands and a -reshape wide- command that put the 6 portfolios in one observation, which we no longer have. It could be done that way for this, too. But the end result is the same. And so, I believe, is the amount of time it takes to run it. I think the current code is simpler and more transparent. So why change it?

                              Comment


                              • #60
                                Please refer to data example in #1 of this thread. We used following code to generate 25 monthly returns series each for value-weighted and equal weighted schemes.

                                Code:
                                capture program drop one_mdate
                                program define one_mdate
                                    gen cutoffs = .
                                    _pctile size, nq(5)
                                    forvalues i = 1/4 {
                                        replace cutoffs = r(r`i') in `i'
                                    }
                                    display `"`cutoffs'"'
                                    xtile size_quintile = size, cutpoints(cutoffs)
                                    drop cutoffs
                                    by size_quintile, sort: egen idiovol_quintile = xtile(idiovol), nq(5)
                                    tab *_quintile
                                    exit
                                end
                                
                                
                                // DOUBLE SORT EACH FISCAL YEAR
                                runby one_mdate, by(mdate) status
                                
                                capture program drop one_weighted_return
                                program define one_weighted_return
                                    egen numerator = total(mcap*rt)
                                    egen denominator = total(mcap)
                                    gen vw_mean_rt = numerator/denominator
                                    exit
                                end
                                
                                runby one_weighted_return, by(mdate size_quintile idiovol_quintile)
                                
                                gen int portfolio_num = (size_quintile-1)*5 + idiovol_quintile
                                
                                by mdate size_quintile idiovol_quintile, sort: egen ew_mean_rt = mean(rt)
                                
                                keep mdate *_quintile *_mean_rt
                                by mdate *_quintile, sort: gen stock_count = _N
                                by mdate *_quintile, sort: keep if _n == 1
                                
                                gen str32 group = "size_q" + string(size_quintile) +"_idiovol_q" + string(idiovol_q) + "_"
                                drop *_quintile
                                reshape wide @stock_count vw_mean_@rt ew_mean_@rt, i(mdate) j(group) string
                                From the output of this code, i need to generate further variables according to below definitions. The new variables are created by taking difference of returns of the two extreme idiovol groups in each size quintile. For instance, for both vw and ew cases,

                                First, vw cases:

                                CV1 = vw mean return if size_q=1 & idiovol_q=5 minus vw mean return if size_q=1 & idiovol_q=1
                                CV2 = vw mean return if size_q=2 & idiovol_q=5 minus vw mean return if size_q=2 & idiovol_q=1
                                CV3 = vw mean return if size_q=3 & idiovol_q=5 minus vw mean return if size_q=3 & idiovol_q=1
                                CV4 = vw mean return if size_q=4 & idiovol_q=5 minus vw mean return if size_q=4 & idiovol_q=1
                                CV5 = vw mean return if size_q=5 & idiovol_q=5 minus vw mean return if size_q=5 & idiovol_q=1
                                CV_a = average(CV1+CV2+CV3+CV4+CV5)

                                Similarly for ew cases:

                                CE1 = ew mean return if size_q=1 & idiovol_q=5 minus ew mean return if size_q=1 & idiovol_q=1
                                CE2 = ew mean return if size_q=2 & idiovol_q=5 minus ew mean return if size_q=2 & idiovol_q=1
                                CE3 = ew mean return if size_q=3 & idiovol_q=5 minus ew mean return if size_q=3 & idiovol_q=1
                                CE4 = ew mean return if size_q=4 & idiovol_q=5 minus ew mean return if size_q=4 & idiovol_q=1
                                CE5 = ew mean return if size_q=5 & idiovol_q=5 minus ew mean return if size_q=5 & idiovol_q=1
                                CE_a = average(CE1+CE2+CE3+CE4+CE5)

                                So, 6 + 6 = 12 new variables in all. Hope that the explanation of the variables is very much clear to enable updating of the code. Request for incorporating of these calculations in the code given supra. Can it be done in a manner that if I change the sort variables i need not to make any changes in the code part for calculations of these 12 variables.





                                Comment

                                Working...
                                X