Fair AI Tools Action Set

Mitigating Bias of a Gradient Boosting Tree 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 gradient boosting tree 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 REASON 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 reason 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 mitigateBiasGradientBoostTree 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 REASON. Note that the syntax of the trainOptions parameter here is the same as the syntax of the decisionTree.gbtreeTrain action.

proc cas;
fairAITools.mitigateBiasGradientBoostTree /
    mitigateOptions={biasMetric="DEMOGRAPHICPARITY",
                     predictedVariables={"P_BAD1", "P_BAD0"},
                     responseLevels={"1", "0"},
                     seed="12345",
                     sensitiveVariable="REASON"},
    trainOptions={casOut={name="gbtreeOut",
                          replace="true"},
                  encodeName="True",
                  inputs={"CLAGE",
                          "DEBTINC",
                          "DEROG",
                          "JOB",
                          "LOAN",
                          "MORTDUE",
                          "REASON",
                          "VALUE"},
                  maxLevel="11",
                  nBins="20",
                  nominals={"BAD",
                          "JOB",
                          "REASON"},
                  saveState={name="hmeq_gbt_astore",
                          replace="True"},
                  table="hmeq",
                  target="BAD"};
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 REASON be used as the sensitive variable in assessing and mitigating bias.

The trainOptions parameter includes the following subparameters that are passed to the decisionTree.gbtreeTrain action call:

  • The casOut subparameter specifies the output table that contains the gradient boosting tree model.

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

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

  • The maxLevel subparameter specifies the maximum tree level.

  • The nBins subparameter specifies the number of bins to use for numeric variables in calculating the decision tree.

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

  • The saveState subparameter specifies the table to store the generated analytic store model.

  • 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 "Mitigation Information" table that these statements produce is shown in Output 15.14.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.14.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.2%, and after 10 iterations of the EGR algorithm it has been reduced to about 1.9%. Meanwhile, the overall accuracy of the model does not change significantly.

The "Group Metrics" table that these statements produce is shown in Output 15.14.3. The table displays the model assessment statistics and average prediction for each sensitive attribute during each iteration of the EGR algorithm.

Output 15.14.1: Mitigation Information

Results from fairAITools.mitigateBiasGradientBoostTree

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


Output 15.14.2: Iteration History

EGR Iteration History
IterationDemographic Parity
(Statistical Parity)
Misclassification
Rate
Multi-Class
Log Loss
10.0324050.03630.1210
20.0340470.03590.1226
30.0278140.03660.1241
40.0250660.03800.1252
50.0222050.03870.1245
60.0189830.03990.1271
70.0228720.03820.1242
80.0229680.03990.1257
90.0152090.03990.1291
100.0192380.04430.1275


Output 15.14.3: Group Metrics History

Group Metrics
IterationREASONNumber 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
1DebtCon39280.86040.01450.99080.03820.12410.90045.275252.75170.17490.1864
 HomeImp17800.89390.01080.99450.03200.11410.91884.494944.94950.20730.2170
2DebtCon39280.86310.01670.99070.03950.12620.89555.248352.48320.17720.1884
 HomeImp17800.91160.01080.99460.02810.11460.92644.494944.94950.21120.2164
3DebtCon39280.86440.01570.99020.03840.12770.89615.288652.88590.17670.1879
 HomeImp17800.88640.00940.99400.03260.11590.92314.494944.94950.20450.2137
4DebtCon39280.86850.01890.98980.04020.12910.88835.261752.61740.18000.1902
 HomeImp17800.88640.01010.99440.03310.11680.92064.494944.94950.20510.2137
5DebtCon39280.86710.01850.99010.04020.12880.89565.248352.48320.17950.1891
 HomeImp17800.87370.00940.99420.03540.11490.91884.494944.94950.20170.2106
6DebtCon39280.86040.01850.98990.04150.13000.89185.261752.61740.17820.1905
 HomeImp17800.86110.00720.99430.03650.12070.92104.494944.94950.19720.2098
7DebtCon39280.86980.01920.99030.04020.12780.89585.234952.34900.18050.1906
 HomeImp17800.88130.00940.99480.03370.11630.92284.494944.94950.20340.2108
8DebtCon39280.85770.01980.98970.04300.12970.89245.261752.61740.17870.1907
 HomeImp17800.87880.00790.99460.03310.11660.92854.494944.94950.20170.2080
9DebtCon39280.86850.01850.99000.04000.13170.89215.208152.08050.17970.1919
 HomeImp17800.84850.00790.99240.03990.12360.91664.494944.94950.19490.2040
10DebtCon39280.84560.02170.98940.04680.13120.89125.261752.61740.17800.1925
 HomeImp17800.85610.00870.99380.03880.11930.91844.494944.94950.19720.2072


Mitigating Bias of a Gradient Boosting Tree 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 mitigateBiasGradientBoostTree action by using the home equity data that are described in the CASL version of this example.

