Fair AI Tools Action Set

Mitigate Demographic Parity for a Nominal Target Variable with a Nominal Sensitive Variable

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 model in order to reduce bias according to the demographic parity metric by using the data set simdata that is described in Example 15.1.

The following statements use the mitigateBias action to run the EGR algorithm in order to train a model with reduced demographic parity bias, calculated on the sensitive attribute mySensitiveVariable:

proc cas;
fairAITools.mitigateBias /
    biasMetric="DEMOGRAPHICPARITY",
    bound="200",
    cutoff="0.33",
    event="A",
    learningRate="0.01",
    maxIters="10",
    predictedVariables={"P_nominalTargetA", "P_nominalTargetB",
                        "P_nominalTargetC"},
    response="nominalTarget",
    responseLevels={"A", "B", "C"},
    seed="12345",
    sensitiveVariable="mySensitiveVariable",
    table="simdata",
    tolerance="0.005",
    trainProgram="
         decisionTree.gbtreeTrain result=train_res /
            table=table,
            weight=weight,
            target=""nominalTarget"",
            inputs={
               ""linearPredictor"",
               ""absolutePredictor"",
               ""quadraticPredictor"",
               ""sinusoidPredictor""
            },
            nominals={""nominalTarget""},
            nBins=50,
            quantileBin=True,
            maxLevel=5,
            maxBranch=2,
            leafSize=5,
            missing=""USEINSEARCH"",
            minUseInSearch=1,
            binOrder=True,
            varImp=True,
            mergeBin=True,
            encodeName=True,
            nTree=500,
            seed=12345,
            ridge=1 ,
            savestate={
               name =""gb_astore"",
               replace=True
            }
         ;
         astore.score result=score_res /
            table=table,
            rstore=""gb_astore"",
            casout=casout,
            copyVars=copyVars
          ;
      ";
run;

The table parameter names the input reference data table. The response parameter specifies the name of the target variable nominalTarget for the training model. The sensitiveVariable parameter specifies that the variable mySensitiveVariable be used as the sensitive variable in assessing bias and mitigating bias. The trainProgram parameter specifies the CASL program that contains the training and scoring code. The predictedVariables parameter specifies that the variables P_nominalTargetA, P_nominalTargetB, and P_nominalTargetC be used as the model predictions. A predicted variable is given for each level of the response variable. The responseLevels parameter specifies the levels of the target that correspond to each variable listed in the predictedVariables parameter. The order of the response levels must match the order of the predicted variables. The event parameter specifies that the level 'A' be used as the event level for the response variable. The cutoff parameter specifies the threshold of the prediction to be counted as the event. In this example, any observation whose prediction of P_nominalTargetA is greater than or equal to 0.33 is counted as an event. The biasMetric parameter specifies that the fairness measurement is demographic parity. The bound parameter specifies the value of the hyperparameter bound in the EGR algorithm. The learningRate parameter specifies the value of the learning rate in the EGR algorithm. If the tuneBound parameter is set to True, the learningRate parameter should be set to 0.01, because the bound value tuner uses the fixed learning rate of 0.01 initially. The maxIters parameter specifies the maximum number of iterations of the EGR algorithm. The seed parameter specifies the seed to use for pseudorandom number generation. The tolerance parameter specifies the threshold for constraint violations. Because constraint violations are calculated by using the difference in bias metrics between the individual sensitive attribute and the overall population, the tolerance value refers to the maximum difference in bias metrics between the individual sensitive attribute and the overall population. When the tolerance condition is satisfied, the EGR algorithm stops, and in this case, the number of iterations of the EGR algorithm might be less than the specified maxIters parameter value.

The "Mitigation Information" table that these statements produce is shown in Output 15.10.1. The table shows some key information about the model and values of the key hyperparameters that the EGR algorithm uses. The "Constraint History" table that these statements produce is shown in Output 15.10.2. The table displays the iteration history of demographic parity of each sensitive attribute, the demographic parity of the whole population, the constraint violations Gamma, the overall misclassification rate, the overall multiclass log loss, and the portion of the input data set that each sensitive attribute level occupies. In this table, "Proportion Into Event Level for Sensitive Level" refers to demographic parity for each subgroup of the variable mySensitiveVariable. From the table, you can see that without bias mitigation, the maximum demographic parity gap among those three sensitive attributes is about 37%, and after 10 iterations of the EGR algorithm it has been reduced to about 1%. Meanwhile, the overall misclassification rate increases about 2%. Compared to the previous bias mitigation examples, the drop in accuracy in this example is somewhat larger, but the behavior is expected, because having more constraints decreases the feasible solution space and the solutions are less optimal.

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

