Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
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
" </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",
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
" <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",
"execution_count": 16,
"metadata": {
"scrolled": false
},
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
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
{
"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": [
"0 26137\n",
"1 23863\n",
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
"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"
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
]
},
{
"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",
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
" '''\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",
"ename": "NameError",
"evalue": "name 'LogisticRegression' is not defined",
"output_type": "error",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[1;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-19-e3446881b865>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 5\u001b[0m \u001b[1;31m# 2\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 6\u001b[1;33m \u001b[0mlr_causal_result\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mLogisticRegression\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msolver\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;34m'lbfgs'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;31m# fit, reshape X to be of shape (n_samples, n_features)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNameError\u001b[0m: name 'LogisticRegression' is not defined"
"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",
"### 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."
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# import the class\n",
"from sklearn.linear_model import LogisticRegression\n",
"\n",
"# 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",
"from sklearn import tree\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": [
"### Causal model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#probs = causal()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Visual comparison"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
1945
1946
1947
1948
1949
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
"source": [
"failure_rates = np.zeros((8, 4))\n",
"\n",
"for r in np.arange(1, 9):\n",
" failure_rates[r - 1, 0] = contraction(\n",
" test[test.decision_T == 1], 'judgeID_J', 'decision_T', 'result_Y',\n",
" 'B_prob_0_machine', 'acceptanceRate_R', r / 10, True)\n",
"\n",
" ## Human error rate - Jotain väärin viel'\n",
" # Get judges with correct leniency as list\n",
" correct_leniency_list = test.judgeID_J[test['acceptanceRate_R'].round(1) ==\n",
" r / 10]\n",
"\n",
" # Released are the people they judged and released, T = 1\n",
" released = test[test.judgeID_J.isin(correct_leniency_list)\n",
" & (test.decision_T == 1)]\n",
"\n",
" # Get their failure rate, aka ratio of reoffenders to number of people judged in total\n",
" failure_rates[r - 1, 1] = np.sum(\n",
" released.result_Y == 0) / correct_leniency_list.shape[0]\n",
"\n",
" ## True evaluation\n",
" failure_rates[r - 1, 2] = contraction(test, 'judgeID_J', 'decision_T',\n",
" 'result_Y', 'B_prob_0_machine',\n",
" 'acceptanceRate_R', r / 10, True)\n",
" ## Dec tree\n",
" failure_rates[r - 1, 3] = contraction(\n",
" test[test.decision_T == 1], 'judgeID_J', 'decision_T', 'result_Y',\n",
" 'B_prob_0_tree', 'acceptanceRate_R', r / 10, True)\n",
"\n",
"# klassifikaatioille scipy.stats semin kautta error barit xerr ja yerr argumenttien kautta\n",
"\n",
"plt.figure(figsize=(14, 8))\n",
"plt.plot(np.arange(0.1, 0.9, .1), failure_rates[:, 0], label='Contraction')\n",
"plt.plot(np.arange(0.1, 0.9, .1), failure_rates[:, 1], label='Human')\n",
"plt.plot(np.arange(0.1, 0.9, .1), failure_rates[:, 2], label='True Evaluation')\n",
"plt.plot(np.arange(0.1, 0.9, .1), probability_list, label='Causal model')\n",
"\n",
"plt.plot(\n",
" np.arange(0.1, 0.9, .1), failure_rates[:, 3], label='Classification tree')\n",
"\n",
"plt.title('Failure rate vs. Acceptance rate')\n",
"plt.xlabel('Acceptance rate')\n",
"plt.ylabel('Failure rate')\n",
"plt.legend()\n",
"plt.show()\n",
"print(failure_rates)\n",
"print(probability_list)"
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Failure rates still too high. Order of curves now correct. Causal model calculates the wrong thing? "