
확률 변수가 취할 수 있는 경우가 2가지인 경우
예) 동전 던지기, 클릭 등
한 유저가 어떤 버튼을 클릭하는 경우가 1, 클릭하지 않는 경우가 0으로 동등하다고 하면 아래와 같이 표현

일반화 수식
시각화 코드
import matplotlib.pyplot as plt
# 베르누이 분포 모수 정의
p_bernoulli = 0.5 # 확률
# 클릭(=성공) 1, 클릭안함(=실패) 0 으로 정의
x_bernoulli = [0, 1]
# 각 확률 계산
y_bernoulli = [1 - p_bernoulli, p_bernoulli]
# 베르누이 분포 시각화
plt.figure(figsize=(6, 4))
plt.bar(x_bernoulli, y_bernoulli,
color='green', alpha=0.7, width=0.4,
label=f'Bernoulli Distribution (p={p_bernoulli})')
plt.xticks([0, 1], ['Failure (0)', 'Success (1)'])
plt.xlabel('Outcome')
plt.ylabel('Probability')
plt.title('Bernoulli Distribution')
plt.legend()
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

이항분포 표현식
이항분포 수식
버튼을 클릭하는 확률이 1/2일 때, 3명의 유저 중 2명이 클릭할 확률은
시각화 코드
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom
# 이항분포 모수 정의
n_users = 3 # 유저의 수
p_click = 1 / 2 # 클릭 확률
# 클릭 수 생성
x_clicks = np.arange(0, n_users + 1) # [0, 1, 2, 3]
# 클릭 0 부터 3(모두 클릭)에 대한 확률을 생성
y_clicks = binom.pmf(x_clicks, n_users, p_click) # [0.125, 0.375, 0.375, 0.125]
# 시각화
plt.figure(figsize=(8, 5))
plt.bar(x_clicks, y_clicks, color='orange', alpha=0.7, label=f'Binomial Distribution (n={n_users}, p={p_click:.2f})')
plt.xlabel('Number of Users Clicking')
plt.ylabel('Probability')
plt.title('Binomial Distribution of Clicks')
plt.xticks(x_clicks)
plt.legend()
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()

자연스럽게 정규분포와 모양이 비슷해진다.
일반적으로 이면서 인 경우 정규분포를 따른다고 "경험적"으로 알려져있다.

수식
시각화 코드
import numpy as np
import matplotlib.pyplot as plt
from scipy.stats import binom
# 이항분포 정의
n_large = 100 # 100명이 시행
p_large = 1 / 2 # 클릭 확률 0.5
#데이터 생성
x_large = np.arange(0, n_large + 1)
# 100명의 시행횟수 수행
y_large = binom.pmf(x_large, n_large, p_large)
# Normal approximation (mean and standard deviation)
mean = n_large * p_large
std = np.sqrt(n_large * p_large * (1 - p_large))
normal_approx = (1 / (std * np.sqrt(2 * np.pi))) * np.exp(-0.5 * ((x_large - mean) / std) ** 2)
# 이항분포와 정규분포 시각화
plt.figure(figsize=(10, 6))
plt.bar(x_large, y_large, color='blue', alpha=0.5, label='Binomial Distribution')
plt.plot(x_large, normal_approx, color='red', lw=2, label='Normal Approximation')
plt.xlabel('Number of Users Clicking')
plt.ylabel('Probability')
plt.title(f'Binomial Distribution (n={n_large}, p={p_large:.2f}) vs Normal Approximation')
plt.legend()
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.show()




