-
Notifications
You must be signed in to change notification settings - Fork 6.4k
/
Copy pathbayesian_normal.py
84 lines (61 loc) · 2.07 KB
/
bayesian_normal.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# https://deeplearningcourses.com/c/artificial-intelligence-reinforcement-learning-in-python
# https://www.udemy.com/artificial-intelligence-reinforcement-learning-in-python
from __future__ import print_function, division
from builtins import range
# Note: you may need to update your version of future
# sudo pip install -U future
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import norm
np.random.seed(1)
NUM_TRIALS = 2000
BANDIT_MEANS = [1, 2, 3]
class Bandit:
def __init__(self, true_mean):
self.true_mean = true_mean
# parameters for mu - prior is N(0,1)
self.m = 0
self.lambda_ = 1
self.tau = 1
self.N = 0
def pull(self):
return np.random.randn() / np.sqrt(self.tau) + self.true_mean
def sample(self):
return np.random.randn() / np.sqrt(self.lambda_) + self.m
def update(self, x):
self.m = (self.tau * x + self.lambda_ * self.m) / (self.tau + self.lambda_)
self.lambda_ += self.tau
self.N += 1
def plot(bandits, trial):
x = np.linspace(-3, 6, 200)
for b in bandits:
y = norm.pdf(x, b.m, np.sqrt(1. / b.lambda_))
plt.plot(x, y, label=f"real mean: {b.true_mean:.4f}, num plays: {b.N}")
plt.title(f"Bandit distributions after {trial} trials")
plt.legend()
plt.show()
def run_experiment():
bandits = [Bandit(m) for m in BANDIT_MEANS]
sample_points = [5,10,20,50,100,200,500,1000,1500,1999]
rewards = np.empty(NUM_TRIALS)
for i in range(NUM_TRIALS):
# Thompson sampling
j = np.argmax([b.sample() for b in bandits])
# plot the posteriors
if i in sample_points:
plot(bandits, i)
# pull the arm for the bandit with the largest sample
x = bandits[j].pull()
# update the distribution for the bandit whose arm we just pulled
bandits[j].update(x)
# update rewards
rewards[i] = x
cumulative_average = np.cumsum(rewards) / (np.arange(NUM_TRIALS) + 1)
# plot moving average ctr
plt.plot(cumulative_average)
for m in BANDIT_MEANS:
plt.plot(np.ones(NUM_TRIALS)*m)
plt.show()
return cumulative_average
if __name__ == '__main__':
run_experiment()