# Generate 1000 values from the truncated distributions with alpha = 5, mu = 0, tau = 1
set.seed(123456)
n_draws <- 1000
z_p <- rtgin(n_draws, 5, 0, 1, TRUE)
z_n <- rtgin(n_draws, 5, 0, 1, FALSE)
# Compare generation from truncation to positive reals with true density
n_values <- 200
z_vals <- seq(-5, 5, length.out = n_values)
fz_p <- sapply(z_vals[z_vals > 0], function(z) dtgin(z, 5, 0, 1, TRUE, FALSE))
fz_p <- c(rep(0, n_values - sum(z_vals > 0)), fz_p)
temp <- hist(z_p, breaks = 100, plot = FALSE)
plot(temp, freq = FALSE, xlim = c(-5, 5), ylim = range(c(fz_p, temp$density)),
main = '', xlab = 'Values', ylab = 'Density', col = 'blue')
lines(z_vals, fz_p, col = 'red', lwd = 2)
# Compare generation from truncation to negative reals with true density
fz_n <- sapply(z_vals[z_vals < 0], function(z) dtgin(z, 5, 0, 1, FALSE, FALSE))
fz_n <- c(fz_n, rep(0, n_values - sum(z_vals < 0)))
temp <- hist(z_n, breaks = 100, plot = FALSE)
plot(temp, freq = FALSE, xlim = c(-5, 5), ylim = range(c(fz_n, temp$density)),
main = '', xlab = 'Values', ylab = 'Density', col = 'blue')
lines(z_vals, fz_n, col = 'red', lwd = 2)
# verbose = TRUE provides info on the acceptance rate of the
# ratio-of-uniforms acceptance-rejection method for sampling the variables
draw_list <- rtgin(50, 5, 0, 1, sign = TRUE, verbose = TRUE)
draw_list$ARiters # Acceptance-Rejection iterations
draw_list$avg_arate # Average of 1/ARiters
Run the code above in your browser using DataLab