Title: | Truncated Multivariate Normal and t Distribution Simulation |
---|---|
Description: | Simulation of random vectors from truncated multivariate normal and t distributions based on the algorithms proposed by Yifang Li and Sujit K. Ghosh (2015) <doi:10.1080/15598608.2014.996690>. |
Authors: | Kaifeng Lu |
Maintainer: | Kaifeng Lu <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.1.3 |
Built: | 2024-11-17 06:43:29 UTC |
Source: | CRAN |
Simulation of random vectors from truncated multivariate normal and t distributions based on the algorithms proposed by Yifang Li and Sujit K. Ghosh (2015) <doi:10.1080/15598608.2014.996690>. We allow the mean, lower and upper bounds to differ across samples to accommodate regression problems. The algorithms are implemented in C++ and hence are highly efficient.
Kaifeng Lu, [email protected]
Yifang Li and Sujit K. Ghosh. Efficient Sampling Methods for Truncated Multivariate Normal and Student-t Distributions Subject to Linear Inequality Constraints. Journal of Statistical Theory and Practice. 2015;9:712-732. doi:10.1080/15598608.2014.996690
Draws from truncated multivariate normal distribution subject to linear inequality constraints represented by a matrix.
rtmvnorm( mean, sigma, blc = NULL, lower, upper, init = NULL, burn = 10, n = NULL )
rtmvnorm( mean, sigma, blc = NULL, lower, upper, init = NULL, burn = 10, n = NULL )
mean |
|
sigma |
|
blc |
|
lower |
|
upper |
|
init |
|
burn |
number of burn-in iterations. Defaults to 10. |
n |
number of random samples when |
Returns an n x p
matrix of random numbers following the
specified truncated multivariate normal distribution.
# Example 1: full rank blc d = 3; rho = 0.9; sigma = matrix(0, d, d); sigma = rho^abs(row(sigma) - col(sigma)); blc = diag(1,d); n = 1000; mean = matrix(rep(1:d,n), nrow=n, ncol=d, byrow=TRUE); set.seed(1203) result = rtmvnorm(mean, sigma, blc, -1, 1, burn=50) apply(result, 2, summary) # Example 2: use the alternative form of input set.seed(1203) result = rtmvnorm(mean=1:d, sigma, blc, -1, 1, burn=50, n=1000) apply(result, 2, summary) # Example 3: non-full rank blc d = 3; rho = 0.5; sigma = matrix(0, d, d); sigma = rho^abs(row(sigma) - col(sigma)); blc = matrix(c(1,1,1,0,1,0,1,0,1), ncol=d); n = 100; mean = matrix(rep(1:d,n), nrow=n, ncol=d, byrow=TRUE); set.seed(1228) result = rtmvnorm(mean, sigma, blc, -1, 1, burn=10) apply(result, 2, summary) # Example 4: non-full rank blc, alternative form of input set.seed(1228) result = rtmvnorm(mean=1:d, sigma, blc, -1, 1, burn=10, n=100) apply(result, 2, summary) # Example 5: means, lower, or upper bounds differ across samples d = 3; rho = 0.5; sigma = matrix(0, d, d); sigma = rho^abs(row(sigma) - col(sigma)); blc = matrix(c(1,0,1,1,1,0), ncol=d, byrow=TRUE) n = 100; set.seed(3084) mean = matrix(runif(n*d), nrow=n, ncol=d); result = rtmvnorm(mean, sigma, blc, -1, 1, burn=50) apply(result, 2, summary)
# Example 1: full rank blc d = 3; rho = 0.9; sigma = matrix(0, d, d); sigma = rho^abs(row(sigma) - col(sigma)); blc = diag(1,d); n = 1000; mean = matrix(rep(1:d,n), nrow=n, ncol=d, byrow=TRUE); set.seed(1203) result = rtmvnorm(mean, sigma, blc, -1, 1, burn=50) apply(result, 2, summary) # Example 2: use the alternative form of input set.seed(1203) result = rtmvnorm(mean=1:d, sigma, blc, -1, 1, burn=50, n=1000) apply(result, 2, summary) # Example 3: non-full rank blc d = 3; rho = 0.5; sigma = matrix(0, d, d); sigma = rho^abs(row(sigma) - col(sigma)); blc = matrix(c(1,1,1,0,1,0,1,0,1), ncol=d); n = 100; mean = matrix(rep(1:d,n), nrow=n, ncol=d, byrow=TRUE); set.seed(1228) result = rtmvnorm(mean, sigma, blc, -1, 1, burn=10) apply(result, 2, summary) # Example 4: non-full rank blc, alternative form of input set.seed(1228) result = rtmvnorm(mean=1:d, sigma, blc, -1, 1, burn=10, n=100) apply(result, 2, summary) # Example 5: means, lower, or upper bounds differ across samples d = 3; rho = 0.5; sigma = matrix(0, d, d); sigma = rho^abs(row(sigma) - col(sigma)); blc = matrix(c(1,0,1,1,1,0), ncol=d, byrow=TRUE) n = 100; set.seed(3084) mean = matrix(runif(n*d), nrow=n, ncol=d); result = rtmvnorm(mean, sigma, blc, -1, 1, burn=50) apply(result, 2, summary)
Draws from truncated multivariate t distribution subject to linear inequality constraints represented by a matrix.
rtmvt( mean, sigma, nu, blc = NULL, lower, upper, init = NULL, burn = 10, n = NULL )
rtmvt( mean, sigma, nu, blc = NULL, lower, upper, init = NULL, burn = 10, n = NULL )
mean |
|
sigma |
|
nu |
degrees of freedom for Student-t distribution. |
blc |
|
lower |
|
upper |
|
init |
|
burn |
number of burn-in iterations. Defaults to 10. |
n |
number of random samples when |
Returns an n x p
matrix of random numbers following the
specified truncated multivariate t distribution.
# Example 1: full rank blc d = 3; rho = 0.5; sigma = matrix(0, d, d); sigma = rho^abs(row(sigma) - col(sigma)); nu = 10; blc = diag(1,d); n = 1000; mean = matrix(rep(1:d,n), nrow=n, ncol=d, byrow=TRUE); set.seed(1203) result = rtmvt(mean, sigma, nu, blc, -1, 1, burn=50) apply(result, 2, summary) # Example 2: use the alternative form of input set.seed(1203) result = rtmvt(mean=1:d, sigma, nu, blc, -1, 1, burn=50, n) apply(result, 2, summary) # Example 3: non-full rank blc, different means d = 3; rho = 0.5; sigma = matrix(0, d, d); sigma = rho^abs(row(sigma) - col(sigma)); nu = 10; blc = matrix(c(1,0,1,1,1,0), nrow=d-1, ncol=d, byrow=TRUE) n = 100; set.seed(3084) mean = matrix(runif(n*d), nrow=n, ncol=d); result = rtmvt(mean, sigma, nu, blc, -1, 1, burn=50) apply(result, 2, summary)
# Example 1: full rank blc d = 3; rho = 0.5; sigma = matrix(0, d, d); sigma = rho^abs(row(sigma) - col(sigma)); nu = 10; blc = diag(1,d); n = 1000; mean = matrix(rep(1:d,n), nrow=n, ncol=d, byrow=TRUE); set.seed(1203) result = rtmvt(mean, sigma, nu, blc, -1, 1, burn=50) apply(result, 2, summary) # Example 2: use the alternative form of input set.seed(1203) result = rtmvt(mean=1:d, sigma, nu, blc, -1, 1, burn=50, n) apply(result, 2, summary) # Example 3: non-full rank blc, different means d = 3; rho = 0.5; sigma = matrix(0, d, d); sigma = rho^abs(row(sigma) - col(sigma)); nu = 10; blc = matrix(c(1,0,1,1,1,0), nrow=d-1, ncol=d, byrow=TRUE) n = 100; set.seed(3084) mean = matrix(runif(n*d), nrow=n, ncol=d); result = rtmvt(mean, sigma, nu, blc, -1, 1, burn=50) apply(result, 2, summary)
Draws from truncated univariate normal distribution within an interval.
rtnorm(mean, sd = 1, lower, upper, n = NULL)
rtnorm(mean, sd = 1, lower, upper, n = NULL)
mean |
vector of means. The length is the number of observations. |
sd |
standard deviation. Defaults to 1. |
lower |
a scalar of lower bound for truncation, or a vector of
lower bounds with the same length as |
upper |
a scalar of upper bound for truncation, or a vector of
upper bounds with the same length as |
n |
number of random samples when |
Returns a vector of random numbers following the specified truncated univariate normal distribution.
set.seed(1203) x = rtnorm(mean=rep(1,1000), sd=2, lower=-2, upper=3) summary(x) # use the alternative form of input set.seed(1203) x = rtnorm(mean=1, sd=2, lower=-2, upper=3, n=1000) summary(x)
set.seed(1203) x = rtnorm(mean=rep(1,1000), sd=2, lower=-2, upper=3) summary(x) # use the alternative form of input set.seed(1203) x = rtnorm(mean=1, sd=2, lower=-2, upper=3, n=1000) summary(x)