Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Numerical maximization

From   "FEIVESON, ALAN H. (AL) (JSC-SD) (NASA)" <>
To   "''" <>
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:
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
      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

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 =

Hope this helps.

Al Feiveson

-----Original Message-----
From: Mark Schaffer []
Sent: Wednesday, October 02, 2002 11:09 AM
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.)


Prof. Mark E. Schaffer
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
44-131-451-3485 CERT administrator
*   For searches and help try:
*   For searches and help try:

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