Description Usage Arguments Value Examples
This function generates semi-random deviates from a continuous random variable with the supplied probability density function via Markov Chain Monte Carlo.
1 | projectq3b(n, pdf, a = NA, b = NA, ...)
|
n |
number of observations. If length(n) > 1, the length is taken to be the number required. |
pdf |
a function that is the pdf of the random variable. |
a, b |
optional lower and upper limits of the distribution. If provided, must be finite such that P(a ≤ X ≤ b) = 1. |
... |
further arguments passed to or from other methods. |
numeric vector of semi-random deviates
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | ## sample from standard uniform
deviates <- projectq3b(n = 10000, pdf = dunif, a = 0, b = 1)
hist(deviates, probability = TRUE)
curve(dunif(x), col = "red", add = TRUE)
## sample from beta(2, 2)
deviates <- projectq3b(n = 10000, pdf = dbeta, a = 0, b = 1, shape1 = 2, shape2 = 2)
hist(deviates, probability = TRUE)
curve(dbeta(x = x, shape1 = 2, shape2 = 2), col = "red", add = TRUE)
## sample from custom random variable
dcustom <- function(x) x / 2
deviates <- projectq3b(n = 10000, pdf = dcustom, a = 0, b = 2)
hist(deviates, probability = TRUE)
curve(dcustom, col = "red", add = TRUE)
## sample from exponential with rate = 2
deviates <- projectq3b(n = 10000, pdf = dexp, rate = 2)
hist(deviates, probability = TRUE)
curve(dexp(x, rate = 2), col = "red", add = TRUE)
## sample from chi-square with df = 1
deviates <- projectq3b(n = 10000, pdf = dchisq, df = 1)
hist(deviates, probability = TRUE)
curve(dchisq(x, df = 1), col = "red", add = TRUE)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.