#' rTrunkInvGamma #' generates a truncated invarse gamma random variable by rejection sampling method. If there is too many rejections, then it generates approximate sample according to two steps. initially it tries to get approximate numerical cdf of the truncated invarse gamma #' random variable using the functions "Gammad" and "Truncate" defined in the r package "distr". If the truncation is too acuate to use the approximation by the package "distr" then a #' discrete approximation is used to sample from the random variable. #' @param alpha = shape parameter #' @param beta = scale parameter #' @param l= lower truncation #' @param u=upper truncation #' @param Max_rejection= once the Max_rejection number of rejections occurs it stops the rejection sampler #' and calls the approximate sampler based on the package "distr" #' @param partition : once the approximate sampler based on the package "distr" fails then a discrete approximate sampler #' is applied. log of the partion ( in base 10) is the precession of the discrete approximation. If precission upto 3 decimal places #' is desired then use partion=100. Default value of partition is 10 #' @export rTrunkInvGamma<-function(alpha,beta,l=0,u=Inf,Max_rejection=1000,partition=10){ y = -1 i = 1 while((y<0)*(i=l)*(r<=u),r,-1) i=i+1 } #print(c(alpha,beta,l,u)) if(y<0){ y = try( rTIgammaInvCdf(list(alpha=alpha,beta=beta,l=l,u=u)), 1 ) if(!is.numeric(y)){ y=rDiscreteTInvGamma(n=1,alpha=alpha,beta=beta,l=l,u=u,partition) } } return(y) }