trmvrnorm_rej_cpp | R Documentation |
Simulates realizations from a truncated multivariate normal with mean mu, covariance matrix sigma in the bounds lower upper.
trmvrnorm_rej_cpp(n, mu, sigma, lower, upper, verb)
n |
number of simulations. |
mu |
mean vector. |
sigma |
covariance matrix. |
lower |
vector of lower bounds. |
upper |
vector of upper bounds. |
verb |
level of verbosity: if lower than 3 nothing, 3 minimal, 4 extended. |
A matrix of size d x n
containing the samples.
Horrace, W. C. (2005). Some results on the multivariate truncated normal distribution. Journal of Multivariate Analysis, 94(1):209–221.
Robert, C. P. (1995). Simulation of truncated normal variables. Statistics and Computing, 5(2):121–125.
# Simulate 1000 realizations from a truncated multivariate normal vector
mu <- rep(0,10)
Sigma <- diag(rep(1,10))
upper <- rep(3,10)
lower <- rep(-0.5,10)
realizations<-trmvrnorm_rej_cpp(n=1000,mu = mu,sigma=Sigma, lower =lower, upper= upper,verb=3)
empMean<-rowMeans(realizations)
empCov<-cov(t(realizations))
# check if the sample mean is close to the actual mean
maxErrorOnMean<-max(abs(mu-empMean))
# check if we can estimate correctly the covariance matrix
maxErrorOnVar<-max(abs(rep(1,200)-diag(empCov)))
maxErrorOnCov<-max(abs(empCov[lower.tri(empCov)]))
## Not run:
plot(density(realizations[1,]))
hist(realizations[1,],breaks="FD")
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.