22 min
Lesson 3 of 4
Robustness and Documentation Testing
From the course AI Audit & Testing
Test AI system robustness and produce audit-ready documentation.
Robustness Testing
Boundary Value Testing
def test_edge_cases(model, feature_ranges):
test_cases = []
for feature, (min_val, max_val) in feature_ranges.items():
# Minimum values
test_cases.append({"description": f"Min {feature}",
feature: min_val})
# Maximum values
test_cases.append({"description": f"Max {feature}",
feature: max_val})
# Boundary values
test_cases.append({"description": f"Near-min {feature}",
feature: min_val + 0.001})
# Extreme outliers
test_cases.append({"description": f"Extreme {feature}",
feature: max_val * 10})
results = []
for case in test_cases:
try:
prediction = model.predict([case])
results.append({
"case": case["description"],
"prediction": prediction[0],
"error": None
})
except Exception as e:
results.append({
"case": case["description"],
"prediction": None,
"error": str(e)
})
return results
Missing Data Robustness
import numpy as np
def test_missing_data_handling(model, X_test):
results = {}
for feature in X_test.columns:
X_missing = X_test.copy()
X_missing[feature] = np.nan # Inject missing values
try:
predictions = model.predict(X_missing)
results[feature] = {"handled": True, "predictions": predictions}
except Exception as e:
results[feature] = {"handled": False, "error": str(e)}
missing_handled = sum(1 for r in results.values() if r["handled"])
print(f"Missing data handled for {missing_handled}/{len(results)} features")
return results
Audit Documentation
Model Card Generation
def generate_model_card(model, eval_data, audit_results):
return {
"model_overview": {
"name": model.name,
"version": model.version,
"type": model.__class__.__name__,
"trained_on": model.training_date,
"intended_use": model.description
},
"performance_metrics": {
"overall_accuracy": audit_results["accuracy"],
"precision": audit_results["precision"],
"recall": audit_results["recall"],
"f1_score": audit_results["f1"]
},
"fairness_evaluation": {
"groups_tested": audit_results["groups"],
"disparate_impact_ratios": audit_results["fairness_ratios"],
"passes_80_pct_rule": all(
v >= 0.8 for v in audit_results["fairness_ratios"].values()
)
},
"limitations": model.known_limitations,
"ethical_considerations": model.ethical_notes,
"audit_date": datetime.utcnow().isoformat(),
"auditor": "AI Governance Team"
}
Key Takeaways
- •Boundary and edge case testing reveals model behavior at extremes of input ranges
- •Missing data robustness testing ensures graceful handling of incomplete inputs
- •Automated model card generation produces consistent audit documentation
- •Audit trails with timestamps and auditor records satisfy regulatory requirements
0 of 4 lessons marked complete
Progress is kept in this browser only.