def main(): import myplot pmf = Pmf.MakePmfFromList([1, 2, 3, 3, 4, 4, 5, 5, 5, 6, 6, 7]) remain = PmfRemainingLifeTime(pmf, age=4) myplot.Hist(pmf) myplot.Show() myplot.Hist(remain) myplot.Show()
def main(): resp = brfss.Respondents() resp.ReadRecords(data_dir='res') d = resp.SummarizeHeight() man_d = d[1] lady_d = d[2] # 男性的mu, var, sigma, 变异系数CV man_mu, man_var = thinkstats.TrimmedMeanVar(man_d) man_sigma = math.sqrt(man_var) man_cv = man_sigma/man_mu print("man: mu = %.3f, var = %.3f, sigma = %.3f, cv = %.3f" % (man_mu, man_var, man_sigma, man_cv)) # 女性的mu, var, sigma, 变异系数CV lady_mu, lady_var = thinkstats.TrimmedMeanVar(lady_d) lady_sigma = math.sqrt(lady_var) lady_cv = lady_sigma/lady_mu print("lady: mu = %.3f, var = %.3f, sigma = %.3f, cv = %.3f" % (lady_mu, lady_var, lady_sigma, lady_cv)) # 男性, 女性Hist分布 man_hist = Pmf.MakeHistFromList(man_d, name='man hist') myplot.Hist(man_hist) myplot.Show() myplot.Clf() lady_hist = Pmf.MakeHistFromList(lady_d, name='lady hist') myplot.Hist(lady_hist) myplot.Show() myplot.Clf() # 男性, 女性Pmf分布 man_pmf = Pmf.MakePmfFromHist(man_hist, name='man pmf') myplot.Pmf(man_pmf) myplot.Show() myplot.Clf() lady_pmf = Pmf.MakePmfFromHist(lady_hist, name='lady pmf') myplot.Pmf(lady_pmf) myplot.Show() myplot.Clf() # 男性/女性Cdf累积分布 man_cdf = Cdf.MakeCdfFromPmf(man_pmf, name='man cdf') lady_cdf = Cdf.MakeCdfFromPmf(lady_pmf, name='lady cdf') myplot.Cdfs((man_cdf, lady_cdf), complement=False, transform=None) myplot.Show()
def main(): results = relay.ReadResults() speeds = relay.GetSpeeds(results) # plot the distribution of actual speeds pmf = Pmf.MakePmfFromList(speeds, 'actual speeds') # myplot.Clf() # myplot.Hist(pmf) # myplot.Save(root='observed_speeds', # title='PMF of running speed', # xlabel='speed (mph)', # ylabel='probability') # plot the biased distribution seen by the observer biased = BiasPmf(pmf, 7.5, name='observed speeds') myplot.Clf() myplot.Hist(biased) myplot.Save(root='observed_speeds', title='PMF of running speed', xlabel='speed (mph)', ylabel='probability') cdf = Cdf.MakeCdfFromPmf(biased) myplot.Clf() myplot.Cdf(cdf) myplot.show(root='observed_speeds_cdf', title='CDF of running speed', xlabel='speed (mph)', ylabel='cumulative probability')
def process(data): # Hist 分布图 hist = Pmf.MakeHistFromList(data, name='hist') myplot.Hist(hist, color='blue') myplot.Show() # Pmf 分布图 pmf = Pmf.MakePmfFromHist(hist, name='pmf') myplot.Pmf(pmf, color='yellow') myplot.Show() myplot.Clf() # 实际数据的CDF分布图 cdf = Cdf.MakeCdfFromList(data, name='loafs') myplot.Cdf(cdf) mu, var = thinkstats.MeanVar(data) sigma = math.sqrt(var) print("mu = %.3f, sigma = %.3f" % (mu, sigma)) # 正态分布 xs = normal_sample(len(data), mu, sigma) # xs = data ys = [erf.NormalCdf(x, mu=mu, sigma=sigma) for x in xs] myplot.Scatter(xs, ys, color='red', label='sample') myplot.Show()
def main(): results = relay.ReadResults() speeds = relay.GetSpeeds(results) pmf = Pmf.MakePmfFromList(speeds, 'actual speeds') observed = BiasPmf(pmf, 7.5, 'observed speeds') myplot.Clf() myplot.Hist(observed) myplot.Show(title='observed speeds', xlabel='speed (mph)', ylabel='probability')
def main(): firsts, others, babies = Babies.PartitionBabies() d = Babies.GetPregnacyList(babies) hist = Pmf.MakeHistFromList(d, name='pregnacylist') myplot.Hist(hist) myplot.Show() g1 = Skewness(d) gp = PearsonSkewness(d) print("g1 = %.3f, gp = %.3f" % (g1, gp)) skewness.show_skewness(l=d, name='pregnancy skewness')
def MakeFigures(pool, firsts, others): """Creates several figures for the book.""" # plot PMFs of birth weights for first babies and others myplot.Clf() myplot.Hist(firsts.weight_pmf, linewidth=0, color='blue') myplot.Hist(others.weight_pmf, linewidth=0, color='orange') myplot.Save(root='nsfg_birthwgt_pmf', title='Birth weight PMF', xlabel='weight (ounces)', ylabel='probability') # plot CDFs of birth weights for first babies and others myplot.Clf() myplot.Cdf(firsts.weight_cdf, linewidth=2, color='blue') myplot.Cdf(others.weight_cdf, linewidth=2, color='orange') myplot.Save(root='nsfg_birthwgt_cdf', title='Birth weight CDF', xlabel='weight (ounces)', ylabel='probability', axis=[0, 200, 0, 1])
def main(): results = relay.ReadResults() speeds = relay.GetSpeeds(results) pmf = Pmf.MakePmfFromList(speeds, 'speeds') pmf = BiasPmf(7,pmf) myplot.Hist(pmf) #myplot.Show(title='PMF of observed speed', # xlabel='speed (mph)', # ylabel='probability') myplot.Save( formats=['png'], root='runner', title='PMF of observed speed', xlabel='speed (mph)', ylabel='probability')
def main(): ran = generate_random_sample(1000) pmf = Pmf.MakePmfFromList(ran) cdf = Cdf.MakeCdfFromPmf(pmf) myplot.Cdf(cdf) myplot.show() myplot.scatter(*cdf.Render()) myplot.show() myplot.Hist(pmf) myplot.show() myplot.Pmf(pmf) myplot.show()
def __init__(self): results = relay.ReadResults() speeds = relay.GetSpeeds(results) self.speeds = speeds self.pmf = Pmf.MakePmfFromList(speeds) def BiasedPmf(self, speed): new_pmf = self.pmf.Copy() for other_speed,p in new_pmf.Items(): diff = abs(other_speed - speed) new_pmf.Mult(other_speed, diff) new_pmf.Normalize() return new_pmf if __name__ == '__main__': r = RelayPmf() biased = r.BiasedPmf(7.5) print "Variance", biased.Var() print "Mean", biased.Mean() myplot.Clf() myplot.Hist(biased) myplot.Save(root='observed_speeds', title='PMF of running speed', xlabel='speed (mph)', ylabel='probability')
def main(name, data_dir='.'): hist = Pmf.MakeHistFromList([1, 2, 2, 3, 5]) print(hist.Freq(2)) myplot.Hist(hist) myplot.Show()
def MakeHistogram(table): lengths = [record.prglength for record in table.records] hist = Pmf.MakeHistFromList(lengths, name='pregnancy length') myplot.Hist(hist, show=True, xlabel='weeks', ylabel='count')