Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

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

From |
Matthew J Baker <matthew.baker@hunter.cuny.edu> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
st: Mata: trouble with nested optimize() |

Date |
Tue, 10 May 2011 08:26:12 -0400 (EDT) |

I think the problem with the example is that the "q" term, which is set as an external in the first function, is not getting there! So, the problem is that the q value is not being passed along. Therefore, one can make two modifications to the code given to get it to run: 1) allow invobj to be a function of q directly, and get rid of the external call, and 2) use the function invd to formally set the q argument of invobj. If I make these two modifications, the program runs. The modified example: clear mata: mata clear mata: void invobj(todo,p,q,f,S,H) //allow function to depend on q explicitly { //Trivial inversion: assume that I cannot invert q=10-p analytically: f=(q-(10-p))^2 } real scalar function invd(real scalar q) { transmorphic I I=optimize_init() optimize_init_evaluator(I,&invobj()) optimize_init_evaluatortype(I,"d0") optimize_init_params(I,(3)) optimize_init_argument(I,1,q) // pass q to invobj optimize_init_which(I,"min") p=optimize(I) return(p) } void objftn(todo,q,f,S,H) { real scalar p p=invd(q); f=q*(p-1) } S=optimize_init() optimize_init_evaluator(S,&objftn()) optimize_init_evaluatortype(S,"d0") optimize_init_params(S,(1)) optimize_init_which(S,"max") s=optimize(S) s end Hope that helps! Matt Baker - Hide quoted text - On Mon, May 9, 2011 at 7:20 PM, Lydia Pikyi Cheung <cheun073@umn.edu> wrote: Dear Statalist users, I would like to maximize a function F(q1,q2) that involves terms p1(q1,q2), p2(q1,q2) that do not have analytical forms. I.e., every time a trial value (q1,q2) is used to evaluate F(), Mata needs to solve a system of equations to back out the corresponding p1(q1,q2) and p2(q1,q2), in order to evaluate F(). I solve this system of equations using optimize(), following http://www.stata.com/support/faqs/lang/nl.html (bottom of page). As a result, I have an inner optimize() routine to solve the system of equations inside the main optimize() routine to maximize F(). Is this possible? I have attached my code below with simplified functions. (I have simplified both p,q be scalars, i.e. F(q,p) is a function of scalars, and p(q) takes a scalar argument.) I keep getting: : s=optimize(S) initial values not feasible (1 line skipped) ---------------------------------------- r(1400); I do not think initial values are the problem. After running the following code, I checked: : S 0x5ac5bdc : I <istmt>: 3499 I not found r(3499); Thus S was initialized successfully while I was not. Is it because objftn() fails to invoke function invd()? Any better way to code this problem is appreciated as well. = = = = = = = = = = = = = = = = = = = clear mata: mata clear mata: void invobj(todo,p,f,S,H) { external real scalar q //Trivial inversion: assume that I cannot invert q=10-p analytically: f=(q-(10-p))^2 } real scalar function invd(real scalar q) { transmorphic I I=optimize_init() optimize_init_evaluator(I,&invobj()) optimize_init_evaluatortype(I,"d0") optimize_init_params(I,(3)) optimize_init_which(I,"min") p=optimize(I) return(p) } void objftn(todo,q,f,S,H) { real scalar p p=invd(q); f=q*(p-1) } S=optimize_init() optimize_init_evaluator(S,&objftn()) optimize_init_evaluatortype(S,"d0") optimize_init_params(S,(1)) optimize_init_which(S,"max") s=optimize(S) s end * * 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/ -- Dr. Matthew J. Baker Department of Economics Hunter College and the Graduate Center, CUNY * * 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/

- Prev by Date:
**Re: st: nonlinear iv** - Next by Date:
**st: Test 2** - Previous by thread:
**st: Mata: trouble with nested optimize()** - Next by thread:
**st: Creating a new variable conditional on the values for 2 other variables** - Index(es):