#112Clipped Inverse Propensity WeightingMedium

Clipped Inverse Propensity Weighting

Background

Click-through training data is biased by where each impression appeared: a slot-1 impression has a much higher prior probability of being clicked than a slot-10 impression. Inverse propensity weighting (IPW) corrects for this by weighting each example by 1/pk1/p_k where pkp_k is the prior click probability at the example's slot. Clipped IPW caps the weight at wmaxw_{\max} so a single rare-slot example doesn't dominate the gradient.

Problem statement

Implement clipped_ipw_weights(slot_indices, slot_priors, w_max=5.0):

wi  =  min ⁣(1pslot(i), wmax)w_i \;=\; \min\!\Bigl(\frac{1}{p_{\text{slot}(i)}},\ w_{\max}\Bigr)

Input

  • slot_indices - sequence of int slot positions for each example.
  • slot_priors - sequence of float > 0, slot prior click probability indexed by slot position.
  • w_max - float > 0, clip ceiling (default 55).

Output

  • np.ndarray of weights, same length as slot_indices.

Examples

Input: slot_indices=[0, 1, 9], slot_priors=[1.0, 0.5, 0.05]
       (where slot 0 always clicks, slot 9 rarely)
Output: [1.0, 2.0, 5.0]   # 1/0.05 = 20 clipped to 5

Constraints

  • p>0p > 0 required; ValueError otherwise.
  • Slot index out of bounds raises IndexError.

Notes

  • Why clip. Without clipping, variance grows like 1/p1/p, so a slot-10 example with p=0.1p = 0.1 has 10x the variance of a slot-1 example. Clipping at wmax=5w_{\max} = 5 is the canonical production default.
Python
Loading...

▶ Run executes the 3 visible sample tests below in your browser. Submit runs the full suite — including hidden tests — on the server for an official verdict.

  • Reference example: rare slot clipped at w_max
  • Worked example: slot with prior 0.1 clips to default 5
  • Sample: mixed slots weighted independently, no clip