Skip to content

Anchor Triangle and Chart

The chart is fixed by three anchor trees. This module builds the anchor triangle from pairwise clade-branch-score distances, validates its conditioning, and places a query tree in barycentric coordinates. See A chart of tree space fixed by three anchor trees.

AnchorTriangle dataclass

Geometry of the three-anchor 2D chart.

The plain constructor stores already-computed geometry. To build a chart from three anchor trees (deriving the metric, measuring distances, running the validation checks, and solving the embedding), use :meth:from_anchors.

Attributes:

Name Type Description
P1, P2, P3 (ndarray, shape(2))

Anchor positions in the local Euclidean chart.

r1, r2, r3 HifukuTree

The three anchor trees.

dist (ndarray, shape(3, 3))

Pairwise metric distances.

qual (ndarray, shape(3, 3))

Pairwise B_frac quality scores.

metric TreeMetric

The chart's metric instance. The metric is normalized per pair of trees, so charts built with one topology weight share one metric. Required: a chart without a metric is broken.

Source code in src/hifuku/anchor_triangle.py
@dataclass
class AnchorTriangle:
    """
    Geometry of the three-anchor 2D chart.

    The plain constructor stores already-computed geometry.  To build a chart
    from three anchor trees (deriving the metric, measuring distances, running
    the validation checks, and solving the embedding), use
    :meth:`from_anchors`.

    Attributes
    ----------
    P1, P2, P3 : np.ndarray, shape (2,)
        Anchor positions in the local Euclidean chart.
    r1, r2, r3 : HifukuTree
        The three anchor trees.
    dist : np.ndarray, shape (3, 3)
        Pairwise metric distances.
    qual : np.ndarray, shape (3, 3)
        Pairwise B_frac quality scores.
    metric : TreeMetric
        The chart's metric instance.  The metric is normalized per pair of
        trees, so charts built with one topology weight share one metric.
        Required: a chart without a metric is broken.
    """
    P1: np.ndarray
    P2: np.ndarray
    P3: np.ndarray
    r1: HifukuTree
    r2: HifukuTree
    r3: HifukuTree
    dist: np.ndarray
    qual: np.ndarray
    metric: object = None

    @property
    def tree_metric(self) -> TreeMetric:
        """The chart's metric instance.  Raises if the chart is missing its
        metric or its anchors, both of which a usable chart must have."""
        if self.metric is None:
            raise ValueError(
                "AnchorTriangle has no metric; build the chart with validate_anchors."
            )
        if self.r1 is None or self.r2 is None or self.r3 is None:
            raise ValueError("AnchorTriangle is missing anchor trees.")
        return self.metric

    @classmethod
    def from_anchors(
        cls,
        r1: HifukuTree,
        r2: HifukuTree,
        r3: HifukuTree,
        *,
        w: float = METRIC_W,
        b_frac_threshold: float = B_FRAC_THRESHOLD,
        anchor_q_min: float = ANCHOR_Q_MIN,
    ) -> "AnchorTriangle":
        """
        Build a validated chart from three anchor trees.

        This is the entry point that turns three anchor trees into a chart.  It
        does the work the plain constructor does not: it builds the chart's
        metric with topology weight ``w``, measures the three pairwise distances,
        checks that the anchors form a usable chart (any failure raises), solves
        the triangle embedding, and returns a fully populated instance.  The
        plain constructor is the low-level counterpart: it takes the already
        computed fields (``P1``, ``P2``, ``P3``, ``dist``, ``qual``,
        ``metric``, ...) and only stores them.  Use it when the geometry is
        already known (loading a saved chart, or a test with fixed geometry);
        use ``from_anchors`` when starting from trees.

        Three checks run in order (any failure raises ValueError and stops):

        1. **Metric saturation** - the B_frac quality score for each of the
           three anchor pairs must be >= b_frac_threshold.  Near-zero B_frac
           means the branch-score metric has lost resolution for that pair
           (metric saturation; distinct from substitution saturation, which is
           a property of the sequence model).

        2. **Chartability** (triangle inequality / Euclidean-embeddability) -
           the three distances must form a valid planar triangle.  Equivalently,
           the double-centered squared-distance matrix must be positive
           semidefinite (gower1966pcoa, specialized to 3 points).  Checked
           before the sqrt in ``embed_anchor_triangle``.

        3. **Triangle quality** Q >= anchor_q_min - the anchor-position
           covariance must be non-degenerate (the three anchors are not
           near-collinear).  The second axis carries too little information to
           form a useful 2D CV when Q is below this floor.

        Parameters
        ----------
        r1, r2, r3 : HifukuTree
            Anchor trees restricted to the shared global namespace, with
            ``global_leaf_indices`` set.
        w : float
            Topology weight in [0, 1] for the chart's metric (default METRIC_W).
            The metric is normalized per pair of trees, so every chart built
            with one ``w`` shares one metric.
        b_frac_threshold : float
            Minimum B_frac quality score per anchor pair (default
            B_FRAC_THRESHOLD).
        anchor_q_min : float
            Minimum triangle quality Q (default ANCHOR_Q_MIN).

        Returns
        -------
        AnchorTriangle
            The fully-built chart: embedding positions, distance/quality
            matrices, the three anchors, and the metric instance the chart owns.

        Raises
        ------
        ValueError
            On metric saturation, chartability failure, or insufficient triangle
            quality.  The error message names the failing pair/triple and the
            relevant diagnostic values.
        """
        metric = BranchScoreMetric(w=w)
        dist_matrix, qual = compute_anchor_distances(r1, r2, r3, metric)

        # 1. Metric saturation check.
        pair_names = [("r1", "r2"), ("r1", "r3"), ("r2", "r3")]
        pair_indices = [(0, 1), (0, 2), (1, 2)]
        for (i, j), (na, nb) in zip(pair_indices, pair_names):
            q = qual[i, j]
            if q < b_frac_threshold:
                raise ValueError(
                    f"Metric saturation: anchor pair ({na}, {nb}) has B_frac = "
                    f"{q:.4f} < b_frac_threshold = {b_frac_threshold}.  "
                    f"The branch-score metric has lost resolution for this pair "
                    f"(metric saturation; distinct from substitution saturation).  "
                    f"Distance d = {dist_matrix[i,j]:.4f}."
                )

        # 2. Chartability check (triangle inequality / Schoenberg condition).
        d12 = float(dist_matrix[0, 1])
        d13 = float(dist_matrix[0, 2])
        d23 = float(dist_matrix[1, 2])
        P1, P2, P3 = embed_anchor_triangle(d12, d13, d23)

        # 3. Triangle quality check.
        Q, lmin, lmax = triangle_quality(P1, P2, P3)
        if Q < anchor_q_min:
            raise ValueError(
                f"Triangle quality Q = {Q:.4f} < anchor_q_min = {anchor_q_min}.  "
                f"Anchor distances: d12={d12:.4g}, d13={d13:.4g}, d23={d23:.4g}.  "
                f"Covariance eigenvalues: lambda_min={lmin:.4g}, lambda_max={lmax:.4g}.  "
                f"The three anchors are near-collinear; the second CV axis carries "
                f"too little information."
            )

        return cls(
            P1=P1,
            P2=P2,
            P3=P3,
            r1=r1,
            r2=r2,
            r3=r3,
            dist=dist_matrix,
            qual=qual,
            metric=metric,
        )

