Lecture
Okapi BM25 — a ranking function used by search engines to order documents by their relevance to a given search query. It is based on a probabilistic model developed in the 1970s and 1980s by Stephen Robertson, Karen Spärck Jones, and others.
The function itself is called BM25 (BM from "best match"), but it is often called «Okapi BM25», after the name of the Okapi search system, created at London City University in the 1980s and 1990s, in which this function was first applied.
BM25 and its various later modifications (such as BM25F) are modern TF-IDF-like ranking functions that are widely used in practice in search engines. In web search, these ranking functions are often included as components of a more complex, often machine-learned, ranking function.
BM25 – a function for computing the textual relevance of documents, developed by the British researchers Stephen Robertson and Karen Spärck Jones and published in 1994. It is based on empirical data, in an attempt to improve on the results of the TF-IDF criterion. The best match between the expected result and the calculated one was shown by algorithm 25 in the list, which is why it got its name «Best matching», or BM25. It was first implemented in the Okapi search system, and was later used as the basis for the text analyzers of modern search engines.
BM25 — a search function over an unordered set of terms (a «bag of words») and a set of documents, which it scores based on the occurrence of the query words in each document, without taking into account the relationships between them (such as proximity). It is not a single function, but a family of functions with various components and parameters. One of the common forms of this function is described below.
Let there be a query , containing the words
, then the BM25 function gives the following relevance score of document
to query
:
where is the frequency of the word (term frequency, TF)
in document
,
is the length of the document (the number of words in it), and
— the average document length in the collection.
and
— free coefficients, usually chosen as
and
.
is the inverse document frequency (IDF) of the word
. There are several interpretations of IDF and small variations of its formula. Classically, it is defined as:
where is the total number of documents in the collection, and
— the number of documents containing
. But «smoothed» variants of this formula are used more often, for example:
Note that the above IDF formula has the following drawback. For words that occur in more than half of the documents in the collection, the IDF value is negative. Thus, given any two nearly identical documents, one of which contains a word while the other does not, the second one may receive a higher score.
In other words, frequently occurring words will spoil the final score of the document. This is undesirable, so in many applications the above formula can be adjusted in the following ways:
Suppose that the search word occurs in
documents. Then a randomly chosen document
contains the word with probability
(where
is the cardinality of the set of documents in the collection). In this case the information value of the phrase «
contains
» is as follows:
Now suppose there are two search words and
. If they occur in a document independently of each other, then the probability of finding them in a randomly chosen document
is as follows:
and the information content of this event
This is roughly what is expressed by the IDF component in BM25.
The «bag-of-words» model used does not take into account:
Therefore, the BM25 formula alone, in its pure form, was not used for evaluating document relevance.
Although Robertson claims that a probabilistic model was used to derive the formula, some experts consider it to be a «fitting» to the desired result. Free coefficients that can take various values are built into the BM25 function. They are chosen so as to «fit» the search results to already available data. Documents are first evaluated by assessors, who determine what is bad and what is good. Then, based on this data, the aforementioned coefficients are chosen so as to arrange the documents in the same way the assessors did – the so-called «monkey principle». The relevance score of document D for query Q , containing the words qi=q1,q2…qn, according to the BM25 formula:

Where:
— the frequency of use of the word qi in the document;
— the total number of words in the document;
For IDF a smoothed formula is used:

Where:
The formula for
TF
also changed:

The relevance for query Q is equal to the sum of the relevances for all the words qi=q1,q2…qn in the query. At first glance, the operating principle is the same as TF-IDF:
But there are also differences:
| Number of occurrences | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
|---|---|---|---|---|---|---|---|---|---|---|
| Score growth in percent | — | 50% | 20% | 11.1% | 7.1% | 5.0% | 3.7% | 2.9% | 2.3% | 1.9% |

a) negative values in the sum are simply ignored;
b) a lower bound is imposed, upon reaching which the IDF value is treated as a fixed number;


