12.1 Pre-Training and Post-Training Bias with SageMaker Clarify
Key Takeaways
- SageMaker Clarify executes as a managed SageMaker Processing Job via ClarifyProcessor, evaluating datasets and model artifacts stored in Amazon S3 to generate bias reports and SHAP feature attributions.
- Pre-training bias metrics evaluate training data fairness before model training, including Class Imbalance (CI) for subgroup sample counts and Difference in Positive Proportions in Labels (DPL) for historical favorable label disparities.
- Post-training bias metrics evaluate model inference predictions, including Difference in Positive Proportions in Predicted Labels (DPPL) and Disparate Impact (DI), where DI < 0.8 violates the EEOC four-fifths rule for adverse impact.
- SageMaker Clarify implements Kernel SHAP to compute global feature importance across entire datasets and local feature attributions for individual inference decisions against a synthetic or clustered baseline dataset.
- Clarify bias and explainability configurations (DataConfig, BiasConfig, ModelConfig, ModelPredictedLabelConfig, SHAPConfig) output analysis.json, PDF/HTML reports, and baseline constraints used for Model Monitor production drift tracking.
Pre-Training and Post-Training Bias with SageMaker Clarify
As machine learning systems increasingly automate high-stakes decisions—such as consumer credit approvals, insurance underwriting, medical triage, employment candidate screening, and criminal risk assessments—ensuring model fairness, transparency, and accountability has transitioned from an ethical ideal to a strict legal and regulatory imperative. Machine learning models trained on historical data often inherit, amplify, and perpetuate systemic societal biases.
On the AWS Certified Machine Learning Engineer — Associate (MLA-C01) exam, you must demonstrate comprehensive expertise in measuring bias and explaining model predictions across the machine learning lifecycle using Amazon SageMaker Clarify. You will be tested on distinguishing pre-training data bias from post-training prediction bias, calculating and interpreting mathematical fairness metrics (such as Class Imbalance, Difference in Positive Proportions, and Disparate Impact), understanding SHAP (SHapley Additive exPlanations) for global and local explainability, and selecting appropriate baseline reference datasets.
1. The AWS Responsible AI Framework & SageMaker Clarify Architecture
AWS structures Responsible Artificial Intelligence around five core foundational pillars:
- Fairness: Ensuring models do not disproportionately disadvantage specific demographic groups defined by protected attributes (e.g., race, gender, age, religion, national origin).
- Explainability & Interpretability: Providing human-understandable insights into both overall model behavior (global explainability) and individual prediction decisions (local explainability).
- Robustness & Reliability: Ensuring model performance remains stable under distributional shifts, adversarial inputs, and unexpected edge cases.
- Privacy & Security: Safeguarding sensitive personal information and intellectual property throughout data ingestion, training, and inference.
- Governance & Transparency: Documenting model lineage, evaluation results, intended use cases, risk assessments, and compliance audits.
+--------------------------------------------------------------------------------------------------+
| SAGEMAKER CLARIFY ARCHITECTURAL WORKFLOW |
| |
| [Amazon S3 Input Bucket] |
| - Training / Validation Dataset (CSV/JSONLines/Parquet) |
| - Baseline Dataset (Background reference samples) |
| | |
| v |
| +------------------------------------------------------------------------------------------+ |
| | SAGEMAKER PROCESSING JOB (ClarifyProcessor Cluster) | |
| | | |
| | 1. Pre-Training Bias Engine 2. Model Scoring Engine 3. SHAP Explainability | |
| | - Evaluates label column - Invokes model artifact - Perturbs features | |
| | against Facet (Protected or endpoint to get against baseline | |
| | demographic attribute) predictions (y_hat) - Computes Shapley | |
| | - Computes CI, DPL, KL, JS - Computes DPPL, DI, DCA local & global values |
| +------------------------------------------------------------------------------------------+ |
| | |
| v |
| [Amazon S3 Output Bucket] |
| - analysis.json (Machine-readable metric values & thresholds) |
| - report.pdf / report.html (Executive visual bias and feature attribution reports) |
| - shap_attributions.csv (Local per-record SHAP explanation values) |
| - analysis_config.json (Baseline constraints for SageMaker Model Monitor drift tracking) |
+--------------------------------------------------------------------------------------------------+
Clarify Execution Mechanics
SageMaker Clarify does not run as a persistent server. Instead, it executes as a managed SageMaker Processing Job using the specialized ClarifyProcessor container image provided by AWS:
- Serverless & Ephemeral: SageMaker provisions the requested instance cluster (e.g.,
ml.c5.xlarge,ml.m5.2xlarge), mounts input datasets from Amazon S3, executes bias algorithms or SHAP kernel computations in parallel, uploads evaluation reports to S3, and immediately tears down the compute fleet. - Model Integration Options: To compute post-training bias and SHAP explainability, Clarify can evaluate predictions by either:
- Spawning a temporary, ephemeral shadow model container inside the Processing Job (using
ModelConfig), eliminating the cost of hosting a live real-time endpoint. - Querying an existing, active SageMaker real-time or serverless endpoint (using
EndpointInputConfig).
- Spawning a temporary, ephemeral shadow model container inside the Processing Job (using
2. Pre-Training Bias Metrics (Data Level)
Pre-training bias metrics analyze the raw training dataset before any model training occurs. They evaluate whether historical data collection practices, sampling errors, or societal disparities have introduced bias into the dataset features or ground-truth target labels ($y$).
Key Definitions:
- Facet ($A$ or $D$): The sensitive or protected demographic feature column (e.g.,
gender,age_group,ethnicity,postal_code). - Privileged / Favored Facet ($a$): The demographic subgroup historically favored or overrepresented (e.g.,
gender = 1for male in historical loan data). - Unprivileged / Disfavored Facet ($d$): The demographic subgroup historically disadvantaged or underrepresented (e.g.,
gender = 0for female). - Positive / Favorable Label ($y = 1$): The desirable business outcome (e.g., loan approved, job applicant hired, bail granted).
- Negative / Unfavorable Label ($y = 0$): The undesirable outcome (e.g., loan rejected, application denied).
+--------------------------------------------------------------------------------------------------+
| PRE-TRAINING BIAS METRICS SUMMARY |
| |
| Metric Name Mathematical Formula Ideal Value Range |
| ---------------------------------- ---------------------------- ------------ ------------- |
| Class Imbalance (CI) (n_a - n_d) / (n_a + n_d) 0.0 [-1.0, 1.0] |
| Difference in Positive Proportions q_a - q_d 0.0 [-1.0, 1.0] |
| in Labels (DPL) |
| Kullback-Leibler Divergence (KL) sum(P_a * ln(P_a / P_d)) 0.0 [0.0, +inf) |
| Jensen-Shannon Divergence (JS) 0.5*KL(P_a||M) + 0.5*KL(P_d||M) 0.0 [0.0, ln(2)] |
| Conditional Demographic sum(w_i * (q_a,i - q_d,i)) 0.0 [-1.0, 1.0] |
| Disparity in Labels (CDDL) |
+--------------------------------------------------------------------------------------------------+
2.1 Class Imbalance (CI)
Class Imbalance (CI) measures the disparity in total sample representation between the privileged group ($n_a$) and the unprivileged group ($n_d$) in the dataset:
- Interpretation:
- $CI = 0.0$: Perfect balance (equal number of samples from both demographic groups).
- $CI > 0$: Privileged group $a$ has more samples than unprivileged group $d$ (e.g., $CI = 0.60$ means group $a$ represents 80% of the dataset and group $d$ represents 20%).
- $CI < 0$: Unprivileged group $d$ has more samples.
- Mitigation: Resample dataset using stratified sampling, SMOTE (Synthetic Minority Over-sampling Technique), or targeted data collection.
2.2 Difference in Positive Proportions in Labels (DPL)
Difference in Positive Proportions in Labels (DPL) measures the difference in the rate of favorable historical labels ($y = 1$) between the privileged group ($q_a$) and unprivileged group ($q_d$):
- Interpretation:
- $DPL = 0.0$: Both demographic groups received historical positive outcomes at identical rates.
- $DPL > 0$: The privileged group received a higher proportion of positive outcomes in the training data (indicates historical label bias).
- $DPL < 0$: The unprivileged group received a higher proportion of positive outcomes.
2.3 Conditional Demographic Disparity in Labels (CDDL)
In many business contexts, demographic disparities may be correlated with legitimate business variables (such as income level, credit history, or education). CDDL measures whether label disparities persist across demographic facets after conditioning on confounding subgroup variables:
Where $w_i$ is the demographic weight of subgroup stratum $i$. If CDDL remains positive after conditioning on valid underwriting attributes, unfair label bias exists independently of qualifications.
3. Post-Training Bias Metrics (Model Prediction Level)
Post-training bias metrics analyze the model's generated predictions ($\hat{y}$) against ground truth ($y$) on a validation or test dataset. They evaluate whether the trained algorithm treats demographic groups equitably or introduces algorithmic discrimination.
+--------------------------------------------------------------------------------------------------+
| POST-TRAINING BIAS METRICS SUMMARY |
| |
| Metric Name Mathematical Formula Ideal Value Range |
| ---------------------------------- ---------------------------- ------------ ------------- |
| Difference in Positive Proportions r_a - r_d 0.0 [-1.0, 1.0] |
| in Predicted Labels (DPPL) |
| Disparate Impact (DI) r_d / r_a 1.0 [0.0, +inf) |
| Difference in Conditional q_a^(y=1) - q_d^(y=1) 0.0 [-1.0, 1.0] |
| Acceptance (DCA) (True Positive Rate parity) |
| Difference in Conditional q_a^(y=0) - q_d^(y=0) 0.0 [-1.0, 1.0] |
| Rejection (DCR) (False Positive Rate parity) |
| Difference in Accuracy (DA) ACC_a - ACC_d 0.0 [-1.0, 1.0] |
| Recall Difference (RD) Recall_a - Recall_d 0.0 [-1.0, 1.0] |
| Counterfactual Fliptest (FT) Fliptest prediction rate 0.0 [0.0, 1.0] |
+--------------------------------------------------------------------------------------------------+
3.1 Difference in Positive Proportions in Predicted Labels (DPPL)
DPPL measures the difference in the rate at which the model assigns favorable predicted outcomes ($\hat{y} = 1$) to the privileged group ($r_a$) versus the unprivileged group ($r_d$):
- If $DPPL > 0$, the model approves privileged candidates at a higher rate than unprivileged candidates.
3.2 Disparate Impact (DI) & The 80% (Four-Fifths) Rule
Disparate Impact (DI) is the foundational legal and regulatory standard (established by the US Equal Employment Opportunity Commission - EEOC) for detecting adverse impact. It calculates the ratio of the selection rate for the unprivileged group to the selection rate for the privileged group:
+--------------------------------------------------------------------------------------------------+
| DISPARATE IMPACT (DI) REGULATORY SPECTRUM |
| |
| [0.0 ---------------- 0.80] ----------- [1.0] ----------- [1.25 -----------------> +inf] |
| ADVERSE IMPACT PERFECT FAIRNESS ADVERSE IMPACT |
| Against Unprivileged Subgroup (DI = 1.0) Against Privileged Subgroup |
| (Violates 80% Rule) (Reverse Discrimination) |
+--------------------------------------------------------------------------------------------------+
- Interpretation Rules for the MLA-C01 Exam:
- $DI = 1.0$: Perfect demographic parity (both groups are selected at exactly the same rate).
- $0.80 \le DI \le 1.25$: Acceptable selection parity under the 80% (four-fifths) rule.
- $DI < 0.80$: Adverse impact against the unprivileged group ($d$). For example, if $r_a = 60%$ and $r_d = 30%$, $DI = 0.30 / 0.60 = 0.50$, indicating severe adverse impact requiring remediation.
- $DI > 1.25$: Adverse impact against the privileged group ($a$).
3.3 Difference in Conditional Acceptance (DCA) & Difference in Accuracy (DA)
- Difference in Conditional Acceptance (DCA): Evaluates parity in True Positive Rates (Sensitivity). Measures whether qualified individuals in the unprivileged group are approved at the same rate as qualified individuals in the privileged group.
- Difference in Accuracy (DA): Measures whether the model's overall prediction accuracy ($ACC = \frac{TP+TN}{TP+TN+FP+FN}$) differs significantly between demographic groups ($DA = ACC_a - ACC_d$). A large DA indicates the model performs poorly for minority subgroups due to inadequate representation.
- Counterfactual Fliptest (FT): Replaces the sensitive demographic feature in a test sample (e.g., changes
gender=maletogender=femalewhile holding all other financial features constant) and measures whether the model flips its prediction from approved to rejected.
4. Model Explainability with SHAP (SHapley Additive exPlanations)
Understanding why a complex machine learning model (such as a Gradient Boosted Tree or Deep Neural Network) generated a specific prediction is essential for debugging, regulatory compliance (e.g., adverse action notices in credit lending), and stakeholder trust.
Game-Theoretic Foundation of Shapley Values
SHAP is rooted in cooperative game theory (introduced by Lloyd Shapley). In game theory, a group of players collaborates in a game to earn a total payout. The Shapley value provides a mathematically unique method to fairly distribute the payout among players proportional to their marginal contribution across all possible player coalitions.
In machine learning:
- The Game: The model making a prediction for a single instance $x$.
- The Players: The individual input features ($x_1, x_2, \dots, x_M$).
- The Payout: The difference between the model's actual prediction $f(x)$ and the expected baseline prediction $E[f(x)]$.
- The Shapley Value ($\phi_i$): The feature attribution value representing how much feature $i$ increased or decreased the prediction relative to the baseline.
+--------------------------------------------------------------------------------------------------+
| LOCAL SHAP FORCE ATTRIBUTION BREAKDOWN |
| |
| Baseline Average Expected Prediction: E[f(x)] = 0.50 (50% Approval Rate) |
| |
| Feature Attributions (Pushing Higher / Approved): |
| + FICO Score = 760 [ +0.22 ] =======================> |
| + Annual Income = $120,000 [ +0.15 ] ==============> |
| |
| Feature Attributions (Pushing Lower / Rejected): |
| - Debt-to-Income = 45% [ -0.18 ] <================== |
| - Delinquencies = 1 [ -0.05 ] <===== |
| |
| Final Model Prediction Output: f(x) = 0.50 + 0.22 + 0.15 - 0.18 - 0.05 = 0.64 (64% Approved) |
+--------------------------------------------------------------------------------------------------+
Global vs. Local Explainability
| Explainability Dimension | Scope & Purpose | Computational Method | Output Visualization & Use Case |
|---|---|---|---|
| Global Explainability | Explains overall model behavior across the entire population dataset. Identifies which features have the greatest overall influence on predictions. | Aggregates absolute SHAP values across all $N$ records in the dataset: $I_j = \frac{1}{N} \sum_{k=1}^N | \phi_j^{(k)} |
| Local Explainability | Explains a single, specific prediction for an individual customer or transaction. | Calculates individual $\phi_i$ values for one specific input vector $x$. | SHAP Force Plots & Waterfall Plots: Used for customer adverse action notices (e.g., explaining the top 3 factors why a loan was denied). |
Kernel SHAP & Baseline Dataset Selection
SageMaker Clarify uses Kernel SHAP, a model-agnostic explainability algorithm that treats any model as a black box. To measure the impact of missing features in coalitions ($S$), Kernel SHAP replaces "absent" features with values drawn from a Baseline (Background) Dataset:
- The Role of the Baseline ($x_0$): The baseline serves as the non-informative reference distribution representing "neutral" feature values ($E[f(x)]$).
- Baseline Construction Options:
- Synthetic Central Baseline (1 record): Uses the median (numerical features) and mode (categorical features) of the training dataset. Fast and computationally cheap, but may create unrealistic feature combinations.
- Clustered Baseline (10–50 records): Uses K-Means clustering to select representative centroid samples from the training set. Highly recommended as the best balance between attribution accuracy and compute speed.
- Random Sample Baseline (50–100 records): A random sample of background training rows. More accurate but increases compute time proportionally ($O(N \cdot M)$).
5. Python SDK Implementation: SageMaker Clarify Processing Job
import boto3
import sagemaker
from sagemaker import clarify
session = sagemaker.Session()
role = sagemaker.get_execution_role()
s3_bucket = session.default_bucket()
prefix = "clarify-bias-explainability"
# 1. Initialize the Clarify Processor
clarify_processor = clarify.SageMakerClarifyProcessor(
role=role,
instance_count=1,
instance_type="ml.c5.xlarge",
sagemaker_session=session
)
# 2. Configure Data Inputs and Outputs
data_config = clarify.DataConfig(
s3_data_input_path=f"s3://{s3_bucket}/{prefix}/train_data.csv",
s3_output_path=f"s3://{s3_bucket}/{prefix}/output/",
label="loan_approved", # Target column name
headers=["applicant_age", "income", "credit_score", "gender", "loan_approved"],
dataset_type="text/csv"
)
# 3. Configure Bias Parameters (Protected demographic facet)
bias_config = clarify.BiasConfig(
label_values_or_threshold=[1], # Favorable outcome: 1 (Approved)
facet_name="gender", # Protected attribute
facet_values_or_threshold=[0], # 0 = Unprivileged (Female), 1 = Privileged (Male)
group_name="applicant_age" # Optional subgroup for CDDL
)
# 4. Configure Model Connection (Ephemeral model container)
model_config = clarify.ModelConfig(
model_name="credit-risk-xgb-v1",
instance_type="ml.c5.xlarge",
instance_count=1,
content_type="text/csv",
accept_type="text/csv"
)
model_predictions_config = clarify.ModelPredictedLabelConfig(
probability_threshold=0.5 # Threshold for binary classification
)
# 5. Configure SHAP Explainability
shap_config = clarify.SHAPConfig(
baseline=f"s3://{s3_bucket}/{prefix}/baseline_kmeans_centroids.csv",
num_samples=50, # Number of Kernel SHAP perturbation samples
agg_method="mean_abs" # Global aggregation: mean absolute SHAP
)
# 6. Execute Full Pre/Post-Training Bias and Explainability Analysis
clarify_processor.run_bias_and_explainability(
data_config=data_config,
bias_config=bias_config,
model_config=model_config,
model_predicted_label_config=model_predictions_config,
explainability_config=shap_config
)
[!TIP] Exam Rapid Decision Rules:
- If the question asks to detect fairness issues before training $\rightarrow$ Pre-training bias metrics (Class Imbalance - CI, Difference in Positive Proportions in Labels - DPL).
- If the question asks to check the ratio of selection rates against the 80% four-fifths rule $\rightarrow$ Disparate Impact (DI) where $DI < 0.8$ indicates adverse impact.
- If the question asks to provide individual adverse action reasons for a denied applicant $\rightarrow$ Local SHAP feature attributions.
- If the question asks how Clarify runs $\rightarrow$ Managed SageMaker Processing Job (
ClarifyProcessor) producinganalysis.jsonand PDF reports in S3.
A data science team at a retail bank is preparing a mortgage approval training dataset containing 500,000 historical loan applications. Before training a gradient boosted tree model, the team needs to assess whether historical racial disparities exist in the target label column ('approved' = 1, 'denied' = 0) between the privileged applicant group and unprivileged applicant group. Which SageMaker Clarify pre-training bias metric specifically measures the difference in the proportion of favorable historical outcomes between these two groups?
An ML engineer runs a post-training bias analysis using SageMaker Clarify on a production loan underwriting model. The Clarify report shows that the model approves 60% of applicants from the privileged demographic group (r_a = 0.60) and 24% of applicants from the unprivileged demographic group (r_d = 0.24). What is the calculated Disparate Impact (DI) metric, and how should it be evaluated under the EEOC 80% (four-fifths) rule?
A fintech company deploys an automated credit limit scoring model on Amazon SageMaker. Regulatory guidelines require the company to generate automated adverse action disclosure notices explaining the specific top three negative factors that contributed to individual credit limit reduction decisions. Which SageMaker Clarify capability and configuration must the engineering team utilize to satisfy this requirement?
An ML engineer needs to configure a SageMaker Clarify job to compute SHAP feature attributions for a complex deep learning classification model. Because calculating Kernel SHAP values across 100,000 test records against the entire training dataset is computationally intractable and would result in excessive processing runtimes, what baseline configuration strategy represents the AWS best practice?