tree_metric property

tree_metric: TreeMetric

The chart's metric instance. Raises if the chart is missing its metric or its anchors, both of which a usable chart must have.

from_anchors classmethod

from_anchors(r1: HifukuTree, r2: HifukuTree, r3: HifukuTree, *, w: float = METRIC_W, b_frac_threshold: float = B_FRAC_THRESHOLD, anchor_q_min: float = ANCHOR_Q_MIN) -> 'AnchorTriangle'

Build a validated chart from three anchor trees.

This is the entry point that turns three anchor trees into a chart. It does the work the plain constructor does not: it builds the chart's metric with topology weight w, measures the three pairwise distances, checks that the anchors form a usable chart (any failure raises), solves the triangle embedding, and returns a fully populated instance. The plain constructor is the low-level counterpart: it takes the already computed fields (P1, P2, P3, dist, qual, metric, ...) and only stores them. Use it when the geometry is already known (loading a saved chart, or a test with fixed geometry); use from_anchors when starting from trees.

Three checks run in order (any failure raises ValueError and stops):

  1. Metric saturation - the B_frac quality score for each of the three anchor pairs must be >= b_frac_threshold. Near-zero B_frac means the branch-score metric has lost resolution for that pair (metric saturation; distinct from substitution saturation, which is a property of the sequence model).

  2. Chartability (triangle inequality / Euclidean-embeddability) - the three distances must form a valid planar triangle. Equivalently, the double-centered squared-distance matrix must be positive semidefinite (gower1966pcoa, specialized to 3 points). Checked before the sqrt in embed_anchor_triangle.

  3. Triangle quality Q >= anchor_q_min - the anchor-position covariance must be non-degenerate (the three anchors are not near-collinear). The second axis carries too little information to form a useful 2D CV when Q is below this floor.

Parameters:

Name Type Description Default
r1 HifukuTree

Anchor trees restricted to the shared global namespace, with global_leaf_indices set.

required
r2 HifukuTree

Anchor trees restricted to the shared global namespace, with global_leaf_indices set.

required
r3 HifukuTree

Anchor trees restricted to the shared global namespace, with global_leaf_indices set.

required
w float

Topology weight in [0, 1] for the chart's metric (default METRIC_W). The metric is normalized per pair of trees, so every chart built with one w shares one metric.

METRIC_W
b_frac_threshold float

Minimum B_frac quality score per anchor pair (default B_FRAC_THRESHOLD).

B_FRAC_THRESHOLD
anchor_q_min float

Minimum triangle quality Q (default ANCHOR_Q_MIN).

ANCHOR_Q_MIN

Returns:

Type Description
AnchorTriangle

The fully-built chart: embedding positions, distance/quality matrices, the three anchors, and the metric instance the chart owns.

