Announcement

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

  • Implied Cost of Equity, mm_root function skips the results of some observations

    Hi There,

    I have used the following codes to calculate the implied cost of equity. While the code calculates implied cost equity correctly for most of the observations, it somehow provides no results for some other ( which I can otherwise solve using excel solver).


    I have a rudimentary knowledge of Mata so I was unable to debug the code or pick up the mistake that I have made.

    The following code is used to calculate the implied cost of Equity

    Code:
    mata: mata clear
    
    gen dv= dpr
    
    gen ii=inds_roe
    gen b1 = b0*(1-dv)
    gen b2 = b1*(1-dv)
    gen f1 = eps1/b0
    gen f2 = eps2/b1
    gen f3 = eps3/b2
    gen fi = (ii-f3)/9
    
    
    generate glsicc=.
    mata
    z=J(1,1,.)
    st_view(z,., "glsicc p b0 f1 f2 f3 fi ii dv")
    function w(x,p,b0,f1,f2,f3,fi,ii,dv) {
    
    f4 = f3+fi
    f5 = f4+fi
    f6 = f5+fi
    f7 = f6+fi
    f8 = f7+fi
    f9 = f8+fi
    f10 = f9+fi
    f11 = f10+fi
    f12 = f11+fi
    
    t1 = (f1-x)/(1+x)*b0
    t2 = ((f2-x)/(1+x)^2)*(b0*(1-dv)^2)
    t3 = ((f3-x)/(1+x)^3)*(b0*(1-dv)^3)
    t4 = ((f4-x)/(1+x)^4)*(b0*(1-dv)^4)
    t5 = ((f5-x)/(1+x)^5)*(b0*(1-dv)^5)
    
    t6 = ((f6-x)/(1+x)^6)*(b0*(1-dv)^6)
    t7 = ((f7-x)/(1+x)^7)*(b0*(1-dv)^7)
    t8 = ((f8-x)/(1+x)^8)*(b0*(1-dv)^8)
    t9 = ((f9-x)/(1+x)^9)*(b0*(1-dv)^9)
    t10 =((f10-x)/(1+x)^10)*(b0*(1-dv)^10)
    t11 =((f11-x)/(1+x)^11)*(b0*(1-dv)^11)
    t12 = ((f12-x)/(x*(1+x)^11))*(b0*(1-dv)^12)
    
    return(-p+b0+t1+t2+t3+t4+t5+t6+t7+t8+t9+t10+t11+t12)    
      
      }
    for (i=1;i<=rows(z);i++) {
        r=mm_root(glsicc=.,&w(),smallestdouble(),1-epsilon(1),1e-9,1000,z[i,2],z[i,3],z[i,4],z[i,5],z[i,6],z[i,7],z[i,8],z[i,9])
        z[i,1]=glsicc
    }
    end

    The variable of interest is calculated is the implied cost of equity / "glsicc" ( last column). In some instances, as seen in row 8, 9, and 22 of the following table, there is no result/output from the calculation ( no data is missing as well).

    Your comments and feedback are highly appreciated.

    Thanks.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long gvkey int year double uniqueid float(p ltg_ b0 eps0 eps1 eps2 eps3 eps4 eps5 dps rf rf_ inds_roe dpr dv ii b1 b2 f1 f2 f3 fi glsicc)
     9899 2011  99000000   31.17  .0387 17.811 3.68 2.36 2.53     2.72      2.87      2.96 1.72 3.18 .0318   .352657   .467391   .467391   .352657 9.4862995  5.052488 .13250238  .26670042  .5383486  -.020632403  .0004840947
     9899 2017  99000000   38.87   .079 22.944 2.35 2.91 2.95     2.96       2.7      3.33 1.96 2.31 .0231   .352657   .834043   .834043   .352657  3.807717  .6319172 .12683055   .7747425 4.6841583    -.4812779 9.313226e-10
     9899 2018  99000000   32.47  .1171 25.281 2.08  3.4 3.46     3.49      3.65      3.74    2 2.85 .0285   .352657   .961538   .961538   .352657  .9723574 .03739879 .13448836  3.5583625  93.31852    -10.32954 9.313226e-10
    10115 2001 101152001  28.985     .2  2.592  .51  .67   .8      .96     1.152    1.3824  .04 5.42 .0542  .5049713 .07843138 .07843138  .5049713  2.388706 2.2013564 .25848767   .3349094  .4360948   .007652951   .019291675
    10115 2002 101152002   26.35     .2  3.661  .63  .83    1        1       1.2      1.44  .05 4.86 .0486  .5049713 .07936508 .07936508  .5049713 3.3704445  3.102949   .226714  .29669678  .3222741   .020299694   .029906055
    10115 2018 101152018  175.46  .1003  31.33 4.69 7.23 7.91     8.66      9.52     10.27 1.88 2.85 .0285  .5049713  .4008529  .4008529  .5049713 18.771278 11.246758 .23076923  .42138845  .7699997  -.029447595 .00028093872
    10115 2019 101152019  183.02  .1057 34.198 5.44 8.15 8.95     9.81     10.65     11.05 2.08    2   .02  .5049713 .38235295 .38235295  .5049713 21.122295 13.046124   .238318   .4237229  .7519475    -.0274418  .0004394989
    10121 2015 101212015   19.34   .076  11.39 2.34  2.2 2.33     2.41   2.59316   2.79024   .6 2.35 .0235 2.8803954 .25641024 .25641024 2.8803954  8.469487  6.297824  .1931519   .2751052  .3826719    .27752483            .
    10121 2018 101212018    14.3 -.0364 14.247  1.8 1.89 1.94     1.98  1.907928 1.8384794  .64 2.85 .0285 2.8803954  .3555556  .3555556 2.8803954  9.181399  5.916902 .13265951  .21129677  .3346346     .2828623            .
    10124 2005 101242005    8.29     .1  4.146  .24   .4   .5      .55      .605     .6655   .4 3.94 .0394   .221887 1.6666666 1.6666666   .221887    -2.764 1.8426664 .09647854 -.18089727  .2984805  -.008510389  .0016042346
    10353 2010 103532010   13.95   .099  9.894  .97  .83 1.11      1.4    1.5386 1.6909214    0 2.97 .0297  .1687773         0         0  .1687773     9.894     9.894 .08388922   .1121892 .14149989   .003030822    .10466353
    10353 2011 103532011   21.87     .1 11.203  .77 1.17 1.49     1.76     1.936    2.1296    0 3.18 .0318  .1687773         0         0  .1687773    11.203    11.203  .1044363  .13300009  .1571008  .0012973895     .0811295
    10390 2009 103902009   24.43    .15  9.933  .28  .36   .4      .46      .529    .60835    0 3.53 .0353 .03219657         0         0 .03219657     9.933     9.933 .03624283  .04026981 .04631028 -.0015681903   .013515483
    10405 2000 104052000 22.5625  .1238 12.935 2.38 1.86 2.16 2.427408  2.727921  3.065638  .64 6.03 .0603 .24846035 .26890758 .26890758 .24846035  9.456681  6.913708  .1437959  .22840995  .3511007  -.011404485   .024919184
    10405 2001 104052001   20.75  .0922 11.762 1.67  .83 1.54 1.681988 1.8370672  2.006445   .8 5.42 .0542 .24846035  .4790419  .4790419 .24846035  6.127509 3.1921754 .07056623   .2513256 .52690965   -.03093881  .0002060111
    10405 2006 104052006   65.74     .2 14.583 2.79 4.07 5.06     5.98       4.7      3.32   .4 5.15 .0515 .24846035 .14336918 .14336918 .24846035 12.492248 10.701244  .2790921   .4050512  .5588135  -.034483686    .01778913
    10405 2007 104052007  116.54    .15 21.708 5.15 7.86 8.65     8.85     10.95   12.5925  .52 5.03 .0503 .24846035 .10097087 .10097087 .24846035 19.516125 17.545565  .3620785   .4432232   .504401  -.028437853   .022462074
    10405 2011 104052011      65   .481 23.274  .94 3.04 4.64     5.63      7.64  11.31484  .72 3.18 .0318 .24846035  .7659575  .7659575 .24846035  5.447106 1.2748544 .13061786   .8518285 4.4161906   -.46308115 4.290278e-09
    10405 2012 104052012   30.33  .3716 23.088 1.83 2.63    4     5.14  7.050024  9.669813  .72 1.67 .0167 .24846035  .3934426  .3934426 .24846035 14.004196  8.494349   .113912  .28562868  .6051082   -.03962754   .070960805
    10405 2013 104052013   27.77  .1199 26.802 1.61 1.08 2.43     3.53  3.953247 4.4272413  .72 2.52 .0252 .24846035   .447205   .447205 .24846035 14.816012  8.190218  .0402955  .16401175   .431002    -.0202824    .10319096
    10405 2015 104052015   32.08    .15 19.078 1.39 1.18 2.59     4.04     4.646    5.3429  .72 2.35 .0235 .24846035  .5179856  .5179856 .24846035   9.19587 4.4325423 .06185135  .28164816  .9114408  -.073664494 .00010481177
    10405 2019 104052019   21.96   .183 16.577 1.29 1.49 2.31     2.75   3.25325  3.848595    0    2   .02 .24846035         0         0 .24846035    16.577    16.577 .08988357   .1393497  .1658925   .009174204            .
    10407 2000 104072000 36.5625  .1633 17.886 2.48  2.8 3.13 3.641129 4.2357254  4.927419  .52 6.03 .0603  .2592632  .2096774  .2096774  .2592632  14.13571  11.17177 .15654702  .22142504  .3259223  -.007406566   .030442247
    10407 2001 104072001   48.43  .1486 19.762 2.76 2.82 3.16     3.49  4.008614  4.604294   .6 5.42 .0542  .2592632  .2173913  .2173913  .2592632 15.465913 12.103758 .14269811   .2043203  .2883402 -.0032307804     .0149684
    10407 2002 104072002   55.87  .1467 22.694 2.93 3.24 3.65 4.185455  4.799461  5.503542  .68 4.86 .0486  .2592632  .2320819  .2320819  .2592632 17.427134  13.38261 .14276901   .2094435  .3127532  -.005943338   .012218882
    end
    To replicate the calculation just drop the following variables first

    Code:
    drop dv ii b1 b2 f1 f2 f3 fi glsicc
    Last edited by Bilal Hafeez; 17 Aug 2020, 00:18. Reason: mm_root

  • #2
    I don't have the faintest idea about the substance of your program. But you haven't any other suggestions, so let me offer this, in the hopes that reflecting on it will help you find a way around your problem.

    Since your tolerance for the solution is 1e-9 perhaps if you replace the lower bound of smallestdouble() with something like 1e-20 you won't have the problem you currently have, which seems to be the result of trying to evaluate the expression for the returned value with x taking the value of smallestdouble().

    Replacing
    Code:
    return(-p+b0+t1+t2+t3+t4+t5+t6+t7+t8+t9+t10+t11+t12)
    with
    Code:
    if (result==.) {
        "missing result"
        "x,p,b0,f1,f3,f3,fi,ii,dv"
        (x,p,b0,f1,f3,f3,fi,ii,dv)
        "t1 to t12"
        (t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12)
        "f12-x,x,1+x,x*(1+x)^11,b0,1-dv"
        (f12-x,x,1+x,x*(1+x)^11,b0,1-dv)
    }
    shows, among other things, that the result is missing when t12 is missing. Assuming your expression for t12 is correct, the missing t12 is likely a consequence of the value of x being smallestdouble() when the expression is being evaluated.

    Comment

    Working...
    X