Grokbase Groups R r-help March 2002
FAQ

On Wed, Mar 20, 2002 at 02:31:03PM +0100, G?ran Brostr?m wrote:
I want to maximise a function using 'optim' with a method that requires
the gradient, so I supply two functions, 'fun' for the function value
and 'd.fun' for its gradient. My question is: Since some calculations are
common to the two functions, is it possible to save time by feeding
'd.fun' with some of the calculations made in 'fun'? That would require
(at least) that it is guaranteed that each time 'd.fun' is evaluated, it
is done immediately after a call to 'fun' at the same parameter value.
The help page says:

"It is guaranteed that `gr' will be called immediately after a
call to `fn' at the same parameter values."

which is not exactly the same. However, look at this trivial example:

prov <- function(x.start = 0){
fun <- function(x) {
e.x <- exp(-x)
x * e.x
}
d.fun <- function(x) {
e.x <- exp(-x)
e.x * (1 - x)
}
optim(x.start, fun, d.fun, method = "BFGS",
control = list(fnscale = -1))
}
prov(2)$counts
function gradient
19 9

This contradicts the help page info, since that implies that the gradient
is called at least as many times as the function.

Q1: What is the correct guarantee? Is it what I need?

Q2: If so, How do I share calculations between the two functions? In my
trivial example, exp(-x) is calculated in both.

Thanks for any enlightenment! (Brian?)

G?ran
--
G?ran Brostr?m tel: +46 90 786 5223
professor fax: +46 90 786 6614
Department of Statistics http://www.stat.umu.se/egna/gb/
Ume? University
SE-90187 Ume?, Sweden e-mail: gb at stat.umu.se

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Here is a (proposal of) reply for Q2:

Storing and accessing the result of the 'shared calculation' in a given environment could be a workaround.


Hopin' it helps,



Laurent



--------------------------------------------------------------
Laurent Gautier CBS, Building 208, DTU
PhD. Student D-2800 Lyngby,Denmark
tel: +45 45 25 24 85 http://www.cbs.dtu.dk/laurent
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._

Search Discussions

Discussion Posts

Previous

Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 9 of 15 | next ›
Discussion Overview
groupr-help @
categoriesr
postedMar 20, '02 at 7:41a
activeMar 21, '02 at 11:00a
posts15
users11
websiter-project.org
irc#r

People

Translate

site design / logo © 2017 Grokbase