Raises:

Type Description
ValueError

On metric saturation, chartability failure, or insufficient triangle quality. The error message names the failing pair/triple and the relevant diagnostic values.

Source code in src/hifuku/anchor_triangle.py
@classmethod
def from_anchors(
    cls,
    r1: HifukuTree,
    r2: HifukuTree,
    r3: HifukuTree,
    *,
    w: float = METRIC_W,
    b_frac_threshold: float = B_FRAC_THRESHOLD,
    anchor_q_min: float = ANCHOR_Q_MIN,
) -> "AnchorTriangle":
    """
    Build a validated chart from three anchor trees.

    This is the entry point that turns three anchor trees into a chart.  It
    does the work the plain constructor does not: it builds the chart's
    metric with topology weight ``w``, measures the three pairwise distances,
    checks that the anchors form a usable chart (any failure raises), solves
    the triangle embedding, and returns a fully populated instance.  The
    plain constructor is the low-level counterpart: it takes the already
    computed fields (``P1``, ``P2``, ``P3``, ``dist``, ``qual``,
    ``metric``, ...) and only stores them.  Use it when the geometry is
    already known (loading a saved chart, or a test with fixed geometry);
    use ``from_anchors`` when starting from trees.

    Three checks run in order (any failure raises ValueError and stops):

    1. **Metric saturation** - the B_frac quality score for each of the
       three anchor pairs must be >= b_frac_threshold.  Near-zero B_frac
       means the branch-score metric has lost resolution for that pair
       (metric saturation; distinct from substitution saturation, which is
       a property of the sequence model).

    2. **Chartability** (triangle inequality / Euclidean-embeddability) -
       the three distances must form a valid planar triangle.  Equivalently,
       the double-centered squared-distance matrix must be positive
       semidefinite (gower1966pcoa, specialized to 3 points).  Checked
       before the sqrt in ``embed_anchor_triangle``.

    3. **Triangle quality** Q >= anchor_q_min - the anchor-position
       covariance must be non-degenerate (the three anchors are not
       near-collinear).  The second axis carries too little information to
       form a useful 2D CV when Q is below this floor.

    Parameters
    ----------
    r1, r2, r3 : HifukuTree
        Anchor trees restricted to the shared global namespace, with
        ``global_leaf_indices`` set.
    w : float
        Topology weight in [0, 1] for the chart's metric (default METRIC_W).
        The metric is normalized per pair of trees, so every chart built
        with one ``w`` shares one metric.
    b_frac_threshold : float
        Minimum B_frac quality score per anchor pair (default
        B_FRAC_THRESHOLD).
    anchor_q_min : float
        Minimum triangle quality Q (default ANCHOR_Q_MIN).

    Returns
    -------
    AnchorTriangle
        The fully-built chart: embedding positions, distance/quality
        matrices, the three anchors, and the metric instance the chart owns.

    Raises
    ------
    ValueError
        On metric saturation, chartability failure, or insufficient triangle
        quality.  The error message names the failing pair/triple and the
        relevant diagnostic values.
    """
    metric = BranchScoreMetric(w=w)
    dist_matrix, qual = compute_anchor_distances(r1, r2, r3, metric)

    # 1. Metric saturation check.
    pair_names = [("r1", "r2"), ("r1", "r3"), ("r2", "r3")]
    pair_indices = [(0, 1), (0, 2), (1, 2)]
    for (i, j), (na, nb) in zip(pair_indices, pair_names):
        q = qual[i, j]
        if q < b_frac_threshold:
            raise ValueError(
                f"Metric saturation: anchor pair ({na}, {nb}) has B_frac = "
                f"{q:.4f} < b_frac_threshold = {b_frac_threshold}.  "
                f"The branch-score metric has lost resolution for this pair "
                f"(metric saturation; distinct from substitution saturation).  "
                f"Distance d = {dist_matrix[i,j]:.4f}."
            )

    # 2. Chartability check (triangle inequality / Schoenberg condition).
    d12 = float(dist_matrix[0, 1])
    d13 = float(dist_matrix[0, 2])
    d23 = float(dist_matrix[1, 2])
    P1, P2, P3 = embed_anchor_triangle(d12, d13, d23)

    # 3. Triangle quality check.
    Q, lmin, lmax = triangle_quality(P1, P2, P3)
    if Q < anchor_q_min:
        raise ValueError(
            f"Triangle quality Q = {Q:.4f} < anchor_q_min = {anchor_q_min}.  "
            f"Anchor distances: d12={d12:.4g}, d13={d13:.4g}, d23={d23:.4g}.  "
            f"Covariance eigenvalues: lambda_min={lmin:.4g}, lambda_max={lmax:.4g}.  "
            f"The three anchors are near-collinear; the second CV axis carries "
            f"too little information."
        )

    return cls(
        P1=P1,
        P2=P2,
        P3=P3,
        r1=r1,
        r2=r2,
        r3=r3,
        dist=dist_matrix,
        qual=qual,
        metric=metric,
    )

TreeMetric

Bases: Protocol

