entropy ├── __init__.py ├── entropy.py └── tests ├── __init__.py └── test_entropy.py 3 directories, 8 files -------------------------- entropy ├── __init__.py from .entropy import entropy -------------------------- entropy ├── entropy.py """ Compute the entropy in bits of a list of probabilities. """ import numpy as np def entropy(ps): """ Compute the entropy in bits of a list of probabilities. The input list of probabilities must sum to one and no element should be larger than 1 or less than 0. :param list ps: list of probabilities :type ps: list """ if any([(p < 0.0) or (p > 1.0) for p in ps]): raise ValueError("At least one input is out of range [0...1]") else: pass if not np.isclose(1, np.sum(ps), atol=1e-08): raise ValueError("The list of input probabilities does not sum to 1") else: pass items = ps * np.log2(ps) new_items = [] for item in items: if np.isnan(item): new_items.append(0) else: new_items.append(item) return np.abs(-np.sum(new_items)) -------------------------- entropy └── tests └── test_entropy.py """ Tests for the entropy function """ import numpy as np import unittest from entropy import entropy class TestEntropy(unittest.TestCase): def test_smoke(self): """ Simple smoke test to make sure function runs. """ entropy([1]) return def test_args_dont_sum_to_1(self): """ Edge test to make sure the function throws a ValueError when the input probabilities do not sum to one. """ with self.assertRaises(ValueError): entropy([.9, .9]) return def test_args_out_of_range(self): """ Edge tst to make sure the function throws a ValueError when the input probabilities are < 0 or > 1. """ with self.assertRaises(ValueError): entropy([-1, 2]) return def test_four_equal_likelihood_states(self): """ One shot test using the known case of four states with equal likelihood of occurrence. Should return 2 bits. """ assert np.isclose(entropy([0.25, 0.25, 0.25, 0.25]), 2.) return def test_equal_probability(self): """ Pattern test using the known relationship of equal probabilities and predefined result. """ def test(count): prob = 1.0/n ps = np.repeat(prob, n) assert np.isclose(entropy(ps), -np.log2(prob)) return # run the test for a large number of iterations for n in range(10, 100000, 10000): test(n) return ------------------------ python -m unittest discover -s entropy