Code Monkey home page Code Monkey logo

vose-alias-method's Introduction

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 )

Generating 10 random samples:

T
T
T
T
H
T
T
T
T
H

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

(equally $ python vose_sampler/vose_sampler.py -p data/Alice.txt -n 10)

[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/*

vose-alias-method's People

Contributors

asmith26 avatar robertodr avatar psafont avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.