Announcement

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

  • Stata to Mata : The case of Optimize functions

    I am just trying to learn the optimize() function, but cannot find a way to get things going. Let us assume that have
    a = ?
    b = ?

    I have 10 observations and want to find the values of a and b for each observation. Presently, the code uses the equation 1 value of 174 and equation 2 of 22 manually inserted into the Mata code, which produce a = 10, b = 12; but how can I automate the process so that the Stata data is read into the Mata code and get back the results of a and b from the Mata code into Stata.

    Code:
    clear
    input float(eq1 eq2)
    174 22
     60 14
    252 24
      .  .
      .  .
      .  .
      .  .
      .  .
      .  .
      .  .
    end
    So the optimize function that I have is given below:

    Code:
     clear mata
     mata
    
     void mysolver(todo, p, lnf, S, H)
       {
         a   = p[1]
         b   = p[2]
         lnf = ((b^2)+(3*a)-174)^2\   
                (a+b-22)^2   
        }
    
     S = optimize_init()
    
     optimize_init_evaluator(S, &mysolver())
    
     optimize_init_evaluatortype(S, "v0")
    
     optimize_init_params(S, (1,1)) 
    
     optimize_init_which(S,  "min" )
    
     optimize_init_tracelevel(S,"none")
    
     optimize_init_conv_ptol(S, 1e-16)
    
     optimize_init_conv_vtol(S, 1e-16)
    
     p = optimize(S)
    
    p
              1    2
        +-----------+
      1 |  10   12  |
        +-----------+         
    
     end

  • #2
    For now, the immediate solution is given below. I shall write a blog post to provide more details later on

    Code:
    clear
    input float(eq1 eq2)
    174 22
     60 14
    252 24
    end
    
    clear mata
     mata
    
     void mysolver(todo, p, real scalar eq1, real scalar eq2, lnf, S, H)
       {
         a   = p[1]
         b   = p[2]
         
         lnf = ((b^2)+(3*a)-eq1)^2 \   
                (a+b-eq2)^2   
        }
    
    EQ1 = st_data(., "eq1")
    EQ2 = st_data(., "eq2")
    
    for (i = 1; i<= 3; ++i){
    
       eq1 = EQ1[1]
    
       eq2 = EQ2[1]
    
      S = optimize_init()
    
      optimize_init_argument(S, 1, eq1)
    
      optimize_init_argument(S, 2, eq2)
    
     optimize_init_evaluator(S, &mysolver())
    
     optimize_init_evaluatortype(S, "v0")
    
     optimize_init_params(S, J(1,2,1)) 
    
     optimize_init_which(S,  "min" )
    
     optimize_init_tracelevel(S,"none")
    
     optimize_init_conv_ptol(S, 1e-16)
    
     optimize_init_conv_vtol(S, 1e-16)
    
     p = optimize(S)
    
     p
    }
           1    2
        +-----------+
      1 |  10   12  |
        +-----------+
           1   2
        +---------+
      1 |  8   6  |
        +---------+
            1    2
        +-----------+
      1 |   9   15  |
        +-----------+
    
    
    
     end
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      I forgot to change the constants to variables, so you need to change this
      Code:
      eq1 = EQ1[1]
      eq2 = EQ2[1]
      to this
      Code:
      eq1 = EQ1[i]
      eq2 = EQ2[i]
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment


      • #4
        I have the following function: Y=R(R-X), the Y and X are known and stated as variables in my dataset with one observation per year. I need to calculate the R for every year, and R has to be non-negative. I am trying to get the implied cost of capital defined by R Easton(2004) method. I know how to do it manually but it will take a lot of time, so i am wondering whether there is a stata function to solve this easily?

        Comment


        • #5
          I have developed such codes details here https://fintechprofessor.com/complet...1-stata-excel/, however, the code is a paid resource.
          Regards
          --------------------------------------------------
          Attaullah Shah, PhD.
          Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
          FinTechProfessor.com
          https://asdocx.com
          Check out my asdoc program, which sends outputs to MS Word.
          For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

          Comment

          Working...
          X