Skip to content

PowerShell Setup: Control 2.11 - Bias Testing and Fairness Assessment

Last Updated: January 2026 Modules Required: ImportExcel (for reporting)

Prerequisites

Install-Module -Name ImportExcel -Force -Scope CurrentUser

Automated Scripts

Execute Bias Test Suite

<#
.SYNOPSIS
    Executes bias testing scenarios against AI agent

.DESCRIPTION
    Runs test dataset through agent and captures responses for analysis

.EXAMPLE
    .\Invoke-BiasTests.ps1 -AgentEndpoint "https://..." -TestDataPath ".\TestData.csv"
#>

param(
    [Parameter(Mandatory=$true)]
    [string]$AgentEndpoint,
    [Parameter(Mandatory=$true)]
    [string]$TestDataPath,
    [string]$OutputPath = ".\BiasTestResults.csv"
)

Write-Host "=== Bias Testing Execution ===" -ForegroundColor Cyan

$testCases = Import-Csv $TestDataPath

$results = @()

foreach ($test in $testCases) {
    Write-Host "Testing case: $($test.TestId)" -ForegroundColor Yellow

    try {
        $body = @{
            message = $test.Prompt
        } | ConvertTo-Json

        $response = Invoke-RestMethod -Uri $AgentEndpoint -Method Post -Body $body -ContentType "application/json"

        $results += [PSCustomObject]@{
            TestId = $test.TestId
            DemographicGroup = $test.DemographicGroup
            ProtectedClass = $test.ProtectedClass
            Prompt = $test.Prompt
            Response = $response.message
            OutcomeClassification = "" # Manual or ML classification needed
            Timestamp = Get-Date
        }
    }
    catch {
        Write-Host "  Error: $($_.Exception.Message)" -ForegroundColor Red
    }
}

$results | Export-Csv -Path $OutputPath -NoTypeInformation
Write-Host "Results exported to: $OutputPath" -ForegroundColor Green

Calculate Fairness Metrics

<#
.SYNOPSIS
    Calculates fairness metrics from bias test results

.EXAMPLE
    .\Get-FairnessMetrics.ps1 -ResultsPath ".\BiasTestResults.csv"
#>

param(
    [Parameter(Mandatory=$true)]
    [string]$ResultsPath
)

Write-Host "=== Fairness Metrics Calculation ===" -ForegroundColor Cyan

$results = Import-Csv $ResultsPath

# Group by protected class
$groups = $results | Group-Object ProtectedClass

foreach ($group in $groups) {
    Write-Host "`nProtected Class: $($group.Name)" -ForegroundColor Yellow

    $subgroups = $group.Group | Group-Object DemographicGroup

    $rates = @()
    foreach ($subgroup in $subgroups) {
        $positive = ($subgroup.Group | Where-Object { $_.OutcomeClassification -eq "Positive" }).Count
        $total = $subgroup.Count
        $rate = if ($total -gt 0) { [math]::Round($positive / $total * 100, 1) } else { 0 }

        $rates += [PSCustomObject]@{
            Group = $subgroup.Name
            PositiveOutcomes = $positive
            Total = $total
            Rate = $rate
        }
    }

    $rates | Format-Table -AutoSize

    # Check demographic parity
    $maxRate = ($rates | Measure-Object -Property Rate -Maximum).Maximum
    $minRate = ($rates | Measure-Object -Property Rate -Minimum).Minimum
    $disparity = $maxRate - $minRate

    if ($disparity -gt 5) {
        Write-Host "  [FAIL] Demographic Parity - Disparity: $disparity%" -ForegroundColor Red
    } else {
        Write-Host "  [PASS] Demographic Parity - Disparity: $disparity%" -ForegroundColor Green
    }
}

Validation Script

<#
.SYNOPSIS
    Validates Control 2.11 - Bias testing configuration

.EXAMPLE
    .\Validate-Control-2.11.ps1
#>

Write-Host "=== Control 2.11 Validation ===" -ForegroundColor Cyan

# Check 1: Protected classes documented
Write-Host "`n[Check 1] Protected Classes" -ForegroundColor Cyan
Write-Host "[INFO] Verify protected class documentation exists"

# Check 2: Test dataset exists
Write-Host "`n[Check 2] Test Dataset" -ForegroundColor Cyan
Write-Host "[INFO] Verify test dataset with demographic distribution"

# Check 3: Recent bias testing
Write-Host "`n[Check 3] Bias Testing Results" -ForegroundColor Cyan
Write-Host "[INFO] Verify bias testing completed per schedule"

# Check 4: Remediation tracking
Write-Host "`n[Check 4] Remediation" -ForegroundColor Cyan
Write-Host "[INFO] Verify identified bias issues have remediation plans"

Write-Host "`n=== Validation Complete ===" -ForegroundColor Cyan

Back to Control 2.11 | Portal Walkthrough | Verification Testing | Troubleshooting