Output 15.10.1: Mitigation Information

Results from fairAITools.mitigateBias

Mitigation Information
DescriptionCharacter ValueValue
Bias MetricDEMOGRAPHICPARITY.
Sensitive VariablemySensitiveVariable.
ResponsenominalTarget.
Bound Value200200
Learning Rate0.010.01
Maximum Iteration1010
Tolerance0.0050.005
Event LevelA.


Output 15.10.2: Constraint History

Constraint History
IterationmySensitiveVariableProportion Into Event
Level for Sensitive
Level
Overall Proportion
Into Event Level
Gamma MaxGamma MinOverall Misclassification
Rate
Overall Multi-Class
Log Loss
Proportion
1ComparisonGroup0.4321670.3201600.112007-0.1120070.50090.81140.333333
 HighPriorGroup0.0792710.320160-0.2408890.2408890.50090.81140.333333
 IncompleteGroup0.4490420.3201600.128882-0.1288820.50090.81140.333333
2ComparisonGroup0.4081150.2844790.123635-0.1236350.50550.81730.333333
 HighPriorGroup0.0795940.284479-0.2048850.2048850.50550.81730.333333
 IncompleteGroup0.3657290.2844790.081250-0.0812500.50550.81730.333333
3ComparisonGroup0.3752810.2528060.122476-0.1224760.50260.82940.333333
 HighPriorGroup0.0779900.252806-0.1748160.1748160.50260.82940.333333
 IncompleteGroup0.3051460.2528060.052340-0.0523400.50260.82940.333333
4ComparisonGroup0.3269170.2213720.105545-0.1055450.49650.88470.333333
 HighPriorGroup0.0778020.221372-0.1435690.1435690.49650.88470.333333
 IncompleteGroup0.2593960.2213720.038024-0.0380240.49650.88470.333333
5ComparisonGroup0.3463750.3272950.019080-0.0190800.54911.00950.333333
 HighPriorGroup0.3913440.3272950.064049-0.0640490.54911.00950.333333
 IncompleteGroup0.2441670.327295-0.0831280.0831280.54911.00950.333333
6ComparisonGroup0.3025310.2646670.037865-0.0378650.51370.95530.333333
 HighPriorGroup0.2187500.264667-0.0459170.0459170.51370.95530.333333
 IncompleteGroup0.2727190.2646670.008052-0.0080520.51370.95530.333333
7ComparisonGroup0.3113650.3028960.008469-0.0084690.53260.98820.333333
 HighPriorGroup0.3275210.3028960.024625-0.0246250.53260.98820.333333
 IncompleteGroup0.2698020.302896-0.0330940.0330940.53260.98820.333333
8ComparisonGroup0.2958130.2792260.016587-0.0165870.52640.97260.333333
 HighPriorGroup0.2593440.279226-0.0198820.0198820.52640.97260.333333
 IncompleteGroup0.2825210.2792260.003295-0.0032950.52640.97260.333333
9ComparisonGroup0.2970630.2943920.002670-0.0026700.52990.98410.333333
 HighPriorGroup0.3040000.2943920.009608-0.0096080.52990.98410.333333
 IncompleteGroup0.2821150.294392-0.0122780.0122780.52990.98410.333333
10ComparisonGroup0.2926670.2866840.005983-0.0059830.52680.97780.333333
 HighPriorGroup0.2812920.286684-0.0053920.0053920.52680.97780.333333
 IncompleteGroup0.2860940.286684-0.0005900.0005900.52680.97780.333333


Output 15.10.3: Group Metrics History

Group Metrics
IterationmySensitiveVariableNumber 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
1ComparisonGroup960000.87090.21690.91490.43390.78760.65372.930029.29970.43220.3293
 HighPriorGroup960000.60920.04000.95250.60800.78810.81437.031170.31110.07930.0688
 IncompleteGroup960000.82770.26210.87200.46070.85860.56492.711627.11630.44900.3301
2ComparisonGroup960000.84400.19420.91370.43580.79240.65122.929029.29020.40810.3076
 HighPriorGroup960000.61250.04010.95510.61940.78950.81947.070470.70370.07960.0690
 IncompleteGroup960000.73240.18470.87000.46130.86990.56192.708227.08160.36570.2822
