コード例 #1
0
    def makePlot(self, root='redline4'):
        """Makes a plot showing the mixture."""
        thinkplot.clf()

        # plot the MetaPmf
        for pmf, prob in sorted(self.metaPmf.items()):
            cdf = pmf.makeCdf().scale(1.0 / 60)
            width = 2 / math.log(-math.log(prob))
            thinkplot.plot(cdf.xs,
                           cdf.ps,
                           alpha=0.2,
                           linewidth=width,
                           color='blue',
                           label='')

        # plot the mixture and the distribution based on a point estimate
        thinkplot.prePlot(2)
        #thinkplot.Cdf(self.point.MakeCdf(name='point').Scale(1.0/60))
        thinkplot.cdf(self.mixture.makeCdf(name='mix').scale(1.0 / 60))

        thinkplot.save(root=root,
                       xlabel='Wait time (min)',
                       ylabel='CDF',
                       formats=FORMATS,
                       axis=[0, 10, 0, 1])
コード例 #2
0
def plotJointDist(pmf1, pmf2, thresh=0.8):
    """Plot the joint distribution of p_correct.

    pmf1, pmf2: posterior distributions
    thresh: lower bound of the range to be plotted
    """
    def clean(pmf):
        """Removes values below thresh."""
        vals = [val for val in pmf.keys() if val < thresh]
        [pmf.remove(val) for val in vals]

    clean(pmf1)
    clean(pmf2)
    pmf = thinkbayes.makeJoint(pmf1, pmf2)

    thinkplot.figure(figsize=(6, 6))
    thinkplot.contour(pmf, contour=False, pcolor=True)

    thinkplot.plot([thresh, 1.0], [thresh, 1.0],
                   color='gray',
                   alpha=0.2,
                   linewidth=4)

    thinkplot.save(root='sat_4_joint',
                   xlabel='p_correct Alice',
                   ylabel='p_correct Bob',
                   axis=[thresh, 1.0, thresh, 1.0],
                   formats=['pdf'])
コード例 #3
0
def MakeNormalPlot(weights):
    """Generates a normal probability plot of birth weights.

    weights: sequence
    """
    mean, var = thinkbayes2.TrimmedMeanVar(weights, p=0.01)
    std = math.sqrt(var)

    xs = [-5, 5]
    xs, ys = thinkbayes2.FitLine(xs, mean, std)
    thinkplot.plot(xs, ys, color='0.8', label='model')

    xs, ys = thinkbayes2.NormalProbability(weights)
    thinkplot.plot(xs, ys, label='weights')
コード例 #4
0
def MakeNormalModel(weights):
    """Plots a CDF with a Normal model.

    weights: sequence
    """
    cdf = thinkbayes2.Cdf(weights, label='weights')

    mean, var = thinkbayes2.TrimmedMeanVar(weights)
    std = math.sqrt(var)
    print('n, mean, std', len(weights), mean, std)

    xmin = mean - 4 * std
    xmax = mean + 4 * std

    xs, ps = thinkbayes2.RenderNormalCdf(mean, std, xmin, xmax)
    thinkplot.plot(xs, ps, label='model', linewidth=4, color='0.8')
    thinkplot.cdf(cdf)
コード例 #5
0
def plotExpectedGains(guess1=20000, guess2=40000):
    """Plots expected gains as a function of bid.

    guess1: player1's estimate of the price of showcase 1
    guess2: player2's estimate of the price of showcase 2
    """
    player1, player2 = makePlayers()
    makePlots(player1, player2)

    player1.makeBeliefs(guess1)
    player2.makeBeliefs(guess2)

    print('\n\nPlayer 1 prior mle', player1.prior.maximumLikelihood())
    print('Player 2 prior mle', player2.prior.maximumLikelihood())
    print('\nPlayer 1 mean', player1.posterior.mean())
    print('Player 2 mean', player2.posterior.mean())
    print('\nPlayer 1 mle', player1.posterior.maximumLikelihood())
    print('Player 2 mle', player2.posterior.maximumLikelihood())

    player1.plotBeliefs('price3_prior,posterior_player1') # was price3
    player2.plotBeliefs('price4_prior,posterior_player2') # was price4

    calc1 = GainCalculator(player1, player2)
    calc2 = GainCalculator(player2, player1)

    thinkplot.clf()
    thinkplot.prePlot(num=2)

    # NOTE: player 1 optimal bid = 21,000, expgain =  16,700, best guesss = 20,000
    bids, gains = calc1.expectedGains()
    thinkplot.plot(bids, gains, label='Player 1')
    print('\nPlayer 1 optimal bid', max(zip(gains, bids)))

    # NOTE: player 2 optimal bid = 31,500, expgain = 19,400, best guess = 40,000
    bids, gains = calc2.expectedGains()
    thinkplot.plot(bids, gains, label='Player 2')
    print('Player 2 optimal bid', max(zip(gains, bids)))

    thinkplot.save(root='price5_expectedGainsFromBids_player1,2',
                   xlabel='bid ($)',
                   ylabel='expected gain ($)',
                   formats=FORMATS)
