Efficiently sample from the Polya-Gamma distribution using NumPy's Generator interface
Project description
polya-gamma
Efficiently sample from the Polya-Gamma distribution using NumPy's Generator interface.
As expected, the saddle
method improves as h
tends to infinity
.
Features
polyagamma
is written in C and optimized for performance.- It is flexible and allows the user to sample using one of 4 available methods.
- Input parameters can be scalars, arrays or both; allowing for easy generation of multi-dimensional samples without specifying the size.
- Random number generation is thread safe.
- The functional API resembles that of common numpy/scipy functions, therefore making it easy to plugin to existing libraries.
Dependencies
- Numpy >= 1.17
Installation
$ pip install -U polyagamma
Example
Python
import numpy as np
from polyagamma import polyagamma
o = polyagamma()
# Get a 5 by 10 array of PG(1, 2) variates.
o = polyagamma(z=2, size=(5, 10))
# Pass sequences as input. Numpy's broadcasting rules apply here.
h = [[1, 2, 3, 4, 5], [9, 8, 7, 6, 5]]
o = polyagamma(h, 1)
# Pass an output array
out = np.empty(5)
polyagamma(out=out)
print(out)
# one can choose a sampling method from {devroye, alternate, gamma, saddle}.
# If not given, the default behaviour is a hybrid sampler that picks a method
# based on the parameter values.
o = polyagamma(method="devroye")
# We can also use an existing instance of `numpy.random.Generator` as a parameter.
# This is useful to reproduce samples generated via a given seed.
rng = np.random.default_rng(12345)
o = polyagamma(random_state=rng)
C
For an example of how to use polyagamma
in a C program, see here.
References
- Luc Devroye. "On exact simulation algorithms for some distributions related to Jacobi theta functions." Statistics & Probability Letters, Volume 79, Issue 21, (2009): 2251-2259.
- Polson, Nicholas G., James G. Scott, and Jesse Windle. "Bayesian inference for logistic models using Pólya–Gamma latent variables." Journal of the American statistical Association 108.504 (2013): 1339-1349.
- J. Windle, N. G. Polson, and J. G. Scott. "Improved Polya-gamma sampling". Technical Report, University of Texas at Austin, 2013b.
- Windle, Jesse, Nicholas G. Polson, and James G. Scott. "Sampling Polya-Gamma random variates: alternate and approximate techniques." arXiv preprint arXiv:1405.0506 (2014)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
polyagamma-1.0.0b1.tar.gz
(138.2 kB
view details)
File details
Details for the file polyagamma-1.0.0b1.tar.gz
.
File metadata
- Download URL: polyagamma-1.0.0b1.tar.gz
- Upload date:
- Size: 138.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.55.0 CPython/3.8.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | c65690e73d438352d7bb48f573dfefb0bf0427d2992614490a22cba0972a9bf3 |
|
MD5 | 6ef1a86c2cf0f3234a74514b3fa46332 |
|
BLAKE2b-256 | 905468d4a55fcb54351c443d79f2769eb11f51a4f686fb2ff50af0e87a0343c2 |