Skip to content
TheChatArchiveThe Chat Archive
GLMGLM-5 / Pony Alpha· unknownHugging Face Frontier Reasoning Traces

Build an AI eval reproducibility pack: datasets, seeds, bias tests, and drift logging.

  1. User

    Build an AI eval reproducibility pack: datasets, seeds, bias tests, and drift logging.

  2. GLM Assistant

    The user wants an "AI eval reproducibility pack." This means I need to provide a comprehensive toolkit or framework that allows someone to evaluate an AI model (likely an LLM or a classification model) in a way that is:

    1. Reproducible: Using fixed seeds and datasets.
    2. Robust: Checking for biases and concept drift.
    3. Transparent: Logging everything.

    The request asks for four specific components:

    1. Datasets: Sample data or loaders.
    2. Seeds: Mechanisms to fix randomness.
    3. Bias Tests: Algorithms or checks to detect fairness issues.
    4. Drift Logging: Tools to detect and log data/model drift over time.

    Target Audience: Data Scientists, ML Engineers, AI Researchers. Format: Python code (using standard libraries like scikit-learn, pandas, numpy, evidently or fairlearn concepts), structured as a "pack" (e.g., a set of scripts or a modular class structure).

    Plan:

    1. Project Structure: Define a clean directory structure.
    2. Configuration (Seeds & Reproducibility): Create a utility to set seeds globally (Python, NumPy, PyTorch/TF, CuDNN).
    3. Dataset Module: Create a synthetic dataset generator or loader that splits data consistently.
    4. Bias Testing Module: Implement a simple fairness check (e.g., demographic parity or disparate impact) using a library like fairlearn or custom logic. Self-correction: Keep it dependency-light where possible, but fairlearn is standard. I will use a custom implementation to make it self-contained and easier to understand, or clearly show imports.
    5. Drift Logging Module: Implement a statistical test (KS test, PSI) to compare a baseline dataset against a new dataset. This simulates production monitoring.
    6. Main Execution Script: Tie it all together in a workflow.

    Drafting the Content:

    • Reproducibility: Need a function set_global_seed(seed=42).
    • Datasets: I'll generate a synthetic classification dataset (e.g., loan approval) with sensitive attributes (gender, age) to facilitate bias testing.
    • Bias Tests: I'll calculate Disparate Impact and Average Odds Difference.
    • Drift Logging: I'll calculate Population Stability Index (PSI) between a baseline and a "current" dataset.

    Refining the "Pack" aspect: Instead of just a wall of code, I should structure it as a GitHub repository proposal or a modular Python package. I will provide the code blocks for the key files.

    Detailed Code Structure:

    1. utils.py: Seed setting.
    2. data.py: Data generation/loading.
    3. evaluation/bias.py: Fairness metrics.
    4. evaluation/drift.py: Drift detection (PSI/KS).
    5. main.py: The runner.

    Constraint Checklist & Confidence Score:

    1. Datasets? Yes.
    2. Seeds? Yes.
    3. Bias Tests? Yes.
    4. Drift Logging? Yes. Confidence Score: 5/5

    Mental Sandbox Simulation:

    • User runs the code.
    • Seed is set.
    • Data is split.
    • A dummy model is trained (or just random predictions are used for the sake of the demo, to keep the code agnostic to framework). Decision: I will simulate model predictions so the user doesn't need to install PyTorch/TensorFlow to run the eval code.
    • Bias metrics are calculated and printed.
    • Drift is simulated by shifting the data distribution, and the logger flags it.

    Let's write the solution. I will use standard libraries (numpy, pandas, scipy) to ensure high portability, adding comments on where industrial libraries (like evidently or fairlearn) would drop in.

    Key Concepts to highlight:

    • PSI (Population Stability Index): Standard for drift.
    • Disparate Impact: Ratio of favorable outcomes for unprivileged vs privileged groups.
    • Global Seed: Handling OS, Python, and RNGs.

    Let's assemble the "Reproducibility Pack". This reproducibility pack provides a modular Python framework designed to evaluate AI models with a focus on scientific rigor. It handles stochasticity (seeds), fairness (bias testing), and production stability (drift logging).

    You can copy-paste this structure directly into your project.

    Project Structure

    eval_pack/
    ├── config.py           # Central configuration for seeds and constants
    ├── data_generator.py   # Creates synthetic data for testing
    ├── reproducibility.py  # Handles seeding across libraries
    ├── bias_tests.py       # Fairness metrics (Disparate Impact)
    ├── drift_monitor.py    # Data drift logging (PSI)
    └── run_pipeline.py     # Main execution script
    

    1. Configuration & Seeding (config.py & reproducibility.py)

    To ensure reproducibility, we must control randomness across Python, NumPy, and PyTorch/TensorFlow.

    config.py

    import os
    
    # The Master Seed
    MASTER_SEED = 42
    
    # Paths
    LOG_DIR = "logs/drift_reports"
    os.makedirs(LOG_DIR, exist_ok=True)
    
    # Model Config
    FEATURES = ['age', 'income', 'credit_score', 'zip_code']
    SENSITIVE_ATTRIBUTE = 'age_group'  # Used for bias testing
    TARGET = 'default_risk'
    

    reproducibility.py

    import random
    import numpy as np
    import torch
    import tensorflow as tf
    from config import MASTER_SEED
    
    def set_global_seed(seed=MASTER_SEED):
        """Sets the seed for Python, NumPy, PyTorch, and TensorFlow."""
        random.seed(seed)
        np.random.seed(seed)
        
        # PyTorch
        torch.manual_seed(seed)
        torch.cuda.manual_seed_all(seed)
        torch.backends.cudnn.deterministic = True
        torch.backends.cudnn.benchmark = False
        
        # TensorFlow
        tf.random.set_seed(seed)
        
        print(f"[Reproducibility] Global seed set to {seed}")
    
    def log_environment():
        """Logs library versions for debugging reproduction issues later."""
        import subprocess
        reqs = subprocess.check_output(["pip", "freeze"])
        with open("requirements_snapshot.txt", "wb") as f:
            f.write(reqs)
        print("[Reproducibility] Requirements snapshot saved.")
    

    2. Synthetic Data with Bias Injection (data_generator.py)

    We generate a dataset that includes a "sensitive attribute" to test for fairness.

    import pandas as pd
    import numpy as np
    from config import FEATURES, TARGET, SENSITIVE_ATTRIBUTE
    
    def generate_synthetic_data(n_samples=1000, drift=False):
        """
        Generates synthetic loan data.
        If drift=True, it shifts the distribution to simulate production drift.
        """
        np.random.seed(42) # Local seed for data generation consistency
        
        # Feature generation
        age = np.random.randint(18, 70, n_samples)
        income = np.random.normal(50000, 15000, n_samples)
        score = np.random.randint(300, 850, n_samples)
        
        # Create sensitive attribute (Young vs Old)
        age_group = np.where(age < 40, 'young', 'mature')
    
        # Simulate bias in the Ground Truth (making it harder for 'young' group)
        # Base probability
        base_prob = (score / 850) * 0.8 
        
        # Add bias penalty for 'young'
        bias_penalty = np.where(age_group == 'young', 0.2, 0.0)
        
        prob = np.clip(base_prob + bias_penalty + (income / 500000), 0, 1)
        target = np.random.binomial(1, prob)
        
        # Simulate Drift (shift income distribution up significantly)
        if drift:
            income = income * 1.5 
            
        df = pd.DataFrame({
            'age': age,
            'income': income,
            'credit_score': score,
            SENSITIVE_ATTRIBUTE: age_group,
            TARGET: target
        })
        
        return df
    
    class SimpleModel:
        """A dummy model that predicts based on credit score threshold."""
        def __init__(self, threshold=600):
            self.threshold = threshold
            
        def predict(self, df):
            # This model is intentionally simple to demonstrate eval
            # It returns 1 (High Risk) if credit score is below threshold
            return (df['credit_score'] < self.threshold).astype(int).values
    

    3. Bias Tests (bias_tests.py)

    We implement a standard fairness metric: Disparate Impact (DI). DI measures the ratio of favorable outcomes for an unprivileged group vs. a privileged group. A score < 0.8 often indicates bias (Adverse Impact).

    import pandas as pd
    from config import TARGET
    
    def calculate_disparate_impact(df, predictions, sensitive_col, privileged_val, favorable_outcome=0):
        """
        Calculates Disparate Impact.
        Ratio = (Rate of favorable outcome for unprivileged) / (Rate for privileged)
        """
        df = df.copy()
        df['pred'] = predictions
        
        # Privileged Group Stats
        priv_mask = df[sensitive_col] == privileged_val
        priv_favorable = len(df[priv_mask & (df['pred'] == favorable_outcome)])
        priv_total = len(df[priv_mask])
        priv_rate = priv_favorable / priv_total if priv_total > 0 else 0
        
        # Unprivileged Group Stats
        unpriv_mask = df[sensitive_col] != privileged_val
        unpriv_favorable = len(df[unpriv_mask & (df['pred'] == favorable_outcome)])
        unpriv_total = len(df[unpriv_mask])
        unpriv_rate = unpriv_favorable / unpriv_total if unpriv_total > 0 else 0
        
        # Calculate Ratio
        di_score = unpriv_rate / priv_rate if priv_rate > 0 else float('inf')
        
        return {
            "privileged_rate": priv_rate,
            "unprivileged_rate": unpriv_rate,
            "disparate_impact": di_score,
            "is_biased": di_score < 0.8
        }
    
    def run_bias_report(df, predictions, sensitive_col):
        print(f"\n--- BIAS AUDIT: {sensitive_col} ---")
        # In this scenario, 'mature' is the privileged group, 0 (No Default) is favorable
        results = calculate_disparate_impact(df, predictions, sensitive_col, privileged_val='mature', favorable_outcome=0)
        
        print(f"Privileged Group Rate (Favorable): {results['privileged_rate']:.2f}")
        print(f"Unprivileged Group Rate (Favorable): {results['unprivileged_rate']:.2f}")
        print(f"Disparate Impact Ratio: {results['disparate_impact']:.2f}")
        
        if results['is_biased']:
            print("⚠️ WARNING: Potential Bias Detected (Ratio < 0.80)")
        else:
            print("✅ PASSED: Bias within acceptable range (>= 0.80)")
    

    4. Drift Logging (drift_monitor.py)

    We use Population Stability Index (PSI) to detect when input data changes significantly from a baseline.

    import numpy as np
    import json
    import os
    from datetime import datetime
    from config import LOG_DIR
    
    def calculate_psi(expected, actual, buckettype='bins', buckets=10, axis=0):
        """
        Calculate the PSI for a single feature.
        PSI < 0.1: No significant change
        PSI 0.1 - 0.25: Moderate change
        PSI > 0.25: Significant change (Action Required)
        """
        def scale_range (input, min, max):
            input += -(np.min(input))
            input /= np.max(input) / (max - min)
            input += min
            return input
    
        breakpoints = np.arange(0, buckets + 1) / (buckets) * 100
    
        if buckettype == 'bins':
            breakpoints = scale_range(breakpoints, np.min(expected), np.max(expected))
        elif buckettype == 'quantiles':
            breakpoints = np.array([np.percentile(expected, b) for b in breakpoints])
    
        expected_percents = np.histogram(expected, breakpoints)[0] / len(expected)
        actual_percents = np.histogram(actual, breakpoints)[0] / len(actual)
    
        # Handle 0 values to avoid divide by zero in log
        expected_percents = np.where(expected_percents == 0, 0.0001, expected_percents)
        actual_percents = np.where(actual_percents == 0, 0.0001, actual_percents)
    
        psi_value = np.sum((actual_percents - expected_percents) * np.log(actual_percents / expected_percents))
        return psi_value
    
    def log_drift(baseline_df, new_df, features):
        print("\n--- DRIFT MONITORING ---")
        report = {}
        alerts = []
        
        for col in features:
            psi = calculate_psi(baseline_df[col].values, new_df[col].values)
            status = "Stable"
            if psi > 0.25:
                status = "CRITICAL DRIFT"
                alerts.append(col)
            elif psi > 0.1:
                status = "Moderate Drift"
                
            report[col] = {"psi": round(psi, 4), "status": status}
            print(f"Feature '{col}': PSI {psi:.4f} [{status}]")
    
        # Save Log
        timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
        log_path = os.path.join(LOG_DIR, f"drift_report_{timestamp}.json")
        
        with open(log_path, 'w') as f:
            json.dump(report, f, indent=4)
            
        if alerts:
            print(f"⚠️ ALERT: Drift detected in features: {alerts}")
    

    5. Execution Script (run_pipeline.py)

    This script ties everything together into a single reproducibility run.

    import sys
    from config import FEATURES, TARGET, SENSITIVE_ATTRIBUTE
    from reproducibility import set_global_seed, log_environment
    from data_generator import generate_synthetic_data, SimpleModel
    from bias_tests import run_bias_report
    from drift_monitor import log_drift
    
    def main():
        # 1. Lock down randomness
        set_global_seed()
        
        # 2. Log environment for true reproducibility
        log_environment()
    
        # 3. Generate Data
        print("[Data] Generating Baseline and Production datasets...")
        baseline_df = generate_synthetic_data(n_samples=5000, drift=False)
        
        # Simulate production data (smaller sample, slightly drifted)
        # Changing the seed here to simulate a different time slice
        production_df = generate_synthetic_data(n_samples=1000, drift=True) 
    
        # 4. Initialize Model
        model = SimpleModel(threshold=650)
    
        # 5. Run Evaluation Suite
        # A. BIAS CHECK (On Baseline)
        print("\n[Eval] Running Bias Audit on Baseline...")
        preds_baseline = model.predict(baseline_df)
        run_bias_report(baseline_df, preds_baseline, SENSITIVE_ATTRIBUTE)
    
        # B. DRIFT CHECK (Compare Production vs Baseline)
        print("\n[Eval] Running Drift Analysis...")
        log_drift(baseline_df, production_df, FEATURES=['income', 'credit_score'])
    
        print("\n[Complete] Eval pack finished.")
    
    if __name__ == "__main__":
        main()
    

    How to Interpret Results

    1. Bias Report: Look at the Disparate Impact Ratio. If it is less than 0.8, your model is statistically treating the "young" group unfairly compared to the "mature" group regarding risk assessment.
    2. Drift Report: Look at the PSI values.
      • If income PSI is > 0.25 (which it likely will be due to drift=True in the generator), the pipeline alerts you that the production data no longer matches the training data.

    Requirements

    To run this, you need:

    pip install pandas numpy torch tensorflow
    

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.