mnormt (version 2.0.2)

mtruncnorm: The multivariate truncated normal distribution

Description

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

Usage

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.

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.

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.

See Also

sadmvn for regulating accuracy, tmvnsim for details on the underlying function generating random numbers

Examples

Run this code
# NOT RUN {
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[1], rb[1]), y=c(ra[2], rb[2]), xlab="", ylab="",type="n")
points(sample, pch=20, col=4, cex=0.4)
xbox <- c(a[1], b[1], b[1], a[1], a[1])
ybox <- c(a[2], a[2], b[2], b[2], a[2])
polygon(xbox, ybox, lty=2, border="gray60")
text(pts[,1], pts[,2], col=2, labels=rownames(pts),  cex=0.9)
# }

Run the code above in your browser using DataCamp Workspace