Created
September 26, 2013 17:57
-
-
Save vsbuffalo/6718031 to your computer and use it in GitHub Desktop.
Vince's version of entropy in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
entropy.py | |
Calculate entropy of a given list. | |
""" | |
from math import log, log10 | |
from collections import Counter | |
import pdb | |
def entropy(x, logfun=lambda x: log(x, 2)): | |
"""Calculate the entropy for a list of objects `x`, using the log | |
function `logfun`. | |
""" | |
counts = Counter() | |
total = 0.0 | |
for item in x: | |
total += 1 | |
counts[item] += 1 | |
probs = [counts[k]/total for k in counts] | |
ent = -sum([p*logfun(p) for p in probs]) | |
return ent | |
if __name__ == "__main__": | |
seq_1 = "GA GA GA GA GT GA GA GA GA GT CG CG GA GA".split() | |
seq_2 = "GA CG TG AG GC AG GT GC AT TA CC AA TT GG".split() | |
# simple unit tests | |
EPS = 1e-6 | |
assert(entropy(seq_1) - 1.97638209746 < EPS) | |
assert(entropy(seq_2) - 3.52164063634 < EPS) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment