Rodrigo Alfaro <[email protected]> writes,
> I already checked the features of the new version. Mata function optimize()
> seems very powerful. Is that useful for ML or GMM (no linear) problems?
Yes.
Mata's new -optimize()- function in Stata 10 is one of those features that
will be very popular with some users -- not all -- and be important in the
future development of Stata by StataCorp.
Before I get to that, however, I want to start by mentioning Stata's -ml-
command because -ml- is and will continue to be the primary way, and the
best way, for most users to obtain estimates from user-defined likelihood
functions. We at StataCorp, as a matter of fact, will continue to use
-ml- for implementing many of our new estimators, too.
So, why -optimize()- in Mata? It addresses two problems:
1. Quickly obtaining maximums or minimums of simple functions, and
2. Obtaining maximums and minimums of exceedingly complicated
functions when the programmer is willing to invest in writing
a lot of code.
Stata's -ml- fills in the vast middle ground.
Rodrigo mentioned that he had "already checked the features of the new
[...] -optimize()-". You can do the same by going to
http://www.stata.com/help.cgi?mata+optimize()
That link actually leads you to the on-line help file, and is equivalent
to typing -help mf_optimize- in Stata 10. Actually, that trick works
for any of the help files, so if you want to start exploring a particular
feature of Stata 10, go to
http://www.stata.com/help.cgi?_____
which will be the same as typing
. help _____
just remember to change any blanks in ______ to plus signs (+)
Altnernatively, go to http://www.stata.com/capabilities/
and fill in the search form at the bottom of the page; it's
equivalent to Stata's -search- command.
Anyway, here's an example of Mata's new -optimize()- being used in a simple
case to find x that maximizes y = exp(x^2+x-3):
: void myeval(todo, x, y, g, H)
> {
> y = exp(-x^2 + x - 3)
> }
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) = .04978707
Iteration 1: f(p) = .04978708
Iteration 2: f(p) = .06381186
Iteration 3: f(p) = .06392786
Iteration 4: f(p) = .06392786
: x
.5
Note that the total code for the above was to define the function,
: void myeval(todo, x, y, g, H)
> {
> y = exp(-x^2 + x - 3)
> }
and then to code
S = optimize_init()
optimize_init_evaluator(S, &myeval())
optimize_init_params(S, 0)
x = optimize(S)
In a more advanced setting, those lines could appear in another function,
and all output could be suppressed:
S = optimize_init()
optimize_init_evaluator(S, &myeval())
optimize_init_params(S, 0)
optimize_init_tracelevel("none") // <- new
x = optimize(S)
-optimize()- has lots of features:
1. You can program just the function, the function and its gradient,
or the function, its gradient, and Hessian.
2. Functions can return overall likelhood values, or may return
a vector of likelihood values for independent "observations".
3. Function debuggers are provided.
4. You can optimize using modified Newton-Raphson,
Davidon-Fletcher-Powell, Broyden-Fletcher-Goldfarb-Shanno,
Berndt-Hall-Hall-Hausman, or (derivative free) Nelder-Mead
simplex.
5. When the Hessian is singular, you can request a modified
Marquardt algorithm, or a mixture of steepest descent and
Newton-Raphson.
6. Once estimates are obtained, you can fetch the parameter vector,
the gradient, the Hessian, the variance matrix calculated various
ways (invsym(-H), invsym(S'S), H*invsym(S'S)*H, etc.), even
if your evaluator did not calculate them and even if they were not
used in estimation.
If some of this sounds familiar, that's because many of those same features
are found in -ml-.
You may have noticed that we are, in our own development, moving toward Mata.
Our goal is not to replace -ml-, but we do plan to rewrite the internals of
-ml- in Mata. That will make -ml- faster, meaning every model currently
estimated by Stata that uses -ml-, which is most of them, both official and
user-written, will execute more quickly and no modification will be
required. Moreover, we will be able to introduce advanced features
both on the Mata side and on the Stata -ml- side.
Mata's new -optimize()- function is the first step in the plan. The
advantages are not just execution speed. Mata code is more easily modified
and extended, and not just by us at StataCorp. Mata code is open.
-- Bill
[email protected]
*
* 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/