projectq3b: projectq3b

Description Usage Arguments Value Examples

Description

This function generates semi-random deviates from a continuous random variable with the supplied probability density function via Markov Chain Monte Carlo.

Usage

1
projectq3b(n, pdf, a = NA, b = NA, ...)

Arguments

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.

Value

numeric vector of semi-random deviates

Examples

 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)

schuelkem/samplr documentation built on May 6, 2019, 7:19 a.m.