Announcement

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

  • #16
    Thank you for your reply. I talked to my professor and indeed I did not understand the problem correctly. I am sorry for that. The data consist of 878 montly observations, but Betas have to be calculated for each portfolio in two subsaples separately (1928:12-1963:6 and 1963:7-2001:12.) So for example, for portfolio FFS1BM1 I should get one cash-flow beta for years 1928:12-1963:6, another for the years 1963:7-2001:12 and discout-rate beta for the years 1963:7-2001:12 and different for years 1963:7-2001:12 (the same refers to any other portfolio in the data set; in total 45 portfolios). So for example for the first subsample I should get : 25 cash-flow Betas and 25 discount-rate Betas for 25 portfolios denoted with variables from FFS1BM1 to FFS5BM5 and 20 cash-flow Betas and 20 discount-rate Betas for 20 portfolios denoted with variables RISK1 to RISK20. (The same applies to the second subsample).
    Here I rewrote the formulas that I attached in the Word document:

    Beta i, CF = Cov(ri,t, NCF,t) / Var (NCF,t - NDR,t) + Cov(ri,t,NCF,t-1) / Var (NCF,t - NDR,t)

    Beta i, DR= Cov(ri,t, -NDR,t) / Var (NCF,t - NDR,t) + Cov(ri,t,-NDR,t-1) / Var (NCF,t - NDR,t)

    Cov and Var denote sample covariance and variance.
    Subscript i denotes observation for particular month; ri, t denotes return of the portfolio (e.g. FFS1BM1) in particular month , CF = cash-flow and DR =discount-rate.
    In the numerator of the Beta i, DR the covariance is calculated between portfolio return and good news about discount rates ( - NDR, t) ( thats why each observation of NDR enters formula with a minus).

    The each Beta denominator ( variance of unexpected market return) : Var (NCF,t - NDR,t), can be equivalently written as Var ( R_Me - Et-1R_Me)

    The second part of each Beta formula ( marked in red) includes one lag of NCF,t (NCF, t-1) and equivalently for NDR, t.
    Adding one lag is motivated by the possibility that, especially during the early years of sample period not all assets were traded frequently and synchronously.

    I Thank you for your time and appologies once again for not providing you with the exact problem description.
    Last edited by Emilia Kosinska; 15 May 2019, 07:38.

    Comment


    • #17
      I am re-using your most recent example data. It only contains observations from period 1, but the code I have written will handle both periods automatically.

      To do this efficiently, I have put the data into long layout, so that instead of a separate variable for each portfolio, you simply have a single variable, called return, and there is a new variable, called portfolio, that tells you which portfolio each observation refers to. The beta_cf and beta_dr are new variables created by the code, and they will be constant for any portfolio within either period. While it is possible to return the data to wide layout (thereby creating a new set of beta_cf and beta_dr variables, one for each portfolio) it is likely that whatever you are going to do next will still be easier in long layout. So I suggest leaving it as is unless you have a compelling reason to go back.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input long Date double(R_Me TY PE VS EtR_Me N_dr N_cf Rrf FFS1BM1 FFS1BM2 FFS1BM3 FFS1BM4 FFS1BM5 RISK1 RISK2 RISK3 RISK4 RISK5)
      192812    .12475028  -.63 3.2207675 1.8571141             .            .            .             .            .           .           .           .            .           .            .             .           .            .
      192903   .029256531  -.72 3.3042084 1.8296557   -.038889224    .10785189  -.039706131    .014241329     -.106354  .080632828  .043384034 -.034735077   .020690477   .10436306   .023249643    .082378998  .082817097    .10902987
      192906   .040631752 -1.01 3.3217432 1.9047829   -.038528673   .087829828 -.0086694034    .015075707   -.10352382  -.11447486 -.042051542 -.021380674  -.086778177  .014665017   .070001628     .08705208 .0089354216   .039139246
      192909    .05995754  -.65  3.491234 1.8646432   -.047649488    .19629581   -.08868878    .015394469   -.13047612  -.18770293  -.23653135 -.089298759   -.10070304   .25333763    .13627692    .074879436  .010797093   .057632931
      192912    -.3454056  -.14 3.0983275 1.7723598   -.049942325    -.3845253   .089062024    .013922151   -.28054889  -.56826115   -.4499719  -.41665723    -.3843622  -.35483874   -.16587278     -.2095297  -.35158381   -.38167334
      193003    .14357217  -.36  3.193036 1.7566879   .0031494299    .13967837 .00074437843     .01120042    .36766992   .33369376   .41376153   .19335355    .24767466  .063182837    .13164092     .17122614   .18871274    .23953424
      193006   -.21419871  -.08 3.0693161 1.9953584   -.030147595  -.046793046   -.13725807   .0091016192   -.21859381  -.26833296   -.3548671  -.26338121    -.3026609  -.13772241   -.12926397    -.21415697  -.30761642    -.2754436
      193009  -.096517094   .11 3.0174617 2.0934404   -.010058928  .0072921574  -.093750324   .0076277354   -.37087942  -.35649811   -.2026719  -.22628594   -.21177819   -.1194015  -.087412144    -.17051761  -.18751216   -.14642401
      193012   -.20156103   .25 2.7124927 2.1785421   -.014371335   -.24818221   .060992517   .0073176859   -.30880873  -.34992217  -.36143485    -.241553   -.33446302  -.18700434    -.2200233    -.23307802  -.22494195   -.32408892
      193103   .090615231   .53 2.8308013 2.1160557   .0055882806    .12294064   -.03791369   .0064724247  -.098217388   .54921705   .20366961   .35318276    .35564113  .031395202 -.0043554296     .06258715   .14558268    .36937381
      193106   -.12255038  1.03 2.5925127 2.1440688  -.0087446753   -.22152976    .10772406   .0054353501  -.015151266  -.06352808  -.19753199  -.15685419   -.14709796 -.044394096   -.10024776     -.1255058  -.10844453  -.023960581
      193109   -.41209437  1.45 2.4293368  2.255968    .021499987   -.16074683   -.27284753   .0050078915    -.6815588  -.42134141   -.4845058  -.43306593   -.41454527  -.28915282   -.34864921    -.45366055  -.48507438   -.49133298
      193112   -.17004316   .13 2.0864168 2.3758314    .047297748   -.27432431   .056983402    .009832534   -.37029346  -.27407194  -.26876198  -.24519036   -.27557655  -.14986516   -.16680979     -.2214484   -.1608628   -.44852102
      193203  -.079924823  -.23 2.0572521 2.3070669    .026209667  -.040253179  -.065881311   .0095216504   -.29682685   -.1046765  .044218465  -.01451279 -.0056994158  .033519014  -.066936055   -.067387274  -.12616949  -.018243618
      193206     -.446867  1.02 1.5006242 2.4491561     .01991643   -.53045509   .063671662   .0074260655    -.1803939  -.69987618   -.2658889  -.36320613   -.25624289  -.24571161   -.25235686    -.41452877  -.44385929    -.5217132
      193209    .55968458   .61 2.0422095 2.2250925    .082018018    .48605745  -.008390892   .0056438147    1.7015554   1.4780774   1.1599623   1.2013086    1.3189924   .47504021     .6158931     .69799037   1.1297997    1.5338106
      193212   -.15655068    .9 1.8478489 2.4710202   .0040287148   -.16719928  .0066198829   .0040803828   -.28850254  -.48295364  -.51891397  -.53625732   -.46590054 -.077603176   -.10139478    -.17453076  -.29386305   -.31707629
      193303   -.12119363   .12 1.7639007 2.3626043    .046322408    -.1194005  -.048115528   .0050160299  -.049518025  -.21128529  -.28613988  -.01728999   .041272825  -.14937511  -.058495154   -.077600031 -.028315325   -.20526428
      193306    .63600875   .72 2.2819462  2.298467    .040496984    .51014389   .085367877   .0050165832    1.5419082   2.0771286   1.7781512   1.8953966     1.733572    .6881956    .81859817     1.0749526   1.0548967    1.3287004
      193309   -.10052403   .94 2.3066893 2.4460944   -.013332628   .021758681   -.10895009   .0035463455   -.19894616  -.22897036  -.15395102  -.10938244   -.25950278  .011673153  -.051708805    -.13180946   -.1375328   -.13055162
      193312   .025922995   .46 2.2560423 2.5477196     .02158164 -.0043295708  .0086709256   .0033368271   -.48316368   -.1677332  -.18069419  .095366198   -.14460339 -.015566804   .026179523 -.00080337097  .031478481  -.030610817
      193403   .099633907   .67 2.3434459 2.3892259   .0058318245   .050372467   .043429615   .0032454132    .93968377   .58809759   .55605409   .48262874    .52535539  .078129583    .12979138     .21564419   .24842462    .18973417
      193406  -.068838249  1.28 2.2792184 2.4696392   .0071516059  -.052725933  -.023263923   .0024017249   -.33254912  -.12430857 -.051795063  -.10462735   -.11049556  .023632931    -.1245006    -.13272673  -.17032888   -.17807953
      193409  -.062210859  1.14 2.1798095 2.5468249    .025870258  -.077021019  -.011060098   .0022014815   -.05526265  -.21115651  -.21172976  -.15520224   -.14818917 -.028956867   -.12448995     -.1914268  -.17568195   -.17081091
      193412   .063450254  1.18 2.2344085 2.6517267    .025303776   .084583746  -.046437267    .002201811   .074613988   .15076708   .18094449   .20943041   .079067713  .093470587    .12903485    .042690693  .040389879 -.0025792253
      193503  -.090574809  1.69 2.1472461 2.7133913    .012453713  -.081505057  -.021523465  .00089380889   -.16992208  -.39199147  -.17091326  -.28286559   -.21277058 -.077221391   -.13887896     -.2278849  -.29066952   -.32900307
      193506    .17308178  1.79 2.3367592 2.6528761    .030721039    .16944898  -.027088246  .00034133036      .205144 -.043301315   .21204829   .23976078    .12239697   .13265591    .22466701     .23666951   .16296174    .26106772
      193509    .12079417  1.61  2.477612 2.6280946    .011266347    .12537375  -.015845928  .00039558633   .070331756   .50669035   .41069757   .31507631    .32259239  .058117387     .2175871     .16314083   .25911247    .28539343
      193512    .16250934   1.6 2.6008892 2.5856493   .0048837235    .11528904   .042336577  .00031095385    .53421831   .17949996   .42281388   .43018879    .46720223   .12800445    .24617738     .21107364   .32429809    .31263112
      193603   .097543911  1.68 2.7440192 2.4694844  -.0020784427     .1067279 -.0071055459  .00025428522    .45919944   .29543703   .36863184   .26220128    .43747001  .029637851    .08788889     .18915103   .27713285    .20519867
      193606 -.0069662492  1.62 2.7438255 2.5319316 -.00012527291   .019683839  -.026524815  .00041507121   -.27732658  -.24726839  -.10614178 -.080117685   -.18880428  .026965953 -.0055344251   -.038213638 -.048831726  -.023537832
      193609   .086598288  1.59 2.8416706 2.5221143   .0019125201    .10649831  -.021812545  .00047070669   -.05319435  .088620894   .20918429   .24449399    .17339032   .12523021    .11633828     .14893978    .1363108    .15618156
      193612    .10174364  1.54 2.9102037 2.4377632   -.008068044   .059310442   .050501244  .00041475863    .22012015   .10693256   .18689049   .26549011    .21962621  .078192717    .16626159     .10850921  .098384819   .092632877
      193703   .039351159  1.27 2.9739517 2.3271827  -.0098348532   .044275108  .0049109042   .0012054395 -.0032482198 -.017044906   .14981228    .1858812    .28024566  .039426212   .077854188     .19118205    .1462972    .16403617
      193706   -.12901254  1.28 2.8314034 2.4930039  -.0098112235  -.082785434  -.036415886   .0015806469   -.37835636  -.22424524   -.3562444  -.22998008   -.37657713  -.11185068   -.14589709    -.15994956  -.22082012   -.23231111
      193709     -.111366  1.33 2.7481621 2.5349238 -.00012041349  -.054406455  -.056839128   .0012218437   -.15689109  -.23859545  -.16647334  -.21435005   -.16832524  -.10911185   -.11739128    -.18467275  -.19092673   -.07566993
      193712   -.22799573  1.57 2.4829122 2.5117296   .0023602342   -.25974179   .029385828  .00034206732   -.34779757  -.41883519  -.44604289  -.37703892   -.37633483  -.21832296    -.2328189    -.30693802  -.29653763   -.27115684
      193803   -.20557469  1.72 2.4172208 2.5509157    .026779483   -.06242012   -.16993405  .00021401526   -.18248701  -.21361298  -.18258644  -.24947606   -.27595785  -.20711283   -.17951596    -.21377983  -.29802665   -.27776103
      193806     .3074818  1.95   2.41311 2.5055259    .029830735    .01381468    .26383638 .000057691025    .58981412   .51267417    .5668762   .44178964    .42286987   .29680753    .38228149     .46751463   .51862673    .64212453
      193809   .050639224  1.82 2.5643184 2.5458843   .0087369519    .13556581  -.093663541  .00011760445   .071553339 -.055866371   .17229561  .083641251  -.032203709 .0025825201   .024841018    .015275459    .1006358   .065560427
      193812   .095621897  1.96 2.6572545 2.5705523    .010741464   .098043267  -.013162834 .000040830425   .052739778   .21871105   .16761924  .089388894   .053481624  .056691637     .1278743     .13696414   .13401058    .26053685
      193903   -.15421872  2.03 2.6536644 2.6162633   .0049223778 -.0083065762   -.15083452 .000018280065   -.36330703  -.29726583  -.28086936  -.25751594   -.23568047  -.12350432   -.23582774    -.28860392  -.31245774   -.37660961
      193906  .0092952309  1.86 2.5919211 2.4885233    .017475855  -.081891004   .073710379 .000023320164  -.007995497 -.032812983 -.016190743 -.017805353   -.04728126  .031622959  -.039183262   -.042528495    -.057109 -.0090714562
      193909    .17588746  1.79 2.7205596 2.3860514    .014398397    .10558706   .055902004  .00013510452    .57815751   .54370251   .71600191    .5027206     .9146629  .045222186    .28945304     .45190724   .44486768    .61583731
      193912  -.011363188  2.02 2.7072421  2.551046   .0020447579   .023291571  -.036699517 .000028460222   -.10960015  -.20460634  -.18071512  -.16005008   -.32780796  .018779601  -.024038765     -.0840026  -.10476488   -.15004633
      end
      
      gen int mdate = ym(floor(Date/100), mod(Date, 100))
      format mdate %tm
      assert !missing(mdate)
      drop Date
      order mdate, first
      
      gen byte period = (mdate < tm(1963m7))
      
      //    GO TO LONG LAYOUT
      rename (FFS* RISK*) return=
      reshape long return, i(mdate) j(portfolio) string
      
      capture program drop one_period
      program define one_period
          tsset mdate, delta(3)
          gen delta = N_cf - N_dr
          summ delta
          local denom = r(Var)
          corr return N_cf, cov
          local cf1 = r(cov_12)
          corr return L1.N_cf, cov
          local cf2 = r(cov_12)
          gen beta_cf = (`cf1' + `cf2')/`denom'
          corr return N_dr, cov
          local dr1 = r(cov_12)
          corr return L1.N_dr, cov
          local dr2 = r(cov_12)
          gen beta_dr = (`dr1' + `dr2')/`denom'
          drop delta
          exit
      end
      
      runby one_period, by(period portfolio) status
      Notes:

      1. To run this code you need to install -runby-, written by Robert Picard and me, available from SSC.
      2. Because you are now computing your covariances over a large number of observations, I have lazily taken it for granted that there will be no portfolios with so much missing data in either period that the covariance calculations fail. So I have not coded the -capture- framework that I used earlier. If that turns out to be wrong, here's what will happen. That particular portfolio and period will be skipped over: they will not appear in the output data file at all. So after running this code, inspect the data to see if there are any missing portfolios in either period. If there are, it means that one of the covariance calculations failed, and you will need to figure out how to fix your data.

      Comment


      • #18
        Thank you for the code! I run it line by line and everything seemed fine until the runby command- it cancels my data again... There are no portfolios with missing data, except the data from 1928m12. When I deleted that row from my data set, the final line of the code cancels the data anyway and I get the notification:

        . runby one_period, by(period portfolio) status

        elapsed ----------- by-groups ---------- ------- observations ------ time
        time count errors no-data processed saved remaining

        00:00:00 90 90 0 39,465 0 00:00:00


        Number of by-groups = 90
        by-groups with errors = 90
        by-groups with no data = 0
        Observations processed = 39,465
        Observations saved = 0


        Can you please explain why this might be the case? Thank you for your help

        Comment


        • #19
          The output you get says that all 90 of your by-groups produced errors. -runby- eliminates data from any group that produces an error, and since all of them did, that's why you were left empty-handed. The question is why you are getting errors in program one_period.

          Please post an example of your data that exhibits this problem and I will troubleshoot it. When I ran that code with your earlier example, it ran with no difficulties and preserved all the original data. So, at present, I have no idea why your data is causing errors.

          Comment


          • #20
            I have provided you with two examples of my data, one of which was from the quaterly data set. The example data that you used for your code is from the quaterly data set. Might this be causing the problem?
            Here an example from the second subsample with different portfolios than before.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input long Date double(R_Me TY PE VS EtR_Me N_dr N_cf Rrf FFS2BM1 FFS3BM1 FFS4BM1 FFS5BM1 FFS5BM5 RISK10 RISK14 RISK17 RISK19)
            199011   .058061626  .67 2.9222078 1.6413188  .0035540038   .062132986 -.0076253644 .005849983  .1183  .1051  .0897  .0646  .0435    .12173959     .10203556   .036494877   .095742293
            199012   .023693474  .75 2.9608154 1.6537005  .0090106973     .0178459  -.003163123 .005433334  .0435  .0645   .042  .0325  .0079    .05887135    .048686188   .023024607    .04431314
            199101   .042781392  .96 2.9476627  1.664684  .0055701994 -.0051041534   .042315346 .005116646  .0977  .0935  .0841  .0516  .0395    .11749612    .099675956    .01321588    .09292419
            199102   .068041102 1.06 3.0515163 1.6708693  .0087658785   .097320626  -.038045402 .004933343  .1229  .0924  .0889  .0815    .08    .11978292    .099244337   .077034147   .094222122
            199103   .023502177 1.11 3.0757451 1.6804807   .010210728 -.0010602499   .014351699 .004883342  .0761  .0743  .0481  .0455   .012   .049943459    .028751036   .030019418   .032099285
            199104 -.0013861052 1.23 3.0923817 1.6590837  .0058615677 -.0052015584 -.0020461145 .004741713 -.0132 -.0289 -.0078 -.0001 -.0019   -.01046825   -.016978515  -.015338632   .011341767
            199105   .035248183  1.5  3.084884   1.67436   .004309962  .0082276908    .02271053 .004549997  .0544  .0376  .0658  .0397  .0686   .058022122    .056415447   .043939377   .067192412
            199106  -.049887627 1.42 3.0835875 1.7203005  .0094085029  -.027222457  -.032073673 .004649961 -.0574 -.0516 -.0528 -.0428 -.0395  -.079378644    -.07320167  -.038958906  -.047176416
            199107   .041101192 1.71 3.0866172 1.7329534 .00031467686   .042206667 -.0014201516  .00466666  .0424  .0598  .0591  .0643  .0081   .073272477    .050325015   .076738999   .056630268
            199108   .022064351 1.61 3.1083668 1.7328576   .010546596  .0053573976  .0061603572 .004500037  .0387  .0546  .0166  .0438  .0176   .039886239    .021745287    .03410797   .034028991
            199109  -.015428971 1.23 3.1014802 1.7581476   .007799295  -.014220606 -.0090076597 .004316644  .0064 -.0012 -.0037  -.021 -.0382 -.0010723043   -.029284393 -.0073710169 -.0080010347
            199110   .013373658 1.65 3.0994341 1.7707362  .0016168025   .010040765  .0017160908 .004199968  .0239  .0297  .0253  .0147   .005  .0012886541   .0027192137   .024965763   .040451829
            199111  -.041675451 1.65 3.0957319 1.7764372  .0068914496  -.027300574  -.021266327 .003699999 -.0502 -.0355 -.0244 -.0121 -.0831  -.051966509   -.039022418   -.01342243  -.046044798
            199112    .09856959 1.68 3.1021296  1.801241  .0016835556   .069642376   .027243658  .00312507  .1093  .1452  .1337  .1534  .1286    .14410748     .11005604    .14353534    .12990558
            199201 -.0048462536 1.89 3.1703972 1.7699284    .01469567 -.0038516738   -.01569025      .0032  .0842   .015 -.0026 -.0325  .0675   .030558833    .041570478  -.040628776   .021325619
            199202  .0099079047 1.68 3.1616104 1.7086906  .0056947576  -.019679887   .023893034 .003299991  .0072  .0007 -.0053  .0022  .1012   .036302385     .04335575  .0057724954    .04176327
            199203  -.027359047 1.66 3.1482634 1.6515203  .0066343946  -.046236251   .012242809 .003399936 -.0849 -.0526 -.0563 -.0287   .008  -.041113085   -.028939616  -.026796741  -.022945689
            199204   .010674801 2.12  3.147724 1.6043993  .0038999019 -.0046238379   .011398737 .003075019 -.0707 -.0527 -.0191 -.0011  .0704  -.024273608  -.0060293406     .0116734 -.0067946755
            199205  .0033786935 2.05 3.1650627 1.5754549    .01102512 -.0028647191 -.0047817072 .003125016 -.0035   .002  .0006  .0131 -.0003  .0028963673 -.00037492673   .011265314 -.0079230652
            199206  -.022481149  2.2 3.1478011 1.4847344   .010000813  -.059766014   .027284052 .003058318 -.0539 -.0488 -.0182 -.0308  .0072  -.036795002   -.026061295  -.030658052  -.014513251
            199207   .036516521 2.54 3.1629036 1.4758222  .0099050674   .023913187  .0026982671 .002650003   .031  .0523  .0535  .0486 -.0033   .042440025    .026477158   .051032197   .042972537
            199208  -.023591751 2.47 3.1684525 1.4582289   .017535883  -.037485448  -.003642186  .00261665 -.0401 -.0441 -.0128 -.0058  -.088  -.035956176   -.039910077  -.010938171  -.036942995
            199209  .0099214541 2.72 3.1676465 1.4569487   .011560726  .0012279816 -.0028672533 .002424986  .0205  .0302  .0033  .0083  .0019   .035946488    .029197342 -.0065920437   .031592421
            199210   .008369042 2.67 3.1511369 1.4829317   .016347348  -.020301042   .012322736 .002475039  .0479  .0597  .0529  .0201 -.0013   .061445302    .021498778  .0060441428   .030091503
            199211    .03665324 2.62 3.1737824 1.5110869   .015794764   .027320823  -.006462348 .002725014  .0948  .0946  .0717  .0368  .0852   .085926641    .079620172    .02495823   .060305642
            199212   .014727906 2.61 3.2005716 1.4884234   .017462875 -.0056763363  .0029413675 .002683292  .0159  .0341  .0168 -.0071  .0284   .016971998    .033786524  .0077716184   .040448992
            199301  .0097941412 1.98  3.196606 1.4406005   .015228246  -.024033194   .018599089 .002483298 -.0059 -.0212  .0123 -.0241  .0928   .029637817    .053976633   -.02022988   .024467618
            199302  .0029814606    2 3.2083474 1.3626069   .011387599  -.022881827   .014475688 .002466725 -.0829 -.0805 -.0529 -.0306  .0209  -.035884624    .010879687 -.0027664923 -.0019350573
            199303   .022255393 2.02 3.2238624 1.3478493   .011678315  .0085172658  .0020598123 .002449972  .0113  .0466  .0301  .0067  .0344   .020475056    .028704573  .0081915352   .047276357
            199304  -.028227883 2.18 3.2045645 1.3356704   .013583876  -.052764174   .010952415 .002399998 -.0309 -.0451 -.0484 -.0599  .0115  -.038069128   -.046358244   -.03604462  -.017833153
            199305   .026448772 2.03 3.2060167 1.3699759    .01027856   .027506062   -.01133585 .002550011  .0732  .0761  .0706  .0313 -.0053   .064019802    .058474132   .020602644    .02753563
            end

            Here also the list of all variables in the data set:
            Date R_Me TY PE VS Et(R_Me) -N_dr N_cf Rrf FFS1BM1 FFS1BM2 FFS1BM3 FFS1BM4 FFS1BM5 FFS2BM1 FFS2BM2 FFS2BM3 FFS2BM4 FFS2BM5 FFS3BM1 FFS3BM2 FFS3BM3 FFS3BM4 FFS3BM5 FFS4BM1 FFS4BM2 FFS4BM3 FFS4BM4 FFS4BM5 FFS5BM1 FFS5BM2 FFS5BM3 FFS5BM4 FFS5BM5 RISK1 RISK2 RISK3 RISK4 RISK5 RISK6 RISK7 RISK8 RISK9 RISK10 RISK11 RISK12 RISK13 RISK14 RISK15 RISK16 RISK17 RISK18 RISK19 RISK20

            When I run your code on the above example of my data (199011 to 199305) but containing all variables,the same problem appears and my data is deleted again.
            Last edited by Emilia Kosinska; 17 May 2019, 08:57.

            Comment


            • #21
              Ah, yes. I see the problem.

              The earlier data set was quarterly data, and the code was written specifically for quarterly data.

              This example is monthly data, and the code fails because of that. You will not be able to use the same code on both kinds of data. To get the code to work with monthly data, all you have to do is change -tsset mdate, delta(3)-, to -tsset mdate- (without the -, delta(3)- and it will work just fine on quarterly data (but will no longer work with monthly data).

              If you have the monthly and quarterly data mixed in the same data set, then I think you will need to split that into two separate data sets. While I can think of a way to write code that will work equally well on either, I can't think of a way to do it safely.

              When I developed this code from the quarterly data in #13, I didn't test it out on the monthly data because the -dataex- output from the monthly example was corrupted (take a look and you'll see a lot of "&gt" in there.) So I didn't catch this problem earlier.

              Comment


              • #22
                Thank you so much for your help! I have two separate data sets, the code with -tsset mdate- works well.

                Comment


                • #23
                  Mr. Schechter, one last question.. How can I extract my beta values (beta_cf and beta_dr) for each portfolio and period from the data set containing all variables that you transformed? Is this code correct?

                  sort beta_cf beta_dr
                  quietly by beta_cf beta_dr : gen dup = cond(_N==1,0,_n)
                  drop if dup>1

                  Many thanks in advance!
                  Last edited by Emilia Kosinska; 23 May 2019, 07:28.

                  Comment


                  • #24
                    The code you write should work. But there's an easier way:

                    Code:
                    duplicates drop beta_cf beta_dr, force

                    Comment


                    • #25
                      Thank you!

                      Comment


                      • #26
                        Dear Mr. Schechter,
                        How can I modify the above betas code so that I can calculte the betas for each portfolio in each year of the two subsamples? Shall I manipulate with -delta- or will a new code be needed?

                        Comment


                        • #27
                          I'm not sure I understand what you want here. I'm interpreting it as you want to do separate analyses for each calendar year (as opposed to just lumping all the years in a period defined by a cutoff of July 1963 together.) This requires only minor changes to the code.

                          Code:
                          gen int mdate = ym(floor(Date/100), mod(Date, 100))
                          format mdate %tm
                          assert !missing(mdate)
                          drop Date
                          order mdate, first
                          
                          gen int year = year(dofm(mdate))
                          
                          //    GO TO LONG LAYOUT
                          rename (FFS* RISK*) return=
                          reshape long return, i(mdate) j(portfolio) string
                          
                          capture program drop one_period
                          program define one_period
                              tsset mdate, delta(3)
                              gen delta = N_cf - N_dr
                              summ delta
                              local denom = r(Var)
                              corr return N_cf, cov
                              local cf1 = r(cov_12)
                              corr return L1.N_cf, cov
                              local cf2 = r(cov_12)
                              gen beta_cf = (`cf1' + `cf2')/`denom'
                              corr return N_dr, cov
                              local dr1 = r(cov_12)
                              corr return L1.N_dr, cov
                              local dr2 = r(cov_12)
                              gen beta_dr = (`dr1' + `dr2')/`denom'
                              drop delta
                              exit
                          end
                          
                          runby one_period, by(year portfolio)
                          Note: When I run this in the example data I have previously used, there are 10 portfolio-year combinations where there are insufficient observations to calculate the covariances. There is no output for those 10. This may occur in your data as well. Remember, too, that the version of the code shown above is designed to work with quarterly data (coded as monthly dates with 3 month gaps between consecutive observations).

                          Comment


                          • #28
                            That is exactly what I wanted to do. I run the code with monthly data (without delta(3)) and it works well. Many thanks!

                            Comment


                            • #29
                              I'm doing the same exercise, with the same data and everything,
                              after all that what I wanted to do is to organize these 45 portfolios in a square matrix:
                              columns "1=Growth=the lowest BE/ME portfolio" 2 3 4 "5=Value=the highest BE/ME" DIFF=is the difference between the extreme cells of a particular column
                              Rows "Small=1= the lowest ME 2 3 4 "5=Large =the highest ME"DIFF=is the difference between the extreme cells of a particular row
                              twice, once for Beta_CF and second for Beta_DF

                              can you help me with that

                              Comment


                              • #30
                                I'm trying to organize my portfolios : I have 25 BEME portfolios and 20 RISK portfolio,
                                so I'm trying to organize only the BEME portfolios in a [5*5] square matrix based on size of Beta_cf

                                Comment

                                Working...
                                X