I need to draw a random number from an exponential distribution (rate $mu$) that is right-truncated with the truncation value coming from a gamma distribution (shape $k$, rate $lambda$).
My naive approach would have been to first draw the truncation value, and then draw from the truncated exponential distribution via Inverse Transform Sampling. However, such draws do not follow the same distribution which I get from simulation results.
The following R code demonstrates that this approach is for some reason not working:
# parameter settings
k <- 2
lambda <- 1
mu <- 0.1
n <- 1e7 # number of draws
# draw right truncation values
trunc_value <- rgamma(n=n, shape=k, rate=lambda)
# draw from right-truncated exponential via inverse transform sampling
itexp <- function(u, rate, trunc) { -log(1-u*(1-exp(-trunc*rate)))/rate }
rtexp <- function(n, rate, trunc) { itexp(runif(n), rate, trunc) }
x0 <- rtexp(n=n, rate=mu, trunc_value)
# simulate by drawing from exponential and then discarding values larger than trunc
x1 <- rexp(n, mu)
x1 <- x1[x1 < trunc_value]
# means differ significantly
mean(x0) # 0.95
mean(x1) # 1.34
trunc_valuehas a mean of about $2$ by construction.But if you take those which were greater than the original
x1you will find those used for the the non-rejectedx1had a mean of about $2.86$.So your rejection method has biased
trunc_valueupwards and so biased the non-rejectedx1values upwards.