def test_eg_five_uniform(self): """ Testing EG Five Uniform Industries """ myIndex = cm.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) print(self.shortDescription()) for i in range(Ni): self.assertTrue(abs(results[i] - 0.0) < ERROR_MARGIN)
def test_eg_max_concentration(self): """ Testing EG Maximum Concentration """ myIndex = cm.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) print(self.shortDescription()) self.assertTrue( abs(myIndex.ellison_glaeser(data, Na, Ni)[0] - 0.0) < ERROR_MARGIN)
def test_ci_api(self): """ Testing that all implemented indexes have monotonic confidence intervals """ myIndex = cm.Index() portfolio = np.random.normal(loc=10, scale=1, size=100) methods = [['cr', 5], ['berger_parker'], ['hhi'], ['hk', 3], ['hoover'], ['gini'], ['shannon'], ['atkinson', 1.5], ['gei', 3], ['theil'], ['kolm', 2]] print(self.shortDescription()) for method in methods: if len(method) > 1: lower_bound, value, upper_bound = myIndex.compute( portfolio, method[1], index=method[0], ci=0.95, samples=1000) else: lower_bound, value, upper_bound = myIndex.compute( portfolio, index=method[0], ci=0.95, samples=1000) self.assertTrue(lower_bound <= value) self.assertTrue(upper_bound >= value)
def test_atkinson_uniform(self): myIndex = cm.Index() vector = np.ones(1000000) print(self.shortDescription()) 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)
def test_hhi(self): """ Testing Herfindahl-Hirschman """ myIndex = cm.Index() vector = np.ones(10) print(self.shortDescription()) self.assertTrue(abs(myIndex.hhi(vector) - 0.0) < ERROR_MARGIN)
def test_gini(self): """ Testing Gini """ myIndex = cm.Index() vector = np.ones(10) print(self.shortDescription()) self.assertTrue(abs(myIndex.gini(vector) - 0.0) < ERROR_MARGIN)
def test_shannon(self): """ Testing Shannon """ myIndex = cl.Index() vector = np.ones(10) print(self.shortDescription()) self.assertTrue(abs(myIndex.shannon(vector) - 0.0) < ERROR_MARGIN)
def test_cr(self): """ Testing Concentration Ratio """ myIndex = cm.Index() n = 1000 vector = np.ones(n) print(self.shortDescription()) self.assertTrue(abs(myIndex.cr(vector, 1) - 1.0 / n) < ERROR_MARGIN)
def test_hk(self): """ Testing Hannah-Kay """ myIndex = cm.Index() n = 10 vector = np.ones(n) print(self.shortDescription()) 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_bp(self): """ Testing Berger-Parker """ myIndex = cm.Index() n = 1000 vector = np.ones(n) print(self.shortDescription()) self.assertTrue( abs(myIndex.cr(vector, 1) - myIndex.berger_parker(vector)) < ERROR_MARGIN)
def test_compare_atkinson_with_R(self): """ Atkinson Index: Comparison with R version in ineq package Results Atkinson a=0.5: 0.1796591 Atkinson a=1: 0.3518251 """ myIndex = cm.Index() x = np.array( [541, 1463, 2445, 3438, 4437, 5401, 6392, 8304, 11904, 22261]) ERROR_MARGIN = 1e-5 print(self.shortDescription()) 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): """ Testing EG Single Industry """ myIndex = cm.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) print(self.shortDescription()) self.assertTrue( abs(myIndex.ellison_glaeser(data, Na, Ni)[0] - 0.0) < ERROR_MARGIN)
# encoding: utf-8 # (c) 2016-2020 Open Risk, all rights reserved # # ConcentrationMetrics is licensed under the MIT license a copy of which is included # in the source distribution of TransitionMatrix. This is notwithstanding any licenses of # third-party software included in this distribution. You may not use this file except in # compliance with the License. # # Unless required by applicable law or agreed to in writing, software distributed under # the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, # either express or implied. See the License for the specific language governing permissions and # limitations under the License. import concentrationMetrics as cm import numpy as np myIndex = cm.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))
| Shannon| |--------:| | 4.018412| | 3.848471| | 3.814059| | 3.976563| | 3.969940| | 3.776575| """ BCI = pd.read_json(dataset_path + "BCI.json") y = BCI.values # Dataframe API myGroupIndex = cm.Index(data=y, index='simpson') myGroupIndex.print(6) myGroupIndex = cm.Index(data=y, index='invsimpson') myGroupIndex.print(6) myGroupIndex = cm.Index(data=y, index='shannon') myGroupIndex.print(6) # Columnwise API mySingleIndex = cm.Index() for i in range(6): print('HHI') print('----------') print(i, mySingleIndex.hhi(y[i, :]))
import concentrationMetrics 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))
def test_atkinson(self): myIndex = cm.Index() vector = np.zeros(1000) vector[0] = 1 print(self.shortDescription()) self.assertTrue(abs(myIndex.atkinson(vector, 2) - 1.0) < ERROR_MARGIN)