You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#!/usr/bin/python2'''The following script explains how to compute inequality indices.First some formulas from Amartya Sen's and James E. Foster's book"On Economic Inequality" (1973/1997) are implemented:(1) Entropy of expected information content (2.11 on pg.35, A. Sen)(2) Theil's measure (redundancy of the expected information content) (2.12 on pg.35, A. Sen)(3) Atkinson family (Chapter A.2.2, pg.128, J. E. Foster)(4) Generalized entropy class (Chapter A.4.1, pg.140, J. E. Foster)Then further inequality computation routines are shown:(5) Redundancy R (R[1] = Theil[1])(6) Gini-Hoover index for societies devided into two a-fractiles: Gini index and Hoover index computed from Theil index(7) Gini Index,Hoover Index, three versions of the Theil[1] redundancy (without any inequality aversion parameters) and the Lorenz curve. This routine is wrapped into the class GiniHooverTheil(), where I may add some functionality later.The last chapter allows you to run experiments with the previous function andthe GiniHooverTheil() class. You should delete that section (or at least anycode which runs experiments) in case you want to use this script in your ownapplication script. Such a script simply would start with two lines:#!/usr/bin/pythonfrom onOEI import *The first line is recommended for UNIX like OS environments and won't do anyharm in other environments. "onOEI" may have to be replaced by the name of theversion you are using. At the end of the listing in the "run demo" section,calls which still are active, may have to be turned into comments.(8) For your experimentsG.Kluge, Munich, 2008-11-17'''################################################################################# Preparation of some tools and constants ################################################################################### NON-PROGRAMMERS DON'T NEED TO READ THIS SECTION##------------------------------------------------------------------------------# required functionsfrommathimporte, exp, fabs, modf, logaslnfrompprintimportpprint#--- constants ----------------------------------------------------------------# constantsinfinity=9e999ln2=ln(2)
binary=1/ln2natural=1# 1/log(2.71828182846)decimal=1/ln(10)
#--- functions ----------------------------------------------------------------# logarithm with configurable basedeflog(x,*logtype):
try:
basefactor=logtype[0]
except:
basefactor=1returnln(x) *basefactor'''application example:print log (16,(binary)) # yields 4.0print log(16) # yields 2.77258872224print log (16,(decimal)) # yields 1.20411998266#'''# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -deferror_if_not_in_range01(value):
if (value<=0) or (value>1): # check rangeraiseException, \
str(value) +' is not in [0,1)!'deferror_if_not_1(value):
if (value!=1):
# not 100%raiseException, 'Sum '+str(value) +' is not 1!'# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -defsum_of_table(x):
sum=0.0forx_iinx:
error_if_not_in_range01(x_i)
sum+=x_ierror_if_not_1(sum)
returnsum#--- remarks ------------------------------------------------------------------# Differences to OIE:# - My n starts from 0: n = n_OIE - 1# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -# Other remarks:# - In programming, a more elegant syntax and use of structures offered by# the Python language has been sacrified to understandability, as I want to# make this script also understandable to readerw who do not know python.################################################################################# Some formulas from ON ECOOMIC INEQUALITY in Python ################################################################################## ===== 2.11 on pg.35 (A. Sen) ================================================# (1) Entropy of expected information content# =============================================================================# (Base of logarithm is configurable. Natural logarithm is default.)# (x is a single column table.)defH(x): # natural logarithm is defaultn=len(x)
entropy=0.0# initiate entropysum=0.0forx_iinx: # work on all x[i]error_if_not_in_range01(x_i)
sum+=x_i# add up probabilities for testgroup_negentropy=x_i*log(x_i)
entropy+=group_negentropy# add group negentropies to entropy# remember: ln(1/x) = -ln(x)error_if_not_1(sum)
return-entropy# turn negentropy into entropy#===== 2.12 on pg.35 (A. Sen) =================================================# (2) Theil's measure (redundancy of the expected information content)#==============================================================================# (x is a single column table.)defT(x): # natural logarithm is defaultn=len(x)
maximum_entropy=log(n)
actual_entropy=H(x)
redundancy=maximum_entropy-actual_entropyinequality=1-exp(-redundancy)
returnredundancy,inequality,'Theil\'s measure T[1]'#===== Chapter A.2.2, pg.128 (J. E. Foster) ===================================# (3) Atkinson family#==============================================================================defatkinson_family(x,epsilon):
should_be_1=sum_of_table(x) # checks tablen_x=len(x)
u_x=1.0/n_x# mean group income (here Foster used u, I use u_x)if (epsilon==0):
message='epsilon = 0'product=1.0forx_iinx: # go through listproduct*=x_i/u_xequality=product** (1.0/n_x)
elif (epsilon<=1):
message='epsilon = '+str(epsilon) +' <=1 and not 0'sum=0.0forx_iinx: # go through listsum+= (x_i/u_x) **epsilonequality= (sum/n_x) ** (1.0/epsilon)
else:
# error: epsilon is >1raiseException, \
'epsilon = ' \
+str(epsilon) +' is out of range!'inequality=1-equality# from Atkinson familyredundancy=-ln(equality) # Klugereturnredundancy,inequality,'Atkinson family with '+message# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -defatkinson_family_demo(group_income_pairs,epsilons):
printout= []
forepsiloninepsilons:
printout.append(atkinson_family(group_income_pairs,epsilon))
returnprintout#print atkinson_family_demo((0.01,0.09,0.2,0.3,0.4),(0.25,1))# You will find such "demo" functions also in the calculation functions below.# You can delete them or keep them. I used them for first tests and just left# them in the code in order to give examples how to call the calculation# functions.#===== Chapter A.4.1, pg.140 (J. E. Foster) ===================================# (4) Generalized entropy class#==============================================================================defgeneralized_entropy(x,alpha):
should_be_1=sum_of_table(x) # checks tablen_x=len(x) # (here Foster used n, I use n_x)u_x=1.0/n_x# mean group income (here Foster used u, I use u_x)if (alpha==0):
message='Theil I[0](x) is the \"second\" Theil measure'sum=0.0forx_iinx: # go through listsum+=ln(u_x/x_i)
redundancy=sum/n_xelif (alpha==1):
message='Theil I[1](x) is the \"first\" Theil measure'sum=0.0forx_iinx: # go through listsum+= (x_i/u_x) *ln(x_i/u_x)
redundancy=sum/n_xelse:
message='Theil I['+str(alpha) \
+'](x) from generalized entropy class'sum=0.0forx_iinx: # go through listsum+=1- ((x_i/u_x) **alpha)
redundancy=sum/alpha/(1-alpha)/n_xinequality=1-exp(-redundancy)
returnredundancy,inequality,message# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -defgeneralized_entropy_demo(group_incomes,alphas):
printout= []
foralphainalphas:
printout.append(generalized_entropy(group_incomes,alpha))
returnprintout#print generalized_entropy_demo((0.01,0.09,0.2,0.3,0.4),(0.25,1,2))################################################################################# Other inequality computation tools ##################################################################################===== (Kluge) ================================================================# (5) Redundancy R#==============================================================================defR(x,beta):
should_be_1=sum_of_table(x) # checks tablen_x=len(x) # (here Foster used n, I use n_x)u_x=1.0/n_x# mean group income (here Foster used u, I use u_x)sum=0.0forx_iinx: # go through listsum+= (x_i*ln(x_i/u_x))
if (beta==1):
redundancy=summessage='R[1](x) = T[1](x)'inequality=1-exp(-redundancy)
else:
if (beta>0):
inequality= (1-exp(-sum)) **betaredundancy=-ln(1-inequality)
message='R['+str(beta) +'](x)'else:
# beta is 0inequality=1redundancy=infinitymessage='R[0](x)'returnredundancy,inequality,message# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -defR_demo(group_incomes,betas):
printout= []
forbetainbetas:
printout.append(R(group_incomes,beta))
returnprintout#print R_demo((0.01,0.09,0.2,0.3,0.4),(0.25,1,2))#===== (Kluge) ================================================================# (6) Gini-Hoover index: Gini index and Hoover index computed from Theil index#==============================================================================# For societies which are devided into two a-fractiles, the Gini index and the# Hoover index are equal. They can be computed from the Theil index.#------------------------------------------------------------------------------# ----- required libraries -----# from math import exp, e, fabs, log as ln # has been done previously alreadyfrommathimportsqrt, tanh# ----- constants required by this function -----ineqTheilAt1=0.647918229# ghFromTheil(1) = 0.647918229oneMinusiTA1=1-ineqTheilAt1approxfactor=-4.63# in ghApproximation are two terms:# - tanh(theil/2.0)*ineqTheilAt1 is "squeezed" 0.5 times on the x scale # - (1-exp(approxfactor*theil))*oneMinusiTA1 is "squeezed"-4.63# times on the x scale.# ----- general functions ----asinh=lambdax: ln(sqrt(x*x+1.0)+x)
atanh=lambdax: ln((1.0+x)/(1.0-x))/2.0atanh2=lambdax: ln((1.0+x)/(1.0-x))
# ----- special functions -----# Theil index computed from GiniHoover indextheilFromGh=lambdagh: atanh2(gh)*gh# atanh2(gh)*gh# GiniHoover index approximated from Theil indexghApproximation=lambdatheil: tanh(theil/2.0)*ineqTheilAt1 \
+(1-exp(approxfactor*theil))*oneMinusiTA1# new GiniHoover approximated from Theil and old GiniHooverghRecursive=lambdatheil,gh: tanh(theil/gh/2)
defghFromTheil(theil):
# ----- computation -----theil=float(theil)
if (theil<=0.0): return0.0,theil# theil = 0, error = theiltheil=min(theil,16) # all Theil indices > 16 are almost 1gh_0=ghApproximation(theil) # start value for GiniHoover indext_0=theilFromGh(gh_0) # start value for Theil indexgh_1=ghRecursive(theil,gh_0) # first try for GiniHoover indexforloopCountinxrange(1000): # limit loop count (for safety)t_1=theilFromGh(gh_1) # first and next tries for Theil indexiffabs(theil-t_1) <0.000001: break# loop exittry: slope= (gh_1-gh_0)/(t_1-t_0) # for interpolationexceptZeroDivisionError: break# annother loop exitdelta= ((t_1+t_0)*slope-gh_1-gh_0)/2.0# for interpolationgh_0,t_0=gh_1,t_1# memorize new valuesgh_1=max(0,theil*slope-delta) # 1st & next tries f. GiniHoover#print '2: gh0=%f, t0=%f, gh1=%f, t1=%f' % (gh_0,t_0,gh_1,t_1) # testreturngh_1,theil-t_1# return with GiniHoover and Theil deviation# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -defghFromTheil_demo():
fortheilinxrange(0,50):
gh,errTheil=ghFromTheil(theil/10.0)
print'gh=%f, t=%f, err=%f'% (gh,theil,errTheil) ### test#ghFromTheil_demo()#===== (Kluge) ================================================================# (7) Gini index, Hoover index, three versions of Theil[1] index, Lorenz curve#==============================================================================defthreeInequalities(x_pairs):
table= []
theiltable= []
rownumber=-1societysize=0.0societyincome=0.0theiltableerror=0grouptableItoGerror=0grouptableGtoIerror=0grouptableSerror=0forgroupdata_iinx_pairs:
groupsize_i,groupincome_i=groupdata_i[0],groupdata_i[1]
# get optional group theil indices (if available) #Ttry: theiltable.append(groupdata_i[2]) #TexceptIndexError: theiltableerror=1#T# get basic group datasocietysize+=groupsize_isocietyincome+=groupincome_ipercapitaincome_i=groupincome_i/float(groupsize_i)
row_i= (percapitaincome_i, \
groupsize_i, \
groupincome_i)
table.append(row_i)
table.sort() # required for Lorenz curve and Gini onlytheiltable=list(theiltable)
lorenzcurve= [(0.0,0.0)]
lorenzsize_previous=0.0lorenzincome_previous=0.0median=0.0groupsize_previous=0.0groupincome_previous=0.0tablelength=len(table)
oddlength=tablelength%2sum_for_gini=0.0sum_for_hoover=0.0sum_for_theilItoG=0.0sum_for_theilGtoI=0.0sum_for_theilS=0.0forpercapitaincome_i,groupsize_i,groupincome_iintable:
rownumber+=1lorenzsize_i=lorenzsize_previous+groupsize_ilorenzsize_previous=lorenzsize_ilorenzincome_i=lorenzincome_previous+groupincome_ilorenzincome_previous=lorenzincome_ilorenzcurve.append((lorenzsize_i,lorenzincome_i))
if (median==0):
if (tablelength>2):
if (lorenzsize_i>societysize/2.0):
medianinfo=str(rownumber+1) +'/'+str(tablelength)
ifoddlength: # odd table lengthmedian=percapitaincome_ielse: # even table lengthmedian= (groupincome_previous+groupincome_i) \
/float(groupsize_previous+groupsize_i)
medianinfo=str(rownumber) +'/'+str(tablelength)\
+'+'+medianinfogroupsize_previous=groupsize_igroupincome_previous=groupincome_ielse:
median=societyincome/societysizemedianinfo='irrelevant'deviation_i=groupincome_i/float(societyincome) \
-groupsize_i/float(societysize)
gini_i= (lorenzincome_i*2.0-groupincome_i) *groupsize_ihoover_i=fabs(deviation_i)
theilItoG_i=ln(percapitaincome_i) *groupsize_itheilGtoI_i=-ln(percapitaincome_i) *groupincome_itheilS_i=ln(percapitaincome_i) *deviation_isum_for_gini+=gini_isum_for_hoover+=hoover_isum_for_theilItoG+=theilItoG_isum_for_theilGtoI+=theilGtoI_isum_for_theilS+=theilS_itry: sum_for_theilItoG-=theiltable[rownumber][0] #TexceptIndexError: grouptableItoGerror=1#Ttry: sum_for_theilGtoI-=theiltable[rownumber][1] #TexceptIndexError: grouptableGtoIerror=1#Ttry: sum_for_theilS+=theiltable[rownumber][2] #TexceptIndexError: grouptableSerror=1#Tpercapitaincome=societyincome/float(societysize)
gini=1-float(sum_for_gini)/societysize/societyincomehoover=sum_for_hoover/2.0# Redundancy = maximum entropy minus effective entropytheilItiG=ln(percapitaincome) -sum_for_theilItoG/float(societysize)
theilGtiI=-ln(percapitaincome) -sum_for_theilGtoI/float(societyincome)
ifgrouptableSerror: theilS= (theilItiG+theilGtiI)/2.0#Telse: theilS=sum_for_theilS/2.0#Tresults= (societysize, \
societyincome, \
percapitaincome, \
medianinfo, \
median, \
gini*100, \
hoover*100, \
theilItiG, \
theilGtiI, \
theilS)
format='Size of society: %.3f\n'+ \
'Income of society: %.3f\n'+ \
'Per capita income: %.3f\n'+ \
'Per capita median (row %s): %.3f\n'+ \
'Gini: %.1f%%'+'\n'+ \
'Hoover: %.1f%%'+'\n'+ \
'Theil[1] income2groups: %.3f\n'+ \
'Theil[1] groups2income: %.3f\n'+ \
'Theil[1] symmetrized: %.3f'message=format%resultsif (theiltableerror==0): #Tmessage=message+'\n(Theil table status: %d%d%d)' \
% (1-grouptableItoGerror,1-grouptableGtoIerror,1-grouptableSerror) #Treturnresults, \
message, \
table, \
lorenzcurve# Remark: The Theil index (more precise: the Theil redundancy) can be #T# computed based on a society dataset. That set is a table containing #T# (groupsize,groupincome) for each group (each fractile) in the society. #T# That is the standard situation. #T# However, also datasets with per group data like #T# (groupsize,groupincome,(groupTheilItoG,groupTheilGtoI,groupTheilS)) may #T# be available as an option. This code contains elements required to deal #T# which such extended per-group data. This can be done, because the Theil #T# redundancy is "decomposable". (Similar support for the Hoover index has #T# not been implemented yet. That may happen later. The Gini index is not #T# decomposable. #T# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -defthreeInequalities_demo(x_pairs):
results,message,table,lorenzcurve= (threeInequalities(x_pairs))
printprint'Table:'pprint(table)
printprint'Lorenz curve:'pprint(lorenzcurve)
printprintmessage#threeInequalities_demo(((40,47.5),(9,27),(1,23),(50,2.5))) defgroupTheil_demo(): # ---- not well tested yet ---# Experimental: Not yet for use.somedata= ( \
((40,47.5),(9,27),(1,23),(50,2.5)), \
\
((40,47.5,(0.00,0.01,0.02)), \
(9,27,(0.10,0.11,0.12)), \
(1,23,(0.20,0.21,0.22)), \
(50,2.5,(0.30,0.31,0.32))),
\
((40,47.5,(0.00,0.01,0.02)), \
(9,27,(0.10,0.11,0.12)), \
(1,23,(0.20,0.21)), \
(50,2.5,(0.30,0.31,0.32))) )
forsocietyinsomedata:
results,message,table,lorenzcurve= \
(threeInequalities(society))
printmessageprint#===== (Kluge) ================================================================# (7a) Gini index, Hoover index, three versions of Theil[1] index, Lorenz curve#==============================================================================# The function threeInequalities() has been written in a way which does not# burden non-programmers with programming techniques. The following class# implements an inequality object. If you read this code just for understanding# the formulas, you can skip reading the code of class GiniHooverTheil.classGiniHooverTheil:
def__init__(self,x_pairs):
# Instead of gropdata with the structure (groupsize,groupincome) also# a-fractile formats can be processed.try:
a,b=x_pairsdummy=a*bx_pairs= ((a,b),(b,a))
except:
passtry:
a=fabs(float(x_pairs))
s=str(a).lstrip('0')
fordigitin'123456789':
s=s.replace(digit,'0')
b=eval('1'+s) -a# complementx_pairs= ((a,b),(b,a))
except:
passtry:
if (len(x_pairs) ==1):
a,b=x_pairs[0]
x_pairs= ((a,b),(b,a))
except:
pass# compute distribution statisticsself.table= []
try:
results,self.message,table,lorenzcurve= \
threeInequalities(x_pairs)
except:
results= (-1,-1,-1,'error',-1,-1,-1,-1,-1,-1)
self.message='error'table=[]
lorenzcurve=[0]
#-------------------------------------------------# prepare output dataself.societysize, \
self.societyincome, \
self.percapitaincome, \
self.medianinfo, \
self.median, \
self.gini, \
self.hoover, \
self.theilItoG, \
self.theilGtoI, \
self.theilS=results# tablelorenzcurve.reverse()
table.reverse()
dummy=lorenzcurve.pop() #remove (0.0,0.0)whilelen(table):
r0,r1,r2=table.pop()
l0,l1=lorenzcurve.pop()
self.table.append((r0,r1,r2,l0,l1))
# compute Plato index (that's my name for the GiniTheil index)self.plato,errTheil=ghFromTheil(self.theilS)
# display in format like the one for the "Pareto principle"self.aFractile=self.plato/2.0+0.5# outputself.message= \
self.message.replace('Hoover', \
'Plato: %.1f%% (%.0f:%.0f)\nHoover' \
% (self.plato*100,self.aFractile*100,100-self.aFractile*100))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -defginiHooverTheil_demo(x_pairs):
# generate inequality objectincomedistribution=GiniHooverTheil(x_pairs)
# print some data provided by the objectprintincomedistribution.message#giniHooverTheil_demo(((40,47.5),(9,27),(1,23),(50,2.5)))defgermany2004_demo():
# table for net incomes in Germany (2004)# column(1): group size# column(2): per capita income (converted into group income)incomedistribution2004de= \
([(groupsize,groupsize*percapitaincome) \
forgroupsize,percapitaincomein (\
(5043009,653),\
(1789731,3664),\
(1658405,6233),\
(1609017,8726),\
(1436832,11219),\
(1371636,13752),\
(2833254,17510),\
(3059569,22539),\
(3099288,27471),\
(3743779,33517),\
(3866269,43156),\
(4971433,70066),\
(419001,163964),\
(87869,331804),\
(21729,670095),\
(9688,2740910))])
giniHooverTheil_demo(incomedistribution2004de)
#germany2004_demo()'''Size of society: 35020509.000Income of society: 1052552946587.000Per capita income: 30055.330Per capita median (row 7/16+8/16): 20121.070Gini: 51.2%Plato: 53.6% (77:23)Hoover: 36.6%Theil[1] income2groups: 0.737Theil[1] groups2income: 0.547Theil[1] symmetrized: 0.642'''defaFractiles_demo():
# generate inequality objectparetoPrinciple=GiniHooverTheil(0.8)
printparetoPrinciple.messageprintparetoPrinciple=GiniHooverTheil(0.05)
printparetoPrinciple.messageprintparetoPrinciple=GiniHooverTheil(600)
printparetoPrinciple.messageprintparetoPrinciple=GiniHooverTheil((800,200))
printparetoPrinciple.messageprintparetoPrinciple=GiniHooverTheil([(800,200)])
printparetoPrinciple.messageprint#aFractiles_demo()defaFractiles2_demo():
afractiles= (
0.620, # Theil - Hoover at minimum0.731, # Theil = Hoover0.740, # Theil = 0.50.800, # "Pareto principle"0.824, # Theil = 1.00.917, # Theil = 2.00.984) # Theil = 4.0fordatasetinafractiles:
inequalityobject=GiniHooverTheil(dataset)
printinequalityobject.messageprint#aFractiles2_demo()#===== (Kluge) ================================================================# (8) For your experiments#==============================================================================################################################################################# Some data to play with ################################################################################## Below the playfield starts. You can removr it in case you do not want to# run experiments in that area. The code in the playfield is not reqyired# by the calculation functions above this area.# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -# example for some propabilities or sharesx= (0.01,0.09,0.2,0.3,0.4)
sum_x=0.0forx_iinx:
sum_x+=x_i# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -# example for some group incomesgroup_incomes= (20000,40000,60000,80000)
sum_group_incomes=0.0forx_iingroup_incomes:
sum_group_incomes+=x_i# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -# example for some pairs of group size shares and group income sharesx_pairs= []
sum_x=0forx_iinx:
x_pairs.append((1/float(len(x)),x_i))
sum_x+=x_i#pprint(x_pairs)# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -epsilons= (1,0.999,0.5,0.0001,0,-0.0001,-0.5,-0.999,-1,-5,-100)
alphas=epsilons# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -a_fractiles= (
(0.620,0.380), # Theil - Hoover at minimum
(0.731,0.269), # Theil = Hoover
(0.740,0.260), # Theil = 0.5
(0.800,0.200), # "Pareto principle"
(0.824,0.176), # Theil = 1.0
(0.917,0.083), # Theil = 2.0
(0.984,0.016)) # Theil = 4.0################################################################################# Play ##################################################################################------------------------------------------------------------------------------defdemo0(x,alpha,epsilon):
ifalpha==1:
printT(x)
if (epsilon<=1):
printatkinson_family(x,epsilon)
printgeneralized_entropy(x,alpha)
printR(x,alpha)
#demo0((0.01,0.29,0.3,0.4),1,1)# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -defdemo1():
print'\n=== H(x) demo ==='printH(x)
print'\n=== T(x) demo ==='printT(x)
print'\n=== atkinson_family demo with different epsilons ==='forlistinatkinson_family_demo(x,epsilons):
printlistprint'\n=== generalized entropy demo with different alphas ==='forlistingeneralized_entropy_demo(x,alphas):
printlistprint'\n=== straight redundancy demo with different alphas ==='forlistinR_demo(x,alphas):
printlist#demo1()# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -defdemo2():
x= (0.01,0.29,0.3,0.4)
epsilons= (2,1.5,1,0.5,0,-0.5,-1,-1.5,-2,-4)
forepsiloninepsilons:
printalpha=epsilondemo0(x,alpha,epsilon)
#demo2()# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#http://www.poorcity.richcity.org/calculator/?quantiles=1,1|1,2|1,4|1,8|1,16defmake_csl0(): # for analysis in spreadsheet#exponent = 1 # Gini 0.465exponent=0.5# Gini 0.264x0= (1,2,4,8,16)
sum=0.0xtest= []
forx_iinx0:
sum+=x_i**exponent#forx_iinx0:
xtest.append(x_i**exponent/sum)
#print T(xtest)#print xtestprint'beta;Atkinson;ineq(Theil);ineq(R)'forbetainxrange(-20,31):
line=''alpha=beta/10.0epsilon=alphaifepsilon<=1:
atkinson=atkinson_family(xtest,epsilon)[1]
line='%f;%f;%f;%f'% \
(alpha, \
atkinson, \
generalized_entropy(xtest,alpha)[1], \
R(xtest,alpha)[1])
printline# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -defdemoThreeInequalities():
threeInequalities_demo(((40,47.5),(9,27),(1,23),(50,2.5)))
#demoThreeInequalities()'''http://www.poorcity.richcity.org/calculator/?quantiles=50,2.5|40,47.5|9,27|1,23100 quantile elements, 4 quantilesMean: 1.000Median: 44.4% 0.556 (#1/4, #2/4) Inequality Welfare1-e^-TheilT: 64.1% 2.786 (1/Welfare)1-e^-TheilL: 72.7% 0.2731-e^-TheilS: 68.7% 0.313Gini: 64.5% 0.355Plato: 68.8% 0.312 Pareto: 844/156100%-SOEP: 78.5% 0.215Hoover: 47.5% 0.525Theil-T Redundancy: 1.025Theil-L Redundancy: 1.299Symmetric Redundancy: 1.162Inequality Issuization: +0.687'''# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -defdemoParetoPrinciple(): #(Old. Now there is direct support fot a-fractiles.)fora,bina_fractiles:
print'----------'threeInequalities_demo(((a,b),(b,a)))
print'----------'#demoParetoPrinciple()#----- run demo ---------------------------------------------------------------##make_csl0()##demoThreeInequalities()##demoParetoPrinciple()germany2004_demo()
##aFractiles_demo()##groupTheil_demo()
source: http://www.poorcity.richcity.org/#macrae
The text was updated successfully, but these errors were encountered: