| 47 | |
| 48 | |
| 49 | def experiment(): |
| 50 | bandits = [Bandit(p) for p in BANDIT_PROBABILITIES] |
| 51 | |
| 52 | sample_points = [5,10,20,50,100,200,500,1000,1500,1999] |
| 53 | rewards = np.zeros(NUM_TRIALS) |
| 54 | for i in range(NUM_TRIALS): |
| 55 | # Thompson sampling |
| 56 | j = np.argmax([b.sample() for b in bandits]) |
| 57 | |
| 58 | # plot the posteriors |
| 59 | if i in sample_points: |
| 60 | plot(bandits, i) |
| 61 | |
| 62 | # pull the arm for the bandit with the largest sample |
| 63 | x = bandits[j].pull() |
| 64 | |
| 65 | # update rewards |
| 66 | rewards[i] = x |
| 67 | |
| 68 | # update the distribution for the bandit whose arm we just pulled |
| 69 | bandits[j].update(x) |
| 70 | |
| 71 | # print total reward |
| 72 | print("total reward earned:", rewards.sum()) |
| 73 | print("overall win rate:", rewards.sum() / NUM_TRIALS) |
| 74 | print("num times selected each bandit:", [b.N for b in bandits]) |
| 75 | |
| 76 | |
| 77 | if __name__ == "__main__": |