def normal_probability_between(low, high, mu=0, sigma=1): return normal_cdf(high, mu, sigma) - normal_cdf(low, mu, sigma)
for _ in range(n): s += bernoulli_trial(p) return s if __name__ == '__main__': trials = 10_000 n = 100 # 동전을 전지는 횟수 p = 0.5 # 동전을 던졌을 때 앞 면이 나올 확률 data = [binominal(n, p) for _ in range(trials)] # plt.hist(data) # plt.show() # 이항 확률 변수와 그에 따른 확률값을 그리기 위해서 histogram = Counter(data) x_bar = [k for k in histogram.keys()] y_bar = [v / trials for v in histogram.values()] plt.bar(x_bar, y_bar, color='0.75') # 이항 확률 변수의 정규 분포 근사(approximation) # 이항 확률 변수의 분포는 n이 충분히 크면 정규 분포가 된다. mu = n * p # 평균 sigma = math.sqrt(n * p * (1 - p)) # 표준편차 # 정규 분포 그래프를 그리기 위해서 x_line = range(min(data), max(data) + 1) # range(0, n+1) y_line = [ normal_cdf(x + 0.5, mu, sigma) - normal_cdf(x - 0.5, mu, sigma) for x in x_line ] plt.plot(x_line, y_line) plt.show()
def normal_probability_above(low, mu=0.0, sigma=1.0): return 1 - normal_cdf(low, mu, sigma)