Cosine Similarity Calculator
About This MicroSim
This MicroSim demonstrates how cosine similarity measures relatedness between vectors by showing the step-by-step calculation and allowing interactive manipulation of vector components.
Iframe Embed Code
1 2 | |
Description
The Cosine Similarity Formula
\[\text{cosine similarity}(A, B) = \frac{A \cdot B}{|A| \times |B|}\]
| Component | Meaning |
|---|---|
| A ยท B | Dot product (multiply and sum components) |
| |A| | Magnitude (length) of vector A |
| |B| | Magnitude (length) of vector B |
| Result | Value from -1 to 1 |
How to Use
- Drag vector endpoints to change their direction
- Watch the angle update between vectors
- Follow the calculation steps on the right panel
- Use presets to see special cases:
- Identical (similarity = 1)
- Perpendicular (similarity = 0)
- Opposite (similarity = -1)
- Toggle calculation steps to show/hide the math
Interpreting Results
| Score | Interpretation | Color |
|---|---|---|
| 0.7 - 1.0 | Similar/Related | Green |
| 0.3 - 0.7 | Somewhat related | Yellow |
| -1.0 - 0.3 | Unrelated/Opposite | Red |
Learning Objectives
After using this MicroSim, students will be able to:
- Calculate cosine similarity between two vectors
- Explain how angle relates to similarity
- Predict similarity values based on vector positions
Lesson Plan
Introduction (5 minutes)
- Review vectors and angles
- Explain why direction matters more than length
- Introduce the cosine similarity formula
Hands-On Calculation (15 minutes)
- Start with default vectors, trace the calculation
- Move to "Identical" preset - why is similarity 1?
- Move to "Perpendicular" - why is similarity 0?
- Move to "Opposite" - why is similarity -1?
- Try intermediate positions, predict before calculating
Connection to Embeddings (10 minutes)
- Embeddings are vectors in 384 dimensions
- Same formula applies in high dimensions
- Cosine similarity ignores magnitude, focuses on direction
Discussion (5 minutes)
- Why use cosine instead of Euclidean distance?
- When would cosine similarity give unexpected results?