All problems
Optimal Baseline Control Variate
Problem
For the control variate (R - b) * s with score s, the baseline that minimizes the estimator variance is b* = E[R * s^2] / E[s^2].
Given reward samples and score samples, return b* computed as sum(R_i * s_i^2) / sum(s_i^2). Assume the scores are not all zero.
Hint
Minimize the variance as a quadratic in b and solve for b.
Test cases
case 1
in: [[1,2],[1,1]]
expected: 1.5
case 2
in: [[1,2,3],[1,-1,0]]
expected: 1.5
case 3
in: [[2,4],[0.5,-0.5]]
expected: 3
Starter codePython
def optimal_baseline(rewards, scores):
# Your code here
pass3 test cases · run your code against them in the editor