RecSys · textbook
Trainer Revision About All chapters

The catalogue

Widgets: what you can turn by hand

26 interactive widgets. Each answers a concrete interview question — not «show me something pretty» but «turn the knob and see why it is so». Every number in the captions comes from running the widgets themselves, and the formulas are checked against an independent implementation.

Metrics and statistics

The things you are asked to compute and explain in almost every interview.

The drill: compute the metric

the trainer

Sixteen types of task, a check of the answer with an explanation, statistics by type that finish off your weak spots, and a session mode of 10 tasks.

PrecisionRecallHitRateMRRMAPNDCGAUCF1accuracyFPRconfusion matrixsmoothed CTRIPSBloom filtercollisions

Ranking metrics on one output

chapter 3

Rearrange the positives: precision and recall will not budge, while RR, AP and NDCG grow. That is the whole difference between metrics of coverage and metrics of order.

Precision@KRecall@KHitRateRRAPNDCGDCGIDCGgaingraded relevance

Scores, the threshold and AUC

the trainer

The threshold does not affect the AUC at all, imbalance barely affects it — while AP collapses from 0.89 to 0.34 because of it.

ROC-AUCPR-AUCAPthresholdTPRFPRclass imbalanceshare of correctly ordered pairs

Calibration: why a good AUC is not enough

the trainer

Any monotone transformation of the score leaves the AUC exactly the same while breaking the calibration. Where a prediction is multiplied by money, an AUC is not enough.

calibrationreliability diagramECELogLossBrierPlatt scalingisotonic regressionmonotone transformation

What to average over: micro against macro

chapter 3

Newcomers are half the headcount and three percent of the queries. A degradation on them is visible only when averaging over users.

averagingmicromacrocohortsnewcomersbias towards the active

Off-policy evaluation: IPS, SNIPS, DM, DR

the trainer

IPS is unbiased, but under weak overlap of the policies the ESS falls from 400 to 25 and the estimate becomes useless. Clipping has an optimum by RMSE.

IPSSNIPSDoubly RobustDirect MethodpropensityESSweight clippingoff-policy evaluationlogging policy

A/B: sample size, MDE and peeking

chapter 4

The MDE falls as the square root of n: an effect twice as small costs four times as much data. Daily peeking turns 5% of false positives into 22%.

A/B testMDEsample sizepowerp-valuepeekingmultiple testingalpha spending

Recall@K against latency

the trainer

Candidate generators are compared not at an equal K but at an equal latency budget — otherwise different engineering solutions are being compared.

Recall@Kcandidate generationlatencyPareto frontbudget

The softmax temperature

chapter 7

With a cosine the logits are squeezed into [-1;1], and without a temperature the softmax is almost uniform while the gradients are weak.

temperaturesoftmaxentropyhard negativescosinelogits

In-batch negatives and the LogQ correction

chapter 7

Real training of two models on one stream of batches: without a correction the score converges to log p − log Q and sinks the popular, with it to log p.

LogQ correctionin-batch negativessampled softmaxpopularity biassampling distribution

Bandits: ε-greedy, UCB, Thompson

the trainer

For ε-greedy the regret is linear because of the fixed share of random traffic, for UCB and Thompson it is sublinear.

banditsexplorationregretUCBThompson samplingε-greedyconfidence interval

The mechanisms of systems

How the systems themselves are built: from the long tail to semantic IDs.

The long tail and Zipf’s law

chapter 1

At α = 1.5 the top 4% of the catalogue collects 93% of the views. Under a weak skew recommendations are not needed, under a strong one they degenerate into popularity.

long tailZipf’s lawpower lawGinicoveragehead and tail

The multi-stage funnel

chapter 1

End-to-end recall is the product of the stages’ recalls. What is lost at candidate generation, no ranker will bring back.

funnelmulti-stage designend-to-end recallthe candidate ceilinglatency budget

Matrix factorisation and ALS

chapter 6

The RMSE on the held-out set has its minimum exactly at the true rank. A cold user gets zero factors and a prediction equal to the average rating.

matrix factorisationALSlatent factorsrankregularisationoverfittingcold startfold-in

Dot product, cosine, Euclidean distance

chapter 5

The «dot or cosine» argument is an argument about whether the norm of the vector is needed. On normalised vectors all three measures give one order.

cosinedot productEuclidean distancenorm of a vectorpopularity biasMIPSnormalisation

Folding: groups laid on top of each other

chapter 7

Two disjoint groups and two-dimensional embeddings. With negatives from impressions the groups overlap and half of the catalogue-wide top-5 belongs to the other group; with negatives from the catalogue it is zero. Inside its own group the «impression» model is even better, while over the catalogue it loses exactly half of the HitRate.

foldingimpression-awarenegativescandidate generationtwo-towerBPRmissing datadistribution shift

Approximate nearest-neighbour search

chapter 9

Recall grows from 0.10 to 1.00 as efSearch goes from 1 to 20, while the number of visited nodes goes from 25 to 68. Approximateness is a tunable parameter, not a failure.

ANNHNSWefSearchapproximate searchRecall@Kgraph traversallatency

MMR: relevance against diversity

chapter 15

The first percent of diversity costs 7% of relevance, the last 21%. The DPP tab shows the same through the determinant of the kernel: a set volume of 1.99e-2 against 4.15e-4 for the greedy top.

MMRdiversityDPPintra-list diversityre-ranking

A bandit over the ranker

chapter 16

A catalogue of 40 items with a new item of the best CTR hidden in it. Greedy sorting never finds it; sorting by μ + α·σ finds it and gives +9% to the output, while too large an α spoils everything again. The premature abandonment of an item by a deterministic UCB is visible too.

explorationbanditsUCBThompson samplingcold startuncertaintyepistemic uncertaintypropensitiesre-ranking

Where to take negatives from

chapter 7

The source of the negatives sets the model’s implicit prior: in-batch gives a 1.75× skew towards the popular, pure hard ones drag in false negatives.

negativesin-batch negativeshard negativesfalse negativesuniform samplinga mixture of negatives

Hashing categories and collisions

chapter 8

It is not any collision that is dangerous but a collision of two frequent values. Several hash functions make the probability of full indistinguishability a product.

hashingthe hashing trickcollisionsembedding tablememoryUnified Embeddingmulti-hash

Piecewise-linear encoding of a feature

chapter 11

A raw feature gives only a straight line, one-hot gives steps, PLE gives a continuous piecewise-linear function with no loss of resolution inside a bin.

PLEpiecewise linear encodingbinningone-hot over binscontinuous featuresquantiles

The Bloom filter

chapter 18

The errors are one-sided: if it said «not seen», it was definitely not seen. The curve over k is U-shaped, with the optimum at exactly half the bits filled.

Bloom filterfalse positivesfalse negativespaginationdeduplication of the outputthe optimal k

Blending through a PID controller

chapter 18

Without the integral part the controller misses the target by 17.7 pp — at zero error it produces no action and cannot live without a miss.

blendingPIDsteady-state errorovershootquotasthe share of a category

Target attention against averaging

chapter 14

The weights depend on the candidate, so the single relevant event of the history gets a weight of 0.835 instead of a uniform 0.083.

attentiontarget attentionDINBSTTransActmean poolinguser historyattention temperature

Residual quantisation and semantic IDs

chapter 9

Every level encodes the residual of the previous one, close items share a prefix, and a new item gets an ID straight away from its content vector.

semantic IDRQ-VAERQ-KMeansresidual quantisationcodebookgenerative retrievalshared prefix