AI Glossary / Causal inference and experimentation / Intermediate

Hamming distance

Definition

Hamming distance counts the positions at which two aligned sequences of equal length contain different symbols. For example, two binary strings that differ in three bit positions have ordinary Hamming distance three. A normalized version divides the count by the number of positions, producing a fraction between zero and one. Some software calls that normalized version simply Hamming distance, so the convention must be checked. The measure compares positional equality, not numerical magnitude or semantic meaning.

What it means

Each position is treated as one comparison: match or mismatch. A difference between numerical symbols 1 and 2 contributes the same unweighted amount as a difference between 1 and 100. Position alignment is essential. If one string contains an inserted character, subsequent symbols may shift, making positional mismatches a poor description of the actual edit. Hamming distance is therefore different from edit distances that allow insertions and deletions to realign sequences.

How it works

Verify that the sequences have the same length and that corresponding positions represent the same kind of information. Compare each position and assign 1 for unequal symbols and 0 for equal symbols. Sum these indicators for the ordinary distance, or divide by length for the normalized distance. For bit vectors, an exclusive-OR operation identifies differing bits and a population count totals them. Weighted versions assign different importance to positions, but their normalization and interpretation must be declared. Empty sequences and missing values require explicit handling.

Three practical examples

  • Binary similarity search: find signatures with few differing bits when the signature design makes bit differences useful for retrieval.
  • Label-vector comparison: count differences between two fixed sets of binary attribute decisions.
  • Data integrity analysis: measure how many symbol positions changed between an original and received fixed-length sequence, without assuming the distance itself repairs those changes.

Think of it this way

Think of two rows of numbered lockers, each containing a colored card. Compare locker 1 with locker 1, locker 2 with locker 2, and count different colors. The analogy captures fixed alignment and equal mismatch cost. It does not handle a newly inserted locker or explain whether two different colors have similar meanings.

Worked example

Let vector A be [1, 0, 1, 1, 0, 0, 1, 0] and vector B be [1, 1, 1, 0, 0, 0, 1, 1]. Positions 2, 4, and 8 differ, while the other five positions match. Ordinary Hamming distance is 3, and normalized Hamming distance is 3/8 = 0.375. A similarity defined as one minus normalized distance would be 0.625, but that is a separately defined similarity, not the distance itself.
Compare A with C = [1, 0, 1, 1, 0, 0, 1, 1]. Only position 8 differs, so its distance from A is 1, or 0.125 normalized. Under this measure, C is closer to A than B is. That ranking is useful only if all eight positions have an appropriate shared interpretation. If position 2 records a critical requirement and position 8 records a minor optional attribute, equal-cost Hamming distance may not reflect the application's priorities. A weighted measure or a separate constraint would require a new, documented definition.

Illustrative case study

A fictional catalog team uses binary signatures to shortlist similar records. Its nearest-neighbor calculation works correctly, but several returned records are poor semantic matches because the signature includes many low-value attributes and too few meaningful ones. The team revisits feature design and validates retrieval quality with reviewed examples. It keeps Hamming distance as an efficient comparison operation while recognizing that a mathematically close code is only a proxy for a useful catalog match.

Common misconceptions and mistakes

Hamming distance does not measure how far apart numeric values are, and it does not automatically measure text meaning. Equal sequence length is not enough; positions must be aligned meaningfully. A normalized result of 0.2 is not two mismatches unless the sequence length is ten. The raw and normalized forms preserve nearest-neighbor ordering for equal-length unweighted vectors, but their numerical thresholds differ. Distances between hashes or embeddings also depend on how those representations were constructed, so closeness should be validated for the task.

Business use

Use the distance for fixed-length categorical or binary representations with meaningful positional correspondence. Document encoding, length, normalization, weights, and missing-value treatment. Check that a threshold expressed in bits is not accidentally applied to a fractional software result. Evaluate the downstream retrieval or comparison task separately. Where insertions, deletions, magnitude, or semantic similarity matter, choose a measure designed for those relationships or justify how the representation converts them into suitable positional comparisons.

People also ask

Is Hamming distance limited to binary data?

No. It can compare equal-length sequences of categorical symbols, counting unequal positions.

Does it change if both sequences are reordered in the same way?

No, provided corresponding positions remain aligned and any weights move with their positions.

Can it compare different-length strings directly?

Ordinary Hamming distance assumes equal length; padding or alignment introduces additional rules and changes the interpretation.

Check your understanding

Why do vectors [1, 2] and [1, 100] have ordinary Hamming distance one?

Answer: Only the second position differs. Hamming distance counts unequal symbols and does not use the numerical size of the difference between 2 and 100.

Related terms

  • Hamming loss
  • Edit distance
  • Levenshtein Distance
  • Similarity Search
  • Alignment
  • Normalized Hamming distance
  • exclusive OR
  • population count
  • binary signature
  • categorical distance

Learn next

Compare two short bit strings by hand, then calculate both raw and normalized distances. Repeat with numeric symbols to see that magnitude is ignored. Next study edit distance and representation design so the chosen comparison reflects the type of difference that matters in your data.

  • Edit distance

More in Causal inference and experimentation

  • Gini impurity - Gini impurity measures how mixed the class labels are within a group of observations, commonly a node in a classification tree.
  • Granger causality - Granger causality is a predictive relationship between time series: the past of one series helps predict another beyond the information already included in...
  • Hamming - Hamming is a name attached to several distinct concepts, including Hamming distance, Hamming loss, Hamming codes, and the Hamming window.
  • Heterogeneous treatment effect - A heterogeneous treatment effect is a causal effect that varies across individuals, groups or contexts.
  • Highly adaptive lasso - Highly adaptive lasso, or HAL, is a flexible regression method that represents complex relationships using a large collection of basis functions and...
  • Holdout cross-validation - Holdout cross-validation is an imprecise phrase commonly used for evaluating a model on a held-out portion of data, sometimes repeated across several random...

Sources

SciPy, Hamming distance and normalization convention: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.hamming.html SciPy, pairwise distance documentation: https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.pdist.html Hamming, Error Detecting and Error Correcting Codes: https://doi.org/10.1002/j.1538-7305.1950.tb00463.x

Previous: Hamming Next: Hamming loss

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer