Items at the top of a list are clicked more because they are seen more, not only because they are better. The examination hypothesis models a click as P(click) = P(examined | position) * P(relevant | item). Training a ranker on raw clicks copies the position bias into the model; weighting each click by the inverse of its examination probability removes it. Implement the estimation and the correction with NumPy.
propensities_from_randomized(positions, clicks)
positions (1-based) and clicks (0/1) record each impression. Estimate P(examined | position) as the click-through rate at each position divided by the click-through rate at position 1. Return the (max_position,) array rounded to 4 decimals; a position with no impressions gets 0.0.expected_clicks(relevance, propensities)
relevance[i] the relevance probability of the item shown at position i + 1, return the expected click probability at every position rounded to 4 decimals.ips_relevance_estimate(item_ids, positions, clicks, propensities, n_items)
0 .. n_items - 1, the unbiased estimate of its relevance is the sum over its impressions of click / propensity[position] divided by the number of its impressions (0.0 for an item never shown). Return the (n_items,) array rounded to 4 decimals.ips_pointwise_loss(scores, positions, clicks, propensities)
scores are the ranker's logits for the impressions. Return (naive, ips): the naive loss is the mean over all impressions of the binary cross-entropy between sigmoid(score) and the click; the IPS loss is the mean over all impressions of click * (-log sigmoid(score)) / propensity[position], which weights only the clicked impressions. Both rounded to 4 decimals.position - 1.-log(sigmoid(s)) = log(1 + exp(-s)); use np.logaddexp(0, -s) for stability.