From: Ravi Varadhan <rvaradhan_at_jhmi.edu>

Date: Thu, 27 Mar 2008 11:59:51 -0400

R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 27 Mar 2008 - 20:06:50 GMT

Date: Thu, 27 Mar 2008 11:59:51 -0400

Hi All,

I would like to compute the simple finite-difference approximation to the gradient of a scalar function of a large number of variables (on the order of 1000). Although a one-time computation using the following function grad() is fast and simple enough, the overhead for repeated evaluation of gradient in iterative schemes is quite significant. I was wondering whether there are better, more efficient ways to approximate the gradient of a large scalar function in R.

Here is an example.

grad <- function(x, fn=func, eps=1.e-07, ...){

npar <- length(x)

df <- rep(NA, npar)

f <- fn(x, ...)

for (i in 1:npar) {

dx <- x

dx[i] <- dx[i] + eps

df[i] <- (fn(dx, ...) - f)/eps

}

df

*}
*

myfunc <- function(x){

nvec <- 1: length(x)

sum(nvec * (exp(x) - x)) / 10

*}
*

myfunc.g <- function(x){

nvec <- 1: length(x)

nvec * (exp(x) - 1) / 10

*}
*

p0 <- rexp(1000)

system.time(g.1 <- grad(x=p0, fn=myfunc))[1]

system.time(g.2 <- myfunc.g(x=p0))[1]

max(abs(g.2 - g.1))

Thanks in advance for any help or hints.

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/Varadhan.html

[[alternative HTML version deleted]]

R-help_at_r-project.org mailing list

https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu 27 Mar 2008 - 20:06:50 GMT

Archive maintained by Robert King, hosted by
the discipline of
statistics at the
University of Newcastle,
Australia.

Archive generated by hypermail 2.2.0, at Thu 27 Mar 2008 - 20:30:24 GMT.

*
Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help.
Please read the posting
guide before posting to the list.
*