# Vector Similarity Metrics

The vector search Metric defines the distance measure between vectors during the vector search. From a mathematical point of view, the K nearest neighbors are those with the smallest distance from the search query vector.&#x20;

In vector search higher score reflects greater similarity, or smaller distance. Thus, a algebraic conversion from distance to score is required. This relation is given in the Hyperspace Score column.

<table><thead><tr><th width="124.33333333333331">Metric</th><th width="346">          Distance Measure</th><th>            Hyperspace Score</th></tr></thead><tbody><tr><td>l2</td><td><span class="math">d(X, Y) = \sum_{i} (x_i - y_i)^2</span></td><td><span class="math">\frac{1}{1 + d}</span></td></tr><tr><td>ip</td><td><span class="math">d(X,Y)=-∑_i  x _i ⋅y_ i </span></td><td><span class="math">\begin{cases}     \frac{1}{1 + d} &#x26; \text{if } d \geq 0 \\     1 - d &#x26; \text{d&#x3C;0} \end{cases}</span></td></tr><tr><td>hamming</td><td><span class="math">d(X,Y)= \sum_{i=1}^{n} \delta(x_i, y_i) </span></td><td><span class="math">\frac{1}{1 + d}</span></td></tr></tbody></table>

### Euclidean Metric (l2) <a href="#euclidean-distance-l2" id="euclidean-distance-l2"></a>

The $$L2$$  metric quantifies the similarity as the distance between two points in a Euclidean space. The metric is derived from the Pythagorean theorem and represents the length of the shortest path between the two points:

$$
L^2(X, Y) = \left( \sum\_{i} |x\_i - y\_i|^2 \right)^{\frac{1}{2}}
$$

Note that the $$L^2$$  metric is a special case of the $$L^p$$  metric -

$$
L^p(X, Y) = \left( \sum\_{i} |x\_i - y\_i|^p \right)^{\frac{1}{p}}
$$

{% hint style="info" %}
Hyperspace uses the squared  $$L2$$  metric for calculation efficiency. This does not affect the order of the candidates.
{% endhint %}

### Inner Product (ip) <a href="#euclidean-distance-l2" id="euclidean-distance-l2"></a>

The inner product metric quantifies the similarity between two vectors as -1 times the projection of one vector on the other, which is -1 if the vectors are parallel and 0 if they are perpendicular. The minus sign ensures that minimal distance corresponds to maximum similarity. In a Euclidean vector space, the inner product is:

$$
IP(X,Y) =-∑\_i  x *i ⋅y* i
$$

{% hint style="info" %}
Vectors must be normalized before using the IP metric.
{% endhint %}

### Hamming Distance (hamming) <a href="#euclidean-distance-l2" id="euclidean-distance-l2"></a>

The Hamming distance metric quantifies the similarity between two lists by measuring the number of positions at which the corresponding symbols differ. The metric operates over binary strings, and counts the bits that are different between two binary vectors. The lower the Hamming distance, the more similar the strings are considered to be, and vice versa.

The hamming distance is given by

$$
\text{Hamming}(X, Y) = \sum\_{i=1}^{n} \delta(x\_i, y\_i)
$$

where  $$\delta(x\_i, y\_i)  =\begin{cases}     1 & \text{if }   x\_i=y\_i\     0 & \text{otherwise} \end{cases}$$ is the [Kronecker delta](https://en.wikipedia.org/wiki/Kronecker_delta#:~:text=In%20mathematics%2C%20the%20Kronecker%20delta,example%2C%20because%20%2C%20whereas%20because%20.). The above formula counts the number of characters that are different between $$X$$ and $$Y$$ .


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.hyper-space.io/hyperspace-docs/flows/setting-up/creating-a-database-schema-configuration-file/vector-similarity-metrics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
