Thom Benjamin Volker
Many statistical models impose multivariate normality, for example, for
example on the regression parameters in a linear regression model. A
question that may arise is how to find the probability that a random
vector falls into some convex region 
If the region pmvnorm function in the
mvtnorm package in R. In such circumstances, we seek to find the
solution of the integral
pnorm() in R.
The following code implements the approach in R. Note, however, that
the current implementations use some additional tricks to increase the
accuracy of the program (or reduce the computation time), by changing
the order of the variables.
set.seed(123)
# Parameters
P <- 5
rho <- 0.5
S <- rho + (1-rho) * diag(P)
mu <- 1:P
# Integration limits
a <- sample(-(1:P)) - mu
b <- 3 * sample(1:P) - mu
# Test results
mvtnorm::pmvnorm(lower = a, upper = b, mean = 0, sigma = S)[1] 0.8409671
attr(,"error")
[1] 1.364544e-05
attr(,"msg")
[1] "Normal Completion"
# Convergence limits
maxerr <- 1e-4
Nmax <- 10000
# Cholesky decomposition
C <- t(chol(S))
# Initialize variables
intsum <- 0
varsum <- 0
N      <- 1
d1 <- pnorm(a[1]/C[1,1])
e1 <- pnorm(b[1]/C[1,1])
f1 <- e1 - d1
# Loop until convergence
conv <- FALSE
out <- numeric(Nmax)
while(!conv) {
  
  w <- runif(P-1)
  di <- d1
  ei <- e1
  fi <- ei - di
  for (i in 2:P) {
    if (i == 2) yi <- 0
    yi[i-1] <- qnorm(di + w[i-1] * (ei - di))
    di <- pnorm((a[i] - sum(C[i, 1:(i-1)]*yi))/C[i,i])
    ei <- pnorm((b[i] - sum(C[i, 1:(i-1)]*yi))/C[i,i])
    fi <- (ei - di) * fi
  }
  intsum <- intsum + fi
  varsum <- varsum + fi^2
  N <- N+1
  err <- 1 * ((varsum/N - (intsum/N)^2)/(N))^.5
  out[N-1] <- intsum/N
  if (N == Nmax | err < maxerr) conv <- TRUE
}
N[1] 10000
err[1] 0.001287477
intsum/N[1] 0.8424054
plot(out[out != 0], type = "l", lwd = 2, col = "blue")
abline(h = mvtnorm::pmvnorm(lower = a, upper = b, mean = 0, sigma = S), lty=2)However, if the integration is not over a hyperrectangle, the current approach cannot be used, and we need to resolve to other techniques.
TO DO
