Commits

Chris Mutel committed cdf80c9

Add econ file for econometrics tests; still need integration into analysis

Comments (0)

Files changed (4)

bw2analyzer/contribution.py

 
         This is a measure of the relative importance of the top-scoring processes compared to other important processes."""
         raise NotImplemented
-        data = self.top_processes(matrix, limit=limit)[::-1, 0]
-        expected = np.arange(1, limit + 1) / total
 
     def concentration_ratio(self, matrix, total, limit=4):
         """A measure of the concentration of LCA scores in the highest contributing processes.

bw2analyzer/econ.py

+from __future__ import division
+import numpy as np
+
+
+def gini_coefficient(x):
+    """
+    Return computed Gini coefficient.
+
+    See http://en.wikipedia.org/wiki/Gini_coefficient
+
+    Adapted from econpy library.
+    copyright: 2005-2009 Alan G. Isaac
+    license: MIT license
+    contact: aisaac AT american.edu
+
+    Args:
+        *x* (list or array): Data
+
+    Returns:
+        Gini coefficient (float)
+
+    """
+    x = np.array(x)
+    x.sort()
+    y = np.cumsum(x)
+    length = float(x.shape[0])
+    B = y.sum() / (y[-1] * length)
+    return float(1. + 1 / length - 2 * B)
+
+
+def herfindahl_index(x, normalize=True):
+    """
+    Return computed Herfindahl index.
+
+    See http://en.wikipedia.org/wiki/Herfindahl_index
+
+    Normalized scores are bounded [0, 1]; non-normalized scores are [1/len(x), 1]. Normalization only counts non-zero values.
+
+    Args:
+        *x* (list or array): Data
+        *normalize* (bool, default=True): Flag to normalize scores.
+
+    Returns:
+        Herfindahl index (float)
+
+    """
+    # 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)
+
+
+def concentration_ratio(x, number=4):
+    """
+    Return computed concentration ratio.
+
+    See http://en.wikipedia.org/wiki/Concentration_ratio
+
+    The concentration ratio measures the share of the market controlled by the top *number* firms. Returned ratio values vary from 0 to 1.
+
+    Args:
+        *x* (list or array): Data
+        *number* (int, default=4): Number of values to consider. 4 and 8 are commonly used.
+
+    Returns:
+        Concentration ratio (float)
+
+    """
+    # Normalize so that total is 1
+    x = np.array(x) / np.sum(x)
+    x.sort()
+    return float(x[-number:].sum())
+

bw2analyzer/tests/__init__.py

+from .econ import EconometricsTestCase

bw2analyzer/tests/econ.py

+from __future__ import division
+from ..econ import concentration_ratio, gini_coefficient, herfindahl_index
+import numpy as np
+import unittest
+
+
+class EconometricsTestCase(unittest.TestCase):
+    def test_concentration_ratio(self):
+        x = (0.2, 0.2, 0.2, 0.2, 0.1, 0.1)
+        self.assertEqual(concentration_ratio(x), 0.8)
+        self.assertTrue(isinstance(concentration_ratio(x), float))
+
+    def test_concentration_ratio_normalization(self):
+        x = np.array((0.2, 0.2, 0.2, 0.2, 0.1, 0.1)) * 2
+        self.assertEqual(concentration_ratio(x), 0.8)
+
+    def test_concentration_ratio_number(self):
+        x = np.array((0.2, 0.2, 0.2, 0.2, 0.1, 0.1))
+        self.assertEqual(concentration_ratio(x, 2), 0.4)
+
+    def test_herfindahl(self):
+        x = np.array((1., 1., 1.), dtype=float)
+        # Correct answer is 3 * (1/3) ^ 2 = 1/3
+        self.assertEqual(herfindahl_index(x, False), 1 / 3)
+        # Normalized it is zero (all values are the same)
+        self.assertEqual(herfindahl_index(x), 0)
+        x = np.array((0.8, 0.1, 0.1))
+        self.assertAlmostEqual(herfindahl_index(x, False), 0.64 + 0.01 + 0.01)
+        self.assertTrue(isinstance(herfindahl_index(x, False), float))
+
+    def test_gini(self):
+        x = np.array((0.2, 0.3, 0.4, 0.5, 0.6))
+        # From wikipedia page
+        self.assertAlmostEqual(gini_coefficient(x), 0.2)