Announcement

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

  • Solvenl - Optimize for 2 unknown variables (in Mata)

    Hi all,
    i try to solve the Black-Scholes-Merton formula for two unknwons: Asset Value & Asset Volatility.

    My basic code is from this topic:
    https://www.statalist.org/forums/for...atility-merton

    but i want to solve it with "solvenl"

    I have problems with the syntax of solvenl. Espacially i don't know how i can change my code the Avalue & Avol change to set lnf to zero (or near zero).

    My error message is:

    Solver(): 3001 expected 2 arguments but received 4
    solvenl__calluser_2(): - function returned error
    solvenl__functioneval(): - function returned error
    solvenl_solve__broydenpowell(): - function returned error
    _solvenl_solve(): - function returned error
    solvenl_solve(): - function returned error
    <istmt>: - function returned error


    With my code

    Code:
    clear*
    set obs 2
    gen ev = 100 
    replace ev = 120 in 2
    gen evola = .15 
    replace evola = .13 in 2
    gen debt = 50 
    replace debt = 70 in 2
    gen r = .05 
    replace r = .045 in 2
    gen t = 1
    gen Avalue = .
    gen avol = .
    
    mata
    mata clear
    j=1
     T = st_data(j,"t")
     sqrtT = sqrt(T)
     rf= st_data(j,"r")
     F = st_data(j, "debt")
     E = st_data(j, "ev")
     Evol = st_data(j, "evola")
     Avol = Evol*(E/(E+F))
     Avalue = (E + F)
     //A = (Avalue, Avol)
     //c = (sqrtT, rf, E, Evol, F)
    
            void function Solver(Avol, Avalue) ///
      
      {
    
                                    //d1=(ln(Avalue/F)+(rf+0.5*(Avol)^2)*(sqrtT)^2)/(Avol*sqrtT)
                                    //d2=d1-Avol*sqrtT
                                    //E_calc=Avalue*normal(d1)- exp(-rf*(sqrtT)^2)*F*normal(d2)
                                    //Evol_calc=(Avalue/E_calc)*normal(d1)*Avol
                                    //lnf =(E_calc/E-1)^2 + (Evol_calc/Evol-1)^2
                                    
                                    lnf=    (Avalue*normal((ln(Avalue/F)+(rf+0.5*(Avol)^2)*(sqrtT)^2)/(Avol*sqrtT))- ///
                                            exp(-rf*(sqrtT)^2)*F* ///
                                            normal((ln(Avalue/F)+(rf+0.5*(Avol)^2)*(sqrtT)^2)/(Avol*sqrtT)-Avol*sqrtT)-E-1)^2+ ///
                                            ((Avalue/E_calc)*normal((ln(Avalue/F)+(rf+0.5*(Avol)^2)*(sqrtT)^2)/(Avol*sqrtT))*Avol-Evol-1)^2
                                    
                    }
    
    
                            S = solvenl_init()
                            solvenl_init_argument(S,1,Avalue)
                            solvenl_init_argument(S,2,Avol)
                            solvenl_init_evaluator(S, &Solver())
                            
                            solvenl_init_numeq(S, 1)
                            solvenl_init_type(S, "zero")
                            
                            solvenl_init_conv_maxiter(S, 100)
                               ERR=solvenl_result_error_code(S)
                            //solvenl_init_iter_log(S,"off)
                            //_solvenl_solve(S)
                            //solvenl_dump(S)
                            A = solvenl_solve(S)
    
                            st_store(j, "Avalue", A[1])
                            st_store(j, "avol", A[2])
    
                            
        
    end
    Can someone please shortly explain which arguments i should use and how i can change the function to min lnf?
    Thank you very much!

    I got my solvenl-knowlege from
    https://www.statalist.org/forums/for...venl-arguments
    and
    https://www.stata.com/help13.cgi?mf_solvenl
    althoug i use Stata 14.


Working...
X