title: Gini coefficient calculation using Numpy
body: <p>I'm trying to write some code to calculate the Gini index for a fake country. I've came up with the following:</p>
  <pre><code>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
  </code></pre>
  <p>The result of <code>Gini = area_perfect - area_lorenz</code> just makes no sense. I've took out the values given by the area variables and did the math by hand and it came out fairly ok, but when I try to get the program to do it, it gives me a completely ??? value (-1.7198...). What am I missing?</p>