def MakeFigure(): frac1 = 0.8 frac2 = 1 - frac1 xs, ys = RenderPdf(1170, 179) pmf1 = Pmf.MakePmfFromDict(dict(zip(xs, ys)), name='blue') xs, ys = RenderPdf(995, 167) pmf2 = Pmf.MakePmfFromDict(dict(zip(xs, ys)), name='green') myplot.Pmfs( [pmf1, pmf2], root='normal1', xlabel='CLA score', ylabel='PDF', ) pmf1.Normalize(frac1) pmf2.Normalize(frac2) ymax = max(pmf1.MaxLike(), pmf2.MaxLike()) ymax = 0.003 pyplot.clf() threshes = [1200, 1300, 1400, 1500, 1570] for thresh in threshes: myplot.Plot([thresh, thresh], [0, ymax], clf=False, line_options=dict(color='gray', alpha=0.5, linewidth=1)) plot_options = [ dict(color='blue', linewidth=2), dict(color='green', linewidth=2) ] myplot.Pmfs( [pmf1, pmf2], plot_options=plot_options, clf=False, root='normal2', xlabel='CLA score', ylabel='PDF', ) cdf1 = Cdf.MakeCdfFromPmf(pmf1) cdf2 = Cdf.MakeCdfFromPmf(pmf2) for thresh in threshes: p1 = frac1 * (1 - cdf1.Prob(thresh)) p2 = frac2 * (1 - cdf2.Prob(thresh)) den = p1 + p2 rep1 = p1 / den rep2 = p2 / den print thresh, den, rep1, rep2 return myplot.Cdfs([cdf1, cdf2], root='normal2', xlabel='', ylabel='', title='')
def Main(): Ratio() return pmf1 = Pmf.Pmf() for x in range(0, 101): pmf1.Set(x, 1) pmf1.Normalize() pmf2 = TrianglePrior() # plot the priors myplot.Clf() myplot.Pmfs([pmf1, pmf2]) myplot.Save(root='simple_coin_both_prior', title='Biased coin', xlabel='x', ylabel='Probability') RunUpdate(pmf1) RunUpdate(pmf2) # plot the posterior distributions myplot.Clf() myplot.Pmfs([pmf1, pmf2]) myplot.Save(root='simple_coin_both_post', title='Biased coin', xlabel='x', ylabel='Probability')
def ClassSizes(): # start with the actual distribution of class sizes from the book d = { 7: 8, 12: 8, 17: 14, 22: 4, 27: 6, 32: 12, 37: 8, 42: 3, 47: 2, } # form the pmf pmf = Pmf.MakePmfFromDict(d, 'actual') print 'mean', pmf.Mean() print 'var', pmf.Var() # compute the biased pmf biased_pmf = BiasPmf(pmf, 'observed') print 'mean', biased_pmf.Mean() print 'var', biased_pmf.Var() # unbias the biased pmf unbiased_pmf = UnbiasPmf(biased_pmf, 'unbiased') print 'mean', unbiased_pmf.Mean() print 'var', unbiased_pmf.Var() # plot the Pmfs myplot.Pmfs([pmf, biased_pmf, unbiased_pmf]) myplot.Show(xlabel='Class size', ylabel='PMF')
def main(script): # make an exam object with data from the 2010 SAT exam = Exam() # look up Alice's raw score alice = 780 alice_correct = exam.GetRawScore(alice) print 'Alice raw score', alice_correct # display the distribution of raw scores for the population alice_dist = exam.GetPrior() alice_dist.name = 'alice' coin.Update(alice_dist, (53, 1)) #myplot.Pmf(alice_dist, show=True) # look up Alice's raw score bob = 760 bob_correct = exam.GetRawScore(bob) print 'Bob raw score', bob_correct # display the distribution of raw scores for the population bob_dist = exam.GetPrior() bob_dist.name = 'bob' coin.Update(bob_dist, (52, 2)) myplot.Pmfs([alice_dist, bob_dist], show=True) print ProbBigger(alice_dist, bob_dist)
def ClassSizes(): # start with the actual distribution of class sizes from the book d = { 7: 8, 12: 8, 17: 14, 22: 4, 27: 6, 32: 12, 37: 8, 42: 3, 47: 2, } # form the pmf pmf = Pmf.MakePmfFromDict(d, 'actual') print 'mean', pmf.Mean() print 'var', pmf.Var() biased = BiasPmf(pmf, name='biased') print 'mean', biased.Mean() print 'var', biased.Var() # plot the actual pmf myplot.Pmfs([pmf, biased], show=True, xlabel='Class size', ylabel='PMF')
def create_error_pmf(): all_events = cyb_records.Events() all_events.ReadRecords() all_records = all_events.records my_pmfs = Pmf_errors(all_records) myplot.Pmfs(my_pmfs) myplot.Show(title="PDF of different types of errors Per Machine", xlabel='Error Codes', ylabel='Probability')
def PlotPMF(records, machine_filter=[]): pmfs = [] errors = GetErrorsPerWeek(records, machine_filter) for key in errors.keys(): if SumErrors(errors.get(key)) > 10: pmf = Pmf.MakeHistFromDict(errors.get(key), key) pmfs.append(pmf) myplot.Pmfs(pmfs) myplot.Show(title="Histogram: Error Rate per Week", xlabel='Date', ylabel='Errors per week')
def MakeFigures(exam, alice, bob): formats = ['png'] myplot.Pmf(exam.prior, label='prior') myplot.Save(root='sat_prior', formats=formats, xlabel='p', ylabel='PMF') myplot.Clf() myplot.Pmfs([alice, bob]) myplot.Save(root='sat_posterior', formats=formats, xlabel='p', ylabel='PMF')
def main(): distro = {7: 8, 12: 8, 17: 14, 22: 4, 27: 6, 32: 12, 37: 8, 42: 3, 47: 2} pmf = Pmf.MakePmfFromDict(distro, 'actual') biased_pmf = BiasPmf(pmf, 'observed') unbiased_pmf = UnbiasPmf(pmf, 'unbiased') for p in [pmf, biased_pmf, unbiased_pmf]: print p.name, "mean = {:.3f}".format(p.Mean()) print p.name, "var = {:.3f}".format(p.Var()) myplot.Pmfs([pmf, biased_pmf]) myplot.Show(xlabel='Class Size', ylabel='PMF')
def RelativeRisk(first, others, week=38): first_cond = ConditionOnWeeks(first.pmf, week, 'first babies') other_cond = ConditionOnWeeks(others.pmf, week, 'others') risk.ComputeRelativeRisk(first_cond, other_cond) return myplot.Pmfs( [first_cond, other_cond], xlabel='weeks', ylabel=r'Prob{x $=$ weeks | x $\geq$ weeks}', title='Conditional Probability', show=True, )
def main(): all_recs = cyb_records.Events() all_recs.ReadRecords() print 'Number of total stats', len(all_recs.records) pmfs = [] errors = GetErrorsPerWeek(all_recs.records) for key in errors.keys(): if SumErrors(errors.get(key)) > 10: pmf = Pmf.MakeHistFromDict(errors.get(key), key) pmfs.append(pmf) myplot.Pmfs(pmfs) myplot.Show( title="Histogram: Error Rate per Week for All Treadmills | 2014-04-17", xlabel='Date', ylabel='Errors per week')
def main(): prior = MakeUniformSuite(0.0, 1.0, 101) post1 = prior.Copy(name='post1') evidence = 1,1 Update(post1, evidence) print 'P(two heads)', TwoUp(post1) post2 = prior.Copy(name='post2') evidence = 49, 49 Update(post2, evidence) print 'P(two heads)', TwoUp(post2) # plot the posterior distributions myplot.Pmfs([prior, post1, post2], root='kasser', xlabel='P(heads)', ylabel='Probability', show=True)
def main(): print 'pae', 0.3 / (0.3 + 3.0 / 13) doorA = MakeUniformSuite(0.0, 1.0, 101, name='Door A') evidence = 3, 2 Update(doorA, evidence) doorC = MakeUniformSuite(0.0, 1.0, 101, name='Door C') evidence = 3, 10 Update(doorC, evidence) print TotalProbability(doorA, doorC, ProbWinning) # plot the posterior distributions myplot.Pmfs([doorA, doorC]) myplot.Save(root='blinky', formats=['pdf', 'png'], title='Probability of blinking', xlabel='P(blink)', ylabel='Posterior probability')
def main(): all_recs = cyb_records.Events() all_recs.ReadRecords() print 'Number of total stats', len(all_recs.records) ErrorNames = {"0x80100009": "Watchdog", \ "0x80100008": "Approaching over-temp", \ "0x80100005": "Communication issue", \ "0x80100007": "Controller foldback", \ "0x80200062": "Unknown"} pmfs = [] errors = GetErrorsPerWeek(all_recs.records) for key in errors.keys(): if SumErrors(errors.get(key)) > 30: pmf = Pmf.MakeHistFromDict(errors.get(key), ErrorNames.get(key)) pmfs.append(pmf) myplot.Pmfs(pmfs) myplot.Show(title="Histogram: errors rate | 2014-04-08", xlabel='Date', ylabel='Errors per week')
def PlotDiffs(half_diffs, diffs): half_cdf = Cdf.MakeCdfFromList(half_diffs, 'half') cdf = Cdf.MakeCdfFromList(diffs, 'full') options = dict(linewidth=2) myplot.Cdfs([half_cdf, cdf], xlabel='time - qualifying time (min)', ylabel='CDF', plot_options=[options, options], root='marathon_cdf') diffs = [int(x) for x in diffs] half_diffs = [int(x) for x in half_diffs] pmf = Pmf.MakePmfFromList(diffs, 'full') half_pmf = Pmf.MakePmfFromList(half_diffs, 'half') myplot.Pmfs([half_pmf, pmf], xlabel='time - qualifying time (min)', ylabel='PMF', plot_options=[options, options], root='marathon_pmf')