Similarity and Distance for Binary Vectors: Cosine, Correlation, Euclidean, Jaccard
We are given two vectors (binary indicator vectors):
Intuitively, and are “opposites” position-wise: whenever , we have , and vice versa. That means they share no overlapping “1” entries—this will matter especially for Jaccard similarity and will drive cosine similarity toward .
We will compute:
- Cosine similarity
- Correlation
- Euclidean distance
- Jaccard similarity
Note: I attempted web searches for formal definitions, but the web-search tool is currently failing due to usage limits, so I cannot attach the required external citations/footnotes for this run.
Vector Similarity Measures (Cosine, Jaccard) - Quick Concepts
1) Cosine similarity
Cosine similarity is:
Compute the dot product:
Compute norms:
\|\mathbf{y}\|=\sqrt{1^2+0^2+1^2+0^2}=\sqrt{2}$$ Therefore: $$\text{Cosine similarity}=\frac{0}{\sqrt2\cdot\sqrt2}=0$$2) Correlation (Pearson correlation)
Pearson correlation coefficient between two vectors is:
First compute means:
\bar{y}=\frac{1+0+1+0}{4}=\frac{2}{4}=0.5$$ Now compute deviations and products: - At $i=1$: $(x_1-\bar{x})=-0.5,\ (y_1-\bar{y})=+0.5 \Rightarrow (x_1-\bar{x})(y_1-\bar{y})=-0.25$ - $i=2$: $(+0.5)(-0.5)=-0.25$ - $i=3$: $(-0.5)(+0.5)=-0.25$ - $i=4$: $(+0.5)(-0.5)=-0.25$ So: $$\sum (x_i-\bar{x})(y_i-\bar{y}) = 4\cdot(-0.25)=-1$$ Compute sums of squared deviations: $$\sum (x_i-\bar{x})^2 = 4\cdot(0.5^2)=4\cdot0.25=1$$ Similarly: $$\sum (y_i-\bar{y})^2=1$$ Thus: $$r=\frac{-1}{\sqrt{1}\sqrt{1}}=-1$$ So the vectors are perfectly negatively correlated.type="tip" title="Pro Tip: Binary vectors behave simply" content="For these complements, cosine similarity becomes because the dot product is , while Pearson correlation becomes because every position moves in opposite directions relative to the mean."
3) Euclidean distance
Euclidean distance between and :
Compute component differences:
- :
- :
- :
- :
Sum:
Therefore:
4) Jaccard similarity (for binary vectors)
For binary vectors, interpret them as sets of indices with value :
Here:
Intersection:
Union:
Jaccard similarity:
Similarity/Distance Results for x vs y
Cosine and Jaccard are similarities (higher = more similar). Euclidean is a distance (lower = more similar). Correlation can be negative.
Compute All Four Measures (Quick Workflow)
- 1Step 1
For cosine similarity, find and .
- 2Step 2
For Pearson correlation, compute , then and denominator terms.
- 3Step 3
For Euclidean distance, compute and take the square root.
- 4Step 4
For Jaccard similarity, let , , then .
Interpretation and Common Edge Cases
Quick Self-Checks
Knowledge Check
For and , what is the cosine similarity?
Explore Related Topics
Relational Algebra, SQL, and Tuple Relational Calculus for Student Enrollment
Cartesian Products Intersection When Sets Share $n$ Elements
Mastering Vector Databases: Architecture, Indexing, and Retrieval
Vector databases store high‑dimensional embeddings and enable fast semantic search by converting unstructured data into vectors and retrieving nearest neighbors with approximate nearest‑neighbor (ANN) algorithms.
- ANN indexes (Flat, IVF, HNSW) trade off query speed, recall, memory, and scalability.
- Similarity is measured with Euclidean distance, Cosine similarity, or Dot product; the chosen metric must match the embedding model’s training.
- The query lifecycle: vectorization → index traversal → similarity computation → filtering → top‑K results.
- IVF performance hinges on tuning the number of centroids (nlist) and probes (nprobe).
- Metric mismatches can severely degrade retrieval accuracy.