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)
Any suggestions/contributions very welcome.
Installation
$ pip install Vose-Alias-Method
Or via conda: $ conda install -c conda-forge vose-alias-method
Depends on:
- The Python Standard Library https://docs.python.org/3/library/)
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).]
Tests
Run via: $ python setup.py test (or $ python tests/tests.py)
Build
A GitHub Action exists for this at: .github/workflows/publish.yaml
Alternatively build and publish manually with:
$ python setup.py sdist bdist_wheel$ twine upload dist/* -r testpypi --skip-existingassuming 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/* - Create new git release
$ git tag <tagname> && git push origin <tag_name>, and create a new release with the same<tagname>.
Release files for Vose-Alias-Method 1.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| vose_alias_method-1.2.2.tar.gz | 10.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| vose_alias_method-1.2.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.4 kB
Release files / vose_alias_method-1.2.2.tar.gz
| Download URL | vose_alias_method-1.2.2.tar.gz |
|---|---|
| Size | 10.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1333655259733da6241c89e256d75ccd055c326b9e262ec3151c792048b3316b
|
|
BLAKE2b-256 checksum How to use checksums |
6968d30092fcec380844f32a9da56a0d5a0bd68a9362055a1774e1765025e8cd
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 14, 2026.
Transparency logRelease files / vose_alias_method-1.2.2-py3-none-any.whl
| Download URL | vose_alias_method-1.2.2-py3-none-any.whl |
|---|---|
| Size | 13.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
edcf6ad8bc469c877bc5e031b544c6698d08bd1da693227ab92fb7110c43a59a
|
|
BLAKE2b-256 checksum How to use checksums |
dcae5b91e98dd04bbd5bdc0b470095874d7a62d6822bf753f32da6f18289b86c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 14, 2026.
Transparency log