| Text volume (words) | 50 | 150 | 250 | 350 | 450 | 550 | 650 | 750 | 850 |
|---|---|---|---|---|---|---|---|---|---|
| Score drop in percent | — | -4.60% | -4.40% | -4.20% | -4.10% | -3.90% | -3.80% | -3.60% | -3.50% |
Few remember the «golden era of SEO», when webmasters spammed keywords, achieved higher relevance through sheer quantity, and, having added a thousand SAPE links on top of all that, reached the TOP within a couple of weeks.
Let us take the same texts and calculate the number of documents containing the query words, and the average document length.
| Textual | Relevance | Of the document | In search | ||
|---|---|---|---|---|---|
| Number of documents containing the word | 2 | 3 | 2 | 3 | |
| Doc.1 | Doc.2 | Doc.3 | Doc.4 | Average | |
| Document length | 25 | 22 | 26 | 17 | 22.5 |
Next, let us find the IDF for each word in the query
| Query words | |||||
| Textual | Relevance | Of the document | In search | ||
| Doc.1 | Word frequency | 0.080 | 0.080 | 0.000 | 0.000 |
| Doc.2 | 0.045 | 0.045 | 0.000 | 0.045 | |
| Doc.3 | 0.000 | 0.115 | 0.038 | 0.038 | |
| Doc.4 | 0.000 | 0.000 | 0.059 | 0.059 | |
| IDF | 0 | -0.368 | 0 | -0.368 |
As a result, we obtain the following TF value and Score for the query «Textual relevance of a document in search»:
| Textual | Relevance | Of the document | In search | ||||
|---|---|---|---|---|---|---|---|
| Doc.1 | TF | 0.107 | 0.107 | 0.000 | 0.000 | Score | 0.00107 |
| Doc.2 | TF | 0.068 | 0.068 | 0.000 | 0.068 | Score | 0.00136 |
| Doc.3 | TF | 0.000 | 0.147 | 0.051 | 0.051 | Score | 0.00198 |
| Doc.4 | TF | 0.000 | 0.000 | 0.104 | 0.104 | Score | 0.00104 |
A clear example of how IDF can take negative values for words that occur in more than half of the documents. Instead of a negative IDF value, a fixed IDF=0.01 was used. «Document 3» received the highest score, whereas in the classical TF-IDF formula it was document 1, which now has the lowest Score under BM25. Distribution of scores across different queries:
| BM25 | Queries | |||
|---|---|---|---|---|
| Document No. | Relevance | Textual relevance | Textual relevance of the document | Textual relevance of the document in search |
| Doc.1 | ||||
| Doc.2 | ||||
| Doc.3 | ||||
| Doc.4 |
| BM25 | Queries | |||
|---|---|---|---|---|
| Document No. | Relevance | Textual relevance | Textual relevance of the document | Textual relevance of the document in search |
| Doc.1 | ||||
| Doc.2 | ||||
| Doc.3 | ||||
| Doc.4 |
Up to this point we have been looking at a limited sample, in which it is known in advance that the documents are roughly on-topic. For a better understanding of the formula, let us consider an example with a non-topical collection of documents. Let us find the Score in a sample of 20,000 documents.

The IDF for the word «document» is significantly higher than for the words «relevance» and «textual». Now let us increase the frequency of the word «document» in doc2 from 2% to 5%.

We see a significant increase in TF and Score while IDF remains unchanged. Let us increase the frequency of «textual» to 6% in doc 1.

The sum of the frequencies of each query word in document 1 and document 2 is the same and amounts to 13%, but the term «document» is used less often in the collection. From the final score, we can say that the significance of narrowly specialized words is considerably higher than that of other terms from the query.
In 2006-2007, based on the methodology of a user with the handle Minych on the Searchengines forum, it was concluded that Yandex used similar formulas when calculating relevance:

Where:
— the number of occurrences of the word ti in the search index b;
— the term weight.
We see that, according to this formula, maximum relevance is achieved when the promoted query ti is the most frequent word in the document, n(ti,d)=nmax, while keyword density has no effect on the score at all. Minych was the first to introduce the concept of «keyword nausea». In his methodology, nausea W=max(√7,√nmax) – the square root of the frequency of the most frequently used word nmax in the document. If nmax is less than 7, then W=√7. When a certain spam threshold was exceeded for a specific word, for example W>25, a penalty was imposed on the document.
Comments