Skip to content

Instantly share code, notes, and snippets.

Created January 17, 2011 17:28
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save audy/783125 to your computer and use it in GitHub Desktop.
Shannon Diversity Index
#!/usr/bin/env python
# Shannon Diversity Index
import sys
def sdi(data):
""" Given a hash { 'species': count } , returns the SDI
>>> sdi({'a': 10, 'b': 20, 'c': 30,})
from math import log as ln
def p(n, N):
""" Relative abundance """
if n is 0:
return 0
return (float(n)/N) * ln(float(n)/N)
N = sum(data.values())
return -sum(p(n, N) for n in data.values() if n is not 0)
if __name__ == '__main__':
import doctest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment