Announcement

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

  • Optimize in Mata to solve for 2 unknwons (Implied volatility - Merton)

    Hi all,
    I tried to follow the following post (http://www.stata.com/statalist/archi.../msg01213.html) to solve for 2 unknows. However, Mata did not work.
    I get this error code:
    numerical derivatives are approximate
    flat or discontinuous region encountered
    Iteration 0: f(p) = 1.029e+13 (not concave)
    could not calculate numerical derivatives -- discontinuous region with missing values
    encountered
    could not calculate numerical derivatives -- discontinuous region with missing values
    encountered
    r(430);
    Could someone tell me the problem?
    Here is the code I used (following the link above):
    Code:
      
        mata
        mata clear
        j=1
    
        T = st_data(j,"time")
        sqrtT = sqrt(T)
        rf= st_data(j,"r") /*risk free rate*/
        F = st_data(j, "x") /*debt*/
        E = st_data(j, "ve") /*Equity*/
        Evol = st_data(j, "sige")  /*Volatility of equity*/
        Avol = Evol/2 /*Asset volatility*/
        Avalue = (E + F)
    
        
        c = (sqrtT, rf, E, Evol, F)
    
        void Merton(todo, A, c, lnf, g, H)
                {
                    d1=((ln(A[1]/c[5]))+(c[2]+0.5*(A[2]^2)*c[1]^2))/(A[2]*c[1])
                    d2=d1-A[2]*c[1]
                    f1=A[1]*normal(d1)- exp(-c[2]*c[1]^2)*c[5]*normal(d2)-c[3]
                    f2=(A[1]/c[3])*normal(d1)*A[2]-c[4]        
                    lnf =(f1)^2 + (f2)^2
            }
            
        while (j<=st_nobs()){
            if (T!=. & rf!=. & F!=. & E!=. & Evol!=.) {
            
                S = optimize_init()
                optimize_init_evaluator(S, &Merton())
                optimize_init_evaluatortype(S, "d0")
                optimize_init_params(S, (Avalue,Avol))
                optimize_init_which(S, "min")
                optimize_init_argument(S,1,c)
                A = optimize(S)
                
                st_store(j, "va", A[1])
                st_store(j, "siga", A[2])
                
                j++
                
                if (j<=st_nobs()){
                    T = st_data(j,"time")
                    sqrtT = sqrt(T)
                    rf= st_data(j,"r")
                    F = st_data (j, "x")
                    E = st_data (j, "ve")
                    Evol = st_data (j, "sige")
                    Avol = Evol/2
                    Avalue = (E + F)
                    c = (sqrtT, rf, E, Evol, F)
                    }
                
                }
            else {
                j++
                
                if (j<=st_nobs()){
                    T = st_data(j,"time")
                    sqrtT = sqrt(T)
                    rf= st_data(j,"r")
                    F = st_data (j, "x")
                    E = st_data (j, "ve")
                    Evol = st_data (j, "sige")
                    Avol = Evol/2
                    Avalue = (E + F)
                    c = (sqrtT, rf, E, Evol, F)
                    }
    
                }
        }
    end
    I also provide a part of my data
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(id quarter ve x va sige siga time r)
     29 107    156015   3217961   3373976  .2379536  .01100314 1 5.78
    252 107  666777.9  14623456  15290234 .15519053 .006767562 1 5.78
     36 107  16589.25    177392 193981.25  .1666332  .01425045 1 5.78
    352 107   1115694  20235900  21351594 .28725544  .01501008 1 5.78
     52 107     14706    208718    223424  .3383645 .022271506 1 5.78
    304 107  205669.5   2213467 2419136.5  .3440633 .029251486 1 5.78
     17 107   2253376 100151000 102404376  .6424432  .01413676 1 5.78
    210 107  682592.8  11802700  12485293 .19676542  .01075751 1 5.78
    255 107    118261   2071609   2189870   .534631 .028872034 1 5.78
     51 107      7254    153226    160480  .6672292 .030160025 1 5.78
     12 107   2681256  59731156  62412412  .1567459  .00673385 1 5.78
    213 107 511008.75   5560495   6071504  .1920298 .016162207 1 5.78
    258 107    427917   5432689   5860606  .1897896 .013857645 1 5.78
    257 107    223184   2017193   2240377  .2317123 .023082934 1 5.78
     11 107 277163.25   4728359   5005522  .2698155 .014940088 1 5.78
    351 107   1447675  26594912  28042588  .1726921 .008915085 1 5.78
     30 107   14007.5    166464  180471.5  .6742408  .05233197 1 5.78
      7 107  565389.5   6306171   6871561  .2142143 .017625475 1 5.78
    143 107  584993.5   3152067 3737060.5 .17075734  .02673008 1 5.78
    281 107    102225    872728    974953  .3628784  .03804824 1 5.78
    474 107    448812   7659290   8108102 .16709583 .009249343 1 5.78
     15 107 277655.75   3422593   3700249  .2908917 .021827655 1 5.78
    243 107     27829    359649    387478 .20803265 .014941082 1 5.78
    481 107    426330   4813621   5239951  .1843567  .01499953 1 5.78
    435 107   1452733  16219592  17672324 .10972014 .009019417 1 5.78
    136 107   1248167  13226869  14475036 .16186854 .013957753 1 5.78
    219 107    230974   2350453   2581427  .5296666  .04739209 1 5.78
      3 107  578070.5  30760992  31339062  .4605464 .008495094 1 5.78
    256 107    434448   6140086   6574534 .22234038 .014692347 1 5.78
    140 107    428750   4045679   4474429  .2242697 .021490036 1 5.78
    285 107 144586.75   3169062   3313649  .4308943  .01880151 1 5.78
     21 107  376605.5   2980117 3356722.5  .3518122  .03947137 1 5.78
     55 107    139468   1009128   1148596  .2419881  .02938335 1 5.78
    216 107    409838   1359806   1769644  .8994959   .2083174 1 5.78
    168 107  439702.9   5580319   6020022  .3094057  .02259902 1 5.78
     26 107   31421.5    483614  515035.5    .45161  .02755201 1 5.78
    470 107 450899.25   5196205   5647104  .1730482 .013817223 1 5.78
    307 107   1450967  21320772  22771740 .18380323 .011711553 1 5.78
    171 107 475682.75   4431634   4907317 .24364486  .02361732 1 5.78
    208 107 1655656.3  19195596  20851252  .2329672 .018498342 1 5.78
     66 107    7845.5    112477  120322.5 .14465873 .009432318 1 5.78
    471 107 499913.75   7364255   7864169 .13235593 .008413673 1 5.78
     81 107   1896017  70631000  72527016 .14298674 .003737991 1 5.78
    138 107   2715116  25279808  27994924 .15011474 .014559031 1 5.78
    181 107 408944.25   5920820   6329764  .2046095 .013219115 1 5.78
    354 107  49577.75    517769 567346.75  .2185538  .01909838 1 5.78
    318 107   1401400  13648643  15050043 .15599944 .014526046 1 5.78
     20 107 2723346.5  42234400  44957748 .16622637 .010069277 1 5.78
     28 107  646930.1  10158831  10805761 .16921726 .010130868 1 5.78
    263 107    222546   2430035   2652581 .21503285 .018040806 1 5.78
    173 107   2657369  25255164  27912532  .1785632 .016999831 1 5.78
    172 107    721400   8971612   9693012 .13001361  .00967623 1 5.78
    167 107  767894.3   8847793   9615687  .2367543  .01890684 1 5.78
    217 107 362631.25   4032297   4394928  .3216273 .026537884 1 5.78
     33 107    330225   3048914   3379139  .2574757  .02516171 1 5.78
    224 107 161438.75   3438277   3599716  .3670127 .016459651 1 5.78
    130 107     31500   3426505   3458005  .6199373 .005647194 1 5.78
    300 107    557100   7207298   7764398  .1726348 .012386644 1 5.78
    215 107    611478   5557419   6168897  .1673322 .016586425 1 5.78
    419 107    120950   1086859   1207809 .36213875 .036264576 1 5.78
     24 107   10323.5    195830  206153.5  .8793883  .04403692 1 5.78
    119 107  112477.5   2445150 2557627.5  .4515339 .019857235 1 5.78
     35 107     24340    234498    258838 .53807527  .05059826 1 5.78
    467 107  583627.6   8594064   9177692  .2155595 .013707856 1 5.78
    132 107    596520   7246920   7843440  .2475891 .018829985 1 5.78
    462 107    457872   6623391   7081263  .2306818 .014915806 1 5.78
     10 107   2444780  52661988  55106768 .22596245 .010024694 1 5.78
     88 107 2880067.5  89883520  92763584 .26011217 .008075804 1 5.78
    234 107  3478.125    143534 147012.13  .1591195 .003764571 1 5.78
    118 107  355936.5   4951378   5307315 .15332127 .010282532 1 5.78
    306 107 1195933.5  10919692  12115626  .1602834 .015821574 1 5.78
    223 107    259175   2798318   3057493 .20509467 .017385293 1 5.78
    131 107  632517.5   8493690   9126208 .13318835 .009230994 1 5.78
    232 107  203362.5    838757 1042119.5  .6223659  .12145045 1 5.78
    254 107    141787   1285280   1427067 .26094636 .025926465 1 5.78
     77 107   3176188  53698644  56874832  .2297719 .012831664 1 5.78
      1 107 450933.25   4596164   5047097 .13559069  .01211436 1 5.78
    225 107 108647.25   1035460 1144107.3 .26390937 .025061486 1 5.78
    174 107 1713614.5  26163570  27877184 .27529234  .01692226 1 5.78
    214 107 379764.25   3543081   3922845 .28074995 .027178945 1 5.78
    468 107 343685.25   3614569   3958254  .3819469 .033163484 1 5.78
    305 107   1695485  32276100  33971584 .27038696  .01349472 1 5.78
      4 107   1584700  16328863  17913564 .15386647 .013611597 1 5.78
     78 107 1166016.8  19656332  20822348  .2071956 .011602607 1 5.78
    272 107    107010    597080    704090  .3012076  .04577855 1 5.78
    221 107 378810.25   3199263   3578073   .227398   .0240746 1 5.78
    368 107 181520.88   4420669   4602190  .3450024 .013607684 1 5.78
     89 107   7293595 187064000 194357600  .2103857 .007895076 1 5.78
    273 107  234148.5   1211189 1445337.5 .29162902  .04724467 1 5.78
     84 107   7340850  70908504  78249352 .17887387 .016780794 1 5.78
    270 107     98091   1030902   1128993  .5040353  .04379241 1 5.78
     19 107 223842.75   3097537   3321380 .18835774 .012694277 1 5.78
    137 107    147805   2348109   2495914  .4005369 .023719307 1 5.78
    469 107    401184   7352823   7754007  .1525159  .00789101 1 5.78
    211 107 36224.375    260018 296242.38 .11759332 .014379255 1 5.78
    212 107  703526.1   4007575   4711101  .2332856 .034837402 1 5.78
     86 107     85190    535826    621016  .3718926  .05101564 1 5.78
    260 107 125510.25   1647974 1773484.3   .316285  .02238363 1 5.78
    402 107 1952828.3  17512120  19464948 .18991856  .01905365 1 5.78
    276 107    102543    523980    626523 .26249704  .04296288 1 5.78
    end
    format %tq quarter

  • #2
    I think solvenl() is better suited to solve systems of non-linear equations (if your version of Stata has it). The routine solvenl() did not exist at the time the posts you're referring to were posted.

    Comment

    Working...
    X