← Back to Table of Contents
Chapter 4

Model Bias Analysis

Once a model is trained, we must assess whether it exhibits bias in its predictions. This chapter covers techniques for analyzing trained models to detect and quantify bias across demographic groups.

Introduction to Model Analysis

Even with clean data, models can learn and amplify biases through the training process. Systematic model analysis is essential to ensure fair AI systems that benefit all users equally.

Disaggregated Performance Evaluation

The foundation of model bias detection is evaluating performance separately for each demographic group rather than only looking at aggregate metrics.

# Disaggregated Evaluation Framework
import numpy as np
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score

def disaggregated_evaluation(model, X_test, y_test, protected_attr):
    """
    Evaluate model performance separately for each group
    """
    groups = np.unique(protected_attr)
    y_pred = model.predict(X_test)

    print("DISAGGREGATED PERFORMANCE EVALUATION")
    print("=" * 80)
    print(f"{'Group':<15} {'Accuracy':<12} {'Precision':<12} {'Recall':<12} {'F1 Score':<12}")
    print("-" * 80)

    results = {}
    for group in groups:
        mask = protected_attr == group

        acc = accuracy_score(y_test[mask], y_pred[mask])
        prec = precision_score(y_test[mask], y_pred[mask], zero_division=0)
        rec = recall_score(y_test[mask], y_pred[mask], zero_division=0)
        f1 = f1_score(y_test[mask], y_pred[mask], zero_division=0)

        results[group] = {'accuracy': acc, 'precision': prec, 'recall': rec, 'f1': f1}
        print(f"{group:<15} {acc:<12.3f} {prec:<12.3f} {rec:<12.3f} {f1:<12.3f}")

    # Calculate disparities
    print("\n" + "=" * 80)
    print("PERFORMANCE DISPARITIES")
    print("-" * 80)

    for metric in ['accuracy', 'precision', 'recall', 'f1']:
        values = [r[metric] for r in results.values()]
        disparity = max(values) - min(values)
        status = "⚠️ HIGH" if disparity > 0.1 else "✓ OK"
        print(f"{metric.capitalize():<15} Disparity: {disparity:.3f} {status}")

    return results

Confusion Matrix Analysis by Group

Examining confusion matrices for each group reveals patterns in how errors are distributed.

# Group-Specific Confusion Matrices
from sklearn.metrics import confusion_matrix
import matplotlib.pyplot as plt

def confusion_matrix_by_group(y_true, y_pred, protected_attr):
    """
    Generate confusion matrices for each demographic group
    """
    groups = np.unique(protected_attr)

    print("CONFUSION MATRICES BY GROUP")
    print("=" * 60)

    for group in groups:
        mask = protected_attr == group
        cm = confusion_matrix(y_true[mask], y_pred[mask])

        tn, fp, fn, tp = cm.ravel() if cm.size == 4 else (0, 0, 0, 0)

        print(f"\n{group}:")
        print(f"  True Negatives:  {tn:>6}  |  False Positives: {fp:>6}")
        print(f"  False Negatives: {fn:>6}  |  True Positives:  {tp:>6}")

        # Calculate rates
        tpr = tp / (tp + fn) if (tp + fn) > 0 else 0
        fpr = fp / (fp + tn) if (fp + tn) > 0 else 0
        tnr = tn / (tn + fp) if (tn + fp) > 0 else 0
        fnr = fn / (fn + tp) if (fn + tp) > 0 else 0

        print(f"  TPR: {tpr:.3f}  FPR: {fpr:.3f}  TNR: {tnr:.3f}  FNR: {fnr:.3f}")

    return True

Decision Threshold Analysis

For probabilistic classifiers, the decision threshold can significantly impact fairness. Different thresholds may be needed for different groups to achieve fairness criteria.

