# 二項分布 (Binomial distribution)

(1)
\begin{align} f(x) = {n \choose x} p^x (1-p)^{n-x} \end{align}

# 二項定理

(2)
\begin{align} (a+b)^n = \sum^{n}_{k=0} {n \choose k} a^k b^{n-k} \end{align}

# 特性

(3)
\begin{eqnarray} 1. && E(X) = \mu = np \\ 2. && Var(X) = \sigma^2 = np (1-p) = npq \end{eqnarray}

# 動差生成函數

(4)
$$m_x(t) = ((1-p)+pe^t)^n = (q+pe^t)^n$$

# R 程式範例一：二項分布的圖形

> n=10; p=0.3; k=seq(0,n)
> plot(k, dbinom(k,n,p), type='h', main='dbinom(0:20, n=10, p=0.3)', xlab='k')
>

# R 程式範例二：(定理) 常態分配可用來逼近二項分布

[$]binom(n, p) \rightarrow norm(np, np(1-p)) \; ; \; 假如 n 夠大 (n*min(p, 1-p) > 5)[$]

op=par(mfrow=c(2,2))
n=3; p=0.3; k=seq(0,n)
plot(k, dbinom(k,n,p), type='h', main='dbinom(n=3, p=0.3)', xlab='k')

n=5; p=0.3; k=seq(0,n)
plot(k, dbinom(k,n,p), type='h', main='dbinom(n=5, p=0.3)', xlab='k')

n=10; p=0.3; k=seq(0,n)
plot(k, dbinom(k,n,p), type='h', main='dbinom(n=10, p=0.3)', xlab='k')

n=100; p=0.3; k=seq(0,n)
plot(k, dbinom(k,n,p), type='h', main='dbinom(n=100, p=0.3)', xlab='k')

# R 程式範例三

> x = rbinom(100000, 100, 0.8)
> hist(x, nclas=max(x)-min(x)+1)
>

# R 程式範例四

> y <- rbinom(50, 25, .4)
> m1 <- mean(y)
> m2 <- sum(y) / 25
> y
[1] 12  9  9  9 12 11 10 11  5  7  8  7 16  6 12 13  9 12  9 13  7 12 15  8
[25]  9  7 10  4 10 10  9 10 13  8 10 14  8 11 11 10 10  9  7 13  5  5 11 13
[49]  9  8
> m1
[1] 9.72
> m2
[1] 19.44
> m3 <- sum ( (y-m1)^2 ) / 50
> m3
[1] 6.8816
>