MATH 4310 · Computational Bioinformatics
Evolutionary Distances and UPGMA
Lesson 13 · 2027-03-03 · Week 8
Core question
Which samples share a more recent ancestor?
From sampled sequences to shared ancestry
Lessons 9 to 12 labeled regions within one genome. We now compare DNA from several organisms or viral samples to ask which are more closely related. Their common ancestors are usually unavailable to sequence. We instead compare the descendants: sequencing gives a DNA letter string from each sample, and substitutions accumulated since their ancestors can leave different letters at corresponding positions. A phylogeny, or evolutionary tree, describes the inferred pattern of shared ancestry. Its branches can also represent the amount of sequence change along each lineage.
During the 2003 SARS outbreak, researchers asked which animal the new coronavirus had come from. An early guess was birds, by analogy with bird flu, but the viral genome sequenced in April 2003 ruled them out. Coronavirus genomes differ in gene content and order, so instead of aligning whole genomes, researchers aligned a single gene, encoding the Spike protein the virus uses to attach to host cells, across coronaviruses from patients and from animals; viruses from palm civets were the closest relatives of the human ones.1 Such a comparison can only rank the viruses that have been sampled, and later sampling of bats pointed to civets as an intermediate rather than the original host. Here we develop the distance-based inference on synthetic DNA.
The sequences must be homologous: descended from a corresponding sequence in a common ancestor. Our input is a supplied multiple alignment, with one sequence per row and corresponding positions in the same columns. Figure 13.1 shows a made-up alignment of four samples beside the kind of tree we want to infer from it.
Figure 13.1. A made-up alignment of four sampled sequences (left), with shaded columns where the rows differ, and the rooted tree these differences suggest (right). Tips are the samples, internal nodes are unobserved ancestors, and branch extents are drawing coordinates. A and B, which differ at one column, share the most recent ancestor.
A and B differ at one of the eight columns, A and C at three, and D differs from each of the others at four. Alignment matters because comparing unrelated positions would turn errors in correspondence into apparent evolutionary changes. We use only columns containing a known DNA base in every row, so all pairwise comparisons rest on the same columns; computing the alignment itself is a separate task. We first summarize the differences in a matrix, then ask which evolutionary tree could explain that matrix.
Observed differences and evolutionary distance
For \(L\) retained columns, define the mismatch fraction \[ f_{ij}=\frac{1}{L}\sum_{\ell=1}^L \mathbf{1}\{x^{(i)}_\ell\ne x^{(j)}_\ell\}. \] The indicator contributes one at a mismatch and zero otherwise, so \(f_{ij}\) is the fraction of retained columns at which samples \(i\) and \(j\) disagree. In Figure 13.1, \(f_{AB}=1/8\), \(f_{BC}=2/8\), \(f_{AC}=3/8\), and \(f_{AD}=f_{BD}=f_{CD}=4/8\). The matrix of these fractions is symmetric and nonnegative with a zero diagonal. Because every pair is compared on the same columns, it also satisfies the triangle inequality \(f_{ij}\leq f_{ik}+f_{kj}\): wherever \(i\) and \(j\) differ, at least one of them must differ from \(k\).
Mismatch fractions describe the observed letters, not the full history of substitutions. Along a lineage, A could change to G and later back to A, leaving no mismatch. A change to G followed by T leaves one mismatch after two substitutions. This loss of historical information becomes substantial for divergent sequences.2
An evolutionary distance estimates the expected number of substitutions per site along the path between two samples. Unlike a mismatch fraction, it can exceed one. The tree algorithm takes these distances as input, whether they come from a substitution model or from raw mismatch counts.
A correction for hidden substitutions
The Jukes–Cantor model assumes equal base frequencies and equal rates for changes between each pair of different bases. Its predicted mismatch fraction after total path distance \(d\) is \[ f=\frac34\left(1-e^{-4d/3}\right). \] Taking this model relation as given, rearranging and taking the natural logarithm gives \[ d=-\frac34\ln\left(1-\frac43f\right),\qquad 0\leq f<\frac34. \] For A and B, \(f=1/8\) gives \(d\approx0.137\), only slightly more than the observed fraction. For A and D, \(f=1/2\) gives \(d=-\tfrac34\ln\tfrac13\approx0.824\): at this level of divergence the observed mismatches miss about two-fifths of the estimated substitutions (Figure 13.2). As the lineages diverge, their bases approach independent uniform draws, so the probability of a mismatch approaches \(3/4\). The observed difference then saturates: large additional evolutionary distances produce small changes in \(f\). The correction diverges as \(f\) approaches \(3/4\) and is not a finite real estimate at or above that value. Near saturation, sampling error in \(f\) becomes large uncertainty in \(d\).2
Figure 13.2. Expected mismatch fraction under the Jukes-Cantor model (blue) and the line on which every substitution stays visible (dashed). The red arrow is the correction for samples A and D, from an observed fraction of 1/2 to about 0.82 substitutions per site. Near the 3/4 level, extra substitutions barely change the observed fraction.
What the tree must explain
We need to distinguish a branching pattern from the distances it predicts. A rooted tree has a distinguished ancestral node, the root. Its leaves or tips are the sampled sequences; internal nodes represent inferred common ancestors. Each edge \(e\) has a nonnegative branch length \(\ell_e\). For the unique path \(P_T(i,j)\) between two tips in tree \(T\), define \[ d_T(i,j)=\sum_{e\in P_T(i,j)}\ell_e. \] These are additive tree distances. We want to reverse this computation.1
Definition 13.1 Distance-based tree reconstruction
Input: an \(n\times n\) distance matrix \(D=(d_{ij})\) for \(n\) sampled sequences.
Output: a tree \(T\) with the samples as tips and nonnegative branch lengths that fits \(D\): \(d_T(i,j)=d_{ij}\) for every pair \(i,j\).
Arbitrary matrices need not admit a fitting tree; we will establish a guarantee under a stronger assumption.
Pairwise distances alone generally do not locate the root. UPGMA adds a molecular-clock assumption: all lineages accumulate substitutions at the same rate, and the tips are sampled at the same time. Under this model every root-to-tip path has the same length.2 A node’s height \(h_u\) is its distance down to any descendant tip. Thus tips have height zero and an edge from parent \(u\) to child \(v\) has length \(\ell_{uv}=h_u-h_v\).
If \(u\) is the most recent common ancestor of tips \(i,j\), their paths to \(u\) have equal length. Therefore \[ d_{ij}=2h_u. \] Heights are measured in substitutions per site; converting them to calendar ages requires a calibrated substitution rate.
Key idea
Under a molecular clock, the distance between two tips is twice the height of their most recent common ancestor. The smallest distance in the matrix therefore marks the most recent split, so merging the closest pair first recovers that split.
Definition 13.2 Ultrametric distances
A symmetric, nonnegative matrix with zero diagonal is ultrametric if \[ d_{ij}\leq\max(d_{ik},d_{jk}) \quad\text{for every }i,j,k. \] Equivalently, among the three distances for any three tips, the largest two are equal. All three may be equal.
A clock tree has this property. For three tips, either all share the same most recent common ancestor, or two meet below the ancestor they share with the third. In the latter case, the two distances to the third tip are equal and larger. This gives an observable constraint on the matrix, stronger than the ordinary triangle inequality.
UPGMA: merge the closest clusters
UPGMA builds the tree from the bottom up as if the data came from a clock tree, which lets it return a tree even when the matrix fits none exactly.1 Start with one cluster per tip. After a merge, a cluster represents an inferred ancestral group. How should we measure its separation from another group? For clusters \(P\) and \(Q\), UPGMA defines their distance as the average over all original cross-cluster tip pairs. \[ d(P,Q)=\frac{1}{|P||Q|}\sum_{i\in P}\sum_{j\in Q}d_{ij}. \] Every tip pair has equal weight. The method’s name is unweighted pair group method with arithmetic mean; equal weighting of tips requires weighting cluster updates by their sizes.
After merging \(P\) and \(Q\), compute the new cluster’s distance to any other active cluster \(K\) by partitioning the sum into pairs originating in \(P\) and pairs originating in \(Q\). \[ d(P\cup Q,K) =\frac{|P|d(P,K)+|Q|d(Q,K)}{|P|+|Q|}. \] This update avoids revisiting every original pair.
Algorithm UPGMA
Input: a symmetric \(n\times n\) matrix of nonnegative tip distances \(d_{ij}\) with \(d_{ii}=0\), for \(n\geq 1\) distinctly named samples.
Output: a rooted tree with the \(n\) samples as tips, node heights and branch lengths, plus the merge history.
- Initialize the
active list to \((\{1\},\ldots,\{n\})\)
in sample order.
// one cluster per tip Foreach singleton \(P=\{i\}\), set its size \(m_P\leftarrow 1\) and its height \(h_P\leftarrow 0\).// tips have height zeroForeach pair \(i<j\), set \(d(\{i\},\{j\})\leftarrow d_{ij}\). Use this value in both orders.// copy the tip distancesWhilethe active list contains at least two clusters:- Choose distinct active clusters \(P,Q\) with smallest \(d(P,Q)\). Among tied pairs with \(P\) before \(Q\), take the earliest \(P\) in the active list, then the earliest \(Q\) after it.
- \(\delta\leftarrow d(P,Q)\), \(M\leftarrow P\cup Q\), \(m_M\leftarrow m_P+m_Q\), and \(h_M\leftarrow\delta/2\).
// merge at height δ/2 - Add branches
from \(M\) to \(P\) and \(Q\) with lengths \(h_M-h_P\) and \(h_M-h_Q\).
// subtract heights Foreach other active cluster \(K\), \[d(M,K)\leftarrow\frac{m_P\,d(P,K)+m_Q\,d(Q,K)}{m_M}.\]// weight by cluster size- Record the merge \((P,Q,\delta,h_M)\). Remove \(P,Q\) from the active list and append \(M\).
Returnthe tree rooted at the sole remaining cluster and the merge history.
The merge distances never decrease. If \(\delta\) is the current minimum, each distance from the new cluster is a weighted average of two distances that are both at least \(\delta\): \[ d(P\cup Q,K)\geq\min\{d(P,K),\,d(Q,K)\}\geq\delta, \] and the retained distances are also at least \(\delta\). Parent heights therefore never lie below child heights, so the algorithm produces nonnegative branches even when its input is not ultrametric.
Four samples, three merges
Distances estimated from finite sequences are rarely exactly ultrametric, because sampling noise and departures from a clock perturb them. Suppose a distance estimator supplies the matrix below, in units of \(0.01\) substitutions per site for convenient hand arithmetic (an entry 4 means 0.04). It is close to ultrametric but not exactly so: in the triple A, B, C the two largest distances are 7 and 9.
Example 13.1 UPGMA on four samples
| A | B | C | D | |
|---|---|---|---|---|
| A | 0 | 4 | 7 | 16 |
| B | 4 | 0 | 9 | 18 |
| C | 7 | 9 | 0 | 20 |
| D | 16 | 18 | 20 | 0 |
The closest pair is A,B at distance 4. Their parent U has height 2, and both terminal branches have length 2. Averaging over tip pairs gives \[ d(AB,C)=\frac{7+9}{2}=8, \qquad d(AB,D)=\frac{16+18}{2}=17. \] The smallest remaining distance is \(d(AB,C)=8\), so AB merges with C. Their parent V has height 4; the branch V→U has length \(4-2=2\), while V→C has length 4. The clusters now have unequal sizes, and the weights matter: \[ d(ABC,D)=\frac{2\,d(AB,D)+1\,d(C,D)}{3} =\frac{2(17)+20}{3}=18. \] This is the average of the three original distances 16, 18, and 20 from D. An unweighted average of the two cluster distances would give \((17+20)/2=18.5\), counting the single pair C,D as heavily as the two pairs A,D and B,D together. The root R has height 9, with a branch of length 5 to V and 9 to D.
Under the clock model, A and B share the most recent ancestor, C joins their lineage earlier, and D separates earlier still; the heights 0.02, 0.04, and 0.09 substitutions per site are not dates. A is not inferred to be the ancestor of B: both are observed tips descending from U.
The tree cannot reproduce every input. Its paths from A and from B to C both have length \(2+2+4=8\), where the matrix has 7 and 9, and all three paths to D have length 18, where it has 16, 18, and 20. Each path distance between two merged clusters is the average of the input distances it replaces, so comparing input and path distances shows how far the data depart from a clock.
Computing and drawing the result
The implementation stores a size, a height, and the distances for each active cluster. It returns a nested tree, with each node’s name, children, and branch length from its parent, and the merge history for comparison with the hand calculation.
Python upgma
import numpy as np
def upgma(labels: list[str], matrix: np.ndarray) -> tuple[dict, list[tuple]]:
"""Return an average-linkage clock tree and its merge history."""
matrix = np.asarray(matrix, dtype=float)
n = len(labels)
if n == 0 or len(set(labels)) != n or matrix.shape != (n, n):
raise ValueError("Use distinct labels and a matching nonempty square matrix.")
active = list(range(n))
sizes = dict.fromkeys(active, 1)
heights = dict.fromkeys(active, 0.0)
members = {i: (labels[i],) for i in active}
nodes = {i: {"name": labels[i], "length": 0.0, "children": []} for i in active}
distances = {}
for i in range(n):
for j in range(i + 1, n):
distances[i, j] = matrix[i, j]
history = []
next_id = n
while len(active) > 1:
# Sorted identifiers make the first minimizing pair the tie rule.
pairs = []
for position, i in enumerate(active):
for j in active[position + 1:]:
pairs.append((i, j))
i, j = min(pairs, key=lambda pair: distances[pair])
delta = distances[i, j]
height = delta / 2
history.append((members[i], members[j], float(delta), float(height)))
nodes[i]["length"] = height - heights[i]
nodes[j]["length"] = height - heights[j]
nodes[next_id] = {"name": "", "length": 0.0,
"children": [nodes[i], nodes[j]]}
sizes[next_id] = sizes[i] + sizes[j]
heights[next_id] = height
members[next_id] = members[i] + members[j]
remaining = [k for k in active if k not in (i, j)]
for k in remaining:
distance_i = distances[tuple(sorted((i, k)))]
distance_j = distances[tuple(sorted((j, k)))]
distances[k, next_id] = (
sizes[i] * distance_i + sizes[j] * distance_j
) / sizes[next_id]
active = remaining + [next_id]
next_id += 1
return nodes[active[0]], history
labels = ["A", "B", "C", "D"]
distances = np.array([
[0, 4, 7, 16],
[4, 0, 9, 18],
[7, 9, 0, 20],
[16, 18, 20, 0],
])
clock_tree, merge_history = upgma(labels, distances)
assert [step[2] for step in merge_history] == [4, 8, 18]
assert [step[3] for step in merge_history] == [2, 4, 9]
merge_historyFigure 13.3. UPGMA tree for the matrix of Example 13.1. Parentheses give node heights in units of 0.01 substitutions per site; all tips have height zero, and internal branches carry their lengths. The tips line up because UPGMA imposes a clock, even though the input distances were not exactly ultrametric.
Why exact ultrametric input is recovered
Under a clock, the closest tips meet at the youngest ancestral height because their distance is twice that height. Merging them therefore recovers the most recent split. Once this group is represented by a cluster, every member has the same distance to a tip outside it: all those paths meet at the same older ancestor. Averaging preserves that distance, so the same reasoning applies at the next merge.
Theorem 13.1 Exact distance recovery
Given an ultrametric distance matrix, UPGMA constructs a rooted tree whose tip-to-tip path distances equal every input distance. Tied merges may resolve a group in different orders, which only adds internal edges of length zero.
Proof
For active clusters \(P,Q\), maintain the invariant \[ d_{ij}=d(P,Q)\qquad\text{for every }i\in P,\ j\in Q, \] which holds initially for singletons. Suppose \(P,Q\) merge at minimum distance \(\delta\), and let \(K\) be another active cluster. For tips \(i\in P\), \(j\in Q\), \(k\in K\), the invariant gives the triple \[ d_{ij}=\delta,\qquad d_{ik}=d(P,K)\geq\delta,\qquad d_{jk}=d(Q,K)\geq\delta. \] If the last two differed, the larger would be the unique largest distance in the triple, which ultrametricity forbids. Hence \[ d(P\cup Q,K)=\frac{|P|\,d(P,K)+|Q|\,d(Q,K)}{|P|+|Q|}=d(P,K)=d(Q,K), \] so every original distance from \(P\cup Q\) to \(K\) equals the stored one, and the invariant survives the merge.
Two tips \(i,j\) first share a cluster at a merge with distance \(\delta=d_{ij}\). Its node has height \(\delta/2\), and branch lengths telescope from it down to each tip, so \[ d_T(i,j)=\frac{\delta}{2}+\frac{\delta}{2}=d_{ij}. \]
The proof also shows when the size weights matter: on ultrametric input the two distances being averaged are equal, so every weighting gives the same tree. The weights changed a distance in Example 13.1 only because that matrix is not ultrametric.
Computational cost
With \(k\) active clusters, searching all pairs costs \(\mathcal{O}(k^2)\) and updating distances costs \(\mathcal{O}(k)\). Summing over the \(n-1\) merges gives \(\mathcal{O}(n^3)\) time and \(\mathcal{O}(n^2)\) space for this straightforward implementation, assuming constant-time arithmetic and dictionary lookups.
When evolutionary rate changes the answer
A small distance can reflect a recent ancestor, a slow rate, or both. Without a clock, the smallest entry of a matrix need not pair two tips that are neighbors in the tree that produced it.1 Suppose the true tree separates the pair A,B from the pair C,D. Give A and C terminal branches of length 1, B and D terminal branches of length 8, and the edge connecting the pairs length 1, all in the same hundredths-of-a-substitution units. Summing along the paths gives
| A | B | C | D | |
|---|---|---|---|---|
| A | 0 | 9 | 3 | 10 |
| B | 9 | 0 | 10 | 17 |
| C | 3 | 10 | 0 | 9 |
| D | 10 | 17 | 9 | 0 |
A and C have the smallest distance, 3, even though they belong to different pairs in the true tree. UPGMA joins these slowly changing tips first. This changes the branching pattern, not merely its branch lengths.
Python Run UPGMA on the unequal-rate distances
# Reuse upgma and the sample labels A through D from the first example.
unequal_rate_distances = np.array([
[0, 9, 3, 10],
[9, 0, 10, 17],
[3, 10, 0, 9],
[10, 17, 9, 0],
])
wrong_tree, wrong_merges = upgma(labels, unequal_rate_distances)
wrong_mergesAfter merging A,C, their distances to B and D are both 9.5. The tie rule joins B next. The last merge occurs at distance 12. The inferred A-to-C path has length 3, but the inferred A-to-B path has length 9.5 instead of 9. More sequence data would not repair this example: the input distances were already exact; the failed assumption was the clock.
Figure 13.4. An unequal-rate tree and UPGMA’s reconstruction from its exact distances. The true tree pairs A with B and C with D; UPGMA instead joins the slowly changing A and C. Horizontal extents show branch lengths in both panels.
The true distances still contain information about the branching pattern. For example, \(d_{AB}+d_{CD}=18\), while both \(d_{AC}+d_{BD}\) and \(d_{AD}+d_{BC}\) equal 20: the latter paths cross the connecting edge twice. Methods for additive distances can use such relationships without imposing a clock; neighbor joining is one example and normally returns an unrooted tree.1,2
In the SARS comparison, the smallest distances marked civet viruses as the closest relatives of the human ones; the unequal-rate example shows that this reading assumes no lineage evolved unusually slowly. Distance summaries also discard which columns supplied the differences. The next lesson keeps those characters and asks how many changes a candidate tree requires.
Exercises
Exercise 13.1. Apply the Jukes–Cantor correction to the mismatch fractions of the alignment in Figure 13.1, then run UPGMA on the corrected distances. Does the result agree with the tree drawn there? Which triple of samples violates the ultrametric condition?
Exercise 13.2. For the tree in Example 13.1, list all six edge lengths and all six tip-to-tip path distances, and compare the path distances with the input matrix. If the substitution rate is 0.01 substitutions per site per million years, what calendar age corresponds to the inferred root height?
Exercise 13.3. In Example 13.1, set \(d_{AC}=d_{BC}=8\) and \(d_{AD}=d_{BD}=d_{CD}=18\). Trace UPGMA once with size-weighted updates and once with unweighted averages of cluster distances. Show that the traces agree, and explain using the ultrametric condition why they must.
Exercise 13.4. For three tips with all off-diagonal distances equal to 6, draw two possible binary outputs. Which branches have length zero? What information about branching order is missing from the matrix?
Exercise 13.5. In the unequal-rate example, change both long terminal branches, to B and D, from 8 to \(b\geq0\). Find the range of \(b\) for which A,C is the unique first merge. Explain why knowing the true distances perfectly is not enough to justify that merge biologically.