The following statements use the mitigateBiasGradientBoostTree 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 REASON. Note that the syntax of the trainOptions parameter here is the same as the syntax of the decisionTree.gbtreeTrain action.

mitigateOptions={biasMetric="DEMOGRAPHICPARITY",
                 predictedVariables={"P_BAD1", "P_BAD0"},
                 responseLevels={"1", "0"},
                 seed=12345,
                 sensitiveVariable="REASON"},
trainOptions={casOut={
               name="gbtreeOut",
               replace=true
            },
            encodeName=True,
            inputs={"CLAGE","DEBTINC","DEROG","JOB","LOAN","MORTDUE","REASON","VALUE"},
            maxLevel=11,
            nBins=20,
            nominals={"BAD", "JOB", "REASON"},
            saveState={
               name="hmeq_gbt_astore",
               replace=True},
            table="hmeq",
            target="BAD"
}

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 REASON be used as the sensitive variable in assessing and mitigating bias.

The trainOptions parameter includes the following subparameters that are passed to the decisionTree.gbtreeTrain action call:

  • The casOut subparameter specifies the output table that contains the gradient boosting tree model.

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

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

  • The maxLevel subparameter specifies the maximum tree level.

  • The nBins subparameter specifies the number of bins to use for numeric variables in calculating the decision tree.

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

  • The saveState subparameter specifies the table to store the generated analytic store model.

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

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

Mitigating Bias of a Gradient Boosting Tree 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 mitigateBiasGradientBoostTree action by using the home equity data that are described in the CASL version of this example.

The following statements use the mitigateBiasGradientBoostTree 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 REASON. Note that the syntax of the trainOptions parameter here is the same as the syntax of the decisionTree.gbtreeTrain action.

s.fairaitools.mitigatebiasgradientboosttree(
    mitigateOptions=dict(biasMetric='DEMOGRAPHICPARITY',
                         predictedVariables=['P_BAD1', 'P_BAD0'],
                         responseLevels=['1', '0'],
                         seed='12345',
                         sensitiveVariable='REASON'),
    trainOptions=dict(casOut=dict(name='gbtreeOut',
                                  replace='true'),
                      encodeName='True',
                      inputs=['CLAGE',
                                  'DEBTINC',
                                  'DEROG',
                                  'JOB',
                                  'LOAN',
                                  'MORTDUE',
                                  'REASON',
                                  'VALUE'],
                      maxLevel='11',
                      nBins='20',
                      nominals=['BAD',
                                  'JOB',
                                  'REASON'],
                      saveState=dict(name='hmeq_gbt_astore',
                                  replace='True'),
                      table='hmeq',
                      target='BAD'))

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 REASON be used as the sensitive variable in assessing and mitigating bias.

The trainOptions parameter includes the following subparameters that are passed to the decisionTree.gbtreeTrain action call:

  • The casOut subparameter specifies the output table that contains the gradient boosting tree model.

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

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

  • The maxLevel subparameter specifies the maximum tree level.

  • The nBins subparameter specifies the number of bins to use for numeric variables in calculating the decision tree.

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

  • The saveState subparameter specifies the table to store the generated analytic store model.

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

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

Mitigating Bias of a Gradient Boosting Tree 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 mitigateBiasGradientBoostTree action by using the home equity data that are described in the CASL version of this example.

The following statements use the mitigateBiasGradientBoostTree 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 REASON. Note that the syntax of the trainOptions parameter here is the same as the syntax of the decisionTree.gbtreeTrain action.

cas.fairAITools.mitigateBiasGradientBoostTree(s,
    mitigateOptions=list(biasMetric='DEMOGRAPHICPARITY',
                         predictedVariables=c('P_BAD1', 'P_BAD0'),
                         responseLevels=c('1', '0'),
                         seed='12345',
                         sensitiveVariable='REASON'),
    trainOptions=list(casOut=list(name='gbtreeOut',
                                  replace='true'),
                      encodeName='True',
                      inputs=c('CLAGE',
                                  'DEBTINC',
                                  'DEROG',
                                  'JOB',
                                  'LOAN',
                                  'MORTDUE',
                                  'REASON',
                                  'VALUE'),
                      maxLevel='11',
                      nBins='20',
                      nominals=c('BAD',
                                  'JOB',
                                  'REASON'),
                      saveState=list(name='hmeq_gbt_astore',
                                  replace='True'),
                      table='hmeq',
                      target='BAD'))

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 REASON be used as the sensitive variable in assessing and mitigating bias.

The trainOptions parameter includes the following subparameters that are passed to the decisionTree.gbtreeTrain action call:

  • The casOut subparameter specifies the output table that contains the gradient boosting tree model.

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

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

  • The maxLevel subparameter specifies the maximum tree level.

  • The nBins subparameter specifies the number of bins to use for numeric variables in calculating the decision tree.

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

  • The saveState subparameter specifies the table to store the generated analytic store model.

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

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

Last updated: July 02, 2026