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 biasMetric subparameter specifies that the demographic parity be used as the bias measurement.

  • The predictedVariables subparameter specifies that the variables P_BAD0 and P_BAD1 be used as the model predictions. A predicted variable is given for each level of the target variable.

  • The responseLevels subparameter specifies the levels of the target that correspond to each variable that is listed in the predictedVariables subparameter. The order of the response levels must match the order of the predicted variables.

  • The seed subparameter specifies the seed to use for pseudorandom number generation.

  • The sensitiveVariable subparameter specifies that the variable JOB be 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 combs subparameter specifies the LINEAR combination function for the neurons in each hidden layer.

  • The encodeName subparameter specifies that the variable names of the predicted probabilities of the target be encoded.

  • The errorFunc subparameter specifies the error function to train the network. ENTROPY is the default setting for nominal targets.

  • The hiddens subparameter specifies the number of hidden neurons for each hidden layer in the neural network feedforward model.

  • The inputs subparameter specifies that the variables CLAGE, DEBTINC, DEROG, JOB, LOAN, MORTDUE, REASON, and VALUE be used as inputs.

  • The nominals subparameter specifies that the variables BAD, JOB, and REASON be used as nominal variables.

  • The saveState subparameter names the data table for saving the analytic store.

  • The seed subparameter specifies the seed to use to perform sampling and partitioning tasks.

  • The std subparameter specifies the standardization to use on the interval variables.

  • The table subparameter names the input data table for training.

  • The target subparameter specifies that the variable BAD be used as the target variable.

  • The targetAct subparameter 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

Results from fairAITools.mitigateBiasNeuralNet

Mitigation Information
DescriptionCharacter ValueValue
Bias MetricDEMOGRAPHICPARITY.
Sensitive VariableJOB.
ResponseBAD.
Bound Value100100
Learning Rate0.010.01
Maximum Iteration1010
Tolerance0.0050.005
Event Level1.


Output 15.15.2: Iteration History

EGR Iteration History
IterationDemographic Parity
(Statistical Parity)
Misclassification
Rate
Multi-Class
Log Loss
10.0366970.08630.2861
20.0183490.08790.2897
30.0183490.08740.2889
40.0183490.08680.2884
50.0183490.08680.2879
60.0183490.08740.2888
70.0020940.08770.2906


Output 15.15.3: Group Metrics History

Group Metrics
IterationJOBNumber of ObservationsTrue Positive
Rate
False Positive
Rate
Area Under ROCMisclassification
Rate
Multi-Class
Log Loss
Best Kolmogorov-Smirnov
Along ROC
Cumulative LiftCumulative ResponseProportion Into
Event Level
Average Prediction
for Event
1Mgr4710.04260.00000.65200.09550.29430.28623.404334.04260.00260.1137
 Office6180.00000.00000.53350.06310.22740.06664.359043.58970.00000.0669
 Other14100.05480.00400.54760.10140.32990.09132.191821.91780.00540.0951
 ProfExe9250.01670.00000.54950.06380.23020.09882.666726.66670.00080.0675
 Sales640.25000.00000.62040.18750.52660.31253.125031.25000.03670.1309
 Self1280.00000.00000.53570.10940.34180.07144.285742.85710.00000.0677
2Mgr4710.00000.00000.63520.09980.30750.26503.404334.04260.00000.0827
 Office6180.00000.00000.52560.06310.23040.05133.333333.33330.00000.0682
 Other14100.04110.00400.57490.10280.32620.09821.780817.80820.00460.1119
 ProfExe9250.01670.00000.54110.06380.23490.08222.166721.66670.00080.0677
 Sales640.12500.00000.62500.21880.60170.25002.500025.00000.01830.0984
 Self1280.00000.00000.53570.10940.35010.07144.285742.85710.00000.0674
3Mgr4710.04260.00000.64100.09550.30070.26973.404334.04260.00260.0914
 Office6180.00000.00000.52560.06310.22890.05133.589735.89740.00000.0665
 Other14100.04110.00400.54850.10280.32910.09051.986319.86300.00460.1179
 ProfExe9250.01670.00000.54110.06380.23310.08222.500025.00000.00080.0662
 Sales640.12500.00000.62500.21880.58480.25003.125031.25000.01830.1035
 Self1280.00000.00000.53570.10940.34750.07144.285742.85710.00000.0659
4Mgr4710.04260.00000.69100.09550.29870.35733.404334.04260.00260.0988
 Office6180.00000.00000.52560.06310.22840.05133.589735.89740.00000.0652
 Other14100.05480.00400.59490.10140.32910.16102.123321.23290.00540.1124
 ProfExe9250.01670.00000.54110.06380.23240.08222.666726.66670.00080.0651
 Sales640.12500.00000.62500.21880.58930.25003.125031.25000.01830.0999
 Self1280.00000.00000.53570.10940.34640.07144.285742.85710.00000.0650
5Mgr4710.04260.00000.63520.09550.29870.28623.404334.04260.00260.1137
 Office6180.00000.00000.54640.06310.22760.09224.871848.71790.00000.0655
 Other14100.05480.00400.60590.10140.32910.20232.260322.60270.00540.1078
 ProfExe9250.01670.00000.54940.06380.23140.09882.666726.66670.00080.0655
 Sales640.12500.00000.65630.21880.58980.31253.125031.25000.01830.0984
 Self1280.00000.00000.53570.10940.34320.07144.285742.85710.00000.0658
