def makeUniformPmf(low, high): """Make a uniform Pmf. low: lowest value (inclusive) high: highest value (inclusive) """ pmf = thinkbayes.PMF() for x in makeRange(low=low, high=high): pmf.set(x, 1) pmf.normalize() return pmf
def divideValues(pmf, denom): """Divides the values in a Pmf by denom. Returns a new Pmf. """ new = thinkbayes.PMF() denom = float(denom) for val, prob in pmf.items(): x = val / denom new.set(x, prob) return new
def coefVariation(suite): """Computes the distribution of CV. suite: Pmf that maps (x, y) to z Returns: Pmf object for CV. """ pmf = thinkbayes.PMF() for (m, s), p in suite.items(): pmf.incr(s / m, p) return pmf
def binaryPmf(p): """Makes a Pmf with values 1 and 0. p: probability given to 1 Returns: Pmf object """ pmf = thinkbayes.PMF() pmf.set(1, p) pmf.set(0, 1 - p) return pmf
def pmfMax(pmf1, pmf2): """Computes the distribution of the max of values drawn from two Pmfs. pmf1, pmf2: Pmf objects returns: new Pmf """ res = thinkbayes.PMF() for v1, p1 in pmf1.items(): for v2, p2 in pmf2.items(): res.incr(max(v1, v2), p1 * p2) return res
def makeRawScoreDist(self, efficacies): """Makes the distribution of raw scores for given difficulty. efficacies: Pmf of efficacy """ pmfs = thinkbayes.PMF() for efficacy, prob in efficacies.items(): scores = self.pmfCorrect(efficacy) pmfs.set(scores, prob) mix = thinkbayes.makeMixture(pmfs) return mix
def pmfCorrect(efficacy, difficulties): """Computes the distribution of correct responses. efficacy: personal ability to answer questions difficulties: list of difficulties, one for each question Returns: new Pmf object """ pmf0 = thinkbayes.PMF([0]) ps = [probCorrect(efficacy, difficulty) for difficulty in difficulties] pmfs = [binaryPmf(p) for p in ps] dist = sum(pmfs, pmf0) return dist
def makeGoalTimePmf(suite): """Makes the distribution of time til first goal. suite: distribution of goal-scoring rate returns: Pmf of goals per game """ metaPmf = thinkbayes.PMF() for lam, prob in suite.items(): pmf = thinkbayes.makeExponentialPmf(lam, high=2, n=2001) metaPmf.set(pmf, prob) mix = thinkbayes.makeMixture(metaPmf, name=suite.name) return mix
def reverseScale(self, pmf): """Applies the reverse scale to the values of a PMF. Args: pmf: Pmf object scale: Interpolator object Returns: new Pmf """ new = thinkbayes.PMF() for val, prob in pmf.items(): raw = self.reverse(val) new.incr(raw, prob) return new
def makeGoalPmf(suite, high=10): """Makes the distribution of goals scored, given distribution of lam. suite: distribution of goal-scoring rate high: upper bound returns: Pmf of goals per game """ metaPmf = thinkbayes.PMF() for lam, prob in suite.items(): pmf = thinkbayes.makePoissonPmf(lam, high) metaPmf.set(pmf, prob) mix = thinkbayes.makeMixture(metaPmf, name=suite.name) return mix
def pmfOfWaitTime(pmf_zb): """Distribution of wait time. pmf_zb: dist of gap time as seen by a random observer (the biased distribution) Returns: dist of wait time (also dist of elapsed time) """ # NOTE: zb = Y + X, where Y = dist of wait time, and X = dist of elapsed time. # NOTE: Y = wait times = time between arrival of passenger and arrival of train. # NOTE: X = wait time = time between arrival of previous train and arrival of next passenger. metaPmf = thinkbayes.PMF() for gap, prob in pmf_zb.items(): uniform = makeUniformPmf(0, gap) metaPmf.set(uniform, prob) pmf_y = thinkbayes.makeMixture(metaPmf, name='y') return pmf_y
def __init__(self, wtc, are, num_passengers=15): """Constructor. wtc: WaitTimeCalculator are: ArrivalTimeEstimator num_passengers: number of passengers seen on the platform """ self.metaPmf = thinkbayes.PMF() for lmbda, prob in sorted(are.posteriorLambda.items()): ete = ElapsedTimeEstimator(wtc, lmbda, num_passengers) self.metaPmf.set(ete.pmf_y, prob) self.mixture = thinkbayes.makeMixture(self.metaPmf) lmbda = are.posteriorLambda.mean() ete = ElapsedTimeEstimator(wtc, lmbda, num_passengers) self.point = ete.pmf_y
def makeLocationPmf(alpha, beta, locations): """Computes the Pmf of the locations, given alpha and beta. Given that the shooter is at coordinates (alpha, beta), the probability of hitting any spot is inversely proportionate to the strafe speed. alpha: x position beta: y position locations: x locations where the pmf is evaluated Returns: Pmf object """ pmf = thinkbayes.PMF() for x in locations: # NOTE: prob of hitting any location on the wall is inversely related to strafing speed prob = 1.0 / strafingSpeed(alpha, beta, x) pmf.set(x, prob) pmf.normalize() return pmf
def main(): pmfDice = thinkbayes.PMF() pmfDice.set(Die(4), 5) pmfDice.set(Die(6), 4) pmfDice.set(Die(8), 3) pmfDice.set(Die(12), 2) pmfDice.set(Die(20), 1) pmfDice.normalize() #@fix: was unhashable error here: # http://stackoverflow.com/questions/10994229/how-to-make-an-object-properly-hashable # http://stackoverflow.com/questions/2909106/python-whats-a-correct-and-good-way-to-implement-hash mix = thinkbayes.PMF() for die, weight in pmfDice.items(): for outcome, prob in die.items(): mix.incr(outcome, weight * prob) mix = thinkbayes.makeMixture(pmfDice) colors = thinkplot.Brewer.getColors() thinkplot.hist(mix, width=0.9, color=colors[4]) thinkplot.save(root='dungeons3', xlabel='Outcome', ylabel='Probability', formats=FORMATS) random.seed(17) d6 = Die(6, 'd6') # finding distribution of rolled-dice sum by SIMULATION dice = [d6] * 3 three = thinkbayes.sampleSum(dice, 1000) three.name = 'sample' print("\n\nSAMPLING: ") three.printSuite() # finding distribution of rolled-dice sum by ENUMERATION threeExact = d6 + d6 + d6 threeExact.name = 'exact' print("\n\nENUMERATION:") threeExact.printSuite() thinkplot.prePlot(num=2) thinkplot.pmf(three) thinkplot.pmf(threeExact, linestyle='dashed') thinkplot.save(root='dungeons1', xlabel='Sum of three d6', ylabel='Probability', axis=[2, 19, 0, 0.15], formats=FORMATS) thinkplot.clf() thinkplot.prePlot(num=1) # Note: pmf of max (best) attribute: bestAttribute2 = pmfMax(threeExact, threeExact) bestAttribute4 = pmfMax(bestAttribute2, bestAttribute2) bestAttribute6 = pmfMax(bestAttribute4, bestAttribute2) thinkplot.pmf(bestAttribute6) # Note: finding pmf max using efficient Cdf method: bestAttributeCdf = threeExact.max(6) #@ Max() in class Cdf bestAttributeCdf.name = '' bestAttributePmf = thinkbayes.makePmfFromCdf(bestAttributeCdf) bestAttributePmf.printSuite() thinkplot.pmf(bestAttributePmf) thinkplot.save(root='dungeons2', xlabel='Sum of three d6', ylabel='Probability', axis=[2, 19, 0, 0.23], formats=FORMATS)