def Main(): # make a redditor with some trustworthiness (mean_t = 0.67) founder = Redditor(name='redditor') beta = thinkbayes.Beta(2, 1) for val, prob in beta.MakePmf().Items(): founder.Set(val * 100, prob) # make a new item with unknown quality (mean_q = 0.5) item = Item(range(0, 101), name='item') # compute the means mean_t = founder.Mean() / 100.0 mean_q = item.Mean() / 100.0 print mean_t print mean_q # perform simultaneous updates founder.Update(('up', mean_q)) item.Update(('up', mean_t)) Summarize(item) # display the posterior distributions myplot.Pmf(founder) myplot.Pmf(item) myplot.Show()
def SampleCtr(n=1, preload=20): """Draw a sample from a beta distribution of CTRs. n: sample size preload: number of preloaded "no" returns: sequence of CTR """ beta = thinkbayes.Beta(1, preload + 1) ps = beta.Sample(n) return ps
def MakePosterior(data, preload=20, steps=201, name=""): """Makes the posterior distribution of CTR given the data. Where CTR is "click through rate" data: sequence of values in [0, 1] preload: number of preloaded "no" steps: number of values to put in the posterior dist name: name to give the pmf returns: Pmf of CTR """ yes, no = data pmf = thinkbayes.Beta(yes + 1, no + 1).MakePmf(steps, name) return pmf
# plot the posteriors PlotSuites([suite1], 'euro1') PlotSuites([suite1, suite2], 'euro3') if __name__ == '__main__': main() # Beta Distribution # # This approach is another optimization for the same problem. Becaus the beta distribution is # defined on an interval between 0 and 1 (inclusive). The beta distribution is a conjugate prior, # meaning that if the prior distribution for x is a beta distribution, then the posterior is also # a beta distribution. # the ThinkBayes.py file has a Beta class. So we will just implement it here. beta = thinkbayes.Beta() beta.Update((140, 110)) print(beta.Mean()) ## Odds and Addends ## # 'Odds in favor' of an event are the ratio of the probability it will occur to the probability # that it will not. import random FORMATS = ['pdf', 'eps', 'png'] class Die(thinkbayes.Pmf): def __init__(self, sides, name=''):
def __init__(self): thinkbayes.Suite.__init__(self) beta = thinkbayes.Beta(1, 1) for val, prob in beta.MakePmf().Items(): self.Set(val, prob)