From dd476e08897b408a7120d5f091a686657aee77ec Mon Sep 17 00:00:00 2001 From: "Adam Hoelscher, ASA, MAAA" Date: Thu, 28 Jun 2018 09:37:11 -0700 Subject: [PATCH] Refactor auc to avoid multiplication Multiplying n_pos and n_neg can result in integer overflow. A little algebraic manipulation can avoid the multiplication, removing the possibility of overflow. --- R/R/metrics.r | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/R/R/metrics.r b/R/R/metrics.r index 3d8ad57..67388bb 100644 --- a/R/R/metrics.r +++ b/R/R/metrics.r @@ -123,7 +123,7 @@ auc <- function(actual, predicted) r <- rank(predicted) n_pos <- sum(actual==1) n_neg <- length(actual) - n_pos - auc <- (sum(r[actual==1]) - n_pos*(n_pos+1)/2) / (n_pos*n_neg) + auc <- sum(r[actual==1])/n_pos/n_neg - (n_pos+1)/2/n_neg auc }