Last active
January 15, 2021 02:59
-
-
Save WhatIThinkAbout/a45001ff327f1b15bbccc3cc0f7653b5 to your computer and use it in GitHub Desktop.
Thompson sampling on a Bernoulli probabilistic power socket.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BernoulliThompsonSocket( PowerSocket ): | |
def __init__( self, q ): | |
self.α = 1 # the number of times this socket returned a charge | |
self.β = 1 # the number of times no charge was returned | |
# pass the true reward value to the base PowerSocket | |
super().__init__(q) | |
def charge(self): | |
""" return some charge with the socket's predefined probability """ | |
return np.random.random() < self.q | |
def update(self,R): | |
""" increase the number of times this socket has been used and | |
update the counts of the number of times the socket has and | |
has not returned a charge (alpha and beta)""" | |
self.n += 1 | |
self.α += R | |
self.β += (1-R) | |
def sample(self): | |
""" return a value sampled from the beta distribution """ | |
return np.random.beta(self.α,self.β) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment