Back to questions
JuniorTheoryCommon
What does this program print?
import numpy as np
counts = np.array([0, 0])
means = np.array([0.0, 0.0])
for arm, reward in [(0, 1.0), (1, 0.0), (0, 0.0), (0, 1.0)]:
counts[arm] += 1
means[arm] += (reward - means[arm]) / counts[arm]
print(round(means[0], 2), counts[0])0.67 3
- Expecting
0.5 3, two rewards over all four steps: each arm keeps its own count, and arm 0 was pulled 3 times, so its mean is2 / 3, rounded to0.67. - Expecting
2.0 3, the sum of rewards: the update moves the mean by1 / countof the gap to the new reward, someans[0]stays the average, not the total.
- Why does a bandit keep a running mean per arm instead of storing every reward?
- What goes wrong if
counts[arm]is increased after the mean update instead of before it?