Protocol for tree distance metrics used by the 2D CV.

A callable that takes two HifukuTree objects and returns a (distance, quality) pair. The quality score is a reliability / confidence value interpretable as a likelihood-like weight: high quality = the metric distinguishes the two trees well; low quality = metric saturation (the metric has lost resolution; distinct from substitution saturation).

BranchScoreMetric below is the only implementation. Code outside it must not assume its internals.

Source code in src/hifuku/metric/branch_score.py
@runtime_checkable
class TreeMetric(Protocol):
    """
    Protocol for tree distance metrics used by the 2D CV.

    A callable that takes two HifukuTree objects and returns a (distance,
    quality) pair.  The quality score is a reliability / confidence value
    interpretable as a likelihood-like weight: high quality = the metric
    distinguishes the two trees well; low quality = metric saturation (the
    metric has lost resolution; distinct from substitution saturation).

    BranchScoreMetric below is the only implementation.  Code outside it must not
    assume its internals.
    """

    def __call__(
        self, t1: HifukuTree, t2: HifukuTree
    ) -> tuple[float, float]:
        """
        Parameters
        ----------
        t1, t2 : HifukuTree
            Trees with ``global_leaf_indices`` set and restricted to the same
            global taxon namespace.

        Returns
        -------
        (distance, quality) : (float, float)
            ``distance >= 0``.  ``quality`` in [0, 1], where 0 = metric
            saturation and 1 = identical trees.
        """
        ...

BranchScoreMetric dataclass

Branch-score implementation of the TreeMetric protocol.

Both trees are cut to the taxa they share, and the distance is read from the subtree each induces there:

d^2 = (1 - w) * (1 - B / sqrt(A C)) + w * RF / (2 m - 6)

A and C are the squared lengths of the two split-length vectors, B is their inner product, RF is the count of splits held by one tree and not the other, and m is the shared taxon count. w in [0, 1] weights topology against branch length.

Each term is a ratio of two sums over the same splits, so the taxon count cancels and the distance reads dissimilarity alone. The branch score itself grows with the taxon count, about 15-fold from 32 shared taxa to 1024, which would otherwise make two pairs of different overlap incomparable. Both terms lie in [0, 1], so w means the same thing on every data set.

The branch term is half the squared distance between the two vectors after each is scaled to unit length, so on one taxon set it is squared Euclidean and the plane embedding holds.

A pair sharing fewer than METRIC_MIN_SHARED_TAXA taxa has no distance, and the distance reads NaN. Trees that share no taxa share no topology, so there is nothing for the distance to measure.

quality = B_frac in [0, 1]; the length-overlap fraction. Near 0 = branch- score saturation; near 1 = similar trees.

Kuhner & Felsenstein (1994), robinson1981rf.

Source code in src/hifuku/metric/branch_score.py
@dataclass
class BranchScoreMetric:
    """
    Branch-score implementation of the TreeMetric protocol.

    Both trees are cut to the taxa they share, and the distance is read from the
    subtree each induces there:

        d^2 = (1 - w) * (1 - B / sqrt(A C)) + w * RF / (2 m - 6)

    ``A`` and ``C`` are the squared lengths of the two split-length vectors,
    ``B`` is their inner product, ``RF`` is the count of splits held by one tree
    and not the other, and ``m`` is the shared taxon count.  ``w`` in [0, 1]
    weights topology against branch length.

    Each term is a ratio of two sums over the same splits, so the taxon count
    cancels and the distance reads dissimilarity alone.  The branch score itself
    grows with the taxon count, about 15-fold from 32 shared taxa to 1024, which
    would otherwise make two pairs of different overlap incomparable.  Both terms
    lie in [0, 1], so ``w`` means the same thing on every data set.

    The branch term is half the squared distance between the two vectors after
    each is scaled to unit length, so on one taxon set it is squared Euclidean
    and the plane embedding holds.

    A pair sharing fewer than ``METRIC_MIN_SHARED_TAXA`` taxa has no distance,
    and the distance reads NaN.  Trees that share no taxa share no topology, so
    there is nothing for the distance to measure.

    ``quality`` = B_frac in [0, 1]; the length-overlap fraction.  Near 0 = branch-
    score saturation; near 1 = similar trees.

    Kuhner & Felsenstein (1994), robinson1981rf.
    """

    w: float = METRIC_W
    #: What the second tree of a pair contributes, kept between calls.  A chart
    #: measures every tree against the same three anchors, so the anchor side of
    #: the work repeats and the tree side does not.  Left out of equality and of
    #: the repr: it is a saving, not part of what the metric is.
    _reference_cache: dict = field(default_factory=dict, compare=False,
                                   repr=False)

    def __call__(
        self, t1: HifukuTree, t2: HifukuTree
    ) -> tuple[float, float]:
        return _branch_score_dist_quality(t1, t2, self.w, self._reference_cache)

    @staticmethod
    def shared_taxa(t1: HifukuTree, t2: HifukuTree) -> int:
        """How many taxa the two trees hold in common.

        Selection reads this to reject a pair the metric cannot measure, and the
        weighted embedding reads it to drop that pair from the fit.
        """
        return len(set(t1.leaf_labels) & set(t2.leaf_labels))

