Fair AI Tools Action Set
Mitigating Bias of a Neural Network Model
This section contains PROC CAS code.
Note: Input data must be accessible in your CAS session, either as a CAS table or as a transient-scope table. A CAS table has a two-level name: the first level is your CAS engine libref, and the second level is the table name. You refer to this table in the CAS procedure by specifying only the second level. For more information about two-level names, see Chapter 2, Shared Concepts (SAS Viya: Machine Learning Procedures). A transient-scope table is called directly from the action and exists in memory for the duration of the action. For more information about accessing data, see SAS Viya: System Programming Guide. For more information about PROC CAS and programming in CASL, see SAS Cloud Analytic Services: CASL Programmer’s Guide and SAS Cloud Analytic Services: CASL Reference.
This example trains a neural network model in order to reduce bias according to the demographic parity metric by using the home equity data, which are available in the sampsio.hmeq data set. The data set has a binary target named BAD, and the example uses the variable JOB as the sensitive attribute.
You can load the sampsio.hmeq data set into your CAS session by specifying your CAS engine libref in the SET statement in the following DATA step:
data mycas.hmeq;
set sampsio.hmeq;
where JOB ne '';
run;
These statements assume that your CAS engine libref is named mycas, but you can substitute any appropriately defined CAS engine libref.
The following statements use the mitigateBiasNeuralNet action to run the exponentiated gradient reduction (EGR) algorithm in order to train a model that has reduced demographic parity bias, which is calculated on the sensitive attribute JOB. Note that the syntax of the trainOptions parameter here is the same as the syntax of the neuralNet.annTrain action.
proc cas;
fairAITools.mitigateBiasNeuralNet /
mitigateOptions={biasMetric="DEMOGRAPHICPARITY",
predictedVariables={"P_BAD1", "P_BAD0"},
responseLevels={"1", "0"},
seed="12345",
sensitiveVariable="JOB"},
trainOptions={combs={"LINEAR"},
encodeName="True",
errorFunc="ENTROPY",
hiddens={2},
inputs={"CLAGE",
"DEBTINC",
"DEROG",
"JOB",
"LOAN",
"MORTDUE",
"REASON",
"VALUE"},
nominals={"BAD",
"JOB",
"REASON"},
saveState={name="hmeq_nnet_astore",
replace="True"},
seed="54321",
std="MIDRANGE",
table="hmeq",
target="BAD",
targetAct="SOFTMAX"};
run;
The mitigateOptions parameter includes the following subparameters:
The
biasMetricsubparameter specifies that the demographic parity be used as the bias measurement.The
predictedVariablessubparameter specifies that the variablesP_BAD0andP_BAD1be used as the model predictions. A predicted variable is given for each level of the target variable.The
responseLevelssubparameter specifies the levels of the target that correspond to each variable that is listed in thepredictedVariablessubparameter. The order of the response levels must match the order of the predicted variables.The
seedsubparameter specifies the seed to use for pseudorandom number generation.The
sensitiveVariablesubparameter specifies that the variableJOBbe used as the sensitive variable in assessing and mitigating bias.
The trainOptions parameter includes the following subparameters that are passed to the neuralNet.annTrain action call:
The
combssubparameter specifies the LINEAR combination function for the neurons in each hidden layer.The
encodeNamesubparameter specifies that the variable names of the predicted probabilities of the target be encoded.The
errorFuncsubparameter specifies the error function to train the network. ENTROPY is the default setting for nominal targets.The
hiddenssubparameter specifies the number of hidden neurons for each hidden layer in the neural network feedforward model.The
inputssubparameter specifies that the variablesCLAGE,DEBTINC,DEROG,JOB,LOAN,MORTDUE,REASON, andVALUEbe used as inputs.The
nominalssubparameter specifies that the variablesBAD,JOB, andREASONbe used as nominal variables.The
saveStatesubparameter names the data table for saving the analytic store.The
seedsubparameter specifies the seed to use to perform sampling and partitioning tasks.The
stdsubparameter specifies the standardization to use on the interval variables.The
tablesubparameter names the input data table for training.The
targetsubparameter specifies that the variableBADbe used as the target variable.The
targetActsubparameter specifies the SOFTMAX function as the activation function for the neurons in the output layer.
The "Mitigation Information" table that these statements produce is shown in Output 15.15.1. The table shows some key information about the model and values of the key hyperparameters that the EGR algorithm uses.
The "Iteration History" table that these statements produce is shown in Output 15.15.2. The table displays the iteration history of the overall demographic parity, the overall misclassification rate, and the overall multiclass log loss. From the table, you can see that without bias mitigation, the demographic parity gap between those two sensitive attributes is about 3.7%, and after seven iterations of the EGR algorithm it has been reduced to about 0.2%. Meanwhile, the overall accuracy of the model increases slightly.
The "Group Metrics" table that these statements produce is shown in Output 15.15.3. The table displays the model assessment statistics and average prediction for each sensitive attribute during each iteration of the EGR algorithm.
Output 15.15.1: Mitigation Information
| Mitigation Information | ||
|---|---|---|
| Description | Character Value | Value |
| Bias Metric | DEMOGRAPHICPARITY | . |
| Sensitive Variable | JOB | . |
| Response | BAD | . |
| Bound Value | 100 | 100 |
| Learning Rate | 0.01 | 0.01 |
| Maximum Iteration | 10 | 10 |
| Tolerance | 0.005 | 0.005 |
| Event Level | 1 | . |
Output 15.15.2: Iteration History
| EGR Iteration History | |||
|---|---|---|---|
| Iteration | Demographic Parity (Statistical Parity) | Misclassification Rate | Multi-Class Log Loss |
| 1 | 0.036697 | 0.0863 | 0.2861 |
| 2 | 0.018349 | 0.0879 | 0.2897 |
| 3 | 0.018349 | 0.0874 | 0.2889 |
| 4 | 0.018349 | 0.0868 | 0.2884 |
| 5 | 0.018349 | 0.0868 | 0.2879 |
| 6 | 0.018349 | 0.0874 | 0.2888 |
| 7 | 0.002094 | 0.0877 | 0.2906 |
Output 15.15.3: Group Metrics History
| Group Metrics | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Iteration | JOB | Number of Observations | True Positive Rate | False Positive Rate | Area Under ROC | Misclassification Rate | Multi-Class Log Loss | Best Kolmogorov-Smirnov Along ROC | Cumulative Lift | Cumulative Response | Proportion Into Event Level | Average Prediction for Event |
| 1 | Mgr | 471 | 0.0426 | 0.0000 | 0.6520 | 0.0955 | 0.2943 | 0.2862 | 3.4043 | 34.0426 | 0.0026 | 0.1137 |
| Office | 618 | 0.0000 | 0.0000 | 0.5335 | 0.0631 | 0.2274 | 0.0666 | 4.3590 | 43.5897 | 0.0000 | 0.0669 | |
| Other | 1410 | 0.0548 | 0.0040 | 0.5476 | 0.1014 | 0.3299 | 0.0913 | 2.1918 | 21.9178 | 0.0054 | 0.0951 | |
| ProfExe | 925 | 0.0167 | 0.0000 | 0.5495 | 0.0638 | 0.2302 | 0.0988 | 2.6667 | 26.6667 | 0.0008 | 0.0675 | |
| Sales | 64 | 0.2500 | 0.0000 | 0.6204 | 0.1875 | 0.5266 | 0.3125 | 3.1250 | 31.2500 | 0.0367 | 0.1309 | |
| Self | 128 | 0.0000 | 0.0000 | 0.5357 | 0.1094 | 0.3418 | 0.0714 | 4.2857 | 42.8571 | 0.0000 | 0.0677 | |
| 2 | Mgr | 471 | 0.0000 | 0.0000 | 0.6352 | 0.0998 | 0.3075 | 0.2650 | 3.4043 | 34.0426 | 0.0000 | 0.0827 |
| Office | 618 | 0.0000 | 0.0000 | 0.5256 | 0.0631 | 0.2304 | 0.0513 | 3.3333 | 33.3333 | 0.0000 | 0.0682 | |
| Other | 1410 | 0.0411 | 0.0040 | 0.5749 | 0.1028 | 0.3262 | 0.0982 | 1.7808 | 17.8082 | 0.0046 | 0.1119 | |
| ProfExe | 925 | 0.0167 | 0.0000 | 0.5411 | 0.0638 | 0.2349 | 0.0822 | 2.1667 | 21.6667 | 0.0008 | 0.0677 | |
| Sales | 64 | 0.1250 | 0.0000 | 0.6250 | 0.2188 | 0.6017 | 0.2500 | 2.5000 | 25.0000 | 0.0183 | 0.0984 | |
| Self | 128 | 0.0000 | 0.0000 | 0.5357 | 0.1094 | 0.3501 | 0.0714 | 4.2857 | 42.8571 | 0.0000 | 0.0674 | |
| 3 | Mgr | 471 | 0.0426 | 0.0000 | 0.6410 | 0.0955 | 0.3007 | 0.2697 | 3.4043 | 34.0426 | 0.0026 | 0.0914 |
| Office | 618 | 0.0000 | 0.0000 | 0.5256 | 0.0631 | 0.2289 | 0.0513 | 3.5897 | 35.8974 | 0.0000 | 0.0665 | |
| Other | 1410 | 0.0411 | 0.0040 | 0.5485 | 0.1028 | 0.3291 | 0.0905 | 1.9863 | 19.8630 | 0.0046 | 0.1179 | |
| ProfExe | 925 | 0.0167 | 0.0000 | 0.5411 | 0.0638 | 0.2331 | 0.0822 | 2.5000 | 25.0000 | 0.0008 | 0.0662 | |
| Sales | 64 | 0.1250 | 0.0000 | 0.6250 | 0.2188 | 0.5848 | 0.2500 | 3.1250 | 31.2500 | 0.0183 | 0.1035 | |
| Self | 128 | 0.0000 | 0.0000 | 0.5357 | 0.1094 | 0.3475 | 0.0714 | 4.2857 | 42.8571 | 0.0000 | 0.0659 | |
| 4 | Mgr | 471 | 0.0426 | 0.0000 | 0.6910 | 0.0955 | 0.2987 | 0.3573 | 3.4043 | 34.0426 | 0.0026 | 0.0988 |
| Office | 618 | 0.0000 | 0.0000 | 0.5256 | 0.0631 | 0.2284 | 0.0513 | 3.5897 | 35.8974 | 0.0000 | 0.0652 | |
| Other | 1410 | 0.0548 | 0.0040 | 0.5949 | 0.1014 | 0.3291 | 0.1610 | 2.1233 | 21.2329 | 0.0054 | 0.1124 | |
| ProfExe | 925 | 0.0167 | 0.0000 | 0.5411 | 0.0638 | 0.2324 | 0.0822 | 2.6667 | 26.6667 | 0.0008 | 0.0651 | |
| Sales | 64 | 0.1250 | 0.0000 | 0.6250 | 0.2188 | 0.5893 | 0.2500 | 3.1250 | 31.2500 | 0.0183 | 0.0999 | |
| Self | 128 | 0.0000 | 0.0000 | 0.5357 | 0.1094 | 0.3464 | 0.0714 | 4.2857 | 42.8571 | 0.0000 | 0.0650 | |
| 5 | Mgr | 471 | 0.0426 | 0.0000 | 0.6352 | 0.0955 | 0.2987 | 0.2862 | 3.4043 | 34.0426 | 0.0026 | 0.1137 |
| Office | 618 | 0.0000 | 0.0000 | 0.5464 | 0.0631 | 0.2276 | 0.0922 | 4.8718 | 48.7179 | 0.0000 | 0.0655 | |
| Other | 1410 | 0.0548 | 0.0040 | 0.6059 | 0.1014 | 0.3291 | 0.2023 | 2.2603 | 22.6027 | 0.0054 | 0.1078 | |
| ProfExe | 925 | 0.0167 | 0.0000 | 0.5494 | 0.0638 | 0.2314 | 0.0988 | 2.6667 | 26.6667 | 0.0008 | 0.0655 | |
| Sales | 64 | 0.1250 | 0.0000 | 0.6563 | 0.2188 | 0.5898 | 0.3125 | 3.1250 | 31.2500 | 0.0183 | 0.0984 | |
| Self | 128 | 0.0000 | 0.0000 | 0.5357 | 0.1094 | 0.3432 | 0.0714 | 4.2857 | 42.8571 | 0.0000 | 0.0658 | |
| 6 | Mgr | 471 | 0.0426 | 0.0000 | 0.6874 | 0.0955 | 0.2996 | 0.3268 | 3.4043 | 34.0426 | 0.0026 | 0.1007 |
| Office | 618 | 0.0000 | 0.0000 | 0.5256 | 0.0631 | 0.2290 | 0.0513 | 4.3590 | 43.5897 | 0.0000 | 0.0644 | |
| Other | 1410 | 0.0411 | 0.0040 | 0.5946 | 0.1028 | 0.3280 | 0.1821 | 2.2603 | 22.6027 | 0.0046 | 0.1051 | |
| ProfExe | 925 | 0.0167 | 0.0000 | 0.5411 | 0.0638 | 0.2328 | 0.0822 | 2.8333 | 28.3333 | 0.0008 | 0.0643 | |
| Sales | 64 | 0.1250 | 0.0000 | 0.6250 | 0.2188 | 0.6174 | 0.2500 | 3.1250 | 31.2500 | 0.0183 | 0.0876 | |
| Self | 128 | 0.0000 | 0.0000 | 0.5357 | 0.1094 | 0.3466 | 0.0714 | 4.2857 | 42.8571 | 0.0000 | 0.0646 | |
| 7 | Mgr | 471 | 0.0000 | 0.0000 | 0.6309 | 0.0998 | 0.3053 | 0.2484 | 3.4043 | 34.0426 | 0.0000 | 0.0866 |
| Office | 618 | 0.0000 | 0.0000 | 0.5256 | 0.0631 | 0.2309 | 0.0513 | 4.1026 | 41.0256 | 0.0000 | 0.0647 | |
| Other | 1410 | 0.0342 | 0.0000 | 0.5488 | 0.1000 | 0.3260 | 0.0934 | 1.8493 | 18.4932 | 0.0021 | 0.0924 | |
| ProfExe | 925 | 0.0000 | 0.0000 | 0.5167 | 0.0649 | 0.2351 | 0.0333 | 2.8333 | 28.3333 | 0.0000 | 0.0642 | |
| Sales | 64 | 0.0000 | 0.0000 | 0.6250 | 0.2500 | 0.6590 | 0.2500 | 3.1250 | 31.2500 | 0.0000 | 0.0758 | |
| Self | 128 | 0.0000 | 0.0000 | 0.5357 | 0.1094 | 0.3511 | 0.0714 | 5.7143 | 57.1429 | 0.0000 | 0.0646 | |
Mitigating Bias of a Neural Network Model
This section contains Lua code for the analysis in the CASL version of this example, which contains details about the results.
For more information about coding in Lua, see Getting Started with SAS Viya for Lua and SAS Viya: System Programming Guide.
This example illustrates the mitigateBiasNeuralNet action by using the home equity data that are described in the CASL version of the example.
The following statements use the mitigateBiasNeuralNet action to run the exponentiated gradient reduction (EGR) algorithm in order to train a model that has reduced demographic parity bias, which is calculated on the sensitive attribute JOB. Note that the syntax of the trainOptions parameter here is the same as the syntax of the neuralNet.annTrain action.
mitigateOptions={biasMetric="DEMOGRAPHICPARITY",
predictedVariables={"P_BAD1", "P_BAD0"},
responseLevels={"1", "0"},
seed=12345,
sensitiveVariable="JOB"},
trainOptions={combs={"LINEAR"},
encodeName=True,
errorFunc="ENTROPY",
hiddens={2},
inputs={"CLAGE",
"DEBTINC",
"DEROG",
"JOB",
"LOAN",
"MORTDUE",
"REASON",
"VALUE"},
nominals={"BAD",
"JOB",
"REASON"},
saveState={name="hmeq_nnet_astore",
replace=True},
seed=54321,
std="MIDRANGE",
table="hmeq",
target="BAD",
targetAct="SOFTMAX"
}
The mitigateOptions parameter includes the following subparameters:
The
biasMetricsubparameter specifies that the demographic parity be used as the bias measurement.The
predictedVariablessubparameter specifies that the variablesP_BAD0andP_BAD1be used as the model predictions. A predicted variable is given for each level of the target variable.The
responseLevelssubparameter specifies the levels of the target that correspond to each variable that is listed in thepredictedVariablessubparameter. The order of the response levels must match the order of the predicted variables.The
seedsubparameter specifies the seed to use for pseudorandom number generation.The
sensitiveVariablesubparameter specifies that the variableJOBbe used as the sensitive variable in assessing and mitigating bias.
The trainOptions parameter includes the following subparameters that are passed to the neuralNet.annTrain action call:
The
combssubparameter specifies the LINEAR combination function for the neurons in each hidden layer.The
encodeNamesubparameter specifies that the variable names of the predicted probabilities of the target be encoded.The
errorFuncsubparameter specifies the error function to train the network. ENTROPY is the default setting for nominal targets.The
hiddenssubparameter specifies the number of hidden neurons for each hidden layer in the neural network feedforward model.The
inputssubparameter specifies that the variablesCLAGE,DEBTINC,DEROG,JOB,LOAN,MORTDUE,REASON, andVALUEbe used as inputs.The
nominalssubparameter specifies that the variablesBAD,JOB, andREASONbe used as nominal variables.The
saveStatesubparameter names the data table for saving the analytic store.The
seedsubparameter specifies the seed to use to perform sampling and partitioning tasks.The
stdsubparameter specifies the standardization to use on the interval variables.The
tablesubparameter names the input data table for training.The
targetsubparameter specifies that the variableBADbe used as the target variable.The
targetActsubparameter specifies the SOFTMAX function as the activation function for the neurons in the output layer.
Mitigating Bias of a Neural Network Model
This section contains Python code for the analysis in the CASL version of this example, which contains details about the results.
For more information about coding in Python, see Getting Started with SAS Viya for Python and SAS Viya: System Programming Guide.
This example illustrates the mitigateBiasNeuralNet action by using the home equity data that are described in the CASL version of the example.
The following statements use the mitigateBiasNeuralNet action to run the exponentiated gradient reduction (EGR) algorithm in order to train a model that has reduced demographic parity bias, which is calculated on the sensitive attribute JOB. Note that the syntax of the trainOptions parameter here is the same as the syntax of the neuralNet.annTrain action.
s.fairaitools.mitigatebiasneuralnet(
mitigateOptions=dict(biasMetric='DEMOGRAPHICPARITY',
predictedVariables=['P_BAD1', 'P_BAD0'],
responseLevels=['1', '0'],
seed='12345',
sensitiveVariable='JOB'),
trainOptions=dict(combs=['LINEAR'],
encodeName='True',
errorFunc='ENTROPY',
hiddens=[2],
inputs=['CLAGE',
'DEBTINC',
'DEROG',
'JOB',
'LOAN',
'MORTDUE',
'REASON',
'VALUE'],
nominals=['BAD',
'JOB',
'REASON'],
saveState=dict(name='hmeq_nnet_astore',
replace='True'),
seed='54321',
std='MIDRANGE',
table='hmeq',
target='BAD',
targetAct='SOFTMAX'))
The mitigateOptions parameter includes the following subparameters:
The
biasMetricsubparameter specifies that the demographic parity be used as the bias measurement.The
predictedVariablessubparameter specifies that the variablesP_BAD0andP_BAD1be used as the model predictions. A predicted variable is given for each level of the target variable.The
responseLevelssubparameter specifies the levels of the target that correspond to each variable that is listed in thepredictedVariablessubparameter. The order of the response levels must match the order of the predicted variables.The
seedsubparameter specifies the seed to use for pseudorandom number generation.The
sensitiveVariablesubparameter specifies that the variableJOBbe used as the sensitive variable in assessing and mitigating bias.
The trainOptions parameter includes the following subparameters that are passed to the neuralNet.annTrain action call:
The
combssubparameter specifies the LINEAR combination function for the neurons in each hidden layer.The
encodeNamesubparameter specifies that the variable names of the predicted probabilities of the target be encoded.The
errorFuncsubparameter specifies the error function to train the network. ENTROPY is the default setting for nominal targets.The
hiddenssubparameter specifies the number of hidden neurons for each hidden layer in the neural network feedforward model.The
inputssubparameter specifies that the variablesCLAGE,DEBTINC,DEROG,JOB,LOAN,MORTDUE,REASON, andVALUEbe used as inputs.The
nominalssubparameter specifies that the variablesBAD,JOB, andREASONbe used as nominal variables.The
saveStatesubparameter names the data table for saving the analytic store.The
seedsubparameter specifies the seed to use to perform sampling and partitioning tasks.The
stdsubparameter specifies the standardization to use on the interval variables.The
tablesubparameter names the input data table for training.The
targetsubparameter specifies that the variableBADbe used as the target variable.The
targetActsubparameter specifies the SOFTMAX function as the activation function for the neurons in the output layer.
Mitigating Bias of a Neural Network Model
This section contains R code for the analysis in the CASL version of this example, which contains details about the results.
For more information about coding in R, see Getting Started with SAS Viya for R and SAS Viya: System Programming Guide.
This example illustrates the mitigateBiasNeuralNet action by using the home equity data that are described in the CASL version of the example.
The following statements use the mitigateBiasNeuralNet action to run the exponentiated gradient reduction (EGR) algorithm in order to train a model that has reduced demographic parity bias, which is calculated on the sensitive attribute JOB. Note that the syntax of the trainOptions parameter here is the same as the syntax of the neuralNet.annTrain action.
cas.fairAITools.mitigateBiasNeuralNet(s,
mitigateOptions=list(biasMetric='DEMOGRAPHICPARITY',
predictedVariables=c('P_BAD1', 'P_BAD0'),
responseLevels=c('1', '0'),
seed='12345',
sensitiveVariable='JOB'),
trainOptions=list(combs=c('LINEAR'),
encodeName='True',
errorFunc='ENTROPY',
hiddens=c(2),
inputs=c('CLAGE',
'DEBTINC',
'DEROG',
'JOB',
'LOAN',
'MORTDUE',
'REASON',
'VALUE'),
nominals=c('BAD',
'JOB',
'REASON'),
saveState=list(name='hmeq_nnet_astore',
replace='True'),
seed='54321',
std='MIDRANGE',
table='hmeq',
target='BAD',
targetAct='SOFTMAX'))
The mitigateOptions parameter includes the following subparameters:
The
biasMetricsubparameter specifies that the demographic parity be used as the bias measurement.The
predictedVariablessubparameter specifies that the variablesP_BAD0andP_BAD1be used as the model predictions. A predicted variable is given for each level of the target variable.The
responseLevelssubparameter specifies the levels of the target that correspond to each variable that is listed in thepredictedVariablessubparameter. The order of the response levels must match the order of the predicted variables.The
seedsubparameter specifies the seed to use for pseudorandom number generation.The
sensitiveVariablesubparameter specifies that the variableJOBbe used as the sensitive variable in assessing and mitigating bias.
The trainOptions parameter includes the following subparameters that are passed to the neuralNet.annTrain action call:
The
combssubparameter specifies the LINEAR combination function for the neurons in each hidden layer.The
encodeNamesubparameter specifies that the variable names of the predicted probabilities of the target be encoded.The
errorFuncsubparameter specifies the error function to train the network. ENTROPY is the default setting for nominal targets.The
hiddenssubparameter specifies the number of hidden neurons for each hidden layer in the neural network feedforward model.The
inputssubparameter specifies that the variablesCLAGE,DEBTINC,DEROG,JOB,LOAN,MORTDUE,REASON, andVALUEbe used as inputs.The
nominalssubparameter specifies that the variablesBAD,JOB, andREASONbe used as nominal variables.The
saveStatesubparameter names the data table for saving the analytic store.The
seedsubparameter specifies the seed to use to perform sampling and partitioning tasks.The
stdsubparameter specifies the standardization to use on the interval variables.The
tablesubparameter names the input data table for training.The
targetsubparameter specifies that the variableBADbe used as the target variable.The
targetActsubparameter specifies the SOFTMAX function as the activation function for the neurons in the output layer.