# mtruncnorm: The multivariate truncated normal distribution In mnormt: The Multivariate Normal and t Distributions, and Their Truncated Versions

## Description

The probability density function, the distribution function and random number generation for the multivariate truncated normal (Gaussian) distribution

## Usage

 ```1 2 3``` ```dmtruncnorm(x, mean, varcov, lower, upper, log = FALSE, ...) pmtruncnorm(x, mean, varcov, lower, upper, ...) rmtruncnorm(n, mean, varcov, lower, upper) ```

## Arguments

 `x` either a vector of length `d` or a matrix with `d` columns, where `d=ncol(varcov)`, representing the coordinates of the point(s) where the density must be evaluated. `mean` a vector representing the mean value of the pre-truncation normal distribution. `varcov` a symmetric positive-definite matrix representing the variance matrix of the pre-truncation normal distribution. `lower` a vector representing the lower truncation values of the component variables; `-Inf` values are allowed. If missing, it is set equal to `rep(-Inf, d)`. `upper` a vector representing the upper truncation values of the component variables; `Inf` values are allowed. If missing, it is set equal to `rep(Inf, d)`. `log` a logical value (default value is `FALSE`); if `TRUE`, the logarithm of the density is computed. `...` arguments passed to `sadmvn`, among `maxpts`, `abseps`, `releps`. `n` the number of (pseudo) random vectors to be generated.

## Details

For `dmtruncnorm` and `pmtruncnorm`, the dimension `d` cannot exceed `20`.

Function `rmtruncnorm` is just a wrapper of the imported function `tmvnsim`, set up so that the names and the pattern of the `rmtruncnorm` arguments are in agreement with the other functions in the package.

## Value

a numeric vector in case of `dmtruncnorm` and `pmtruncnorm`; a matrix in case of `rmtruncnorm`, unless `n=1` in which case it is a vector.

## Author(s)

`sadmvn` for regulating accuracy, `tmvnsim` for details on the underlying function generating random numbers
 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27``` ```m2 <- c(0.5, -1) V2 <- matrix(c(3, 3, 3, 6), 2, 2) lower <- a <- c(-1, -2.5) upper <- b <- c(2, 1) set.seed(1) # generate a set of coordinates, pts, on the plane pts <- matrix(runif(10, min=-1.5, max=1.5), nrow=5, ncol=2) rownames(pts) <- LETTERS[1:nrow(pts)] # compute PDF and CDF at the chosen coordinates, pts pdf <- dmtruncnorm(pts, mean=m2, varcov=V2, lower, upper) cdf <- pmtruncnorm(pts, mean=m2, varcov=V2, lower, upper) print(cbind(pts, pdf, cdf)) #-- # generate a sample of random numbers sample <- rmtruncnorm(300, mean=m2, varcov=V2, lower, upper) # # then plot the sample along with the pertaining box and the earlier points, # just for graphical illustration eps <- 0.5 ra <- lower -eps rb <- upper + eps plot(x=c(ra, rb), y=c(ra, rb), xlab="", ylab="",type="n") points(sample, pch=20, col=4, cex=0.4) xbox <- c(a, b, b, a, a) ybox <- c(a, a, b, b, a) polygon(xbox, ybox, lty=2, border="gray60") text(pts[,1], pts[,2], col=2, labels=rownames(pts), cex=0.9) ```