shared_taxa staticmethod

shared_taxa(t1: HifukuTree, t2: HifukuTree) -> int

How many taxa the two trees hold in common.

Selection reads this to reject a pair the metric cannot measure, and the weighted embedding reads it to drop that pair from the fit.

Source code in src/hifuku/metric/branch_score.py
@staticmethod
def shared_taxa(t1: HifukuTree, t2: HifukuTree) -> int:
    """How many taxa the two trees hold in common.

    Selection reads this to reject a pair the metric cannot measure, and the
    weighted embedding reads it to drop that pair from the fit.
    """
    return len(set(t1.leaf_labels) & set(t2.leaf_labels))

compute_anchor_distances

compute_anchor_distances(r1: HifukuTree, r2: HifukuTree, r3: HifukuTree, metric: TreeMetric) -> tuple[np.ndarray, np.ndarray]

Compute pairwise distances and quality scores for three anchor trees.

The three-anchor form of :func:anchor_distances, which one chart uses.

Parameters:

Name Type Description Default
r1 HifukuTree

Anchor trees, each restricted to the shared global taxon namespace with global_leaf_indices set.

required
r2 HifukuTree

Anchor trees, each restricted to the shared global taxon namespace with global_leaf_indices set.

required
r3 HifukuTree

Anchor trees, each restricted to the shared global taxon namespace with global_leaf_indices set.

required
metric TreeMetric

The metric to use for the pairwise distances (see validate_anchors).

required

Returns:

Type Description
(dist, qual) : two (3, 3) float64 arrays

dist[i,j] = the metric distance between anchor i and anchor j. qual[i,j] = B_frac quality score in [0, 1]; 1 = identical trees, 0 = metric saturation. Both arrays are symmetric with zero diagonal.

Source code in src/hifuku/anchor_triangle.py
def compute_anchor_distances(
    r1: HifukuTree,
    r2: HifukuTree,
    r3: HifukuTree,
    metric: TreeMetric,
) -> tuple[np.ndarray, np.ndarray]:
    """
    Compute pairwise distances and quality scores for three anchor trees.

    The three-anchor form of :func:`anchor_distances`, which one chart uses.

    Parameters
    ----------
    r1, r2, r3 : HifukuTree
        Anchor trees, each restricted to the shared global taxon namespace
        with ``global_leaf_indices`` set.
    metric : TreeMetric
        The metric to use for the pairwise distances (see ``validate_anchors``).

    Returns
    -------
    (dist, qual) : two (3, 3) float64 arrays
        ``dist[i,j]`` = the metric distance between anchor i and anchor j.
        ``qual[i,j]`` = B_frac quality score in [0, 1]; 1 = identical trees,
        0 = metric saturation.  Both arrays are symmetric with zero diagonal.
    """
    return anchor_distances((r1, r2, r3), metric)

embed_anchor_triangle

embed_anchor_triangle(d12: float, d13: float, d23: float) -> tuple[np.ndarray, np.ndarray, np.ndarray]

Embed three anchor trees in a 2D plane from their pairwise tree distances.

This is the law-of-cosines embedding, which is classical multidimensional scaling specialized to three points:

P1 = (0, 0)
P2 = (d12, 0)
x3 = (d12^2 + d13^2 - d23^2) / (2 * d12)
y3 = sqrt(d13^2 - x3^2)
P3 = (x3, y3)

The resulting positions satisfy |P1-P2| = d12, |P1-P3| = d13, |P2-P3| = d23, providing the local Euclidean chart of tree space.

Parameters:

Name Type Description Default
d12 float

Pairwise tree distances between anchors 1-2, 1-3, and 2-3.

required
d13 float

Pairwise tree distances between anchors 1-2, 1-3, and 2-3.

required
d23 float

Pairwise tree distances between anchors 1-2, 1-3, and 2-3.

required

Returns:

Type Description
P1, P2, P3 : np.ndarray, shape (2,)

Anchor positions in the 2D chart.

Raises:

Type Description
ValueError

If d12 == 0 (degenerate anchors 1 and 2), or if the three distances violate the triangle inequality (the square inside the sqrt is negative, indicating the distances are not Euclidean-embeddable). This is the condition for a real configuration, specialized to three points: the double-centered squared-distance matrix must be positive semi-definite.

Notes

On one taxon set the tree distance is a Euclidean distance, so the anchors span a plane in a real vector space and the chart is the orthogonal projection onto it. The out-of-plane residual z, computed by barycentric, is a tree's distance from that plane. For trees compared on a restricted set of shared taxa the projection is approximate.

GOWER (1966) : classical multidimensional scaling; the positive semi-definite condition for a real configuration. Kuhner & Felsenstein (1994) : branch score distance; distances used here.

