== Listing 9.1: the synthetic world ==
users=800, items=500, latent dimension=6
logged interactions=17068, density=0.0427, mean interactions per user=21.3
mean click-if-examined probability rho=0.2203
Gini of interaction counts over items=0.317; top 10% of items hold 0.260 of interactions
corr(log popularity, mean rho of item)=0.488
examination probabilities theta_k at k=1,2,5,10,20: 1.000, 0.574, 0.276, 0.158, 0.091
train interactions=13637, held-out interactions=3431

== Listing 9.2: ranking metrics with bootstrap intervals ==
k=10; mean over users with a held-out item; 95% percentile bootstrap over users
random                     recall 0.0210 [0.0156, 0.0270]  nDCG 0.0142 [0.0108, 0.0177]  MAP 0.0055 [0.0038, 0.0073]
popularity (train counts)  recall 0.1072 [0.0944, 0.1189]  nDCG 0.0957 [0.0833, 0.1077]  MAP 0.0524 [0.0446, 0.0609]
oracle ranking by rho      recall 0.1172 [0.1049, 0.1295]  nDCG 0.0859 [0.0771, 0.0950]  MAP 0.0397 [0.0341, 0.0457]
oracle ranking by rho*e    recall 0.1939 [0.1782, 0.2100]  nDCG 0.1656 [0.1514, 0.1792]  MAP 0.0905 [0.0814, 0.1003]

== Listing 9.3: alternating least squares ==
objective after half-steps 0..4: 41943, 40747, 39930, 36630, 34964
objective after the last half-step: 33549
largest change over 20 half-steps: -2.895 (every half-step decreases the objective: True)
recall ALS 0.1597 [0.1453, 0.1746]   difference to popularity 0.0525 [0.0404, 0.0663]
ndcg   ALS 0.1368 [0.1250, 0.1489]   difference to popularity 0.0411 [0.0321, 0.0506]
map    ALS 0.0725 [0.0647, 0.0817]   difference to popularity 0.0202 [0.0138, 0.0263]

== Listing 9.4: sampled softmax, gradient bias and the logQ correction ==
exact full-softmax gradient projected on log q: -1.1693
limit of the uncorrected gradient as m grows (softmax of s + log q): error +1.0508
   m | corrected: mean error (std err) | uncorrected: mean error (std err)
   5 | +0.4753 (0.0022)             | +1.1825 (0.0022)
  20 | +0.1909 (0.0016)             | +1.0961 (0.0018)
  80 | +0.0665 (0.0012)             | +1.0643 (0.0006)
 320 | +0.0188 (0.0004)             | +1.0545 (0.0004)

== Listing 9.5: two-tower retrieval ==
k=10; mean over users; 95% bootstrap interval over users
popularity                  recall 0.1072 [0.0937, 0.1207]  nDCG 0.0957 [0.0844, 0.1072]  head share 1.000
ALS                         recall 0.1597 [0.1458, 0.1741]  nDCG 0.1368 [0.1244, 0.1488]  head share 0.779
two-tower, logQ correction  recall 0.1202 [0.1076, 0.1342]  nDCG 0.0998 [0.0901, 0.1103]  head share 0.464
two-tower, no correction    recall 0.0735 [0.0635, 0.0849]  nDCG 0.0549 [0.0476, 0.0625]  head share 0.109

== Listing 9.6: position bias, naive and IPS evaluation ==
queries per log=3000, candidates per query=20, logs=200; true value of the ranker = 2.5129 expected clicks per query
estimator                 mean     bias     std
naive                     0.8851  -1.6277  0.0107
IPS                       2.5145  +0.0016  0.0392
DM (popularity-only model)  2.1933  -0.3196  0.0247
DM (richer model)         2.4875  -0.0254  0.0269
IPS, eta_hat=0.5          2.2642  -0.2487  0.0302
IPS, eta_hat=1.2          3.1944  +0.6816  0.0664
largest |DR - IPS| over logs, with the popularity-only model: 8.88e-16

== Listing 9.7: the variance cost of IPS ==
3000 queries, logged ranking held fixed, clicks redrawn 400 times
ranker                          true value  max weight  ESS share  predicted std  empirical std
close to logging (popularity)      2.2078       10.54      0.722         0.0289         0.0284
ALS scores                         2.6587       10.99      0.584         0.0396         0.0390
oracle (true rho)                  3.0696       10.99      0.462         0.0537         0.0567

