Study interactive :: Progress tools open in the Study Hub reader.

Ethics in Machine Learning

Understanding and addressing ethical issues in machine learning, including bias, fairness, and responsible AI practices.

Table of Contents


Introduction to ML Ethics

Why Ethics Matter in ML

Machine learning systems can have significant impacts on people's lives:

Key Principles:

  1. Fairness: Systems should not discriminate against protected groups
  2. Transparency: Decisions should be explainable
  3. Accountability: Clear responsibility for system outcomes
  4. Privacy: Protect user data and privacy
  5. Safety: Systems should be robust and secure

Types of Bias in ML

1. Historical Bias

Bias present in the training data due to historical inequalities.

Example:

Mitigation:

2. Representation Bias

When certain groups are underrepresented in training data.

Example:

Mitigation:

3. Measurement Bias

When the way data is collected or labeled introduces bias.

Example:

Mitigation:

4. Aggregation Bias

When a single model is used for diverse groups that should be treated differently.

Example:

Mitigation:

5. Evaluation Bias

When evaluation metrics don't account for different groups' needs.

Example:

Mitigation:

6. Confirmation Bias

When model development reinforces existing beliefs.

Example:

Mitigation:


Fairness in Machine Learning

Fairness Definitions

Different definitions of fairness serve different purposes:

1. Demographic Parity (Statistical Parity)

Definition: Equal positive prediction rates across groups

P(Ŷ=1 | A=a) = P(Ŷ=1 | A=b)

Example: Same loan approval rate for all demographic groups

Trade-offs: May reduce accuracy, may not reflect true differences

2. Equalized Odds

Definition: Equal true positive and false positive rates across groups

P(Ŷ=1 | Y=1, A=a) = P(Ŷ=1 | Y=1, A=b)
P(Ŷ=1 | Y=0, A=a) = P(Ŷ=1 | Y=0, A=b)

Example: Same accuracy for all groups

Trade-offs: More restrictive than demographic parity

3. Calibration

Definition: Predicted probabilities should be well-calibrated for all groups

P(Y=1 | Ŷ=p, A=a) = P(Y=1 | Ŷ=p, A=b) = p

Example: A 70% predicted probability should mean 70% chance for all groups

4. Individual Fairness

Definition: Similar individuals should receive similar predictions

Example: Two applicants with similar qualifications should get similar outcomes

Fairness Metrics

from sklearn.metrics import confusion_matrix

def calculate_fairness_metrics(y_true, y_pred, groups):
    """
    Calculate fairness metrics for different groups
    
    Parameters:
    - y_true: True labels
    - y_pred: Predicted labels
    - groups: Group membership (e.g., gender, race)
    """
    metrics = {}
    
    for group in set(groups):
        group_mask = groups == group
        y_true_group = y_true[group_mask]
        y_pred_group = y_pred[group_mask]
        
        cm = confusion_matrix(y_true_group, y_pred_group)
        tn, fp, fn, tp = cm.ravel()
        
        metrics[group] = {
            'accuracy': (tp + tn) / (tp + tn + fp + fn),
            'precision': tp / (tp + fp) if (tp + fp) > 0 else 0,
            'recall': tp / (tp + fn) if (tp + fn) > 0 else 0,
            'fpr': fp / (fp + tn) if (fp + tn) > 0 else 0,
            'positive_rate': (tp + fp) / (tp + tn + fp + fn)
        }
    
    return metrics

# Example usage
metrics = calculate_fairness_metrics(y_test, y_pred, demographic_groups)
for group, group_metrics in metrics.items():
    print(f"{group}: Accuracy={group_metrics['accuracy']:.3f}, "
          f"Positive Rate={group_metrics['positive_rate']:.3f}")

Responsible AI Principles

1. Fairness

2. Reliability & Safety

3. Privacy & Security

4. Inclusiveness

5. Transparency

6. Accountability


Real-World Examples

1. COMPAS Recidivism Risk Assessment

Issue: Algorithm used in criminal justice showed racial bias

Problem:

Lessons:

References:

2. Amazon Hiring Algorithm

Issue: AI recruiting tool showed bias against women

Problem:

Lessons:

References:

3. Facial Recognition Systems

Issue: Poor performance on darker-skinned individuals and women

Problem:

Lessons:

References:

4. Healthcare Algorithms

Issue: Algorithms allocating healthcare resources showed racial bias

Problem:

Lessons:

References:


Detecting and Mitigating Bias

Detection Methods

1. Data Auditing

import pandas as pd
import matplotlib.pyplot as plt

