Stardust
Your problem is not numpy.trapz ; these are: 1) your definition of a perfect distribution of equality, and 2) the normalization of the Gini coefficient.
First, you determined the ideal distribution of equality as Q5*H5/2 , which is half the product of the income of the fifth quintile and the aggregate percentage (1.0). I'm not sure what this number means.
Secondly, you need to normalize the area under the full distribution of equality; i.e:.
gini = (area under full equality - area under Lorentz) / (area under full equality)
You do not need to worry about this if you define a perfect equality curve so that it has an area of โโ1, but it is good protection in the event of an error in determining the ideal equality curve.
To solve both of these problems, I defined a perfect equality curve using numpy.linspace . The first advantage of this is that you can use the actual properties of the distribution to define it in the same way. In other words, if you use quartiles or quintiles or deciles, then the ideal CDF equality ( y_pe , below) will have the correct form. The second advantage is that its area is calculated using numpy.trapz , and the parallelism bit, which makes the code more readable and protects against erroneous calculations.
import numpy as np from matplotlib import pyplot as plt from numpy import trapz GDP = (653200000000) A = (0.49 * GDP) / 100
source share