Announcement

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

  • Non linear equation in STATA

    Hello,

    I am looking for the code to solve for t(p):

    e^t + t = p

    for values p=1 and p = 4.

    Thanks in advance!

  • #2
    So you want to solve the equations
    Code:
    et + t = 1
    and
    Code:
    et + t = 4
    For the first, the solution is t=0.

    For the second, the solution is t=1.0737289375564990871

    Both solutions were obtained using the WolframAlpha site, by typing the equation into the query box, and in the second case, clicking on the "approximate form" and "more digits" buttons in the "real solution" results box.
    Last edited by William Lisowski; 20 Dec 2020, 09:37.

    Comment


    • #3
      Originally posted by William Lisowski View Post
      So you want to solve the equations
      Code:
      et + t = 1
      and
      Code:
      et + t = 4
      For the first, the solution is t=0.

      For the second, the solution is t=1.0737289375564990871

      Both solutions were obtained using the WolframAlpha site, by typing the equation into the query box, and in the second case, clicking on the "approximate form" and "more digits" buttons in the "real solution" results box.
      William,

      Thank you. I am aware of that site and other ways to solve the equation. I was looking for ways to solve it using STATA program such as nl. But I am completely clueless about how to do that.

      Best regards.

      Comment


      • #4
        There is nothing built-in in Stata for this, as far as I know. However, for a simple function that has a simple derivative, like this one, it is easy to iterate an approximate solution using the Newton-Raphson method:

        Code:
        . clear*
        
        .
        . scalar t = 1
        
        . scalar y = exp(t) + t - 4
        
        .
        . while abs(y) > 1e-6 {
          2.      scalar dydt = exp(t) + 1
          3.     scalar t = t - y/dydt
          4.     scalar y = exp(t) + t - 4
          5.     display t, y
          6. }
        1.0757657 .0080029
        1.0737305 6.069e-06
        1.0737289 3.495e-12
        There is nothing magical about 1e-6 as the degree of approximation. Pick what you like (within reason)

        Comment


        • #5
          Note that help nl tells us

          nl fits an arbitrary nonlinear regression function by least squares.
          whereas as both Clyde and I note, this is an equation that admits to an exact solution. Clyde's solution is a straightforward application of freshman calculus.

          In general least squares is not helpful when there is no "error" to minimize, so nl would seem to be of little use.
          Code:
          . set obs 1
          number of observations (_N) was 0, now 1
          
          . generate p = 4
          
          . generate e = exp(1)
          
          . nl (p = e^{t} + {t})
          (obs = 1)
          
          p has zero variance
          r(498);
          But as a learning exercise, if we choose two distinct values of p close to each other, we can persuade nl to give us an approximation.
          Code:
          . set obs 2
          number of observations (_N) was 0, now 2
          
          . generate p = 3.995 + .010*(_n==2)
          
          . generate e = exp(1)
          
          . nl (p = e^{t} + {t})
          (obs = 2)
          
          Iteration 0:  residual SS =  7.815957
          Iteration 1:  residual SS =  .1346935
          Iteration 2:  residual SS =  .0001234
          Iteration 3:  residual SS =    .00005
          Iteration 4:  residual SS =    .00005
          
          
                Source |      SS            df       MS
          -------------+----------------------------------    Number of obs =          2
                 Model |          0          0           .    R-squared     =     0.0000
              Residual |     .00005          1  .000050002    Adj R-squared =     0.0000
          -------------+----------------------------------    Root MSE      =   .0070712
                 Total |     .00005          1  .000050002    Res. dev.     =  -15.51742
          
          ------------------------------------------------------------------------------
                     p |      Coef.   Std. Err.      t    P>|t|     [95% Conf. Interval]
          -------------+----------------------------------------------------------------
                    /t |   1.073729   .0012735   843.13   0.001     1.057548     1.08991
          ------------------------------------------------------------------------------
            Parameter t taken as constant term in model & ANOVA table
          Or we can pose it as an optimization problem to be solved using mata to minimize the difference between p and et+t, which we do by maximizing the negative of the square of the difference.
          Code:
          clear all
          mata:
          void myeval(todo, t,  y, g, H)
           {
                   y = - ( exp(t) + t - 4)^2
           }
          S = optimize_init()
          optimize_init_evaluator(S, &myeval())
          optimize_init_params(S, 0)
          x = optimize(S)
          x
          end
          Code:
          . clear all
          
          . mata:
          ------------------------------------------------- mata (type end to exit) -----------------------
          : void myeval(todo, t,  y, g, H)
          >  {
          >          y = - ( exp(t) + t - 4)^2
          >  }
          note: argument todo unused
          note: argument g unused
          note: argument H unused
          
          : S = optimize_init()
          
          : optimize_init_evaluator(S, &myeval())
          
          : optimize_init_params(S, 0)
          
          : x = optimize(S)
          Iteration 0:   f(p) =         -9  
          Iteration 1:   f(p) = -3.9270875  
          Iteration 2:   f(p) = -.00854249  
          Iteration 3:   f(p) = -.00038131  
          Iteration 4:   f(p) = -1.687e-06  
          Iteration 5:   f(p) = -1.550e-09  
          Iteration 6:   f(p) = -2.843e-11  
          
          : x
            1.07372758
          
          : end
          -------------------------------------------------------------------------------------------------
          
          .
          Last edited by William Lisowski; 20 Dec 2020, 17:37.

          Comment


          • #6
            You might also look up this FAQ: https://www.stata.com/support/faqs/p...ear-equations/

            Comment


            • #7

              Thank you very much Clyde. This is very helpful.
              Last edited by Lars Pete; 20 Dec 2020, 23:47.

              Comment


              • #8

                Thank you so much William. I tried using the optimization method. However, I was using it mindlessly, without understanding what it actually does. This makes it clearer.
                Last edited by Lars Pete; 20 Dec 2020, 23:49.

                Comment


                • #9
                  Originally posted by Joro Kolev View Post
                  Thanks Joro. I did refer to these. But I couldn't get the solution.

                  Comment

                  Working...
                  X