[Rd] symbollic differentiation in R
Gabor Grothendieck
ggrothendieck at gmail.com
Sun May 13 19:37:09 CEST 2007
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
>
More information about the R-devel
mailing list