\HeaderA{pmvnorm}{Multivariate Normal Distribution}{pmvnorm}
\keyword{distribution}{pmvnorm}
\begin{Description}\relax
Computes the distribution function of the multivariate normal 
distribution for arbitrary limits and correlation matrices 
based on algorithms by Genz and Bretz.
\end{Description}
\begin{Usage}
\begin{verbatim}
pmvnorm(lower=-Inf, upper=Inf, mean=rep(0, length(lower)),
        corr=NULL, sigma=NULL, maxpts = 25000, abseps = 0.001,
        releps = 0)
\end{verbatim}
\end{Usage}
\begin{Arguments}
\begin{ldescription}
\item[\code{lower}] the vector of lower limits of length n.
\item[\code{upper}] the vector of upper limits of length n.
\item[\code{mean}] the mean vector of length n.
\item[\code{corr}] the correlation matrix of dimension n.
\item[\code{sigma}] the covariance matrix of dimension n. Either \code{corr} or
\code{sigma} can be specified. If \code{sigma} is given, the
problem is standardized. If neither \code{corr} nor
\code{sigma} is given, the identity matrix is used 
for \code{sigma}. 
\item[\code{maxpts}] maximum number of function values as integer. 
\item[\code{abseps}] absolute error tolerance as double. 
\item[\code{releps}] relative error tolerance as double. 
\end{ldescription}
\end{Arguments}
\begin{Details}\relax
This program involves the computation of 
multivariate normal probabilities with arbitrary correlation matrices.
It involves both the computation of singular and nonsingular 
probabilities. The methodology is described in
Genz (1992, 1993).

Note that both \code{-Inf} and \code{+Inf} may be specified in \code{lower} and
\code{upper}. For more details see \code{\LinkA{pmvt}{pmvt}}. 

The multivariate normal 
case is treated as a special case of \code{\LinkA{pmvt}{pmvt}} with \code{df=0} and 
univariate problems are passed to \code{\LinkA{pnorm}{pnorm}}.

Multivariate normal density and random numbers are available using
\code{\LinkA{dmvnorm}{dmvnorm}} and \code{\LinkA{rmvnorm}{rmvnorm}}.
\end{Details}
\begin{Value}
The evaluated distribution function is returned with attributes
\begin{ldescription}
\item[\code{error}] estimated absolute error and
\item[\code{msg}] status messages.
\end{ldescription}
\end{Value}
\begin{Author}\relax
Fortran Code by Alan Genz <AlanGenz@wsu.edu> and Frank Bretz
<frank.bretz@pharma.novartis.com>, \R{} port by Torsten Hothorn
<Torsten.Hothorn@rzmail.uni-erlangen.de>
\end{Author}
\begin{References}\relax
Genz, A. (1992). Numerical computation of multivariate normal probabilities.
\emph{Journal of Computational and Graphical Statistics}, \bold{1}, 141--150 

Genz, A. (1993). Comparison of methods for the computation of multivariate
normal probabilities. \emph{Computing Science and Statistics}, \bold{25},
400--405
\end{References}
\begin{SeeAlso}\relax
\code{\LinkA{qmvnorm}{qmvnorm}}
\end{SeeAlso}
\begin{Examples}
\begin{ExampleCode}

n <- 5
mean <- rep(0, 5)
lower <- rep(-1, 5)
upper <- rep(3, 5)
corr <- diag(5)
corr[lower.tri(corr)] <- 0.5
corr[upper.tri(corr)] <- 0.5
prob <- pmvnorm(lower, upper, mean, corr)
print(prob)

stopifnot(pmvnorm(lower=-Inf, upper=3, mean=0, sigma=1) == pnorm(3))

a <- pmvnorm(lower=-Inf,upper=c(.3,.5),mean=c(2,4),diag(2))

stopifnot(round(a,16) == round(prod(pnorm(c(.3,.5),c(2,4))),16))

a <- pmvnorm(lower=-Inf,upper=c(.3,.5,1),mean=c(2,4,1),diag(3))

stopifnot(round(a,16) == round(prod(pnorm(c(.3,.5,1),c(2,4,1))),16))

# Example from R News paper (original by Genz, 1992):

m <- 3
sigma <- diag(3)
sigma[2,1] <- 3/5
sigma[3,1] <- 1/3
sigma[3,2] <- 11/15
pmvnorm(lower=rep(-Inf, m), upper=c(1,4,2), mean=rep(0, m), corr=sigma)

# Correlation and Covariance

a <- pmvnorm(lower=-Inf, upper=c(2,2), sigma = diag(2)*2)
b <- pmvnorm(lower=-Inf, upper=c(2,2)/sqrt(2), corr=diag(2))
stopifnot(all.equal(round(a,5) , round(b, 5)))

\end{ExampleCode}
\end{Examples}

