tdr.new {Runuran} | R Documentation |
UNU.RAN random variate generator for continuous distributions with given probability density function (PDF). It is based on the Transformed Density Rejection method (‘TDR’).
[Universal] – Rejection Method.
tdr.new(pdf, dpdf=NULL, lb, ub, islog=FALSE, ...)
pdf |
probability density function. (R function) |
dpdf |
derivative of pdf . (R function) |
lb |
lower bound of domain;
use -Inf if unbounded from left. (numeric) |
ub |
upper bound of domain;
use Inf if unbounded from right. (numeric) |
islog |
whether pdf is given as log-density (the
dpdf must then be the derivative of the log-density). (boolean) |
... |
(optional) arguments for pdf |
This function creates an unuran
object based on ‘TDR’
(Transformed Density Rejection). It can be used to draw samples of a
continuous random variate with given probability density function
using ur
.
The density must be provided by a function pdf
which must
return non-negative numbers and which need not be normalized (i.e., it
can be any multiple of a density function).
Moreover, the given function must be T_(-0.5)-concave
(i.e., unimodal densities with tails not higher than
(1/x^2); this includes all log-concave distributions).
It is recommended to use the log-density (instead of the density function) as this is numerically more stable.
The derivative dpdf
is optional. If omitted, numerical
differentiation is used. Notice, however, that this might cause some
round-off errors such that generation does not work. This is in
particular the case when the density function is provided instead of
the log-density.
The setup time of this method depends on the given PDF, whereas its marginal generation times are almost independent of the target distribution.
There exists a variant of ‘TDR’ which is numerically more
stable (albeit a bit slower and less flexible) which is avaible
via the ars.new
function.
Josef Leydold and Wolfgang H"ormann unuran@statmath.wu-wien.ac.at.
W. H"ormann, J. Leydold, and G. Derflinger (2004): Automatic Nonuniform Random Variate Generation. Springer-Verlag, Berlin Heidelberg. See Chapter 4 (Tranformed Density Rejection).
ur
, ars.new
,
unuran.new
, unuran
.
## Create a sample of size 100 for a Gaussian distribution pdf <- function (x) { exp(-0.5*x^2) } gen <- tdr.new(pdf=pdf, lb=-Inf, ub=Inf) x <- ur(gen,100) ## Create a sample of size 100 for a ## Gaussian distribution (use logPDF) logpdf <- function (x) { -0.5*x^2 } gen <- tdr.new(pdf=logpdf, islog=TRUE, lb=-Inf, ub=Inf) x <- ur(gen,100) ## Same example but additionally provide derivative of log-density ## to prevent possible round-off errors logpdf <- function (x) { -0.5*x^2 } dlogpdf <- function (x) { -x } gen <- tdr.new(pdf=logpdf, dpdf=dlogpdf, islog=TRUE, lb=-Inf, ub=Inf) x <- ur(gen,100) ## Draw sample from Gaussian distribution with mean 1 and ## standard deviation 2. Use 'dnorm'. gen <- tdr.new(pdf=dnorm, lb=-Inf, ub=Inf, mean=1, sd=2) x <- ur(gen,100) ## Draw a sample from a truncated Gaussian distribution ## on domain [5,Inf) logpdf <- function (x) { -0.5*x^2 } gen <- tdr.new(pdf=logpdf, lb=5, ub=Inf, islog=TRUE) x <- ur(gen,100)