Announcement

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

  • How to solve equations in Stata?

    For example, I have a equation:

    1/((1+p)^30)*10,000=100

    I need to solve p. How to do it in Stata?

  • #2
    Code:
     mata: mata clear  
    mata 
    void eval0(todo, p, v, g, H)
     {
        v = (100-  10000/((1 + p)^30))^2
    }
        S = optimize_init()
        optimize_init_which(S,  "min")
        optimize_init_conv_ptol(S, 1e-12)
        optimize_init_conv_vtol(S, 1e-12)
        optimize_init_evaluator(S, &eval0())
    
        optimize_init_params(S, (0))
        p= optimize(S)
        p
        test = 10000/((1 +p)^30)
        test
            
    end

    Comment


    • #3
      Originally posted by Yao Zhao View Post
      For example, I have a equation:

      1/((1+p)^30)*10,000=100

      I need to solve p. How to do it in Stata?
      Alternatively, you could enter that equation into Wolfram Alpha. If you enter the problem as typed, it will know to solve for p automatically.
      Be aware that it can be very hard to answer a question without sample data. You can use the dataex command for this. Type help dataex at the command line.

      When presenting code or results, please use the code delimiters format them. Use the # button on the formatting toolbar, between the " (double quote) and <> buttons.

      Comment


      • #4
        There is also a solution in the negative range, -2.165914401. I recommend Ben Jann's mathematical function mm_root() in moremata (SSC).

        Code:
        mata: mata clear  
        mata 
        function myfunc(p) return( 1 / ((1 + p) ^ 30) * 10000 - 100)
        
        mm_root(p=., &myfunc(), 0, 100, 0, 1000)  // range 0 to +100
        p  // .1659144012
        
        mm_root(p=., &myfunc(), -100, 0, 0, 1000)  // range -100 to 0
        p  // -2.165914401
        
        end
        It would be nice to get both solutions in the same call, e.g., to only have to run mm_root() once, but I do not know how to do that.

        Comment


        • #5
          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

          Working...
          X