Skip to content
Permalink
main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#We evaluate the Accuracy, Balanced Accuracy and bias using measures such as demographic parity difference (DP) and equalized odds (EO)
#for grouping variables such as Gender and Ethnicity.
```{r, eval=FALSE}
#This is a helper function to compute subgroup measures of any desired grouping variable
get_subgroup_measures <- function(y_true, y_pred, groups) {
df <- data.frame(matrix(ncol=7,nrow=0,
dimnames=list(NULL, c("Group", "TPR", "FPR", "TNR", "FNR", "PR", "BA"))))
sg <- unique(groups)
for (s in sg) {
cm <- as.matrix(table(Actual=y_true[groups == s],
Predicted=y_pred[groups == s]))
# if (dim(cm)[1] != 2 || dim(cm)[2] != 2) {
# next
# }
tpr <- cm[2,2] / (cm[2,2]+cm[2,1])
fpr <- cm[1,2] / (cm[1,2]+cm[1,1])
tnr <- 1 - fpr
fnr <- 1 - tpr
pr <- (cm[1,2]+cm[2,2]) / sum(cm)
ba <- (tpr + tnr) / 2
df <- rbind(df, list(Group=s, TPR=tpr, FPR=fpr,
TNR=tnr, FNR=fnr, PR=pr, BA=ba))
}
df
}
# return demographic parity difference which is the
# maximum difference in predicted positive rates between two subgroups
demographic_parity_difference <- function(sg_measures) {
combos <- combn(sg_measures$Group, 2)
dpd <- c()
for (i in 1:ncol(combos)) {
g1 <- combos[1,i]
g1_pr <- sg_measures[sg_measures["Group"] == g1, "PR"]
g2 <- combos[2,i]
g2_pr <- sg_measures[sg_measures["Group"] == g2, "PR"]
dpd <- c(dpd, abs(g1_pr - g2_pr))
}
max(dpd)
}
# return the equalized odds difference which is
# the maximum difference between true positive rates or true negative rates
# between two subgroups
equalized_odds_difference <- function(sg_measures) {
combos <- combn(sg_measures$Group, 2)
eod <- c()
for (i in 1:ncol(combos)) {
g1 <- combos[1,i]
g1_tpr <- sg_measures[sg_measures["Group"] == g1, "TPR"]
g1_fpr <- sg_measures[sg_measures["Group"] == g1, "FPR"]
g2 <- combos[2,i]
g2_tpr <- sg_measures[sg_measures["Group"] == g2, "TPR"]
g2_fpr <- sg_measures[sg_measures["Group"] == g2, "FPR"]
eod <- c(eod, abs(g1_tpr - g2_tpr))
eod <- c(eod, abs(g1_fpr - g2_fpr))
}
max(eod)
}
```