mtrp_unif: Metropolis-Hastings Algorithm Using Uniform Distribution

Description Usage Arguments Value Examples

View source: R/mtrp_unif.R

Description

MCMC method for continuous random vector with uniform distribution as proposal distribution using an independence sampler algorithm.

Usage

1
mtrp_unif(f, n, init, a, b, burn = 1000)

Arguments

f

Density function from which one wants to sample.

n

The numbers of samples one wants to obtain.

init

The initial value vector, which indicates the dimensions.

a, b

The left and right support boundary vector. if a numeric number is provided, it will be broadcast to a vector with the same length as init.

burn

Times of iterations one wants to omit before recording.

Value

A "mcmcn" object 'list("chain" = chain, "reject" = k/iters, "acpt" = acpt)' with chain storing samples by row, reject being the rejection rate, acpt being whether to be accepted each iter.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# the Exp(1) distribution right-truncated at 1-------------------------------
# i.e., with support [0,1]

# pdf f(x) = exp(1 - x) / (e - 1)
f <- function(x) ifelse(x >= 0 && x <=1, exp(-x), 0)

# generating random variates using function `mtrp_unif`
x.exp2 <- mtrp_unif(f, 10000, 1, a = 0, b = 1, burn = 0)

# exploring the results
summary(x.exp2)
plot(x.exp2, "c")
hist(x.exp2$chain, freq = FALSE, main = "Histogram of Samples", xlab = "X")
curve(exp(1 - x) / (exp(1) - 1), from = 0, to = 1, col = "red", add = TRUE)

hjy78/mcmcn documentation built on Jan. 1, 2020, 1:03 p.m.