Source code in src/hifuku/anchor_triangle.py
def embed_anchor_triangle(
    d12: float,
    d13: float,
    d23: float,
) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
    """
    Embed three anchor trees in a 2D plane from their pairwise tree distances.

    This is the law-of-cosines embedding, which is classical multidimensional
    scaling specialized to three points:

        P1 = (0, 0)
        P2 = (d12, 0)
        x3 = (d12^2 + d13^2 - d23^2) / (2 * d12)
        y3 = sqrt(d13^2 - x3^2)
        P3 = (x3, y3)

    The resulting positions satisfy |P1-P2| = d12, |P1-P3| = d13,
    |P2-P3| = d23, providing the local Euclidean chart of tree space.

    Parameters
    ----------
    d12, d13, d23 : float
        Pairwise tree distances between anchors 1-2, 1-3, and 2-3.

    Returns
    -------
    P1, P2, P3 : np.ndarray, shape (2,)
        Anchor positions in the 2D chart.

    Raises
    ------
    ValueError
        If d12 == 0 (degenerate anchors 1 and 2), or if the three distances
        violate the triangle inequality (the square inside the sqrt is negative,
        indicating the distances are not Euclidean-embeddable).  This is the
        condition for a real configuration, specialized to three points: the
        double-centered squared-distance matrix must be positive semi-definite.

    Notes
    -----
    On one taxon set the tree distance is a Euclidean distance, so the anchors
    span a plane in a real vector space and the chart is the orthogonal
    projection onto it.  The out-of-plane residual z, computed by
    ``barycentric``, is a tree's distance from that plane.  For trees compared
    on a restricted set of shared taxa the projection is approximate.

    GOWER (1966) : classical multidimensional scaling; the positive
    semi-definite condition for a real configuration.
    Kuhner & Felsenstein (1994) : branch score distance; distances used here.
    """
    if d12 == 0.0:
        raise ValueError(
            f"Anchors 1 and 2 are at tree distance zero (d12=0); "
            f"they may be the same tree."
        )

    P1 = np.array([0.0, 0.0])
    P2 = np.array([d12, 0.0])

    x3 = (d12 * d12 + d13 * d13 - d23 * d23) / (2.0 * d12)
    radicand = d13 * d13 - x3 * x3

    if radicand < 0.0:
        raise ValueError(
            f"Anchor distances violate the triangle inequality: "
            f"d12={d12:.6g}, d13={d13:.6g}, d23={d23:.6g} do not admit a "
            f"planar triangle (radicand = {radicand:.6g} < 0).  "
            f"The three anchor trees are not Euclidean-embeddable at these "
            f"distances; this is the Schoenberg chartability condition."
        )

    y3 = math.sqrt(radicand)
    P3 = np.array([x3, y3])

    return P1, P2, P3

triangle_quality

triangle_quality(P1: ndarray, P2: ndarray, P3: ndarray) -> tuple[float, float, float]

Compute the triangle quality Q = lambda_min / lambda_max.

Q is the ratio of the minimum to the maximum eigenvalue of the 2x2 population covariance of the three anchor positions:

centroid = (P1 + P2 + P3) / 3
Cov = (1/3) * sum_i (Pi - centroid)(Pi - centroid)^T
Q = lambda_min(Cov) / lambda_max(Cov)

Q in (0, 1]: 1 at the equilateral triangle, 0 at collinear anchors. Q is scale-invariant and pivot-independent. It is the a-priori proxy for geometric dilution of precision (GDOP): trilateration is most accurate when the query sees the three anchors ~120 degrees apart, which occurs at the centroid of an equilateral triangle.

Parameters:

Name Type Description Default
P1 (ndarray, shape(2))

Anchor positions in the 2D Euclidean chart.

required
P2 (ndarray, shape(2))

Anchor positions in the 2D Euclidean chart.

required
P3 (ndarray, shape(2))

Anchor positions in the 2D Euclidean chart.

required

Returns:

Type Description
(Q, lambda_min, lambda_max) : (float, float, float)

Q is the quality ratio; lambda_min and lambda_max are the covariance eigenvalues (for diagnostic reporting).

Raises:

Type Description
ValueError

If lambda_max is zero (all three anchors are identical points).

Notes

Spilker (1996) : GDOP, geometric dilution of precision in navigation; Q is the anchor-level GDOP proxy prior to any sample evaluation.

