Gini系数公式原理

基尼系数通常用于衡量分布的不平等程度,在经济学中经常用来衡量收入分配的不平等。基尼系数的值介于0到1之间,0表示完全平等,1表示完全不平等。

基尼系数用于衡量各特征对波动的解释效果。以下是计算基尼系数的详细步骤:

步骤1:准备数据

  1. 获取数据:收集各个分组(如不同城市等级)的基期值和波动值。
  2. 计算波动值:波动值 = 本期值 - 基期值。

示例数据:

维度(城市等级) 基期值 本期值 波动值
一线城市 50 55 5
二线城市 100 90 -10
三线城市 150 165 15
四线城市 200 180 -20

步骤2:计算累计占比

  1. 计算基期累计占比

    基期累计占比 = 当前基期值之和 / 总基期值

  2. 计算波动值累计占比

    波动值累计占比 = 当前波动值之和 / 总波动值

示例

维度(城市等级) 基期值 本期值 波动值 基期累计占比 波动值累计占比
一线城市 50 55 5 0.1 -0.5
二线城市 100 90 -10 0.3 0.5
三线城市 150 165 15 0.6 1
四线城市 200 180 -20 1 0

步骤3:绘制曲线

绘制基期累计占比(横轴)和波动值累计占比(纵轴)的曲线,同时绘制45度对角线。

import matplotlib.pyplot as plt

# 数据
x = [0.1, 0.3, 0.6, 1.0]
y = [-0.5, 0.5, 1.0, 0.0]

# 绘制图表
plt.figure(figsize=(8, 6))
plt.plot(x, y, marker='o', label='Cumulative Proportion of Variance')
plt.plot([0, 1], [0, 1], linestyle='--', color='gray', label='45-degree Line')  # 45度对角线

# 添加标签和标题
plt.xlabel('Cumulative Proportion of Baseline')
plt.ylabel('Cumulative Proportion of Variance')
plt.title('Cumulative Proportion of Baseline vs Cumulative Proportion of Variance')
plt.legend()

# 显示图表
plt.grid(True)
plt.show()

 

步骤4:计算曲线与45度对角线之间的面积(A),曲线与45度对角线之间的面积如下图所示,可以切分为三部分。

import numpy as np
import matplotlib.pyplot as plt

# 数据
x = np.array([0.1, 0.3, 0.6, 1.0])
y = np.array([-0.5, 0.5, 1.0, 0.0])
y_diag = x  # 45度对角线的y值

# 设置颜色
colors = ['yellow', 'blue', 'green', 'red']

# 绘制图表
plt.figure(figsize=(8, 6))
plt.plot(x, y, marker='o', label='Cumulative Proportion of Variance')
plt.plot([0, 1], [0, 1], linestyle='--', color='gray', label='45-degree Line')  # 45度对角线

# 标注每个数据点
for i in range(len(x)):
    plt.text(x[i], y[i], f'({x[i]}, {y[i]})')
    plt.text(x[i], y_diag[i], f'({x[i]}, {y_diag[i]})', color='gray', fontsize=8)

# 添加标签和标题
plt.xlabel('Cumulative Proportion of Baseline')
plt.ylabel('Cumulative Proportion of Variance')
plt.title('Cumulative Proportion of Baseline vs Cumulative Proportion of Variance')
plt.legend()
plt.grid(True)

# 高亮每段面积
for i in range(1, len(x)):
    plt.fill_between([x[i-1], x[i]], [y[i-1], y[i]], [y_diag[i-1], y_diag[i]], color=colors[i-1], alpha=0.5)

plt.show()

 

 

步骤5:计算基尼系数

# 计算每段面积
areas = []
y_diag = x
for i in range(1, len(x)):
    dx = x[i] - x[i-1]
    y_avg = (y[i] + y[i-1]) / 2.0
    y_diag_avg = (y_diag[i] + y_diag[i-1]) / 2.0
    area = dx * (y_avg - y_diag_avg)
    areas.append(area)

