This function generates random numbers from the truncated multivariate normal
distribution with mean equal to mean
and covariance matrix
sigma
and general linear constraints
rtmvnorm2(n, mean = rep(0, nrow(sigma)),
sigma = diag(length(mean)),
lower = rep(-Inf, length = length(mean)),
upper = rep(Inf, length = length(mean)),
D = diag(length(mean)),
algorithm = c("gibbs", "gibbsR", "rejection"), ...)
Number of random points to be sampled. Must be an integer
Mean vector (d x 1), default is rep(0, length = ncol(x))
.
Covariance matrix (d x d), default is diag(ncol(x))
.
Vector of lower truncation points (r x 1),
default is rep( Inf, length = length(mean))
.
Vector of upper truncation points (r x 1),
default is rep( Inf, length = length(mean))
.
Matrix for linear constraints (r x d), defaults to diagonal matrix (d x d), i.e. r = d.
Method used, possible methods are the Fortan Gibbs sampler ("gibbs", default), the Gibbs sampler implementation in R ("gibbsR") and rejection sampling ("rejection")
additional parameters for Gibbs sampling, given to the internal method rtmvnorm.gibbs()
,
such as burn.in.samples
, start.value
and thinning
, see details in rtmvnorm
This method will be merged with rtmvnorm
in one of the next releases.
Stefan Wilhelm
This method allows for rtmvnorm
requires a full-rank matrix D lower
and upper
are D
is
rtmvnorm
if (FALSE) {
################################################################################
#
# Example 5a: Number of linear constraints r > dimension d
#
################################################################################
# general linear restrictions a <= Dx <= b with x (d x 1); D (r x d); a,b (r x 1)
# Dimension d=2, r=3 linear constraints
#
# a1 <= x1 + x2 <= b2
# a2 <= x1 - x2 <= b2
# a3 <= 0.5x1 - x2 <= b3
#
# [ a1 ] <= [ 1 1 ] [ x1 ] <= [b1]
# [ a2 ] [ 1 -1 ] [ x2 ] [b2]
# [ a3 ] [ 0.5 -1 ] [b3]
D <- matrix(
c( 1, 1,
1, -1,
0.5, -1), 3, 2, byrow=TRUE)
a <- c(0, 0, 0)
b <- c(1, 1, 1)
# mark linear constraints as lines
plot(NA, xlim=c(-0.5, 1.5), ylim=c(-1,1))
for (i in 1:3) {
abline(a=a[i]/D[i, 2], b=-D[i,1]/D[i, 2], col="red")
abline(a=b[i]/D[i, 2], b=-D[i,1]/D[i, 2], col="red")
}
### Gibbs sampling for general linear constraints a <= Dx <= b
mean <- c(0, 0)
sigma <- matrix(c(1.0, 0.2,
0.2, 1.0), 2, 2)
x0 <- c(0.5, 0.2) # Gibbs sampler start value
X <- rtmvnorm2(n=1000, mean, sigma, lower=a, upper=b, D, start.value=x0)
# show random points within simplex
points(X, pch=20, col="black")
}
Run the code above in your browser using DataLab