title: Theil Index Python vs R
body: <p>I am trying to calculate the Theil index in python and R, but with the given functions, I am getting different answers. Here is the formula that I am trying to use:</p>
  
  <p><a href="https://i.stack.imgur.com/Qw7tm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Qw7tm.png" alt="Theil Calculation"></a></p>
  
  <p>Using the ineq package in R, I can easily get the Theil index:</p>
  
  <pre><code>library(ineq)
  x=c(26.1,16.1,15.5,15.4,14.8,14.7,13.7,12.1,11.7,11.6,11,10.8,10.8,7.5)
  Theil(x)
  0.04152699
  </code></pre>
  
  <p>This implementation seems to make sense and I can look at the code provided to see what exact calculations that are happening and it seems to follow the formula (deleting zeros when I get have them in order to take the log):</p>
  
  <pre><code>getAnywhere(Theil )
  Out[24]:
  A single object matching ‘Theil’ was found
  It was found in the following places
    package:ineq
    namespace:ineq
  with value
  
  function (x, parameter = 0, na.rm = TRUE) 
  {
      if (!na.rm &amp;&amp; any(is.na(x))) 
          return(NA_real_)
      x &lt;- as.numeric(na.omit(x))
      if (is.null(parameter)) 
          parameter &lt;- 0
      if (parameter == 0) {
          x &lt;- x[!(x == 0)]
          Th &lt;- x/mean(x)
          Th &lt;- sum(x * log(Th))
          Th &lt;- Th/sum(x)
      }
      else {
          Th &lt;- exp(mean(log(x)))/mean(x)
          Th &lt;- -log(Th)
      }
      Th
  }
  </code></pre>
  
  <p>However, I see that this question has been answered previously before for python <a href="https://stackoverflow.com/questions/20279458/implementation-of-theil-inequality-index-in-python">here </a>. The code is here, but the answers do not match for some reason:</p>
  
  <pre><code>def T(x):
      n = len(x)
      maximum_entropy = math.log(n)
      actual_entropy = H(x)
      redundancy = maximum_entropy - actual_entropy
      inequality = 1 - math.exp(-redundancy)
      return redundancy,inequality 
  
  def Group_negentropy(x_i):
      if x_i == 0:
          return 0
      else:
          return x_i*math.log(x_i)
  
  def H(x):
      n = len(x)
      entropy = 0.0
      summ = 0.0
      for x_i in x: # work on all x[i]
          summ += x_i
          group_negentropy = Group_negentropy(x_i)
          entropy += group_negentropy
      return -entropy
  x=np.array([26.1,16.1,15.5,15.4,14.8,14.7,13.7,12.1,11.7,11.6,11,10.8,10.8,7.5])
  T(x)
  (512.62045438815949, 1.0)
  </code></pre>