Skip to content

Instantly share code, notes, and snippets.

@martinjc
Created May 7, 2014 09:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save martinjc/f227b447791df8c90568 to your computer and use it in GitHub Desktop.
Save martinjc/f227b447791df8c90568 to your computer and use it in GitHub Desktop.
Simpson Diversity Index
#!/usr/bin/env python
# Simpson Diversity Index
# http://en.wikipedia.org/wiki/Diversity_index
# modified from Shannon Diversity Index implementation by audy
# https://gist.github.com/audy/783125
# https://gist.github.com/audy
def simpson_di(data):
""" Given a hash { 'species': count } , returns the Simpson Diversity Index
>>> simpson_di({'a': 10, 'b': 20, 'c': 30,})
0.3888888888888889
"""
def p(n, N):
""" Relative abundance """
if n is 0:
return 0
else:
return float(n)/N
N = sum(data.values())
return sum(p(n, N)**2 for n in data.values() if n is not 0)
def inverse_simpson_di(data):
""" Given a hash { 'species': count } , returns the inverse Simpson Diversity Index
>>> inverse_simpson_di({'a': 10, 'b': 20, 'c': 30,})
2.571428571428571
"""
return float(1)/simpson_di(data)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment