Skip to contents

Builds a parfn that evaluates inner parameters as expressions in outer parameters, with their derivatives. It is evaluable only after compilation.

Usage

Pexpl(
  trafo,
  parameters = NULL,
  attachInput = FALSE,
  condition = NULL,
  compile = FALSE,
  modelname = NULL,
  verbose = FALSE,
  derivMode = "forward",
  outdir = .dmodOutdir()
)

Arguments

trafo

Named character or eqnvec; names are inner parameters, values are expressions in the outer parameters, in the language of cppDE::expressions.

parameters

Outer parameters; default NULL, the symbols of trafo. Names given here that trafo does not map pass through as identity.

attachInput

Logical. Append the inputs that trafo does not map; varying ones with identity derivatives, fixed ones as fixed. Default FALSE. Can be changed with controls().

condition

NULL (default) for a transformation valid in every condition, or the name of the condition it belongs to.

compile

Logical. TRUE compiles now, FALSE (default) leaves it to compile().

modelname

Base name of the generated files, default NULL, which is "expl_parfn", followed by _<condition>.

verbose

Logical. Print the compiler output, default FALSE.

derivMode

The derivatives to build, one or more of "forward" (default), "reverse", "forward-forward" and "forward-reverse", or "none", as in odemodel(). "forward" builds the Jacobian, attr(, "deriv"), [inner, outer], "forward-forward" the second derivatives, attr(, "deriv2"), [inner, outer, outer]; "reverse" is needed for sweep = "reverse", "forward-reverse" for sweep = "reverse" with deriv2 = TRUE. The second-order directions build "forward" as well. A call that asks for a derivative not built is an error.

outdir

Directory for the generated source and shared object, default getOption("dMod2.outdir"), else the working directory.

Value

A parfn, called as p(pars, ...), see section Calling a dMod function.

Calling a dMod function

The leading arguments depend on the class and are matched by position or by name:

  • prediction function: x(times, pars, ...), returning a prdlist;

  • observation function: g(out, pars, ...) with a prdlist out, returning a prdlist;

  • parameter transformation: p(pars, ...), returning a list with one parvec per condition;

  • objective function: obj(pars, ...), returning an objlist.

pars is a named numeric vector. If it has a deriv attribute, as the output of a parameter transformation does, the derivatives are taken with respect to the parameters behind it. A composition is called according to the resulting class: g * x * p is a prediction function, (g * x * p)(times, pars).

The other arguments are named:

  • fixed: named numeric vector of parameters that enter the evaluation without derivatives. Default NULL.

  • deriv: TRUE (default) returns first derivatives with respect to pars. Every function of the chain has to be built with derivMode = "forward", see odemodel(), Y() and P().

  • deriv2: TRUE returns second derivatives as well. Default FALSE. Every function of the chain has to be built with derivMode = "forward-forward".

  • hessian: objective functions only, see the table below. Default NULL.

  • sweep: objective functions only. "forward" (default) propagates the sensitivities through the chain; "reverse" computes the gradient in one backward pass, whose cost does not grow with the number of parameters. "reverse" needs every function of the chain built with derivMode = "reverse", and with "forward-reverse" for deriv2 = TRUE.

  • conditions: character vector, the conditions to evaluate. Default all conditions of the function.

  • env: environment for intermediate results. Default NULL, a new one. An objective function stores the prediction there as prediction and returns the environment as attribute env.

  • cores: number of threads for the ODE solves and the residual kernel. Default getOption("dMod2.cores", 1L).

Prediction, observation and parameter functions accept hessian and sweep and ignore them. With deriv = TRUE an objective function returns this Hessian:

sweephessianderiv2 = FALSEderiv2 = TRUE
"forward"NULL, TRUEGauss-Newtonexact
"forward"FALSEnonenone, with a warning
"reverse"NULLnoneexact
"reverse"TRUEnone, with a warningexact
"reverse"FALSEnonenone, with a warning

hessian = "sparse" returns the Gauss-Newton Hessian of the forward sweep as a sparse Matrix, assembled from the blocks of the conditions; trust() factorizes it where the argument sparse takes the sparse path, and vcov() solves it for the columns of which.

hessian = "expected" returns, in place of the Gauss-Newton Hessian of the forward sweep, the expected (Fisher) information of the likelihood: 2 / sigma^2 in the prediction and 4 / sigma^2 in sigma per data point, positive semidefinite also where an error model depends on the parameters. The Gauss-Newton Hessian keeps the exact curvature of the loss in the prediction and sigma, which is indefinite there and shows the optimizer a saddle.

With deriv = FALSE it returns the value alone.

See also

Examples

# \donttest{
trafo <- eqnvec(k1 = "exp(log_k1)", k2 = "exp(log_k2)", A = "A0")
p <- Pexpl(trafo, condition = "C1", compile = TRUE,
           modelname = "Pexpl_example", outdir = tempdir())
#> using C++ compiler: g++ [-O2 -DNDEBUG -w -fPIC -fopenmp]
out <- p(c(log_k1 = 0, log_k2 = -1, A0 = 2))
out$C1
#> Parameter vector:
#>   k1 :       1
#>   k2 :  0.367879
#>    A :       2
#> 
#> Attributes:
#>   deriv  : 3 x 3 matrix
#>   deriv2 : <none>
#>   fixed  : <none>
attr(out$C1, "deriv")
#>    log_k1    log_k2 A0
#> k1      1 0.0000000  0
#> k2      0 0.3678794  0
#> A       0 0.0000000  1
# }