def audit_dataset(df, protected_attributes, target):
    """
    Audit dataset for potential bias
    
    Parameters:
    - df: DataFrame
    - protected_attributes: List of protected attribute columns
    - target: Target variable column
    """
    results = {}
    
    for attr in protected_attributes:
        print(f"\n{'='*50}")
        print(f"Auditing: {attr}")
        print(f"{'='*50}")
        
        # Distribution
        print("\nDistribution:")
        print(df[attr].value_counts(normalize=True))
        
        # Target distribution by group
        print(f"\nTarget distribution by {attr}:")
        print(pd.crosstab(df[attr], df[target], normalize='index'))
        
        # Statistical tests
        from scipy.stats import chi2_contingency
        c df[target])
        chi2, p_value, dof, expected = chi2_contingency(contingency)
        print(f"\nChi-square test: p-value = {p_value:.4f}")
        
        results[attr] = {
            'distribution': df[attr].value_counts(normalize=True).to_dict(),
            'target_by_group': pd.crosstab(df[attr], df[target], normalize='index').to_dict(),
            'chi2_pvalue': p_value
        }
    
    return results

# Example usage
audit_results = audit_dataset(df, ['gender', 'race'], 'target')

2. Model Performance by Group

from sklearn.metrics import classification_report

def evaluate_by_group(y_true, y_pred, groups):
    """
    Evaluate model performance for each group
    """
    results = {}
    
    for group in set(groups):
        group_mask = groups == group
        y_true_group = y_true[group_mask]
        y_pred_group = y_pred[group_mask]
        
        print(f"\n{'='*50}")
        print(f"Group: {group}")
        print(f"{'='*50}")
        print(classification_report(y_true_group, y_pred_group))
        
        results[group] = classification_report(
            y_true_group, y_pred_group, output_dict=True
        )
    
    return results

# Example usage
group_results = evaluate_by_group(y_test, y_pred, demographic_groups)

Mitigation Techniques

1. Pre-processing (Data Level)

from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler

# Oversample minority groups
smote = SMOTE(random_state=42)
X_resampled, y_resampled = smote.fit_resample(X, y)

# Or use stratified sampling
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, stratify=y, random_state=42
)

2. In-processing (Algorithm Level)

# Example: Using class weights to handle imbalance
from sklearn.ensemble import RandomForestClassifier
from sklearn.utils.class_weight import compute_class_weight

class_weights = compute_class_weight(
    'balanced', classes=np.unique(y_train), y=y_train
)
class_weight_dict = dict(zip(np.unique(y_train), class_weights))

model = RandomForestClassifier(
    class_weight=class_weight_dict,
    random_state=42
)
model.fit(X_train, y_train)

3. Post-processing (Output Level)

from sklearn.calibration import CalibratedClassifierCV

# Calibrate probabilities
calibrated_model = CalibratedClassifierCV(
    base_model, method='isotonic', cv=3
)
calibrated_model.fit(X_train, y_train)

# Adjust threshold per group
def adjust_threshold_by_group(y_proba, groups, thresholds):
    """
    Apply different thresholds for different groups
    """
    y_pred = np.zeros_like(y_proba)
    for group, threshold in thresholds.items():
        group_mask = groups == group
        y_pred[group_mask] = (y_proba[group_mask] >= threshold).astype(int)
    return y_pred

Tools and Frameworks

1. Fairness Indicators (TensorFlow)

Purpose: Evaluate fairness metrics for classification and regression models

Installation:

pip install fairness-indicators

Usage:

import tensorflow_model_analysis as tfma
from tensorflow_model_analysis.addons.fairness.view import widget_view

# Define fairness metrics
fairness_metrics = [
    tfma.metrics.FairnessIndicators(
        thresholds=[0.1, 0.3, 0.5, 0.7, 0.9],
        labels_key='label'
    )
]

# Evaluate model
eval_result = tfma.run_model_analysis(
    model_location=model_path,
    data_location=eval_data_path,
    slicing_spec=[tfma.slicer.SingleSliceSpec(columns=['gender'])],
    metrics_specs=fairness_metrics
)

# Visualize
widget_view.render_fairness_indicator(eval_result)

Resources:

2. AI Fairness 360 (IBM)

Purpose: Comprehensive toolkit for detecting and mitigating bias

Installation:

pip install aif360

Usage:

from aif360.datasets import BinaryLabelDataset
from aif360.algorithms.preprocessing import Reweighing
from aif360.metrics import BinaryLabelDatasetMetric

# Load dataset
dataset = BinaryLabelDataset(
    df=df,
    label_names=['target'],
    protected_attribute_names=['gender'],
    favorable_label=1,
    unfavorable_label=0
)

# Check for bias
metric = BinaryLabelDatasetMetric(
    dataset,
    unprivileged_groups=[{'gender': 0}],
    privileged_groups=[{'gender': 1}]
)
print(f"Disparate impact: {metric.disparate_impact()}")

