Subgroup metrics and uncertainty
Quick answer
Subgroup evaluation computes metrics separately for meaningful slices of a dataset. A single aggregate error can hide unequal performance. Every reported rate needs its denominator and a measure of uncertainty. Examples are static Python snippets tested locally on CPU; The confusion matrix example requires scikit-learn; exercises use standard-library Python. ByteMind execution is unverified.
Native confusion matrix audit
from sklearn.metrics import confusion_matrixprint(confusion_matrix([0,0,1,1],[0,1,1,1],labels=[0,1]).ravel().tolist())Expected output (locally tested): [1, 1, 0, 2]. Explicit labels preserve the binary matrix shape.
How it works
For binary decisions let TP, TN, FP and FN denote counts. Error=(FP+FN)/n; TPR=TP/(TP+FN); FPR=FP/(FP+TN). If a denominator is zero the rate is undefined, not zero. Always report group membership counts and positive/negative support. A group with only two positive outcomes can show a recall of 100% by chance. A Wilson score interval for a binomial proportion p=k/n has center (p+z²/(2n))/(1+z²/n) and half-width z*sqrt(p(1-p)/n+z²/(4n²))/(1+z²/n). Use z=1.96 for an approximate 95% interval under independent Bernoulli observations. Repeated observations from a device require cluster-aware uncertainty instead. For a paired policy difference bootstrap entire independent rows (or clusters), not isolated predictions. Many subgroup comparisons increase chance findings; predefine relevant slices, report support and treat sparse groups as inconclusive. An interval is not a guarantee of fairness. In this chapter groups are fictional sensor families; no real person attributes are used. Independent library confusion counts can audit manual computations.
Wilson interval width
from math import sqrtdef width(k,n): p=k/n; z=1.96 return 2*z*sqrt(p*(1-p)/n+z*z/(4*n*n))/(1+z*z/n)print(width(1,5)>width(20,100))Expected output (locally tested): True. Equal rates have unequal precision.
Common mistakes
Do not average rates without stating weights, treat empty denominators as perfect performance, or interpret overlapping intervals as a formal test of equality.
Practice and next steps
Compare 1 error in 5 observations with 20 errors in 100. Both rates are 20%; explain why uncertainty differs.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Subgroup metrics and uncertainty — knowledge check
Apply subgroup metrics and uncertainty to small examples and debugging scenarios.
Which denominator defines TPR?
What if a group has no positive labels?
Which has narrower Wilson uncertainty at equal observed rate?
How should repeated device readings be bootstrapped?
Why report subgroup counts?