G.O.D. Framework

Script: ai_bias_auditor.py - Ensuring AI Fairness

Introduction

The ai_bias_auditor.py script is a tool for analyzing and auditing AI models for biases. It plays a critical role in ensuring ethical AI development by uncovering and addressing unfair biases within datasets, training processes, and predictions.

Implementing this auditor allows developers to meet compliance standards, adhere to ethical AI principles, and maintain confidence in AI predictions. This script is especially critical for AI systems deployed in high-stakes environments like finance, healthcare, legal, and recruitment.

Purpose

Key Features

Logic and Implementation

The core workflow encompasses three stages: dataset/model analysis, bias identification, and reporting/remediation. The script employs Python libraries like scikit-learn, pandas, and matplotlib for processing, computation, and visualization.


            import pandas as pd
            from sklearn.metrics import confusion_matrix

            def disparate_impact_analysis(data, protected_column, target_column):
                """
                Calculates Disparate Impact (DI) to measure bias.
                :param data: DataFrame containing the dataset.
                :param protected_column: Column containing sensitive attribute (e.g., gender/race).
                :param target_column: Prediction/outcome column.
                :return: Disparate Impact value.
                """
                protected_group = data[data[protected_column] == 1]
                non_protected_group = data[data[protected_column] == 0]

                protected_positive_rate = (
                    sum(protected_group[target_column] == 1) / len(protected_group)
                )
                non_protected_positive_rate = (
                    sum(non_protected_group[target_column] == 1) / len(non_protected_group)
                )

                return protected_positive_rate / non_protected_positive_rate

            def visualize_bias(data, protected_column, target_column):
                """
                Creates bias heatmap for the given dataset.
                """
                import seaborn as sns
                import matplotlib.pyplot as plt

                heatmap_data = pd.crosstab(
                    data[protected_column], data[target_column], normalize="index"
                )
                sns.heatmap(heatmap_data, annot=True, cmap="coolwarm")
                plt.title("Bias Heatmap")
                plt.show()

            if __name__ == "__main__":
                # Example usage
                sample_data = pd.DataFrame({
                    "gender": [1, 0, 1, 0, 0],
                    "hired": [1, 1, 0, 1, 0]
                })

                di_score = disparate_impact_analysis(sample_data, "gender", "hired")
                print(f"Disparate Impact Score: {di_score:.2f}")

                visualize_bias(sample_data, "gender", "hired")
            

The code demonstrates:

  1. Calculating Disparate Impact (DI) – a fairness metric.
  2. Generating a heatmap visualizing fairness across groups.

Dependencies

How to Use This Script

  1. Prepare the dataset ensuring the inclusion of sensitive variables (e.g., race, gender) and target outcomes (e.g., loan approval).
  2. Call disparate_impact_analysis to calculate fairness metrics.
  3. Use visualize_bias to create visual reports for interpretation.
  4. Iterate workflows until biases are reduced or mitigated effectively.

            # Example testing unfairness:
            unfair_data = pd.DataFrame({
                "race": [1, 1, 1, 0, 0, 0, 0],
                "approved": [1, 1, 1, 0, 0, 0, 0]
            })

            print("Analyzing biases...")
            bias_score = disparate_impact_analysis(unfair_data, "race", "approved")
            print(f"Fairness Check - Disparate Impact Score: {bias_score:.2f}")

            visualize_bias(unfair_data, "race", "approved")
            

Role in the G.O.D. Framework

Future Enhancements