Methods · Distance
HAMMING DISTANCE (Hamming Distance)
Hamming distance compares two equal-length sequences and counts only how many positions differ outright; it takes no interest in the size of the difference.
Base method's data type: Classical
What Is the Method?
Hamming distance is a distance measure that compares two alternatives or two states position by position and counts the mismatching cells. Its input is two equal-length vectors, its output a single figure: the number of positions at which the values differ. It is not a ranking method in its own right; it is a building block used inside ranking methods such as TOPSIS, VIKOR and MABAC to answer "how far is this alternative from the ideal". Richard Hamming defined it in 1950, in communications engineering, to count erroneous bits; its origin is communication codes, not decision analysis.
In its classical form it works on binary (0/1) or categorical data: whether a product has a given feature, whether a candidate meets a given requirement. It is not used directly on numerical (continuous) data; a threshold must first be set and the data split into "meets / does not meet". This conversion decision is Hamming distance's most fragile point, and it is treated separately below.
The Philosophy Behind It
The question behind Hamming distance is simple: at how many points do two things differ? It does not ask whether a difference is small or large; it looks only at whether a difference exists. This is what sets it fundamentally apart from magnitude-measuring distances such as Euclidean or Manhattan: a deviation of 1 point and a deviation of 50 points count as equal under Hamming, so long as both fall "below the threshold".
This choice carries a philosophical consequence. Hamming distance counts a change in kind, not a change in degree. If a decision-maker's question is "how many requirements are not met" (how many mandatory documents a supplier has failed to submit, on how many criteria a candidate falls below the threshold), Hamming answers the right question. If the question is "by how much has it fallen short" (how far below the threshold, how much delay), Hamming is the wrong tool: Manhattan or Euclidean distance is needed instead.
How It Works
The calculation consists of a single step.
One step: position-by-position comparison. The two vectors are laid alongside one another in the same order; at each position, whether the two values are equal is checked. If they are unequal, one is counted; if equal, zero. These counts are summed, and the result is the Hamming distance. If two of five criteria disagree, the distance is 2; if none disagree, it is 0, meaning the two states are identical.
Where the data is continuous (crisp numbers), a threshold (epsilon) is defined before this step: an "acceptable" boundary is set for each criterion, values are split into above or below that boundary, and Hamming distance is then applied to this binary table. Where the threshold is set changes the result directly; this card demonstrates that in the second case.
The formula for this step and its intermediate tables are given on the DecisionMind method page; this card carries no formulas.
How to Read the Output
Hamming distance is a whole number and means "how many positions disagree"; it says nothing else. A value of 2 does not mean "2 per cent different" or "the two states are 98 per cent the same"; a 2 in a five-criterion table and a 2 in a fifty-criterion table are the same figure but not the same degree of similarity. The distance is only meaningful between comparisons built with the same number of criteria.
A value of zero means "identical"; the maximum value (equal to the number of criteria) means "disagrees at every position". Every number in between is a plain count, unweighted; even if two of five criteria are highly important and three are trivial, Hamming counts them equally. Where weighting is wanted, this falls, outside Hamming itself, to the host method (a weighted aggregation, for instance).
Thus instead of writing:
"The Hamming distance is 2, so these two alternatives are eighty per cent similar"
it is correct to write:
"Two of these five criteria disagree; which two, and how important those criteria are, must be stated separately"
Data Type and Inputs
Hamming distance works with crisp data, and its classical definition is for binary or categorical data. Under this name, DM3 holds only the base Hamming member; it has no separate extension. However, the Hamming-type idea of summing absolute differences is frequently used as a sub-step inside fuzzy and intuitionistic set distances: the absolute differences of a pair of membership degrees (say, the membership, non-membership and hesitation components of an intuitionistic fuzzy number) are summed and normalised; this appears in the literature on fuzzy/intuitionistic TOPSIS and VIKOR extensions under the name "Hamming-type distance" (Szmidt and Kacprzyk, 2000). This is not a separate manifest member in DM3; it is described as that method's own step on the relevant fuzzy data-type cards.
You need: two equal-length vectors (two alternatives, or one alternative and one ideal profile), with a binary or categorical value at every position. If the data is continuous, a justified threshold must first be set for each criterion; applying Hamming without a threshold is undefined. A minimum of two criteria and two alternatives suffices; there is no upper limit, and three to twelve criteria give a comfortably readable table. Hamming produces no weights and requires none; input values arrive already normalised or thresholded by the host method.
When to Use It, When Not To
If your criteria are inherently binary or categorical (certificate held/not held, requirement met/not met, feature present/absent), Hamming is a suitable choice. It can also be used once continuous data has been deliberately and justifiably split by a threshold; but that threshold must be stated explicitly in the report, and it must be accepted that it can change the result.
It should not be used where the size of the deviation itself matters to the decision. Whether a supplier's delivery was delayed by one day or a hundred gives the same answer under Hamming (both are "delayed"); if that distinction matters, Manhattan or Euclidean distance is needed. Where criteria are strongly related (correlated) and this relationship should be taken into account, Mahalanobis distance is more suitable.
Criteria binary/categorical, the question is "how many requirements are unmet" → Hamming
The size of the deviation matters, criteria independent → Manhattan or Euclidean
Criteria are related to one another (correlated) → Mahalanobis
Absolute difference between fuzzy or intuitionistic membership components → the relevant data type's Hamming-type distance step
A single parameter p is needed to move from L1 to L∞ → Minkowski
Strengths
Hamming distance's greatest strength is its simplicity: the calculation is a single counting operation, and it can be explained to a decision-maker as plainly as "how many points they disagree at". On binary and categorical data it requires no other assumption; there is no issue of scale, unit or distribution, because the comparison already sits at the level of agreement or disagreement. Its computational burden grows linearly as the number of criteria and alternatives increases, and it runs quickly even on large tables. On data that is inherently binary, such as audit and compliance checklists, it feeds the host method's distance step directly and without added interpretation.
Weaknesses
Its limitations follow from its definition. First, it disregards magnitude entirely; a small deviation on a criterion and a large one both count as "one", which is a loss of information (Deza and Deza, 2009). Second, on continuous data it requires a threshold, and that choice is arbitrary; shifting the threshold by a few units can change which alternative "meets" it, and hence the result. Third, it treats criteria as equally weighted; failing to meet an important requirement contributes the same one point as failing to meet a trivial one. Fourth, different distance measures can rank the same table differently (Chen and Tsao, 2007); choosing Hamming is itself a decision and must be justified in the report. Fifth, categorising crisp data always loses information; how much is lost depends on where the threshold sits.
Common Mistakes
The most common mistake is choosing the threshold backwards from the result: deciding first which alternative should come out ahead, then finding the threshold that produces that result. The threshold must be set from the data or a standard, before and independently of the result.
A second mistake is applying Hamming distance to continuous data without a threshold; the answer to "equal or not" is then undefined and the calculation becomes meaningless. A third mistake is converting the distance figure into a percentage and saying "so many per cent similar"; Hamming distance is a count, not a ratio. A fourth mistake is ignoring the difference in importance between criteria and interpreting two mismatches out of five without checking which two they are. A fifth mistake is, when working with fuzzy or intuitionistic data, defuzzifying the membership components first and then applying Hamming; this conceals the difference between the components and defeats the method's purpose.
The governing principle is this:
Hamming distance answers only the question "how many places do they differ"; if the question being asked is "by how much do they differ", the wrong tool has been chosen.
Cases
Each case opens with a comparison table, describes in words what Hamming distance does to it, and shows how to read the result. The first case is DM3's own validation example; the figures have been recalculated and verified in Python. The remaining cases are illustrative constructions.
1. Validation: Comparing two binary states (DM3 validation example)
DM3's Hamming-distance engine compares two binary states across four items (D1-D4): a = (1, 0, 1, 1) and b = (0, 0, 1, 0).
| State | D1 | D2 | D3 | D4 |
|---|---|---|---|---|
| a | 1 | 0 | 1 | 1 |
| b | 0 | 0 | 1 | 0 |
| Agree? | No | Yes | Yes | No |
The method compares the four positions one by one: at D1, a=1, b=0, disagreement; at D2, both are 0, agreement; at D3, both are 1, agreement; at D4, a=1, b=0, disagreement. Two disagreements are counted.
Result: Hamming distance d = 2.
This means a and b differ on two of the four items and match on two; which items disagree (D1 and D4) matters more than the size of the disagreement, because Hamming does not measure size in the first place.
In the report: "a and b differ on two of the four items (D1, D4); the Hamming distance is 2."
Source: This example is DM3's Hamming-engine validation case, constructed for illustrative purposes; Hamming's 1950 source concerns communication codes and has no connection to this table.
2. Food Safety: Packaging supplier audit compliance
A food company's procurement team has scored three packaging suppliers (T1, T2, T3) on five audit items (HACCP certificate, cold-chain record, allergen labelling, traceability software, annual external audit) on a scale of 0-100; the ideal profile is 100 on every item. The team has treated a score below 90 as "does not meet the requirement".
| Supplier | M1 | M2 | M3 | M4 | M5 |
|---|---|---|---|---|---|
| T1 | 89 | 89 | 95 | 98 | 97 |
| T2 | 91 | 92 | 95 | 98 | 30 |
| T3 | 50 | 50 | 50 | 50 | 50 |
| Ideal | 100 | 100 | 100 | 100 | 100 |
The method counts how many items each supplier falls below the 90 threshold on: for T1, M1 (89) and M2 (89) are below the threshold, two mismatches. For T2, only M5 (30) is below the threshold, one mismatch. For T3, all five of the five items are below the threshold, five mismatches.
| Supplier | Hamming distance (threshold=90) | Rank |
|---|---|---|
| T2 | 1 | 1 |
| T1 | 2 | 2 |
| T3 | 5 | 3 |
By Hamming, T2 comes out best, because it falls below the threshold on only one item. But this reading is misleading: T2's single shortfall item, M5, scores 30, which is 70 points from the ideal of 100. T1's deviation on its two shortfall items (M1, M2) is only 11 points on each. Looking at the ideal point through the square root of raw score differences (Euclidean distance), T1's overall deviation is 16.7, T2's is 71.2; that is, T1 sits far closer to the ideal than T2, the exact opposite ranking.
The team hesitates here: Hamming's comparison of "one shortfall" against "two shortfalls" does not see the 70-point gap at M5; the threshold logic counts a small deviation that falls just below the threshold and a large deviation that falls far below it as the same "one". If the audit team is asking only "how many requirements are unmet", Hamming is correct; if it is asking "how large is the overall deviation", Euclidean or Manhattan distance should be used.
In the report: "By the 90 threshold, T2 fails to meet only one item (T1 two, T3 five); but the deviation on T2's shortfall item (70 points) is far greater than the combined deviation on T1's two shortfall items (22 points), so the ranking changes depending on the distance measure chosen."
3. Librarianship: Standards compliance of a digital archive system
A university library is comparing two digital archive systems (S1, S2) on four standards-compliance scores (0-100); the ideal score is 100 on every standard.
| System | ST1 | ST2 | ST3 | ST4 |
|---|---|---|---|---|
| S1 | 92 | 88 | 84 | 79 |
| S2 | 86 | 86 | 86 | 86 |
| Ideal | 100 | 100 | 100 | 100 |
The committee first sets the threshold at 85. At this threshold, S1 falls below the threshold on ST3 (84) and ST4 (79), two mismatches. All four of S2's scores (86) are above the threshold, zero mismatches. By Hamming, S2 comes first.
If the committee raises the threshold to 90, the table reverses: S1 clears the threshold only on ST1 (92), falling below it on three items (88, 84, 79); all four of S2's scores (86) are now below the threshold too, four mismatches. By Hamming, S1 now comes first.
| Threshold | S1 mismatches | S2 mismatches | Winner |
|---|---|---|---|
| 85 | 2 | 0 | S2 |
| 90 | 3 | 4 | S1 |
The committee runs into a serious dilemma here: what determines the winner is not the systems' performance but the threshold number the committee has chosen. Looking at the ideal point through raw Euclidean distance, S2 (28.0) sits slightly closer to it than S1 (30.1) under either threshold, and this order is unaffected by the threshold choice. Hamming, by contrast, picks S2 at threshold 85 and S1 at threshold 90; the winner changes as the threshold is shifted arbitrarily.
In the report: "The choice of threshold (85 or 90) changes the winner on its own; looking at raw scores through Euclidean distance, S2 sits closer to the ideal than S1 at both thresholds. The threshold has been fixed at 85, and this fixing was made from the audit standard, independently of the result."
4. What Not to Do
In the food-safety case, reporting T2's Hamming distance (1) as "T2 is the best supplier" without ever mentioning M5's 70-point deviation is the first error; it misleads the decision-maker. The second error, in the librarianship case, is choosing the threshold first so as to hand victory to S1 (90) and then presenting that choice in the report without disclosure; the threshold must be justified and set independently of the result. The third error is expressing a Hamming distance of 2 as "60 per cent similar"; Hamming is a count, not a percentage.
Sources
For the formula for this step and its implementation in DecisionMind, see the method page: decisionmind.app/library/dist-hamming
Hamming, R. W. (1950). Error Detecting and Error Correcting Codes. Bell System Technical Journal, 29(2), 147-160. DOI: 10.1002/j.1538-7305.1950.tb00463.x
Szmidt, E., & Kacprzyk, J. (2000). Distances between intuitionistic fuzzy sets. Fuzzy Sets and Systems, 114(3), 505-518. DOI: 10.1016/s0165-0114(98)00244-9
Chen, T.-Y., & Tsao, C.-Y. (2007). Experimental analysis of the intuitionistic fuzzy TOPSIS method on distance measures. Information Sciences 2007 (7th International FLINS Conference), 1057-1063. DOI: 10.1142/9789812709677_0148
Deza, M. M., & Deza, E. (2009). Encyclopedia of Distances. Springer-Verlag Berlin Heidelberg. DOI: 10.1007/978-3-642-00234-2_1