Build new tapes (i.e ADFun
objects) from existing tapes, including differentiation, swapping independent variables and dynamic parameters, and Jacobian determinants.
tapeJacobian(tape)tapeHessian(tape)
tapeGradOffset(tape)
tapeLogJacDet(tape)
tapeSwap(tape)
An ADFun
object.
An ADFun
object.
tapeJacobian
: Tape the Jacobian of a tape. The resulting tape returns the Jacobian as a vector.
tapeHessian
: Tape the Hessian of a tape. The resulting tape returns the Jacobian as a vector (see https://cppad.readthedocs.io/latest/Hessian.html).
tapeGradOffset
: A quadratic function can be written as
$$f(x;\theta) = \frac{1}{2} x^T W(\theta) x + b(\theta)^Tx + c.$$
The function tapeGradOffset
creates a tape of \(b(\theta)\) where \(\theta\) is the independent variable.
tapeLogJacDet
: Creates a tape of the log of the Jacobian determinant of a function taped in tape
. The dimensions of the domain (length of independent variable) and range (length of output variable) of tape
must be equal for computation of the determinant.
tapeSwap
: Convert an ADFun so that the independent variables become dynamic parameters
and the dynamic parameters become independent variables.
The information in the fields xtape
and dyntape
of tape
are used to perform the taping.
The returned vector is ordered with the range elements iterating fastest, then the domain elements. See https://cppad.readthedocs.io/latest/Jacobian.html.
Suppose the function represented by tape
maps from \(d\)-dimensional space to \(1\)-dimensional space, then
the first \(d\) elements of the vector is the gradient of the partial derivative with respect to the first dimension of the function's domain.
The next \(d\) elements of the vector is the gradient of the partial derivative of the second dimension of the function's domain.
The Hessian as a matrix, can be obtained by using matrix()
with ncol = d
.
A quadratic function can be written as
$$f(x;\theta) = \frac{1}{2} x^T W(\theta) x + b(\theta)^Tx + c,$$
where the vector \(x\) is the independent variable of tape
and the vector \(\theta\) is the dynamic parameter vector of tape
.
The gradient of \(f(x; \theta)\) with respect to \(x\) is
$$\Delta f(x; \theta) = \frac{1}{2}(W(\theta) + W(\theta)^T)x + b(\theta).$$
The Hessian is
$$H f(x; \theta) = \frac{1}{2}(W(\theta) + W(\theta)^T),$$
which does not depend on \(x\),
so the gradient of the function can be rewritten as
$$\Delta f(x;\theta) = H f(x; \theta) x + b(\theta)^T.$$
The tape calculates \(b(\theta)\) as
$$b(\theta) = \Delta f(x;\theta) - H f(x; \theta) x,$$
which does not depend on \(x\).
ADFun
Other tape builders:
buildsmdtape()
tapes <- buildsmdtape("sph", "identity", "sph", "vMF",
ytape = rep(1, 3)/sqrt(3),
usertheta = rep(NA, 3)
)
tapeJacobian(tapes$smdtape)
tapeHessian(tapes$smdtape)
tapeLogJacDet(tapeJacobian(tapes$smdtape))
tapeSwap(tapes$smdtape)
Run the code above in your browser using DataLab