Skip to content

Instantly share code, notes, and snippets.

@vsbuffalo
Created September 26, 2013 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vsbuffalo/6718031 to your computer and use it in GitHub Desktop.
Save vsbuffalo/6718031 to your computer and use it in GitHub Desktop.
Vince's version of entropy in Python
"""
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