3ComparisonGroup960000.79800.16780.91020.43330.80440.64272.926229.26170.37530.2837
 HighPriorGroup960000.59770.03950.95440.60390.79180.82087.046270.46210.07800.0684
 IncompleteGroup960000.64740.13620.86630.47050.89190.55812.707527.07530.30510.2487
4ComparisonGroup960000.72390.13210.89360.44120.85120.61712.925229.25220.32690.2812
 HighPriorGroup960000.55100.04270.91070.56860.87480.67186.268562.68500.07780.1452
 IncompleteGroup960000.57450.10380.85140.47980.92810.54002.702227.02170.25940.2325
5ComparisonGroup960000.65330.19580.79590.47140.94120.45812.905029.04970.34640.3123
 HighPriorGroup960000.55930.37890.62330.67101.10750.18941.813718.13650.39130.3130
 IncompleteGroup960000.50440.11570.79440.50500.97980.41312.658826.58840.24420.2338
6ComparisonGroup960000.64840.13280.83390.45730.91280.52132.919529.19530.30250.2904
 HighPriorGroup960000.51250.19700.73320.60801.01680.35293.035330.35340.21880.2489
 IncompleteGroup960000.58100.12050.82870.47570.93640.49602.709727.09740.27270.2498
7ComparisonGroup960000.62010.15990.79650.47030.94170.45682.899628.99590.31140.2961
 HighPriorGroup960000.51810.31340.64230.64551.07370.22272.006920.06950.32750.2910
 IncompleteGroup960000.55920.12690.81250.48190.94910.45942.689926.89880.26980.2498
8ComparisonGroup960000.61920.13710.81150.46770.93410.48622.906929.06870.29580.2877
 HighPriorGroup960000.49740.24170.68300.62711.04280.27652.410124.10150.25930.2667
 IncompleteGroup960000.58960.13090.82340.48430.94100.48542.698726.98710.28250.2563
9ComparisonGroup960000.60290.14700.79310.47230.94490.45672.898028.98010.29710.2898
 HighPriorGroup960000.49380.28990.64560.63951.06520.22262.016020.16010.30400.2841
 IncompleteGroup960000.58090.13460.81670.47800.94230.46962.687726.87680.28210.2562
10ComparisonGroup960000.60400.13990.80030.47130.94100.46832.908829.08770.29270.2866
 HighPriorGroup960000.49260.26560.66220.63201.05250.24462.158021.57960.28130.2749
 IncompleteGroup960000.58990.13610.82060.47720.93990.47822.685826.85790.28610.2587


Mitigate Demographic Parity for a Nominal Target Variable with a Nominal Sensitive Variable

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 trains a model in order to reduce bias according to the demographic parity metric by using the data set simdata that is described in Example 15.1.

The following statements use the mitigateBias action to run the EGR algorithm in order to train a model with reduced demographic parity bias, calculated on the sensitive attribute mySensitiveVariable:

fairAITools.mitigateBias{
   table='simdata',
   predictedVariables={
      'P_nominalTargetA',
      'P_nominalTargetB',
      'P_nominalTargetC'
   },
   response='nominalTarget',
   responseLevels={'A', 'B', 'C'},
   event='A',
   sensitiveVariable='mySensitiveVariable',
   biasMetric='DEMOGRAPHICPARITY',
   learningRate=0.01,
   bound=200,
   tolerance=0.005,
   maxIters=10,
   cutoff=0.33,
   seed=12345,
   trainProgram= '
      decisionTree.gbtreeTrain result=train_res /
         table=table,
         weight=weight,
         target="nominalTarget",
         inputs={
            "linearPredictor",
            "absolutePredictor",
            "quadraticPredictor",
            "sinusoidPredictor"
         },
         nominals={"nominalTarget"},
         nBins=50,
         quantileBin=True,
         maxLevel=5,
         maxBranch=2,
         leafSize=5,
         missing="USEINSEARCH",
         minUseInSearch=1,
         binOrder=True,
         varImp=True,
         mergeBin=True,
         encodeName=True,
         nTree=500,
         seed=12345,
         ridge=1 ,
         savestate={
            name ="gb_astore",
            replace=True
         }
      ;
      astore.score result=score_res /
         table=table,
         rstore="gb_astore",
         casout=casout,
         copyVars=copyVars
       ;
   '
}

