Beispiel #1
0
    def MakeRawScoreDist(self, efficacies):
        """Makes the distribution of raw scores for given difficulty.

        efficacies: Pmf of efficacy
        """
        pmfs = thinkbayes2.Pmf()
        for efficacy, prob in efficacies.Items():
            scores = self.PmfCorrect(efficacy)
            pmfs.Set(scores, prob)

        mix = thinkbayes2.MakeMixture(pmfs)
        return mix
Beispiel #2
0
def PmfOfWaitTime(pmf_zb):
    """Distribution of wait time.

    pmf_zb: dist of gap time as seen by a random observer

    Returns: dist of wait time (also dist of elapsed time)
    """
    metapmf = thinkbayes2.Pmf()
    for gap, prob in pmf_zb.Items():
        uniform = MakeUniformPmf(0, gap)
        metapmf.Set(uniform, prob)

    pmf_y = thinkbayes2.MakeMixture(metapmf, label='y')
    return pmf_y
Beispiel #3
0
def MakeGoalTimePmf(suite):
    """Makes the distribution of time til first goal.

    suite: distribution of goal-scoring rate

    returns: Pmf of goals per game
    """
    metapmf = thinkbayes2.Pmf()

    for lam, prob in suite.Items():
        pmf = thinkbayes2.MakeExponentialPmf(lam, high=2, n=2001)
        metapmf.Set(pmf, prob)

    mix = thinkbayes2.MakeMixture(metapmf, label=suite.label)
    return mix
Beispiel #4
0
    def __init__(self, label=None):
        """
            - Upon setting priors, we generate a pmf for each hypo that represents
            the probability that an observed user has not logged in for a
            specified amount of time.
            - This generation of pmfs was initially done in likelihood, but this
            became to computationally expensive to do given the size of our data
            set. It is faster to calculate all pmfs before trying to run any updates.
        """
        # Ensure that the __init__'s of super classes are carried out
        super(Lambda, self).__init__()

        # Initialize container for hypo pmfs
        self.hypPmfs = []

        # Iterate through all 100 hypos. These each represent hours since login
        for hypo in range(1, 101):

            # Set up exponential Pmf for a given lambda value;
            if (hypo != 0):
                interarrival = thinkbayes2.MakeExponentialPmf(1 / hypo,
                                                              high=101)
            for val, prob in interarrival.Items():
                interarrival[val] *= val
            interarrival.Normalize()

            # Make a mixture of uniform distributions of time since last login
            metapmf = thinkbayes2.Pmf()
            for time, prob in interarrival.Items():
                if time == 0:
                    continue
                pmf = thinkbayes2.MakeUniformPmf(0, time, 101)
                metapmf[pmf] = prob

            timesince = thinkbayes2.MakeMixture(metapmf)

            # Make a cdf using the mixture
            cdf = thinkbayes2.Cdf(timesince)

            # Take derivative of cdf to generate its pmf
            xs = numpy.linspace(0, 100, 101)
            ys = [scipy.misc.derivative(cdf.Prob, x) for x in xs]
            items = dict(zip(xs, ys))
            pmf = thinkbayes2.MakePmfFromItems(items)
            pmf.Normalize()

            # Store pmf in object to be called on later in Likelihood
            self.hypPmfs.append(pmf)
Beispiel #5
0
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 = thinkbayes2.Pmf()

    for lam, prob in suite.Items():
        pmf = thinkbayes2.MakePoissonPmf(lam, high)
        metapmf.Set(pmf, prob)

    mix = thinkbayes2.MakeMixture(metapmf, label=suite.label)
    return mix
    def PredRemaining(self, rem_time, score):
        """Plots the predictive distribution for final number of goals.

        rem_time: remaining time in the game in minutes
        score: number of goals already scored
        """
        metapmf = thinkbayes2.Pmf(
        )  #PMF about PMFS. probabilities of pmf values
        for lam, prob in self.Items():  #loop through probabilities of lamdas
            lt = lam * rem_time / 60
            pmf = thinkbayes2.MakePoissonPmf(lt, 20)
            metapmf[pmf] = prob

        mix = thinkbayes2.MakeMixture(metapmf)
        mix += score
        return mix
    def PredRemaining(self, rem_time, score):
        """Plots the predictive distribution for final number of goals.

        rem_time: remaining time in the game in minutes
        score: number of goals already scored
        """
        metapmf = thinkbayes2.Pmf()
        for lam, prob in self.Items():
            lt = lam * rem_time / 90
            pred = thinkbayes2.MakePoissonPmf(lt, 15)
            metapmf[pred] = prob
            #thinkplot.Pdf(pred, color='gray', alpha=0.1, linewidth=0.5)

        mix = thinkbayes2.MakeMixture(metapmf)
        mix += score
        thinkplot.Hist(mix)
        thinkplot.Show()
