Weighted Importance Sampling reduces variance of off-policy estimation by normalizing by the sum of importance weights. This trades unbiasedness for lower variance.
The problem, from first principles
Ordinary importance sampling can have infinite variance when the behavior policy is far from the target. Weighted IS divides by the sum of weights: V(s) = Σ(W_i * G_i) / Σ(W_i), which reduces variance but adds bias.
Use the formula: V(s,a) = Σ(W_t * G_t) / Σ(W_t) for all visits.
Constraints
Zero weights are handled gracefully (skip or clip)
Return dict of (state, action) -> value
Gamma discount factor
All episodes are valid sequences
Hints
Hint 1: Accumulate weighted sums
Keep sum of weights and sum of weighted returns separately.
Hint 2: Compute importance weight
W_t = product of π(a|s) / β(a|s) along path from first visit.
Hint 3: Divide at the end
V(s,a) = (sum of weighted returns) / (sum of weights) for each (s,a).
Keywords
rlmonte-carlooff-policyimportance-weighting
Python
Run your code to see results
No results yet
Run your code to check it against the examples, or submit to run every test.