Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RES: Estimation of a non-linear system of equations


From   "Henrique Neder" <hdneder@ufu.br>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RES: Estimation of a non-linear system of equations
Date   Thu, 20 Dec 2007 10:32:54 -0200

I copied the following answer to FAC due to Feiveson. I hope that this help
you. Henrique 

How can I use Stata to solve a system of nonlinear equations?
Title   Using Stata to solve a system of nonlinear equations 
Author Alan H. Feiveson, NASA 
Date June 2001; updated October 2005 

----------------------------------------------------------------------------
----
Suppose that you want to solve 
    f1(a1,...,an) = 0
    f2(a1,...,an) = 0
    .
    .
    .
    fn(a1,...,an) = 0

(n nonlinear equations in n unknowns a1,..,an) 
First, rewrite the first equation such that its right-hand side is 1: 

    f1(a1,...,an) + 1 = 1
    f2(a1,...,an) = 0
    .
    .
    .
    fn(a1,...,an) = 0

Then, set up a fake dataset with n observations as follows: 
The dependent variable y takes on the value 1 for the first observation and
0 for all the others. Stata's nl estimation won't work if y is a constant,
so you need to write the first equation so that the "right-hand sides" are
not all the same; that is why I reformulated the problem above. In this
example, I used one for the first observation and zero for the others. 

Write an nl program that fills in the dependent variable passed to it with
the values of the functions. Suppose n=3. Then your program should look
something like this: 
        program nlfaq

                syntax varlist (min=1 max=1) [if], at(name)

                tempname a1 a2 a3
                scalar `a1' = `at'[1, 1]
                scalar `a2' = `at'[1, 2]
                scalar `a3' = `at'[1, 3]

                tempvar yh
                generate double `yh' = f1(`a1', `a2', `a3') in 1
                replace `yh' = f2(`a1', `a2', `a3') in 2
                replace `yh' = f3(`a1', `a2', `a3') in 3

                replace `varlist' = `yh'

        end

nl requires that our program accept an if clause, though we can ignore it in
our program since we do not have missing data and will not be restricting
the estimation sample when calling nl. 
Call nl with y as the dependent variable, specifying initial values for a1,
a2, ..., an at which the functions can be evaluated. 
Here is an example. Suppose that I want to solve the following system for A,
B, and C: 

  exp(A) + B*C = 3
  A/B + C^2    = log(B)
  A/(A+B+C)    = sin(C)

Here is my nl program: 
  program nlfaq
 
         syntax varlist(min=1 max=1) [if], at(name)

         tempname A B C
         scalar `A' = `at'[1, 1]
         scalar `B' = `at'[1, 2]
         scalar `C' = `at'[1, 3]

         tempvar yh
         gen double `yh' = exp(`A') + `B'*`C' - 2 in 1
         replace `yh' = `A'/`B' + `C'^2 - log(`B') in 2
         replace `yh' = `A'/(`A'+`B'+`C') - sin(`C') in 3

         replace `varlist' = `yh'

  end

Now I generate the dataset that nl requires: 
  . clear
  . set obs 3
  obs was 0, now 3

  . generate y = 0

  . replace y = 1 in 1
  (1 real change made)

I estimate using nl: 
  . nl faq @ y, parameters(A B C) initial(A 1 B 1 C 1)
  (obs = 3)
        
  Iteration 0:  residual SS =  .5792985
  Iteration 1:  residual SS =  .0364809
  Iteration 2:  residual SS =  .0001378
  Iteration 3:  residual SS =  2.92e-09
  Iteration 4:  residual SS =  1.43e-18
  Iteration 5:  residual SS =  2.25e-31
  
        Source |       SS       df       MS
  -------------+------------------------------         Number of obs =
3
         Model |           1     3  .333333333         R-squared     =
1.0000
      Residual |  1.6332e-31     0           .         Adj R-squared =
.
  -------------+------------------------------         Root MSE      =
.
         Total |           1     3  .333333333         Res. dev.     =
-207.451
  
 
----------------------------------------------------------------------------
--
             y |      Coef.   Std. Err.      t    P>|t|     [95% Conf.
Interval]
 
-------------+--------------------------------------------------------------
--
            /A |   .8973072          .        .       .            .
.
 
-------------+--------------------------------------------------------------
--
            /B |   1.803287          .        .       .            .
.
 
-------------+--------------------------------------------------------------
--
            /C |   .3033412          .        .       .            .
.
 
----------------------------------------------------------------------------
--
  * (SEs, P values, CIs, and correlations are asymptotic approximations)

Finally, I verify the solution: 
  . scalar A = [A]_b[_cons]
  . scalar B = [B]_b[_cons]
  . scalar C = [C]_b[_cons]
  . di exp(A) + B*C
  3
  . di A/B + C^2 "   " log(B)
  .58961117   .58961117
  
  . di A/(A+B+C) "   " sin(C) 
  .29871053   .29871053





-----Mensagem original-----
De: owner-statalist@hsphsun2.harvard.edu
[mailto:owner-statalist@hsphsun2.harvard.edu] Em nome de Craig Martin
Enviada em: quarta-feira, 19 de dezembro de 2007 17:14
Para: statalist@hsphsun2.harvard.edu
Assunto: st: Estimation of a non-linear system of equations

I am using Stata 9 and need to estimate a system of equations.

I can estimate each equations separately using nl.  However, when  I try 
to estimate the system using the same syntax as linear estimation for a 
system of equations, I get a varlist error.  What is the syntax or how 
do you estimate a non-linear system of eqation in Stata 9.0?

Thanks,

-- 
Craig Martin M.Sc.
PhD Candidate
Department of Food, Agricultural and Resource Economics
University of Guelph
Guelph, Ontario  N1G 2W1

(519) 824-4120 Ext. 58315
Fax (519) 767-1510

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

No virus found in this incoming message.
Checked by AVG Free Edition. 
Version: 7.5.503 / Virus Database: 269.17.4/1187 - Release Date: 16/12/2007
11:36
 

No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.503 / Virus Database: 269.17.4/1187 - Release Date: 16/12/2007
11:36
 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2021 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index