Skip to content

Instantly share code, notes, and snippets.

@quintona
Created November 27, 2013 03:42
Show Gist options
  • Save quintona/7670373 to your computer and use it in GitHub Desktop.
Save quintona/7670373 to your computer and use it in GitHub Desktop.
Simple example calculating the gini in R
calc.gini <- function(y)
{
num.pos <- sum(y==1)
num.neg <- sum(y==0)
tpr <- cumsum(y==1)/num.pos
fpr <- cumsum(y==0)/num.neg
area <- (tpr[-1] + tpr[1:(length(tpr)-1)]) %*% (fpr[-1] - fpr[1:(length(fpr)-1)])
1 - as.numeric(area)
}
d <- data.frame(gt = c(1,1,1,1,1,0,0,0,0), p = c(0.9, 0.7, 0.1, 0.2, 0.5, 0.01, 0.4, 0.23, 0.3))
calc.gini(d$gt[order(d$p)])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment