| rmvnorm | R Documentation |
Fast ways to draw multivariate normals when the variance or precision matrix is sparse.
rmvnorm(n, mu=rep.int(0, dim(Sigma)[1]), Sigma, ..., mean, sigma)
rmvnorm.spam(n, mu=rep.int(0, dim(Sigma)[1]), Sigma, Rstruct=NULL, ..., mean, sigma)
rmvnorm.prec(n, mu=rep.int(0, dim(Q)[1]), Q, Rstruct=NULL, ...)
rmvnorm.canonical(n, b, Q, Rstruct=NULL, ...)
n |
number of observations. |
mu |
mean vector. |
Sigma |
covariance matrix (of class |
Q |
precision matrix. |
b |
vector determining the mean. |
Rstruct |
the Cholesky structure of |
... |
arguments passed to |
mean, sigma |
similar to |
All functions rely on a Cholesky factorization of the
covariance or precision matrix.
The functions rmvnorm.prec and rmvnorm.canonical
do not require sparse precision matrices
Depending on the the covariance matrix Sigma, rmvnorm
or rmvnorm.spam is used. If wrongly specified, dispatching to
the other function is done.
Default mean is zero. Side note: mean is added via sweep() and
no gain is accieved by distinguishing this case.
Often (e.g., in a Gibbs sampler setting), the sparsity structure of
the covariance/precision does not change. In such setting, the
Cholesky factor can be passed via Rstruct in which only updates
are performed (i.e., update.spam.chol.NgPeyton instead of a
full chol).
Reinhard Furrer
See references in chol.
rgrf, chol and ordering.
# Generate multivariate from a covariance inverse:
# (usefull for GRMF)
set.seed(13)
n <- 25 # dimension
N <- 1000 # sample size
Sigmainv <- .25^abs(outer(1:n,1:n,"-"))
Sigmainv <- as.spam( Sigmainv, eps=1e-4)
Sigma <- solve( Sigmainv) # for verification
iidsample <- array(rnorm(N*n),c(n,N))
mvsample <- backsolve( chol(Sigmainv), iidsample)
norm( var(t(mvsample)) - Sigma, type="m")
# compare with:
mvsample <- backsolve( chol(as.matrix( Sigmainv)), iidsample, n)
#### ,n as patch
norm( var(t(mvsample)) - Sigma, type="m")
# 'solve' step by step:
b <- rnorm( n)
R <- chol(Sigmainv)
norm( backsolve( R, forwardsolve( R, b))-
solve( Sigmainv, b) )
norm( backsolve( R, forwardsolve( R, diag(n)))- Sigma )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.