Cosine, Dot Product, and Distance

Cosine similarity compares vector direction; dot product is sensitive to magnitude unless vectors are normalized; Euclidean distance measures geometric distance. The right metric depends on the model's intended use and index configuration. Raw scores across models are not universal probabilities of relevance.

Calibrate thresholds on labeled queries. A similarity score of 0.82 does not inherently mean an 82% chance of correctness. Confirm whether the database expects normalized vectors and which metric is configured.

Implementation example
function cosine(a,b) {
  if (a.length!==b.length) throw new Error("dimension mismatch");
  const dot=a.reduce((sum,x,i)=>sum+x*b[i],0);
  const na=Math.sqrt(a.reduce((sum,x)=>sum+x*x,0));
  const nb=Math.sqrt(b.reduce((sum,x)=>sum+x*x,0));
  return dot/(na*nb);
}
💡 This educational implementation assumes non-zero, equal-length vectors; production systems use optimized index operations.
Key Takeaways
  • Inspect the relevant pipeline stage instead of guessing.
  • Preserve source provenance, versions, and access scope.
  • Measure quality on representative queries.
Knowledge Check

What does cosine similarity compare?