Title: | Polya Gamma Distribution Sampler |
---|---|
Description: | Provides access to a series of highly performant random distribution samplers for the Polya Gamma Distribution as described by Polson, Scott, and Windle (2013) <arXiv:1205.0310> using either 'C++' headers for 'Rcpp' or 'RcppArmadillo' and 'R'. The 'C++' header approach was developed to enable computations in Balamuta (2021) <https://www.ideals.illinois.edu/items/121209>. |
Authors: | James Balamuta [aut, cre, cph] |
Maintainer: | James Balamuta <[email protected]> |
License: | GPL (>= 3) |
Version: | 0.2.4 |
Built: | 2024-11-05 06:13:24 UTC |
Source: | https://github.com/tmsalab/pg |
Compute the theoretical mean and variance for a Polya Gamma variable.
pg_mean(h, z) pg_var(h, z)
pg_mean(h, z) pg_var(h, z)
h |
A single |
z |
A single |
Either the theoretical mean or theoretical variance for a Polya Gamma distribution.
# Fixed parameter distribution simulation ---- ## Parameters ---- h = 1; z = .5 ## Attempt distribution recovery ---- vector_of_pg_samples = rpg_vector(1e6, h, z) head(vector_of_pg_samples) length(vector_of_pg_samples) ## Obtain the empirical results ---- empirical_mean = mean(vector_of_pg_samples) empirical_var = var(vector_of_pg_samples) ## Take the theoretical values ---- theoretical_mean = pg_mean(h, z) theoretical_var = pg_var(h, z) ## Form a comparison table ---- # empirically sampled vs. theoretical values rbind(c(empirical_mean, theoretical_mean), c(empirical_var, theoretical_var))
# Fixed parameter distribution simulation ---- ## Parameters ---- h = 1; z = .5 ## Attempt distribution recovery ---- vector_of_pg_samples = rpg_vector(1e6, h, z) head(vector_of_pg_samples) length(vector_of_pg_samples) ## Obtain the empirical results ---- empirical_mean = mean(vector_of_pg_samples) empirical_var = var(vector_of_pg_samples) ## Take the theoretical values ---- theoretical_mean = pg_mean(h, z) theoretical_var = pg_var(h, z) ## Form a comparison table ---- # empirically sampled vs. theoretical values rbind(c(empirical_mean, theoretical_mean), c(empirical_var, theoretical_var))
Chooses the most efficient implemented method to sample from a Polya Gamma distribution. Details on algorithm selection presented below.
rpg_scalar(h, z) rpg_vector(n, h, z) rpg_hybrid(h, z) rpg_gamma(h, z, trunc = 1000L) rpg_devroye(h, z) rpg_sp(h, z) rpg_normal(h, z)
rpg_scalar(h, z) rpg_vector(n, h, z) rpg_hybrid(h, z) rpg_gamma(h, z, trunc = 1000L) rpg_devroye(h, z) rpg_sp(h, z) rpg_normal(h, z)
h |
|
z |
|
n |
The number of samples to taken from a PG(h, z). Used only by the vector sampler. |
trunc |
Truncation cut-off. Only used by the gamma sampler. |
The following sampling cases are enabled:
h > 170
: Normal approximation method
h > 13
: Saddlepoint approximation method
h = 1
or h = 2
: Devroye method
h > 0
: Sum of Gammas method.
h < 0
: Result is automatically set to zero.
A single numeric
value.
# Fixed parameter distribution simulation ---- ## Parameters ---- h = 1; z = .5 ## Sample only one value ---- single_value = rpg_scalar(h, z) single_value ## Attempt distribution recovery ---- vector_of_pg_samples = rpg_vector(1e6, h, z) head(vector_of_pg_samples) length(vector_of_pg_samples) ## Obtain the empirical results ---- empirical_mean = mean(vector_of_pg_samples) empirical_var = var(vector_of_pg_samples) ## Take the theoretical values ---- theoretical_mean = pg_mean(h, z) theoretical_var = pg_var(h, z) ## Form a comparison table ---- # empirically sampled vs. theoretical values rbind(c(empirical_mean, theoretical_mean), c(empirical_var, theoretical_var)) # Varying distribution parameters ---- ## Generate varying parameters ---- u_h = 20:100 u_z = 0.5*u_h ## Sample from varying parameters ---- x = rpg_hybrid(u_h, u_z)
# Fixed parameter distribution simulation ---- ## Parameters ---- h = 1; z = .5 ## Sample only one value ---- single_value = rpg_scalar(h, z) single_value ## Attempt distribution recovery ---- vector_of_pg_samples = rpg_vector(1e6, h, z) head(vector_of_pg_samples) length(vector_of_pg_samples) ## Obtain the empirical results ---- empirical_mean = mean(vector_of_pg_samples) empirical_var = var(vector_of_pg_samples) ## Take the theoretical values ---- theoretical_mean = pg_mean(h, z) theoretical_var = pg_var(h, z) ## Form a comparison table ---- # empirically sampled vs. theoretical values rbind(c(empirical_mean, theoretical_mean), c(empirical_var, theoretical_var)) # Varying distribution parameters ---- ## Generate varying parameters ---- u_h = 20:100 u_z = 0.5*u_h ## Sample from varying parameters ---- x = rpg_hybrid(u_h, u_z)