Solves \(f(x, p) = 0\) for the states \(x\) by pseudo-transient
continuation (cppDE::ptc()) and returns a parfn with sensitivities from
the implicit function theorem.
Arguments
- trafo
Named character, eqnvec or eqnlist defining \(f\), in the language of cppDE::expressions.
- parameters
Outer parameter names. For an eqnlist the totals \(T\) of the conserved quantities are added. States named here are held at their value instead of solved for, and a state whose right-hand side is
"0"becomes a parameter.- forcings
Forcing names, set to 0. Default
NULL.- condition
NULL(default) for a transformation valid in every condition, or the name of the condition it belongs to.- keepRoot
If
TRUE(default), roots are kept and repeated calls with the same values are answered from memory.- flow
If
TRUE, \(\dot{x} = f(x, p)\) and a stable steady state is returned. IfFALSE, any regular root. DefaultTRUEfor an eqnlist, elseFALSE.- compile
Logical.
TRUEcompiles now,FALSE(default) leaves it tocompile().- modelname
Base name of the generated files, default
NULL, which is"impl_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 inodemodel(). The reverse directions come from the forward ones:"reverse"builds the Jacobian,"forward-reverse"the second derivatives, as"forward-forward"does. A call that asks for a derivative not built is an error.- controlsPTC
Named list of solver controls; an entry not given keeps the default, an unknown entry is an error:
reltol,abstolConvergence tolerances, default
1e-10,1e-14.flowTolRelative local error per pseudo-time step, default
1.maxitIterations per start, default
ceiling(70 * log(n + 1))fornstates.dtInitInitial pseudo-time step relative to the fastest rate, default
1e-2.positiveKeep states positive, default
TRUE.stabilityRequire a stable root if
flow = TRUE, defaultTRUE; where no start reaches one, the evaluation fails. WithFALSEan unstable root is returned, on which aconstraintStability()term rises.stabilityTolWith
stability, a root counts as stable while the real part of the leading eigenvalue on the manifold of the conserved quantities is at most the larger of1e-8times the largest eigenvalue modulus there, the precision of the eigenvalues, andstabilityTol, an absolute value in the units of the rates; default0. A positive value admits a resting state that departs from rest more slowly than a time scale of interest, as near a Hopf bifurcation.constraintStability()reaches the weight atstabilityTol.nStartsRandom starts after the deterministic ones, default
20.startRangeRange of a random start, drawn uniformly per state on
startScale, defaultc(-5, 5).startScale"log10"or"linear", default"log10"ifpositiveand"linear"otherwise.seedSeed of the random starts, default
1. The starts depend on the seed and the values; the global RNG is not used.
- outdir
Directory for the generated files, default
getOption("dMod2.outdir"), else the working directory.
Value
A parfn, called as p(pars, ...), see section Calling a dMod
function.
Details
Conserved quantities of an eqnlist enter as \(C x = T\).
States without influx are 0. A state whose reactions all vanish with them
rests: it takes the value from pars, 0 where pars has none, and is not
among the parameters. The root is solved from the states in pars,
missing ones at 1, then from all states at 1, then from nStarts random
starts; with flow = TRUE the stable root the flow leads to. If all fail, it
is an error. A root depends on the parameters, the states in pars and the
controls only, not on the calls before it. Identical values are solved once,
also across conditions, and conditions are solved in parallel with the
cores of the evaluation. keepRoot and controlsPTC can be changed with
controls().
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 prdlistout, 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. DefaultNULL.deriv:TRUE(default) returns first derivatives with respect topars. Every function of the chain has to be built withderivMode = "forward", seeodemodel(),Y()andP().deriv2:TRUEreturns second derivatives as well. DefaultFALSE. Every function of the chain has to be built withderivMode = "forward-forward".hessian: objective functions only, see the table below. DefaultNULL.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 withderivMode = "reverse", and with"forward-reverse"forderiv2 = TRUE.conditions: character vector, the conditions to evaluate. Default all conditions of the function.env: environment for intermediate results. DefaultNULL, a new one. An objective function stores the prediction there aspredictionand returns the environment as attributeenv.cores: number of threads for the ODE solves and the residual kernel. DefaultgetOption("dMod2.cores", 1L).
Prediction, observation and parameter functions accept hessian and
sweep and ignore them. With deriv = TRUE an objective function returns
this Hessian:
sweep | hessian | deriv2 = FALSE | deriv2 = TRUE |
"forward" | NULL, TRUE | Gauss-Newton | exact |
"forward" | FALSE | none | none, with a warning |
"reverse" | NULL | none | exact |
"reverse" | TRUE | none, with a warning | exact |
"reverse" | FALSE | none | none, 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
Pexpl(), P(); the guide chapter
Setting up models.
Examples
# \donttest{
## Steady state of A <-> B; the total of A and B enters as a parameter
r <- addReaction(NULL, "A", "B", "k1*A")
r <- addReaction(r, "B", "A", "k2*B")
p <- Pimpl(r, condition = "C1", compile = TRUE,
modelname = "Pimpl_example", outdir = tempdir())
#> using C++ compiler: g++ [-O2 -DNDEBUG -w -fPIC -fopenmp]
#> using C++ compiler: g++ [-O2 -DNDEBUG -w -fPIC -fopenmp]
getParameters(p)
#> [1] "k1" "k2" "total_1"
pars <- setNames(c(1, 0.5, 3), getParameters(p))
out <- p(pars)
out$C1
#> Parameter vector:
#> A : 1
#> B : 2
#> k1 : 1
#> k2 : 0.5
#> total_1 : 3
#>
#> Attributes:
#> deriv : 5 x 3 matrix
#> deriv2 : <none>
#> fixed : <none>
attr(out$C1, "deriv")
#> k1 k2 total_1
#> A -0.6666667 1.333333 0.3333333
#> B 0.6666667 -1.333333 0.6666667
#> k1 1.0000000 0.000000 0.0000000
#> k2 0.0000000 1.000000 0.0000000
#> total_1 0.0000000 0.000000 1.0000000
# }