The table parameter names the input reference data table. The response parameter specifies the name of the target variable nominalTarget for the training model. The sensitiveVariable parameter specifies that the variable mySensitiveVariable be used as the sensitive variable in assessing bias and mitigating bias. The trainProgram parameter specifies the CASL program that contains the training and scoring code. The predictedVariables parameter specifies that the variables P_nominalTargetA, P_nominalTargetB, and P_nominalTargetC be used as the model predictions. A predicted variable is given for each level of the response variable. The responseLevels parameter specifies the levels of the target that correspond to each variable listed in the predictedVariables parameter. The order of the response levels must match the order of the predicted variables. The event parameter specifies that the level 'A' be used as the event level for the response variable. The cutoff parameter specifies the threshold of the prediction to be counted as the event. In this example, any observation whose prediction of P_nominalTargetA is greater than or equal to 0.33 is counted as an event. The biasMetric parameter specifies that the fairness measurement is demographic parity. The bound parameter specifies the value of the hyperparameter bound in the EGR algorithm. The learningRate parameter specifies the value of the learning rate in the EGR algorithm. If the tuneBound parameter is set to True, the learningRate parameter should be set to 0.01, because the bound value tuner uses the fixed learning rate of 0.01 initially. The maxIters parameter specifies the maximum number of iterations of the EGR algorithm. The seed parameter specifies the seed to use for pseudorandom number generation. The tolerance parameter specifies the threshold for constraint violations. Because constraint violations are calculated by using the difference in bias metrics between the individual sensitive attribute and the overall population, the tolerance value refers to the maximum difference in bias metrics between the individual sensitive attribute and the overall population. When the tolerance condition is satisfied, the EGR algorithm stops, and in this case, the number of iterations of the EGR algorithm might be less than the specified maxIters parameter value.

Mitigate Demographic Parity for a Nominal Target Variable with a Nominal Sensitive Variable

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 trains a model in order to reduce bias according to the demographic parity metric by using the data set simdata that is described in Example 15.1.

The following statements use the mitigateBias action to run the EGR algorithm in order to train a model with reduced demographic parity bias, calculated on the sensitive attribute mySensitiveVariable:

s.fairaitools.mitigatebias(
    biasMetric='DEMOGRAPHICPARITY',
    bound='200',
    cutoff='0.33',
    event='A',
    learningRate='0.01',
    maxIters='10',
    predictedVariables=['P_nominalTargetA', 'P_nominalTargetB',
                        'P_nominalTargetC'],
    response='nominalTarget',
    responseLevels=['A', 'B', 'C'],
    seed='12345',
    sensitiveVariable='mySensitiveVariable',
    table='simdata',
    tolerance='0.005',
    trainProgram='''
         decisionTree.gbtreeTrain result=train_res /
            table=table,
            weight=weight,
            target="nominalTarget",
            inputs={
               "linearPredictor",
               "absolutePredictor",
               "quadraticPredictor",
               "sinusoidPredictor"
            },
            nominals={"nominalTarget"},
            nBins=50,
            quantileBin=True,
            maxLevel=5,
            maxBranch=2,
            leafSize=5,
            missing="USEINSEARCH",
            minUseInSearch=1,
            binOrder=True,
            varImp=True,
            mergeBin=True,
            encodeName=True,
            nTree=500,
            seed=12345,
            ridge=1 ,
            savestate={
               name ="gb_astore",
               replace=True
            }
         ;
         astore.score result=score_res /
            table=table,
            rstore="gb_astore",
            casout=casout,
            copyVars=copyVars
          ;
      ''')

The table parameter names the input reference data table. The response parameter specifies the name of the target variable nominalTarget for the training model. The sensitiveVariable parameter specifies that the variable mySensitiveVariable be used as the sensitive variable in assessing bias and mitigating bias. The trainProgram parameter specifies the CASL program that contains the training and scoring code. The predictedVariables parameter specifies that the variables P_nominalTargetA, P_nominalTargetB, and P_nominalTargetC be used as the model predictions. A predicted variable is given for each level of the response variable. The responseLevels parameter specifies the levels of the target that correspond to each variable listed in the predictedVariables parameter. The order of the response levels must match the order of the predicted variables. The event parameter specifies that the level 'A' be used as the event level for the response variable. The cutoff parameter specifies the threshold of the prediction to be counted as the event. In this example, any observation whose prediction of P_nominalTargetA is greater than or equal to 0.33 is counted as an event. The biasMetric parameter specifies that the fairness measurement is demographic parity. The bound parameter specifies the value of the hyperparameter bound in the EGR algorithm. The learningRate parameter specifies the value of the learning rate in the EGR algorithm. If the tuneBound parameter is set to True, the learningRate parameter should be set to 0.01, because the bound value tuner uses the fixed learning rate of 0.01 initially. The maxIters parameter specifies the maximum number of iterations of the EGR algorithm. The seed parameter specifies the seed to use for pseudorandom number generation. The tolerance parameter specifies the threshold for constraint violations. Because constraint violations are calculated by using the difference in bias metrics between the individual sensitive attribute and the overall population, the tolerance value refers to the maximum difference in bias metrics between the individual sensitive attribute and the overall population. When the tolerance condition is satisfied, the EGR algorithm stops, and in this case, the number of iterations of the EGR algorithm might be less than the specified maxIters parameter value.

