Skip to main content
 首页 » 编程设计

python之使用Numpy计算基尼系数

2025年05月04日14over140

我正在尝试编写一些代码来计算一个假国家的基尼系数。我想出了以下内容:

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 ...)。我错过了什么?

请您参考如下方法:

第一个问题是没有正确分解基尼系数的方程:

gini = (area between Lorenz curve and perfect equality) / (area under perfect equality)

计算中没有包含 in 的分母,并且还使用了不正确的等式线下面积方程(参见使用 np.linspace 和 np.trapz 的方法的代码)。

还有一个问题是缺少洛伦兹曲线的第一个点(它需要从 0 开始,而不是第一个五分位数的份额)。虽然洛伦兹曲线下面积在0到第一个五分位数之间很小,但与等线下面积延长后的面积之比却相当大。

以下提供了对方法 given in the answers to this question 的等效回答:

import numpy as np 
     
GDP = 653200000000 # this value isn't actually needed 
     
# Decile percents of global GDP 
gdp_decile_percents = [0.49, 0.59, 0.69, 0.79, 1.89, 2.55, 5.0, 10.0, 18.0, 60.0] 
print('Percents sum to 100:', sum(gdp_decile_percents) == 100) 
     
gdp_decile_shares = [i/100 for i in gdp_decile_percents] 
     
# Convert to quintile shares of total GDP 
gdp_quintile_shares = [(gdp_decile_shares[i] + gdp_decile_shares[i+1]) for i in range(0, len(gdp_decile_shares), 2)] 
     
# Insert 0 for the first value in the Lorenz curve 
gdp_quintile_shares.insert(0, 0) 
     
# Cumulative sum of shares (Lorenz curve values) 
shares_cumsum = np.cumsum(a=gdp_quintile_shares, axis=None) 
     
# Perfect equality line 
pe_line = np.linspace(start=0.0, stop=1.0, num=len(shares_cumsum)) 
 
area_under_lorenz = np.trapz(y=shares_cumsum, dx=1/len(shares_cumsum)) 
area_under_pe = np.trapz(y=pe_line, dx=1/len(shares_cumsum)) 
     
gini = (area_under_pe - area_under_lorenz) / area_under_pe 
     
print('Gini coefficient:', gini) 

用 np.trapz 计算的面积系数为 0.67。不使用 Lorenz 曲线的第一点并使用 trapz 计算的值为 0.59。我们对全局不平等的计算现在大致等于上面链接的问题中的方法提供的计算(您不需要向这些方法中的列表/数组添加 0)。请注意,使用 scipy.integrate.simps给出 0.69,这意味着另一个问题中的方法更符合梯形而不是辛普森积分。

这是绘图,其中包括 plt.fill_between 以在洛伦兹曲线下着色:

from matplotlib import pyplot as plt 
 
plt.plot(pe_line, shares_cumsum, label='lorenz_curve') 
plt.plot(pe_line, pe_line, label='perfect_equality') 
plt.fill_between(pe_line, shares_cumsum) 
plt.title('Gini: {}'.format(gini), fontsize=20) 
plt.ylabel('Cummulative Share of Global GDP', fontsize=15) 
plt.xlabel('Income Quintiles (Lowest to Highest)', fontsize=15) 
plt.legend() 
plt.tight_layout() 
plt.show()