> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Evaluating LLM Bias Across Demographic Groups

> Guidelines for detecting and measuring LLM performance disparities across demographic groups via stratified evaluations, group-level metrics, statistical testing, and privacy-aware practices.

Question 6.

Which approach is most effective for evaluating an LLM's performance across diverse demographic groups to identify potential biases?

* Testing on a single, large, randomly selected dataset?
* Disaggregated evaluation using stratified test sets representing different demographic groups?
* Asking the model to self-evaluate its biases, or measuring only the overall accuracy on the complete dataset?

<Callout icon="lightbulb" color="#1CB2FE">
  Disaggregated evaluation using stratified test sets representing different demographic groups is the most effective approach.
</Callout>

## Why disaggregated (stratified) evaluation matters

A single, large random test set produces aggregate metrics (e.g., overall accuracy) that can mask important performance differences across demographic groups. A model may appear to perform well in aggregate while systematically underperforming on minority or intersectional groups—disaggregated evaluation surfaces those disparities.

Key reasons to prefer stratified evaluation:

* It isolates group-level performance so you can detect and quantify disparities.
* It supports targeted diagnostics (e.g., error types by group) and remediation.
* It enables fairness-aware metrics (not just accuracy) to be computed per group.

## What a robust disaggregated evaluation includes

* Stratified test sets that represent the demographic groups of interest (including intersectional slices where relevant).
* Multiple metrics per group:
  * Performance: accuracy, precision, recall, F1.
  * Error analysis: false positive/negative rates.
  * Calibration: confidence vs accuracy per group.
  * Fairness metrics: statistical parity difference, equalized odds, demographic parity, predictive parity—choose depending on your deployment context.
* Statistical rigour:
  * Confidence intervals or hypothesis testing to determine whether observed gaps are statistically significant.
  * Adequate sample sizes for each subgroup; if some groups are small, consider targeted data collection.
* Qualitative review:
  * Examine representative failures from each group to understand root causes.
* Data quality and labeling consistency:
  * Ensure labels and metadata are consistent across groups to avoid evaluation artifacts.

## Quick comparison

| Approach                                   |                                                               Strengths | Weaknesses                                                      | Recommendation                                                        |
| ------------------------------------------ | ----------------------------------------------------------------------: | --------------------------------------------------------------- | --------------------------------------------------------------------- |
| Single, random dataset (aggregate metrics) |                                        Simple to run; large sample size | Can hide group-level disparities; gives false sense of fairness | Not sufficient alone—use as complement to disaggregated evaluation    |
| Disaggregated, stratified evaluation       | Reveals group disparities; supports fairness metrics and targeted fixes | Requires careful data collection and sufficient subgroup sizes  | Recommended primary approach for bias detection                       |
| Model self-evaluation                      |                                                       Fast and low-cost | Unreliable introspection; prompt-sensitive; can be manipulated  | Not recommended as sole approach; can be used as supplementary signal |

## Example evaluation workflow (high-level)

1. Define demographic groups and intersectional slices to analyze.
2. Create or sample stratified test sets that ensure adequate representation for each slice.
3. Run model inference and compute per-group metrics.
4. Compute fairness metrics and statistical tests (e.g., difference in means with confidence intervals, bootstrap).
5. Perform qualitative error analysis on representative failures.
6. Report both aggregate and disaggregated results; iterate on mitigation strategies.

## Example: compute group-level metrics (Python/pseudocode)

```python theme={null}
import pandas as pd
from sklearn.metrics import accuracy_score, precision_score, recall_score

# df contains columns: 'text', 'true_label', 'pred_label', 'demographic_group'
results = []
for group, dfg in df.groupby('demographic_group'):
    acc = accuracy_score(dfg['true_label'], dfg['pred_label'])
    prec = precision_score(dfg['true_label'], dfg['pred_label'], average='binary')
    rec = recall_score(dfg['true_label'], dfg['pred_label'], average='binary')
    results.append({'group': group, 'accuracy': acc, 'precision': prec, 'recall': rec})

group_metrics = pd.DataFrame(results)
print(group_metrics)
```

Use bootstrap or other resampling to obtain confidence intervals for these metrics when subgroup sizes are limited.

## Metrics to track (examples)

| Metric                                         | Why it matters                                                    |
| ---------------------------------------------- | ----------------------------------------------------------------- |
| Accuracy / F1                                  | Baseline performance per group                                    |
| Precision / Recall                             | Reveals bias in error types (false positives vs false negatives)  |
| False positive rate / False negative rate      | Critical in high-stakes settings                                  |
| Calibration error                              | Whether model confidence aligns with actual correctness per group |
| Statistical parity difference / Equalized odds | Common fairness criteria to quantify disparities                  |

For reference implementations and libraries: consider Fairlearn ([https://fairlearn.org/](https://fairlearn.org/)) and IBM AI Fairness 360 ([https://aif360.mybluemix.net/](https://aif360.mybluemix.net/)).

<Callout icon="warning" color="#FF6B6B">
  Handle sensitive demographic data with care. Obtain consent where required, follow privacy regulations, and minimize re-identification risk when collecting or reporting demographic attributes.
</Callout>

## Practical considerations and tips

* Small groups: If a subgroup has few samples, avoid overinterpreting noisy metrics—use uncertainty quantification, combine targeted data collection with careful statistical methods, or aggregate similar slices thoughtfully.
* Intersectionality: Bias often appears at intersectional slices (e.g., gender × age × dialect). Evaluate those where relevant to the application.
* Labeling consistency: Ensure labeling practices are consistent across groups to avoid systematic label bias.
* Reporting: Always publish both aggregate and disaggregated results so stakeholders can assess overall and group-level performance.

## Why the other approaches fall short

* Model self-evaluation: LLMs are not reliable judges of their own biases—responses depend on prompts and can be gamed. Use external, systematic evaluation instead.
* Overall accuracy or a single random test set: These hide group disparities and can create false confidence about fairness.

## Summary

Disaggregated evaluation using stratified test sets and group-level metrics is the most effective way to detect and measure LLM performance disparities across demographic groups. It enables rigorous detection of bias, supports targeted mitigation, and provides transparent reporting to stakeholders. For best results, combine quantitative group metrics with qualitative error analysis, statistical testing, and strong privacy-aware data practices.

## Links and references

* Fairlearn: [https://fairlearn.org/](https://fairlearn.org/)
* IBM AI Fairness 360: [https://aif360.mybluemix.net/](https://aif360.mybluemix.net/)
* "Fairness" concepts and metrics: [https://en.wikipedia.org/wiki/Fairness\_(machine\_learning)](https://en.wikipedia.org/wiki/Fairness_\(machine_learning\))

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/nvidia-generative-ai-llms-associate-certification/module/44b444b3-19d6-4856-95a6-a46628fb2cf0/lesson/ed193d80-f5ad-49c4-b529-cc994f037a7c" />
</CardGroup>
