# NOT RUN {
################################################################################
#
# 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