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

Function run_experiment

ab_testing/convergence.py:15–37  ·  view source on GitHub ↗
(p1, p2, p3, N)

Source from the content-addressed store, hash-verified

13
14
15def 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
40run_experiment(0.2, 0.25, 0.3, 100000)

Callers 1

convergence.pyFile · 0.70

Calls 4

BanditClass · 0.90
sampleMethod · 0.45
pullMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected