projectq2a: projectq2a

Description Usage Arguments Value Examples

Description

This function generates random deviates from a continuous random variable with the supplied probability density function via rejection sampling.

Usage

1
projectq2a(n, pdf, a, b, C, ...)

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

lower and upper limits of the distribution. Must be finite such that P(a ≤ X ≤ b) = 1.

C

a numeric such that f(x) ≤ C for all values of x.

...

further arguments passed to or from other methods.

Value

numeric vector of random deviates

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
## sample from standard uniform
projectq2a(n = 1, pdf = dunif, a = 0, b = 1, C = 1, min = 0, max = 1)

## plot many samples from standard uniform as densities
hist(projectq2a(n = 10000, pdf = dunif, a = 0, b = 1, C = 1, min = 0, max = 1),
     probability = TRUE)
curve(dunif, col = "red", add = TRUE)

## sample from beta(2, 2)
projectq2a(n = 1, pdf = dbeta, a = 0, b = 1, C = 1.5, shape1 = 2, shape2 = 2)

## plot many samples from beta(2, 2)
hist(projectq2a(n = 10000, pdf = dbeta, a = 0, b = 1, C = 1.5, shape1 = 2, shape2 = 2),
     probability = TRUE)
curve(dbeta(x, shape1 = 2, shape2 = 2), col = "red", add = TRUE)

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