# Threshold Optimization for Fairness
def analyze_thresholds(model, X_test, y_test, protected_attr, metric='tpr'):
    """
    Analyze how different thresholds affect fairness
    """
    y_proba = model.predict_proba(X_test)[:, 1]
    groups = np.unique(protected_attr)

    thresholds = np.linspace(0, 1, 101)
    results = {group: [] for group in groups}

    for threshold in thresholds:
        y_pred = (y_proba >= threshold).astype(int)

        for group in groups:
            mask = protected_attr == group

            if metric == 'tpr':
                tp = np.sum((y_test[mask] == 1) & (y_pred[mask] == 1))
                fn = np.sum((y_test[mask] == 1) & (y_pred[mask] == 0))
                value = tp / (tp + fn) if (tp + fn) > 0 else 0
            elif metric == 'fpr':
                fp = np.sum((y_test[mask] == 0) & (y_pred[mask] == 1))
                tn = np.sum((y_test[mask] == 0) & (y_pred[mask] == 0))
                value = fp / (fp + tn) if (fp + tn) > 0 else 0

            results[group].append(value)

    # Find optimal threshold for fairness
    min_disparity = float('inf')
    best_threshold = 0.5

    for i, threshold in enumerate(thresholds):
        values = [results[group][i] for group in groups]
        disparity = max(values) - min(values)
        if disparity < min_disparity:
            min_disparity = disparity
            best_threshold = threshold

    print(f"Optimal threshold for {metric.upper()} parity: {best_threshold:.3f}")
    print(f"Minimum disparity: {min_disparity:.3f}")

    return best_threshold, results

Feature Importance and SHAP Analysis

Understanding which features drive predictions helps identify potential sources of bias.

# Feature Importance Analysis
def analyze_feature_importance_by_group(model, X_test, protected_attr, feature_names):
    """
    Analyze if feature importance differs across groups
    """
    from sklearn.inspection import permutation_importance

    groups = np.unique(protected_attr)

    print("FEATURE IMPORTANCE BY GROUP")
    print("=" * 80)

    for group in groups:
        mask = protected_attr == group
        X_group = X_test[mask]
        y_group = y_test[mask]

        # Calculate permutation importance
        perm_importance = permutation_importance(
            model, X_group, y_group, n_repeats=10, random_state=42
        )

        print(f"\n{group} - Top 5 Most Important Features:")
        indices = perm_importance.importances_mean.argsort()[-5:][::-1]

        for idx in indices:
            importance = perm_importance.importances_mean[idx]
            print(f"  {feature_names[idx]:<30} {importance:.4f}")

    print("\n⚠️ Look for features that are important for one group but not others")
    print("   This may indicate differential treatment")

Calibration Analysis

A well-calibrated model's probability predictions should match observed frequencies across all groups.

# Calibration Analysis by Group
from sklearn.calibration import calibration_curve

def calibration_analysis(model, X_test, y_test, protected_attr, n_bins=10):
    """
    Assess calibration separately for each group
    """
    y_proba = model.predict_proba(X_test)[:, 1]
    groups = np.unique(protected_attr)

    print("CALIBRATION ANALYSIS")
    print("=" * 80)

    calibration_errors = {}

    for group in groups:
        mask = protected_attr == group

        prob_true, prob_pred = calibration_curve(
            y_test[mask],
            y_proba[mask],
            n_bins=n_bins,
            strategy='quantile'
        )

        # Calculate calibration error (ECE - Expected Calibration Error)
        calibration_error = np.mean(np.abs(prob_true - prob_pred))
        calibration_errors[group] = calibration_error

        print(f"\n{group}:")
        print(f"  Expected Calibration Error: {calibration_error:.4f}")

        print(f"  {'Predicted':<12} {'Actual':<12} {'Difference':<12}")
        print(f"  {'-'*36}")
        for pred, true in zip(prob_pred, prob_true):
            diff = abs(pred - true)
            print(f"  {pred:<12.3f} {true:<12.3f} {diff:<12.3f}")

    # Check for calibration disparities
    errors = list(calibration_errors.values())
    disparity = max(errors) - min(errors)

    print(f"\nCalibration error disparity: {disparity:.4f}")
    if disparity > 0.05:
        print("⚠️ Significant calibration disparity detected")

    return calibration_errors

Subgroup Analysis

Examine performance on specific subgroups that may be particularly vulnerable to bias.

