Rejection sampling from the (pseudo) pdf f in the interval between x_min and x_max. Returns n samples. Note that values of f below 0 are capped to zero
Arguments
- f
function. (pseudo) pdf from which the sample is drawn
- x_min
number, lower limit of the examined interval
- x_max
number, upper limit of the examined interval
- n
integer. number of samples drawn
- f_max
number, maximum value of
fin the interval fromx_mintox_max. If f attains values larger thanf_maxa warning is throw,f_maxis adjusted, and sampling is started again- max_try
maximum number of tries in the rejection sampling algorithm. If more tries are needed, an error is thrown. If this is the case, inspect of your function
fis well-defined and positive, and iff_maxprovides a reasonable upper bound on it. Adjustmax_tryif you are certain that both is the case, e.g. iffis highly irregular.
See also
p3_var_rate() for the derived variable rate Poisson point process implementation.
Examples
f = sin
x = rej_samp(f, 0, 3*pi, n = 100)
hist(x) # note that no samples are drawn where sin is negative