def DecayTest(): X = [1.5,2,3,4,5,12] prior = MakeUniformSuite(0.001,1.5,1000) posterior = EstimateParameter(prior,X) myplot.Clf() myplot.Pmf(posterior) 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 EstimateTest(): evidence = [2.675, 0.198, 1.152, 0.787, 2.717, 4.269] prior = MakeUniformSuite(0.5,1.5,1000) posterior = EstimateParameter(prior,evidence) myplot.Clf() myplot.Pmf(posterior) myplot.show()
def main(): # Exercise 5.6 sample_size=365 for n in range(1,6): distribution = [] for _ in range(sample_size): distribution.append(get_bread(n)) print(np.mean(distribution), np.std(distribution)) found_distribution = [] for _ in range(sample_size): found_distribution.append(int(get_bread(4))) print('picking 4 loaves: mu={} sigma={}'.format(np.mean(found_distribution),np.std(found_distribution))) expected_distribution = np.random.normal(np.mean(found_distribution), np.std(found_distribution), sample_size) myplot.Cdfs(cdfs=(Cdf.MakeCdfFromList(found_distribution), Cdf.MakeCdfFromList(expected_distribution))) myplot.show() plt.hist(found_distribution, normed=True, bins=20, label='found') plt.hist(expected_distribution, normed=True, bins=20, alpha=.75, label='expected') plt.ylabel('Probability') plt.xlabel('Bread Weight') plt.legend() plt.show() # Exercise 5.7 men_heights = np.random.normal(loc=178, scale=59.4, size=99999) women_heights = np.random.normal(loc=163, scale=52.8, size=99999) pairs = zip(men_heights, women_heights) l = [w>m for m, w in pairs] print('In {}% of the pairs the woman will be taller than the man'.format(sum(l)/len(l)*100.0))
def HenriPoincare(): ave = 950 sigma = 50 weights = [] days= 360 n = 4 for i in range(days): p = 0.0 for j in range(n): temp = random.random() if p<temp: p = temp weights.append(erf.NormalCdfInverse(p,mu=ave,sigma = sigma)) mu, var = thinkstats.MeanVar(weights) print mu,math.sqrt(var) normal = [] for i in range(1000): normal.append(erf.NormalCdfInverse(random.random(),mu = mu,sigma = math.sqrt(var))) # histReal = Pmf.MakeHistFromList(weights,'real') # histStand = Pmf.MakeHistFromList(normal,'stand') # pmfReal = Pmf.MakePmfFromList(weights,"real") # pmfStand = Pmf.MakePmfFromList(normal,'stand') cdfReal = Cdf.MakeCdfFromList(weights) cdfStand = Cdf.MakeCdfFromList(normal) myplot.Clf() # myplot.Hists([histReal,histStand]) myplot.Cdfs([cdfReal,cdfStand]) # myplot.Pmfs([pmfReal,pmfStand]) myplot.show()
def NormalPlot(ys): n = len(ys) xs = [random.normalvariate(0,1) for i in range(n)] ys.sort() xs.sort() myplot.Clf() myplot.Plot(xs,ys) myplot.show()
def ResambleBirth(): pool,first,other = descriptive.MakeTables() allCdf = Cdf.MakeCdfFromPmf(pool.pmf) randomCdf = Cdf.MakeCdfFromList(Sample(allCdf,1000)) randomCdf.name = 'random birth' myplot.Clf() myplot.Cdfs([allCdf,randomCdf]) myplot.show()
def BirthDate(): pool,first,other = descriptive.MakeTables() pmf = pool.pmf print 'mean',pmf.Mean() print 'var',pmf.Var() cdf = Cdf.MakeCdfFromPmf(pmf) myplot.Clf() myplot.Cdf(cdf) myplot.show()
def main(): #Exercise 3.5 results = relay.ReadResults() speeds = relay.GetSpeeds(results) speedsCdf = Cdf.MakeCdfFromList(speeds, "race speeds") mplt.Cdf(speedsCdf) mplt.show(title="Race Speed CDF", xlabel="speed in mph", ylabel="probability")
def ParetovariateTest(): results = [] a=1 xm=0.5 for i in range(1000): results.append(random.paretovariate(a)*xm) cdf = Cdf.MakeCdfFromList(results) myplot.Clf() myplot.Cdf(cdf,complement=True,xscale = 'log',yscale = 'log') myplot.show()
def expovariateTest(): results = [] lamada = 1.0/32.6 for i in range(44): results.append(random.expovariate(lamada)) cdf = Cdf.MakeCdfFromList(results) myplot.Clf() myplot.Cdf(cdf,complement=True,xscale = 'linear',yscale = 'log') myplot.show()
def main(): data_dir = '../chap1/' preg = survey.Pregnancies() preg.ReadRecords(data_dir) cdf = weight_cdf(preg) myplot.Cdf(cdf) myplot.show() sample = Sample(cdf, 1000) cdf_sample = Cdf.MakeCdfFromList(sample) myplot.Cdf(cdf_sample) myplot.Show()
def main(): pareto = paretovariate(1, 0.5) cdf = Cdf.MakeCdfFromList(pareto) myplot.Cdf(cdf) myplot.show() ccdf = ccdf_list(cdf) plt = myplot.pyplot plt.plot(cdf.Values(), ccdf) plt.xscale('log') plt.yscale('log') plt.show()
def main(): pareto = paretovariate(1,0.5) cdf = Cdf.MakeCdfFromList(pareto) myplot.Cdf(cdf) myplot.show() ccdf = ccdf_list(cdf) plt = myplot.pyplot plt.plot(cdf.Values(), ccdf) plt.xscale('log') plt.yscale('log') plt.show()
def main(): """when k=1 weibull would be liner""" weibull = weibullvariate(1,1) cdf = Cdf.MakeCdfFromList(weibull) myplot.Cdf(cdf) myplot.show() ccdf = ccdf_list(cdf) plt = myplot.pyplot plt.plot(cdf.Values(), ccdf) #plt.xscale('log') plt.yscale('log') plt.show()
def main(): """when k=1 weibull would be liner""" weibull = weibullvariate(1, 1) cdf = Cdf.MakeCdfFromList(weibull) myplot.Cdf(cdf) myplot.show() ccdf = ccdf_list(cdf) plt = myplot.pyplot plt.plot(cdf.Values(), ccdf) #plt.xscale('log') plt.yscale('log') plt.show()
def WeibullDistribution(): results = [] k = 2 lamada = 1 sampleNum = 6000 for i in range(sampleNum): results.append(random.weibullvariate(lamada,k)) cdf = Cdf.MakeCdfFromList(results) myplot.Clf() myplot.Cdf(cdf,complement=True,xscale = 'log',yscale = 'log') # myplot.Cdf(cdf) myplot.show()
def Random100(): results = [] for i in range(1000): results.append(random.random()) pmf = Pmf.MakePmfFromList(results) cdf = Cdf.MakeCdfFromPmf(pmf) myplot.Clf() myplot.Pmf(pmf) myplot.show() myplot.Clf() myplot.Cdf(cdf) myplot.show()
def main(): n = 44 mean = 32.6 lambd = 1.0/mean data = sample(lambd, n) cdf = Cdf.MakeCdfFromList(data) ccdf = [1 - p for x, p in cdf.Items()] plt.plot(cdf.Values(),ccdf) plt.xscale('log') plt.show() myplot.Cdf(cdf, complement=True,xscale='linear',yscale='log') myplot.show()
def main(): # Exercise 4.1 mean = 32.6 lambd = 1 / mean # l for lambda exp_values = [] for _ in range(44): exp_values.append(random.expovariate(lambd)) exponential_distribution_cdf = Cdf.MakeCdfFromList( exp_values, name='Exponential Distribution') myplot.Cdf(exponential_distribution_cdf, complement=True, xscale='linear', yscale='log') myplot.show()
def Brfss_figs(): resp = brfss.Respondents() resp.ReadRecords() print len(resp.records) results = [r.wtkg2 for r in resp.records if r.wtkg2 != 'NA'] cdf = Cdf.MakeCdfFromList(results) t = results[:] t.sort() mean,var = thinkstats.TrimmedMeanVar(t) print 'len,mean,var',len(t),mean,var sigma = math.sqrt(var) myplot.Clf() xs,ps = continuous.RenderNormalCdf(mean,sigma,175) myplot.Plot(xs,ps) xs,ps = cdf.Render() myplot.Plot(xs,ps) myplot.show()
def CentralLimitTheorem(): num = 4 times = 100 maxX = 2 lamadas = [i+2 for i in range(num)] sumHist = Pmf.Hist() for i in range(times): sumRes = 0 x = random.random()*maxX for lamada in lamadas: sumRes += ExpLamadaX(lamada,x) sumHist.Incr(sumRes) cdf = Cdf.MakeCdfFromHist(sumHist) myplot.Clf() myplot.Cdf(cdf) myplot.show()
def main(): # Exercise 4.3 # function defined below random_pareto_sample = [] for i in range(1000): random_pareto_sample.append(paretovariate(1, .5)) random_pareto_sample_cdf = Cdf.MakeCdfFromList(random_pareto_sample) myplot.Cdf(random_pareto_sample_cdf, complement=True, xscale='log', yscale='log') myplot.show() # Exercise 4.4 heights = [] for _ in range(6000000000): heights.append(paretovariate(alpha=1.7, x_sub_m=100)) heights_cdf = Cdf.MakeCdfFromList(heights) print(heights.Mean()) print(heights.Percentile(heights.Mean())) print(heights.sort())
def main(): # Exercise 3.9 table = survey.Pregnancies() table.ReadRecords() unfilteredLiveBirthWeights = [(p.birthwgt_lb, p.birthwgt_oz) for p in table.records if p.outcome == 1] liveBirthWeights = [ lbs * 16 + oz for lbs, oz in unfilteredLiveBirthWeights if type(lbs) == int and type(oz) == int and lbs * 16 + oz <= 200 ] liveBirthWeightsCdf = Cdf.MakeCdfFromList(liveBirthWeights, name="live birth weights") samepleListLiveBirthWeights = sample(liveBirthWeightsCdf, 1000) myplot.Cdf(Cdf.MakeCdfFromList(samepleListLiveBirthWeights)) myplot.show(title="CDF of live births resampled") # Exercise 3.10 randomList = [random.random() for x in range(1000)] myplot.Pmf(Pmf.MakePmfFromList(randomList)) myplot.show(title="random pmf") myplot.Cdf(Cdf.MakeCdfFromList(randomList)) myplot.Show(title="random cdf")
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()
mycsv.save([y for x in rho for y in x], save_name=DATA + "/rho" + str(fig) + ".csv", header=()) return fig, fbc if __name__ == "__main__": from sympy import * import matplotlib.pyplot as plt s = symbols('s') import os try: os.makedirs(DATA) except: pass fig = -1 import Example1_Single_FRF_Nano_Scale_Servo as ex1 import Example3_Another_Various_FRFs_Nano_Scale_Servo as ex3 fig, o, h = ex3.plant_data(fig, datapath=DATA) fig, fbc = optimize(fig, o, h) ex1.plotall(fig, fbc, ndata=NDATA, datapath=DATA) myplot.show()
def population3(): results = populations.ReadData() cdf = Cdf.MakeCdfFromList(results) myplot.Clf() myplot.Cdf(cdf) myplot.show(xscale = 'log')
def Irs(): data = irs.ReadIncomeFile() hist, pmf, cdf = irs.MakeIncomeDist(data) myplot.Clf() myplot.Cdf(cdf) myplot.show(xscale = 'log')
def main(): maxQuestionAvg = 2000 minQuestionAvg = 400 questionInterval = (maxQuestionAvg - minQuestionAvg) / QUESTION_LIMIT questionBank = [ fakeNormal.FakeNormalQuestion(i, midpoint_rating=r) for i, r in enumerate(range(minQuestionAvg, maxQuestionAvg, questionInterval)) ] assert len(questionBank) == QUESTION_LIMIT maxCompAvg = 1800 minCompAvg = 600 compInterval = (maxCompAvg - minCompAvg) / NUM_COMPETITORS competitors = [ fakeNormal.FakeNormalCompetitor(questionBank, r) for r in range(minCompAvg, maxCompAvg, compInterval) ] assert len(competitors) == NUM_COMPETITORS for competitor in competitors: competitor.init() myplot.init() for t in range(TRIALS): selectedCompetitor1 = random.choice(competitors) selectedCompetitor2 = selectedCompetitor1 while selectedCompetitor1 == selectedCompetitor2: selectedCompetitor2 = random.choice(competitors) actualRatingof1 = selectedCompetitor1.hiddenRating actualRatingof2 = selectedCompetitor2.hiddenRating currentRatingof1 = selectedCompetitor1.rating currentRatingof2 = selectedCompetitor2.rating print "Facing off competitor (", currentRatingof1, print "/", actualRatingof1, ") vs. competitor (", print currentRatingof2, "/", actualRatingof2, ")" # 1 judges 2 numCorrectof2 = selectedCompetitor1.judge.judgeCompetitor(selectedCompetitor2.subject) estRatingof2 = selectedCompetitor1.judge.guessCompetitorRating() # 2 judges 1 numCorrectof1 = selectedCompetitor2.judge.judgeCompetitor(selectedCompetitor1.subject) estRatingof1 = selectedCompetitor2.judge.guessCompetitorRating() # Cannot predict on the first time if estRatingof1 is not None and estRatingof2 is not None: score1 = CORRECT_SCALE * numCorrectof1 - abs(estRatingof2 - currentRatingof2) score2 = CORRECT_SCALE * numCorrectof2 - abs(estRatingof1 - currentRatingof1) if score1 > score2: print "First competitor is winner!" score1 = 1.0 score2 = 0.0 elif score2 > score1: print "Second competitor is winner!" score1 = 0.0 score2 = 1.0 else: print "Draw!" score1 = 0.5 score2 = 0.5 newRating1, newRating2 = elo(score1, score2, currentRatingof1, currentRatingof2) selectedCompetitor1.rating = newRating1 selectedCompetitor2.rating = newRating2 # myplot.plotCompRatings(competitors) # myplot.draw() # time.sleep(0.5) # myplot.close() selectedCompetitor1.finishCompetition(currentRatingof2) selectedCompetitor2.finishCompetition(currentRatingof1) myplot.plotCompRatings(competitors) myplot.show()
def runGame(competitors): myplot.init() for t in range(TRIALS): selectedCompetitor1 = random.choice(competitors) selectedCompetitor2 = selectedCompetitor1 while selectedCompetitor1 == selectedCompetitor2: selectedCompetitor2 = random.choice(competitors) currentRatingof1 = selectedCompetitor1.rating currentRatingof2 = selectedCompetitor2.rating print "Facing off competitor (", currentRatingof1, print "/", selectedCompetitor1.id, ") vs. competitor (", print currentRatingof2, "/", selectedCompetitor2.id, ")" # 1 judges 2 numCorrectof2 = selectedCompetitor1.judge.judgeCompetitor(selectedCompetitor2.subject) estRatingof2 = selectedCompetitor1.judge.guessCompetitorRating() selectedCompetitor2.totalCorrectAnswers += numCorrectof2 # 2 judges 1 numCorrectof1 = selectedCompetitor2.judge.judgeCompetitor(selectedCompetitor1.subject) estRatingof1 = selectedCompetitor2.judge.guessCompetitorRating() selectedCompetitor1.totalCorrectAnswers += numCorrectof1 # Cannot predict on the first time if estRatingof1 is not None and estRatingof2 is not None: score1 = CORRECT_SCALE*numCorrectof1-abs(estRatingof2-currentRatingof2) score2 = CORRECT_SCALE*numCorrectof2-abs(estRatingof1-currentRatingof1) if score1 > score2: print "First competitor is winner!" score1 = 1.0 score2 = 0.0 elif score2 > score1: print "Second competitor is winner!" score1 = 0.0 score2 = 1.0 else: print "Draw!" score1 = 0.5 score2 = 0.5 newRating1, newRating2 = elo(score1, score2, currentRatingof1, currentRatingof2) selectedCompetitor1.rating = newRating1 selectedCompetitor2.rating = newRating2 # myplot.plotRatings(competitors) # myplot.draw() # time.sleep(0.5) # myplot.close() selectedCompetitor1.finishCompetition(currentRatingof2) selectedCompetitor2.finishCompetition(currentRatingof1) for competitor in competitors: print competitor.id, print ":", print competitor.totalCorrectAnswers myplot.plotRatings(competitors) myplot.show()
def min_2norm(fig=0): """ 2 norm optimization :return: """ """ CONDITION AND CONSTRAINTS """ MIN = JER # What to be minimized TS = 0.001 # Sampling Period TINIT = (0, 1) START, END = 0, 1 TSAMPLE = np.linspace(0, TINIT[END], TINIT[END] / TS + 1) QC = (0, 1.0) VC = (0, 0) AC = (0, 0) JC = (0, 0) CONSTRAINTS = (*QC, *VC, *AC, *JC) LABEL_CONSTRAINTS = (POS, VEL, ACC, JER) NCONSTRAINTS = len(CONSTRAINTS) assert NCONSTRAINTS == len(LABEL_CONSTRAINTS) * 2 VMAX = 10000 AMAX = 10000 nc = NCONSTRAINTS + 60 # number of control points == number of theta p = 4 # jerk continous u = [TINIT[END] * i / (nc - 1) for i in range(nc)] # knots bspl = Bspline(u, nc, p, verbose=True) """ QUDRATIC PROGRAMMING """ M = np.array(bspl.basis(TSAMPLE, der=MIN)) P = np.dot(M.T, M) q = np.zeros(nc) A = None for d in LABEL_CONSTRAINTS: for x in bspl.basis(TINIT, der=d): if A is None: A = np.array(x) else: A = np.block([[A], [np.array(x)]]) b = np.matrix(CONSTRAINTS).reshape((NCONSTRAINTS, 1)) if True: G = np.array([*bspl.basis(TSAMPLE, der=VEL), *bspl.basis(TSAMPLE, der=ACC)]) hv = np.ones((len(TSAMPLE), 1)) * VMAX ha = np.ones((len(TSAMPLE), 1)) * AMAX h = np.append(hv, ha, axis=0) G, h = mycvxopt.constraints(G, h, -h) else: G, h = None, None # No ineq theta = mycvxopt.solve("qp", [P, q], G=G, h=h, A=A, b=b) print(theta) """ Plot """ for d in STATES: fig += 1 text = None myplot.time(TSAMPLE, bspl.bspline(theta, TSAMPLE, der=d), fig, text=text, save_name="data/" + LABELS[d][:-1].split()[0], label=("Time (s)", LABELS[d])) myplot.show()
import Cdf import myplot import Pmf import random random_numbers = [] for i in range(1000): random_numbers.append(random.random()) pmf = Pmf.MakePmfFromList(random_numbers) cdf = Cdf.MakeCdfFromList(random_numbers) myplot.Pmf(pmf) myplot.show(title='PMF of random numbers 0-1',xlabel='value',ylabel='probability') myplot.Cdf(cdf) myplot.show(title='CDF of random numbers 0-1',xlabel='value', ylabel='probability')
import Cdf import myplot import Pmf import random random_numbers = [] for i in range(1000): random_numbers.append(random.random()) pmf = Pmf.MakePmfFromList(random_numbers) cdf = Cdf.MakeCdfFromList(random_numbers) myplot.Pmf(pmf) myplot.show(title='PMF of random numbers 0-1', xlabel='value', ylabel='probability') myplot.Cdf(cdf) myplot.show(title='CDF of random numbers 0-1', xlabel='value', ylabel='probability')