# Mitigate bias
rw = Reweighing(
    unprivileged_groups=[{'gender': 0}],
    privileged_groups=[{'gender': 1}]
)
dataset_transformed = rw.fit_transform(dataset)

Resources:

3. SHAP (SHapley Additive exPlanations)

Purpose: Explain model predictions and detect bias

Installation:

pip install shap

Usage:

import shap

# Explain model predictions
explainer = shap.TreeExplainer(model)
shap_values = explainer.shap_values(X_test)

# Visualize
shap.summary_plot(shap_values, X_test)

# Check for bias in feature importance
shap.summary_plot(shap_values, X_test, feature_names=feature_names)

Resources:

4. Fairlearn

Purpose: Assess and mitigate unfairness in ML models

Installation:

pip install fairlearn

Usage:

from fairlearn.metrics import (
    demographic_parity_difference,
    equalized_odds_difference,
    MetricFrame
)
from fairlearn.postprocessing import ThresholdOptimizer

# Calculate fairness metrics
metrics = {
    'accuracy': accuracy_score,
    'selection_rate': selection_rate
}
metric_frame = MetricFrame(
    metrics=metrics,
    y_true=y_test,
    y_pred=y_pred,
    sensitive_features=demographic_groups
)

print(metric_frame.by_group)
print(f"\nDemographic parity difference: "
      f"{demographic_parity_difference(y_test, y_pred, sensitive_features=demographic_groups)}")

# Mitigate bias
postprocessor = ThresholdOptimizer(
    estimator=model,
    constraints='equalized_odds',
    prefit=True
)
postprocessor.fit(X_train, y_train, sensitive_features=train_groups)
y_pred_fair = postprocessor.predict(X_test, sensitive_features=test_groups)

Resources:

5. What-If Tool (Google)

Purpose: Visualize model behavior and test fairness

Resources:


Best Practices

1. Data Collection

2. Model Development

3. Deployment

4. Governance

Checklist for Ethical ML


Resources and Further Reading

Academic Papers

  1. "Fairness in Machine Learning" - Solon Barocas, Moritz Hardt, Arvind Narayanan

  2. "Fairness Definitions Explained" - Sahil Verma, Julia Rubin

  3. "The Myth of the Impartial Machine" - Cathy O'Neil

    • Book: "Weapons of Math Destruction"
  4. "Algorithmic Fairness" - Arvind Narayanan

Online Courses

  1. "Fairness in Machine Learning" - Cornell University

  2. "Ethics in AI" - MIT

  3. "Responsible AI" - Google

Organizations and Initiatives

  1. Partnership on AI

    • Website
    • Focuses on responsible AI development
  2. AI Now Institute

    • Website
    • Research on social implications of AI
  3. Algorithmic Justice League

    • Website
    • Combats bias in AI systems
  4. Fairness, Accountability, and Transparency in Machine Learning (FAccT)

Books

  1. "Weapons of Math Destruction" by Cathy O'Neil

    • Explores how algorithms can perpetuate inequality
  2. "The Ethical Algorithm" by Michael Kearns and Aaron Roth

    • Technical approaches to algorithmic fairness
  3. "Atlas of AI" by Kate Crawford

    • Examines the social and political implications of AI

Tools and Frameworks

  1. Fairness Indicators (TensorFlow)

  2. AI Fairness 360 (IBM)

  3. Fairlearn (Microsoft)

  4. SHAP (Model Explainability)

  5. What-If Tool (Google)

Regulations and Guidelines

  1. GDPR (General Data Protection Regulation)

  2. Algorithmic Accountability Act (Proposed US Legislation)

  3. EU AI Act

Additional Resources

  1. Fairness and Machine Learning (Online Book)

    • Link
    • Comprehensive technical treatment
  2. Responsible AI Practices (Google)

  3. Microsoft Responsible AI Resources

  4. IBM AI Ethics


Key Takeaways

  1. Bias is Inevitable: All data and models have some bias. The goal is to identify and mitigate harmful bias

  2. Fairness is Context-Dependent: Different definitions of fairness may conflict. Choose based on context

  3. Trade-offs Exist: Fairness and accuracy may trade off. Document and justify choices

  4. Testing is Essential: Test models across demographic groups before deployment

  5. Monitoring is Ongoing: Continuously monitor deployed models for bias and fairness

  6. Transparency Matters: Explain decisions when possible, especially in high-stakes applications

  7. Human Oversight: Maintain human oversight for critical decisions

  8. Ethics is Everyone's Responsibility: All ML practitioners should consider ethics


Try next: Write down who can be harmed if your model is wrong. Add one check for that failure mode.