ADFun {scorematchingad} | R Documentation |
A Class for Storing a CppAD Tape (ADFun) Object
Description
This is a low level object useful for implementing score matching estimators.
An R6
class for storing a 'pointer' to a CppAD
tape in C++
(also called an ADFun
) and associated information.
Currently tools for modifying this information are not available in this package, however tools for creating new ADFun
objects from an existing ADFun
are available.
Typically an ADFun
object will be created by buildsmdtape()
.
Introduction to CppAD Tapes
This package uses version 2024000.5 of the algorithmic differentiation library CppAD
(Bell 2023) to build score matching estimators.
Full help for CppAD
can be found at https://cppad.readthedocs.io/.
Differentiation proceeds by taping the basic (atomic) operations performed on the independent variables and dynamic parameters. The atomic operations include multiplication, division, addition, sine, cosine, exponential and many more.
Example values for the variables and parameters are used to conduct this taping, so care must be taken with any conditional (e.g. if-then) operations, and CppAD
has a special tool for this called CondExp
(short for conditional expressions
).
The result of taping is an object of class ADFun
in CppAD
and is often called a tape.
This ADFun
object can be evaluated, differentiated, used for further taping (via CppAD
's base2ad()
), solving differential equations and more.
The differentiation is with respect to the independent variables, however the dynamic parameters can be altered which allows for creating a new ADFun
object where the dynamic parameters become independent variables (see tapeSwap()
).
For the purposes of score matching, there are also fixed parameters, which are the elements of the model's parameter vector that are given and not estimated.
Warning: multiple CPU
Each time a tape is evaluated the corresponding C++
object is altered. Parallel use of the same ADFun
object thus requires care and is not tested. For now I recommend creating a new ADFun
object for each CPU.
Active bindings
ptr
A
Rcpp
external pointer to aCppAD
ADFun
object.xtape
The (numeric) vector of independent variable values used for taping.
dyntape
The (numeric) vector of dynamic parameters used for taping.
usertheta
A (numeric) vector of
NA
values and fixed values specifying the parameters of taped function that were considered dynamic parameters or fixed parameters respectively.name
An easy to read name for the taped function
Methods
Public methods
Method new()
Create a new ADFun
object from an external pointer to a CppAD
ADFun
object.
Usage
ADFun$new( ptr, name = NULL, xtape = vector("numeric"), dyntape = vector("numeric"), usertheta = rep(NA_real_, length(dyntape)) )
Arguments
ptr
A
Rcpp
external pointer to aCppAD
ADFun
object.name
An easy to read name for the taped function
xtape
The (numeric) vector of independent variables used for taping.
dyntape
The (numeric) vector of dynamic parameters used for taping.
usertheta
A (numeric) vector of
NA
values and fixed values specifying the inputs of the taped function that were considered independent variables or dynamic parameters respectively.
Examples
tapes <- buildsmdtape(
"sim", "sqrt", "sph",
ll = "ppi",
ytape = rep(1/3, 3),
usertheta = ppi_paramvec(p=3),
bdryw = "minsq",
acut = 0.01,
verbose = FALSE)
tapes$smdtape$xtape
tapes$smdtape$dyntape
tapes$smdtape$name
tapes$smdtape$ptr