bbReject | R Documentation |
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.
bbReject(lb, ub, constr, N, homogeneous=FALSE, transform=NULL)
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) |
See har
for a description of the constraint definition and the homogeneous coordinate representation.
A list, containing:
samples |
A matrix containing the generated samples as rows. |
rejectionRate |
The mean number of samples rejected for each accepted sample. |
Gert van Valkenhoef
createBoundBox
harConstraints
simplex.createTransform
simplex.createConstraints
# 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.