# Day 01 典型統計應用在社群媒體分析(Classical statistics applied to social data) part 1

## 大綱

$$B(k;p,N) = \frac{N!}{k!(N-k)!}p^k(1-p)^{N-k}$$

# matplotlib plots are placed inline
%matplotlib inline

# standard matplotlib and numpy imports
import matplotlib.pyplot as plt
import numpy as np

# use scipy.stats to define the distribution
import scipy
from scipy import stats

N = 10 # number of coin flips in a set
p = 0.5 # probability of head

x = scipy.linspace(0,N,N+1) # create bins
pmf = scipy.stats.binom.pmf(x,N,p)
# "pmf" => probability mass function, which (in a snowstorm of ill-used vocabulary) is in this case what we would usually call
# the probability density function

plt.bar(x,pmf)


N = 10 # number of trials ("coin-flips") in a set
p = 0.5 # probability of success ("heads")
size = 50 # number of sets of coin flips

np.random.binomial(N,p,size) # number of heads in a set of coin flips

# how many sets of trials are required to make the approximation appear visually identical to the exact distribution?
size = 10

data = np.random.binomial(N,p,size)
n_bins = N
binned_data, bins, patches = plt.hist(data, n_bins, range = (0,10), normed=True)


N = 100 # number of trials in a set
p = 0.5 # probability of success

x = scipy.linspace(0,N,N+1) # create bins
pmf = scipy.stats.binom.pmf(x,N,p)
plt.bar(x,pmf)