With implicit feedback we only see which items each user selected, not how much they liked them. Bayesian personalised ranking (BPR) trains a score \(f(u,i)=b_i+\mathbf p_u^{\mathsf T}\mathbf q_i\) so that, for a sampled triple \((u,i,j)\) with \(i\) selected by \(u\) and \(j\) not, \(f(u,i)\) exceeds \(f(u,j)\): the loss is \(-\log\sigma\big(f(u,i)-f(u,j)\big)\), or the hinge \(\max(0,m-f(u,i)+f(u,j))\). Here 150 simulated users selected 10 to 25 of 100 items each, and one selected item per user is held out. The quality is the AUC of the held-out item against the items the user never selected, and the hit rate at 10.
Data are simulated (the book's example is a recipe, not a data set): users and items have three latent factors, and each user draws their selections with probability increasing in affinity plus item popularity. The popularity ranking scores every item by its number of training selections. Negatives are sampled uniformly from the items the user has not selected; the hinge margin is 1.
code/ch22_recommender_systems.py (bpr_data, bpr_train, bpr_eval).