# Commits

committed e75c204

• Participants
• Parent commits cdf80c9
• Branches default

# File bw2analyzer/econ.py

• Ignore whitespace
` `
`     """`
`     # Normalize so that total is 1`
`-    print x`
`     x = np.array(x) / np.sum(x)`
`-    print x`
`     index = (x ** 2).sum()`
`     if normalize:`
`         correction = 1 / (x != 0).sum()`
`-        print index, correction`
`         index = (index - correction) / (1 - correction)`
`     return float(index)`
` `
`     x.sort()`
`     return float(x[-number:].sum())`
` `
`+`
`+def theil_index(x):`
`+    """`
`+    Return Theil index.`
`+`
`+    See http://en.wikipedia.org/wiki/Theil_Index`
`+`
`+    The Theil index is a measure of economic inequality based on information theory. It is the difference between a dataset's maximum possible entropy and observed entropy.`
`+`
`+    Args:`
`+        *x* (list or array): Data`
`+`
`+    Returns:`
`+        Theil index (float)`
`+`
`+    """`
`+    x = np.array(x).copy()`
`+    # Have to filter values because of log transform`
`+    x = np.abs(x[x != 0])`
`+    average, n = np.average(x), x.shape[0]`
`+    return float(1 / n * ((x / average) * np.log(x / average)).sum())`

# File bw2analyzer/tests/econ.py

• Ignore whitespace
` from __future__ import division`
`-from ..econ import concentration_ratio, gini_coefficient, herfindahl_index`
`+from ..econ import concentration_ratio, gini_coefficient, herfindahl_index, \`
`+    theil_index`
` import numpy as np`
` import unittest`
` `
`         x = np.array((0.2, 0.3, 0.4, 0.5, 0.6))`
`         # From wikipedia page`
`         self.assertAlmostEqual(gini_coefficient(x), 0.2)`
`+`
`+    def test_theil(self):`
`+        # Include negative and zero values to test filtering`
`+        x = np.array((0., -2., 2., 6., 20.))`
`+        average = 30 / 4`
`+        y = np.array((2., 2., 6., 20.))`
`+        answer = 1 / 4 * ((y / average) * np.log(y / average)).sum()`
`+        self.assertAlmostEqual(float(answer), theil_index(x))`