title: Summing over pair of indices (or more) in Python
body: <p>One way to calculate the Gini coefficient of a sample is using the relative mean difference (RMD) which is 2 times the Gini coefficient. RMD depends on the mean difference which is given by:</p>
  
  <p><img src="https://i.stack.imgur.com/htAXv.png" alt="enter image description here"></p>
  
  <p>So I need to calculate each difference between pair of elements in a sample <code>(yi - yj)</code>. It took me quite a bit to figure out a way to do it but I want to know if there is a function that does this for you.</p>
  
  <p>At first I tried this but I bet it's very slow in big data sets (by the way, s is the sample):</p>
  
  <pre><code>In [124]:
  
  %%timeit
  from itertools import permutations
  k = 0
  for i, j in list(permutations(s,2)):
      k += abs(i-j)
  MD = k/float(len(s)**2)
  G = MD / float(mean(s))
  G = G/2
  G
  10000 loops, best of 3: 78 us per loop
  </code></pre>
  
  <p>Then I tried the following which is less understandable but quicker:</p>
  
  <pre><code>In [126]:
  %%timeit
  m = abs(s - s.reshape(len(s), 1))
  MD = np.sum(m)/float((len(s)**2))
  G = MD / float(mean(s))
  G = G/2
  G
  10000 loops, best of 3: 46.8 us per loop
  </code></pre>
  
  <p>Is there something efficient but easy to generalize? For example, what if I want to sum over three indices?</p>
  
  <p>This is the sample I was using:</p>
  
  <pre><code>sample = array([5487574374,     686306,    5092789,   17264231,   41733014,
           60870152,   82204091,  227787612,  264942911,  716909668,
          679759369, 1336605253,  788028471,  331434695,  146295398,
           88673463,  224589748,  128576176,  346121028])
  
  gini(sample)
  Out[155]:
  0.2692307692307692
  </code></pre>
  
  <p>Thanks!</p>