[R] optim()

Spencer Graves spencer.graves at pdf.com
Tue Jul 18 07:49:01 CEST 2006

	  I had good luck translating constrained into unconstrained problems 
and then optimizing the unconstrained problem.  Have you tried something 
like the following:

	  z = c(z1, z2, z3), where p1=1/(1+exp(-z1), etc.  This translates the 
constraints on the p's to

	  G(z) = P*(f1(z)-r12*f2(z))^2-f1(z)

where f1(z) = f1(p1(z1), p2(z2), p3(z3), and similarly for f2(z), and 
where P = a penalty term,
and r12 = (1-c)*k1/(c*(1-k1).

	  Can f2(z) ever go outside (0, 1)?  If yes, I would modify G(z) by 
adding a term like (min(0, f2(z), 1-f2(z))^2)

	  If I haven't made a math error, your problem should translate into 
this form.  I first solve this problem for z with P small like 1.  Then 
after I've got a solution for that, I increase P to 2, then 10, then 
100, etc., until the penalty is so great that the desired equality has 
been effectively achieved.

	  With 'P' fixed, 'optim' should handle this kind of problem handily. 
To learn how, I suggest you work through the examples in the ?optim help 
page.  I'd ignore the gradient, at least initially.  A silly math error 
in computing the gradient can delay a solutions unnecessarily.  If you 
need to solve thousands of problems like this for  different values of 
k1 and 'c', I might later program the gradient.  However, I would not do 
that initially.

	  Also, if you are not already familiar with Venables and Ripley (2002) 
Modern Applied Statistics with S, 4th ed. (Springer -- or an earlier 
edition), I would encourage you to spend some quality time with this 
book.  It can help you with 'optim', with contour plots, etc.

	  Hope this helps,
	  Spencer Graves

Iris Zhao wrote:
> Dear all,
> I am working on optimization problem and have some trouble running optim().
> I have two functions (f1, f2) and 4 unknown parameters (p1, p2, p3, p4).
> Both f1 and f2 are functions of p1, p2, and p3, denoted by f1(p1, p2, p3)
> and f2(p1,p2,p3) respectively.
> The goal is to maximize f1(p1, p2, p3) subject to two constraints:
> (1)  c = k1*p4/(k1*p4+(1-k1)*f1(p1,p2,p3)), where c and k1 are some known
> constants
> (2)  p4 = f2(p1, p2, p3)
> In addition, each parameter ranges from 0 to 1, and both f1 and f2 involve
> integrations.
> I tried to use lagrange multipliers to eliminate two equality constraints
> and then use optim() to find the maximum value and optimal parameter
> estimates.
> So I let fn be f1+lambda1*(c- k1*p4/(k1*p4+(1-k1)*f1(p1,p2,p3))) +
> lambda2(p4-f2(p1,p2,p3)). The error message I got was "Error in fn(par, ...)
> : recursive default argument reference."
> I wonder whether current build-in functions in R can do this type of jobs.
> Any suggestion will be greatly appreciated.
> Iris
> 	[[alternative HTML version deleted]]
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

More information about the R-help mailing list