dit is a Python package for information theory on discrete random variables.
Project description
dit is a Python package for information theory.
- Documentation:
- Downloads:
Coming soon.
- Dependencies:
Python 2.7, 3.3, 3.4, 3.5, or 3.6
numpy
iterutils
six
contextlib2
prettytable
networkx
- Optional Dependencies:
cython
cvxopt
numdifftools
scipy
- Note:
The cython extensions are currently not supported on windows. Please install using the --nocython option.
- Install:
Until dit is available on PyPI, the easiest way to install is:
pip install git+https://github.com/dit/dit/#egg=dit
Alternatively, you can clone this repository, move into the newly created dit directory, and then install the package. Be sure to include the period (.) in the install command:
git clone https://github.com/dit/dit.git cd dit pip install .
- Mailing list:
None
- Code and bug tracker:
- License:
BSD 2-Clause, see LICENSE.txt for details.
Quickstart
The basic usage of dit corresponds to creating distributions, modifying them if need be, and then computing properties of those distributions. First, we import:
>>> import dit
Suppose we have a really thick coin, one so thick that there is a reasonable chance of it landing on its edge. Here is how we might represent the coin in dit.
>>> d = dit.Distribution(['H', 'T', 'E'], [.4, .4, .2])
>>> print d
Class: Distribution
Alphabet: ('E', 'H', 'T') for all rvs
Base: linear
Outcome Class: str
Outcome Length: 1
RV Names: None
x p(x)
E 0.2
H 0.4
T 0.4
Calculate the probability of H and also of the combination H or T.
>>> d['H']
0.4
>>> d.event_probability(['H','T'])
0.8
Calculate the Shannon entropy and extropy of the joint distribution.
>>> dit.shannon.entropy(d)
1.5219280948873621
>>> dit.other.extropy(d)
1.1419011889093373
Create a distribution where Z = xor(X, Y).
>>> import dit.example_dists
>>> d = dit.example_dists.Xor()
>>> d.set_rv_names(['X', 'Y', 'Z'])
>>> print d
Class: Distribution
Alphabet: ('0', '1') for all rvs
Base: linear
Outcome Class: str
Outcome Length: 3
RV Names: ('X', 'Y', 'Z')
x p(x)
000 0.25
011 0.25
101 0.25
110 0.25
Calculate the Shannon mutual informations I[X:Z], I[Y:Z], and I[X,Y:Z].
>>> dit.shannon.mutual_information(d, ['X'], ['Z'])
0.0
>>> dit.shannon.mutual_information(d, ['Y'], ['Z'])
0.0
>>> dit.shannon.mutual_information(d, ['X', 'Y'], ['Z'])
1.0
Calculate the marginal distribution P(X,Z). Then print its probabilities as fractions, showing the mask.
>>> d2 = d.marginal(['X', 'Z'])
>>> print d2.to_string(show_mask=True, exact=True)
Class: Distribution
Alphabet: ('0', '1') for all rvs
Base: linear
Outcome Class: str
Outcome Length: 2 (mask: 3)
RV Names: ('X', 'Z')
x p(x)
0*0 1/4
0*1 1/4
1*0 1/4
1*1 1/4
Convert the distribution probabilities to log (base 3.5) probabilities, and access its probability mass function.
>>> d2.set_base(3.5)
>>> d2.pmf
array([-1.10658951, -1.10658951, -1.10658951, -1.10658951])
Draw 5 random samples from this distribution.
>>> dit.math.prng.seed(1)
>>> d2.rand(5)
['01', '10', '00', '01', '00']
Enjoy!
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 Distributions
Built Distributions
Hashes for dit-1.0.0.dev1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 63ae0c56c2d60528a52c57a09e06dc00603d5828b7e91a77127ca07ac78361a2 |
|
MD5 | 26f84ea1e9a28e86f4c3260d56ae644f |
|
BLAKE2b-256 | 506d0fc317db06ebce0c4bdaa35527ccec7924b8762ba6ca8a742e91f3ff1e37 |
Hashes for dit-1.0.0.dev1-cp36-cp36m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa13a443aa47a5fcc29e9216e9a8a40a757e7b76ad9a2b3b5f95d137798278b0 |
|
MD5 | 49714eab6a3620afc71bd5074dcce40e |
|
BLAKE2b-256 | 3befc782a51027a84c5336b132909483fa599a5dfc1a20d61821a27854950c97 |
Hashes for dit-1.0.0.dev1-cp27-cp27m-macosx_10_11_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23b93414571831baf0b288e10a170d441adb96b8bb0b44b4c91a3dc87595e3c0 |
|
MD5 | 0280135c0ab7db6055b6f79bf9489729 |
|
BLAKE2b-256 | 8996252d869ed0c69c9dc39cb484d465e726c9a936e36eb3a1a3f43612697b51 |