Description Usage Arguments Value Examples
This function implements single variabel rejection sampling for rvs with bounded support and which have have bounded pdf.
1 | oneDsample(f, N, lb = -Inf, ub = Inf, discrete = FALSE)
|
f |
the pdf that we are sampling from |
N |
the number of attempted samples. |
lb |
lower bound of support of f |
ub |
upper bound of support of f |
A vector containing samples from pdf
1 2 3 4 5 6 7 8 9 10 11 12 | f <- function(x) {ifelse(0 < x & x < 1, 4*x^3, 0)}
a <- oneDsample(f,10000)
ggplot(a,aes(x)) + geom_density() + stat_function(fun = f, color = "red")
f <- function(x) {1/pi/(1+x^2)}
a <- oneDsample(f,20000)
ggplot(a,aes(x)) + geom_density() + stat_function(fun = f, color = "red")
f <- function(x) {dunif(x, -100, 100)}
a <- oneDsample(f,50000, -100, 100)
a <- oneDsample(f,500)
ggplot(a,aes(x)) + geom_density() + stat_function(fun = f, color = "red")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.