Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Stata's ml stucks


From   Michael Manti <[email protected]>
To   [email protected]
Subject   Re: st: Stata's ml stucks
Date   Wed, 28 Jan 2009 07:29:03 -0500

Sergiy,

I have some observations about your code. They will not necessarily unstick -ml-.

1. Use scalars instead of locals.
2. Declare your variables to be doubles.
3. Make sure your evaluator returns missing when A < 0.

Hope this helps,

Michael Manti

On Jan 27, 2009, at 11:55 PM, Sergiy Radyakin wrote:

Dear All,

I am solving equation F(x) = A, e.g. F(x) = -0.065

It took me 26 iterations (computations of F) to solve it manually to 8
digits precision:
. F 0.1		-.03582777
. F 0.2                 -.08729092
. F 0.15                -.06267811
. F 0.16                -.06777041
. F 0.155               -.06523528
. F 0.153               -.06421507
. F 0.154               -.06472562
. F 0.1545              -.06498056
. F 0.15456             -.06501114
. F 0.15455             -.06500604
. F 0.15454             -.06500095
. F 0.15453             -.06499585
. F 0.154536            -.06499891
. F 0.1545367           -.06499927
. F 0.1545368           -.06499932
. F 0.1545369           -.06499937
. F 0.15453698          -.06499941
. F 0.154537            -.06499942
. F 0.1545375           -.06499967
. F 0.15453778          -.06499982
. F 0.1545379           -.06499988
. F 0.154538            -.06499993
. F 0.1545383           -.06500008
. F 0.1545382           -.06500003
. F 0.1545381           -.06499998
. F 0.15453816          -.06500001
. F 0.15453815          -.065

I have also checked that I can solve it similarly to a couple of other
randomly chosen reasonable values, e.g. -0.071.

I am using Stata's -ml- command to maximize -(F(x)+0.065)^2 but it
gets stuck even after thousands of evaluations of F:

8947.Requested F(.1054786)=-.0623424094117909
8948.Requested F(.1054786)=-.0623424094117909
8949.Requested F(.1054786)=-.0623424094117909
8950.Requested F(-2.634596)=.2516850686732928
8951.Requested F(2.845553)=-.4834788090804468
numerical derivatives are approximate
flat or discontinuous region encountered
Iteration 55:  log likelihood = -.05241295  (backed up)
8952.Requested F(-.2668051)=.1354119673051408
8953.Requested F(-.0806633)=.0490795853955872
8954.Requested F(.0124076)=-.0080075923011136
8955.Requested F(.0589431)=-.036368878923033
8956.Requested F(.0822108)=-.0496348343265961
8957.Requested F(.0938447)=-.0560561994045774
8958.Requested F(.0996616)=-.0592159036741122
8959.Requested F(.1025701)=-.060783280290728
8960.Requested F(.1040243)=-.0615638449113507
8961.Requested F(.1047514)=-.061953356620413
8962.Requested F(.105115)=-.0621479470183188
8963.Requested F(.1052968)=-.0622451942077213
8964.Requested F(.1053877)=-.0622938058069336
8965.Requested F(.1054331)=-.0623180818738749
8966.Requested F(.1054558)=-.0623302191596138
8967.Requested F(.1054672)=-.0623363143485496
8968.Requested F(.1054729)=-.0623393618959092
8969.Requested F(.1054757)=-.0623408589251915
8970.Requested F(.1054771)=-.0623416074369894
8971.Requested F(.1054778)=-.0623419816921718
8972.Requested F(.1054782)=-.0623421955520671
8973.Requested F(.1054784)=-.0623423024819647
8974.Requested F(.1054785)=-.0623423559468972
8975.Requested F(.1054785)=-.0623423559468972
8976.Requested F(.1054785)=-.0623423559468972
8977.Requested F(.1054785)=-.0623423559468972

(8977 is number of computations of F() )

I am using option difficult already.

I wonder if there is any way to tell Stata to stop requesting F(x)
over and over again with the same x.
F is quite computationally intensive, and I would like to have a very
low number of calls to it.

The graph of F looks like this:
http://img217.imageshack.us/my.php?image=functionfffpd8.png
And I need to be able to solve F(x)=A for (A<0)  , which is a "good"
part of the graph.

Here is a part of my code:

/* --------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
program define myfunction
         args lnf Xb
/* ----- all this mess just to get the only parameter beta ----- */
         summarize $ADEPT_ELAST_Income, meanonly
         local beta= r(mean)
         summarize `Xb', meanonly
         local beta=r(mean)-`beta'
/* -------------- determined parameter beta ------------------- */
         /* could I just use local beta = ML_b[1,1] here ?? didn't
work for some reason ??? */
         F `beta'
         local result =-(r(result)-${target})^2
         quietly  replace `lnf' = `result'
         global MyIter = 0$MyIter + 1
end
/* --------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/

   ml model lf myfunction (irrelevant_var = )
   ml init 27401, copy      /* 27401 is about mean of irrelevant_var
I would like to initialize close to zero, say in [0;0.5] */
   global MyIter =0
   ml maximize , difficult
   mat B=e(b)
   summarize irrelevant_var, meanonly
   local solution = B[1,1] - r(mean)

I am also looking for a tutorial on optimization in Stata of functions
(of one or more variables). I could find plenty of ML tutorials, but
very few info on how to max(F(x)-A) numerically w.r.t x.
I have a "ML estimation with Stata" book by Gould, Pitblado and
Sribney, so any references to it are welcomed.
And most important is that the solution must work in Stata 9. I know
Mata 10 has optimize() engine. But it was not available in Stata 9.
Also is there a list and description of all the globals, that
-maximize- passes to -myfunction-, available somewhere? I did a search
in Google and I found that some of them are not mentioned anywhere in
the www. (eg: ML_tn1 etc)

Thank you,
   Sergiy Radyakin

PS: seems like I am crunching nonlinear equations faster than 8x3GHz
machine. Just passed 18,500 requests to F() and hopelessly continuing
:)
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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