rtgin {ginormal} | R Documentation |
Generating random numbers from the generalized inverse normal distribution truncated to the positive or negative reals
Description
Generating random numbers from the generalized inverse normal distribution truncated to the positive or negative reals
Usage
rtgin(
size,
alpha,
mu,
tau,
sign,
algo = "hormann",
method = "Fortran",
verbose = FALSE
)
Arguments
size |
number of desired draws. Output is numpy vector of length equal to size. |
alpha |
degrees-of-freedom parameter. |
mu |
similar to location parameter, controls asymmetry of the distribution. |
tau |
similar to scale parameter, controls spread of the distribution. |
sign |
logical. |
algo |
string with desired algorithm to compute minimal bounding rectangle. If "hormann", use the method from Hörmann and Leydold (2014). When "leydold", use the one from Leydold (2001). Defaults to "hormann" and returns an error for any other values. |
method |
string with the method used to compute the parabolic cylinder function
in the normalization constant. |
verbose |
logical; should the acceptance rate from the ratio-of-uniforms method be provided along with additional information? Defaults to FALSE. |
Details
Currently, only values of alpha
> 2 are supported. For Bayesian posterior sampling,
alpha
is always larger than 2 even for non-informative priors.
Generate from positive region (z
> 0) hen sign = TRUE
, and from
negative region (z
< 0) when sign = FALSE
. When verbose = TRUE
,
a list is returned containing the actual draw in value
, as well as average
acceptance rate avg_arate
and total number of acceptance-rejection steps ARiters
.
Value
If verbose = FALSE
(default), a numeric vector of length size
.
Otherwise, a list with components value
, avg_arate
, and ARiters
Examples
# 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