import numpy as np
import matplotlib.pyplot as plt
import random
from scipy.stats import norm
num1 = int(input('0부터 8 중 숫자 입력~~'))
def coin_hypo(num1):
def coin_prob(num1):
try:
num1=int(num1)
ccnt=0
for i in range(100000):
cnt=0
for j in range(8):
if random.randint(0,1)==1:
cnt += 1
if cnt == num1:
ccnt += 1
return (ccnt/100000)
except:
return('숫자가 아니어서 실행할 수없습니다')
# 정규분포 그리기
x = [0,1,2,3,4,5,6,7,8]
y = norm.pdf(x,8/2,np.sqrt(8/4))
plt.plot(x,y, color="blue")
# P-value 시각화 (상한) 68%
x2 = np.arange(4+np.sqrt(2)*1,8,0.001)
y2 = norm.pdf(x2,4,np.sqrt(2) )
plt.fill_between(x2, y2, interpolate=True, color='yellow', alpha=0.5)
x3 = np.arange(0,4-np.sqrt(2)*1,0.001)
y3 = norm.pdf(x3,4,np.sqrt(2) )
plt.fill_between(x3, y3, interpolate=True, color='yellow', alpha=0.5)
# 검정 통계량값이 기각역에 있는 표시
plt.scatter(num1, 0, c='green', alpha=0.9)
if 4+np.sqrt(2)*1>= coin_prob(num1) and coin_prob(num1)>= 4-np.sqrt(2)*1 :
print('동전을 8번 던졌을 때 뒷면이 나오는 횟수가 %d번이 나올 확률은 신뢰구간 68% 안에 있습니다.'%num1)
else:
print('동전을 8번 던졌을 때 뒷면이 나오는 횟수가 %d번이 나올 확률은 신뢰구간 68% 안에 없습니다.'%num1)
y1_value=[]
x_name=[]
# 막대그래프 그리기
for e in range(9):
y1_value.append(coin_prob(e))
x_name.append(e) # x축
n_groups = len(x_name) # 12 가 N_groups 에 할당
index = np.arange(n_groups) # 0~11 까지 리스트화 함
plt.bar(index, y1_value, tick_label=x_name, color = "red",align='center') # 막대그래프 시각화 코드
plt.xlabel('# of H') # x 축 라벨 이름
plt.ylabel('Proability') # y 축 라벨 이름
plt.title('Coin Proability') # 그래프 제목
plt.xlim( -1, n_groups) # x축 눈금
plt.ylim( 0.00, 0.35) # y축 눈금
plt.show()
print(coin_hypo(num1))