Sub topic 2
Correcting for Chance Agreement
This chapter assumes you know basic Python (functions, lists, and dictionaries) and what a proportion is. It builds on the pilot round, where two annotators labeled the same ten items.
Why chance matters
Annotators agree sometimes even when they guess. If most items are labeled "pos", two annotators will match often without reading closely. Chance agreement, written p_e, is the agreement you expect from label frequencies alone. Cohen's kappa removes that expected part:
kappa = (po - pe) / (1 - p_e)
Here p_o is the observed agreement, the share of items where the labels match. The denominator is the largest agreement possible beyond chance.
Cohen's kappa for two annotators
To find p_e, count how often each annotator uses each label. Multiply the two counts for each label, add the products, and divide by n squared.
| Label | Annotator 1 | Annotator 2 | Product |
|---|---|---|---|
| pos | 4 | 4 | 16 |
| neg | 4 | 5 | 20 |
| neu | 2 | 1 | 2 |
The products sum to 38, so p_e = 38 / 100 = 0.38. Observed agreement is 8 of 10, or 0.80. Kappa is (0.80 - 0.38) / (1 - 0.38), which is about 0.677.
from collections import Counter
def cohen_kappa(a, b):
n = len(a)
p_o = sum(x == y for x, y in zip(a, b)) / n
count_a, count_b = Counter(a), Counter(b)
labels = set(a) | set(b)
p_e = sum(count_a[l] * count_b[l] for l in labels) / n ** 2
return (p_o - p_e) / (1 - p_e)
ann1 = ["pos", "pos", "neg", "neg", "pos", "neu", "neg", "pos", "neu", "neg"]
ann2 = ["pos", "neg", "neg", "neg", "pos", "neu", "neg", "pos", "pos", "neg"]
print(round(cohen_kappa(ann1, ann2), 3))Interpreting kappa values
Landis and Koch (1977) proposed a rough guide. Values below 0 mean worse than chance. 0 to 0.20 is slight, 0.21 to 0.40 is fair, 0.41 to 0.60 is moderate, 0.61 to 0.80 is substantial, and 0.81 to 1.00 is almost perfect. By this guide, 0.677 is substantial. Treat the cut-offs as conventions, not thresholds that prove quality.
Where kappa misleads: prevalence and bias
When one label dominates, pe becomes large, so kappa drops even when observed agreement is high. Consider 100 items where both annotators say "no" for 89, both say "yes" for 1, and they split 5 and 5 on the rest. Observed agreement is 0.90, but pe is 0.887 because "no" is so common. Kappa is about 0.11.
Bias works the other way. When one annotator says "yes" much more often than the other, the label totals differ, and p_e falls. For the same observed agreement, kappa rises. Report observed agreement, label prevalence, and a confusion matrix together with kappa so readers can see both effects.
Exercise
Two annotators label 100 items. Observed agreement is 0.90 and chance agreement is 0.50. What is Cohen's kappa?
Show answer
kappa = (0.90 - 0.50) / (1 - 0.50) = 0.40 / 0.50 = 0.80, which is almost perfect by the Landis and Koch guide.