[Rd] symbollic differentiation in R

Andrew Clausen clausen at econ.upenn.edu
Sun May 13 19:22:41 CEST 2007


Hi all,

I wrote a symbollic differentiation function in R, which can be downloaded
here:

        http://www.econ.upenn.edu/~clausen/computing/Deriv.R
        http://www.econ.upenn.edu/~clausen/computing/Simplify.R

It is just a prototype.  Of course, R already contains two differentiation
functions: D and deriv.  However, these functions have several limitations.
They can probably be fixed, but since they are written in C, this would
require a lot of work.  Limitations include:
 * The derivatives table can't be modified at runtime, and is only available
in C.
 * The output of "deriv" can not be differentiated again.
 * Neither function can substitute function calls.  eg:
       f <- function(x, y) x + y; deriv(f(x, x^2), "x")
 * They can't differentiate vector-valued functions (although my code also
can't do this yet)

I think these limitations are fairly important.  As it stands, it's rather
difficult to automatically differentiate a likelihood function.  Ideally, I
would like to be able to write

        ll <- function(mean, sd)
                -sum(log(dnorm(x, mean, sd)))

        ll.deriv <- Deriv.function(ll)

I can't get this to work with my code since:
 * since sum can't add a list of vectors (although I could easily write a sum
replacement.)
 * "x" is assumed to be a scalar in this contect.  I'm not sure if there's a
good way to generalize.

The above code would work right now if there were one parameter (so
sum doesn't screw it up) and one scalar data point "x".

Is there an existing way of doing this that is close to being this convenient?
Is it really much easier to solve the limitations I listed with a fresh
R implementation?

Cheers,
Andrew



More information about the R-devel mailing list