Learn R Programming

bbricks (version 0.1.4)

rWishart: Random generation for Wishart distribution

Description

Generate random samples from Wishart distribution. For a random matrix x, the density function of Wishart distribution is defined as: $$(2^{(df p)/2} Gamma_p(df/2) |rate|^{-df/2})^{-1} |x|^{(df-p-1)/2} exp(-1/2 tr(x rate))$$ Where x is a pxp symmetric positive definite matrix, Gamma_p() is the multivariate Gamma function of dimension p.

Usage

rWishart(df, rate = NULL, scale = NULL)

Arguments

df

numeric, the degree of freedom.

rate

matrix, a symmetric positive-definite matrix, the 'rate', or 'inverse-scale' parameter. The 'rate' parameter in Wishart is the 'scale' parameter in InvWishart

scale,

matrix, the inverse of rate. Only one of 'rate' and 'scale' should be non-NULL.

Value

A symmetric positive-definite matrix.

References

Smith, W. B., and R. R. Hocking. "Algorithm as 53: Wishart variate generator." Journal of the Royal Statistical Society. Series C (Applied Statistics) 21.3 (1972): 341-345.

Examples

Run this code
# NOT RUN {
rate <- crossprod(matrix(rnorm(15),5,3)) #the prior inverse-scale
m <- matrix(0,3,3)
## get 1000 samples and calculate the sample mean
for(i in 1:100){
    m <- m+rWishart(df=5,rate=rate)/100
}
## m should roughly equal to df*inverse(rate):
m
pdsInverse(rate)*5
## try generating samples with 'rate' parameter:
scale <- pdsInverse(rate)
m2 <- matrix(0,3,3)
for(i in 1:100){
    m2 <- m2+rWishart(df=5,scale=scale)/100
}
## m2 should roughly equal df*scale:
m2
5*scale
# }

Run the code above in your browser using DataLab