- Introduced by
- Hamming, R. W.
- Year
- 1950
- Type
- Distance (categorical, count of differing positions)
- Family
- Distance
What does it do?
Hamming Distance is a distance measure that counts the positions at which two equal-length sequences differ.
It is attributed to Hamming, R. W. The formula compares two vectors position by position. It does not rank alternatives or weigh criteria, and its output is a single non-negative integer.
The intuition behind the method
Place the two sequences side by side and compare them position by position. Each position where the entries are not equal adds one to the count, and each position where they match adds nothing. The result says how many positions disagree, but not how large each disagreement is. In the worked example, a = [1.0, 0.0, 1.0, 1.0] and b = [0.0, 0.0, 1.0, 0.0] differ at positions 1 and 4, so d = 2.
History
The Hamming distance is attributed to R. W. Hamming. It is widely used in coding theory to compare codewords of equal length, and it is also applied to binary morphological data in phylogenetic analysis.
When to use it
Use Hamming Distance when two objects are described by sequences of the same length whose entries are nominal or binary, such as bit strings, encoded profiles or codewords, and when only agreement or disagreement at each position matters. For nominal features every mismatch is equally meaningful, which is why the count is appropriate there. It is also suitable when you need a simple, integer count of disagreements that can be checked by hand. Do not apply it to ordinal codes: a mismatch between adjacent levels and a mismatch between distant levels score the same, so the order of the levels is lost. Do not use it when the sequences have different lengths, or when the size of each difference matters (for example, two numerical scores that differ slightly versus widely), because every mismatch is weighted the same. Do not use it as a ranking method for alternatives, since it does not weigh criteria or aggregate scores.
Limitations and considerations
- The count ignores the size of each mismatch: a small difference and a large difference at the same position both add 1.
- The formula aggregates no criteria and assigns no importance to positions, so it cannot express that some positions matter more than others.
- The measure is defined only for sequences of equal length with aligned positions; it has no mechanism to handle insertions or deletions.
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- The formula has no weights, scaling or normalisation step, so the count does not depend on how criteria were scaled, and the only design decision is what counts as equal in each position.
- Each mismatch contributes exactly 1, so the result is an integer count that can be verified directly, as in the example where two mismatched positions give d = 2.
- For equal-length sequences the measure is zero only when the sequences are identical and is symmetric in its two arguments, which the mechanics formula makes clear.
Applications
- Comparing equal-length binary fingerprints or bit strings.
- Counting disagreements between two categorical labellings of the same items, position by position.
- Measuring differences between encoded sequences of symbols of the same length.
How it works
How it works
Compare a_j with b_j for each position j and count the positions where they are not equal; this count is d_H(a, b). Every mismatch contributes exactly 1, regardless of how different the two entries are.
Formula
d_H(\mathbf{a},\mathbf{b}) = |\{j : a_j \ne b_j\}|Common pitfalls
- On real-valued inputs the rule a_j != b_j is an exact inequality, so two scores that differ only by a tiny amount count as a full mismatch. The formula has no tolerance parameter, so any tolerance-based variant is a modification that must be stated explicitly.
- If categories are encoded as numbers (for example 1, 2, 3), the measure only tests equality. A mismatch between codes 1 and 2 counts the same as a mismatch between codes 1 and 9, so the ordering implied by the codes is discarded.
- Results depend on how values were encoded before comparison. Transforming the entries (for example rounding or re-coding) can change which positions are equal and therefore change the distance.
- The formula assumes that every position holds a defined entry. A missing value cannot be judged equal or unequal, so the formula does not say how to count it. Any rule for missing entries, such as comparing only positions where both entries are present, changes the definition of d and must be stated, together with the number of positions actually compared. Sequences of unequal length have no defined distance under this formula, and truncating or padding them changes which positions are compared and therefore the count.
Frequently asked questions
Is Hamming Distance an MCDM ranking method?
No. The formula counts the positions where two vectors differ. It does not weigh criteria or produce a ranking of alternatives, so it is a comparison measure and not a ranking method.
Can Hamming Distance be used with continuous scores?
It can be computed, but the rule counts any inequality as a mismatch, so the magnitude of differences is lost. If small differences should count as equal, a tolerance rule is needed. Such a rule modifies the formula and must be stated explicitly.
Does Hamming Distance need normalisation?
The formula contains no normalisation or weights, so scaling the values does not change the count by itself. However, any transformation that changes which entries are equal will change the result.
Sources
Hamming, R. W. (1950). Hamming Distance. Bell System Technical Journal
Open publication referenceShipunov-visual_statistics, p. 325