Skip to main content

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.

Dependencies

  • Numpy >= 1.17

Installation

$ pip install -U polyagamma

Example

Python

polyagamma can act as a drop-in replacement for numpy's Generator class.

import numpy as np

from polyagamma import default_rng, Generator

g = Generator(np.random.PCG64())  # or use default_rng()
print(g.polyagamma())

# Get a 5 by 10 array of PG(1, 2) variates.
print(g.polyagamma(z=2, size=(5, 10)))

# Pass sequences as input. Numpy's broadcasting semantics apply here.
h = [[1, 2, 3, 4, 5], [9, 8, 7, 6, 5]]
print(g.polyagamma(h, 1))

# Pass an output array
out = np.empty(5)
g.polyagamma(out=out)
print(out)

# one can choose a sampling method from {devroye, alternate, gamma}.
# If not given, the default behaviour is a hybrid sampler that picks
# the best method based on the parameter values
out = g.polyagamma(method="devroye")

# other numpy distributions are still accessible
print(g.standard_normal())
print(g.standard_gamma())

C

For an example of how to use polyagamma in a C program, see here.

TODO

  • Add devroye and gamma convolution methods.
  • Add the "alternate" sampling method.
  • Add the hybrid sampler based on a combination of available methods.
  • Add the "saddle point approximation" method.
  • Add the hybrid sampler based on all four methods.
  • Add array broadcasting support for paramater inputs.

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

polyagamma-0.1.0rc1.tar.gz (138.6 kB view details)

Uploaded Source

File details

Details for the file polyagamma-0.1.0rc1.tar.gz.

File metadata

  • Download URL: polyagamma-0.1.0rc1.tar.gz
  • Upload date:
  • Size: 138.6 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

Hashes for polyagamma-0.1.0rc1.tar.gz
Algorithm Hash digest
SHA256 e6558ba4ce561e95bba1042944b5480aac25595b1b14e75c61a772aa24107215
MD5 995e081543ab7622b35d9ce2ba8fbb31
BLAKE2b-256 23a2f8b9b36f10956ee07b46bc5e435970badc6fea3439e13656a1846ebcb7f9

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page