Skip to content

Instantly share code, notes, and snippets.

@shibacow
Created August 2, 2015 08:56
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 shibacow/8716f614b802d5104cdd to your computer and use it in GitHub Desktop.
Save shibacow/8716f614b802d5104cdd to your computer and use it in GitHub Desktop.
gini coefficient by python
#!/usr/bin/python
# -*- coding:utf-8 -*-
import numpy as np
import doctest
def gini_coefficient(src):
"""
>>> gini_coefficient([245,362,826])
0.40544312630844381
>>> gini_coefficient([2,10,5,6,12])
0.35714285714285715
"""
out = []
for i in range(0,len(src)):
for j in range(i+1,len(src)):
out.append(abs(src[i]-src[j]))
avdiff = np.mean(out)
mn = np.mean(src)
return avdiff / (2*mn)
def main():
doctest.testmod()
if __name__=='__main__':main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment