(p1, p2, p3, N)
| 13 | |
| 14 | |
| 15 | def run_experiment(p1, p2, p3, N): |
| 16 | bandits = [Bandit(p1), Bandit(p2), Bandit(p3)] |
| 17 | |
| 18 | data = np.empty(N) |
| 19 | |
| 20 | for i in range(N): |
| 21 | # thompson sampling |
| 22 | j = np.argmax([b.sample() for b in bandits]) |
| 23 | x = bandits[j].pull() |
| 24 | bandits[j].update(x) |
| 25 | |
| 26 | # for the plot |
| 27 | data[i] = x |
| 28 | cumulative_average_ctr = np.cumsum(data) / (np.arange(N) + 1) |
| 29 | |
| 30 | # plot moving average ctr |
| 31 | plt.plot(cumulative_average_ctr) |
| 32 | plt.plot(np.ones(N)*p1) |
| 33 | plt.plot(np.ones(N)*p2) |
| 34 | plt.plot(np.ones(N)*p3) |
| 35 | plt.ylim((0,1)) |
| 36 | plt.xscale('log') |
| 37 | plt.show() |
| 38 | |
| 39 | |
| 40 | run_experiment(0.2, 0.25, 0.3, 100000) |
no test coverage detected