R/wishartTest.R

Defines functions wishartTest

Documented in wishartTest

#' Test if Matrix is a Wishart Matrix
#' 
#' Given a random Wishart matrix, B, from W_p(Sigma, df) and independent random vector \code{a}, then (a' B a) / (a' Sigma a) 
#' is chi-squared with df degrees of freedom.
#'
#' @param WishMat random Wishart Matrix from W_p(Sigma, df)
#' @param Sigma Covariance matrix for W_p(Sigma, df)
#' @param vec independent random vector
#' 
#' @return A chi-squared random variable with df degrees of freedom.
#'
#' @export
#' @importFrom stats rnorm
#'
#' @examples wishartTest(rWishart(1, 5, diag(1, 20), simplify = FALSE)[[1]], diag(1, 20))
wishartTest <- function(WishMat, Sigma, vec = NULL){
  if(is.null(vec)){
    vec <- rnorm(ncol(WishMat))
  }
  (t(vec) %*% WishMat %*% vec) / 
           (t(vec) %*% Sigma %*% vec)
}

Try the rWishart package in your browser

Any scripts or data that you put into this service are public.

rWishart documentation built on Nov. 20, 2019, 1:07 a.m.