MCPcopy Create free account
hub / github.com/lazyprogrammer/machine_learning_examples / experiment

Function experiment

ab_testing/bayesian_bandit.py:49–74  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

47
48
49def 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
77if __name__ == "__main__":

Callers 1

bayesian_bandit.pyFile · 0.70

Calls 5

BanditClass · 0.70
plotFunction · 0.70
sampleMethod · 0.45
pullMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected