Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"sns.distplot(compas.age)\n",
"plt.title(\"Histogram of defendants' ages\")\n",
"plt.xlabel(\"Age of defendant\")\n",
"plt.show()"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"scrolled": false
},
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>is_recid</th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" </tr>\n",
" <tr>\n",
" <th>age_cat</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>25 - 45</th>\n",
" <td>1784</td>\n",
" <td>1748</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Greater than 45</th>\n",
" <td>847</td>\n",
" <td>446</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Less than 25</th>\n",
" <td>551</td>\n",
" <td>796</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"is_recid 0 1\n",
"age_cat \n",
"25 - 45 1784 1748\n",
"Greater than 45 847 446\n",
"Less than 25 551 796"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>is_recid</th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" </tr>\n",
" <tr>\n",
" <th>sex</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>Female</th>\n",
" <td>740</td>\n",
" <td>435</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Male</th>\n",
" <td>2442</td>\n",
" <td>2555</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"is_recid 0 1\n",
"sex \n",
"Female 740 435\n",
"Male 2442 2555"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>is_recid</th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" </tr>\n",
" <tr>\n",
" <th>race</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">African-American</th>\n",
" <th>25 - 45</th>\n",
" <td>847.0</td>\n",
" <td>1051.0</td>\n",
" <th>Greater than 45</th>\n",
" <td>261.0</td>\n",
" <td>207.0</td>\n",
" <th>Less than 25</th>\n",
" <td>294.0</td>\n",
" <td>515.0</td>\n",
" <th rowspan=\"3\" valign=\"top\">Asian</th>\n",
" <th>25 - 45</th>\n",
" <td>10.0</td>\n",
" <td>4.0</td>\n",
" <th>Greater than 45</th>\n",
" <td>7.0</td>\n",
" <td>4.0</td>\n",
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
" <th>Less than 25</th>\n",
" <td>4.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Caucasian</th>\n",
" <th>25 - 45</th>\n",
" <td>620.0</td>\n",
" <td>508.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Greater than 45</th>\n",
" <td>442.0</td>\n",
" <td>186.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Less than 25</th>\n",
" <td>167.0</td>\n",
" <td>180.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Hispanic</th>\n",
" <th>25 - 45</th>\n",
" <td>180.0</td>\n",
" <td>111.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Greater than 45</th>\n",
" <td>81.0</td>\n",
" <td>28.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Less than 25</th>\n",
" <td>51.0</td>\n",
" <td>58.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Native American</th>\n",
" <th>25 - 45</th>\n",
" <td>5.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Greater than 45</th>\n",
" <td>NaN</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Less than 25</th>\n",
" <td>NaN</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th rowspan=\"3\" valign=\"top\">Other</th>\n",
" <th>25 - 45</th>\n",
" <td>122.0</td>\n",
" <td>72.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Greater than 45</th>\n",
" <td>56.0</td>\n",
" <td>19.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Less than 25</th>\n",
" <td>35.0</td>\n",
" <td>39.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"is_recid 0 1\n",
"race age_cat \n",
"African-American 25 - 45 847.0 1051.0\n",
" Greater than 45 261.0 207.0\n",
" Less than 25 294.0 515.0\n",
"Asian 25 - 45 10.0 4.0\n",
" Greater than 45 7.0 4.0\n",
" Less than 25 4.0 2.0\n",
"Caucasian 25 - 45 620.0 508.0\n",
" Greater than 45 442.0 186.0\n",
" Less than 25 167.0 180.0\n",
"Hispanic 25 - 45 180.0 111.0\n",
" Greater than 45 81.0 28.0\n",
" Less than 25 51.0 58.0\n",
"Native American 25 - 45 5.0 2.0\n",
" Greater than 45 NaN 2.0\n",
" Less than 25 NaN 2.0\n",
"Other 25 - 45 122.0 72.0\n",
" Greater than 45 56.0 19.0\n",
" Less than 25 35.0 39.0"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"tab = compas.groupby(['age_cat', 'is_recid']).size()\n",
"display(tab.unstack())\n",
"\n",
"tab = compas.groupby(['sex', 'is_recid']).size()\n",
"display(tab.unstack())\n",
"\n",
"tab = compas.groupby(['race', 'age_cat', 'is_recid']).size()\n",
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From above it is clear that there are no Native American recidivists of age over 45 or under 25. There are some other value combinations that might be problematic."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Synthetic data\n",
"In the chunk below, we generate the synthetic data as described by Lakkaraju et al. The default values are as per their description. The definitions of $Y$ and $T$ values follow their description.\n",
"* M = `nJudges_M`, number of judges\n",
"* N = `nSubjects_N`, number of subjects assigned to each judge\n",
"* betas $\\beta_i$ = `beta_i`, where $i \\in \\{X, Z, W\\}$ are coefficients for the respected variables\n",
"* R = `acceptanceRate_R`, acceptance rates\n",
"* X = `X`, invidual's features observable to all (models and judges)\n",
"* Z = `Z`, information observable for judges only\n",
"* W = `W`, unobservable / inaccessible information\n",
"* T = `decision_T`, decisions where $T=0$ represents decision to deny and if $T=1$ then bail is granted.\n",
"* Y = `result_Y`, result variable, if $Y=0$ person will or would recidivate and if $Y=1$ person would not commit a crime."
]
},
{
"cell_type": "code",
"metadata": {
"scrolled": false
},
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>count</th>\n",
" <th>mean</th>\n",
" <th>std</th>\n",
" <th>min</th>\n",
" <th>25%</th>\n",
" <th>50%</th>\n",
" <th>75%</th>\n",
" <th>max</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>judgeID_J</th>\n",
" <td>50000.0</td>\n",
" <td>49.500000</td>\n",
" <td>28.866359</td>\n",
" <td>0.000000</td>\n",
" <td>24.750000</td>\n",
" <td>49.500000</td>\n",
" <td>74.250000</td>\n",
" <td>99.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>acceptanceRate_R</th>\n",
" <td>50000.0</td>\n",
" <td>0.478235</td>\n",
" <td>0.230644</td>\n",
" <td>0.103756</td>\n",
" <td>0.264643</td>\n",
" <td>0.473985</td>\n",
" <td>0.647587</td>\n",
" <td>0.890699</td>\n",
" </tr>\n",
" <tr>\n",
" <th>X</th>\n",
" <td>50000.0</td>\n",
" <td>-0.003875</td>\n",
" <td>0.996715</td>\n",
" <td>-4.659953</td>\n",
" <td>-0.671782</td>\n",
" <td>-0.001726</td>\n",
" <td>0.668077</td>\n",
" <td>3.831790</td>\n",
" </tr>\n",
" <tr>\n",
" <th>Z</th>\n",
" <td>50000.0</td>\n",
" <td>0.006964</td>\n",
" <td>0.998001</td>\n",
" <td>-4.852118</td>\n",
" <td>-0.666258</td>\n",
" <td>0.004730</td>\n",
" <td>0.679477</td>\n",
" <td>4.241772</td>\n",
" </tr>\n",
" <tr>\n",
" <th>W</th>\n",
" <td>50000.0</td>\n",
" <td>0.010863</td>\n",
" <td>0.996944</td>\n",
" <td>-4.029138</td>\n",
" <td>-0.666574</td>\n",
" <td>0.012306</td>\n",
" <td>0.679578</td>\n",
" <td>4.285856</td>\n",
" </tr>\n",
" <tr>\n",
" <th>result_Y</th>\n",
" <td>50000.0</td>\n",
" <td>0.496500</td>\n",
" <td>0.499993</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" </tr>\n",
" <tr>\n",
" <th>probabilities_T</th>\n",
" <td>50000.0</td>\n",
" <td>0.500794</td>\n",
" <td>0.279762</td>\n",
" <td>-0.335627</td>\n",
" <td>0.276723</td>\n",
" <td>0.501317</td>\n",
" <td>0.723352</td>\n",
" <td>1.295719</td>\n",
" </tr>\n",
" <tr>\n",
" <th>decision_T</th>\n",
" <td>50000.0</td>\n",
" <td>0.477260</td>\n",
" <td>0.499488</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>0.000000</td>\n",
" <td>1.000000</td>\n",
" <td>1.000000</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" count mean std min 25% \\\n",
"judgeID_J 50000.0 49.500000 28.866359 0.000000 24.750000 \n",
"acceptanceRate_R 50000.0 0.478235 0.230644 0.103756 0.264643 \n",
"X 50000.0 -0.003875 0.996715 -4.659953 -0.671782 \n",
"Z 50000.0 0.006964 0.998001 -4.852118 -0.666258 \n",
"W 50000.0 0.010863 0.996944 -4.029138 -0.666574 \n",
"result_Y 50000.0 0.496500 0.499993 0.000000 0.000000 \n",
"probabilities_T 50000.0 0.500794 0.279762 -0.335627 0.276723 \n",
"decision_T 50000.0 0.477260 0.499488 0.000000 0.000000 \n",
"\n",
" 50% 75% max \n",
"judgeID_J 49.500000 74.250000 99.000000 \n",
"acceptanceRate_R 0.473985 0.647587 0.890699 \n",
"X -0.001726 0.668077 3.831790 \n",
"Z 0.004730 0.679477 4.241772 \n",
"W 0.012306 0.679578 4.285856 \n",
"result_Y 0.000000 1.000000 1.000000 \n",
"probabilities_T 0.501317 0.723352 1.295719 \n",
"decision_T 0.000000 1.000000 1.000000 "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\\begin{tabular}{lrrrrrrrr}\n",
"\\hline\n",
" & Määrä & Keskiarvo & Keskihajonta & Minimi & 25\\% & 50\\% & 75\\% & Maksimi \\\\\n",
"\\hline\n",
" judgeID\\_J & 50000.00 & 49.50 & 28.87 & 0.00 & 24.75 & 49.50 & 74.25 & 99.00 \\\\\n",
" acceptanceRate\\_R & 50000.00 & 0.48 & 0.23 & 0.10 & 0.26 & 0.47 & 0.65 & 0.89 \\\\\n",
" X & 50000.00 & -0.00 & 1.00 & -4.66 & -0.67 & -0.00 & 0.67 & 3.83 \\\\\n",
" Z & 50000.00 & 0.01 & 1.00 & -4.85 & -0.67 & 0.00 & 0.68 & 4.24 \\\\\n",
" W & 50000.00 & 0.01 & 1.00 & -4.03 & -0.67 & 0.01 & 0.68 & 4.29 \\\\\n",
" result\\_Y & 50000.00 & 0.50 & 0.50 & 0.00 & 0.00 & 0.00 & 1.00 & 1.00 \\\\\n",
" probabilities\\_T & 50000.00 & 0.50 & 0.28 & -0.34 & 0.28 & 0.50 & 0.72 & 1.30 \\\\\n",
" decision\\_T & 50000.00 & 0.48 & 0.50 & 0.00 & 0.00 & 0.00 & 1.00 & 1.00 \\\\\n",
"\\hline\n",
"\\end{tabular}\n",
"0 26137\n",
"1 23863\n",
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
"Name: decision_T, dtype: int64\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>decision_T</th>\n",
" <th>0</th>\n",
" <th>1</th>\n",
" </tr>\n",
" <tr>\n",
" <th>result_Y</th>\n",
" <th></th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0.0</th>\n",
" <td>20083</td>\n",
" <td>5092</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1.0</th>\n",
" <td>6054</td>\n",
" <td>18771</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"0.0 20083 5092\n",
"1.0 6054 18771"
"output_type": "display_data"
"# Set seed for reproducibility\n",
"npr.seed(0)\n",
"\n",
"def generateData(nJudges_M=100,\n",
" nSubjects_N=500,\n",
" beta_X=1.0,\n",
" beta_Z=1.0,\n",
" beta_W=0.2):\n",
"\n",
" # Assign judge IDs as running numbering from 0 to nJudges_M - 1\n",
" judgeID_J = np.repeat(np.arange(0, nJudges_M, dtype=np.int32), nSubjects_N)\n",
"\n",
" # Sample acceptance rates uniformly from a closed interval\n",
" # from 0.1 to 0.9 and round to tenth decimal place.\n",
" acceptance_rates = np.round(npr.uniform(.1, .9, nJudges_M), 10)\n",
"\n",
" # Replicate the rates so they can be attached to the corresponding judge ID.\n",
" acceptanceRate_R = np.repeat(acceptance_rates, nSubjects_N)\n",
"\n",
" # Sample the variables from standard Gaussian distributions.\n",
" X = npr.normal(size=nJudges_M * nSubjects_N)\n",
" Z = npr.normal(size=nJudges_M * nSubjects_N)\n",
" W = npr.normal(size=nJudges_M * nSubjects_N)\n",
"\n",
" probabilities_Y = 1 / (1 + np.exp(-(beta_X * X + beta_Z * Z + beta_W * W)))\n",
"\n",
" # 0 if P(Y = 0| X = x; Z = z; W = w) >= 0.5 , 1 otherwise\n",
" result_Y = 1 - probabilities_Y.round()\n",
"\n",
" probabilities_T = 1 / (1 + np.exp(-(beta_X * X + beta_Z * Z)))\n",
" probabilities_T += npr.normal(0, .1, nJudges_M * nSubjects_N)\n",
"\n",
" # Initialize decision values as 1\n",
" decision_T = np.ones(nJudges_M * nSubjects_N)\n",
"\n",
" # Initialize the dataframe\n",
" df_init = pd.DataFrame(\n",
" np.column_stack((judgeID_J, acceptanceRate_R, X, Z, W, result_Y,\n",
" probabilities_T, decision_T)),\n",
" columns=[\n",
" \"judgeID_J\", \"acceptanceRate_R\", \"X\", \"Z\", \"W\", \"result_Y\",\n",
" \"probabilities_T\", \"decision_T\"\n",
" ])\n",
"\n",
" # Sort by judges then probabilities\n",
" data = df_init.sort_values(\n",
" by=[\"judgeID_J\", \"probabilities_T\"], ascending=False)\n",
"\n",
" # Iterate over the data. Subject is in the top (1-r)*100% if\n",
" # his within-judge-index is over acceptance threshold times\n",
" # the number of subjects assigned to each judge. If subject\n",
" # is over the limit they are assigned a zero, else one.\n",
" data.reset_index(drop=True, inplace=True)\n",
"\n",
" data['decision_T'] = np.where(\n",
" (data.index.values % nSubjects_N) <\n",
" ((1 - data['acceptanceRate_R']) * nSubjects_N), 0, 1)\n",
"\n",
" return data\n",
"\n",
"\n",
"df = generateData()\n",
"\n",
"# Basic stats of the created data set.\n",
"display(df.describe().T)\n",
"print(df.decision_T.value_counts())\n",
"\n",
"tab = df.groupby(['result_Y', 'decision_T']).size()\n",
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(25000, 8)\n",
"(25000, 8)\n",
"(11866, 8)\n",
"(11997, 8)\n"
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th>decision_T</th>\n",
" </tr>\n",
" <tr>\n",
" <th>result_Y</th>\n",
" <th></th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0.0</th>\n",
" </tr>\n",
" <tr>\n",
" <th>1.0</th>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
"0.0 2495\n",
"1.0 9371"
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Split the data set to test and train\n",
"from sklearn.model_selection import train_test_split\n",
"train, test = train_test_split(df, test_size=0.5, random_state=0)\n",
"\n",
"print(train.shape)\n",
"print(test.shape)\n",
"\n",
"train_labeled = train[train.decision_T == 1]\n",
"\n",
"print(train_labeled.shape)\n",
"\n",
"tab = train_labeled.groupby(['result_Y', 'decision_T']).size()\n",
"tab.unstack()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Contraction algorithm\n",
"Below is an implementation of Lakkaraju's team's algorithm presented in [their paper](https://helka.finna.fi/PrimoRecord/pci.acm3098066). Relevant parameters to be passed to the function are presented in the description."
]
},
{
"cell_type": "code",
"def contraction(df,\n",
" judgeIDJ_col,\n",
" decisionT_col,\n",
" resultY_col,\n",
" modelProbS_col,\n",
" accRateR_col,\n",
" r,\n",
" binning=False):\n",
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
" '''\n",
" This is an implementation of the algorithm presented by Lakkaraju\n",
" et al. in their paper \"The Selective Labels Problem: Evaluating \n",
" Algorithmic Predictions in the Presence of Unobservables\" (2017).\n",
" \n",
" Parameters:\n",
" df = The (Pandas) data frame containing the data, judge decisions,\n",
" judge IDs, results and probability scores.\n",
" judgeIDJ_col = String, the name of the column containing the judges' IDs\n",
" in df.\n",
" decisionT_col = String, the name of the column containing the judges' decisions\n",
" resultY_col = String, the name of the column containing the realization\n",
" modelProbS_col = String, the name of the column containing the probability\n",
" scores from the black-box model B.\n",
" accRateR_col = String, the name of the column containing the judges' \n",
" acceptance rates\n",
" r = Float between 0 and 1, the given acceptance rate.\n",
" binning = Boolean, should judges with same acceptance rate be binned\n",
" \n",
" Returns:\n",
" u = The estimated failure rate at acceptance rate r.\n",
" '''\n",
" # Sort first by acceptance rate and judge ID.\n",
" sorted_df = df.sort_values(\n",
" by=[accRateR_col, judgeIDJ_col], ascending=False)\n",
"\n",
" if binning:\n",
" # Get maximum leniency\n",
" max_leniency = sorted_df[accRateR_col].values[0].round(1)\n",
"\n",
" # Get list of judges that are the most lenient\n",
" most_lenient_list = sorted_df.loc[sorted_df[accRateR_col].round(1) ==\n",
" max_leniency, judgeIDJ_col]\n",
"\n",
" # Subset to obtain D_q\n",
" D_q = sorted_df[sorted_df[judgeIDJ_col].isin(\n",
" most_lenient_list.unique())]\n",
" else:\n",
" # Get most lenient judge\n",
" most_lenient_ID = sorted_df[judgeIDJ_col].values[0]\n",
" # Subset\n",
" D_q = sorted_df[sorted_df[judgeIDJ_col] == most_lenient_ID]\n",
" R_q = D_q[D_q[decisionT_col] == 1]\n",
"\n",
" number_to_remove = int(\n",
" np.round((1 - r) * D_q.shape[0] - (D_q.shape[0] - R_q.shape[0])))\n",
"\n",
" R_B = R_sort_q[number_to_remove:R_sort_q.shape[0]]\n",
"\n",
" return np.sum(R_B[resultY_col] == 0) / D_q.shape[0]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Our model is defined by the probabilistic expression \n",
"P(Y=0 | \\text{do}(R=r)) = \\sum_x \\underbrace{P(Y=0|X=x, T=1)}_\\text{1} \n",
"\\overbrace{P(T=1|R=r, X=x)}^\\text{2} \n",
"\\underbrace{P(X=x)}_\\text{3}\n",
"As a picture (Z not in model):\n",
"\n",
"\n",
"\n",
"**Algorithm**\n",
"Our model will be constructed sequentially.\n",
"Input: Data $(\\mathbf{x}, t, y) \\in \\mathcal{D}$ and acceptance rate $r$. \n",
"Returns: $P(Y=0 | \\text{do}(R=r))$\n",
"1. Model $P(X=x)$ in a suitable way and assign to $\\mathcal{M}_0$\n",
"* Build model $\\mathcal{M}_1$ predicting response $Y$ with predictors $X$ from the labeled observations (where $T=1$) in training data.\n",
"* Predict $P(Y=0|X=x)$ for every observation in the test data using model $\\mathcal{M}_1$.\n",
"* Order all observations by the probability score from previous phase in ascending order. Now the \"most harmless\" are at the top of the list.\n",
"* Initialize `sum = 0`\n",
"* For every point in the parameter space (for every $x$ in $X$)\n",
" 1. $p_x \\leftarrow P(X=x)$ from $\\mathcal{M}_0$\n",
" * $\\mathcal{D_x} \\leftarrow \\{\\mathcal{D} | X = x\\}$\n",
" * Assign first $r\\cdot 100\\%$ observations from $\\mathcal{D_x}$ to $\\mathcal{D}_{rx}$\n",
" * $p_t \\leftarrow \\dfrac{|\\{\\mathcal{D}_{rx}|T=1\\}|}{|\\mathcal{D}_{rx}|}$\n",
" * $p_y$ will be predicted from the model $\\mathcal{M}_1$\n",
" * `sum +=` $p_y \\cdot p_t \\cdot p_x$\n",
"* Return `sum`\n",
"**Constructing $\\mathcal{M}_0$, preliminary ideas:**\n",
"* Approximate $P(X=x)$ with frequencies (make variables factors first)\n",
"\n",
"TEE ALLA OLEVASTA FUNKTIO!!! Kaikki sklearnin mallit implementtaa fitin ja predictin https://scikit-learn.org/stable/tutorial/basic/tutorial.html"
]
},
{
"cell_type": "code",
"name": "stdout",
"output_type": "stream",
"text": [
"[0.13017179 0.1265672 0.12309921 0.12101056 0.11946303 0.1163152\n",
" 0.11309497 0.10995003]\n"
"r_list = np.linspace(0.1, 0.8, 8, endpoint=True)\n",
"# 1 For now we don't model f(x), just utilize it's N(0, 1)\n",
"lr_causal_result = LogisticRegression(solver='lbfgs')\n",
"\n",
"# fit, reshape X to be of shape (n_samples, n_features)\n",
"lr_causal_result.fit(train_labeled['X'].values.reshape(-1, 1),\n",
" train_labeled.result_Y)\n",
"\n",
"# 3\n",
"causal_probs = lr_causal_result.predict_proba(test.X.values.reshape(-1, 1))\n",
"\n",
"test = test.assign(B_prob_0_causal=causal_probs[:, 0])\n",
"\n",
"# 4\n",
"test_ordered = test.sort_values(by='B_prob_0_causal', ascending=True)\n",
"\n",
"#5\n",
"probability_list = np.zeros_like(r_list)\n",
"i = 0\n",
" # 6\n",
" for x in range(-5, 5):\n",
" # A\n",
" p_x = scs.norm.pdf(x)\n",
" # B\n",
" D_x = test_ordered[test_ordered.X.round(0) == x]\n",
"\n",
" if D_x.shape[0] == 0:\n",
" continue\n",
" # C\n",
" if round(r * D_x.shape[0]) == 0:\n",
" continue\n",
"\n",
" D_rx = D_x[0:int(round(r * D_x.shape[0]))]\n",
" # D\n",
" p_t = np.sum(D_rx.decision_T == 1) / D_rx.shape[0]\n",
" # E\n",
" p_y = lr_causal_result.predict_proba(np.array(x).reshape(-1, 1))\n",
" probability_list[i] += p_y[0, 0] * p_t * p_x\n",
"# 7\n",
"print(probability_list)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Performance comparison\n",
"\n",
"Below we try to replicate the results obtained by Lakkaraju and compare their model's performance to the one of ours.\n",
"### Synthetic data\n",
"\n",
"#### Predictive models\n",
"\n",
"Lakkaraju says that they used logistic regression to predict recidivism. We train the model using only *observed observations*, i.e. defendants that were granted bail and are in the train set. We then predict the probability of recidivism for all observations in the test data and attach it to our data set."
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
"metadata": {},
"outputs": [],
"source": [
"# instantiate the model (using the default parameters)\n",
"logreg_machine = LogisticRegression(solver='lbfgs')\n",
"\n",
"# fit, reshape X to be of shape (n_samples, n_features)\n",
"logreg_machine.fit(\n",
" train_labeled.X.values.reshape(-1, 1), train_labeled.result_Y)\n",
"\n",
"# predict probabilities and attach to data\n",
"label_probabilities_machine = logreg_machine.predict_proba(\n",
" test.X.values.reshape(-1, 1))\n",
"\n",
"test = test.assign(B_prob_0_machine=label_probabilities_machine[:, 0])\n",
"\n",
"\n",
"clf = tree.DecisionTreeClassifier()\n",
"clf = clf.fit(train_labeled.X.values.reshape(-1, 1), train_labeled.result_Y)\n",
"\n",
"preds = clf.predict_proba(test.X.values.reshape(-1, 1))\n",
"\n",
"test = test.assign(B_prob_0_tree=preds[:, 0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
]
},
{
"cell_type": "code",
"metadata": {},
"outputs": [],
"source": [
"#probs = causal()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
]
},
{
"cell_type": "code",