[R] Automatic differentiation in R

Gabor Grothendieck ggrothendieck at gmail.com
Wed Jul 22 13:41:19 CEST 2009


R does not currently have AD (except for the Ryacas package
which can do true AD for certain simple one line functions, i..e.
input the function and output a function representing its
derivative); however, for specific problems one can get close
using deriv and associated functions or the approach explained
below using rSymPy:

1. R has symbolic differentiation (not quite the same as AD but
it could be built upon) already:
?deriv

2. the rSymPy package on CRAN is an interface to the sympy
computer algebra system.  It is meant for symbolic manipulation
rather than AD but it can be used for semi-automatic differentiation
as in this example:

http://code.google.com/p/rsympy/#Semi-Automatic_Differentiation

You just replace the indented code with your function, suitably modified,
as in the example.

Home page is at:
http://rsympy.googlecode.com

3. the Ryacas R package on CRAN can do AD for certain simple one
line functions (and an approach similar to rSymPy is also possible):

library(Ryacas)
demo("Ryacas-Function")

Home page is at:
http://ryacas.googlecode.com

If its your intention to use it that way you would probably want
to write a short wrapper function to make it more convenient to
use; however, the basic capability is there.

4. if numeric differentiation is sufficient see:
?numericDeriv


On Wed, Jul 22, 2009 at 5:21 AM, Finlay Scott
(Cefas)<finlay.scott at cefas.co.uk> wrote:
> Hi
> I recently gave a presentation about Automatic Differentiation (AD) and R at the Eighth Euro AD Workshop in Oxford (17/07/09).  The presentation was intended as a general introduction to R and the desire for a generic AD interface for R.  During the presentation I emphasised the need and the high level of interest that the R community has in developing such an interface and that input from the AD community is essential to achieve it.
>
> The presentation seemed to generate a good level of interest and several suggestions for potential directions were made.  However, it is not clear at the moment what the best way is of implementing an AD interface in R in terms of success, difficulty and amount of work.
>
> One suggestion was that it may be possible to develop it entirely in R, using the S4 classes and overloading the intrinsic functions.  This may not result in the fastest solution, in terms of time taken to calculate the derivatives, but could result in a 'clean' interface that was relatively straightforward to extend.
>
> Alternatively, it may be possible to make use of a pre-exisiting AD library in another language (e.g. ADOL-C or the AutoDiff libraries from AD Model Builder).  As these libraries are C++ based this would require functions that were written in R to be parsed into C++ and then compiled before their derviatives could be found.  Although this has the advantage of using work that is already well developed, the parsing and compilation from R to C++ may make the interface 'clunky'.
>
> It is likely that we won't know how successful either of these approaches could be until someone actually sits down and has a go at doing it.  Is there anyone out there who is interested in getting involved with this project or who has advice / time to offer?
>
> Also, would it be possible to also post this message in the R-devel list?  I realise that this would mean 'double posting' but the potential for a generic AD interface affects many packages and may also be of interest to developers (e.g. John Nash's 'optimx' package)
>
> Finlay Scott
>
>
> ***********************************************************************************
> This email and any attachments are intended for the name...{{dropped:10}}
>
> ______________________________________________
> 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.
>




More information about the R-help mailing list