def poisson_pmf(): rv=Pie(4.5)#the average incident is 4.5 x = np.arange(0, 11, 1)#return evenly spaced values within 1 interval between [1,11) y = rv.pmf(x)#probability mass function plt.bar(x, y, width=0.6, color='grey')#make bar chart plt.savefig('fig.png')
def expon_cdf_pdf(): x = np.linspace(0, 5, 100)##return evenly spaced samples, calculated over the interval [0,5] rv=E(scale = 1)#the scale is 1 plt.plot(x, rv.pdf(x), color='blue')#make pdf chart plt.plot(x, rv.cdf(x), color='red') #make cdf chart plt.savefig('fig.png')
def geom_pmf(): rv=G(0.2)#probability of success is 0.2 x = np.arange(1, 11, 1)#return evenly spaced values within 1 interval between [1,11) y = rv.pmf(x)#probability mass function plt.bar(x, y, width=0.6, color='grey')#make bar chart plt.savefig('fig.png')
def draw(): #get input data x = get_data() c = ('b', 'g', 'r', 'c', 'm', 'y', 'k', 'w') # the pie chart of the data plt.pie(x, colors=c, shadow=False) #show image plt.savefig('fig.png')
def expon_pdf(): x = np.linspace(0, 20, 100)#return evenly spaced samples, calculated over the interval [0,20] rv1=E(scale = 1.5)#the scale is 1.5 rv2=E(scale = 1.0)#the scale is 1.0 rv3=E(scale = 0.5)#the scale is 0.5 plt.plot(x, rv1.pdf(x), color='green')#make chart plt.plot(x, rv2.pdf(x), color='blue')#make chart plt.plot(x, rv3.pdf(x), color='red')#make chart plt.savefig('fig.png')
def norm_pdf(): x = np.linspace(-10, 10, 100)#return evenly spaced samples, calculated over the interval [-10,10] rv1=N(loc=0, scale = 1)#the mean is 0, the standard deviation is 1 rv2=N(loc=-5, scale = 1)#the mean is -5, the standard deviation is 1 rv3=N(loc=0, scale = 3)#the mean is 0, the standard deviation is 3 plt.plot(x, rv1.pdf(x), color='green')#make chart plt.plot(x, rv2.pdf(x), color='blue')#make chart plt.plot(x, rv3.pdf(x), color='red')#make chart plt.savefig('fig.png')
def central_limit_theorem(): y = [] n=100 for i in range(1000): r = expon.rvs(scale=1, size=n) rsum=np.sum(r) z=(rsum-n)/np.sqrt(n) y.append(z) plt.hist(y,color='grey') plt.savefig('central_limit_theorem.png')
def second_year(): X=sta.norm(loc=950, scale=20)#generate random data in normal distribution whose expectation is 950 and standard deviation is 20 wbread=[] for i in range(365): x=X.rvs(size=100) wbread.append(max(x))#get the random data for one day log(numpy.mean(wbread))#print mean value log(sta.skew(wbread))#print skew value plt.hist(wbread,color='grey') plt.savefig('second_year.png')
def law_of_large_numbers(): x = np.arange(1, 1001, 1) r = bernoulli.rvs(0.3, size=1000) y = [] rsum =0.0 for i in range(1000): if r[i]==1: rsum=rsum+1 y.append(rsum/(i+1)) plt.plot(x, y, color='red') plt.savefig('law_of_large_numbers.png')
def central_limit_theorem(): y = [] n=100 for i in range(1000): r = binom.rvs(n, 0.3) rsum=np.sum(r) z=(rsum-n*0.3)/np.sqrt(n*0.3*0.7) y.append(z) plt.hist(y,color='grey') plt.savefig('central_limit_theorem.png')
def first_year(): X=sta.norm(loc=950, scale=20)#generate random data in normal distribution whose expectation is 950 and standard deviation is 20 wbread=[] for i in range(365): x=X.rvs(size=100) wbread.append(x[0])#get the random data for one day log(numpy.mean(wbread))#print mean value log(sta.skew(wbread))#print skew value plt.hist(wbread,color='grey') plt.savefig('first_year.png')
def law_of_large_numbers(): x = np.arange(1, 1001, 1) r1 = binom.rvs(10, 0.6, size=1000) r2 = poisson.rvs(mu=6, size=1000) r3 = norm.rvs(loc=6, size=1000) y = [] rsum=0.0 for i in range(1000): rsum=rsum+(r1[i]+r2[i]+r3[i]) y.append(rsum/((i+1)*3)-6) plt.plot(x, y, color='red') plt.savefig('law_of_large_numbers.png')
def draw(): #get input data menMeans = (20, 35, 30, 35, 27) menStd = (2, 3, 4, 1, 2) womenMeans = (25, 32, 34, 20, 25) womenStd = (3, 5, 2, 3, 3) ind = np.arange(5) width = 0.35 # the histogram of the data plt.bar(ind, menMeans, width, color='r') plt.bar(ind+width, womenMeans, width, color='y') #show image plt.savefig('fig.png')
def linregress1(): x = np.linspace(-5, 5, num=150) y = x + np.random.normal(size=x.size) y[12:14] += 10 slope, intercept, r_value, p_value, std_err = stats.linregress(x,y) log(slope) log(intercept) log(r_value) log(p_value) log(std_err) plt.plot(x, y, 'b.') plt.plot(x, slope * x + intercept, 'r-') plt.savefig('linregress1.png')
def sampling_distribution(): fig, ax = plt.subplots(1, 1) #display the probability density function df = 10 x=np.linspace(t.ppf(0.01, df), t.ppf(0.99, df), 100) ax.plot(x, t.pdf(x, df)) #simulate the sampling distribution y = [] for i in range(1000): r = norm.rvs(loc=5, scale=2, size=df+1) rt =(np.mean(r)-5)/np.sqrt(np.var(r)/df) y.append(rt) ax.hist(y, normed=True, alpha=0.2) plt.savefig('sampling_distribution.png')
def t_distribution(): fig, ax = plt.subplots(1, 1) #display the probability density function df = 10 x=np.linspace(-4, 4, 100) ax.plot(x, t.pdf(x,df)) #simulate the t-distribution y = [] for i in range(1000): rx = norm.rvs() ry = chi2.rvs(df) rt = rx/np.sqrt(ry/df) y.append(rt) ax.hist(y, normed=True, alpha=0.2) plt.savefig('t_distribution.png')
def sampling_distribution(): fig, ax = plt.subplots(1, 1) #display the probability density function dfn, dfm = 10, 5 x=np.linspace(f.ppf(0.01, dfn, dfm), f.ppf(0.99, dfn, dfm), 100) ax.plot(x, f.pdf(x, dfn, dfm)) #simulate the sampling distribution y = [] for i in range(1000): r1 = norm.rvs(loc=5, scale=2, size=dfn+1) r2 = norm.rvs(loc=3, scale=2, size=dfm+1) rf =np.var(r1)/np.var(r2) y.append(rf) ax.hist(y, normed=True, alpha=0.2) plt.savefig('sampling_distribution.png')
def sampling_distribution(): fig, ax = plt.subplots(1, 1) #display the probability density function x = np.linspace(-4, 4, 100) ax.plot(x, norm.pdf(x)) #simulate the sampling distribution y = [] n=100 for i in range(1000): r = expon.rvs(scale=1, size=n) rsum=np.sum(r) z=(rsum-n)/np.sqrt(n) y.append(z) ax.hist(y, normed=True, alpha=0.2) plt.savefig('sampling_distribution.png')
def F_distribution(): fig, ax = plt.subplots(1, 1) #display the probability density function dfn, dfm = 10, 5 x = np.linspace(f.ppf(0.01, dfn, dfm), f.ppf(0.99, dfn, dfm), 100) ax.plot(x, f.pdf(x, dfn, dfm)) #simulate the F-distribution y = [] for i in range(1000): rx = chi2.rvs(dfn) ry = chi2.rvs(dfm) rf = np.sqrt(rx/dfn)/np.sqrt(ry/dfm) y.append(rf) ax.hist(y, normed=True, alpha=0.2) plt.savefig('F_distribution.png')
def chi2_distribution(): fig, ax = plt.subplots(1, 1) #display the probability density function df = 10 x=np.linspace(chi2.ppf(0.01, df), chi2.ppf(0.99, df), 100) ax.plot(x, chi2.pdf(x,df)) #simulate the chi2 distribution y = [] n=10 for i in range(1000): chi2r=0.0 r = norm.rvs(size=n) for j in range(n): chi2r=chi2r+r[j]**2 y.append(chi2r) ax.hist(y, normed=True, alpha=0.2) plt.savefig('chi2_distribution.png')