R/Rayleigh.MC.R

Defines functions Rayleigh.MC

Documented in Rayleigh.MC

#' @title Generate samples from Rayleigh distribution
#' @description Generate samples from Rayleigh distribution
#' @param x vector of quantiles
#' @param sigma the parameter of Rayleigh density
#' @param m the sample size
#' @param antithetic using antithetic method or not
#' @return a random sample of size \code{length(x)}
#' @examples
#' \dontrun{
#' X <- sample.chain(100,1)
#' print(Gelman.Rubin(X))
#' }
#' @export
Rayleigh.MC <- function(x, sigma, m = 10000, antithetic = TRUE) {
  u <- runif(m/2)
  if (!antithetic){
    v <- runif(m/2)
  }else{
    v <- 1 - u
  }
  u <- c(u, v)
  cdf <- numeric(length(x))
  for (i in 1:length(x)) {
    g <- x[i]^2/sigma^2 * u * exp(-x[i]^2/(2 * sigma^2) * u^2)
    cdf[i] <- mean(g)
  }
  return(cdf)
}
18087/StatComp18087 documentation built on May 5, 2019, 11:05 p.m.