Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 1.4 — BM25 by Hand
RAG and Vector Search · Example 2 of 2

Saturation and length normalisation, isolated

The factor is the part of the term score that depends on the passage. Multiply it by idf to get the full term score.

BM25 repairs TF-IDF: repeated words add less and less to the score (saturation, controlled by k1), and long passages are scaled down against the average length (controlled by b). It is the default keyword ranker of search engines.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 5 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write idf(n, df) with BM25's form and print it for n = 12 and df = 1, 8 and 12, to 3 decimals.

02

Write factor(tf, k1=1.2) for a passage of average length (len = avg_len, so the length part is 1) and print it for tf = 1, 2, 5.

Explain it without notes

01

Write the BM25 score for one term and explain each part.

02

What does k1 control, and what is the maximum value of the saturating factor?

03

What does b control, and what do b = 0 and b = 1 mean?

04

Why does the 1 + inside BM25's idf matter?

05

Why can't you compare BM25 scores between two different queries?

Saturation and length normalisation, isolated
Sign in to run this example in your browser.

Expected output

saturation, a passage of average length
tf   raw     k1=0.5  k1=1.2  k1=3.0
1    1       1.000   1.000   1.000
2    2       1.200   1.375   1.600
3    3       1.286   1.571   2.000
5    5       1.364   1.774   2.500
8    8       1.412   1.913   2.909
20   20      1.463   2.075   3.478
limits: 1.5, 2.2, 4.0 (that is k1 + 1)
length, tf=2 and k1=1.2
len/avg  b=0     b=0.75  b=1
0.5      1.375   1.600   1.692
1.0      1.375   1.375   1.375
2.0      1.375   1.073   1.000
4.0      1.375   0.746   0.647