Skip to main content

Python implementation of Vose's alias method, an efficient algorithm for sampling from a discrete probability distribution.

Project description

Vose-Alias-Method

Python implementation of Vose's alias method, an efficient algorithm for sampling from a discrete probability distribution (a good explanation of which can be found at http://www.keithschwarz.com/darts-dice-coins/).

For example, this code can be used for creating and efficiently sampling from a probability distribution representing rolling a weighted die (i.e where side j has probability P(j) of being rolled). Alternatively, it could be used for creating a simple unigram language model (see example below)

Installation

$ pip install Vose-Alias-Method

Depends on:

Example Usage

In a python shell:

>>> from vose_sampler import VoseAlias
>>> # Create the required probability distribution (here we use the example of a weighted coin with probability H:=Heads=0.2 and T:=Tail=0.8)
>>> dist = {"H":0.2, "T":0.8}
>>> # Create probability and alias tables from the probability distribution, for sampling via Vose's alias method
>>> VA = VoseAlias(dist)
>>> # Generate n random outcomes (here n=10)
>>> VA.sample_n(size=10)
['T', 'T', 'H', 'T', 'T', 'T', 'T', 'H', 'T', 'T']

Unigram language model example

To create a unigram language model for Alice in Wonderland and sample 10 words from this, run the main script from the command line with options:

$ vose-sampler -p data/Alice.txt -n 10  # or: python vose_sampler/vose_sampler.py -p data/Alice.txt -n 10

Generating 10 random samples:

the
more
she
Rabbit,
say
suddenly
at
soon
thing
solemn

[Note, this is intended to illustrate how Vose's alias method could be used. Thus I have not included any preprocessing steps that would make the language model more realistic; for example, we could add handling of upper vs. lower case words (so that e.g. "The" and "the" are not considered distinct), as well as handling of punctuation (e.g. so "the" and "the." are considered the same).

Likewise, should the text(s) you wish to sample from be particularly large, you may wish to integrate my Hadoop MapReduce job for counting the word frequencies of text file(s).]

Tests

Run via: $ python setup.py test (or $ python tests/tests.py)

Build

  • $ python setup.py sdist bdist_wheel
  • $ twine upload dist/* -r testpypi --skip-existing assuming twine is installed and ~/.pypirc exists with something like:
[distutils]
index-servers=
    testpypi
    pypi

[testpypi]
repository = https://test.pypi.org/legacy/
username = asmith26
password = some_password

[pypi]
repository = https://upload.pypi.org/legacy/
username = asmith26
password = some_harder_password
  • Assuming everything looks good $ twine upload dist/*

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

Vose-Alias-Method-1.1.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Vose_Alias_Method-1.1.0-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

File details

Details for the file Vose-Alias-Method-1.1.0.tar.gz.

File metadata

  • Download URL: Vose-Alias-Method-1.1.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for Vose-Alias-Method-1.1.0.tar.gz
Algorithm Hash digest
SHA256 6ddc19008b917cbe46f3b2251ea0a5588f2261ad7b6f4f827285b47f9e099040
MD5 f36ffb97a6648b2a15ad2d5be657124a
BLAKE2b-256 d3fd3c8cb0f5e408e8fdc9afe1e6f1c9097888815cf3284fbb3ca1264937e013

See more details on using hashes here.

File details

Details for the file Vose_Alias_Method-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: Vose_Alias_Method-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.0 setuptools/40.5.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.6

File hashes

Hashes for Vose_Alias_Method-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a929d1ccc2b96bf5b7a08e8326bac2d87186349e65ac1d3ad9272aa79553333c
MD5 8901df9c8bf799a581996cbee55b16fa
BLAKE2b-256 71490f15b676c96d319da11d45a8ee3cd0f257d5070e08943229a59ffe717962

See more details on using hashes here.

Supported by

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