A weighted sum, and how it breaks
D15 is a passage added to the handbook for this example only. The toy embedder knows none of its words, so its vector score is 0.
The obvious way to combine BM25 and cosine is to add the scores. They live on different scales, so you must normalise first, and even then the weights you tuned stop working when the data changes.
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.
Write mix(keyword, vector, alpha) that takes two lists of scores already scaled to 0..1 and returns the list of mixed scores. Print the result for [1.0, 0.0], [0.0, 1.0] and alpha = 0.7.
Write min_max(scores) that returns zeros when all scores are equal. Print it for [2, 4, 6] and for [3, 3].
Explain it without notes
Why can't you simply add a BM25 score and a cosine similarity?
How does min-max normalisation work, and what is one weakness?
Explain how adding a single document can change the order of two unrelated documents in a weighted-sum hybrid.
Why can't a min-max normalised score be used to decide that no passage is good enough?
Expected output
150 dollar fee
alpha 0.5: D12=0.798, D8=0.500, D1=0.455
alpha 0.2: D8=0.800, D1=0.728, D12=0.677
alpha 0.5 + D15: D15=1.000, D12=0.505, D8=0.408
I forgot my login, how do I get back in?
alpha 0.5: D3=0.883, D10=0.572, D1=0.500
alpha 0.2: D3=0.953, D10=0.371, D5=0.338
alpha 0.5 + D15: D3=0.875, D10=0.572, D1=0.500