# Subgroup Performance Analysis
def subgroup_analysis(model, X_test, y_test, protected_attrs):
    """
    Analyze performance across intersectional subgroups
    """
    # Create subgroup identifiers
    subgroups = X_test[protected_attrs].apply(
        lambda x: '_'.join(x.astype(str)), axis=1
    )

    unique_subgroups = subgroups.unique()
    y_pred = model.predict(X_test)

    print("SUBGROUP ANALYSIS")
    print("=" * 80)
    print(f"{'Subgroup':<30} {'Count':<10} {'Accuracy':<12} {'Precision':<12} {'Recall':<12}")
    print("-" * 80)

    subgroup_results = []

    for subgroup in unique_subgroups:
        mask = subgroups == subgroup
        count = mask.sum()

        if count < 10:  # Skip very small subgroups
            continue

        acc = accuracy_score(y_test[mask], y_pred[mask])
        prec = precision_score(y_test[mask], y_pred[mask], zero_division=0)
        rec = recall_score(y_test[mask], y_pred[mask], zero_division=0)

        subgroup_results.append({
            'subgroup': subgroup,
            'count': count,
            'accuracy': acc,
            'precision': prec,
            'recall': rec
        })

        print(f"{subgroup:<30} {count:<10} {acc:<12.3f} {prec:<12.3f} {rec:<12.3f}")

    # Identify worst-performing subgroups
    sorted_results = sorted(subgroup_results, key=lambda x: x['accuracy'])

    print("\n⚠️ Worst Performing Subgroups:")
    for result in sorted_results[:3]:
        print(f"  {result['subgroup']}: Accuracy = {result['accuracy']:.3f}")

    return subgroup_results

Counterfactual Fairness Testing

Test whether changing protected attributes (while keeping other features constant) changes predictions.

# Counterfactual Fairness Test
def counterfactual_test(model, X_test, protected_attr_col, protected_attr_values):
    """
    Test if predictions change when only protected attribute changes
    """
    X_counterfactual = X_test.copy()

    original_predictions = model.predict_proba(X_test)[:, 1]
    counterfactual_predictions = {}

    for value in protected_attr_values:
        X_counterfactual[protected_attr_col] = value
        counterfactual_predictions[value] = model.predict_proba(X_counterfactual)[:, 1]

    # Calculate how often predictions change
    changes = 0
    total = len(X_test)

    for i in range(total):
        preds = [counterfactual_predictions[v][i] for v in protected_attr_values]
        if max(preds) - min(preds) > 0.1:  # Significant change
            changes += 1

    change_rate = changes / total

    print(f"Counterfactual change rate: {change_rate:.3f}")
    if change_rate > 0.1:
        print("⚠️ Model is sensitive to protected attribute")
        print("   This suggests potential discrimination")

    return change_rate

Bias Amplification Detection

Check if the model amplifies biases present in the training data.

# Bias Amplification Analysis
def detect_bias_amplification(train_data, model, X_test, protected_attr):
    """
    Compare bias in training data vs model predictions
    """
    # Measure bias in training data
    train_positive_rates = {}
    for group in train_data[protected_attr].unique():
        mask = train_data[protected_attr] == group
        train_positive_rates[group] = train_data[mask]['label'].mean()

    # Measure bias in predictions
    y_pred = model.predict(X_test)
    test_protected = X_test[protected_attr]

    pred_positive_rates = {}
    for group in test_protected.unique():
        mask = test_protected == group
        pred_positive_rates[group] = y_pred[mask].mean()

    print("BIAS AMPLIFICATION ANALYSIS")
    print("=" * 80)
    print(f"{'Group':<15} {'Training %':<15} {'Prediction %':<15} {'Change':<15}")
    print("-" * 80)

    for group in train_positive_rates.keys():
        train_rate = train_positive_rates.get(group, 0) * 100
        pred_rate = pred_positive_rates.get(group, 0) * 100
        change = pred_rate - train_rate

        status = "⚠️ AMPLIFIED" if abs(change) > 5 else "✓ OK"
        print(f"{group:<15} {train_rate:<15.2f} {pred_rate:<15.2f} {change:+.2f}% {status}")

    return train_positive_rates, pred_positive_rates

Comprehensive Model Audit

