In [1]:
theta = 0.1
N = 9
xx = np.arange(0, N+1)
#빠진 부분1: 베르누이 시도를 9번 한 결과의 합을 1000번 시뮬레이션하여 s라는 변수에 넣는다.
x = sp.stats.bernoulli(theta).rvs(size=(1000, N))
s = x.sum(axis=1)
f1 = np.bincount(s, minlength=N+1)
#빠진 부분2: f1를 정규화(normalize)하고 N과 theta를 파라미터로 가지는 이항 분포의 pmf값 f2를 생성한다.
f1 = f1/f1.sum()
f2 = sp.stats.binom(N, theta).pmf(xx)
plt.bar(xx-0.3, f1, 0.3, alpha=0.6, color='b', label='Bernoulli Histogram')
plt.bar(xx, f2, 0.3, alpha=0.6, color='r', label='Binomial')
plt.legend();