Skip to content

Edit distance

Measure the cheapest sequence of edits between two strings and use the score to shortlist possible instrument matches for review.

Edit distance is the minimum total cost of changing one string into another using a specified set of edits. When a trade file contains an unfamiliar instrument label, the distance can help an analyst shortlist records to inspect before joining prices or positions to the file.

The edit rules change the answer

In standard Levenshtein distance, inserting, deleting or substituting a single character costs one. Keeping a matching character costs zero. For the fictional strings BID1 and BDI2, the distance is 3. Three substitutions suffice. Two substitutions can fix at most two of the three mismatched positions. A deletion and an insertion would preserve at least three original characters in order, but these strings share only B, I and D, and I and D occur in opposite orders. Wagner and Fischer (1974), The String-to-String Correction Problem
Allow a swap of two adjacent characters at cost one, alongside the same three unit-cost edits, and the answer becomes 2. Swap ID to get BDI1, then replace 1 with 2. A swap alone leaves the digit unchanged, and a single substitution leaves the reversal unresolved. This convention is unrestricted Damerau–Levenshtein distance, which allows adjacent swaps anywhere in the edit sequence. Lowrance and Wagner (1975), An Extension of the String-to-String Correction Problem

Compute Levenshtein distance one prefix at a time

Let Di,jD_{i,j} be the minimum edit cost to turn the first ii characters of source xx into the first jj characters of target yy. The empty-prefix edges are Di,0=iD_{i,0}=i and D0,j=jD_{0,j}=j. At each interior cell, dynamic programming reuses three smaller prefix costs.
Di,j=min⁡{Di−1,j+1,  Di,j−1+1,  Di−1,j−1+1xi≠yj}.D_{i,j} =\min\bigl\{ D_{i-1,j}+1,\; D_{i,j-1}+1,\; D_{i-1,j-1}+\mathbf{1}_{x_i\ne y_j} \bigr\}.
The three terms delete xix_i, insert yjy_j, or align the two characters and charge one only if they differ. At row BI and column BDI, matching the last I carries over the cost of one from B to BD. Filling the table left to right and top to bottom gives D4,4=3D_{4,4}=3. For strings of lengths mm and nn, the table takes O(mn)O(mn) time when both lengths are positive. Wagner and Fischer (1974), section 4
Levenshtein table for fictional labels BID1 and BDI2. The highlighted path keeps B, inserts D, keeps I, deletes the original D and replaces 1 with 2. Running costs are 0, 1, 1, 2 and 3.
Levenshtein table for fictional labels BID1 and BDI2. The highlighted path keeps B, inserts D, keeps I, deletes the original D and replaces 1 with 2. Running costs are 0, 1, 1, 2 and 3.
The row and column identify the prefixes being compared. Each cell gives their minimum edit cost, and the bottom-right cell gives the distance between the full strings.

A closer label can still be the wrong instrument

Suppose BID2 is another candidate for BID1. Its distance is one under either rule, so it ranks ahead of BDI2. If its listing venue or share class conflicts with the trade record, the smaller score gives no basis for choosing it. Even identical ticker text can refer to different instruments across venues or dates.
Check the candidate against reference data for the date on the record. Use the identifier scheme, venue and relevant instrument fields, such as share class or futures contract expiry, to resolve the match. Keep the record unresolved for review if the reference fields cannot identify a single instrument. OpenFIGI’s mapping endpoint illustrates why those fields matter by accepting identifier types and filters such as market identification code and currency, and by allowing multiple results per query. OpenFIGI, v3 Mapping API documentation
The SelfMadeQuant lesson Instrument Data Conventions covers how ticker reuse and venue and share-class differences cause mapping failures. The full lesson requires membership.

Sources

Last updated
  • algorithms
  • dynamic programming
  • string matching
  • reference data
← Back to the glossaryBack to top ↑