# Complete Model Bias Audit
def comprehensive_model_audit(model, X_train, y_train, X_test, y_test,
                              protected_attrs, feature_names):
    """
    Perform complete bias audit of trained model
    """
    print("\n" + "=" * 80)
    print("COMPREHENSIVE MODEL BIAS AUDIT")
    print("=" * 80)

    # 1. Disaggregated Performance
    print("\n1. DISAGGREGATED PERFORMANCE")
    print("-" * 80)
    for attr in protected_attrs:
        disaggregated_evaluation(model, X_test, y_test, X_test[attr])

    # 2. Confusion Matrix Analysis
    print("\n2. CONFUSION MATRIX ANALYSIS")
    print("-" * 80)
    for attr in protected_attrs:
        confusion_matrix_by_group(y_test, model.predict(X_test), X_test[attr])

    # 3. Calibration Check
    print("\n3. CALIBRATION ANALYSIS")
    print("-" * 80)
    for attr in protected_attrs:
        calibration_analysis(model, X_test, y_test, X_test[attr])

    # 4. Subgroup Analysis
    if len(protected_attrs) > 1:
        print("\n4. INTERSECTIONAL SUBGROUP ANALYSIS")
        print("-" * 80)
        subgroup_analysis(model, X_test, y_test, protected_attrs)

    # Final Recommendations
    print("\n" + "=" * 80)
    print("AUDIT SUMMARY AND RECOMMENDATIONS")
    print("=" * 80)
    print("1. Review all performance disparities greater than 10%")
    print("2. Investigate groups with high error rates")
    print("3. Check for bias amplification")
    print("4. Consider threshold adjustments for fairness")
    print("5. Implement mitigation techniques if necessary")
    print("\n弘益人間 - Ensure your model serves all users fairly")
    print("=" * 80)

    return True

弘益人間 Model Analysis Principles

Chapter Summary

Review Questions

  1. Why is disaggregated evaluation more informative than aggregate metrics for fairness assessment?
  2. What insights can confusion matrix analysis provide that accuracy alone cannot?
  3. How can adjusting decision thresholds improve fairness? What are the tradeoffs?
  4. What does it mean if feature importance differs significantly across demographic groups?
  5. Explain how a model can be well-calibrated overall but poorly calibrated for specific groups.
  6. Why is subgroup analysis important? What can it reveal that single-attribute analysis cannot?
  7. What does counterfactual fairness testing measure? How is it different from other fairness metrics?
  8. How can a model amplify bias beyond what exists in training data? Provide an example mechanism.
  9. Design a model audit strategy for a criminal risk assessment system. What would you prioritize?
  10. How does 弘益人間 philosophy inform decisions about acceptable performance disparities?

Korea Industrial, Research, Education Infrastructure Mapping

