Created
August 2, 2015 08:56
-
-
Save shibacow/8716f614b802d5104cdd to your computer and use it in GitHub Desktop.
gini coefficient by 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
#!/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