Sub topic 7
Correcting for chance agreement
This chapter assumes you can read basic Python, know what a proportion is, and have seen text classification tasks such as sentiment labeling. The previous chapter showed that observed agreement is easy to compute but easy to misread. This chapter explains why, and shows how to estimate the agreement you would get by luck alone. The next chapter uses that estimate to define Cohen's kappa.
Why raw agreement flatters annotators
Observed agreement is the share of items on which two annotators gave the same label. It ignores how likely a match was before anyone read the text.
Suppose two annotators label 100 reviews as spam or ok, and 90 percent of reviews are ok. An annotator who labels everything ok without reading would still match a careful annotator on about 90 percent of items. A reported agreement of 0.90 would then say almost nothing about annotation quality.
Two things drive the amount of agreement that happens by chance:
- The number of labels. With two equally likely labels, random guessing agrees about half the time. With five equally likely labels, it agrees about a fifth of the time.
- The label distribution. When one label dominates, annotators who lean toward it agree often, even if they never look at the text.
A useful agreement measure should score 0 when annotators behave as if they were guessing, and should score higher only for agreement beyond that baseline.
Estimating chance agreement
Chance agreement, written p_e, is the probability that two annotators pick the same label if each one labels independently, following their own overall label frequencies.
The estimate works in three steps:
- For each annotator, compute how often they use each label (a proportion).
- For each label, multiply the two annotators' proportions. This is the probability that both choose that label independently.
- Add these products across all labels.
In symbols, with annotator A's proportion for label k written as pA(k) and annotator B's as pB(k):
pe = sum over labels k of pA(k) × p_B(k)
The code later in this chapter computes the same value from counts: it sums countA(k) × countB(k) and divides by n², which is the same formula without converting counts to proportions first.
A worked example
Take ten items labeled by two annotators:
| Item | Annotator 1 | Annotator 2 |
|---|---|---|
| 1 | pos | pos |
| 2 | pos | neg |
| 3 | neg | neg |
| 4 | neg | neg |
| 5 | pos | pos |
| 6 | neu | neu |
| 7 | neg | neg |
| 8 | pos | pos |
| 9 | neu | pos |
| 10 | neg | neg |
They agree on items 1, 3, 4, 5, 6, 7, 8 and 10, so observed agreement p_o is 8/10 = 0.8.
Label counts for each annotator:
| Label | Annotator 1 count | Annotator 2 count | Proportions (A1, A2) | Product |
|---|---|---|---|---|
| pos | 4 | 4 | 0.4, 0.4 | 0.16 |
| neg | 4 | 5 | 0.4, 0.5 | 0.20 |
| neu | 2 | 1 | 0.2, 0.1 | 0.02 |
Chance agreement is p_e = 0.16 + 0.20 + 0.02 = 0.38. Of the 0.8 observed agreement, 0.38 would be expected by luck. Only the remaining part reflects real shared understanding.
From chance agreement to a corrected score
Two quantities measure how far annotators got beyond luck:
- Achieved: po − pe, the agreement above chance.
- Available: 1 − p_e, the most agreement above chance that was possible.
Their ratio is the chance-corrected score:
score = (po − pe) / (1 − p_e)
For the example, that is (0.8 − 0.38) / (1 − 0.38) = 0.42 / 0.62 ≈ 0.677. This is Cohen's kappa. The next chapter treats it in detail. Here the point is how the correction behaves:
- If po equals pe, the score is 0. The annotators agree exactly as often as independent guessing would predict.
- If po is 1, the score is 1, whatever pe is.
- If po is below pe, the score is negative. The annotators agree less often than chance predicts, which usually signals a systematic problem such as swapped label meanings or a misread guideline.
Computing it in Python
The function below follows the formula directly. It counts how often each annotator used each label, then sums the products of those counts.
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))It prints 0.677, matching the hand calculation. The set union set(a) | set(b) matters. If one annotator never uses a label, Counter returns 0 for it, so that label adds nothing to p_e. Looping over only one annotator's labels would still work here, but the union is the safe habit.
Seeing the effect of prevalence
The same observed agreement can produce very different corrected scores. The next snippet keeps p_o at 0.90 in both cases but changes the label balance.
from collections import Counter
def agreement_parts(a, b):
n = len(a)
p_o = sum(x == y for x, y in zip(a, b)) / n
ca, cb = Counter(a), Counter(b)
p_e = sum(ca[l] * cb[l] for l in set(a) | set(b)) / n ** 2
return p_o, p_e, (p_o - p_e) / (1 - p_e)
# Balanced labels: 50 pos, 50 neg; annotators differ on 10 items
a1 = ["pos"] * 50 + ["neg"] * 50
a2 = ["pos"] * 45 + ["neg"] * 5 + ["neg"] * 45 + ["pos"] * 5
# Skewed labels: 90 ok, 10 spam; annotators differ on 10 items
b1 = ["ok"] * 90 + ["spam"] * 10
b2 = ["ok"] * 85 + ["spam"] * 5 + ["spam"] * 5 + ["ok"] * 5
for name, (x, y) in {"balanced": (a1, a2), "skewed": (b1, b2)}.items():
p_o, p_e, score = agreement_parts(x, y)
print(f"{name}: p_o={p_o:.2f} p_e={p_e:.2f} score={score:.3f}")Expected output:
balanced: p_o=0.90 p_e=0.50 score=0.800
skewed: p_o=0.90 p_e=0.82 score=0.512Both pairs agree on 90 of 100 items. In the skewed set, most of that agreement is expected from the dominant label, so the corrected score is much lower. This is the mechanism behind the prevalence problems covered later in the tutorial.
Common errors
- Computing p_e from the pooled labels of both annotators. Pooling treats the two annotators as interchangeable. The formula above uses each annotator's own frequencies, so differences in how they use labels (bias) are reflected in the result.
- Using too few items. With 10 items, one disagreement changes p_o by 0.1. Treat small pilot scores as rough signals, not stable estimates.
- Dividing by zero. If both annotators use only one label, p_e is 1 and the denominator is 0. The score is undefined. Check for this case before reporting.
- Treating a high score as proof of correct labels. Annotators can agree and still be wrong, for example if the guidelines teach a shared misreading. Chance correction measures consistency, not truth.
- Counting unseen labels. Include every label in the schema when you build count tables, even those no one used, or your tables may differ in shape between runs.
Exercise
Two annotators label 20 items as yes or no. They agree on 16 items. Annotator 1 said yes 15 times and Annotator 2 said yes 10 times. Compute po, pe, and the chance-corrected score.
Show answer
po = 16/20 = 0.8. Annotator 1: yes 0.75, no 0.25. Annotator 2: yes 0.5, no 0.5. pe = 0.75 × 0.5 + 0.25 × 0.5 = 0.375 + 0.125 = 0.5. Score = (0.8 − 0.5) / (1 − 0.5) = 0.6.
Exercise
Two annotators both label every item ok. Why is the chance-corrected score undefined, and what should you report instead?
Show answer
Both annotators use one label with proportion 1, so pe = 1 × 1 = 1 and the denominator 1 − pe is 0. Observed agreement is 1.0, but there is no variation to separate real agreement from chance. Report the label distribution and observed agreement, and note that the score cannot be computed. Collect or sample items so that more than one label appears.
Exercise
A pilot round gives po = 0.70 and pe = 0.75. What does the score show, and what would you check first?
Show answer
The score is (0.70 − 0.75) / (1 − 0.75) = −0.2. The annotators agree less often than chance predicts. Check first for a systematic problem: swapped or misunderstood label meanings, a guideline that two annotators read in opposite ways, or a data formatting error such as misaligned item order between the two files.