bbReject: Bounding box rejection sampler

View source: R/sample.R

bbRejectR Documentation

Bounding box rejection sampler

Description

Generates uniform random variates over a convex polytope defined by a set of linear constraints by generating uniform variates over a bounding box and rejecting those outside the polytope.

Usage

bbReject(lb, ub, constr, N, homogeneous=FALSE, transform=NULL)

Arguments

lb

Lower bound for each dimension (not including homogeneous coordinate)

ub

Upper bound for each dimension (not including homogeneous coordinate)

constr

Constraint definition (see details)

N

Number of samples to generate

homogeneous

Whether constr and transform are given in homogeneous coordinate representation (see details)

transform

Transformation matrix to apply to the generated samples (optional)

Details

See har for a description of the constraint definition and the homogeneous coordinate representation.

Value

A list, containing:

samples

A matrix containing the generated samples as rows.

rejectionRate

The mean number of samples rejected for each accepted sample.

Author(s)

Gert van Valkenhoef

See Also

createBoundBox

harConstraints simplex.createTransform simplex.createConstraints

Examples

# constraints: x_1 >= 0, x_2 >= 0, x_1 + x_2 <= 1
A <- rbind(c(-1, 0), c(0, -1), c(1, 1))
b <- c(0, 0, 1)
d <- c("<=", "<=", "<=")
constr <- list(constr=A, rhs=b, dir=d)

# create a bounding box that contains the polytope
lb <- c(0, 0)
ub <- c(1, 1)

# sample 10,000 points
samples <- bbReject(lb, ub, constr, 1E4)$samples

# Check dimension of result
stopifnot(dim(samples) == c(1E4, 2))

# Check that x_i >= 0
stopifnot(samples >= 0)

# Check that x_1 + x_2 <= 1
stopifnot(samples[,1] + samples[,2] <= 1)

plot(samples)


hitandrun documentation built on May 28, 2022, 1:09 a.m.