Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

st: Mata: trouble with nested optimize()


From   Lydia Pikyi Cheung <[email protected]>
To   [email protected]
Subject   st: Mata: trouble with nested optimize()
Date   Mon, 9 May 2011 16:20:01 -0700

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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index