Audit a logged policy with importance weights
Implement policy_report(events, max_weight=None), returning ordinary IPS, self-normalized IPS, effective sample size, and event count for logged reward observations.
Your task
- Each event is a dict with finite numeric reward, propensity, and target_probability. Require 0 < propensity <= 1 and 0 <= target_probability <= 1.
- Compute weight = target_probability / propensity. If max_weight is supplied, require a finite positive value and cap each weight.
- Return {"ips": weighted_reward_sum / n, "snips": weighted_reward_sum / weight_sum, "ess": weight_sum squared / sum of squared weights, "n": n}.
- For empty input return ips=None, snips=None, ess=0.0, n=0. For all-zero weights return ips=0.0, snips=None, ess=0.0.
- Reject malformed or nonfinite numeric values with ValueError. The estimator cannot establish support for unobserved actions; explain that limitation.
Examples
EXAMPLE 1
InputRewards [1, 0], logged probabilities [.5, .5], target probabilities [1, 0]
Output{"ips": 1.0, "snips": 1.0, "ess": 1.0, "n": 2}
Implement the function, then run the tests.
Ctrl / ⌘ + Enter to test · Shift + Tab leaves editor