Korea operates its industrial ecosystem and standardization system through the following core infrastructure. Korea Top 5 Groups: Samsung, Hyundai Motor, LG, SK, Lotte. Each group operates standardization committees and ISO/IEC TC Korean secretariats. Samsung Electronics (semiconductors, displays, home appliances, telecom)·Hyundai Motor (automobiles, mobility)·LG Electronics (home appliances, displays, OLED)·SK hynix (memory)·LG Energy Solution·Samsung SDI (batteries)·POSCO Future M (materials)·Hyundai Mobis (parts). Korean IT Big Tech: NAVER (search, cloud, AI HyperCLOVA)·Kakao (messenger, payment, mobility, banking)·Coupang (e-commerce, logistics)·Karrot Market·Toss·Woowa Brothers. Korea Telcos: SK Telecom·KT·LG U+. 5G·5G dedicated networks·B2B cloud·AI businesses operating. Korea Top 7 Research Universities: Seoul National University·KAIST·POSTECH·Yonsei University·Korea University·UNIST·DGIST·GIST. All serve as standardization R&D bases and ISO/IEC/IEEE Korean chairs. Korea Government-affiliated National Research Institutes (26): KIST, KAERI, KIMM, KIER, KFRI, KRICT, KRIBB, KARI, KASI, KIGAM, KICT, KISTI, KETI, ETRI, NIMS, KIMS, KISDI, KOTRA, STEPI, KOEN, KICCE, KIET, KIPF, KIHASA, KICJ, KLRI. Korea Industrial Complexes / Tech Valleys: Pangyo Techno Valley·Dongtan·Gwanggyo·Songdo IBD·Yeouido·Gangnam·Sihwa·Banwol·Gumi·Ulsan·Changwon·Geoje·Yeosu·Onsan·Cheongju·Iksan·Gwangyang·POSCO Gwangyang Steel Mill·Asan Bay·Seosan·Songdo·Incheon Airport·Sejong·Cheongna·Geomdan. Korea Trade and Finance Infrastructure: Korea International Trade Association (KITA)·Korea Trade-Investment Promotion Agency (KOTRA)·Export-Import Bank of Korea (KEXIM)·Bank of Korea·Kookmin Bank·Shinhan·Hana·Woori·NH Nonghyup·IBK Industrial Bank·SC First Bank·Citi Bank Korea·HSBC Korea·DBS Korea — 14 Korean major banks and foreign banks. Korea K-POP / K-Content: HYBE·SM·YG·JYP 4 major entertainment companies·CJ ENM·tvN·MBC·KBS·SBS·EBS·YTN·Yonhap News TV·JTBC Korean broadcasting·NETFLIX Korea·Disney Plus·TVING·Wavve·Watcha·Coupang Play. Korea Gaming Industry: Nexon·NCsoft·Krafton·Netmarble·Kakao Games·Pearl Abyss·Com2uS·Gamevil·NHN·Smilegate·Webzen. Korea Automotive / Battery: Hyundai Motor·Kia·Genesis·LG Energy Solution·Samsung SDI·SK On·POSCO Future M·EcoPro·L&F battery cathode material suppliers. Korea Semiconductor: Samsung Electronics (HBM3E·HBM4)·SK hynix (HBM3E 12-Hi)·DB HiTek·SK siltron·SK Enpulse·Dongjin Semichem·Seoul Semiconductor·Simmtech·Samsung Display·LG Display.

Korea Standardization Infrastructure Mapping

