Skip to content

Commit

Permalink
fix: binary auc
Browse files Browse the repository at this point in the history
  • Loading branch information
be-marc committed Aug 20, 2024
1 parent ecf5f70 commit 5a05084
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 16 deletions.
19 changes: 7 additions & 12 deletions R/classif_auc.R
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,13 @@ mauc_mu = function(truth, prob, na_value = NaN, ...) {
v = a[class_i, ] - a[class_j, ]
scores = temp_preds %*% v

# calculate binary AUC
# order scores in decreasing order and order labels accordingly
response = temp_labels[order(scores, decreasing = TRUE)]

# calculate true positive rate and false positive rate
tpr = cumsum(response) / sum(response)
fpr = cumsum(!response) / sum(!response)

# calculate the AUC using the trapezoidal rule
tpr = c(0, tpr)
fpr = c(0, fpr)
sum(diff(fpr) * (tpr[-1] + tpr[-length(tpr)]) / 2)
# calculate binary auc
i = which(temp_labels == 1)
n_pos = length(i)
n_neg = length(temp_labels) - n_pos

r = rank(scores, ties.method = "average")
(mean(r[i]) - (as.numeric(n_pos) + 1) / 2) / as.numeric(n_neg)
})

sum(aucs * 1 / length(aucs))
Expand Down
6 changes: 2 additions & 4 deletions tests/testthat/test_classif.R
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,14 @@ test_that("multiclass auc", {
expect_equal(mauc_aunp(equalizer_truth, maxent_prob), 0.5)
expect_equal(mauc_aunu(equalizer_truth, maxent_prob), 0.5)
expect_equal(mauc_au1u(equalizer_truth, maxent_prob), 0.5)

# expect_equal(mauc_mu(equalizer_truth, maxent_prob), 0.5)
# does not give 0.5
expect_equal(mauc_mu(equalizer_truth, maxent_prob), 0.5)

# reversing prob gives 1 - auc
expect_equal(mauc_aunu(truth, prob), 1 - mauc_aunu(truth, 1 - prob))
expect_equal(mauc_aunp(truth, prob), 1 - mauc_aunp(truth, 1 - prob))
expect_equal(mauc_au1u(truth, prob), 1 - mauc_au1u(truth, 1 - prob))
expect_equal(mauc_au1p(truth, prob), 1 - mauc_au1p(truth, 1 - prob))
expect_equal(mauc_mu(truth, prob) , 1 - mauc_mu(truth, 1 - prob))
expect_equal(mauc_mu(truth, prob), 1 - mauc_mu(truth, 1 - prob))

# manually calculate au1u, au1p
compmat = sapply(levels(truth), function(t1) {
Expand Down

0 comments on commit 5a05084

Please sign in to comment.