customll {scorematchingad} | R Documentation |
Compile a custom log-likelihood function.
Description
Supply C++
code to specify a custom log-likelihood, much like TMB::compile()
is passed C++
code that formulate models.
For score matching the normalising constant of the log-likelihood can be omitted.
Taping of the function currently only works with the gcc
compiler, typically on Linux; taping works if customll_test()
returns TRUE
.
Packages RcppEigen
and RcppXPtrUtils
must be installed.
Usage
customll(
code,
rebuild = FALSE,
includes = character(),
cacheDir = getOption("rcpp.cache.dir", tempdir()),
showOutput = verbose,
verbose = getOption("verbose")
)
customll_test()
Arguments
code |
|
rebuild |
passed to |
includes |
passed to |
cacheDir |
passed to |
showOutput |
passed to |
verbose |
passed to |
Details
The function uses RcppXPtrUtils::cppXPtr()
and Rcpp::cppFunction()
.
It is good practice to check the returned object using evalll()
.
The first compilation in a session can be very slow.
Value
customll()
returns an adloglikelood
object (which is just an externalptr
with attributes) for the compiled log-likelihood function. The returned object has an attribute fname
.
customll_test()
returns TRUE
if taping works in your R
session. Otherwise it will return FALSE
and generate warnings.
Code Argument
code
must be C++
that uses only CppAD
and Eigen
, which makes it very similar to the requirements of the input to TMB::compile()
(which also uses CppAD
and Eigen
).
The start of code
should always be "a1type fname(const veca1 &x, const veca1 &theta){
" where fname
is your chosen name of the log-likelihood function, x
represents a point in the data space and theta
is a vector of parameters for the log-likelihood. This specifies that the function will have two vector arguments (of type veca1
) and will return a single numeric value (a1type
).
The type a1type
is a double with special ability for being taped by CppAD
. The veca1
type is a vector of a1type
elements, with the vector wrapping supplied by the Eigen
C++ package (that is an Eigen
matrix with 1 column and dynamic number of rows).
The body of the function must use operations from Eigen and/or CppAD, prefixed by Eigen::
and CppAD::
respectively.
There are no easy instructions for writing these as it is genuine C++
code, which can be very opaque to those unfamiliar with C++
.
I've found the quick reference pages for for Eigen
useful. Limited unary and binray operations are available directly from CppAD
without Eigen
.
For the purposes of score matching the operations should all be smooth to create a smooth log-likelihood and the normalising constant may be omitted.
Examples
## Not run: customll_test()
myll <- customll("a1type dirichlet(const veca1 &u, const veca1 &beta) {
size_t d = u.size();
a1type y(0.); // initialize summation at 0
for(size_t i = 0; i < d; i++)
{ y += beta[i] * log(u[i]);
}
return y;
}")
evalll(myll, rep(1/3, 3), rep(-0.5, 3))
tapes <- buildsmdtape("sim", "identity", "sim",
myll, rep(1/3, 3), rep(NA, 3),
bdryw="minsq", acut = 0.01)
evaltape(tapes$lltape, rep(1/3, 3), rep(-0.5, 3))
## End(Not run)