Source code in src/hifuku/anchor_triangle.py
def triangle_quality(
    P1: np.ndarray,
    P2: np.ndarray,
    P3: np.ndarray,
) -> tuple[float, float, float]:
    """
    Compute the triangle quality Q = lambda_min / lambda_max.

    Q is the ratio of the minimum to the maximum eigenvalue of the 2x2
    population covariance of the three anchor positions:

        centroid = (P1 + P2 + P3) / 3
        Cov = (1/3) * sum_i (Pi - centroid)(Pi - centroid)^T
        Q = lambda_min(Cov) / lambda_max(Cov)

    Q in (0, 1]: 1 at the equilateral triangle, 0 at collinear anchors.
    Q is scale-invariant and pivot-independent.  It is the a-priori proxy for
    geometric dilution of precision (GDOP): trilateration is most accurate when
    the query sees the three anchors ~120 degrees apart, which occurs at the
    centroid of an equilateral triangle.

    Parameters
    ----------
    P1, P2, P3 : np.ndarray, shape (2,)
        Anchor positions in the 2D Euclidean chart.

    Returns
    -------
    (Q, lambda_min, lambda_max) : (float, float, float)
        Q is the quality ratio; lambda_min and lambda_max are the covariance
        eigenvalues (for diagnostic reporting).

    Raises
    ------
    ValueError
        If lambda_max is zero (all three anchors are identical points).

    Notes
    -----
    Spilker (1996) : GDOP, geometric dilution of precision in navigation;
    Q is the anchor-level GDOP proxy prior to any sample evaluation.
    """
    pts = np.array([P1, P2, P3], dtype=np.float64)  # (3, 2)
    centroid = pts.mean(axis=0)
    diffs = pts - centroid                            # (3, 2)
    cov = (diffs.T @ diffs) / 3.0                    # (2, 2)

    evals = np.linalg.eigvalsh(cov)   # ascending order
    lambda_min = float(evals[0])
    lambda_max = float(evals[1])

    if lambda_max == 0.0:
        raise ValueError(
            "All three anchor positions are identical; cannot compute Q."
        )

    Q = lambda_min / lambda_max
    return Q, lambda_min, lambda_max

barycentric

barycentric(s1: float, s2: float, s3: float, triangle: AnchorTriangle) -> tuple[np.ndarray, float]

Map a query tree's anchor distances to barycentric coordinates plus the out-of-plane residual z.

