R/Antithetic.method.R

#' @title The antithetic variables method for variance reducing
#' @description The antithetic variables method to variance reducing in samples from a Rayleigh distribution
#' @param n the number of observations
#' @param sigma parameter in Rayleigh distribution function
#' @return The percent reduction in variance by using antithetic variables method
#' @examples
#' \dontrun{
#' Antithetic.method(1, 1000)
#' }
#' @export
Antithetic.method<- function(sigma, n) {#construct funtions to generate samples from  Rayleigh distribution
  X <- X_ <- numeric(n)
  for (i in 1:n) {
    U<- runif(n)#generate n observations from U~U(0,1)
    V<- 1-U#then V~U(0,1)
    X<- sigma*sqrt(-2*log(V))
    X_<-sigma*sqrt(-2*log(U))
    #according inverse transform from annalysis above,we obtain that $X=F_{X}^{-1}(U)=\sqrt{-2ln(1-U)}=\sqrt{-2ln(V)}$\qquad
    var1<-var(X)#if X1 and X2 are independent,then var((X1+X2)/2)=var(X)
    var2<-(var(X)+var(X_)+2*cov(X,X_))/4#compute the variance of (X+X')/2 generated by antithetic variables
    reduction <-((var1-var2)/var1)#compute the reduction in variance by using antithetic variables
  }
  percent_reduction<-paste0(format(100*reduction, format = "fg", digits = 4), "%")#set format type by function format
  return(percent_reduction)
}
zhangmanustc/StatComp18024 documentation built on May 9, 2019, 10:45 a.m.