コード例 #6
0
def plotOptimalBid():
    """Plots optimal bid vs estimated price.
    """
    player1, player2 = makePlayers()
    guesses = numpy.linspace(15000, 60000, 21) # note we don't know the guesses so we make some up.

    res = []
    for guess in guesses:
        player1.makeBeliefs(guess)

        mean = player1.posterior.mean()
        mle = player1.posterior.maximumLikelihood()

        calc = GainCalculator(player1, player2)
        bids, gains = calc.expectedGains()
        gain, bid = max(zip(gains, bids))

        res.append((guess, mean, mle, gain, bid))

    guesses, means, _mles, gains, bids = zip(*res)

    thinkplot.prePlot(num=3)
    pyplot.plot([15000, 60000], [15000, 60000], color='gray')
    thinkplot.plot(guesses, means, label='mean')
    #thinkplot.Plot(guesses, mles, label='MLE')
    thinkplot.plot(guesses, bids, label='bid')
    thinkplot.plot(guesses, gains, label='gain')
    thinkplot.save(root='price6_mean_bid_gains',
                   xlabel='guessed price ($)',
                   formats=FORMATS)
コード例 #7
0
def runLoop(gap_times, nums, lmbda=0.0333):
    """Runs the basic analysis for a range of num_passengers.

    gap_times: sequence of float
    nums: sequence of values for num_passengers
    lam: arrival rate in passengers per second

    Returns: WaitMixtureEstimator
    """
    global UPPER_BOUND
    UPPER_BOUND = 4000

    thinkplot.clf()

    randomSeed(18)

    # resample gap_times
    n = 220
    cdf_z = thinkbayes.makeCdfFromList(gap_times)
    sample_z = cdf_z.sample(n)
    pmf_z = thinkbayes.makePmfFromList(sample_z)

    # compute the biased pmf and add some long delays
    cdf_zp = biasPmf(pmf_z).makeCdf()
    sample_zb = cdf_zp.sample(n) + [1800, 2400, 3000]

    # smooth the distribution of zb
    pdf_zb = thinkbayes.EstimatedPDF(sample_zb)
    xs = makeRange(low=60)
    pmf_zb = pdf_zb.makePmf(xs)

    # unbias the distribution of zb and make wtc
    pmf_z = unbiasPmf(pmf_zb)
    wtc = WaitTimeCalculator(pmf_z)

    # NOTE: THis is the prob of long wait part on page 89
    # Given number of passengers on platform, problongwait makes an
    # * elapsedtimeestimator
    # * extracts dist of wait time (y)
    # * compute probability that wait time exceeds minutes (15 here)
    # RESULT PLOT: when passgrs num < 20, system isoperating normally so prob of long delay is small
    # But if greater than 30 pssgrs, then it has been 15 mins since last train, which is longer than
    # normal delay so need to take taxi.
    probs = []
    for num_passengers in nums:
        ete = ElapsedTimeEstimator(wtc, lmbda, num_passengers)

        # compute the posterior prob of waiting more than 15 minutes
        cdf_y = ete.pmf_y.makeCdf()
        prob = 1 - cdf_y.prob(900)
        probs.append(prob)

        # thinkplot.Cdf(ete.pmf_y.MakeCdf(name=str(num_passengers)))

    thinkplot.plot(nums, probs)
    thinkplot.save(
        root='redline5',
        xlabel='Num passengers',
        ylabel='P(y > 15 min)',
        formats=FORMATS,
    )