Matlab draws Gini coefficient and draws Lorentz curve

insert image description here
insert image description here
The meaning is: sort the income of all people (assuming exactly 100 people) from small to large, and then start accumulating from the least income. For each person, the abscissa is the ratio of the cumulative value of the number of people to the total number of people, and the ordinate is the cumulative value of the income. Gross income percentage, up to the last highest earner.

Obviously, the abscissa and ordinate in the figure are between [0-1]. Connecting the lower left corner and upper right corner of the figure means that the cumulative proportion of the number of people is always equal to the cumulative proportion of income, which means that the income is completely equal.

Well, the area between the above line of equal income for everyone and the real income curve is A, and the area between the real income curve and the X-axis is B.
matlab code

%x1=0:0.1:1;
%总人口 7681520041
%总收入 1313967.445亿元
%a1=xlsread('C:\Users\zh128\Desktop\update_NY.GNP.xls',1,'C5:C195');
%a2=xlsread('C:\Users\zh128\Desktop\update_NY.GNP.xls',2,'D1:D192');
%a3=xlsread('C:\Users\zh128\Desktop\update_NY.GNP.xls',2,'E1:E192');
a1=xlsread('C:\Users\zh128\Desktop\update_NY.GNP.xls',1,'C5:C195');
a2=xlsread('C:\Users\zh128\Desktop\update_NY.GNP.xls',3,'D1:D192');
a3=xlsread('C:\Users\zh128\Desktop\update_NY.GNP.xls',3,'E1:E192');
a2=a2./7681520041;
x1=a2';

x2=0:0.001:1;%作为拟合数据使用
a3=a3./1313967.445;
t=a3';%收入数百分比

xlen=length(x1);

y=zeros(1,xlen);
x0=zeros(1,xlen);
for i=1:xlen

y(i)=sum(t(1:i));

end

for i=1:xlen

x0(i)=sum(x1(1:i));

end


% c=polyfit(x1,y,7);

c=fit(x0',y','smoothingspline');

%d=polyval(c,x2);

d=c(x2);

%plot([0,1],[0,1],x1,y,['-'])

plot(x0,x0,'b-.',x0,y,'*',x2,d,'-')

title('Lorenz Curve')

xlabel('Cumulative percentage of population'),ylabel('Cumulative revenue percentage')

axis equal

axis([0,1,0,1])

grid on

%计算基尼系数

area1=trapz(x2,d);

area2=trapz(x0,x0);

JN=(area2-area1)/area2;

disp(['Gini coefficient of national income=',num2str(JN)])


Guess you like

Origin blog.csdn.net/david2000999/article/details/123017727