From: astanabe (@astanabe) <noreply+astanabe@github.com>
To: pgvector/pgvector <noreply+pgvector-pgvector@github.com>
Subject: Re: [pgvector/pgvector] PR #806: Added support of inner product distance for bit vectors
Date: Tue, 25 Mar 2025 19:32:35 +0000
Message-ID: <gh-pgvector-pgvector-806-c2752322541@github.com> (raw)
In-Reply-To: <gh-pgvector-pgvector-806@github.com>
References: <gh-pgvector-pgvector-806@github.com>
Consider a case where the following DNA base sequence is stored in a database:
```
DNA1 CGTATACGTATAG
```
We can create a vector by extracting 6-character-long words, shifting one character at a time, and counting their frequency.
This is similar to an n-gram indexing in full-text search.
In molecular biology, this is called a k-mer, but it is exactly the same as n-gram in full-text search.
DNA1 would look like this:
```
CGTATA 2
GTATAC 1
TATACG 1
ATACGT 1
TACGTA 1
ACGTAT 1
GTATAG 1
```
DNA consists of only four types of characters: A, C, G, and T.
Therefore, when the word length is 6 characters, there are 4096 possible words, making the vector 4096-dimensional.
Now, consider using the following as a query for searching:
```
Query CGTATAC
```
This can be broken down as follows:
```
CGTATA 1
GTATAC 1
```
In this scenario, the Hamming distance between DNA1 and the Query is 5, and the negative inner product is -2.
Furthermore, consider that the database also contains the following sequence:
```
DNA2 AGTATAC
```
For DNA2 and the Query, the Hamming distance is 1, and the negative inner product is -1.
In this search, we want to retrieve DNA1, but because DNA2 is closer when using the Hamming distance, we would need to increase the LIMIT.
In real-world molecular biology research, the database often contains a mixture of very long and short nucleotide sequences, and they are searched with short query sequences.
In this situation, when using the Hamming distance, shorter sequences that are slightly less similar can appear as closer neighbors than longer sequences that are more similar.
Jaccard distance improves upon the Hamming distance.
Sorensen distance improves upon Jaccard distance.
Simpson distance improves upon Sorensen distance.
However, the negative inner product surpasses all of these in terms of accuracy, and it is the fastest to compute in this case.
The same thing should happen when searching for short keywords or phrases in a large number of long natural language texts.