From RVaradhan at jhmi.edu  Wed Nov 18 23:39:29 2009
From: RVaradhan at jhmi.edu (Ravi Varadhan)
Date: Wed, 18 Nov 2009 17:39:29 -0500
Subject: [Rd] bug in '...' of constrOptim (PR#14071)
In-Reply-To: <4B04713A.8030801@stat.berkeley.edu>
References: <20091118181016.4F3362831FFC@mail.pubhealth.ku.dk>
	<00d301ca689b$10db2f60$32918e20$@edu>
	<4B04713A.8030801@stat.berkeley.edu>
Message-ID: <00d701ca689f$fd7914a0$f86b3de0$@edu>

Elizabeth,

If you look at the arguments in optim(), you will see that the only
additional argument that is in optim but not in constrOptim is the
`hessian'.  So, it makes sense to add that explicitly in constrOptim() as
well.  The other additional arguments are passed to optim via the `control'
argument.  So, apart from `control' and `hessian' you cannot pass any other
argument to optim.  

Ravi.

----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:
http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h
tml

 

----------------------------------------------------------------------------
--------


-----Original Message-----
From: Elizabeth Purdom [mailto:epurdom at stat.berkeley.edu] 
Sent: Wednesday, November 18, 2009 5:12 PM
To: Ravi Varadhan
Cc: r-devel at stat.math.ethz.ch; R-bugs at r-project.org
Subject: Re: [Rd] bug in '...' of constrOptim (PR#14071)

Hi Ravi,
I just briefly looked at the code (and haven't run it). But it seems 
like there is still the underlying problem that '...' argument is sent 
to both optim and f. The documentation says that ... arguments are sent 
to optim, so at least based on this the ... should only appear in optim. 
This seems prefered since you don't need an explicit argument 'hessian' 
added; after all the user gets to define f, so the flexibility of ... 
there is minimal compared to being able to pass arguments to optim.

Here's the relevant code from what you sent:
	a <- optim(theta.old, fun, gradient, control = control,
             method = method, hessian=hessian, ...)
         r <- a$value
         if (is.finite(r) && is.finite(r.old) && abs(r - 
r.old)/(outer.eps +
             abs(r - r.old)) < outer.eps)
             break
         theta <- a$par
         obj <- f(theta, ...) ###I don't think this should have the ...

Best,
Elizabeth

Ravi Varadhan wrote:
> Hi Elizabeth,
> 
> I have fixed this problem.  See the attached function.  There is also
> another bug that can create a problem when control$fnscale = -1, i.e. when
> the objective function is to be maximized.  I have commented this in the
> code and have also fixed this problem.
> 
> There is a word of caution.  Hessian is problematic when the converged
> parameter estimate is on the boundary of the feasible region, since the
> derivatives are not defined there. 
> 
> Also, note that the default method is Nelder-Mead.  If you want to use
> "BFGS", you have to specify the gradient function.  I have written an
> extension of constrOptim that can use numerical gradients, and can also
> handle nonlinear constraints. 
> 
> 
> Hope this helps,
> 
> Ravi.
> 
>
----------------------------------------------------------------------------
> -------
> 
> Ravi Varadhan, Ph.D.
> 
> Assistant Professor, The Center on Aging and Health
> 
> Division of Geriatric Medicine and Gerontology 
> 
> Johns Hopkins University
> 
> Ph: (410) 502-2619
> 
> Fax: (410) 614-9625
> 
> Email: rvaradhan at jhmi.edu
> 
> Webpage:
>
http://www.jhsph.edu/agingandhealth/People/Faculty_personal_pages/Varadhan.h
> tml
> 
>  
> 
>
----------------------------------------------------------------------------
> --------
> 
> 
> -----Original Message-----
> From: r-devel-bounces at r-project.org [mailto:r-devel-bounces at r-project.org]
> On Behalf Of epurdom at stat.berkeley.edu
> Sent: Wednesday, November 18, 2009 1:10 PM
> To: r-devel at stat.math.ethz.ch
> Cc: R-bugs at r-project.org
> Subject: [Rd] bug in '...' of constrOptim (PR#14071)
> 
> Dear all,
> 
> There appears to be a bug in how constrOptim handles ... arguments that 
> are suppose to be passed to optim, according to the documentation. This 
> means you can't get the hessian to be returned, for example (so this is 
> a real problem, and not just a question of mistaken documentation).
> 
> Looking at the code, it appears that a call to the user-defined f 
> includes the ..., when the ... should only be passed to the optim call. 
>   I get around it by putting a dummy 'hessian=TRUE' argument in my f 
> function, but this is not how the function should work.
> 
> I show the relevant problem in the code, give an example, and also give 
> my sessionInfo() call below.
> 
> Thanks,
> Elizabeth
> 
> ##Copy of the relevant segment of the code of constrOptim and where I 
> think the problem might be:
> for (i in 1L:outer.iterations) {
>          obj.old <- obj
>          r.old <- r
>          theta.old <- theta
>          fun <- function(theta, ...) { ##this one's okay
>              R(theta, theta.old, ...)##this one's okay
>          }
>          gradient <- function(theta, ...) {##this one's okay
>              dR(theta, theta.old, ...)##this one's okay
>          }
>          a <- optim(theta.old, fun, gradient, control = control,
>              method = method, ...)##this one's okay
>          r <- a$value
>          if (is.finite(r) && is.finite(r.old) && abs(r - 
> r.old)/(outer.eps +
>              abs(r - r.old)) < outer.eps)
>              break
>          theta <- a$par
>          obj <- f(theta, ...)##this one's NOT okay
>          if (obj > obj.old)
>              break
>      }
> 
> ###Here is an example modified from the examples of the help page of 
> constrOptim:
>  > fr <- function(x) {   ## Rosenbrock Banana function
> +     x1 <- x[1]
> +     x2 <- x[2]
> +     100 * (x2 - x1 * x1)^2 + (1 - x1)^2
> + }
>  > grr <- function(x) { ## Gradient of 'fr'
> +     x1 <- x[1]
> +     x2 <- x[2]
> +     c(-400 * x1 * (x2 - x1 * x1) - 2 * (1 - x1),
> +        200 *      (x2 - x1 * x1))
> + }
>  > constrOptim(c(-1.2,0.9), fr, grr, ui=rbind(c(-1,0),c(0,-1)),
ci=c(-1,-1))
> $par
> [1] 0.9999761 0.9999522
> 
> $value
> [1] 5.708626e-10
> 
> $counts
> function gradient
>         6        1
> 
> $convergence
> [1] 0
> 
> $message
> NULL
> 
> $outer.iterations
> [1] 14
> 
> $barrier.value
> [1] -0.0001999198
> 
>  > constrOptim(c(-1.2,0.9), fr, grr, ui=rbind(c(-1,0),c(0,-1)), 
> ci=c(-1,-1),hessian=TRUE)
> Error in f(theta, ...) : unused argument(s) (hessian = TRUE)
> #add in a dummy argument to my f function
>  > fr <- function(x,hessian=TRUE) {   ## Rosenbrock Banana function
> +     x1 <- x[1]
> +     x2 <- x[2]
> +     100 * (x2 - x1 * x1)^2 + (1 - x1)^2
> + }
>  > constrOptim(c(-1.2,0.9), fr, grr, ui=rbind(c(-1,0),c(0,-1)), 
> ci=c(-1,-1),hessian=TRUE)
> $par
> [1] 0.9999761 0.9999522
> 
> $value
> [1] 5.708626e-10
> 
> $counts
> function gradient
>         6        1
> 
> $convergence
> [1] 0
> 
> $message
> NULL
> 
> $hessian
>            [,1]      [,2]
> [1,]  801.9599 -399.9905
> [2,] -399.9905  199.9952
> 
> $outer.iterations
> [1] 14
> 
> $barrier.value
> [1] -0.0001999198
> ###My Session info
>  > sessionInfo()
> R version 2.10.0 (2009-10-26)
> x86_64-apple-darwin9.8.0
> 
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/C/C/en_US.UTF-8/en_US.UTF-8
> 
> attached base packages:
> [1] stats     graphics  grDevices utils     datasets  methods   base
> 
> other attached packages:
> [1] GEOquery_2.10.0    RCurl_1.2-1        bitops_1.0-4.1 
> Biobase_2.5.8      biomaRt_2.1.0      projectManager_1.0 lattice_0.17-26 
>     RColorBrewer_1.0-2
> [9] XML_2.6-0
> 
> loaded via a namespace (and not attached):
> [1] grid_2.10.0  tools_2.10.0
> 

-- 
Elizabeth Purdom
Assistant Professor
Department of Statistics
UC, Berkeley
Evans Hall, Rm 433
epurdom at stat.berkeley.edu
(510) 642-6154 (office)
(510) 642-7892 (fax)



[Rd] bug in '...' of constrOptim (PR#14071)
Ravi Varadhan