【问题标题】:Gini coefficient calculation using NumpyPython - 使用 Numpy 计算基尼系数
【发布时间】:2023-04-02 19:50:01
【问题描述】:

我是一个新手,首先,刚开始学习 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 ...)。我错过了什么?有人能指出我正确的方向吗?

谢谢!

【问题讨论】:

  • 打印 area_perfect 和 area_lorenz 的值并进行调试。它们是整数还是浮点数?
  • 为什么你有 dx=5?我认为 dx 应该是 0.2,所以 x 的总距离是 1.0。
  • 其实安娜,你是对的!谢谢!我设法找到了数学中的其他一些错误(休息后,哈哈),显然已经纠正了。我仍然不明白这将如何影响实际问题。无论如何,现在这不是问题!
  • @Anna,@stardust,这是不对的。将 dx 更改为 0.2 使 Gini 等于 175763056000.0。真正的问题在于area_perfect 的定义。请在下面查看我的答案。

标签:
python
numpy
economics