BM25, Best Matching 25, is a probabilistic ranking function used in information retrieval to score how relevant a document is to a query. It is a refined version of TF IDF and is widely used in search engines and classical retrieval systems
Before reading this and applying this you need to know about TF-IDF and Inverted Index.
IDF is calculated as log(N + 1 / df + 1)
The problems with this formula:
df = 0 (we "solved" this by adding 1 to both the numerator and denominator)BM25 solved this by comparing the documents that don’t contain the term against documents that do contain it:
$$ IDF(t) = \log \frac{N - df + 0.5}{(df + 0.5) + 1} $$
(N - df + 0.5): How many documents DON'T have this word (with a small adjustment)(df + 0.5): How many documents DO have this word (with a small adjustment)+ 1 at the end makes sure the score never goes negative. This helps handle unusual cases.