我是一个新手,首先,刚开始学习Python,我正在尝试编写一些代码来计算一个假国家的基尼指数。我想出了以下几点:
GDP = (653200000000)
A = (0.49 * GDP) / 100 # Poorest 10%
B = (0.59 * GDP) / 100
C = (0.69 * GDP) / 100
D = (0.79 * GDP) / 100
E = (1.89 * GDP) / 100
F = (2.55 * GDP) / 100
G = (5.0 * GDP) / 100
H = (10.0 * GDP) / 100
I = (18.0 * GDP) / 100
J = (60.0 * GDP) / 100 # Richest 10%
# Divide into quintiles and total income within each quintile
Q1 = float(A + B) # lowest quintile
Q2 = float(C + D) # second quintile
Q3 = float(E + F) # third quintile
Q4 = float(G + H) # fourth quintile
Q5 = float(I + J) # fifth quintile
# Calculate the percent of total income in each quintile
T1 = float((100 * Q1) / GDP) / 100
T2 = float((100 * Q2) / GDP) / 100
T3 = float((100 * Q3) / GDP) / 100
T4 = float((100 * Q4) / GDP) / 100
T5 = float((100 * Q5) / GDP) / 100
TR = float(T1 + T2 + T3 + T4 + T5)
# Calculate the cumulative percentage of household income
H1 = float(T1)
H2 = float(T1+T2)
H3 = float(T1+T2+T3)
H4 = float(T1+T2+T3+T4)
H5 = float(T1+T2+T3+T4+T5)
# Magic! Using numpy to calculate area under Lorenz curve.
# Problem might be here?
import numpy as np
from numpy import trapz
# The y values. Cumulative percentage of incomes
y = np.array([Q1,Q2,Q3,Q4,Q5])
# Compute the area using the composite trapezoidal rule.
area_lorenz = trapz(y, dx=5)
# Calculate the area below the perfect equality line.
area_perfect = (Q5 * H5) / 2
# Seems to work fine until here.
# Manually calculated Gini using the values given for the areas above
# turns out at .58 which seems reasonable?
Gini = area_perfect - area_lorenz
# Prints utter nonsense.
print Gini
Gini = area_perfect - area_lorenz
只是没有意义的结果。我已经取出了区域变量给出的值并手动进行了数学计算,结果还不错,但是当我尝试让程序去做时,它给了我一个完全???值(-1.7198 ...)。我错过了什么?有人可以指出我正确的方向吗?
谢谢!