Beispiel #8
0
    def __init__(self, wtc, are, num_passengers=15):
        """Constructor.

        wtc: WaitTimeCalculator
        are: ArrivalTimeEstimator
        num_passengers: number of passengers seen on the platform
        """
        self.metapmf = thinkbayes2.Pmf()

        for lam, prob in sorted(are.post_lam.Items()):
            ete = ElapsedTimeEstimator(wtc, lam, num_passengers)
            self.metapmf.Set(ete.pmf_y, prob)

        self.mixture = thinkbayes2.MakeMixture(self.metapmf)

        lam = are.post_lam.Mean()
        ete = ElapsedTimeEstimator(wtc, lam, num_passengers)
        self.point = ete.pmf_y
    def PredRemaining(self, rem_time, score):
        """Plots the predictive distribution for final number of goals.

        rem_time: remaining time in the game in minutes
        score: number of goals already scored
        """
        metapmf = thinkbayes2.Pmf(
        )  #PMF about PMFS. probabilities of pmf values
        for lam, prob in self.Items():  #loop through probabilities of lamdas
            #print(lam,prob)
            lt = lam * rem_time / 60
            pmf = thinkbayes2.MakePoissonPmf(lt, 20)
            #thinkplot.Pdf(pmf,linewidth=1,alpha=0.2,color='purple')
            metapmf[pmf] = prob

        mix = thinkbayes2.MakeMixture(metapmf)
        mix += score  #shift by 2 because we've already seen 2
        return mix
    def PredRemaining(self, rem_time, points_scored):
        """Plots the predictive distribution for final number of goals.

        rem_time: remaining time in the game in minutes
        points_scored: points already scored
        """
        scorePredict = self.score.PredRemaining(rem_time, 0)
        scorePmf = thinkbayes2.Pmf()
        for prob_td, prob_p in self.TDPercent.Items():
            tdProbPmf = thinkbayes2.Pmf()
            for scores, prob_s in scorePredict.Items():
                for num_tds in range(scores + 1):
                    num_fgs = scores - num_tds
                    points = 7 * num_tds + 3 * num_fgs
                    ncr = thinkbayes2.BinomialCoef(scores, num_tds)
                    tdProbPmf.Incr(
                        points,
                        prob_s * ncr * (prob_td**num_tds *
                                        (1 - prob_td)**num_fgs))
            scorePmf.Incr(tdProbPmf, prob_p)

        mix = thinkbayes2.MakeMixture(scorePmf)
        mix += points_scored
        return mix
def main():
    pmf_dice = thinkbayes2.Pmf()
    pmf_dice.Set(Die(4), 5)
    pmf_dice.Set(Die(6), 4)
    pmf_dice.Set(Die(8), 3)
    pmf_dice.Set(Die(12), 2)
    pmf_dice.Set(Die(20), 1)
    pmf_dice.Normalize()

    mix = thinkbayes2.Pmf()
    for die, weight in pmf_dice.Items():
        for outcome, prob in die.Items():
            mix.Incr(outcome, weight * prob)

    mix = thinkbayes2.MakeMixture(pmf_dice)

    thinkplot.Hist(mix, width=0.9)
    thinkplot.Save(root='dungeons3',
                   xlabel='Outcome',
                   ylabel='Probability',
                   formats=FORMATS)

    random.seed(17)

    d6 = Die(6, 'd6')

    dice = [d6] * 3
    three = thinkbayes2.SampleSum(dice, 1000)
    three.label = 'sample'
    three.Print()

    three_exact = d6 + d6 + d6
    three_exact.label = 'exact'
    three_exact.Print()

    thinkplot.PrePlot(num=2)
    thinkplot.Pmf(three)
    thinkplot.Pmf(three_exact, 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)

    # compute the distribution of the best attribute the hard way
    best_attr2 = PmfMax(three_exact, three_exact)
    best_attr4 = PmfMax(best_attr2, best_attr2)
    best_attr6 = PmfMax(best_attr4, best_attr2)
    # thinkplot.Pmf(best_attr6)

    # and the easy way
    best_attr_cdf = three_exact.Max(6)
    best_attr_cdf.label = ''
    best_attr_pmf = best_attr_cdf.MakePmf()
    best_attr_pmf.Print()

    thinkplot.Pmf(best_attr_pmf)
    thinkplot.Save(root='dungeons2',
                   xlabel='Sum of three d6',
                   ylabel='Probability',
                   axis=[2, 19, 0, 0.23],
                   formats=FORMATS,
                   legend=False)