我正在计算Gini coefficient(类似于:Python - Gini coefficient calculation using Numpy),但得到了一个奇怪的结果。对于从np.random.rand()
采样的均匀分布,基尼系数是0.3,但我本希望它接近0(完全相等)。这里出什么事了?
def G(v):
bins = np.linspace(0., 100., 11)
total = float(np.sum(v))
yvals = []
for b in bins:
bin_vals = v[v <= np.percentile(v, b)]
bin_fraction = (np.sum(bin_vals) / total) * 100.0
yvals.append(bin_fraction)
# perfect equality area
pe_area = np.trapz(bins, x=bins)
# lorenz area
lorenz_area = np.trapz(yvals, x=bins)
gini_val = (pe_area - lorenz_area) / float(pe_area)
return bins, yvals, gini_val
v = np.random.rand(500)
bins, result, gini_val = G(v)
plt.figure()
plt.subplot(2, 1, 1)
plt.plot(bins, result, label="observed")
plt.plot(bins, bins, '--', label="perfect eq.")
plt.xlabel("fraction of population")
plt.ylabel("fraction of wealth")
plt.title("GINI: %.4f" %(gini_val))
plt.legend()
plt.subplot(2, 1, 2)
plt.hist(v, bins=20)
对于给定的一组数字,上面的代码计算每个百分位bin中总分布值的分数。
结果是:
均匀分布应该接近“完全相等”,这样洛伦兹曲线弯曲就消失了。
基尼系数是洛伦斯曲线下的面积,通常用来分析收入在人口中的分布。https://github.com/oliviaguest/gini使用python为其提供了简单的实现。
这是意料之中的。来自均匀分布的随机样本不会产生均匀值(即所有值都相对接近)。通过一点微积分,可以证明[0,1]上均匀分布的样本基尼系数的期望值(在统计学意义上)是1/3,因此得到给定样本的1/3左右的值是合理的。
你会得到一个更低的基尼系数,比如
v = 10 + np.random.rand(500)
。这些值都接近10.5;相对变化低于样本v = np.random.rand(500)
。 实际上,样本base + np.random.rand(n)
的基尼系数的期望值是1/(6*base+3)。这是基尼系数的一个简单实现。它使用的事实是基尼系数是relative mean absolute difference的一半。
以下是几种样本的基尼系数
v = base + np.random.rand(500)
:相关问题 更多 >
编程相关推荐