Measuring Bias in Text Generation
You'll learn how to quantitatively assess bias in LLM outputs using template-based and co-occurrence methods.
Now that you understand what stereotyping and association bias look like, you need systematic ways to measure them. You can't improve what you can't measure, so bias quantification is essential for responsible AI engineering.
Template-Based Measurement
This approach uses fill-in-the-blank prompts with controlled variations. You create templates where you swap demographic attributes while keeping everything else constant:
- "The [nurse/doctor] walked into the room. [He/She] was..."
- "The CEO arrived late. [He/She] seemed..."
By comparing how the model completes these prompts across different groups (gender, race, age), you can measure disparities in sentiment, role association, or trait attribution. Calculate metrics like mean sentiment difference or stereotype alignment scores across groups.
Co-occurrence Analysis
This method examines which words and concepts appear together in generated text. You generate large samples of text mentioning different demographic groups, then measure:
- PMI (Pointwise Mutual Information): How strongly two words co-occur compared to chance
- Embedding associations: Distance between group identifiers and trait words in embedding space
- Frequency ratios: How often positive vs. negative terms appear near demographic mentions
Think of it like analyzing a city map: if "programmer" roads always lead to "male" neighborhoods but rarely to "female" ones, that reveals systemic bias in your model's mental geography.
Quantifying the Results
Both methods produce numerical scores you can track over time, compare across models, or set thresholds for. Combine them with statistical tests to determine if differences are significant rather than random noise.
Key Takeaway: Template-based methods reveal bias through controlled prompt variations, while co-occurrence analysis uncovers systematic associations—together they provide quantitative evidence of bias patterns in your LLM's outputs.