def MakePosteriorPlot(suite): """Plots the posterior marginal distributions for alpha and beta. suite: posterior joint distribution of location """ marginal_alpha = suite.Marginal(0) marginal_alpha.name = 'alpha' marginal_beta = suite.Marginal(1) marginal_beta.name = 'beta' print('alpha CI', marginal_alpha.CredibleInterval(50)) print('beta CI', marginal_beta.CredibleInterval(50)) thinkplot.PrePlot(num=2) #thinkplot.Pmf(marginal_alpha) #thinkplot.Pmf(marginal_beta) thinkplot.Cdf(thinkbayes.MakeCdfFromPmf(marginal_alpha)) thinkplot.Cdf(thinkbayes.MakeCdfFromPmf(marginal_beta)) thinkplot.Save('paintball2', xlabel='Distance', ylabel='Prob', loc=4, formats=FORMATS)
def PlotCoefVariation(suites): """Plot the posterior distributions for CV. suites: map from label to Pmf of CVs. """ thinkplot.Clf() thinkplot.PrePlot(num=2) pmfs = {} for label, suite in suites.iteritems(): pmf = CoefVariation(suite) print('CV posterior mean', pmf.Mean()) cdf = thinkbayes.MakeCdfFromPmf(pmf, label) thinkplot.Cdf(cdf) pmfs[label] = pmf thinkplot.Save(root='variability_cv', xlabel='Coefficient of variation', ylabel='Probability') print('female bigger', thinkbayes.PmfProbGreater(pmfs['female'], pmfs['male'])) print('male bigger', thinkbayes.PmfProbGreater(pmfs['male'], pmfs['female']))
def PlotPosteriors(self, other): """Plots posterior distributions of efficacy. self, other: Sat objects. """ thinkplot.Clf() thinkplot.PrePlot(num=2) cdf1 = thinkbayes.MakeCdfFromPmf(self, 'posterior %d' % self.score) cdf2 = thinkbayes.MakeCdfFromPmf(other, 'posterior %d' % other.score) thinkplot.Cdfs([cdf1, cdf2]) thinkplot.Save(xlabel='efficacy', ylabel='CDF', axis=[0, 4.6, 0.0, 1.0], root='sat_posteriors_eff', formats=['pdf', 'eps'])
def CalibrateDifficulty(self): """Make a plot showing the model distribution of raw scores.""" thinkplot.Clf() thinkplot.PrePlot(num=2) cdf = thinkbayes.MakeCdfFromPmf(self.raw, name='data') thinkplot.Cdf(cdf) efficacies = thinkbayes.MakeGaussianPmf(0, 1.5, 3) pmf = self.MakeRawScoreDist(efficacies) cdf = thinkbayes.MakeCdfFromPmf(pmf, name='model') thinkplot.Cdf(cdf) thinkplot.Save(root='sat_calibrate', xlabel='raw score', ylabel='CDF', formats=['pdf', 'eps'])
def CH3_4(): """ 不同先验计算后验 Train: 先验概率为 均匀分布uniform Train2: 先验概率为 指数分布power law alpha = 1: +-------------------------------------+ | 先验概率(未归一化) | | 1 1/1 0 | | 2 1/2 0 | | ... ... ... | | 59 1/59 0 | | 60 1/60 1/60 | | 61 1/61 1/61 | | ... ... | | 1000 1/1000 1/1000 | +-------------------------------------+ """ dataset = [60] N = 1000 def _makePosterior(uppernum, constructor, dataset): """ 根据构造器函数和数据集, 生成后验概率suite """ suite = constructor(range(1, uppernum)) for data in dataset: suite.Update(data) return suite thinkplot.Clf() thinkplot.PrePlot(num=2) constructors = [Train, Train2] labels = ['uniform', 'power law'] for constructor, label in zip(constructors, labels): suite = _makePosterior(N, constructor, dataset) suite.name = label thinkplot.Pmf(suite) thinkplot.Show(title='compare priors', xlabel='Number of trains', ylabel='prob') dataset = [60, 30, 90] for constructor, label in zip(constructors, labels): for n in [500, 1000, 2000]: suite = _makePosterior(n, constructor, dataset) # 单点估计 print("%s: n = %d, mu = %.3f" % (label, n, suite.Mean())) # 后验概率的置信区间5% - 95% (see CH3_5) interval = thinkbayes.Percentile(suite, 5), thinkbayes.Percentile(suite, 95) print(interval) # CH3_6, 累计分布函数计算百分位数 if n == 2000: cdf = thinkbayes.MakeCdfFromPmf(suite) interval = cdf.Percentile(5), cdf.Percentile(95) print("MakeCdfFromPmf:", interval)
def PlotMarginals(suite): """Plots marginal distributions from a joint distribution. suite: joint distribution of mu and sigma. """ thinkplot.Clf() pyplot.subplot(1, 2, 1) pmf_m = suite.Marginal(0) cdf_m = thinkbayes.MakeCdfFromPmf(pmf_m) thinkplot.Cdf(cdf_m) pyplot.subplot(1, 2, 2) pmf_s = suite.Marginal(1) cdf_s = thinkbayes.MakeCdfFromPmf(pmf_s) thinkplot.Cdf(cdf_s) thinkplot.Show()
def GenerateSampleGaps(self, n): """Generates a random sample of gaps seen by passengers. n: sample size Returns: sequence of values """ cdf_zb = thinkbayes.MakeCdfFromPmf(self.pmf_zb) sample = cdf_zb.Sample(n) return sample
def GenerateSampleWaitTimes(self, n): """Generates a random sample of wait times. n: sample size Returns: sequence of values """ cdf_y = thinkbayes.MakeCdfFromPmf(self.pmf_y) sample = cdf_y.Sample(n) return sample
def PlotPriorDist(pmf): """Plot the prior distribution of p_correct. pmf: prior """ thinkplot.Clf() thinkplot.PrePlot(num=1) cdf1 = thinkbayes.MakeCdfFromPmf(pmf, 'prior') thinkplot.Cdf(cdf1) thinkplot.Save(root='sat_prior', xlabel='p_correct', ylabel='CDF', formats=['pdf', 'eps'])
def main(script, *args): #random.seed(22) lam1 = 1 lam2 = 1.5 n = 300 t = MakeSeries(n, lam1, n, lam2) series = Series(t) low, high = 0.01, 5.01 for cons in [Split2, Split3]: print cons.__name__ split = cons(series.n, low, high) split.Update(series) cdf = thinkbayes.MakeCdfFromPmf(split) cdf.name = cons.__name__ myplot.Cdf(cdf) myplot.Show()
def main(): ComparePriors() dataset = [30, 60, 90] thinkplot.Clf() thinkplot.PrePlot(num=3) for high in [500, 1000, 2000]: suite = MakePosterior(high, dataset, Train2) print(high, suite.Mean()) thinkplot.Save(root='train3', xlabel='Number of trains', ylabel='Probability') interval = Percentile(suite, 5), Percentile(suite, 95) print(interval) cdf = thinkbayes.MakeCdfFromPmf(suite) interval = cdf.Percentile(5), cdf.Percentile(95) print(interval)
def addTeam( self, team, this_team, rest_of_league ): this_x_cdf = tb.MakeCdfFromPmf( tb.MakePmfFromList( this_team.X ) ) this_y_cdf = tb.MakeCdfFromPmf( tb.MakePmfFromList( this_team.Y ) ) other_x_cdf = tb.MakeCdfFromPmf( tb.MakePmfFromList( rest_of_league.X ) ) other_y_cdf = tb.MakeCdfFromPmf( tb.MakePmfFromList( rest_of_league.Y ) ) self.addCDFs( team, this_x_cdf, this_y_cdf, other_x_cdf, other_y_cdf )