def test_compare_atkinson_with_R(self): """ Comparison with R version in ineq package Results Atkinson a=0.5: 0.1796591 Atkinson a=1: 0.3518251 """ myIndex = cl.Index() x = np.array([541, 1463, 2445, 3438, 4437, 5401, 6392, 8304, 11904, 22261]) ERROR_MARGIN = 1e-5 self.assertTrue(abs(myIndex.atkinson(x, 0.5) - 0.1796591) < ERROR_MARGIN) self.assertTrue(abs(myIndex.atkinson(x, 1.0) - 0.3518251) < ERROR_MARGIN)
def test_eg_single_industry(self): myIndex = cl.Index() # Number of observations N = 10 # Number of areas Na = 10 # Number of industries Ni = 1 # uniform exposure exposure = np.ones(N) # single industry industry = np.zeros(N, dtype=np.int) # uniform area distribution area = np.arange(0, N) # create dataframe d = {'Exposure': exposure, 'Area': area, 'Industry': industry} data = pd.DataFrame(data=d) self.assertTrue(abs(myIndex.ellison_glaeser(data, Na, Ni)[0] - 0.0) < ERROR_MARGIN)
def test_eg_five_uniform(self): myIndex = cl.Index() # Number of observations N = 50 # Number of areas Na = 10 # Number of industries Ni = 5 # uniform exposure exposure = np.ones(N) # single industry industry = np.array(list(np.arange(0, Ni)) * Na) # uniform area distribution y_list = [[j for i in range(Ni)] for j in range(Na)] area = np.array([y for x in y_list for y in x]) # create dataframe d = {'Exposure': exposure, 'Area': area, 'Industry': industry} data = pd.DataFrame(data=d) results = myIndex.ellison_glaeser(data, Na, Ni) for i in range(Ni): self.assertTrue(abs(results[i] - 0.0) < ERROR_MARGIN)
def test_eg_max_concentration(self): myIndex = cl.Index() # Number of observations N = 400000 # Number of areas Na = 200000 # Number of industries Ni = 2 # uniform exposure exposure = np.zeros(N) exposure[0:199999] = 1 exposure[399999] = 1 exposure[399998] = 0.0001 # two industries area = np.array(list(np.arange(0, Na)) * Ni) # uniform area distribution y_list = [[j for i in range(Na)] for j in range(Ni)] industry = np.array([y for x in y_list for y in x]) # create dataframe d = {'Exposure': exposure, 'Area': area, 'Industry': industry} data = pd.DataFrame(data=d) self.assertTrue(abs(myIndex.ellison_glaeser(data, Na, Ni)[0] - 0.0) < ERROR_MARGIN)
def test_hk(self): myIndex = cl.Index() n = 10 vector = np.ones(n) self.assertTrue(abs(myIndex.hk(vector, 1) - 1.0 / n) < ERROR_MARGIN) self.assertTrue(abs(myIndex.hk(vector, 3) - 1.0 / n) < ERROR_MARGIN)
def test_shannon(self): myIndex = cl.Index() vector = np.ones(10) self.assertTrue(abs(myIndex.shannon(vector) - 0.0) < ERROR_MARGIN)
def test_bp(self): myIndex = cl.Index() n = 1000 vector = np.ones(n) self.assertTrue(abs(myIndex.cr(vector, 1) - myIndex.berger_parker(vector)) < ERROR_MARGIN)
def test_atkinson(self): myIndex = cl.Index() vector = np.zeros(1000) vector[0] = 1 self.assertTrue(abs(myIndex.atkinson(vector, 2) - 1.0) < ERROR_MARGIN)
def test_atkinson_uniform(self): myIndex = cl.Index() vector = np.ones(1000000) self.assertTrue(abs(myIndex.atkinson(vector, 0.5) - 0.0) < ERROR_MARGIN) self.assertTrue(abs(myIndex.atkinson(vector, 1) - 0.0) < ERROR_MARGIN) self.assertTrue(abs(myIndex.atkinson(vector, 2) - 0.0) < ERROR_MARGIN)
import concentration_library as cl import numpy as np myIndex = cl.Index() x = np.array([541, 1463, 2445, 3438, 4437, 5401, 6392, 8304, 11904, 22261]) # Comparison with R version in ineq package # Results # Gini: 0.4620911 # Atkinson a=0.5: 0.1796591 # Atkinson a=1: 0.3518251 print(myIndex.gini(x)) print(myIndex.atkinson(x, 0.5)) print(myIndex.atkinson(x, 1.0))