Permalink
Cannot retrieve contributors at this time
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?
CSCI6968_TML_Project/DOandEO
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
60 lines (58 sloc)
2.12 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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) | |
} | |
``` |