다음 그래프들은 전자책 파이썬과 함께하는 통계이야기 3 장에 수록된 그림들의 코드들입니다.
import numpy as np import pandas as pd from scipy import stats import matplotlib.pyplot as plt import seaborn as sns sns.set_style("darkgrid")
#fig 311 uni, p=[0, 1, 2],[0.25, 0.5, 0.25] fig, ax=plt.subplots(figsize=(4,3)) ax.bar(uni, p, color="g", alpha=0.3) ax.set_xlabel("# of head(H)") ax.set_ylabel("PMF") ax.set_yticks(p) ax.set_xticks(uni) plt.show()
#fig 312 import itertools ca=list(itertools.product(range(1, 7), repeat=2)) S=[sum(i) for i in ca] uni, fre=np.unique(S, return_counts=True) fresum=sum(fre) p=[i/fresum for i in fre] fig, ax=plt.subplots(figsize=(4,3)) ax.bar(uni, p, color="g", alpha=0.3) ax.set_xlabel("Sum of number") ax.set_ylabel("PMF") ax.set_yticks(np.array(p).round(3)) ax.set_xticks(uni) plt.show()
#fig 313 ca=list(itertools.product([0, 1], repeat=2)) S=[sum(i) for i in ca] uni, fre=np.unique(S, return_counts=True) re=pd.DataFrame([uni, fre, fre/fre.sum()], index=["x","Freq", "prop"]) p_cum=np.cumsum(re.iloc[2,:]) fig, ax=plt.subplots(figsize=(4,3)) ax.bar(uni, re.values[2,:], color="g", alpha=0.3, label="PMF") ax.step(uni, p_cum, color="b", label="CDF") ax.set_xlabel("x") ax.set_ylabel("Probability") ax.set_yticks([0, 0.25, 0.5, 0.75, 1]) ax.set_xticks(re.values[0,:]) ax.legend(loc="best") plt.show()
#fig 314 pmf1=stats.binom.pmf(range(0, 11), 10, 0.3) pmf2=stats.binom.pmf(range(0, 21), 20, 0.6) fig, ax=plt.subplots(figsize=(4,3)) ax.bar(range(0, 11), pmf1, color="brown", label="B(10, 0.3)") ax.bar(range(0, 21), pmf2, color="g", label="B(20, 0.6)") ax.set_xlabel("x") ax.set_ylabel("Probability") ax.legend(loc="best") plt.show()
# fig 315 x=range(1, 16) p1=stats.geom.pmf(x, 0.1) p2=stats.geom.pmf(x, 0.3) p3=stats.geom.pmf(x, 0.5) p4=stats.geom.pmf(x, 0.7) fig, ax=plt.subplots(figsize=(4,3)) p=[p1,p2,p3,p4] col=['g','b','r','k'] nme=[0.1, 0.3, 0.5, 0.7] for i in range(len(p)): ax.plot(x, p[i], color=col[i], label=f"Geometric({nme[i]})") ax.set_xlabel("x") ax.set_ylabel("Probability") ax.legend(loc="best") plt.show()
#fig 316 x=range(3, 30) p1=stats.nbinom.pmf(x, 3, 0.1) p3=stats.nbinom.pmf(x, 3, 0.3) p5=stats.nbinom.pmf(x, 3, 0.5) p7=stats.nbinom.pmf(x, 3, 0.7) fig, ax=plt.subplots(figsize=(4,3)) p=[p1,p3,p5,p7] col=['g','b','r','k'] nme=[0.1, 0.3, 0.5, 0.7] for i in range(len(p)): ax.plot(x, p[i], color=col[i], label=f"NB(3, {nme[i]})") ax.set_xlabel("x") ax.set_ylabel("Probability") ax.legend(loc="best") plt.show()
#fig 317 M=80; n=50; N=20 re=stats.hypergeom.pmf(range(21), M, n, N) fig, ax=plt.subplots(figsize=(4,3)) ax.bar(range(21), re, color="g", label="hypergeom(80, 50, 20)") ax.set_xlabel("x") ax.set_ylabel("Probability") ax.set_ylim([0, 0.25]) ax.legend(loc="best", frameon=False) plt.show()
#fig 318 x=range(1, 41) p=[1, 5, 10, 30] pmf={} for i in p: pmf[i]=stats.poisson.pmf(x, i) fig, ax=plt.subplots(figsize=(4,3)) col=["g",'b','r','k'] for i in range(4): ax.plot(x, pmf[p[i]], color=col[i], alpha=0.6, label=f"Poisson({p[i]})") ax.set_xlabel("x") ax.set_ylabel("Probability") ax.legend(loc="best", frameon=False) plt.show()
#fig 319 x=range(1, 20) pmf=stats.poisson.pmf(x, 25/3) fig, ax=plt.subplots(figsize=(4,3)) ax.bar(x, pmf, color="g", alpha=0.6, label=r"Poisson$\left(\frac{25}{3}\right)$") ax.set_xlabel("x") ax.set_ylabel("Probability") ax.legend(loc="best", frameon=False) plt.show()
#fig321 x=np.linspace(-4, 4, 100) p=stats.norm.pdf(x) nme=[r"-2.56$\sigma$", r"-1.96$\sigma$", r"$\sigma$", r'$\mu$', r"$\sigma$", r"1.96$\sigma$", r"2.56$\sigma$"] x1=np.linspace(-1, 1, 100) x21=np.linspace(-1.96, -1, 100) x22=np.linspace(1, 1.96, 100) x31=np.linspace(-2.56, -1.96, 100 ) x32=np.linspace(1.96, 2.56, 100) fig, ax=plt.subplots(figsize=(4,3)) ax.plot(x, p, color="r") ax.fill_between(x1, stats.norm.pdf(x1), color="g", alpha=0.3, label="68%") ax.fill_between(x21, stats.norm.pdf(x21), color="b", alpha=0.3, label="95%") ax.fill_between(x22, stats.norm.pdf(x22), color="b", alpha=0.3) ax.fill_between(x31, stats.norm.pdf(x31), color="brown", alpha=0.3, label="99%") ax.fill_between(x32, stats.norm.pdf(x32), color="brown", alpha=0.3) ax.set_xlabel("x") ax.set_ylabel("Probability") ax.set_xticks(ticks=[-2.56, -1.96, -1, 0, 1, 1.96, 2.56], labels=nme) ax.set_title("Normal Distribution") ax.legend(loc="best") plt.show()
#fig 322 x=np.sort(stats.norm.rvs(size=50, random_state=3)) y=stats.norm.pdf(x) x2=np.sort(stats.norm.rvs(size=1000, random_state=3)) y2=stats.norm.pdf(x2) fig, (ax1, ax2)=plt.subplots(nrows=1, ncols=2, figsize=(8, 3)) ax1.hist(x,bins=10, density=True, rwidth=0.8, label="size=50") ax1.plot(x, y, color="r", label="N(0,1)") ax1.set_xlabel("x") ax1.set_ylabel("probability") ax1.legend(loc="best") ax2.hist(x2,bins=10, density=True, rwidth=0.8, label="size=1000") ax2.plot(x2, y2, color="r", label="N(0,1)") ax2.set_xlabel("x") ax2.legend(loc="best") plt.show()
#fig 323 x=np.sort(stats.norm.rvs(size=1000, random_state=3)) y=stats.norm.pdf(x) mu=[-2, 0, 1, 2] sigma=[1, 1.5, 2, 2.5] col=["r",'b', 'g','orange'] fig, (ax1, ax2)=plt.subplots(nrows=1, ncols=2, figsize=(8, 3)) for i in range(4): ax1.plot(x, stats.norm.pdf(x, mu[i], 1), color=col[i], label=f"N({mu[i]},1)") ax2.plot(x, stats.norm.pdf(x, 0, sigma[i]), color=col[i], label=f"N(0, {sigma[i]})") ax1.set_xlabel("x, (a) Change in $\mu$", loc="right") ax1.set_ylabel("probability") ax1.set_ylim(0, 0.7) ax1.vlines(0, 0, 0.7, color="k", alpha=0.3) ax1.legend(loc="best", frameon=False) ax2.set_xlabel("x, (b) Change in $\sigma$", loc="right") ax2.legend(loc="best", frameon=False) ax2.set_ylim(0, 0.42) ax2.vlines(0, 0, 0.42, color="k", alpha=0.3) plt.show()
#fig 324 x=np.linspace(-3, 3, 100) x1=np.linspace(-3, 0, 100) x2=np.linspace(0, 3, 100) y=stats.norm.pdf(x) plt.figure(figsize=(4,3)) plt.plot(x, y, color="g", label="N(0, 1)") plt.fill_between(x1, stats.norm.pdf(x1), color="b", alpha=0.3, label="50%") plt.fill_between(x2, stats.norm.pdf(x2), color="g", alpha=0.3, label="50%") plt.xlabel("x") plt.ylabel("pdf") plt.yticks([0.1, 0.2, 0.3, 0.4]) plt.legend(loc="upper left", frameon=False) plt.text(1, 0.3, r"f(x)=$\frac{1}{\sqrt{2}}e^{-\frac{x^2}{2}}$", color="g", size="12") plt.show()
#fig 325 x=np.linspace(-25, 5, 1000) y1=stats.norm.pdf(x, -10, 4) y2=stats.norm.pdf(x) plt.figure(figsize=(4,3)) plt.plot(x, y1, color="g", label="N(-10, 4)") plt.plot(x, y2, color="b", label="N(0,1)") plt.xlabel("x") plt.ylabel("pdf", rotation="horizontal", labelpad=10) plt.legend(loc="center left", frameon=False) plt.show()
#fig 326 x=np.linspace(0.01, 6, 1000) plt.figure(figsize=(4,3)) col=["g", "b", "r"] for i, j in enumerate([0.5, 1, 2]): y=stats.expon.pdf(x, j) idx=np.where(y>0)[0] plt.plot(x[idx], y[idx], color=col[i], label=r"$\lambda$="+str(j)) plt.xlabel("x") plt.ylabel("pdf", rotation="horizontal", labelpad=10) plt.legend(loc="best", frameon=False) plt.show()
#fig 327 x=np.linspace(0.01, 14, 1000) fig, ax=plt.subplots(figsize=(4,3)) lam=0.413 y=stats.expon.pdf(x, lam, 1/lam) num=[ 1, 2, 3 , 4, 5, 6 , 7, 11, 12, 13, 14] prop=[0.489, 0.252, 0.067, 0.067, 0.052, 0.03 , 0.007, 0.007, 0.007, 0.007 ,0.015] idx=np.where(y>0)[0] plt.bar(num, prop, color="g", alpha=0.3) plt.plot(x[idx], y[idx], color='g', label=r"$\lambda$="+str(lam)) plt.xlabel("x") plt.ylabel("pdf", rotation="horizontal", labelpad=10) plt.legend(loc="best", frameon=False) plt.show()
#fig 328 from scipy import special x=np.linspace(0.1, 4.1, 100) y=special.gamma(x) plt.figure(figsize=(4,3)) plt.plot(x, y) plt.xlabel("x") plt.ylabel(r"$\Gamma(x)$", rotation="horizontal", labelpad=10) plt.show()
#fig 329 x=np.linspace(0, 20, 1000) ab=[1, 3,5,7] col=['g','b','r','k'] fig, (ax1, ax2)=plt.subplots(nrows=1, ncols=2, figsize=(8, 3)) for i, j in zip(col, ab): y=stats.gamma.pdf(x, j, 1) ax1.plot(x, y, color=i, label=f"Gamma({j}, 1)") y2=stats.gamma.pdf(x, 10, 0, 1/j) ax2.plot(x, y2, color=i, label=f"Gamma(10, {j})") ax1.set_xlabel("x\n(a)", loc="center" , size="12") ax1.set_ylabel("pdf") ax1.legend(loc="best") ax2.set_xlabel("x\n(b)", loc="center", size="12" ) ax2.legend(loc="best") plt.show()
#fig 3210 x=np.linspace(0, 21, 1000) k=[1, 3,5,10,20] col=['g','b','r','k', "orange"] plt.figure(figsize=(4, 3)) for i, j in zip(col, k): y=stats.chi2.pdf(x, j) plt.plot(x, y, color=i, label=r"$\chi^2$("+str(j)+")") plt.xlabel("x") plt.ylabel("pdf", rotation="horizontal", labelpad=10) plt.ylim(-0.01, 0.4) plt.legend(loc="best") plt.show()
#fig 3211 x=stats.norm.rvs(size=1000) y=stats.norm.rvs(size=900) z=np.append(x**2, y**2) z1=np.sort(z) fig, ax=plt.subplots(figsize=(4,3)) ax.hist(z1, bins=15, rwidth=0.9, color="g", alpha=0.3, label="histogram") ax.set_xlabel("z1") ax.set_ylabel("frequency", color="g") ax2=plt.twinx() ax2.plot(z1, stats.chi2.pdf(z1, 2), color="b", label=r"$\chi^2$(2)") ax2.set_ylabel("pdf", color="b") ax.legend(loc=(0.6, 0.8), frameon=False) ax2.legend(loc=(0.6, 0.7), frameon=False) plt.show()
#fig 3212 x=np.linspace(-3, 3, 1000) k=[1, 5, 10] col=['g','b','r'] plt.figure(figsize=(4,3)) plt.plot(x, stats.norm.pdf(x), label="N(0, 1)") for i, j in zip(k, col): plt.plot(x, stats.t.pdf(x, i), color=j, label=f"t({i})") plt.xlabel("x") plt.ylabel("pdf", rotation="horizontal", labelpad=10) plt.legend(loc='best', frameon=False) plt.show()
#fig 3213 x=np.linspace(0, 4, 1000) n=[2, 4, 8, 12] d=[4, 8, 12, 16] col=['g','b','r', 'k'] plt.figure(figsize=(4,3)) for i in range(4): plt.plot(x, stats.f.pdf(x, n[i], d[i]), color=col[i], label=f"F{n[i], d[i]}") plt.xlabel("x") plt.ylabel("pdf", rotation="horizontal", labelpad=10) plt.legend(loc='best', frameon=False) plt.show()
댓글
댓글 쓰기