#' returns an approximate (discrete approximation) sample from a truncated invarse gamma distribution #' @param n number of sample to be generated #' @param alpha= shape parameter #' @param beta= rate parameter #' @param l= lower truncation value #' @param u= upper truncation value #' @param partition is a parameter such that the generated discrete random variable will be of precise upto log_10(partition) decimal places #' @param epsilon is the tail probability left for a gamma random variable while upper truncation in Infinity. default value is .00001 #' @export rDiscreteTInvGamma<-function(n=1,alpha,beta,l,u,partition=10,epsilon=.00001){ #print(" This Truncated Gamma sample is generated by Discrete approximation") if(u==Inf){ u=l*exp(1)*( (1+exp(1))*(1-epsilon)/epsilon )^(1/alpha); #print(paste("truncated at upper=",u,"instead of upper=Inf")) } if(u<=0){print("Warning: Upper bound for truncated invarse gamma is Negative");return(NULL)} if(l<0){print("Warning: Lower bound for truncated invarse gamma is Negative");l=0} if(l==u){print("Warning: Lower bound and Upper bound for truncated invarse gamma is SAME");return(l)} if(l > u){print("Serious Warning: Lower bound is greater than the Upper bound for truncated invarse gamma.");return(l)} #print(paste("Gamma lower=",l,"upper=",u)) if(l==0){l=(u-l)/100000} if((u-l)/1000<1/partition){S=seq(l,u,(u-l)/1000)} if((u-l)/1000>=1/partition){S=seq((l+.5/partition),u,1/partition)} #print(S) Lprob=(-beta/S)+log(S)*(-alpha-1) prob=exp(Lprob-max(Lprob)) S1=S[which(prob>0)] prob1=prob[which(prob>0)] x=sample(S1, size=n, replace = TRUE, prob = prob1) print(paste("sampled gamma",x)) #return(prob) return(x) }