# brightway2-analyzer

committed e75c204

# bw2analyzer/econ.py

` `
`     """`
`     # 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())`

# bw2analyzer/tests/econ.py

` 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))`
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.