| Version: | 2.4 |
| Date: | 2026-06-05 |
| Title: | PolyaGamma Sampling |
| Maintainer: | Jesse Windle <jesse.windle@gmail.com> |
| Description: | Tools for sampling from the PolyaGamma distribution based on Polson, Scott, and Windle (2013) <doi:10.1080/01621459.2013.829001>. Useful for logistic regression. |
| License: | GPL (≥ 3) |
| Depends: | R (≥ 3.6.0) |
| Suggests: | testthat (≥ 3.0.0) |
| BugReports: | https://github.com/jwindle/BayesLogit/issues |
| URL: | https://github.com/jwindle/BayesLogit |
| NeedsCompilation: | yes |
| Packaged: | 2026-06-05 17:24:59 UTC; jesse |
| Author: | Nicholas G. Polson [aut, cph], James G. Scott [aut, cph], Jesse Windle [aut, cre, cph], Jari Oksanen [ctb], James Balamuta [ctb], Andrew Finley [ctb] |
| Repository: | CRAN |
| Date/Publication: | 2026-06-06 05:20:09 UTC |
Polya-Gamma Distribution Moments
Description
Compute the first moment (mean), second moment, and variance of the Polya-Gamma distribution PG(b, z).
Usage
pg.m1(b, z)
pg.m2(b, z)
pg.var(b, z)
Arguments
b |
Shape parameter. Must be positive. |
z |
Tilt parameter. May be any real number. |
Details
For X \sim \mathrm{PG}(b, z), the moments are derived from
the moment generating function
E[e^{tX}] = \left( \frac{\cosh(z/2)}{\cosh(\sqrt{z^2/4 - t/2})} \right)^b.
The first moment is
E[X] = \frac{b \tanh(z/2)}{2z}, \quad z \neq 0,
with E[X] = b/4 when z = 0. Taylor series are used
near z = 0 for numerical stability.
pg.m2 returns E[X^2], and pg.var returns
\mathrm{Var}(X) = E[X^2] - E[X]^2.
Value
A scalar giving the requested moment.
References
Nicholas G. Polson, James G. Scott, and Jesse Windle. Bayesian inference for logistic models using Polya-Gamma latent variables. https://arxiv.org/abs/1205.0310
See Also
Examples
## Mean and variance of PG(1, 0)
pg.m1(1, 0) # 0.25
pg.var(1, 0) # 1/24
## Compare sample moments to theoretical values
set.seed(1)
x <- rpg(10000, h = 2, z = 1)
mean(x)
pg.m1(2, 1)
var(x)
pg.var(2, 1)
Polya-Gamma Random Variates
Description
Generate random variates from the Polya-Gamma distribution.
Usage
rpg(num=1, h=1, z=0.0)
rpg.gamma(num=1, h=1, z=0.0, trunc=200)
rpg.devroye(num=1, h=1, z=0.0)
rpg.sp(num=1, h=1, z=0.0)
rpg.gamma.R(num=1, h=1, z=0.0, trunc=200)
rpg.devroye.R(num=1, h=1, z=0.0)
rpg.sp.R(num=1, h=1, z=0.0)
Arguments
num |
The number of random variates to simulate. |
h |
Shape parameter. |
z |
Parameter associated with tilting. |
trunc |
The number of elements used in sum of gammas approximation. |
Details
A random variable X with distribution PG(h,z) is distributed like
X \sim \sum_{k=1}^\infty G(h,1) / ( 2 \pi^2 (k-1/2)^2 + z^2/2).
The density for X may be derived by exponentially tilting the PG(h,0) density:
p(x|h,z) \propto \exp(-x z^2/2) p(x|h,0).
Different methods for generating this random variable are
implemented, each of which is useful for certain parameters. The
parameters supplied by the user automatically determine which method
is used. One may manually call each routine using
rpg.METHOD. Functions ending in ".R" are pure R
implementations.
You may call rpg when n and z are vectors.
Value
This function returns num Polya-Gamma samples.
References
Nicholas G. Polson, James G. Scott, and Jesse Windle. Bayesian inference for logistic models using Polya-Gamma latent variables. https://arxiv.org/abs/1205.0310
Examples
h = c(1, 2, 3);
z = c(4, 5, 6);
## Devroye-like method -- only use if h contains integers, preferably small integers.
X = rpg.devroye(100, h, z);
h = c(1.2, 2.3, 3.2);
z = c(4, 5, 6);
## Sum of gammas method -- this is slow.
X = rpg.gamma(100, h, z);
h = c(1, 4, 2.3);
z = c(4, 5, 6);
## Hybrid method -- automatically chooses best procedure.
X = rpg(100, h, z);