# 打印每段面积
for i, area in enumerate(areas):
    print(f'Area of segment {i+1}: {area:.4f}')

# 计算总面积,考虑负值
total_area = sum(areas)
total_area_abs =abs(total_area)
print(total_area_abs)

# 计算基尼系数
B = 0.5  # 45度对角线下方的面积
G = total_area_abs / (total_area_abs + B)

print(f'Total area between the curve and the 45-degree line: {total_area:.4f}')
print(f'Absolute total area between the curve and the 45-degree line: {total_area_abs:.4f}')
print(f'Gini coefficient: {G:.4f}')

 

总结

通过上述步骤,我们计算得出的基尼系数为0.1228。这个值表示特征对波动的解释效果,基尼系数越大,说明该特征对波动的解释效果越好。通过这种方法,我们可以评估不同特征在解释指标波动中的作用。

 

附说明,图形面积求解原理,我们以第一段面积为例讲解:

import numpy as np
import matplotlib.pyplot as plt

# 定义数据点
x = np.array([0.1, 0.3])
y = np.array([-0.5, 0.5])

# 定义对角线
x_diag = np.array([0.1, 0.3])
y_diag = x_diag  # 45度对角线

# 定义平均高度
y_avg = (y[0] + y[1]) / 2
y_diag_avg = (x[0] + x[1]) / 2

# 绘制图表
plt.figure(figsize=(8, 6))
plt.plot(x, y, marker='o', label='Curve')
plt.plot(x_diag, y_diag, linestyle='--', color='gray', label='45-degree Line')

# 标注数据点和平均高度
plt.text(x[0], y[0], f'({x[0]}, {y[0]})')
plt.text(x[1], y[1], f'({x[1]}, {y[1]})')
plt.text((x[0] + x[1]) / 2, y_avg, f'Avg: {y_avg:.2f}', color='blue')
plt.text((x[0] + x[1]) / 2, y_diag_avg, f'Avg Diag: {y_diag_avg:.2f}', color='gray')

# 填充面积
plt.fill_between(x, y, y_diag, color='lightgreen', alpha=0.5, label='Area')

# 添加标签和标题
plt.xlabel('x')
plt.ylabel('y')
plt.title('Average Height Calculation')
plt.legend()
plt.grid(True)
plt.show()

 

数据点:X轴向量[0.1,0.3],Y轴向量[-0.5,0.5]

  • 点1:(0.1, -0.5)
  • 点2:(0.3, 0.5)

对角线对应的y值

计算步骤:

  1. 底边 dx: dx = 0.3 - 0.1 = 0.2 

  2. 曲线y值的平均值 y_avg(两个数据点的y值的平均值): y_avg = (0.5 - 0.5)/2 = 0 

  3. 对角线y值的平均值 y_diag_avg(两个数据点在对角线上的y值的平均值):  y_diag_avg = (0.1 + 0.3)/2 = 0.2

  4. 梯形面积 Area:Area =  (y_avg - y_diag_avg) *dx= (0 - 0.2)*0.2 =  -0.04 

解释

  • y_avg 是曲线在这段 x 范围内的平均高度。
  • y_diag_avg 是对角线在这段 x 范围内的平均高度。

在第一段,曲线的平均高度(y_avg = 0)低于对角线的平均高度(y_diag_avg = 0.2)。因此,高度差 y_avg - y_diag_avg 是负的,导致面积为负。

如果想验证这种计算方式是否正确,可以使用Y=-1/3x+2,求直线和X轴、Y轴的面积。

关注公众号「水沐教育科技」,在手机上阅读所有教程,随时随地都能学习。内含一款搜索神器,免费下载全网书籍和视频。

公众号二维码
微信扫码关注公众号

微信交流群 关注微信公众号,加入官方交流群。内含一款搜索神器,免费下载全网书籍和视频。