Algorithm Search⌘K

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 SS, on which they agree, and possible links P⊇SP \supseteq S, which are acceptable but not certain, for example for words of an idiom. For a predicted set of links AA, AER=1−(∣A∩S∣+∣A∩P∣)/(∣A∣+∣S∣)\mathrm{AER} = 1 - (|A \cap S| + |A \cap P|) / (|A| + |S|). A predicted link in PP is not an error, and a sure link that AA 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 S≠PS \ne P, and Fraser and Marcu (2007) showed that a lower AER does not reliably give better translations.

Definition

A link is a pair (j,i)(j, i) of a source position jj and a target position ii. Let S⊆PS \subseteq P be the sure and possible gold links of a sentence pair, and AA the predicted links. Then

precision(A,P)=∣A∩P∣∣A∣,recall(A,S)=∣A∩S∣∣S∣,\mathrm{precision}(A, P) = \frac{|A \cap P|}{|A|}, \qquad \mathrm{recall}(A, S) = \frac{|A \cap S|}{|S|},
AER(A;S,P)=1−∣A∩S∣+∣A∩P∣∣A∣+∣S∣.\mathrm{AER}(A; S, P) = 1 - \frac{|A \cap S| + |A \cap P|}{|A| + |S|}.

Precision is measured against the possible links and recall against the sure links, so a link in P∖SP \setminus S 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. ∣A∩S∣≤∣S∣|A \cap S| \le |S| and ∣A∩P∣≤∣A∣|A \cap P| \le |A|, so 0≤AER≤10 \le \mathrm{AER} \le 1. AER is 1 when AA has no link in PP.
  • Zero error. AER is 0 exactly when both inequalities are equalities, that is when S⊆A⊆PS \subseteq A \subseteq P. So every AA between the sure and the possible links is perfect.
  • A weighted mean. 1−AER=(∣A∣⋅precision+∣S∣⋅recall)/(∣A∣+∣S∣)1 - \mathrm{AER} = (|A| \cdot \mathrm{precision} + |S| \cdot \mathrm{recall}) / (|A| + |S|), a mean of precision and recall weighted by the sizes of AA and SS. If S=PS = P, it is 2∣A∩S∣/(∣A∣+∣S∣)2|A \cap S| / (|A| + |S|), the Dice coefficient, which equals the F1 score 2pr/(p+r)2 p r / (p + r). If S≠PS \ne P, the weights depend on AA, and 1−AER1 - \mathrm{AER} is not the harmonic mean of the two.

The pseudocode below computes AER for one sentence pair.

  1. AER(A, S, P)
  2. 1check that S ⊆ P
  3. 2hit_S ← |A ∩ S|
  4. 3hit_P ← |A ∩ P|
  5. 4return 1 - (hit_S + hit_P) / (|A| + |S|)

With hash sets the cost is Θ(∣A∣+∣S∣+∣P∣)\Theta(|A| + |S| + |P|) expected time.

Examples and non-examples

  • A small case. Let S={(1,1),(2,2)}S = \{(1,1), (2,2)\}, P=S∪{(3,2)}P = S \cup \{(3,2)\} and A={(1,1),(3,2),(3,3)}A = \{(1,1), (3,2), (3,3)\}. Then ∣A∩S∣=1|A \cap S| = 1, ∣A∩P∣=2|A \cap P| = 2, and AER=1−(1+2)/(3+2)=2/5\mathrm{AER} = 1 - (1 + 2)/(3 + 2) = 2/5. Precision is 2/32/3 and recall is 1/21/2, so F1 is 4/74/7 and 1−F1=3/71 - \mathrm{F1} = 3/7, which is not 2/52/5.
  • Perfect alignments. With S={(1,1),(2,2)}S = \{(1,1), (2,2)\} and P=S∪{(3,2),(3,3)}P = S \cup \{(3,2), (3,3)\}, the sets A=SA = S, A=PA = P and A=S∪{(3,3)}A = S \cup \{(3,3)\} all have AER 0.
  • One error of each kind. With the same SS and PP, the prediction S∪{(5,5)}S \cup \{(5,5)\} has one wrong link and AER 1/51/5; the prediction {(1,1)}\{(1,1)\} misses one sure link and has AER 1/31/3. An empty AA has AER 1.
  • Same AER, different quality (Fraser and Marcu 2007). Let ∣A∣=∣S∣=100|A| = |S| = 100. In the first alignment, 50 links are sure and the other 50 are outside PP: precision and recall are both 0.50.5. In the second, 25 links are sure, 50 are possible and 25 are outside PP: precision is 0.750.75 and recall 0.250.25. Both have 1−AER=(50+50)/200=(25+75)/200=0.51 - \mathrm{AER} = (50 + 50)/200 = (25 + 75)/200 = 0.5, but the balanced F is 0.50.5 and 0.3750.375.
  • Sentence and corpus values. For two pairs with sentence AERs 0 and 3/43/4, where the second has 3 predicted and 1 sure link, the corpus value with summed counts is 1−3/6=1/21 - 3/6 = 1/2, not the mean 3/83/8.
  • Non-example: a gold set with S⊈PS \not\subseteq P. The definition needs every sure link to be possible. The code rejects such a gold set.
  • Non-example: an empty AA and an empty SS. The fraction is 0/00/0, and AER has no value.
  • Precision and recall are the two measures that AER mixes. Fraser and Marcu (2007) proposed the weighted F measure 1/(α/precision+(1−α)/recall)1 / (\alpha / \mathrm{precision} + (1 - \alpha) / \mathrm{recall}) instead, with α\alpha tuned for each language pair and task. In their experiments the squared correlation r2r^2 with BLEU was 0.16 for 1−AER1 - \mathrm{AER}, 0.20 for F with sure and possible links, and 0.67 for F on gold links without the sure and possible distinction.
  • When S=PS = P, 1−AER1 - \mathrm{AER} is the Dice coefficient of AA and SS, which is a monotone function of their Jaccard similarity JJ: Dice =2J/(1+J)= 2J/(1 + J).
  • 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.

alignment_error_rate.pyI0RawGitHub
# 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

  1. Och and Ney 2000, the evaluation with sure and possible links and the definition of the alignment error rate
  2. Och and Ney 2003, Section 5 (the annotation with sure and possible links, precision, recall and the alignment error rate)
  3. 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
  4. 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.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
Code (python)I0 not checked yet
  • the tests did not run in this build
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/T1YMPJ/text.yaml