1. Architecture and scope
Text embedding models map text to vectors. A retrieval system can compare a query vector with document vectors and rank candidates by a chosen similarity function. That representation is learned by a real model; the four vectors below are hand-authored geometric examples, not embeddings of the displayed labels. [1]
Cosine similarity divides the dot product by both vector lengths. It measures direction for nonzero vectors. Multiplying one vector by a positive scalar leaves its cosine with another vector unchanged. On unit-length vectors, the denominator is one, so cosine equals the dot product. [2]
Scope. Hand-authored two-dimensional vectors. No embedding model is downloaded or evaluated.
Workflow and scope notes
- Prepare text. Decide which text units the actual system will encode. Chunking and model choice are outside the geometric experiment.
- Encode vectors. A real encoder supplies numeric representations for query and passages. Here the coordinates are declared by hand so every score can be inspected.
- Score candidates. Compare nonzero vectors using the stated cosine formula. Other similarity functions have different assumptions and behavior.
- Inspect retrieval. Sort scores and evaluate against the question’s actual evidence needs. A high score is a geometric result, not an independent truth or relevance judgment.
2. Vector direction and cosine ranking
Documents D1–D4 point at 0°, 45°, 90° and 135°. Rotate the query between 0° and 180°. Each arrow has length 1. The second chart shows all four scores on the full −1 to +1 scale.
Vector direction and cosine ranking
Original synthetic experiment · Change an input to update the figure.
cos(q,d) = (q · d) / (‖q‖ ‖d‖), for nonzero vectors. Exact ties use document ID order in this toy. Labels have no semantic meaning and the score is not a probability.
Calculation record
{
"inputs": {
"angle": "30"
},
"scores": [
0.8660254037844387,
0.9659258262890683,
0.5000000000000001,
-0.25881904510252085
],
"order": [
"D2",
"D1",
"D3",
"D4"
]
}| Case | Result | Interpretation |
|---|---|---|
| 0° between vectors | Cosine = 1 | Same direction, including positive rescaling. |
| 90° between vectors | Cosine = 0 | Orthogonal directions. |
| 180° between vectors | Cosine = −1 | Opposite directions; negative values are possible. |
3. Interpretation and limitations
At a query angle of 45°, D2 has score 1, while D1 and D3 tie near 0.7071. The tie is broken by ID here. A query at 180° gives D1 score −1. The figure therefore preserves negative scores rather than clipping them into an apparently probabilistic range.
The mathematical denominator vanishes for a zero vector. This notebook’s Python helper raises an error instead of choosing a library-specific fallback. A production system should record the actual encoder and similarity implementation, including normalization and its zero-vector behavior. [2]
Editorial retrieval check: inspect which passages a ranking actually returns and label their relevance to real questions. A model’s similarity convention, an index configuration and evidence quality are separate parts of that evaluation. This exercise establishes only the declared geometry. [1] [3]
At 90°, predict the order of D1–D4, including the tie. Then explain why multiplying D3 by 7 would not change its cosine with the query.
4. Worked Python example
The core calculation below uses only the Python standard library. The full download adds the verification assertions and input fixtures. It runs locally on synthetic data; it does not load a model or contact a service.
from math import cos, sin, radians, sqrt, isclose
def vector(angle):
# Hand-authored unit vectors; these are not text model outputs.
return (cos(radians(angle)), sin(radians(angle)))
def cosine(a, b):
denominator = sqrt(sum(x*x for x in a) * sum(y*y for y in b))
if denominator == 0:
raise ValueError("Zero vector is outside this toy's domain")
return sum(x*y for x, y in zip(a, b)) / denominator
def result(angle):
scores = [cosine(vector(angle), vector(d)) for d in (0, 45, 90, 135)]
order = sorted(range(4), key=lambda i: (-round(scores[i], 12), i))
return {"scores": scores, "order": ["D" + str(i+1) for i in order]}
Download full Python example and checks ↓
Run: python -I lab.py. Recorded run: Python 3.13; 7 fixture states plus scenario assertions passed. The JSON output is a reproducibility record, not a real-system benchmark.
5. Questions and explanations
Predict before opening the explanation. Answers refer to the stated scope and synthetic inputs.
References and source links
- Intel / Hugging Face authors. (2024). CPU Optimized Embeddings with 🤗 Optimum Intel and fastRAG.https://huggingface.co/blog/intel-fast-embedding
- scikit-learn maintainers. (v1.9.1). cosine_similarity — scikit-learn 1.9.1 documentation.https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.cosine_similarity.html
- Sentence Transformers maintainers. (Documentation accessed 2026). Semantic Textual Similarity — Sentence Transformers documentation.https://www.sbert.net/docs/sentence_transformer/usage/semantic_textual_similarity.html
Source checks and provenance
Primary publisher pages and relevant passages checked on 13 Nov 2026. Source support is not independent expert fact verification. Main documentation can change; retrieved-body hashes, dates, version labels and saved revision IDs are in the audit download.
Evidence: Information Retrieval with Embedding Models. Saved public original used for the embedding/retrieval context, not its hardware measurements.
Public ResearchNode original e22d4f6821d6b88bbaee5294a65f8a31
Revision add9531d9d9c7a46dfd0f2c4d2bad106. Captured 2026-11-06.
Evidence: cosine_similarity API definition. Mathematical definition. This toy rejects zero vectors explicitly.
Evidence: Similarity Calculation. Library documents multiple similarity choices. A cosine score is not a calibrated relevance probability.