# HG changeset patch # User Chris Mutel # Date 1377242570 -7200 # Node ID e75c20487f547dbf0812d30e70a87b405a37db63 # Parent cdf80c9f00c599453c5cc9c5ea1202cec801b2a8 Add Theil index diff --git a/bw2analyzer/econ.py b/bw2analyzer/econ.py --- a/bw2analyzer/econ.py +++ b/bw2analyzer/econ.py @@ -45,13 +45,10 @@ """ # 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) @@ -77,3 +74,24 @@ 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()) diff --git a/bw2analyzer/tests/econ.py b/bw2analyzer/tests/econ.py --- a/bw2analyzer/tests/econ.py +++ b/bw2analyzer/tests/econ.py @@ -1,5 +1,6 @@ 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 @@ -32,3 +33,11 @@ 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))