Definitioncore
Alignment error rate
Also called AER.
1 - (|A and S| + |A and P|) / (|A| + |S|) for predicted links A, sure gold links S and possible gold links P.
The alignment error rate (AER) measures how well a predicted word alignment matches a gold alignment made by people. The annotators mark two kinds of links: sure links , on which they agree, and possible links , which are acceptable but not certain, for example for words of an idiom. For a predicted set of links , . A predicted link in is not an error, and a sure link that misses is an error. Och and Ney (2000) defined it, and for some years it was the standard measure of word aligners. It is a mix of precision and recall, but not a proper F measure when , and Fraser and Marcu (2007) showed that a lower AER does not reliably give better translations.
Definition
A link is a pair of a source position and a target position . Let be the sure and possible gold links of a sentence pair, and the predicted links. Then
Precision is measured against the possible links and recall against the sure links, so a link in can raise precision but is never required. For a test set, the code of this page sums the four counts over all sentence pairs before the division. This value is not the mean of the sentence values, so a report should say which of the two it gives.
Three facts follow from the definition.
- Range. and , so . AER is 1 when has no link in .
- Zero error. AER is 0 exactly when both inequalities are equalities, that is when . So every between the sure and the possible links is perfect.
- A weighted mean. , a mean of precision and recall weighted by the sizes of and . If , it is , the Dice coefficient, which equals the F1 score . If , the weights depend on , and is not the harmonic mean of the two.
The pseudocode below computes AER for one sentence pair.
AER(A, S, P)- 1
check that S ⊆ P - 2
hit_S ← |A ∩ S| - 3
hit_P ← |A ∩ P| - 4
return 1 - (hit_S + hit_P) / (|A| + |S|)
With hash sets the cost is expected time.
Examples and non-examples
- A small case. Let , and . Then , , and . Precision is and recall is , so F1 is and , which is not .
- Perfect alignments. With and , the sets , and all have AER 0.
- One error of each kind. With the same and , the prediction has one wrong link and AER ; the prediction misses one sure link and has AER . An empty has AER 1.
- Same AER, different quality (Fraser and Marcu 2007). Let . In the first alignment, 50 links are sure and the other 50 are outside : precision and recall are both . In the second, 25 links are sure, 50 are possible and 25 are outside : precision is and recall . Both have , but the balanced F is and .
- Sentence and corpus values. For two pairs with sentence AERs 0 and , where the second has 3 predicted and 1 sure link, the corpus value with summed counts is , not the mean .
- Non-example: a gold set with . The definition needs every sure link to be possible. The code rejects such a gold set.
- Non-example: an empty and an empty . The fraction is , and AER has no value.
Related definitions
- Precision and recall are the two measures that AER mixes. Fraser and Marcu (2007) proposed the weighted F measure instead, with tuned for each language pair and task. In their experiments the squared correlation with BLEU was 0.16 for , 0.20 for F with sure and possible links, and 0.67 for F on gold links without the sure and possible distinction.
- When , is the Dice coefficient of and , which is a monotone function of their Jaccard similarity : Dice .
- The aligners evaluated with AER include IBM Model 1, the HMM alignment model and fast_align. Symmetrization heuristics such as grow-diag-final trade precision for recall, and AER is one way to compare them.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/alignment-error-rate/. The authors are listed in prov/T1YMPJ.jsonl.
"""Alignment error rate (Och and Ney 2000, 2003) and the measures around it.
A link is a pair (j, i): source position j, target position i. A is the
predicted set, S the sure gold links and P the possible gold links, with
S a subset of P. All results are exact fractions.
precision(A, P) = |A & P| / |A|
recall(A, S) = |A & S| / |S|
AER(A, S, P) = 1 - (|A & S| + |A & P|) / (|A| + |S|)
"""
from fractions import Fraction
from typing import Iterable, Set, Tuple
Link = Tuple[int, int]
def _check(s: Set[Link], p: Set[Link]) -> None:
if not s <= p:
raise ValueError("the sure links S must be a subset of the possible links P")
def aer(a: Iterable[Link], s: Iterable[Link], p: Iterable[Link]) -> Fraction:
"""The alignment error rate. Raises ValueError if S is not inside P or if A and S are both empty."""
a, s, p = set(a), set(s), set(p)
_check(s, p) # @line L1
if not a and not s:
raise ValueError("AER is undefined when A and S are both empty")
hit_s = len(a & s) # @line L2
hit_p = len(a & p) # @line L3
return 1 - Fraction(hit_s + hit_p, len(a) + len(s)) # @line L4
def precision(a: Iterable[Link], p: Iterable[Link]) -> Fraction:
a, p = set(a), set(p)
if not a:
raise ValueError("precision is undefined for an empty A")
return Fraction(len(a & p), len(a))
def recall(a: Iterable[Link], s: Iterable[Link]) -> Fraction:
a, s = set(a), set(s)
if not s:
raise ValueError("recall is undefined for an empty S")
return Fraction(len(a & s), len(s))
def f_measure(a: Iterable[Link], s: Iterable[Link], p: Iterable[Link], alpha: Fraction = Fraction(1, 2)) -> Fraction:
"""The weighted F of Fraser and Marcu: 1 / (alpha / precision(A, P) + (1 - alpha) / recall(A, S)).
Returns 0 if the precision or the recall is 0.
"""
a, s, p = set(a), set(s), set(p)
_check(s, p)
pr, rc = precision(a, p), recall(a, s)
if pr == 0 or rc == 0:
return Fraction(0)
return 1 / (alpha / pr + (1 - alpha) / rc)
def corpus_aer(items: Iterable[Tuple[Iterable[Link], Iterable[Link], Iterable[Link]]]) -> Fraction:
"""AER over a test set: the counts of all sentences are summed before the division."""
hit_s = hit_p = size_a = size_s = 0
for a, s, p in items:
a, s, p = set(a), set(s), set(p)
_check(s, p)
hit_s += len(a & s)
hit_p += len(a & p)
size_a += len(a)
size_s += len(s)
if size_a + size_s == 0:
raise ValueError("AER is undefined when A and S are both empty")
return 1 - Fraction(hit_s + hit_p, size_a + size_s)
test_alignment_error_rate.py 83 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/alignment-error-rate/. The authors are listed in prov/T1YMPJ.jsonl.
import random
from fractions import Fraction as F
import pytest
from alignment_error_rate import aer, corpus_aer, f_measure, precision, recall
def test_check_value() -> None:
s = {(1, 1), (2, 2)}
p = s | {(3, 2)}
a = {(1, 1), (3, 2), (3, 3)}
assert aer(a, s, p) == F(2, 5)
assert precision(a, p) == F(2, 3) and recall(a, s) == F(1, 2)
def test_edge_cases() -> None:
s = {(1, 1), (2, 2)}
p = s | {(3, 2), (3, 3)}
assert aer(s, s, p) == 0 # A = S
assert aer(p, s, p) == 0 # A = P: possible links cost nothing
assert aer({(1, 1), (2, 2), (3, 3)}, s, p) == 0 # any A between S and P
assert aer(set(), s, p) == 1 # empty prediction
assert aer({(5, 5), (6, 6)}, s, p) == 1 # nothing right
assert aer({(1, 1), (2, 2), (5, 5)}, s, p) == F(1, 5) # one wrong extra link
assert aer({(1, 1)}, s, p) == F(1, 3) # one sure link missing
with pytest.raises(ValueError):
aer({(1, 1)}, {(1, 1)}, set())
with pytest.raises(ValueError):
aer(set(), set(), set())
def random_links(rng, k, n):
return {(rng.randint(1, n), rng.randint(1, n)) for _ in range(k)}
def test_equals_one_minus_f1_when_s_equals_p() -> None:
rng = random.Random(51)
for _ in range(200):
s = random_links(rng, rng.randint(1, 8), 5)
a = random_links(rng, rng.randint(1, 8), 5)
pr, rc = precision(a, s), recall(a, s)
f1 = 2 * pr * rc / (pr + rc) if pr + rc else F(0)
assert aer(a, s, s) == 1 - f1
assert f_measure(a, s, s) == f1
def test_fraser_marcu_example() -> None:
# |A| = |S| = 100: the same AER, different F
s = {(k, 0) for k in range(100)}
p = s | {(k, 1) for k in range(100)}
a1 = {(k, 0) for k in range(50)} | {(k, 2) for k in range(50)} # 50 in S, 50 outside P
a2 = {(k, 0) for k in range(25)} | {(k, 1) for k in range(50)} | {(k, 3) for k in range(25)}
assert len(a1) == len(a2) == 100
assert len(a1 & p) == 50 and len(a1 & s) == 50
assert len(a2 & p) == 75 and len(a2 & s) == 25
assert 1 - aer(a1, s, p) == F(1, 2) == 1 - aer(a2, s, p)
assert f_measure(a1, s, p) == F(1, 2)
assert f_measure(a2, s, p) == F(3, 8)
def test_range_and_monotone_in_possible_links() -> None:
rng = random.Random(52)
for _ in range(300):
s = random_links(rng, rng.randint(0, 6), 4)
p = s | random_links(rng, rng.randint(0, 6), 4)
a = random_links(rng, rng.randint(1, 6), 4)
v = aer(a, s, p)
assert 0 <= v <= 1
# turning a predicted link that is outside P into a possible link never raises AER
extra = a - p
if extra:
assert aer(a, s, p | {next(iter(extra))}) <= v
def test_corpus_aer_sums_counts() -> None:
items = [({(1, 1)}, {(1, 1)}, {(1, 1)}), ({(1, 2), (2, 2), (3, 3)}, {(1, 1)}, {(1, 1), (2, 2)})]
# sentence AERs are 0 and 1 - 1/4; the corpus value pools the counts: 1 - (1 + 2) / (4 + 2)
assert aer(*items[0]) == 0 and aer(*items[1]) == F(3, 4)
assert corpus_aer(items) == F(1, 2)
References
- Och and Ney 2000, the evaluation with sure and possible links and the definition of the alignment error rate
- Och and Ney 2003, Section 5 (the annotation with sure and possible links, precision, recall and the alignment error rate)
- Alexander Fraser and Daniel Marcu, Measuring word alignment quality for statistical machine translation, Computational Linguistics 33(3), 2007, pages 293 to 303, the example of two alignments with the same AER, the weighted F measure, and the correlation of the measures with BLEU
- Philipp Koehn, Statistical Machine Translation, Cambridge University Press, 2010, Section 4.5 (word alignment and its evaluation)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag T1YMPJ Edit this page History Report an error Markdown