mvtnorm/0000755000176200001440000000000014623152553011762 5ustar liggesusersmvtnorm/NAMESPACE0000644000176200001440000000312614623127655013210 0ustar liggesusers useDynLib(mvtnorm, .registration = TRUE, .fixes = "mvtnorm_") importFrom("stats", pnorm, qnorm, rnorm, dnorm, runif, pt, qt, rchisq, uniroot, cov2cor, optim, coef, glm, pcauchy, qcauchy, predict, quasi) export(rmvnorm, dmvnorm, pmvnorm, pmvt, rmvt, qmvnorm, qmvt, dmvt, GenzBretz, Miwa, TVPACK, ltMatrices, syMatrices, as.syMatrices, Tcrossprod, Crossprod, diagonals, chol2cov, invchol2chol, chol2invchol, invchol2cov, invchol2pre, chol2pre, Dchol, invcholD, chol2cor, invchol2cor, invchol2pc, chol2pc, "diagonals<-", vectrick, Lower_tri, marg_mvnorm, cond_mvnorm, Mult, lpmvnorm, slpmvnorm, ldmvnorm, sldmvnorm, ldpmvnorm, sldpmvnorm, standardize, destandardize) S3method("as.array", "ltMatrices") S3method("as.array", "syMatrices") S3method("[", "ltMatrices") S3method("[", "syMatrices") S3method("print", "ltMatrices") S3method("print", "syMatrices") S3method("dim", "ltMatrices") S3method("dimnames", "ltMatrices") S3method("names", "ltMatrices") S3method("dim", "syMatrices") S3method("dimnames", "syMatrices") S3method("names", "syMatrices") S3method("solve", "ltMatrices") S3method("diagonals", "ltMatrices") S3method("diagonals", "syMatrices") S3method("diagonals", "matrix") S3method("diagonals", "integer") S3method("diagonals<-", "ltMatrices") S3method("diagonals<-", "syMatrices") S3method("chol", "syMatrices") S3method("aperm", "ltMatrices") S3method("Mult", "ltMatrices") S3method("Mult", "syMatrices") S3method("Mult", "default") ### internal methods S3method("probval", "GenzBretz") S3method("probval", "Miwa") S3method("probval", "TVPACK") mvtnorm/man/0000755000176200001440000000000014620624356012537 5ustar liggesusersmvtnorm/man/pmvnorm.Rd0000644000176200001440000001262614436104625014530 0ustar liggesusers\name{pmvnorm} \alias{pmvnorm} \title{ Multivariate Normal Distribution } \description{ Computes the distribution function of the multivariate normal distribution for arbitrary limits and correlation matrices. } \usage{ pmvnorm(lower=-Inf, upper=Inf, mean=rep(0, length(lower)), corr=NULL, sigma=NULL, algorithm = GenzBretz(), keepAttr=TRUE, seed = NULL, \dots) } \arguments{ \item{lower}{ the vector of lower limits of length n.} \item{upper}{ the vector of upper limits of length n.} \item{mean}{ the mean vector of length n.} \item{corr}{ the correlation matrix of dimension n.} \item{sigma}{ the covariance matrix of dimension n less than 1000. Either \code{corr} or \code{sigma} can be specified. If \code{sigma} is given, the problem is standardized internally. If \code{corr} is given, it is assumed that appropriate standardization was performed by the user. If neither \code{corr} nor \code{sigma} is given, the identity matrix is used for \code{sigma}. } \item{algorithm}{ an object of class \code{\link{GenzBretz}}, \code{\link{Miwa}} or \code{\link{TVPACK}} specifying both the algorithm to be used as well as the associated hyper parameters.} \item{keepAttr}{\code{\link{logical}} indicating if \code{\link{attributes}} such as \code{error} and \code{msg} should be attached to the return value. The default, \code{TRUE} is back compatible.} \item{seed}{an object specifying if and how the random number generator should be initialized, see \code{\link[stats]{simulate}}.} \item{\dots}{ additional parameters (currently given to \code{\link{GenzBretz}} for backward compatibility issues). } } \details{ This program involves the computation of multivariate normal probabilities with arbitrary correlation matrices. It involves both the computation of singular and nonsingular probabilities. The implemented methodology is described in Genz (1992, 1993) (for algorithm GenzBretz), in Miwa et al. (2003) for algorithm Miwa (useful up to dimension 20) and Genz (2004) for the TVPACK algorithm (which covers 2- and 3-dimensional problems for semi-infinite integration regions). Note the default algorithm GenzBretz is randomized and hence slightly depends on \code{\link{.Random.seed}} and that both \code{-Inf} and \code{+Inf} may be specified in \code{lower} and \code{upper}. For more details see \code{\link{pmvt}}. The multivariate normal case is treated as a special case of \code{\link{pmvt}} with \code{df=0} and univariate problems are passed to \code{\link{pnorm}}. The multivariate normal density and random deviates are available using \code{\link{dmvnorm}} and \code{\link{rmvnorm}}. \code{pmvnorm} is based on original implementations by Alan Genz, Frank Bretz, and Tetsuhisa Miwa developed for computing accurate approximations to the normal integral. Users interested in computing log-likelihoods involving such normal probabilities should consider function \code{\link{lpmvnorm}}, which is more flexible and efficient for this task and comes with the ability to evaluate score functions. } \value{ The evaluated distribution function is returned, if \code{keepAttr} is true, with attributes \item{error}{estimated absolute error} \item{msg}{status message(s).} \item{algorithm}{a \code{\link{character}} string with \code{class(algorithm)}.} } \references{ 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. Genz, A. (2004), Numerical computation of rectangular bivariate and trivariate normal and t-probabilities, \emph{Statistics and Computing}, \bold{14}, 251--260. Genz, A. and Bretz, F. (2009), \emph{Computation of Multivariate Normal and t Probabilities}. Lecture Notes in Statistics, Vol. 195. Springer-Verlag, Heidelberg. Miwa, T., Hayter J. and Kuriki, S. (2003). The evaluation of general non-centred orthant probabilities. \emph{Journal of the Royal Statistical Society}, Ser. B, 65, 223--234. } %%% can't validate certificate anymore %\source{ % \url{http://www.sci.wsu.edu/math/faculty/genz/homepage} %} \seealso{\code{\link{qmvnorm}} for quantiles and \code{\link{lpmvnorm}} for log-likelihoods.} \examples{ 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))) } \keyword{distribution} mvtnorm/man/algorithms.Rd0000644000176200001440000000653114172227731015202 0ustar liggesusers\name{algorithms} \alias{GenzBretz} \alias{Miwa} \alias{TVPACK} \title{ Choice of Algorithm and Hyper Parameters } \description{ Choose between three algorithms for evaluating normal (and t-) distributions and define hyper parameters. } \usage{ GenzBretz(maxpts = 25000, abseps = 0.001, releps = 0) Miwa(steps = 128, checkCorr = TRUE, maxval = 1e3) TVPACK(abseps = 1e-6) } \arguments{ \item{maxpts}{maximum number of function values as integer. The internal FORTRAN code always uses a minimum number depending on the dimension. (for example 752 for three-dimensional problems).} \item{abseps}{absolute error tolerance; for \code{TVPACK} only used for dimension 3.} \item{releps}{ relative error tolerance as double. } \item{steps}{number of grid points to be evaluated; cannot be larger than 4097.} \item{checkCorr}{logical indicating if a check for singularity of the correlation matrix should be performed (once per function call to \code{pmvt()} or \code{pmvnorm()}).} \item{maxval}{replacement for \code{Inf} when non-orthant probabilities involving \code{Inf} shall be computed.} } \details{ There are three algorithms available for evaluating normal (and two algorithms for t-) probabilities: The default is the randomized Quasi-Monte-Carlo procedure by Genz (1992, 1993) and Genz and Bretz (2002) applicable to arbitrary covariance structures and dimensions up to 1000. For normal probabilities, smaller dimensions (up to 20) and non-singular covariance matrices, the algorithm by Miwa et al. (2003) can be used as well. This algorithm can compute orthant probabilities (\code{lower} being \code{-Inf} or \code{upper} equal to \code{Inf}). Non-orthant probabilities are computed from the corresponding orthant probabilities, however, infinite limits are replaced by \code{maxval} along with a warning. For two- and three-dimensional problems and semi-infinite integration region, \code{TVPACK} implements an interface to the methods described by Genz (2004). } \value{ An object of class \code{"GenzBretz"}, \code{"Miwa"}, or \code{"TVPACK"} defining hyper parameters. } \references{ 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. Genz, A. and Bretz, F. (2002), Methods for the computation of multivariate t-probabilities. \emph{Journal of Computational and Graphical Statistics}, \bold{11}, 950--971. Genz, A. (2004), Numerical computation of rectangular bivariate and trivariate normal and t-probabilities, \emph{Statistics and Computing}, \bold{14}, 251--260. Genz, A. and Bretz, F. (2009), \emph{Computation of Multivariate Normal and t Probabilities}. Lecture Notes in Statistics, Vol. 195. Springer-Verlag, Heidelberg. Miwa, A., Hayter J. and Kuriki, S. (2003). The evaluation of general non-centred orthant probabilities. \emph{Journal of the Royal Statistical Society}, Ser. B, 65, 223--234. Mi, X., Miwa, T. and Hothorn, T. (2009). \code{mvtnorm}: New numerical algorithm for multivariate normal probabilities. \emph{The R Journal} \bold{1}(1): 37--39. \url{https://journal.r-project.org/archive/2009-1/RJournal_2009-1_Mi+et+al.pdf} } \keyword{distribution} mvtnorm/man/mvtnorm-package.Rd0000644000176200001440000000320014436110035016101 0ustar liggesusers\name{mvtnorm-package} \alias{mvtnorm-package} \alias{mvtnorm} \docType{package} \title{ \packageTitle{mvtnorm} } \description{ \packageDescription{mvtnorm} } \details{ Package \pkg{mvtnorm} provides functionality for dealing with multivariate normal and t-distributions. The package interfaces \proglang{FORTRAN} and \code{C} code for evaluating multivariate normal probabilities written by Alan Genz and Tetsuhisa Miwa. Functions \code{\link{pmvnorm}}, \code{\link{pmvt}}, \code{\link{qmvnorm}}, and \code{\link{qmvt}} return normal and t probabilities or corresponding quantiles computed by these original implementations. Users interested in the computation of such probabilities or quantiles, for example for multiple testing purposes, should use this functionality. When the multivariate normal log-likelihood function, defined by the log-probability in the discrete or interval-censored case or by the log-density for exact real observations, or a mix thereof, shall be computed, functions \code{\link{lpmvnorm}}, \code{\link{ldmvnorm}}, and \code{\link{ldpmvnorm}} are better suited. They rely on an independent implementation of Genz' algorithm (for log-probabilities), can be customised (different quasi-Monte Carlo schemes), and are a bit faster. Most importantly, the corresponding score functions are available through functions \code{\link{slpmvnorm}}, \code{\link{sldmvnorm}}, or \code{\link{sldpmvnorm}}, which help to speed-up parameter estimation considerably. Users interested in this functionality should consult the \code{lmvnorm_src} package vignette. } \keyword{ package } \seealso{ \code{vignette("lmvnorm_src", package = "mvtnorm")} } mvtnorm/man/qmvnorm.Rd0000644000176200001440000000637014436104705014527 0ustar liggesusers\name{qmvnorm} \alias{qmvnorm} \title{ Quantiles of the Multivariate Normal Distribution } \description{ Computes the equicoordinate quantile function of the multivariate normal distribution for arbitrary correlation matrices based on inversion of \code{\link{pmvnorm}}, using a stochastic root finding algorithm described in Bornkamp (2018). } \usage{ qmvnorm(p, interval = NULL, tail = c("lower.tail", "upper.tail", "both.tails"), mean = 0, corr = NULL, sigma = NULL, algorithm = GenzBretz(), ptol = 0.001, maxiter = 500, trace = FALSE, seed = NULL, ...) } %- maybe also 'usage' for other objects documented here. \arguments{ \item{p}{ probability.} \item{interval}{ optional, a vector containing the end-points of the interval to be searched. Does not need to contain the true quantile, just used as starting values by the root-finder. If equal to NULL a guess is used.} \item{tail}{ specifies which quantiles should be computed. \code{lower.tail} gives the quantile \eqn{x} for which \eqn{P[X \le x] = p}, \code{upper.tail} gives \eqn{x} with \eqn{P[X > x] = p} and \code{both.tails} leads to \eqn{x} with \eqn{P[-x \le X \le x] = p}.} \item{mean}{ the mean vector of length n. } \item{corr}{ the correlation matrix of dimension n.} \item{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 internally. If \code{corr} is given, it is assumed that appropriate standardization was performed by the user. If neither \code{corr} nor \code{sigma} is given, the identity matrix is used for \code{sigma}. } \item{algorithm}{ an object of class \code{\link{GenzBretz}}, \code{\link{Miwa}} or \code{\link{TVPACK}} specifying both the algorithm to be used as well as the associated hyper parameters.} \item{ptol, maxiter, trace}{Parameters passed to the stochastic root-finding algorithm. Iteration stops when the 95\% confidence interval for the predicted quantile is inside [p-ptol, p+ptol]. \code{maxiter} is the maximum number of iterations for the root finding algorithm. \code{trace} prints the iterations of the root finder.} \item{seed}{an object specifying if and how the random number generator should be initialized, see \code{\link[stats]{simulate}}.} \item{...}{ additional parameters to be passed to \code{\link{GenzBretz}}.} } \details{ Only equicoordinate quantiles are computed, i.e., the quantiles in each dimension coincide. The result is seed dependend. } \value{ A list with two components: \code{quantile} and \code{f.quantile} give the location of the quantile and the difference between the distribution function evaluated at the quantile and \code{p}. } \references{ Bornkamp, B. (2018). Calculating quantiles of noisy distribution functions using local linear regressions. \emph{Computational Statistics}, \bold{33}, 487--501. } \seealso{\code{\link{pmvnorm}}, \code{\link{qmvt}}} \examples{ qmvnorm(0.95, sigma = diag(2), tail = "both") } \keyword{distribution} mvtnorm/man/ltMatrices.Rd0000644000176200001440000001340014620622062015123 0ustar liggesusers\name{ltMatrices} \alias{ltMatrices} \alias{syMatrices} \alias{as.syMatrices} \alias{Tcrossprod} \alias{Crossprod} \alias{diagonals} \alias{Mult} \alias{Mult.ltMatrices} \alias{Mult.syMatrices} \alias{solve.ltMatrices} \alias{as.array.ltMatrices} \alias{as.array.syMatrices} \alias{diagonals.ltMatrices} \alias{diagonals.syMatrices} \alias{diagonals.matrix} \alias{diagonals<-} \alias{diagonals<-.ltMatrices} \alias{diagonals<-.syMatrices} \alias{diagonals.integer} \alias{Lower_tri} \alias{chol.syMatrices} \alias{aperm.ltMatrices} \alias{adddiag} \alias{chol2cov} \alias{invchol2chol} \alias{chol2invchol} \alias{invchol2cov} \alias{invchol2pre} \alias{chol2pre} \alias{Dchol} \alias{invcholD} \alias{chol2cor} \alias{invchol2cor} \alias{invchol2pc} \alias{chol2pc} \alias{vectrick} \alias{standardize} \alias{destandardize} \title{ Multiple Lower Triangular or Symmetric Matrices } \description{ A class representing multiple lower triangular or symmetric matrices and some methods. } \usage{ ltMatrices(object, diag = FALSE, byrow = FALSE, names = TRUE) syMatrices(object, diag = FALSE, byrow = FALSE, names = TRUE) as.syMatrices(object) \S3method{as.array}{ltMatrices}(x, symmetric = FALSE, \dots) \S3method{as.array}{syMatrices}(x, \dots) \S3method{diagonals}{ltMatrices}(x, \dots) \S3method{diagonals}{syMatrices}(x, \dots) \S3method{diagonals}{matrix}(x, \dots) \S3method{diagonals}{integer}(x, \dots) diagonals(x) <- value \S3method{diagonals}{ltMatrices}(x) <- value \S3method{diagonals}{syMatrices}(x) <- value \S3method{solve}{ltMatrices}(a, b, transpose = FALSE, \dots) \S3method{chol}{syMatrices}(x, \dots) \S3method{aperm}{ltMatrices}(a, perm, is_chol = FALSE, \dots) \S3method{Mult}{ltMatrices}(x, y, transpose = FALSE, \dots) \S3method{Mult}{syMatrices}(x, y, \dots) Tcrossprod(x, diag_only = FALSE) Crossprod(x, diag_only = FALSE) Lower_tri(x, diag = FALSE, byrow = attr(x, "byrow")) chol2cov(x) invchol2chol(x) chol2invchol(x) invchol2cov(x) invchol2pre(x) chol2pre(x) Dchol(x, D = 1 / sqrt(Tcrossprod(x, diag_only = TRUE))) invcholD(x, D = sqrt(Tcrossprod(solve(x), diag_only = TRUE))) chol2cor(x) invchol2cor(x) chol2pc(x) invchol2pc(x) vectrick(C, S, A, transpose = c(TRUE, TRUE)) standardize(chol, invchol) destandardize(chol = solve(invchol), invchol, score_schol) } \arguments{ \item{object}{a \code{matrix} representing the lower triagular elements of \eqn{N} lower triangular matrix, each of dimension \eqn{J \times J}. Dimensions of \code{object} depend on \code{diag}: With diagonal elements, \code{object} is a \eqn{J(J+1)/2 \times N} matrix, otherwise, the number of rows is \eqn{J(J - 1) / 2}. } \item{diag}{logical, \code{object} contains diagonal elements if \code{TRUE}, otherwise unit diagonal elements are assumed. } \item{byrow}{logical, \code{object} represents matrices in row-major order if \code{TRUE} or, otherwise, in column-major order. } \item{names}{logical or character vector of length \eqn{J}. } \item{symmetric}{logical, object is interpreted as a symmetric matrix if \code{TRUE}. } \item{diag_only}{logical, compute diagonal elements of crossproduct only if \code{TRUE}. } \item{x,chol,invchol}{object of class \code{ltMatrices} or \code{syMatrices} (for \code{chol}). } \item{value}{a matrix of diagonal elements to be assigned (of dimension \eqn{J \times N}). } \item{a}{object of class \code{ltMatrices}. } \item{perm}{a permutation of the covariance matrix corresponding to \code{a}. } \item{is_chol}{a logical indicating if \code{a} is the Cholesky of the covariance (\code{chol = TRUE}) of the precision matrix. } \item{D}{a matrix (of dimension \eqn{J \times N}) of diagonal elements to be multiplied with. } \item{y}{matrix with \eqn{J} rows. } \item{b}{matrix with \eqn{J} rows. } \item{C}{an object of class \code{ltMatrices}.} \item{S}{an object of class \code{ltMatrices} or a matrix with \eqn{J^2} rows representing multiple \eqn{J x J} matrices (columns of vec operators).} \item{A}{an object of class \code{ltMatrices}.} \item{transpose}{a logical of length two indicating if \code{A} or \code{B} shall be transposed in \code{vectrick}. For \code{solve}, this argument being true computes \code{solve(t(a), b)} (in absence of a \code{t()} method for \code{ltMatrices} objects).} \item{score_schol}{score matrix for a standardized \code{chol} object.} \item{\dots}{additional arguments, currently ignored. } } \details{ \code{ltMatrices} interprets a matrix as lower triangular elements of multiple lower triangular matrices. The corresponding class can be used to store such matrices efficiently. Matrix multiplications, solutions to linear systems, explicite inverses, and crossproducts can be computed based on such objects. Details can be found in the \code{lmvnorm_src} package vignette. \code{syMatrices} only store the lower triangular parts of multiple symmetric matrices. } \value{ The constructor \code{ltMatrices} returns objects of class \code{ltMatrices} with corresponding methods. The constructor \code{syMatrices} returns objects of class \code{syMatrices} with a reduced set of methods. } \seealso{\code{vignette("lmvnorm_src", package = "mvtnorm")}} \examples{ J <- 4L N <- 2L dm <- paste0("d", 1:J) xm <- paste0("x", 1:N) (C <- ltMatrices(matrix(runif(N * J * (J + 1) / 2), ncol = N, dimnames = list(NULL, xm)), diag = TRUE, names = dm)) ## dimensions and names dim(C) dimnames(C) names(C) ## subset C[,2:3] ## multiplication y <- matrix(runif(N * J), nrow = J) Mult(C, y) ## solve solve(C) solve(C, y) ## tcrossprod Tcrossprod(C) ## convert to matrix as.array(solve(C[1,]))[,,1] } \keyword{matrix} mvtnorm/man/pmvt.Rd0000644000176200001440000001725114436104655014022 0ustar liggesusers\name{pmvt} \alias{pmvt} \title{ Multivariate t Distribution } \description{ Computes the the distribution function of the multivariate t distribution for arbitrary limits, degrees of freedom and correlation matrices based on algorithms by Genz and Bretz. } \usage{ pmvt(lower=-Inf, upper=Inf, delta=rep(0, length(lower)), df=1, corr=NULL, sigma=NULL, algorithm = GenzBretz(), type = c("Kshirsagar", "shifted"), keepAttr=TRUE, seed = NULL, ...) } \arguments{ \item{lower}{ the vector of lower limits of length n.} \item{upper}{ the vector of upper limits of length n.} \item{delta}{ the vector of noncentrality parameters of length n, for \code{type = "shifted"} delta specifies the mode.} \item{df}{ degree of freedom as integer. Normal probabilities are computed for \code{df=0}.} \item{corr}{ the correlation matrix of dimension n.} \item{sigma}{ the scale matrix of dimension n. Either \code{corr} or \code{sigma} can be specified. If \code{sigma} is given, the problem is standardized internally. If \code{corr} is given, it is assumed that appropriate standardization was performed by the user. If neither \code{corr} nor \code{sigma} is given, the identity matrix is used for \code{sigma}. } \item{algorithm}{ an object of class \code{\link{GenzBretz}} or \code{\link{TVPACK}} defining the hyper parameters of this algorithm.} \item{type}{ type of the noncentral multivariate t distribution to be computed. The choice \code{type = "Kshirsagar"} corresponds to formula (1.4) in Genz and Bretz (2009) (see also Chapter 5.1 in Kotz and Nadarajah (2004)). This is the noncentral t-distribution needed for calculating the power of multiple contrast tests under a normality assumption. \code{type = "shifted"} corresponds to the formula right before formula (1.4) in Genz and Bretz (2009) (see also formula (1.1) in Kotz and Nadarajah (2004)). It is a location shifted version of the central t-distribution. This noncentral multivariate t distribution appears for example as the Bayesian posterior distribution for the regression coefficients in a linear regression. In the central case both types coincide. } \item{keepAttr}{\code{\link{logical}} indicating if \code{\link{attributes}} such as \code{error} and \code{msg} should be attached to the return value. The default, \code{TRUE} is back compatible.} \item{seed}{an object specifying if and how the random number generator should be initialized, see \code{\link[stats]{simulate}}. } \item{...}{additional parameters (currently given to \code{GenzBretz} for backward compatibility issues). } } \details{ This function involves the computation of central and noncentral multivariate t-probabilities with arbitrary correlation matrices. It involves both the computation of singular and nonsingular probabilities. The methodology (for default \code{algorithm = GenzBretz()}) is based on randomized quasi Monte Carlo methods and described in Genz and Bretz (1999, 2002). \cr Because of the randomization, the result for this algorithm (slightly) depends on \code{\link{.Random.seed}}. For 2- and 3-dimensional problems one can also use the \code{\link{TVPACK}} routines described by Genz (2004), which only handles semi-infinite integration regions (and for \code{type = "Kshirsagar"} only central problems). For \code{type = "Kshirsagar"} and a given correlation matrix \code{corr}, for short \eqn{A}, say, (which has to be positive semi-definite) and degrees of freedom \eqn{\nu} the following values are numerically evaluated %% FIXME add non-LaTeX alternative \deqn{LaTeX}{non-LaTex} \deqn{I = 2^{1-\nu/2} / \Gamma(\nu/2) \int_0^\infty s^{\nu-1} \exp(-s^2/2) \Phi(s \cdot lower/\sqrt{\nu} - \delta, s \cdot upper/\sqrt{\nu} - \delta) \, ds } where \deqn{\Phi(a,b) = (det(A)(2\pi)^m)^{-1/2} \int_a^b \exp(-x^\prime Ax/2) \, dx} is the multivariate normal distribution and \eqn{m} is the number of rows of \eqn{A}. For \code{type = "shifted"}, a positive definite symmetric matrix \eqn{S} (which might be the correlation or the scale matrix), mode (vector) \eqn{\delta} and degrees of freedom \eqn{\nu} the following integral is evaluated: \deqn{c\int_{lower_1}^{upper_1}...\int_{lower_m}^{upper_m} (1+(x-\delta)'S^{-1}(x-\delta)/\nu)^{-(\nu+m)/2}\, dx_1 ... dx_m, } where \deqn{c = \Gamma((\nu+m)/2)/((\pi \nu)^{m/2}\Gamma(\nu/2)|S|^{1/2}),} and \eqn{m} is the number of rows of \eqn{S}. Note that both \code{-Inf} and \code{+Inf} may be specified in the lower and upper integral limits in order to compute one-sided probabilities. Univariate problems are passed to \code{\link{pt}}. If \code{df = 0}, normal probabilities are returned. } \value{ The evaluated distribution function is returned, if \code{keepAttr} is true, with attributes \item{error}{estimated absolute error and} \item{msg}{status message (a \code{\link{character}} string).} \item{algorithm}{a \code{\link{character}} string with \code{class(algorithm)}.} } \references{ Genz, A. and Bretz, F. (1999), Numerical computation of multivariate t-probabilities with application to power calculation of multiple contrasts. \emph{Journal of Statistical Computation and Simulation}, \bold{63}, 361--378. Genz, A. and Bretz, F. (2002), Methods for the computation of multivariate t-probabilities. \emph{Journal of Computational and Graphical Statistics}, \bold{11}, 950--971. Genz, A. (2004), Numerical computation of rectangular bivariate and trivariate normal and t-probabilities, \emph{Statistics and Computing}, \bold{14}, 251--260. Genz, A. and Bretz, F. (2009), \emph{Computation of Multivariate Normal and t Probabilities}. Lecture Notes in Statistics, Vol. 195. Springer-Verlag, Heidelberg. S. Kotz and S. Nadarajah (2004), \emph{Multivariate t Distributions and Their Applications}. Cambridge University Press. Cambridge. Edwards D. and Berry, Jack J. (1987), The efficiency of simulation-based multiple comparisons. \emph{Biometrics}, \bold{43}, 913--928. } %%% can't validate certificate anymore %\source{ % \url{http://www.sci.wsu.edu/math/faculty/genz/homepage} %} \seealso{\code{\link{qmvt}}} \examples{ n <- 5 lower <- -1 upper <- 3 df <- 4 corr <- diag(5) corr[lower.tri(corr)] <- 0.5 delta <- rep(0, 5) prob <- pmvt(lower=lower, upper=upper, delta=delta, df=df, corr=corr) print(prob) pmvt(lower=-Inf, upper=3, df = 3, sigma = 1) == pt(3, 3) # Example from R News paper (original by Edwards and Berry, 1987) n <- c(26, 24, 20, 33, 32) V <- diag(1/n) df <- 130 C <- c(1,1,1,0,0,-1,0,0,1,0,0,-1,0,0,1,0,0,0,-1,-1,0,0,-1,0,0) C <- matrix(C, ncol=5) ### scale matrix cv <- C \%*\% tcrossprod(V, C) ### correlation matrix cr <- cov2cor(cv) delta <- rep(0,5) myfct <- function(q, alpha) { lower <- rep(-q, ncol(cv)) upper <- rep(q, ncol(cv)) pmvt(lower=lower, upper=upper, delta=delta, df=df, corr=cr, abseps=0.0001) - alpha } ### uniroot for this simple problem round(uniroot(myfct, lower=1, upper=5, alpha=0.95)$root, 3) # compare pmvt and pmvnorm for large df: a <- pmvnorm(lower=-Inf, upper=1, mean=rep(0, 5), corr=diag(5)) b <- pmvt(lower=-Inf, upper=1, delta=rep(0, 5), df=300, corr=diag(5)) a b stopifnot(round(a, 2) == round(b, 2)) # correlation and scale matrix a <- pmvt(lower=-Inf, upper=2, delta=rep(0,5), df=3, sigma = diag(5)*2) b <- pmvt(lower=-Inf, upper=2/sqrt(2), delta=rep(0,5), df=3, corr=diag(5)) attributes(a) <- NULL attributes(b) <- NULL a b stopifnot(all.equal(round(a,3) , round(b, 3))) a <- pmvt(0, 1,df=10) attributes(a) <- NULL b <- pt(1, df=10) - pt(0, df=10) stopifnot(all.equal(round(a,10) , round(b, 10))) } \keyword{distribution} mvtnorm/man/margcond.Rd0000644000176200001440000000350614422737133014622 0ustar liggesusers\name{margcond} \alias{marg_mvnorm} \alias{cond_mvnorm} \title{ Marginal and Conditional Multivariate Normal Distributions } \description{ Computes means and Cholesky factors of covariance or precision matrices of multiple multivariate normal distributions. } \usage{ marg_mvnorm(chol, invchol, which = 1L) cond_mvnorm(chol, invchol, which_given = 1L, given, center = FALSE) } \arguments{ \item{chol}{Cholesky factors of covariance matrices as \code{\link{ltMatrices}} object, length is recylced to length of \code{lower} and \code{upper}. } \item{invchol}{Cholesky factors of precision matrices as \code{\link{ltMatrices}} object, length is recylced to length of \code{lower} and \code{upper}. Either \code{chol} or \code{invchol} must be given. } \item{which}{names or indices of elements those marginal distribution is of interest. } \item{which_given}{names or indices of elements to condition on. } \item{given}{matrix of realisations to condition on (number of rows is equal to \code{length(which)}, the number of columns corresponds to the number of matrices in \code{chol} or \code{invchol}. } \item{center}{logical, if \code{TRUE}, the negative rescaled conditional mean is returned (such that it can be specified as \code{center} argument to \code{\link{slpmvnorm}}). By default, the conditional mean is returned. } } \details{ Derives parameters of the requested marginal or conditional distributions, defined by \code{chol} (Cholesky factor of covariance) or \code{invchol} (Cholesky factor of precision matrix) and, for conditional distributions, the mean. More details can be found in the \code{lmvnorm_src} package vignette. } \value{ A named list. } \seealso{\code{vignette("lmvnorm_src", package = "mvtnorm")}} \keyword{distribution} mvtnorm/man/lpmvnorm.Rd0000644000176200001440000001373314436104460014701 0ustar liggesusers\name{lpmvnorm} \alias{lpmvnorm} \alias{slpmvnorm} \alias{ldmvnorm} \alias{sldmvnorm} \alias{ldpmvnorm} \alias{sldpmvnorm} \title{ Multivariate Normal Log-likelihood and Score Functions } \description{ Computes the log-likelihood (contributions) of multiple exact or interval-censored observations (or a mix thereof) from multivariate normal distributions and evaluates corresponding score functions. } \usage{ lpmvnorm(lower, upper, mean = 0, center = NULL, chol, invchol, logLik = TRUE, M = NULL, w = NULL, seed = NULL, tol = .Machine$double.eps, fast = FALSE) slpmvnorm(lower, upper, mean = 0, center = NULL, chol, invchol, logLik = TRUE, M = NULL, w = NULL, seed = NULL, tol = .Machine$double.eps, fast = FALSE) ldmvnorm(obs, mean = 0, chol, invchol, logLik = TRUE) sldmvnorm(obs, mean = 0, chol, invchol, logLik = TRUE) ldpmvnorm(obs, lower, upper, mean = 0, chol, invchol, logLik = TRUE, ...) sldpmvnorm(obs, lower, upper, mean = 0, chol, invchol, logLik = TRUE, ...) } \arguments{ \item{lower}{matrix of lower limits (one column for each observation, \eqn{J} rows). } \item{upper}{matrix of upper limits (one column for each observation, \eqn{J} rows). } \item{obs}{matrix of exact observations (one column for each observation, \eqn{J} rows). } \item{mean}{matrix of means (one column for each observation, length is recycled to length of \code{obs}, \code{lower} and \code{upper}). } \item{center}{matrix of negative rescaled means (one column for each observation, length is recycled to length of \code{lower} and \code{upper}) as returned by \code{cond_mvnorm(..., center = TRUE)}. } \item{chol}{Cholesky factors of covariance matrices as \code{\link{ltMatrices}} object, length is recylced to length of \code{obs}, \code{lower} and \code{upper}. } \item{invchol}{Cholesky factors of precision matrices as \code{\link{ltMatrices}} object, length is recylced to length of \code{lower} and \code{upper}. Either \code{chol} or \code{invchol} must be given. } \item{logLik}{logical, if \code{TRUE}, the log-likelihood is returned, otherwise the individual contributions to the sum are returned. } \item{M}{number of iterations, early stopping based on estimated errors is NOT implemented. } \item{w}{an optional matrix of weights with \eqn{J - 1} rows. This allows to replace the default Monte-Carlo procedure (Genz, 1992) with a quasi-Monte-Carlo approach (Genz & Bretz, 2002). Note that the same weights for evaluating the multivariate normal probability are used for all observations when \code{ncol(w) == M} is specified. If \code{ncol(w) == ncol(lower) * M}, each likelihood contribution is evaluated on the corresponding sub-matrix. If \code{w} is \code{NULL}, different uniform numbers are drawn for each observation. } \item{seed}{an object specifying if and how the random number generator should be initialized, see \code{\link[stats]{simulate}}. Only applied when \code{w} is \code{NULL}. } \item{tol}{tolerance limit, values smaller than \code{tol} are interpreted as zero. } \item{fast}{logical, if \code{TRUE}, a faster but less accurate version of \code{pnorm} is used internally. } \item{\dots}{additional arguments to \code{lpmvnorm}. } } \details{ Evaluates the multivariate normal log-likelihood defined by \code{means} and \code{chol} over boxes defined by \code{lower} and \code{upper} or for exact observations \code{obs}. Monte-Carlo (Genz, 1992, the default) and quasi-Monte-Carlo (Genz & Bretz, 2002) integration is implemented, the latter with weights obtained, for example, from packages \pkg{qrng} or \pkg{randtoolbox}. It is the responsibility of the user to ensure a meaningful lattice is used. In case of doubt, use plain Monte-Carlo (\code{w = NULL}) or \code{\link{pmvnorm}}. \code{slpmvnorm} computes both the individual log-likelihood contributions and the corresponding score matrix (of dimension \eqn{J \times (J + 1) / 2 \times N}) if \code{chol} contains diagonal elements. Otherwise, the dimension is \eqn{J \times (J - 1) / 2 \times N}. The scores for exact or mixed exact-interval observations are computed by \code{sldmvnorm} and \code{sldpmvnorm}, respectively. More details can be found in the \code{lmvnorm_src} package vignette. } \value{ The log-likelihood (\code{logLik = TRUE}) or the individual contributions to the log-likelihood. \code{slpmvnorm}, \code{sldmvnorm}, and \code{sldpmvnorm} return the score matrices and, optionally (\code{logLik = TRUE}), the individual log-likelihood contributions as well as scores for \code{obs}, \code{lower}, \code{upper}, and \code{mean}. } \seealso{\code{\link{dmvnorm}}, \code{vignette("lmvnorm_src", package = "mvtnorm")}} \references{ Genz, A. (1992). Numerical computation of multivariate normal probabilities. \emph{Journal of Computational and Graphical Statistics}, \bold{1}, 141--150. Genz, A. and Bretz, F. (2002), Methods for the computation of multivariate t-probabilities. \emph{Journal of Computational and Graphical Statistics}, \bold{11}, 950--971. } \examples{ ### five observations N <- 5L ### dimension J <- 4L ### lower and upper bounds, ie interval-censoring lwr <- matrix(-runif(N * J), nrow = J) upr <- matrix(runif(N * J), nrow = J) ### Cholesky factor (C <- ltMatrices(runif(J * (J + 1) / 2), diag = TRUE)) ### corresponding covariance matrix (S <- as.array(Tcrossprod(C))[,,1]) ### plain Monte-Carlo (Genz, 1992) w <- NULL M <- 25000 ### quasi-Monte-Carlo (Genz & Bretz, 2002, but with different weights) if (require("qrng")) w <- t(ghalton(M * N, J - 1)) ### log-likelihood lpmvnorm(lower = lwr, upper = upr, chol = C, w = w, M = M) ### compare with pmvnorm exp(lpmvnorm(lower = lwr, upper = upr, chol = C, logLik = FALSE, w = w, M = M)) sapply(1:N, function(i) pmvnorm(lower = lwr[,i], upper = upr[,i], sigma = S)) ### log-lik contributions and score matrix slpmvnorm(lower = lwr, upper = upr, chol = C, w = w, M = M, logLik = TRUE) } \keyword{distribution} mvtnorm/man/Mvnorm.Rd0000644000176200001440000000614514453742277014321 0ustar liggesusers\name{Mvnorm} \alias{dmvnorm} \alias{rmvnorm} \title{Multivariate Normal Density and Random Deviates} \description{ These functions provide the density function and a random number generator for the multivariate normal distribution with mean equal to \code{mean} and covariance matrix \code{sigma}. } \usage{ dmvnorm(x, mean = rep(0, p), sigma = diag(p), log = FALSE, checkSymmetry = TRUE) rmvnorm(n, mean = rep(0, nrow(sigma)), sigma = diag(length(mean)), method=c("eigen", "svd", "chol"), pre0.9_9994 = FALSE, checkSymmetry = TRUE, rnorm = stats::rnorm) } \arguments{ \item{x}{vector or matrix of quantiles. When \code{x} is a matrix, each row is taken to be a quantile and columns correspond to the number of dimensions, \code{p}.} \item{n}{number of observations.} \item{mean}{mean vector, default is \code{rep(0, length = ncol(x))}. In \code{ldmvnorm} or \code{sldmvnorm}, \code{mean} is a matrix with observation-specific means arranged in columns.} \item{sigma}{covariance matrix, default is \code{diag(ncol(x))}.} \item{log}{logical; if \code{TRUE}, densities d are given as log(d).} \item{method}{string specifying the matrix decomposition used to determine the matrix root of \code{sigma}. Possible methods are eigenvalue decomposition (\code{"eigen"}, default), singular value decomposition (\code{"svd"}), and Cholesky decomposition (\code{"chol"}). The Cholesky is typically fastest, not by much though.} \item{pre0.9_9994}{logical; if \code{FALSE}, the output produced in mvtnorm versions up to 0.9-9993 is reproduced. In 0.9-9994, the output is organized such that \code{rmvnorm(10,...)} has the same first ten rows as \code{rmvnorm(100, ...)} when called with the same seed.} \item{checkSymmetry}{logical; if \code{FALSE}, skip checking whether the covariance matrix is symmetric or not. This will speed up the computation but may cause unexpected outputs when ill-behaved \code{sigma} is provided. The default value is \code{TRUE}.} \item{rnorm}{a function with the same interface as \code{\link[stats]{rnorm}}. This allows switching to other generators of standard normal variables.} } \details{ \code{dmvnorm} computes the density function of the multivariate normal specified by mean and the covariance matrix \code{sigma}. \code{rmvnorm} generates multivariate normal variables. } \seealso{\code{\link{pmvnorm}}, \code{\link{rnorm}}, \code{\link{qmvnorm}}, \code{vignette("lmvnorm_src", package = "mvtnorm")}} \examples{ dmvnorm(x=c(0,0)) dmvnorm(x=c(0,0), mean=c(1,1)) sigma <- matrix(c(4,2,2,3), ncol=2) x <- rmvnorm(n=500, mean=c(1,2), sigma=sigma) colMeans(x) var(x) dS <- dmvnorm(x, sigma = sigma) ### alternative interface C <- t(chol(sigma)) (C <- ltMatrices(C[lower.tri(C, diag = TRUE)], diag = TRUE)) dC <- exp(ldmvnorm(obs = t(x), chol = C, logLik = FALSE)) all.equal(dS, dC) x <- rmvnorm(n=500, mean=c(1,2), sigma=sigma, method="chol") colMeans(x) var(x) plot(x) } \keyword{distribution} \keyword{multivariate} mvtnorm/man/qmvt.Rd0000644000176200001440000001151714436104752014020 0ustar liggesusers\name{qmvt} \alias{qmvt} \title{ Quantiles of the Multivariate t Distribution } \description{ Computes the equicoordinate quantile function of the multivariate t distribution for arbitrary correlation matrices based on inversion of \code{\link{pmvt}}, using a stochastic root finding algorithm described in Bornkamp (2018). } \usage{ qmvt(p, interval = NULL, tail = c("lower.tail", "upper.tail", "both.tails"), df = 1, delta = 0, corr = NULL, sigma = NULL, algorithm = GenzBretz(), type = c("Kshirsagar", "shifted"), ptol = 0.001, maxiter = 500, trace = FALSE, seed = NULL, ...) } \arguments{ \item{p}{ probability.} \item{interval}{ optional, a vector containing the end-points of the interval to be searched. Does not need to contain the true quantile, just used as starting values by the root-finder. If equal to NULL a guess is used.} \item{tail}{ specifies which quantiles should be computed. \code{lower.tail} gives the quantile \eqn{x} for which \eqn{P[X \le x] = p}, \code{upper.tail} gives \eqn{x} with \eqn{P[X > x] = p} and \code{both.tails} leads to \eqn{x} with \eqn{P[-x \le X \le x] = p}.} \item{delta}{ the vector of noncentrality parameters of length n, for \code{type = "shifted"} delta specifies the mode.} \item{df}{ degree of freedom as integer. Normal quantiles are computed for \code{df = 0} or \code{df = Inf}.} \item{corr}{ the correlation matrix of dimension n.} \item{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 internally. If \code{corr} is given, it is assumed that appropriate standardization was performed by the user. If neither \code{corr} nor \code{sigma} is given, the identity matrix in the univariate case (so \code{corr = 1}) is used for \code{corr}. } \item{algorithm}{ an object of class \code{\link{GenzBretz}} or \code{\link{TVPACK}} defining the hyper parameters of this algorithm.} \item{type}{type of the noncentral multivariate t distribution to be computed. The choice \code{type = "Kshirsagar"} corresponds to formula (1.4) in Genz and Bretz (2009) (see also Chapter 5.1 in Kotz and Nadarajah (2004)) and \code{type = "shifted"} corresponds to the formula before formula (1.4) in Genz and Bretz (2009) (see also formula (1.1) in Kotz and Nadarajah (2004)). } \item{ptol, maxiter, trace}{Parameters passed to the stochastic root-finding algorithm. Iteration stops when the 95\% confidence interval for the predicted quantile is inside [p-ptol, p+ptol]. \code{maxiter} is the maximum number of iterations for the root finding algorithm. \code{trace} prints the iterations of the root finder.} \item{seed}{an object specifying if and how the random number generator should be initialized, see \code{\link[stats]{simulate}}.} \item{...}{ additional parameters to be passed to \code{\link{GenzBretz}}.} } \details{ Only equicoordinate quantiles are computed, i.e., the quantiles in each dimension coincide. The result is seed dependend. } \value{ A list with two components: \code{quantile} and \code{f.quantile} give the location of the quantile and the difference between the distribution function evaluated at the quantile and \code{p}. } \references{ Bornkamp, B. (2018). Calculating quantiles of noisy distribution functions using local linear regressions. \emph{Computational Statistics}, \bold{33}, 487--501. } \seealso{\code{\link{pmvnorm}}, \code{\link{qmvnorm}}} \examples{ ## basic evaluation qmvt(0.95, df = 16, tail = "both") ## check behavior for df=0 and df=Inf Sigma <- diag(2) set.seed(29) q0 <- qmvt(0.95, sigma = Sigma, df = 0, tail = "both")$quantile set.seed(29) q8 <- qmvt(0.95, sigma = Sigma, df = Inf, tail = "both")$quantile set.seed(29) qn <- qmvnorm(0.95, sigma = Sigma, tail = "both")$quantile stopifnot(identical(q0, q8), isTRUE(all.equal(q0, qn, tol = (.Machine$double.eps)^(1/3)))) ## if neither sigma nor corr are provided, corr = 1 is used internally df <- 0 set.seed(29) qt95 <- qmvt(0.95, df = df, tail = "both")$quantile set.seed(29) qt95.c <- qmvt(0.95, df = df, corr = 1, tail = "both")$quantile set.seed(29) qt95.s <- qmvt(0.95, df = df, sigma = 1, tail = "both")$quantile stopifnot(identical(qt95, qt95.c), identical(qt95, qt95.s)) df <- 4 set.seed(29) qt95 <- qmvt(0.95, df = df, tail = "both")$quantile set.seed(29) qt95.c <- qmvt(0.95, df = df, corr = 1, tail = "both")$quantile set.seed(29) qt95.s <- qmvt(0.95, df = df, sigma = 1, tail = "both")$quantile stopifnot(identical(qt95, qt95.c), identical(qt95, qt95.s)) } \keyword{distribution} mvtnorm/man/Mvt.Rd0000644000176200001440000001250214172227731013572 0ustar liggesusers\name{Mvt} \alias{dmvt} \alias{rmvt} \title{The Multivariate t Distribution} \description{ These functions provide information about the multivariate \eqn{t} distribution with non-centrality parameter (or mode) \code{delta}, scale matrix \code{sigma} and degrees of freedom \code{df}. \code{dmvt} gives the density and \code{rmvt} generates random deviates. } \usage{ rmvt(n, sigma = diag(2), df = 1, delta = rep(0, nrow(sigma)), type = c("shifted", "Kshirsagar"), ...) dmvt(x, delta = rep(0, p), sigma = diag(p), df = 1, log = TRUE, type = "shifted", checkSymmetry = TRUE) } \arguments{ \item{x}{vector or matrix of quantiles. If \code{x} is a matrix, each row is taken to be a quantile.} \item{n}{number of observations.} \item{delta}{the vector of noncentrality parameters of length n, for \code{type = "shifted"} delta specifies the mode.} \item{sigma}{scale matrix, defaults to \code{diag(ncol(x))}.} \item{df}{degrees of freedom. \code{df = 0} or \code{df = Inf} corresponds to the multivariate normal distribution.} \item{log}{\code{\link{logical}} indicating whether densities \eqn{d} are given as \eqn{\log(d)}{log(d)}.} \item{type}{type of the noncentral multivariate \eqn{t} distribution. \code{type = "Kshirsagar"} corresponds to formula (1.4) in Genz and Bretz (2009) (see also Chapter 5.1 in Kotz and Nadarajah (2004)). This is the noncentral t-distribution needed for calculating the power of multiple contrast tests under a normality assumption. \code{type = "shifted"} corresponds to the formula right before formula (1.4) in Genz and Bretz (2009) (see also formula (1.1) in Kotz and Nadarajah (2004)). It is a location shifted version of the central t-distribution. This noncentral multivariate \eqn{t} distribution appears for example as the Bayesian posterior distribution for the regression coefficients in a linear regression. In the central case both types coincide. Note that the defaults differ from the default in \code{\link{pmvt}()} (for reasons of backward compatibility).} \item{checkSymmetry}{logical; if \code{FALSE}, skip checking whether the covariance matrix is symmetric or not. This will speed up the computation but may cause unexpected outputs when ill-behaved \code{sigma} is provided. The default value is \code{TRUE}.} \item{\dots}{additional arguments to \code{\link{rmvnorm}()}, for example \code{method}.} } \details{ If \eqn{\bm{X}}{X} denotes a random vector following a \eqn{t} distribution with location vector \eqn{\bm{0}}{0} and scale matrix \eqn{\Sigma}{Sigma} (written \eqn{X\sim t_\nu(\bm{0},\Sigma)}{X ~ t_nu(0, Sigma)}), the scale matrix (the argument \code{sigma}) is not equal to the covariance matrix \eqn{Cov(\bm{X})}{Cov(X)} of \eqn{\bm{X}}{X}. If the degrees of freedom \eqn{\nu}{nu} (the argument \code{df}) is larger than 2, then \eqn{Cov(\bm{X})=\Sigma\nu/(\nu-2)}{Cov(X)=Sigma nu/(nu-2)}. Furthermore, in this case the correlation matrix \eqn{Cor(\bm{X})}{Cor(X)} equals the correlation matrix corresponding to the scale matrix \eqn{\Sigma}{Sigma} (which can be computed with \code{\link{cov2cor}()}). Note that the scale matrix is sometimes referred to as \dQuote{dispersion matrix}; see McNeil, Frey, Embrechts (2005, p. 74). For \code{type = "shifted"} the density \deqn{c(1+(x-\delta)'S^{-1}(x-\delta)/\nu)^{-(\nu+m)/2}} is implemented, where \deqn{c = \Gamma((\nu+m)/2)/((\pi \nu)^{m/2}\Gamma(\nu/2)|S|^{1/2}),} \eqn{S} is a positive definite symmetric matrix (the matrix \code{sigma} above), \eqn{\delta}{delta} is the non-centrality vector and \eqn{\nu}{nu} are the degrees of freedom. \code{df=0} historically leads to the multivariate normal distribution. From a mathematical point of view, rather \code{df=Inf} corresponds to the multivariate normal distribution. This is (now) also allowed for \code{rmvt()} and \code{dmvt()}. Note that \code{dmvt()} has default \code{log = TRUE}, whereas \code{\link{dmvnorm}()} has default \code{log = FALSE}. } \references{ McNeil, A. J., Frey, R., and Embrechts, P. (2005). \emph{Quantitative Risk Management: Concepts, Techniques, Tools}. Princeton University Press. } \seealso{\code{\link{pmvt}()} and \code{\link{qmvt}()}} \examples{ ## basic evaluation dmvt(x = c(0,0), sigma = diag(2)) ## check behavior for df=0 and df=Inf x <- c(1.23, 4.56) mu <- 1:2 Sigma <- diag(2) x0 <- dmvt(x, delta = mu, sigma = Sigma, df = 0) # default log = TRUE! x8 <- dmvt(x, delta = mu, sigma = Sigma, df = Inf) # default log = TRUE! xn <- dmvnorm(x, mean = mu, sigma = Sigma, log = TRUE) stopifnot(identical(x0, x8), identical(x0, xn)) ## X ~ t_3(0, diag(2)) x <- rmvt(100, sigma = diag(2), df = 3) # t_3(0, diag(2)) sample plot(x) ## X ~ t_3(mu, Sigma) n <- 1000 mu <- 1:2 Sigma <- matrix(c(4, 2, 2, 3), ncol=2) set.seed(271) x <- rep(mu, each=n) + rmvt(n, sigma=Sigma, df=3) plot(x) ## Note that the call rmvt(n, mean=mu, sigma=Sigma, df=3) does *not* ## give a valid sample from t_3(mu, Sigma)! [and thus throws an error] try(rmvt(n, mean=mu, sigma=Sigma, df=3)) ## df=Inf correctly samples from a multivariate normal distribution set.seed(271) x <- rep(mu, each=n) + rmvt(n, sigma=Sigma, df=Inf) set.seed(271) x. <- rmvnorm(n, mean=mu, sigma=Sigma) stopifnot(identical(x, x.)) } \keyword{distribution} \keyword{multivariate} mvtnorm/DESCRIPTION0000644000176200001440000000362314623152552013473 0ustar liggesusersPackage: mvtnorm Title: Multivariate Normal and t Distributions Version: 1.2-5 Date: 2024-05-18 Authors@R: c(person("Alan", "Genz", role = "aut"), person("Frank", "Bretz", role = "aut"), person("Tetsuhisa", "Miwa", role = "aut"), person("Xuefei", "Mi", role = "aut"), person("Friedrich", "Leisch", role = "ctb"), person("Fabian", "Scheipl", role = "ctb"), person("Bjoern", "Bornkamp", role = "ctb", comment = c(ORCID = "0000-0002-6294-8185")), person("Martin", "Maechler", role = "ctb", comment = c(ORCID = "0000-0002-8685-9910")), person("Torsten", "Hothorn", role = c("aut", "cre"), email = "Torsten.Hothorn@R-project.org", comment = c(ORCID = "0000-0001-8301-0471"))) Description: Computes multivariate normal and t probabilities, quantiles, random deviates, and densities. Log-likelihoods for multivariate Gaussian models and Gaussian copulae parameterised by Cholesky factors of covariance or precision matrices are implemented for interval-censored and exact data, or a mix thereof. Score functions for these log-likelihoods are available. A class representing multiple lower triangular matrices and corresponding methods are part of this package. Imports: stats Depends: R(>= 3.5.0) Suggests: qrng, numDeriv License: GPL-2 URL: http://mvtnorm.R-forge.R-project.org NeedsCompilation: yes Packaged: 2024-05-21 14:29:39 UTC; hothorn Author: Alan Genz [aut], Frank Bretz [aut], Tetsuhisa Miwa [aut], Xuefei Mi [aut], Friedrich Leisch [ctb], Fabian Scheipl [ctb], Bjoern Bornkamp [ctb] (), Martin Maechler [ctb] (), Torsten Hothorn [aut, cre] () Maintainer: Torsten Hothorn Repository: CRAN Date/Publication: 2024-05-21 17:10:02 UTC mvtnorm/build/0000755000176200001440000000000014623127721013060 5ustar liggesusersmvtnorm/build/vignette.rds0000644000176200001440000000043414623127721015420 0ustar liggesusersuPN0t@D|E *Ӹavq6^OfwfB$  ,#L߶d7tTZ>\̫E5Ɂ[(jY?(T8|(ڧv wgn[~[~3G8:2sD"a++vR$MX %qſT0}W'l]dځO8N؉I|?g}JjU^l^'RUչ1XZ5 ?y>mvtnorm/build/partial.rdb0000644000176200001440000001002014623127675015206 0ustar liggesuserskwVy:#mKҖ&1)B !$ lW[,]Zd .'=ڽ#+ʉ p2xyϝx9V( Bad,rxZS_hku epLZ&7Up ȷ[UäpӂE^_fu:eZo{V~yq_E4Se K ~u9[TPR4"Jɭ7Jecᮼ)+M+7f7wJR]J,==Y8Ea N7Aنo,F1b ^)8xu Ipӈ밢b!sԾWN)3!O#~|2~xG ^yd< F1'O%눢v!b ?u^+W3y@RZ%lѷ oeMe};7k&[^^ +pM:734s~ZyseLW0`L=f[9v|BAA|I|\0vUP]JrmTN\`cTa[,:+w6zكY#ӐSa]ߪҨ[W2xw\oM'` ^*qlC2)S9F?E ҄xt:rm8gvt)#uXb h8vE͓7Mry+Qu]CR/g;\HcI^;U{vxGtUA)ԍĭQEqBRmZ7Zys?bF|VLHVT@Ÿ[ kT&=5t:8;.|ötOzMyܺn`$XP˹s~X1>Њ \\9)pL/Wa+dlJE؁:⌣(+c<Þ$ZtS4k=: gEz'()ħ2;9Ϟ":8/f{[ 5I dF=gد-mlhPkQ\Ʉ72_(F[m;nnv3g4 7_˥Ux옪]fl3]H2NfG+2f37)cj#HOä\38PW:Q.o5FGky`h* |2h|öYH?l>$(#5) M5o6H%w{ "́?4G~!1CM(L:_v<Q)S4%ȇO$O#fq3M/~}$ʁMa3NT,|\b5iɨZZL* %/e2`u.1R~l&S͛NȤL>kv$n0-dT}N#壉 Osy).+ 4 M1; hbScbC4IZ@UOOQSOW,>)`LRn% `WD8'@-[kNܤ1C쳅$XBTsO$ZPnbx3~u(WhMu8TO7̬nT\.0caYU&LquA|y~HpIU3"lf}#rχY`qlDM1&N͝ (>mE`pV&鏞.bx Td؝BH'_:8ˈ_}A8aEF ĺ6⷏&(! .#9(`x"bmBL F<ώ'&0])PB^%vcq Fjl Jfs`khFz:1j|3t]$zaR Z]S/484MkCJt(̐"ո+NqQ@/ v/"~1NO;LD)_Rf)`0눢SwɲqǑlV WN3b>plG܇P؝9j3+_ Od>(#s3BoG=2g&_6WuKOOv)@rK$Fj2;&d@+Z}S8)UYVCABՁ" C= / ^sZ)Zp;.Utra[ sM1,𚝀p0B$*AЦ-*Uc鈤g"$5j=;Uq*B4X+'B7VX kV1݀t\ivZ=RTF15?=?kT-=ՙ.Bؖ GN)Π RmD+ۤ]RnX;#r?:v(~Y"ЬZ(NJn6GK 1ó?"PWq݉@wf h,tCٮ>w hD,zи$Mmr9xK+MaF44S]! :DA1掑=ce3\IIƀcBUBRacOإj4neZ|Xc4aWId}BhN~^Mz[x+ tܫcd}WxQq&a1phC޼ E&X[c&e, 8uemvtnorm/tests/0000755000176200001440000000000014623074726013131 5ustar liggesusersmvtnorm/tests/Examples/0000755000176200001440000000000014172227731014702 5ustar liggesusersmvtnorm/tests/Examples/mvtnorma-Ex.Rout.save0000644000176200001440000002656414172227731020744 0ustar liggesusers R version 3.2.3 (2015-12-10) -- "Wooden Christmas-Tree" Copyright (C) 2015 The R Foundation for Statistical Computing Platform: x86_64-pc-linux-gnu (64-bit) R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. Natural language support but running in an English locale R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > pkgname <- "mvtnorm" > source(file.path(R.home("share"), "R", "examples-header.R")) > options(warn = 1) > base::assign(".ExTimings", "mvtnorm-Ex.timings", pos = 'CheckExEnv') > base::cat("name\tuser\tsystem\telapsed\n", file=base::get(".ExTimings", pos = 'CheckExEnv')) > base::assign(".format_ptime", + function(x) { + if(!is.na(x[4L])) x[1L] <- x[1L] + x[4L] + if(!is.na(x[5L])) x[2L] <- x[2L] + x[5L] + options(OutDec = '.') + format(x[1L:3L], digits = 7L) + }, + pos = 'CheckExEnv') > > ### * > library('mvtnorm') > > base::assign(".oldSearch", base::search(), pos = 'CheckExEnv') > cleanEx() > nameEx("Mvnorm") > ### * Mvnorm > > flush(stderr()); flush(stdout()) > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: Mvnorm > ### Title: Multivariate Normal Density and Random Deviates > ### Aliases: dmvnorm rmvnorm > ### Keywords: distribution multivariate > > ### ** Examples > > dmvnorm(x=c(0,0)) [1] 0.1591549 > dmvnorm(x=c(0,0), mean=c(1,1)) [1] 0.05854983 > > sigma <- matrix(c(4,2,2,3), ncol=2) > x <- rmvnorm(n=500, mean=c(1,2), sigma=sigma) > colMeans(x) [1] 0.9492868 1.9916602 > var(x) [,1] [,2] [1,] 4.276371 2.105874 [2,] 2.105874 3.186058 > > x <- rmvnorm(n=500, mean=c(1,2), sigma=sigma, method="chol") > colMeans(x) [1] 0.8724731 1.9804160 > var(x) [,1] [,2] [1,] 4.804847 2.430442 [2,] 2.430442 3.148344 > > plot(x) > > > > base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") > base::cat("Mvnorm", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() > nameEx("Mvt") > ### * Mvt > > flush(stderr()); flush(stdout()) > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: Mvt > ### Title: The Multivariate t Distribution > ### Aliases: dmvt rmvt > ### Keywords: distribution multivariate > > ### ** Examples > > ## basic evaluation > dmvt(x = c(0,0), sigma = diag(2)) [1] -1.837877 > > ## check behavior for df=0 and df=Inf > x <- c(1.23, 4.56) > mu <- 1:2 > Sigma <- diag(2) > x0 <- dmvt(x, delta = mu, sigma = Sigma, df = 0) # default log = TRUE! > x8 <- dmvt(x, delta = mu, sigma = Sigma, df = Inf) # default log = TRUE! > xn <- dmvnorm(x, mean = mu, sigma = Sigma, log = TRUE) > stopifnot(identical(x0, x8), identical(x0, xn)) > > ## X ~ t_3(0, diag(2)) > x <- rmvt(100, sigma = diag(2), df = 3) # t_3(0, diag(2)) sample > plot(x) > > ## X ~ t_3(mu, Sigma) > n <- 1000 > mu <- 1:2 > Sigma <- matrix(c(4, 2, 2, 3), ncol=2) > set.seed(271) > x <- rep(mu, each=n) + rmvt(n, sigma=Sigma, df=3) > plot(x) > > ## Note that the call rmvt(n, mean=mu, sigma=Sigma, df=3) does *not* > ## give a valid sample from t_3(mu, Sigma)! [and thus throws an error] > try(rmvt(n, mean=mu, sigma=Sigma, df=3)) Error in rmvt(n, mean = mu, sigma = Sigma, df = 3) : Providing 'mean' does *not* sample from a multivariate t distribution! > > ## df=Inf correctly samples from a multivariate normal distribution > set.seed(271) > x <- rep(mu, each=n) + rmvt(n, sigma=Sigma, df=Inf) > set.seed(271) > x. <- rmvnorm(n, mean=mu, sigma=Sigma) > stopifnot(identical(x, x.)) > > > > base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") > base::cat("Mvt", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() > nameEx("pmvnorm") > ### * pmvnorm > > flush(stderr()); flush(stdout()) > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: pmvnorm > ### Title: Multivariate Normal Distribution > ### Aliases: pmvnorm > ### Keywords: distribution > > ### ** Examples > > > 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) [1] 0.5800051 attr(,"error") [1] 0.0002696831 attr(,"msg") [1] "Normal Completion" > > 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) [1] 0.8279847 attr(,"error") [1] 2.658133e-07 attr(,"msg") [1] "Normal Completion" > > # 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))) > > > > > base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") > base::cat("pmvnorm", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() > nameEx("pmvt") > ### * pmvt > > flush(stderr()); flush(stdout()) > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: pmvt > ### Title: Multivariate t Distribution > ### Aliases: pmvt > ### Keywords: distribution > > ### ** Examples > > > n <- 5 > lower <- -1 > upper <- 3 > df <- 4 > corr <- diag(5) > corr[lower.tri(corr)] <- 0.5 > delta <- rep(0, 5) > prob <- pmvt(lower=lower, upper=upper, delta=delta, df=df, corr=corr) > print(prob) [1] 0.5063832 attr(,"error") [1] 0.0002426557 attr(,"msg") [1] "Normal Completion" > > pmvt(lower=-Inf, upper=3, df = 3, sigma = 1) == pt(3, 3) upper TRUE > > # Example from R News paper (original by Edwards and Berry, 1987) > > n <- c(26, 24, 20, 33, 32) > V <- diag(1/n) > df <- 130 > C <- c(1,1,1,0,0,-1,0,0,1,0,0,-1,0,0,1,0,0,0,-1,-1,0,0,-1,0,0) > C <- matrix(C, ncol=5) > ### scale matrix > cv <- C %*% V %*% t(C) > ### correlation matrix > dv <- t(1/sqrt(diag(cv))) > cr <- cv * (t(dv) %*% dv) > delta <- rep(0,5) > > myfct <- function(q, alpha) { + lower <- rep(-q, ncol(cv)) + upper <- rep(q, ncol(cv)) + pmvt(lower=lower, upper=upper, delta=delta, df=df, + corr=cr, abseps=0.0001) - alpha + } > > ### uniroot for this simple problem > round(uniroot(myfct, lower=1, upper=5, alpha=0.95)$root, 3) [1] 2.561 > > # compare pmvt and pmvnorm for large df: > > a <- pmvnorm(lower=-Inf, upper=1, mean=rep(0, 5), corr=diag(5)) > b <- pmvt(lower=-Inf, upper=1, delta=rep(0, 5), df=rep(300,5), + corr=diag(5)) Warning in if (df < 0) stop("cannot compute multivariate t distribution with ", : the condition has length > 1 and only the first element will be used Warning in if (isInf(df)) df <- 0 : the condition has length > 1 and only the first element will be used > a [1] 0.4215702 attr(,"error") [1] 0 attr(,"msg") [1] "Normal Completion" > b [1] 0.4211423 attr(,"error") [1] 2.31377e-06 attr(,"msg") [1] "Normal Completion" > > stopifnot(round(a, 2) == round(b, 2)) > > # correlation and scale matrix > > a <- pmvt(lower=-Inf, upper=2, delta=rep(0,5), df=3, + sigma = diag(5)*2) > b <- pmvt(lower=-Inf, upper=2/sqrt(2), delta=rep(0,5), + df=3, corr=diag(5)) > attributes(a) <- NULL > attributes(b) <- NULL > a [1] 0.5653944 > b [1] 0.5654 > stopifnot(all.equal(round(a,3) , round(b, 3))) > > a <- pmvt(0, 1,df=10) > attributes(a) <- NULL > b <- pt(1, df=10) - pt(0, df=10) > stopifnot(all.equal(round(a,10) , round(b, 10))) > > > > > base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") > base::cat("pmvt", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() > nameEx("qmvnorm") > ### * qmvnorm > > flush(stderr()); flush(stdout()) > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: qmvnorm > ### Title: Quantiles of the Multivariate Normal Distribution > ### Aliases: qmvnorm > ### Keywords: distribution > > ### ** Examples > > qmvnorm(0.95, sigma = diag(2), tail = "both") $quantile [1] 2.236358 $f.quantile [1] -1.681424e-06 attr(,"message") [1] "Normal Completion" > > > > base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") > base::cat("qmvnorm", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > cleanEx() > nameEx("qmvt") > ### * qmvt > > flush(stderr()); flush(stdout()) > > base::assign(".ptime", proc.time(), pos = "CheckExEnv") > ### Name: qmvt > ### Title: Quantiles of the Multivariate t Distribution > ### Aliases: qmvt > ### Keywords: distribution > > ### ** Examples > > ## basic evaluation > qmvt(0.95, df = 16, tail = "both") $quantile [1] 2.119905 $f.quantile [1] 0.975 > > ## check behavior for df=0 and df=Inf > Sigma <- diag(2) > set.seed(29) > q0 <- qmvt(0.95, sigma = Sigma, df = 0, tail = "both")$quantile > set.seed(29) > q8 <- qmvt(0.95, sigma = Sigma, df = Inf, tail = "both")$quantile > set.seed(29) > qn <- qmvnorm(0.95, sigma = Sigma, tail = "both")$quantile > stopifnot(identical(q0, q8), + isTRUE(all.equal(q0, qn, tol = (.Machine$double.eps)^(1/3)))) > > ## if neither sigma nor corr are provided, corr = 1 is used internally > df <- 0 > set.seed(29) > qt95 <- qmvt(0.95, df = df, tail = "both")$quantile > set.seed(29) > qt95.c <- qmvt(0.95, df = df, corr = 1, tail = "both")$quantile > set.seed(29) > qt95.s <- qmvt(0.95, df = df, sigma = 1, tail = "both")$quantile > stopifnot(identical(qt95, qt95.c), + identical(qt95, qt95.s)) > > df <- 4 > set.seed(29) > qt95 <- qmvt(0.95, df = df, tail = "both")$quantile > set.seed(29) > qt95.c <- qmvt(0.95, df = df, corr = 1, tail = "both")$quantile > set.seed(29) > qt95.s <- qmvt(0.95, df = df, sigma = 1, tail = "both")$quantile > stopifnot(identical(qt95, qt95.c), + identical(qt95, qt95.s)) > > > > base::assign(".dptime", (proc.time() - get(".ptime", pos = "CheckExEnv")), pos = "CheckExEnv") > base::cat("qmvt", base::get(".format_ptime", pos = 'CheckExEnv')(get(".dptime", pos = "CheckExEnv")), "\n", file=base::get(".ExTimings", pos = 'CheckExEnv'), append=TRUE, sep="\t") > ### *