Package: | Tinflex |
Type: | Package |
Version: | 2.4 |
Date: | 2023-03-21 |
License: | GPL 2 or later |
Package Tinflex serves three purposes:
The installed package provides a fast routine for
sampling from any distribution that has a piecewise twice
differentiable density function.
It provides C routines functions that could be used in
other packages (see the installed C header files).
The R source (including comments) presents all details of the
general sampling method which are not entirely worked out in our
paper cited in the see references below.
Algorithm Tinflex
is a universal random variate generator based
on transformed density rejection which is a variant of the
acceptance-rejection method. The generator first computes
and stores hat and squeeze functions and then uses these functions
to generate variates from the distribution of interest. Since the
setup procedure is separated from the generation procedure, many
samples can be drawn from the same distribution without rerunning the
(expensive) setup.
The algorithm requires the following data about the distribution
(for further details see Tinflex.setup
):
the log-density of the targent distribution;
its first derivative;
its second derivative (optionally);
a starting partition of its domain such that each subinterval
contains at most one inflection point of the transformed density;
a transformation for the density
(default is the logarithm transformation).
The following routines are provided.
Tinflex.setup
computes hat and squeeze. The
table is then stored in a generator object of class
"Tinflex"
.
Tinflex.sample
draws a random sample from a
particular generator object.
print.Tinflex
prints the properties a generator
object of class "Tinflex"
.
plot.Tinflex
plots density, hat and squeeze
functions for a given generator object of class "Tinflex"
.
For further details see Tinflex.setup
.
There are variants of the method.
The first one uses the second derivative to determine regions whre the
transformed density is convex, concave, or has a single inflection
points.
The second variant estimates the signs on the second derivative by
means of the first derivative. Thus it is easier to use at the expense
of a more complex algorithm.
There are two different implementation:
Routine Tinflex.setup
is implemented mainly in R and
serves (together with Tinflex:::Tinflex.sample.R
) as
a reference implementation of the published algorithm.
Nevertheless, the sampling routine Tinflex.sample
runs
quite fast.
Routine Tinflex.setup.C
on the other hand is implemented
entirely in C. So it also allows to link to the underlying C code from
other packages.