Mitigate Demographic Parity for a Nominal Target Variable with a Nominal Sensitive Variable

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 trains a model in order to reduce bias according to the demographic parity metric by using the data set simdata that is described in Example 15.1.

The following statements use the mitigateBias action to run the EGR algorithm in order to train a model with reduced demographic parity bias, calculated on the sensitive attribute mySensitiveVariable:

cas.fairAITools.mitigateBias(s,
    biasMetric='DEMOGRAPHICPARITY',
    bound='200',
    cutoff='0.33',
    event='A',
    learningRate='0.01',
    maxIters='10',
    predictedVariables=c('P_nominalTargetA', 'P_nominalTargetB',
                         'P_nominalTargetC'),
    response='nominalTarget',
    responseLevels=c('A', 'B', 'C'),
    seed='12345',
    sensitiveVariable='mySensitiveVariable',
    table='simdata',
    tolerance='0.005',
    trainProgram='
         decisionTree.gbtreeTrain result=train_res /
            table=table,
            weight=weight,
            target="nominalTarget",
            inputs={
               "linearPredictor",
               "absolutePredictor",
               "quadraticPredictor",
               "sinusoidPredictor"
            },
            nominals={"nominalTarget"},
            nBins=50,
            quantileBin=True,
            maxLevel=5,
            maxBranch=2,
            leafSize=5,
            missing="USEINSEARCH",
            minUseInSearch=1,
            binOrder=True,
            varImp=True,
            mergeBin=True,
            encodeName=True,
            nTree=500,
            seed=12345,
            ridge=1 ,
            savestate={
               name ="gb_astore",
               replace=True
            }
         ;
         astore.score result=score_res /
            table=table,
            rstore="gb_astore",
            casout=casout,
            copyVars=copyVars
          ;
      ')

The table parameter names the input reference data table. The response parameter specifies the name of the target variable nominalTarget for the training model. The sensitiveVariable parameter specifies that the variable mySensitiveVariable be used as the sensitive variable in assessing bias and mitigating bias. The trainProgram parameter specifies the CASL program that contains the training and scoring code. The predictedVariables parameter specifies that the variables P_nominalTargetA, P_nominalTargetB, and P_nominalTargetC be used as the model predictions. A predicted variable is given for each level of the response variable. The responseLevels parameter specifies the levels of the target that correspond to each variable listed in the predictedVariables parameter. The order of the response levels must match the order of the predicted variables. The event parameter specifies that the level 'A' be used as the event level for the response variable. The cutoff parameter specifies the threshold of the prediction to be counted as the event. In this example, any observation whose prediction of P_nominalTargetA is greater than or equal to 0.33 is counted as an event. The biasMetric parameter specifies that the fairness measurement is demographic parity. The bound parameter specifies the value of the hyperparameter bound in the EGR algorithm. The learningRate parameter specifies the value of the learning rate in the EGR algorithm. If the tuneBound parameter is set to True, the learningRate parameter should be set to 0.01, because the bound value tuner uses the fixed learning rate of 0.01 initially. The maxIters parameter specifies the maximum number of iterations of the EGR algorithm. The seed parameter specifies the seed to use for pseudorandom number generation. The tolerance parameter specifies the threshold for constraint violations. Because constraint violations are calculated by using the difference in bias metrics between the individual sensitive attribute and the overall population, the tolerance value refers to the maximum difference in bias metrics between the individual sensitive attribute and the overall population. When the tolerance condition is satisfied, the EGR algorithm stops, and in this case, the number of iterations of the EGR algorithm might be less than the specified maxIters parameter value.

Last updated: July 02, 2026