Skip to main content
Expose the internals a bit to show the numerical difference in the sampling
Source Link
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)

enter image description hereenter image description here

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


#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))

enter image description here

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)

enter image description here

added 158 characters in body
Source Link
Demetri Pananos
  • 39.6k
  • 2
  • 64
  • 151
import numpy as np
from scipy.stats import beta, binom
import matplotlib.pyplot as plt

%matplotlib inline

coin1 = binom(pimport =numpy 0.4,as n=1)np
coin2 =from binom(pscipy.stats =import 0.6beta, n=1)

binom
Aa =import 1
Abmatplotlib.pyplot =as 1plt
Ba = 1
Bb =class 1Coin():
n_flips = 100

A = beta(Aa, Ab)
B = beta(Ba, Bb)

for i indef range__init__(100self):
    
     draw_Aself.a = A.rvs()1
    draw_B = B.rvs()
  self.b = 1
    ifdef draw_A>draw_Bdraw(self):
        flipreturn =beta(self.a, coin1self.b).rvs(1)
       def ifupdate(self, flip>0flip):
           if Aa+=1flip>0:
            A = beta(Aa, Ab)self.a+=1
        else:
            Ba+=1self.b+=1
       

#Unknown to us
np.random.seed(19920908)
coin1 = binom(p=0.4, An=1)
coin2 = betabinom(Aap=0.6, Abn=1) 


model1 = Coin()
model2 = else:Coin()
 
for i in range(100):

    flipdraw1 = coin2model1.rvsdraw()
    draw2 = model2.draw()

    if flip>0draw1>draw2:
          flip = Ba+=1coin1.rvs()
            B = betamodel1.update(Ba, Bbflip)
        else:
          flip = Bb+=1coin2.rvs()
            B = betamodel2.update(Ba, Bbflip) 


        

 
x = np.linspace(0,1, 101)

plt.plot(x, Abeta(model1.a, model1.b).pdf(x))
plt.plot(x,B beta(model2.a, model2.b).pdf(x))

enter image description hereenter image description here

import numpy as np
from scipy.stats import beta, binom
import matplotlib.pyplot as plt

%matplotlib inline

coin1 = binom(p = 0.4, n=1)
coin2 = binom(p = 0.6, n=1)


Aa = 1
Ab = 1
Ba = 1
Bb = 1
n_flips = 100

A = beta(Aa, Ab)
B = beta(Ba, Bb)

for i in range(100):
    
     draw_A = A.rvs()
    draw_B = B.rvs()
    
    if draw_A>draw_B:
        flip = coin1.rvs()
        if flip>0:
            Aa+=1
            A = beta(Aa, Ab)
        else:
            Ba+=1
            A = beta(Aa, Ab)
    else:
        flip = coin2.rvs()
        if flip>0:
            Ba+=1
            B = beta(Ba, Bb)
        else:
            Bb+=1
            B = beta(Ba, Bb)
        

 
x = np.linspace(0,1, 101)

plt.plot(x, A.pdf(x))
plt.plot(x,B.pdf(x))

enter image description here

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


#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))

enter image description here

added 145 characters in body
Source Link
Demetri Pananos
  • 39.6k
  • 2
  • 64
  • 151

EDIT: I'm leaving this up for posterity, but this probably should not be the accepted answer. In the comments I note that assuming the coins have a bias of 0.5 and 0.51, then OP's strategy selects the correct coin 58% of the time. The bandit is (ironically) a coin flip.

Nothing frustrates me more than when someone tells you to do the "optimal" thing without telling you the criteria over which to optimize. That being said, I'm betting that since it was an interview, they intended for you to determine what you wanted to optimize for.

EDIT: I'm leaving this up for posterity, but this probably should not be the accepted answer. In the comments I note that assuming the coins have a bias of 0.5 and 0.51, then OP's strategy selects the correct coin 58% of the time. The bandit is (ironically) a coin flip.

Nothing frustrates me more than when someone tells you to do the "optimal" thing without telling you the criteria over which to optimize. That being said, I'm betting that since it was an interview, they intended for you to determine what you wanted to optimize for.

Nothing frustrates me more than when someone tells you to do the "optimal" thing without telling you the criteria over which to optimize. That being said, I'm betting that since it was an interview, they intended for you to determine what you wanted to optimize for.

added 276 characters in body
Source Link
Demetri Pananos
  • 39.6k
  • 2
  • 64
  • 151
Loading
edited body
Source Link
Demetri Pananos
  • 39.6k
  • 2
  • 64
  • 151
Loading
added 1383 characters in body
Source Link
Demetri Pananos
  • 39.6k
  • 2
  • 64
  • 151
Loading
Source Link
Demetri Pananos
  • 39.6k
  • 2
  • 64
  • 151
Loading