Sub topic 7
Correcting for Chance Agreement
Prerequisites
This chapter assumes that you know basic Python, proportions, and categorical text classification. You should also understand that two annotators label the same fixed set of items, such as sentences or documents.
Why Percent Agreement Is Not Enough
Observed agreement is the proportion of items on which annotators chose the same label:
observed agreement = matching labels / total itemsFor example, if two annotators agree on 8 of 10 sentences, their observed agreement is 0.80, or 80%.
This number can be misleading when one label is common. Suppose almost every sentence is negative. Two annotators might agree often simply because both frequently choose neg, even without applying the guidelines consistently. A useful agreement measure should subtract this expected agreement.
Estimating Chance Agreement
Cohen's kappa estimates how much agreement remains after accounting for agreement expected from each annotator's label distribution:
kappa = (observed agreement - chance agreement)
/ (1 - chance agreement)Chance agreement is calculated from the annotators' marginal label frequencies. For each label, multiply the proportion assigned by annotator 1 by the proportion assigned by annotator 2. Then add those products.
Consider these label counts:
| Label | Annotator 1 count | Annotator 2 count |
|---|---|---|
| `pos` | 4 | 3 |
| `neu` | 2 | 2 |
| `neg` | 4 | 5 |
The chance agreement is:
(4/10 × 3/10) + (2/10 × 2/10) + (4/10 × 5/10)
= 0.12 + 0.04 + 0.20
= 0.36The exact value depends on the label lists and their marginal counts. With observed agreement of 0.80 and chance agreement of 0.38, kappa is:
(0.80 - 0.38) / (1 - 0.38) = 0.677The score is lower than 0.80 because some agreement was expected from the annotators' label frequencies.
Compute Kappa in Python
from collections import Counter
def cohen_kappa(a, b):
if len(a) != len(b) or not a:
raise ValueError("Inputs must have the same nonzero length")
n = len(a)
observed = sum(x == y for x, y in zip(a, b)) / n
counts_a = Counter(a)
counts_b = Counter(b)
labels = set(a) | set(b)
chance = sum(
counts_a[label] * counts_b[label] for label in labels
) / (n * n)
if chance == 1:
return 1.0
return (observed - chance) / (1 - chance)
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))The output is:
0.677The inputs must represent the same items in the same order. Reordering one annotator's labels can produce an invalid score while still producing a number.
Exercise
Two annotators agree on 90 of 100 items. Their label distributions produce chance agreement of 0.80. What is Cohen's kappa?
Show answer
Kappa is (0.90 - 0.80) / (1 - 0.80) = 0.50.