You cannot use 1 uniform random variable to decide whether all proposal sample are accepted or not. Each sample require 1 independent uniform random variable.

Also the optimal is very easy to find.

So according to Dason's code, just need to change

Code:

id.accept <- (u <= 10.125*r)

then will improve the acceptance rate by almost 10 times from 0.06 to 0.65.

I used another proposal function which can be integrated analytically and inverted. Therefore I only need uniform sampling in my code. Gamma sampling also uses rejection sampling.

Code:

tmp <- function(x){
x^4*exp(-3*x)*(1-exp(-2*x))^7
}
k <- 1/integrate(tmp, 0, Inf)$value
desired.density <- function(x){k*tmp(x)}
n <- 100000
proposals <- tan(pi*runif(n)) + 1.6
u <- runif(n)
r <- tmp(proposals)/0.045*(1 + (proposals - 1.6)^2)
id.accept <- (u <= r)
samp <- proposals[id.accept]
accept.rate <- mean(id.accept)
print(accept.rate)
plot(density(samp))
curve(desired.density, add = T, col = "red")

This probably wouldn't be good for your assignment but there is also brute force sampling. The basic idea is that you grid up the support of your distribution. Since the distribution of interest has infinite support we need to find where essentially all of the mass is contained. Then you sample points from your grid with probability proportional to the density of interest.

Code:

tmp <- function(x){
x^4*exp(-3*x)*(1-exp(-2*x))^7
}
integrate(tmp, 0, Inf)$value
# [1] 0.06483012
integrate(tmp, 0, Inf)$value -> k
f <- function(x){1/k*tmp(x)}
integrate(f, 0, 5)
# 0.9986952 with absolute error < 1e-05
integrate(f, 0, 10)
# 1 with absolute error < 1e-05
grid <- seq(0, 10, .00001)
p <- tmp(grid)
?sample
# starting httpd help server ... done
samp <- sample(grid, 10000, replace = TRUE, prob = p)

To fanky: you try to use the Cauchy as the proposal, which will generate negative random variates. Now the target density evaluated at negative points is negative, which make the code u <= r also reject those invalid case as the ratio r < 0; but in general it is not true and need to be more careful.