Skip to main content

dit is a Python package for information theory on discrete random variables.

Project description

dit is a Python package for information theory.

Continuous Integration Status Continuous Integration Status (windows) Test Coverage Status Code Health Documentation Status Requirements Status DOI Join the Chat Say Thanks!

Documentation:

http://docs.dit.io

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:

https://github.com/dit/dit

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dit-1.0.0.dev10.tar.gz (224.9 kB view details)

Uploaded Source

Built Distributions

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

dit-1.0.0.dev10-py2.py3-none-any.whl (2.0 MB view details)

Uploaded Python 2Python 3

dit-1.0.0.dev10-cp36-cp36m-macosx_10_12_x86_64.whl (390.1 kB view details)

Uploaded CPython 3.6mmacOS 10.12+ x86-64

File details

Details for the file dit-1.0.0.dev10.tar.gz.

File metadata

  • Download URL: dit-1.0.0.dev10.tar.gz
  • Upload date:
  • Size: 224.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for dit-1.0.0.dev10.tar.gz
Algorithm Hash digest
SHA256 40d0e3af8ca4155c3760e9f47a3b01cf0ef31acf09ee0131972e6b8c4ee24be6
MD5 a721f86248a27058aa8f8dbfd30622ce
BLAKE2b-256 d57a361e38d6379033f04b8cb35941e8d6bc253b2b62f07ab81922f3bc7a58b5

See more details on using hashes here.

File details

Details for the file dit-1.0.0.dev10-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for dit-1.0.0.dev10-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 5750c60661aceb60bfe21f73a3151d6aa34bae7bbc235e6d3a3c9e2d8acc7626
MD5 7b12fe39fae467c530853881cb9e71f2
BLAKE2b-256 809f5241acd0ae47fdce84d45c4e30814e45234da3890f41b7904ce2e8d9848d

See more details on using hashes here.

File details

Details for the file dit-1.0.0.dev10-cp36-cp36m-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for dit-1.0.0.dev10-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 c4f50e5c517912514e77df2b3ae476f650f4964f038363222ce9fb113dbee019
MD5 5d528b3f75d1d7e475fc2af3196ce28a
BLAKE2b-256 f81aaef7496bc111a3909a578a02e3c55f3f23aa5097b99df92702a9da847184

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