Given distances s1, s2, s3 from the query to the three anchors (obtained via the chart's TreeMetric), solve for the planar position X and convert to barycentric coordinates (lambda1, lambda2, lambda3).

Solve method: linear trilateration. Write:

f_i(X) = |X - Pi|^2 - si^2

Subtract f_1 from f_2 and f_3 to cancel |X|^2, giving a 2x2 linear system:

2 (P2 - P1) . X = (|P2|^2 - |P1|^2) - (s2^2 - s1^2)
2 (P3 - P1) . X = (|P3|^2 - |P1|^2) - (s3^2 - s1^2)

Solve for X (the in-plane position), then convert to barycentric via:

[lambda1, lambda2]^T = [P1-P3, P2-P3]^{-1} (X - P3)
lambda3 = 1 - lambda1 - lambda2

Out-of-plane residual (measure-only). After solving for X, the residual c = f_1(X) = f_2(X) = f_3(X) satisfies c = -z^2, where z is the distance of the true tree position from the anchor plane. When the three distances si are planar-consistent, z ~ 0. When the query lies off the plane (the 2D CV is lossy there), z > 0. No code in the loop may branch on z; it is stored as a diagnostic only.

Geometric dilution of precision (GDOP): the reliability of the in-plane position depends on the angles at which the query sees the three anchors. With unit line-of-sight vectors u_i = (X - Pi) / |X - Pi| and H = [u1^T; u2^T; u3^T], GDOP = sqrt(trace((H^T H)^{-1})). GDOP is smallest at the triangle centroid (equilateral, ~120 deg between anchors) and grows as the query moves outside the triangle. GDOP is the in-plane companion to z; both are measure-only diagnostics. Spilker (1996).

Parameters:

Name Type Description Default
s1 float

Tree distances from the query tree to r1, r2, r3.

required
s2 float

Tree distances from the query tree to r1, r2, r3.

required
s3 float

Tree distances from the query tree to r1, r2, r3.

required
triangle AnchorTriangle

Precomputed anchor geometry.

required

Returns:

Type Description
(lambdas, z) : (np.ndarray shape (3,), float)

lambdas = [lambda1, lambda2, lambda3] with sum == 1. Trees inside the triangle have all lambda_i >= 0; trees outside have at least one lambda_i < 0. z >= 0 is the out-of-plane residual (measure-only, no code may branch on it in the loop).

Source code in src/hifuku/anchor_triangle.py
def barycentric(
    s1: float,
    s2: float,
    s3: float,
    triangle: AnchorTriangle,
) -> tuple[np.ndarray, float]:
    """
    Map a query tree's anchor distances to barycentric coordinates plus
    the out-of-plane residual z.

    Given distances s1, s2, s3 from the query to the three anchors (obtained
    via the chart's TreeMetric), solve for the planar position X and convert to
    barycentric coordinates (lambda1, lambda2, lambda3).

    **Solve method: linear trilateration.**  Write:

        f_i(X) = |X - Pi|^2 - si^2

    Subtract f_1 from f_2 and f_3 to cancel |X|^2, giving a 2x2 linear system:

        2 (P2 - P1) . X = (|P2|^2 - |P1|^2) - (s2^2 - s1^2)
        2 (P3 - P1) . X = (|P3|^2 - |P1|^2) - (s3^2 - s1^2)

    Solve for X (the in-plane position), then convert to barycentric via:

        [lambda1, lambda2]^T = [P1-P3, P2-P3]^{-1} (X - P3)
        lambda3 = 1 - lambda1 - lambda2

    **Out-of-plane residual (measure-only).**  After solving for X, the
    residual c = f_1(X) = f_2(X) = f_3(X) satisfies c = -z^2, where z is the
    distance of the true tree position from the anchor plane.  When the three
    distances si are planar-consistent, z ~ 0.  When the query lies off the
    plane (the 2D CV is lossy there), z > 0.  No code in the loop may branch
    on z; it is stored as a diagnostic only.

    Geometric dilution of precision (GDOP): the reliability of the in-plane
    position depends on the angles at which the query sees the three anchors.
    With unit line-of-sight vectors u_i = (X - Pi) / |X - Pi| and
    H = [u1^T; u2^T; u3^T], GDOP = sqrt(trace((H^T H)^{-1})).  GDOP is
    smallest at the triangle centroid (equilateral, ~120 deg between anchors)
    and grows as the query moves outside the triangle.  GDOP is the in-plane
    companion to z; both are measure-only diagnostics.
    Spilker (1996).

    Parameters
    ----------
    s1, s2, s3 : float
        Tree distances from the query tree to r1, r2, r3.
    triangle : AnchorTriangle
        Precomputed anchor geometry.

    Returns
    -------
    (lambdas, z) : (np.ndarray shape (3,), float)
        ``lambdas = [lambda1, lambda2, lambda3]`` with sum == 1.  Trees inside
        the triangle have all lambda_i >= 0; trees outside have at least one
        lambda_i < 0.
        ``z >= 0`` is the out-of-plane residual (measure-only, no code may
        branch on it in the loop).
    """
    # The closed form is the device's, operation for operation, so the two
    # engines map equal squared distances to equal results bit for bit.
    return barycentric_from_squared(s1 * s1, s2 * s2, s3 * s3, triangle)

gdop

gdop(X: ndarray, triangle: AnchorTriangle) -> float

Geometric dilution of precision at in-plane query position X.

GDOP = sqrt(trace((H^T H)^{-1})) where H = [u1^T; u2^T; u3^T] and u_i = (X - Pi) / |X - Pi| are unit line-of-sight vectors to the three anchors. GDOP is a principled measure of in-plane trilateration reliability: it is smallest (2/sqrt(3) ~= 1.155) at the centroid of an equilateral triangle and grows without bound as the query moves far outside the triangle (lines of sight become near-parallel).

GDOP is the in-plane companion to the out-of-plane residual z: - z measures departure from the chart's plane (how non-Euclidean the local tree space is), - GDOP measures how weakly the in-plane position is constrained by the three anchor distances.

No code may branch on GDOP in the loop. It is a measure-only diagnostic. Spilker (1996) : GDOP definition and navigation context.

Parameters:

Name Type Description Default
X (ndarray, shape(2))

In-plane position (e.g., from the Cartesian coordinates derived during the barycentric solve, or from lambda @ [P1, P2, P3]).

required
triangle AnchorTriangle
required

Returns:

Type Description
float

GDOP >= 0. Very large values indicate poor in-plane conditioning.

Source code in src/hifuku/anchor_triangle.py
def gdop(X: np.ndarray, triangle: AnchorTriangle) -> float:
    """
    Geometric dilution of precision at in-plane query position X.

    GDOP = sqrt(trace((H^T H)^{-1})) where H = [u1^T; u2^T; u3^T] and
    u_i = (X - Pi) / |X - Pi| are unit line-of-sight vectors to the three
    anchors.  GDOP is a principled measure of in-plane trilateration
    reliability: it is smallest (2/sqrt(3) ~= 1.155) at the centroid of an
    equilateral triangle and grows without bound as the query moves far outside
    the triangle (lines of sight become near-parallel).

    GDOP is the in-plane companion to the out-of-plane residual z:
      - z measures departure from the chart's plane (how non-Euclidean
        the local tree space is),
      - GDOP measures how weakly the in-plane position is constrained by
        the three anchor distances.

    No code may branch on GDOP in the loop.  It is a measure-only diagnostic.
    Spilker (1996) : GDOP definition and navigation context.

    Parameters
    ----------
    X : np.ndarray, shape (2,)
        In-plane position (e.g., from the Cartesian coordinates derived
        during the barycentric solve, or from lambda @ [P1, P2, P3]).
    triangle : AnchorTriangle

    Returns
    -------
    float
        GDOP >= 0.  Very large values indicate poor in-plane conditioning.
    """
    H_rows = []
    for P in (triangle.P1, triangle.P2, triangle.P3):
        diff = X - P
        norm = float(np.linalg.norm(diff))
        if norm < _GDOP_NORM_FLOOR:
            return float("inf")
        H_rows.append(diff / norm)
    H = np.array(H_rows)  # (3, 2)
    HtH = H.T @ H         # (2, 2)
    det = float(HtH[0, 0] * HtH[1, 1] - HtH[0, 1] * HtH[1, 0])
    if abs(det) < _GDOP_DET_FLOOR:
        return float("inf")
    # Trace of (H^T H)^{-1}
    inv_trace = (HtH[1, 1] + HtH[0, 0]) / det
    return math.sqrt(max(0.0, inv_trace))