我想生成具有下限和上限的多元正态分布的随机数。以下是示例代码:
x1 <- qnorm(runif(100,min=pnorm(0),max=pnorm(1)))*(100-10)+10
x2 <- qnorm(runif(100,min=pnorm(0),max=pnorm(1)))
x3 <- qnorm(runif(100,min=pnorm(0),max=pnorm(1)))*(10-0.01)+0.01
x <- as.matrix(cbind(x1,x2,x3))
mu <- colMeans(x)
sigma <- cov(x)
library(mvtnorm)
x.test <- rmvnorm(n=10000, mean=mu, sigma=sigma)
mu.test <- colMeans(x.test)
sigma.test <- cov(x.test)
all.equal(mu,mu.test)
all.equal(sigma,sigma.test)
x.test.sub <- subset(x.test,
x.test[,1] <= 100 & x.test[,1] >= 10 &
x.test[,2] <= 1 & x.test[,2] >= 0 &
x.test[,3] <= 10 & x.test[,3] >= 0.01)
print(nrow(x.test.sub))
您可以看到超出限制的一些行。那么有人可以告诉我如何修改代码以执行带有限制的多元正态分布的随机样本?提前致谢!