All problems
HardLoss & Trainingv2026-06-29

Loss & Training ยท Hard

IPS Weighting

Implement IPS Weighting with production-minded edge case handling.

Task

Implement ips_weighted_loss(losses: list[float], propensities: list[float], min_propensity: float) -> float. Compute mean inverse-propensity-score weighted loss with propensity clipping.

Requirements

  • losses and propensities have equal length.
  • Every propensity is in [0, 1], and min_propensity is in (0, 1].
  • Clip each propensity from below with max(propensity, min_propensity).
  • Divide each loss by its clipped propensity, then return the arithmetic mean of those corrected losses.
  • Do not normalize by the sum of inverse-propensity weights.
  • Return 0.0 when the inputs are empty.
  • Do not mutate either input.

Example

ips_weighted_loss(
    [1.0, 2.0, 3.0],
    [0.5, 1.0, 0.25],
    0.1,
)
# 5.333333333333333
solution.pySign in to save
Public tests run locally in your browser.
Run your code to see public test results.