Re: [Rd] symbollic differentiation in R

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Sun, 13 May 2007 13:37:09 -0400

On 5/13/07, Andrew Clausen <clausen_at_econ.upenn.edu> wrote:
> 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.

Try this:

> D(D(quote(x^3), "x"), "x")

3 * (2 * x)

> * Neither function can substitute function calls. eg:
> f <- function(x, y) x + y; deriv(f(x, x^2), "x")

Try Ryacas package:

> library(Ryacas)
> x <- Sym("x")
> f <- function(x)x^2
> deriv(f(x^3))
expression(6 * x^5)

> * They can't differentiate vector-valued functions (although my code also
> can't do this yet)

> library(Ryacas)
> x <- Sym("x")
> deriv(List(x, x^2))

expression(list(1, 2 * x))

>
> 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
>
> ______________________________________________
> R-devel_at_r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



R-devel_at_r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel Received on Sun 13 May 2007 - 17:39:33 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 Sun 13 May 2007 - 23:33:56 GMT.

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