Korea operates a comprehensive standards governance system through inter-ministerial cooperation. National Standards Council (under Prime Minister's Office, per Framework Act on National Standards Article 5) coordinates KATS (Korean Agency for Technology and Standards), MFDS (Ministry of Food and Drug Safety), MOTIE (Ministry of Trade, Industry and Energy), MSIT (Ministry of Science and ICT), MOIS (Ministry of the Interior and Safety), MOE (Ministry of Environment), MOHW (Ministry of Health and Welfare), MND (Ministry of National Defense), MCST (Ministry of Culture, Sports and Tourism), MOFA (Ministry of Foreign Affairs), MOJ (Ministry of Justice), and FSC (Financial Services Commission). Accreditation and Testing: KOLAS (Korea Laboratory Accreditation Scheme) accredits 800+ testing laboratories. KAS (Korea Accreditation System) accredits 50+ certification bodies. KTC (Korea Testing Certification), KTR (Korea Testing & Research Institute), KTL (Korea Testing Laboratory), and KCL (Korea Conformity Laboratories) provide conformance testing. Telecom and Cyber: KCC (Korea Communications Commission), KCA (Korea Communications Agency), TTA (Telecommunications Technology Association), IITP (Institute for Information & Communications Technology Planning & Evaluation), NIPA (National IT Industry Promotion Agency), KISA (Korea Internet & Security Agency), KCMVP (Korea Cryptographic Module Validation Program), NIS (National Intelligence Service), NSR (National Security Research Institute), and NCSC (National Cyber Security Center). National R&D Centers: KIST, ETRI, KAIST, Seoul National University, Yonsei University, Korea University, POSTECH, UNIST, GIST, DGIST, KISTI, KIER, KIMM, KRICT, KFRI, KRIBB. International Standards Cooperation: ISO TC/SC Korean secretariats, IEC TC/SC Korean secretariats, ITU-T Study Group Korean chairs, 3GPP RAN/SA Korean chairs, IEEE 802 Korean chairs, W3C Korea office, OASIS Korea office, IETF Korea cooperation, OECD CSTP, UN ESCAP, APEC SCSC Korean cooperation. Korean Industrial Standards (KS) Catalog: KS X (Information) 25,000+, KS A (Basic) 15,000+, KS B (Machinery) 25,000+, KS C (Electrical) 18,000+, KS D (Metallurgy) 12,000+, KS E (Mining) 5,000+, KS F (Construction) 18,000+, KS H (Food) 8,000+, KS I (Environment) 5,000+, KS J (Biology) 3,000+, KS K (Textile) 15,000+, KS L (Ceramics) 7,000+, KS M (Chemistry) 12,000+, KS P (Medical) 5,000+, KS Q (Quality Mgmt) 4,000+, KS R (Transport) 12,000+, KS S (Service) 3,000+, KS T (Packaging) 4,000+, KS V (Shipbuilding) 5,000+, KS W (Aerospace) 3,000+ — totaling 220,000+ Korean Industrial Standards. Key Acts: Personal Information Protection Act (Act 19234, effective Sept 15, 2024), Electronic Government Act, Electronic Signature Act, Act on Promotion of Information and Communications Network Utilization and Information Protection, Information and Communications Infrastructure Protection Act, Data Industry Act, Public Data Act, AI Framework Act (Act 20212, effective July 2026), Industrial Technology Innovation Promotion Act, Framework Act on Science and Technology — 70+ Korean standardization-related laws.

Korea Digital Transformation Detailed Mapping

Korea operates digital transformation through a comprehensive governance system. Digital Government: Digital Platform Government Committee (established September 2022, under the President)·Ministry of the Interior and Safety Digital Government Bureau·e-Government Support Center·Gov.kr·National Citizen Service·KDIS (Korea Digital Information Society)·NIA (National Information Society Agency)·MOIS (Ministry of the Interior and Safety). K-DNS Infrastructure: Korea Internet & Security Agency (KISA) Korea Internet Center·KISA DNS Root Server·KRNIC (Korea Network Information Center)·BGP Korea·National Cyber Security Center (NCSC)·KCC (Korea Communications Commission)·MSIT (Ministry of Science and ICT)·NIA·NIPA. Korean Cloud Infrastructure: KT Cloud·NAVER Cloud (NCloud)·Samsung SDS Cloud·LG U+ Cloud·NHN Cloud·Kakao Enterprise Cloud·SK Telecom Cloud·KISA Cloud Security Assurance Program (CSAP)·KCMVP-validated cloud·ISMS-P (Information Security & Personal Information Management System). Korean Security Certifications: KISA ISMS-P certification·KCMVP (Korean Cryptographic Module Validation Program)·NIS (National Intelligence Service) "National Cryptographic Technology Operation Standards"·NCSC "National Cyber Security Strategy 2024-2028"·CC (Common Criteria) Korean evaluation bodies·EAL4·EAL5·KS X ISO/IEC 15408·19790·24759 Korean Profile. Korean Data Standards: NIA AI Hub·National Data Standardization Committee·Statistics Korea (KOSTAT)·MyData 4 Designated Combination Specialists (Samsung SDS, KICI, KOSTAT, KFTC)·National Institute of Korean Language·National Law Information Center·National Spatial Information Platform·National Spatial Data Center·Korean Spatial Information Standards. Finance and Fintech Standards: FSC (Financial Services Commission)·FSS (Financial Supervisory Service)·FIU (Financial Intelligence Unit)·BOK (Bank of Korea)·FSEC (Financial Security Institute)·KFTC (Korea Financial Telecommunications)·KSD (Korea Securities Depository)·KRX (Korea Exchange) 8-agency cooperation. 5G/6G Communications Infrastructure: 5G subscribers 35 million (2024)·5G base stations 350,000·6G commercialization target 2028·5G dedicated networks 16 operators·6G Acceleration Council (MSIT, 2024). K-Content: KOCCA (Korea Creative Content Agency)·MCST (Ministry of Culture, Sports and Tourism)·KCA (Korea Communications Agency)·Korea Culture Information Service Agency·Korean Film Archive·Korea Publishing Industry Promotion Agency. Data 3 Acts (Personal Information Protection Act·Credit Information Act·Telecommunications Network Act, 2020 enforcement)·Data Industry Act (2021)·Public Data Act (2013)·AI Framework Act (2026)·Digital Platform Government Framework Act (2024 proposed) — Korea digital transformation core legislation.