def test_roc_auc(self): """roc_auc should calculate the trapezoidal approximation to the area under a ROC curve """ #Uninformative single-point case: points = [(0.5,0.5)] exp = 0.50 self.assertFloatEqual(roc_auc(points,add_endpoints=True),exp) #Perfect single-point case: points = [(0.0,1.0)] exp = 1.00 self.assertFloatEqual(roc_auc(points,add_endpoints=True),exp) #Basic test of two-point perfect prediction points = [(0.0,1.00),(0.75,1.00)] #exp = 0.75*0.75+(0.25*0.75*0.25)*2 #Calculate using one rectangle and two triangles exp = 1.0 self.assertFloatEqual(roc_auc(points,add_endpoints=True),exp) # Per Hand and Till 2001, AUC = 2*Gini - 1 #So I derive the expected value from the Gini example below: # From Catalano et al 2009, table 3, page 11 # Available here: #http://scholarcommons.usf.edu/cgi/viewcontent.cgi?article=1032&context=numeracy proportion_of_population = [0.1*i for i in range (11)] cumulative_portion_of_consumption =\ [0.000,\ 0.023,\ 0.060,\ 0.110,\ 0.175,\ 0.254,\ 0.345,\ 0.459,\ 0.588,\ 0.754,\ 1.000] points = zip(proportion_of_population,\ cumulative_portion_of_consumption) gini_obs = gini_coefficient(points) gini_exp = 0.346 self.assertFloatEqual(gini_obs,gini_exp,eps=1e-3) roc_obs = roc_auc(points, add_endpoints=True) print "ROC AUC obs:",roc_obs exp = gini_exp #Convert ROC to equivalent Gini index obs = abs(2.0*roc_obs - 1.0) self.assertFloatEqual(obs,exp,eps=1e-3)
def test_roc_auc(self): """roc_auc should calculate the trapezoidal approximation to the area under a ROC curve """ #Uninformative single-point case: points = [(0.5,0.5)] exp = 0.50 self.assertFloatEqual(roc_auc(points,add_endpoints=True),exp) #Perfect single-point case: points = [(0.0,1.0)] exp = 1.00 self.assertFloatEqual(roc_auc(points,add_endpoints=True),exp) #Basic test of two-point perfect prediction points = [(0.0,1.00),(0.75,1.00)] #exp = 0.75*0.75+(0.25*0.75*0.25)*2 #Calculate using one rectangle and two triangles exp = 1.0 self.assertFloatEqual(roc_auc(points,add_endpoints=True),exp) # Per Hand and Till 2001, AUC = 2*Gini - 1 #So I derive the expected value from the Gini example below: # From Catalano et al 2009, table 3, page 11 # Available here: #http://scholarcommons.usf.edu/cgi/viewcontent.cgi?article=1032&context=numeracy proportion_of_population = [0.1*i for i in range (11)] cumulative_portion_of_consumption =\ [0.000,\ 0.023,\ 0.060,\ 0.110,\ 0.175,\ 0.254,\ 0.345,\ 0.459,\ 0.588,\ 0.754,\ 1.000] points = zip(proportion_of_population,\ cumulative_portion_of_consumption) gini_obs = gini_coefficient(points) gini_exp = 0.346 self.assertFloatEqual(gini_obs,gini_exp,eps=1e-3) roc_obs = roc_auc(points, add_endpoints=True) exp = gini_exp #Convert ROC to equivalent Gini index obs = abs(2.0*roc_obs - 1.0) self.assertFloatEqual(obs,exp,eps=1e-3)
def test_gini_coefficient(self): """gini_coefficient should calculate the trapezoidal approximation to the Gini coefficient""" # From Catalano et al 2009, table 3, page 11 # Available here: #http://scholarcommons.usf.edu/cgi/viewcontent.cgi?article=1032&context=numeracy proportion_of_population = [0.1*i for i in range (11)] cumulative_portion_of_consumption =\ [0.000,\ 0.023,\ 0.060,\ 0.110,\ 0.175,\ 0.254,\ 0.345,\ 0.459,\ 0.588,\ 0.754,\ 1.000] points = zip(proportion_of_population,\ cumulative_portion_of_consumption) obs = gini_coefficient(points) exp = 0.346 self.assertFloatEqual(obs,exp,eps=1e-3)