6Mgr4710.04260.00000.68740.09550.29960.32683.404334.04260.00260.1007
 Office6180.00000.00000.52560.06310.22900.05134.359043.58970.00000.0644
 Other14100.04110.00400.59460.10280.32800.18212.260322.60270.00460.1051
 ProfExe9250.01670.00000.54110.06380.23280.08222.833328.33330.00080.0643
 Sales640.12500.00000.62500.21880.61740.25003.125031.25000.01830.0876
 Self1280.00000.00000.53570.10940.34660.07144.285742.85710.00000.0646
7Mgr4710.00000.00000.63090.09980.30530.24843.404334.04260.00000.0866
 Office6180.00000.00000.52560.06310.23090.05134.102641.02560.00000.0647
 Other14100.03420.00000.54880.10000.32600.09341.849318.49320.00210.0924
 ProfExe9250.00000.00000.51670.06490.23510.03332.833328.33330.00000.0642
 Sales640.00000.00000.62500.25000.65900.25003.125031.25000.00000.0758
 Self1280.00000.00000.53570.10940.35110.07145.714357.14290.00000.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 biasMetric subparameter specifies that the demographic parity be used as the bias measurement.

  • The predictedVariables subparameter specifies that the variables P_BAD0 and P_BAD1 be used as the model predictions. A predicted variable is given for each level of the target variable.

  • The responseLevels subparameter specifies the levels of the target that correspond to each variable that is listed in the predictedVariables subparameter. The order of the response levels must match the order of the predicted variables.

  • The seed subparameter specifies the seed to use for pseudorandom number generation.

  • The sensitiveVariable subparameter specifies that the variable JOB be 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 combs subparameter specifies the LINEAR combination function for the neurons in each hidden layer.

  • The encodeName subparameter specifies that the variable names of the predicted probabilities of the target be encoded.

  • The errorFunc subparameter specifies the error function to train the network. ENTROPY is the default setting for nominal targets.

  • The hiddens subparameter specifies the number of hidden neurons for each hidden layer in the neural network feedforward model.

  • The inputs subparameter specifies that the variables CLAGE, DEBTINC, DEROG, JOB, LOAN, MORTDUE, REASON, and VALUE be used as inputs.

  • The nominals subparameter specifies that the variables BAD, JOB, and REASON be used as nominal variables.

  • The saveState subparameter names the data table for saving the analytic store.

  • The seed subparameter specifies the seed to use to perform sampling and partitioning tasks.

  • The std subparameter specifies the standardization to use on the interval variables.

  • The table subparameter names the input data table for training.

  • The target subparameter specifies that the variable BAD be used as the target variable.

  • The targetAct subparameter 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 biasMetric subparameter specifies that the demographic parity be used as the bias measurement.

  • The predictedVariables subparameter specifies that the variables P_BAD0 and P_BAD1 be used as the model predictions. A predicted variable is given for each level of the target variable.

  • The responseLevels subparameter specifies the levels of the target that correspond to each variable that is listed in the predictedVariables subparameter. The order of the response levels must match the order of the predicted variables.

  • The seed subparameter specifies the seed to use for pseudorandom number generation.

  • The sensitiveVariable subparameter specifies that the variable JOB be 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 combs subparameter specifies the LINEAR combination function for the neurons in each hidden layer.

  • The encodeName subparameter specifies that the variable names of the predicted probabilities of the target be encoded.

  • The errorFunc subparameter specifies the error function to train the network. ENTROPY is the default setting for nominal targets.

  • The hiddens subparameter specifies the number of hidden neurons for each hidden layer in the neural network feedforward model.

  • The inputs subparameter specifies that the variables CLAGE, DEBTINC, DEROG, JOB, LOAN, MORTDUE, REASON, and VALUE be used as inputs.

  • The nominals subparameter specifies that the variables BAD, JOB, and REASON be used as nominal variables.

  • The saveState subparameter names the data table for saving the analytic store.

  • The seed subparameter specifies the seed to use to perform sampling and partitioning tasks.

  • The std subparameter specifies the standardization to use on the interval variables.

  • The table subparameter names the input data table for training.

  • The target subparameter specifies that the variable BAD be used as the target variable.

  • The targetAct subparameter 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 biasMetric subparameter specifies that the demographic parity be used as the bias measurement.

  • The predictedVariables subparameter specifies that the variables P_BAD0 and P_BAD1 be used as the model predictions. A predicted variable is given for each level of the target variable.

  • The responseLevels subparameter specifies the levels of the target that correspond to each variable that is listed in the predictedVariables subparameter. The order of the response levels must match the order of the predicted variables.

  • The seed subparameter specifies the seed to use for pseudorandom number generation.

  • The sensitiveVariable subparameter specifies that the variable JOB be 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 combs subparameter specifies the LINEAR combination function for the neurons in each hidden layer.

  • The encodeName subparameter specifies that the variable names of the predicted probabilities of the target be encoded.

  • The errorFunc subparameter specifies the error function to train the network. ENTROPY is the default setting for nominal targets.

  • The hiddens subparameter specifies the number of hidden neurons for each hidden layer in the neural network feedforward model.

  • The inputs subparameter specifies that the variables CLAGE, DEBTINC, DEROG, JOB, LOAN, MORTDUE, REASON, and VALUE be used as inputs.

  • The nominals subparameter specifies that the variables BAD, JOB, and REASON be used as nominal variables.

  • The saveState subparameter names the data table for saving the analytic store.

  • The seed subparameter specifies the seed to use to perform sampling and partitioning tasks.

  • The std subparameter specifies the standardization to use on the interval variables.

  • The table subparameter names the input data table for training.

  • The target subparameter specifies that the variable BAD be used as the target variable.

  • The targetAct subparameter specifies the SOFTMAX function as the activation function for the neurons in the output layer.

Last updated: August 04, 2026