# st: RE: Numerical maximization

 From "FEIVESON, ALAN H. (AL) (JSC-SD) (NASA)" To "'statalist@hsphsun2.harvard.edu'" Subject st: RE: Numerical maximization Date Fri, 4 Oct 2002 10:47:22 -0500

```Mark -

If you want to find a value of x that maximizes an arbitrary funcrion f(x),
you might be able to fool -nl- into doing it, provided you can bound the
function. Here's how. Suppose you know f(x) < f0 for all x. Then create a
dummy data set with two observations as follows:
clear
set obs 2
gen y=1000 in 1/*(the value of f0)*/
replace y=0 in 2  /* it doesn't matter what y is on obs. 2 as long as it's
different from f0 */
nl max y y

The last command calls Stata's nonlinear least square -nl- program. To use
it you have to write your own nl-program. Here's an example where you are
trying to maximize f(x)=x*(x+2)*exp(-0.5*x)/(x+1) for x > 0.

program define nlmax

if "`1'" == "?" {
global S_1 "  Z "
global Z= 1
exit
}
cap gen fh=.
global X=exp(\$Z)
replace fh=\$X*(2+\$X)*exp(-.5*\$X)/(1+\$X) in 1
replace fh=`2' in 2

replace `1' = fh

end

What this is doing is finding a value of Z = log(x) that makes f(x) come as
close to 1000 as possible. Since 1000 is an upper bound on f(x), this will
maximize f. Use of log(x) instead of x prevents the consideration of
negative values. For example this function has a vertical asymptote at x =
-1.

Hope this helps.

Al Feiveson

-----Original Message-----
From: Mark Schaffer [mailto:M.E.Schaffer@hw.ac.uk]
Sent: Wednesday, October 02, 2002 11:09 AM
To: statalist@hsphsun2.harvard.edu
Subject: st: Numerical maximization

Hi everybody.  A very general question for you:

What's the recommended strategy if you're writing a Stata estimator
that requires numerical maximization methods but isn't maximum
likelihood?  And if you think you might want to make the estimator
available for general use?

Stata doesn't have a maximize program as such (-maximize- seems to be
an ML program ... but maybe it's good enough for the task?). -findit-
told me about a couple of user-written programs called -amoeba- and
-quasi-.  I have no experience with these, nor with the tradeoffs in
writing code that rely on other people's routines.  (It's great that
they make their code publicly available, but we can't expect them to
refrain from changing the specs if they see fit to do so, and of
course that could cause your own code to fail to work.)

--Mark

Prof. Mark E. Schaffer
Director
Centre for Economic Reform and Transformation
Department of Economics
School of Management & Languages
Heriot-Watt University, Edinburgh EH14 4AS  UK
44-131-451-3494 direct
44-131-451-3008 fax
http://www.som.hw.ac.uk/cert
*
*   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/
*
*   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/
```