== Listing 9.8: off-policy evaluation of a carousel slot ==
K=25 featured items, n=3000 logged impressions, 150 logs
true value of target policy = 0.5797; value of logging policy = 0.2626; mean effective sample size = 528
estimator             mean     bias      std     rmse
DM                   0.5314  -0.0484   0.0122   0.0499
IPS                  0.5813  +0.0015   0.0360   0.0360
SNIPS                0.5817  +0.0019   0.0218   0.0219
DR                   0.5791  -0.0006   0.0179   0.0179
IPS clipped at 10    0.5411  -0.0387   0.0304   0.0492
DR clipped at 10     0.5724  -0.0073   0.0165   0.0181

== Listing 9.9: offline model selection ==
mixing weight lambda (score = lambda*ALS + (1-lambda)*popularity, standardised)
lambda                 0.00   0.25   0.50   0.75   1.00   1.50   2.00
true value            2.208  2.331  2.462  2.579  2.659  2.717  2.723
share picked, naive   0.00   0.00   0.50   0.50   0.00   0.00   0.00
  true value of the picked ranker, averaged over logs: 2.520  (best available 2.723)
share picked, IPS     0.00   0.00   0.00   0.00   0.00   0.24   0.76
  true value of the picked ranker, averaged over logs: 2.722  (best available 2.723)

== Listing 9.10: UCB1 and Thompson sampling ==
arm means [0.55, 0.5, 0.45, 0.4, 0.3], 100 runs, mean cumulative pseudo-regret
     t |    UCB1 (std) | Thompson (std) | Theorem 9.6 bound
   100 |     9.1 ( 1.1) |      7.6 ( 2.4) |     1499.0
  1000 |    64.7 ( 7.5) |     35.1 (15.6) |     2248.1
 10000 |   287.3 (38.4) |     72.4 (26.3) |     2997.3
trivial bound max-gap * t at t=100: 25.0

== Listing 9.11: contextual bandits ==
K=10 arms, d=5, 12 runs; mean cumulative pseudo-regret
policy                      t=500   t=1000   t=4000    std at 4000
random                      74.7    151.3    608.1          6.3
greedy ridge                90.1    168.5    637.6        409.0
LinUCB (alpha 0.5)          32.1     61.1    175.9         80.0
linear Thompson (v 0.3)     39.6     65.7    161.8         40.9

== Listing 9.12: fairness of exposure ==
10 queries x 15 items; protected share of items 0.407; mean predicted relevance: protected 0.0267, others 0.0571
unconstrained optimum: utility 4.068; exposure protected 0.2082, others 0.3307
allowed gap (share of unconstrained gap) | utility share | exposure protected | others
                                    1.00 |        1.0000 |             0.2082 | 0.3307
                                    0.75 |        0.9995 |             0.2264 | 0.3182
                                    0.50 |        0.9982 |             0.2446 | 0.3058
                                    0.25 |        0.9933 |             0.2627 | 0.2933
                                    0.00 |        0.9836 |             0.2809 | 0.2809
query 4: Birkhoff-von Neumann decomposition uses 2 permutations (weights sum to 1.000000)
max |sampled exposure - LP exposure| over items, 20000 sampled rankings: 0.0003

== Listing 9.13: a feedback loop ==
500 users, 300 items, 10 slots, history of 7486 interactions (Gini of history counts 0.306)
reference, ranking by true rho: Gini of impressions 0.634, coverage 0.723
variant   round | Gini of impressions | coverage | utility / oracle | clicks per user
greedy       0 |               0.889 |    0.267 |            0.744 |           2.488
greedy       1 |               0.900 |    0.220 |            0.755 |           2.466
greedy       4 |               0.905 |    0.190 |            0.767 |           2.688
greedy       9 |               0.904 |    0.170 |            0.775 |           2.658
greedy      19 |               0.903 |    0.160 |            0.780 |           2.546
explore      0 |               0.739 |    0.980 |            0.641 |           2.158
explore      1 |               0.755 |    0.973 |            0.647 |           2.242
explore      4 |               0.765 |    0.957 |            0.664 |           2.228
explore      9 |               0.764 |    0.980 |            0.676 |           2.364
explore     19 |               0.761 |    0.987 |            0.686 |           2.314
ips          0 |               0.889 |    0.267 |            0.744 |           2.488
ips          1 |               0.896 |    0.203 |            0.766 |           2.472
ips          4 |               0.892 |    0.170 |            0.787 |           2.744
ips          9 |               0.889 |    0.163 |            0.799 |           2.756
ips         19 |               0.887 |    0.163 |            0.799 |           2.628

== Listing 9.14: optimising a proxy ==
800 users, 500 items; mean P(satisfied | click) = 0.669
ranker                            clicks  satisfied clicks  share of slots on clickbait
by click probability              3.513             1.349                        0.648
by satisfied-click probability    3.234             2.780                        0.007
by rho * sqrt(g) (blend)          3.330             2.751                        0.032
