Skip to content

Instantly share code, notes, and snippets.

@msjgriffiths
Created October 25, 2021 20:26
Show Gist options
  • Save msjgriffiths/2a188dd43e25e3096f42254aefaafa59 to your computer and use it in GitHub Desktop.
Save msjgriffiths/2a188dd43e25e3096f42254aefaafa59 to your computer and use it in GitHub Desktop.
Lift to AUC
#' Lift to AUC
#'
#' @param percentiles
#' @param conversion_rate
#'
#' @return list
#'
#' @description
#'
#' This function takes in percentiles and the conversion rate,
#' and computes the AUC.
#'
#' Often we see tables that have groups and conversion rate (or relative
#' to a baseline) and it's a report of model performance. However, it's
#' difficult to reason about.
#'
#' This function converts that to AUC, and reports Gini on the way.
#'
#' @examples
#'
#' x <- rbeta(10, 1, 9)
#' percentiles <- cumsum(x) / sum(x)
#' conversion_rate <- rbeta(10, 1, 100)
#'
#' lift_to_auc(percentiles, conversion_rate)
#'
lift_to_auc <- function(percentiles, conversion_rate) {
sizes <- diff(c(0, percentiles))
cume_convs <- cumsum(sizes * conversion_rate) / sum(sizes * conversion_rate)
breaks <- ((cume_convs + na.exclude(lag(c(0, cume_convs)))) / 2) * sizes
gini <- sum(breaks)
auc <- (1+gini) / 2
return(list(
Gini = gini,
AUC = auc
))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment