When the algorithm is a table

A cost that is not one

The same eighty-one cells, filled by the same recurrence, return 6, 10, 10 and 15 — in edits, in cost, in bits and in bits again. Only the first is a count of anything, two of them are equal by arithmetic coincidence, and the alignment each one chooses is different.

Every distance on this site so far has charged one for an insertion, one for a deletion and one for a substitution. Every one of them. It is such a natural thing to do that it is easy to miss that it is a decision, and that it is the only decision under which the answer is a count of anything at all.

Take two strings of eight characters and fill the same eighty-one cells four times, changing nothing but what a change costs:

model answer unit alignments achieving it
unit cost 6 edits 21
transition/transversion 10 cost 6
log-odds on a drift model 10 bits 2
unit cost, affine gaps 7 cost 1

The two tens are not the same number. One counts a substitution as one or two according to which pair of symbols it is; the other is a sum of logarithms and is measured in bits. Nothing in the arithmetic distinguishes them, and a plate that printed distance 10 without naming the model would be printing something that is not a quantity.

"aatgagtt" against "agccgtag", priced four waysThe same two strings and the same rectangle of subproblems under four cost models. The bars are not comparable with each other and that is the point: the first is a count of edits, the third is a number of bits under a stated mutation model, and the fourth counts gap events and gap characters at different rates. Each is correct under its own rule and none of them is the distance between these strings.unit cost6editstransition/transversion10costlog-odds on a drift model10bitsunit cost, affine gaps (3 + 1·k)7costfour models, one pair of strings, four answers81 cells each
Fig. 1 The same pair of strings under four rules. The bars are deliberately not comparable with each other: that is the finding rather than a defect of the drawing. Each is correct under its own rule and none of them is the distance between these two strings, because there is no such thing until a rule is named.

What unit cost assumes

It is worth stating the assumption plainly, because it is invisible in the recurrence.

D(i,j)=min{D(i1,j1)+[aibj]D(i1,j)+1D(i,j1)+1D(i,j) = \min\begin{cases} D(i-1,j-1) + [a_i \ne b_j] \\ D(i-1,j) + 1 \\ D(i,j-1) + 1 \end{cases}

The bracket is an indicator: one when the characters differ, zero when they agree. Written this way the model is a single character of notation, and what it claims is that every substitution is equally surprising. Reading a g where an a was expected costs exactly what reading a q where an a was expected costs. So does every insertion, and so does every deletion, and an insertion costs the same as a substitution.

For the case the measure was invented for — one keystroke wrong in a short word — that is defensible and works well. For almost everything else it is a claim nobody would make out loud. A single mutation in a nucleotide sequence is far more likely to swap one purine for the other than to swap a purine for a pyrimidine; a single slip at a keyboard is far more likely to hit a neighbouring key than a distant one; and a gap of four characters in a text is usually one event rather than four.

The first two are about the substitution rule and are this essay’s subject. The third is about the gap rule and needs three tables rather than one.

Where a matrix comes from

Published substitution matrices exist, they are good, and quoting one here would be useless — a number copied out of a table is a number this collection cannot check, and every claim on these pages is supposed to be recomputable from a rule written beside it.

So the matrices here are derived. State a mutation process and let the arithmetic produce the costs:

A symbol drifts by a random walk. At each step it stays where it is with a stated probability and otherwise moves to another symbol, with a weight ww times higher for a neighbour — which for nucleotides means the other member of its purine or pyrimidine pair, and for a typed letter means a key adjacent on the keyboard. Run the walk for one step, compute its stationary distribution π\pi by iterating the chain, and the cost of seeing bb where aa was expected is the log-odds

s(a,b)=log2P(ba)π(b)s(a,b) = \left\lceil -\log_2 \frac{P(b \mid a)}{\pi(b)} \right\rfloor

rounded to a whole number, with identity set to zero and everything else floored at one so that the table can be minimised unchanged.

log-odds on a drift model: what one substitution costsThe rule is the log-odds of one step of a random walk that stays put with probability 0.6 and otherwise moves, eight times more readily within a purine or pyrimidine pair than between them; a gap character costs 2. Read a row as the symbol expected and a column as the symbol seen; the entry is what the table charges for that pair, in bits. The diagonal is zero by construction so that a minimising fill can be used unchanged. Nothing here is copied from a published table: every entry is computed from the rule beside it, which is the difference between a matrix that can be checked and one that can only be quoted.acgtacgtseen ->0313303113033130a gap of k characters costs 2krows: expected · columns: seen · unit: bitslinear gaps
Fig. 2 The result for four nucleotides. A transition — swapping the two purines, or the two pyrimidines — costs 1; a transversion costs 3. Nothing here was typed in: both numbers came out of the walk, and changing the walk’s parameters changes them.
transition/transversion: what one substitution costsThe rule is a↔g and c↔t cost 1; every other substitution costs 2; a gap character costs 2. Read a row as the symbol expected and a column as the symbol seen; the entry is what the table charges for that pair, in cost. The diagonal is zero by construction so that a minimising fill can be used unchanged. Nothing here is copied from a published table: every entry is computed from the rule beside it, which is the difference between a matrix that can be checked and one that can only be quoted.acgtacgtseen ->0212202112022120a gap of k characters costs 2krows: expected · columns: seen · unit: costlinear gaps
Fig. 3 The same distinction stated directly rather than derived, at 1 and 2 rather than 1 and 3. It is a coarser version of the plate above and it is here because the pair of them shows what deriving buys: the ratio between the two costs is a consequence of the mutation probability rather than a number somebody chose to be plausible.

The same derivation runs over any alphabet that has a process behind it, and the process does not have to be biology. Typing is a walk as well: a finger aiming at one key lands on a neighbour far more often than on a key across the board, so the log-odds of that walk is a substitution matrix over the letters with no chemistry in it anywhere.

log-odds on a keyboard walk: what one substitution costsThe rule is the log-odds of one step of a random walk over the twenty-six letters that stays put with probability 0.5 and otherwise moves, twenty times more readily to a key adjacent on a QWERTY keyboard than to any other; a gap character costs 3. Read a row as the symbol expected and a column as the symbol seen; the entry is what the table charges for that pair, in bits. The diagonal is zero by construction so that a minimising fill can be used unchanged. Nothing here is copied from a published table: every entry is computed from the rule beside it, which is the difference between a matrix that can be checked and one that can only be quoted.qwerasdfqwerasdfseen ->0133113310131114310131113310341111330144111410143111410134114410a gap of k characters costs 3krows: expected · columns: seen · unit: bitslinear gaps
Fig. 4 Eight of the twenty-six letters under a walk on the keyboard’s adjacency graph. The matrix is graded rather than binary — six distinct costs over the full alphabet — and it is computed over all twenty-six letters and then drawn on a subset, because a matrix computed on the subset would not be the matrix the measurements used.

The construction has one property worth flagging now and arguing later: a log-odds score has no reason to obey the triangle inequality, and over the keyboard it does not. That makes the answer a score rather than a distance, and it breaks things that assume otherwise — which is an essay of its own.

Only the ratios matter, and two of them are edges

A cost model here has two kinds of number in it — what a substitution costs and what a gap character costs — and scaling both by the same factor changes every total and no alignment. So the model has fewer degrees of freedom than it looks to have: what decides the shape of the answer is the ratio between a substitution and a gap, and there are two values of that ratio at which the behaviour changes qualitatively.

Set the substitution cost to ss and the gap character to gg. Aligning aa against bb directly costs ss; the alternative is to delete aa and insert bb, which costs 2g2g and achieves the same thing. So:

  • when s>2gs > 2g, no substitution is ever chosen — every mismatch is cheaper as a deletion followed by an insertion, and the model has quietly stopped being an edit distance and become a longest-common-subsequence measure with a scale factor;
  • when s<gs < g, no gap is ever chosen unless the strings differ in length, because a substitution is cheaper than either half of the pair it replaces.

Both are reachable and neither announces itself. On the pair above:

ss gg substitutions used gap characters used
1 3 7 0
1 1 4 2
2 1 2 4
3 1 0 8

The last row is a model that will never report a substitution on any pair of strings, and the numbers in it — 3 and 1 — are not extreme. Somebody typing plausible-looking costs into a configuration file can land there, and the output remains a perfectly well-formed alignment with a perfectly reasonable-looking total.

That is the practical reason to derive a matrix from a process rather than choose one. The walk above produces a transversion cost of 3 against a gap of 2; three is under twice two, so substitutions survive, and the fact that they do is a consequence of the mutation probability rather than something that had to be noticed.

The alignment moves, not just the number

If the cost model only changed the total, it would be a matter of units and not much else. It changes the answer.

The same pair, aligned under 2 models"aatgagtt" against "agccgtag", aligned under each model's own arithmetic. The alignments differ, not merely the totals: unit cost prefers to open a gap where log-odds on a drift model prefers a substitution, because under the second the substitution in question is cheap. Scoring each alignment under the other model gives 6 and 16 against the models' own 6 and 10, so neither is a better alignment in any sense that does not name a model.unit cost: 6 edits, one of 21 optimalaatgagt-t|×××||×-agccgtaglog-odds on a drift model: 10 bits, one of 2 optimalaa-tg-agtt|××|||agccgtag--| identical x substituted - gap2 models
Fig. 5 The same pair aligned twice. Unit cost opens a gap where the log-odds model prefers a substitution, because under that model the substitution in question is a transition and costs 1. The two alignments have different shapes, different numbers of gaps and different columns of matched characters.

Score each model’s chosen alignment under the other model and the picture sharpens into a number:

alignment chosen by scored under unit under transversion under log-odds
unit 6 12 16
transversion 6 10 12
log-odds 6 10 10

Read down the last column. The alignment unit cost picked costs 16 bits under the log-odds model, whose own answer is 10 — a 60% overstatement from taking one table’s alignment and pricing it with another table’s rule. That is a specific and common error: an alignment produced under whatever the tool defaulted to, reported with a score computed under the model somebody wanted.

Read along the diagonal and every model prefers its own alignment, which is what makes the table a check rather than a display. That property is asserted in this site’s gate, and so is its opposite: an alignment quoted with a foreign model’s number is a case the gate is required to catch.

The row that is the actual finding

Read the first column instead and something more interesting happens. The log-odds alignment costs 6 under unit cost — which is unit cost’s own optimum.

The finer model’s answer was available to the coarser one all along. Unit cost simply had no way to prefer it.

How many alignments are optimal for "aatgagtt" against "agccgtag"The number of distinct alignments achieving the model's own optimum, counted by walking the table's predecessor graph. Where this number is above one, any drawn alignment is a choice made by a tie-break rather than a result, and a picture that did not say so would be presenting an arbitrary path as the answer. The models that distinguish more substitution costs have fewer ties, which is the one respect in which a finer model gives a sharper answer.unit cost216 editstransition/transversion610 costlog-odds on a drift model210 bitsunit cost, affine gaps (3 + 1·k)17 costone unit = one optimal alignmentaatgagtt / agccgtag
Fig. 6 How many alignments achieve each model’s optimum on this pair. Twenty-one under unit cost; six under the transition/transversion rule; two under log-odds; one under affine gaps. The finer the distinctions a model makes, the fewer ties it has.

Twenty-one alignments of these two strings cost exactly 6 edits. They are not all equally sensible — some of them pair a purine with a pyrimidine where another pairs it with the other purine — and unit cost cannot see the difference, because under unit cost every one of those pairings costs 1.

So when a program prints an alignment under unit cost, what it has printed is one of twenty-one, chosen by whichever way its traceback happened to break ties. Mine breaks them towards substitution, then deletion, then insertion. That is a stated convention and it is not a result, which is why every plate here that draws an alignment prints how many were optimal beside it.

A matrix is a claim about the data, not about the algorithm

There is a temptation to treat the cost model as a tuning knob — try a few, keep the one whose output looks best — and it is worth resisting for a reason that has nothing to do with taste.

The rule above is a model of how the data was produced. It says: symbols drift, drift within a class is eight times more likely than drift between classes, and one step of drift separates the two strings. If that is true of the strings being compared, the alignment the model returns is the most probable explanation of their difference, and the total is the number of bits of surprise in that explanation. If it is false, the alignment is the most probable explanation under an assumption that does not hold, and nothing in the output says so.

Which means the honest way to choose a matrix is to state what process is assumed and check whether it is plausible, and the dishonest way is to try several and keep the prettiest. The two produce identical-looking output.

This is the same shape as the entropy floor two fields over, where one stream has five correct floors and which one applies is decided by the model rather than by the data. Here the parallel is exact: one pair of strings has as many correct distances as there are cost models, every one of them is right, and choosing between them is a claim about how the strings came to differ.

What the finer model costs

Nothing measurable, which is the last thing worth saying about it.

Every linear model above fills 81 cells and considers 192 transitions on this pair. The recurrence takes three arguments per interior cell whatever the substitution rule is, and looking a cost up in a matrix is not more work than testing two characters for equality. The whole difference between unit cost and a derived matrix is which numbers are added.

The affine model is the exception and it is a real one: 243 cells and 448 transitions on the same pair, three times the table, because a cell has to hold three numbers to know where it is. That is a cost in cells, not in the rule; the substitution matrix is still a lookup.

It also only shows itself on strings that have something for a gap model to be right about. The pair used above is eight characters against eight and its optimal alignment has one gap in it, so charging gap openings differently from gap characters has almost nothing to price.

"ccagggagta" against "agcacacggatcag", priced four waysThe same two strings and the same rectangle of subproblems under four cost models. The bars are not comparable with each other and that is the point: the first is a count of edits, the third is a number of bits under a stated mutation model, and the fourth counts gap events and gap characters at different rates. Each is correct under its own rule and none of them is the distance between these strings.unit cost7editstransition/transversion13costlog-odds on a drift model15bitsunit cost, affine gaps (3 + 1·k)14costfour models, one pair of strings, four answers165 cells each
Fig. 7 Ten characters against fourteen, priced four ways. The four bars are in four different units and are not a ranking; what the plate is for is the last one, where a model that charges 8 to open a gap and 1 to extend it prices a pair whose alignment needs several gap characters in a row. Against the eight-character pair above, affine had one gap event to charge for; here it has the case it was written for.

The symmetry nobody declared

There is a third assumption in the recurrence, alongside the two the essay has named, and it is the one that is hardest to see because the notation makes it look like a fact about arithmetic.

D(i1,j)+1D(i-1,j)+1 and D(i,j1)+1D(i,j-1)+1 charge the same for consuming a character of the first string and for consuming one of the second. The substitution term charges s(a,b)s(a,b) with no suggestion that s(b,a)s(b,a) might differ. Together those say the two strings are interchangeable: whatever is being measured, it is a property of the unordered pair.

For the derived matrix that turns out to be true, and it is true for a reason rather than by construction. The walk is a random walk on an undirected graph — the purine and pyrimidine pairing is symmetric, keyboard adjacency is symmetric — so the chain is reversible and its stationary distribution satisfies π(a)P(ba)=π(b)P(ab)\pi(a)P(b\mid a) = \pi(b)P(a\mid b). Divide through and the two log-odds scores are equal:

P(ba)π(b)=P(ab)π(a)s(a,b)=s(b,a)\frac{P(b\mid a)}{\pi(b)} = \frac{P(a\mid b)}{\pi(a)} \quad\Longrightarrow\quad s(a,b) = s(b,a)

So the matrix is symmetric because the process is reversible, and not because anybody imposed it. That is worth knowing chiefly because it says what would break it. A mutation process with a direction — a drift towards one composition, a keyboard slip that is likelier to land to the right of the intended key, a sequencer that miscalls in one direction more than the other — is not a reversible walk, its log-odds table is not symmetric, and s(a,b)s(b,a)s(a,b) \ne s(b,a).

Nothing in the recurrence stops that. The table fills perfectly well with an asymmetric matrix; what changes is that the answer depends on which string was put along the rows. A tool that computes an asymmetric score without saying which argument is the reference is reporting a number whose meaning depends on argument order, and the failure is silent in both directions — reversing the arguments produces a different total with no indication that anything was different.

The gap terms carry the same assumption and break more often. Comparing a reference against a read, an original against an edit, or a specification against an implementation are all situations where deleting from the first and inserting into the second are different events with different likelihoods, and charging them equally is a claim that they are not. The repair is two gap costs rather than one, it costs nothing in the table, and it converts the answer from a distance into a directed score — which is the same conversion the log-odds model already performed and which is why this collection is careful to call the result a score.

Counting the ties costs one number per cell

The tie counts printed beside every alignment here — twenty-one, six, two, one — look like an expensive thing to know. They are not, and the cheapness is worth spelling out because it is the reason there is no excuse for a tool that does not report them.

Fill a second table alongside the first. Where the first cell takes the minimum of three candidates, the second takes the sum of the counts of whichever candidates achieved that minimum, with the origin seeded at one. That is one addition and up to three comparisons per cell, on a table that already exists, and the final corner holds the number of distinct optimal alignments.

The whole machinery is the same recurrence read over a different pair of operations — minimise-and-add becomes count-the-minimisers — and it is the standard trick for turning an optimisation into an enumeration. Every model in this essay is scored by exactly that method, which is why the counts can be printed for four different cost rules without four different implementations.

Two practical cautions come with it, and both are the kind of thing that turns up as a bug rather than as a caveat.

The count grows exponentially. Two strings of a few hundred characters under unit cost can have more optimal alignments than there are atoms worth counting, so the second table needs arbitrary-precision integers or has to be kept in logarithms. On the eight-character pairs here the numbers are small; on anything real they are not, and a count silently overflowing a machine word reports a plausible small number rather than an error.

And the count is a property of the cost model, not of the strings — which is the essay’s whole subject arriving in the one place a reader might not expect it. Twenty-one under unit cost and one under affine gaps are counts of the same set of alignments, partitioned differently by two rules about what things cost. A tie is not an ambiguity in the data; it is a distinction the model declined to make, and the number of them is the most direct measurement available of how much resolution a cost model actually has.

What to take from this

Three things, in the order they bite.

A distance is not a property of two strings. It is a property of two strings and a rule, and the rule is an input to the computation in exactly the sense that the strings are. Every earlier table on this site is a statement under one particular rule and none of them says so — which is the same omission a guarantee quoted without its model makes one field over, and it is worth being embarrassed about in both places.

The unit is part of the answer. Edits, arbitrary cost units, and bits are three currencies. Averaging across them, comparing them, or plotting them on one axis is an error that no assertion about the arithmetic will catch, because the arithmetic is fine.

A model that makes no distinctions has ties it cannot see. Twenty-one to two is a large ratio, and the number that matters is not which alignment is right but how many the model considered indistinguishable. A tool that reports one of them without saying so is reporting its tie-break.

And one thing this essay has deliberately not done. Nothing above compares any of these models against a correct answer, because on this material there is no such thing to compare against: the strings were generated, not observed, and no rule external to the models says which alignment of them is the true one. What has been measured is that the models disagree, by how much, in which direction, and at what cost — which is a set of facts about the models. Whether a particular matrix suits a particular corpus is an empirical question about that corpus and it is not one this collection can settle from a stated rule.

What this makes readable

Essays that name this one as a prerequisite.

Named alongside this one

Essays reaching for the same objects. Nobody chose these; they are what the concept index makes visible.

What links here

The 8 essays that link to this one and share the most of its objects, of 13 that link here.

The objects this essay names

Each one links to every other essay that touches it.

Affine gapAlignmentCost modelDynamic programmingEdit distanceHonest limitLog oddsMeasurementSubproblemSubstitution matrixTrade offTriangle inequalityUnit of cost