Python package for information theory.
Project description
dit is a Python package for information theory.
- Documentation:
- Downloads:
- Dependencies:
Python 2.7, 3.3, 3.4, 3.5, or 3.6
boltons
contextlib2
debtcollector
networkx
numpy
prettytable
scipy
six
- Optional Dependencies:
colorama
cython
numdifftools
- Note:
The cython extensions are currently not supported on windows. Please install using the --nocython option.
- Install:
The easiest way to install is:
pip install dit
Alternatively, you can clone this repository, move into the newly created dit directory, and then install the package:
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 Distribution
Built Distributions
Hashes for dit-1.0.0.dev20-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 965ae48e258644604eb52fbf6afb39a9abfb867aa47e7d591b1ad5959c3f8cf8 |
|
MD5 | 684c0c35b9be6598ec4ce467b85988e2 |
|
BLAKE2b-256 | c4c05f363c33e7fd384140c8fb81a56f6b3b61541aa71c83076ad67a2d8d9c8b |
Hashes for dit-1.0.0.dev20-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b3edecc2390a3e090c20ed183549f84d5062e6b8506f4e57fed3f1f9f941931 |
|
MD5 | ab99485dc6f1377bafc6f4a5907804ab |
|
BLAKE2b-256 | 2646164c7e945815881973a78ff671353a5469ae151c3b7d6f11fe6fe9a530f3 |
Hashes for dit-1.0.0.dev20-cp27-cp27m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee5297704cfc0f22ef4b378129b208701d2f697afac3b8ac1631c0bf300a1a2f |
|
MD5 | 0eb65987b761f6185d13ae89a6d7cb3b |
|
BLAKE2b-256 | 2a59910d5f4635666eda36ef6b377766868ceb72788f12a3f678fa6d4178d761 |