import numpy as np
from scipy.stats import beta, binom
import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import beta, binom
import matplotlib.pyplot as plt
class Coin():
def __init__(self):
self.a = 1
self.b = 1
def draw(self):
return beta(self.a, self.b).rvs(1)
def update(self, flip):
if flip>0:
self.a+=1
else:
self.b+=1
def __str__(self):
return f"{self.a}:{self.b}={self.a/(self.a+self.b):.3f}"
#Unknown to us
np.random.seed(19920908)
coin1 = binom(p=0.4, n=1)
coin2 = binom(p=0.6, n=1)
model1 = Coin()
model2 = Coin()
for i in range(100):
draw1 = model1.draw()
draw2 = model2.draw()
if draw1>draw2:
flip = coin1.rvs()
model1.update(flip)
else:
flip = coin2.rvs()
model2.update(flip)
x = np.linspace(0,1,101)
plt.plot(x, beta(model1.a, model1.b).pdf(x))
plt.plot(x, beta(model2.a, model2.b).pdf(x))
print(model1,model2)
Expose the internals a bit to show the numerical difference in the sampling