import numpy as np
from empiricaldist import Pmf
= 10000
n = np.random.randint(1, 7, size=(n, 4))
a =1)
a.sort(axis= a[:, 1:].sum(axis=1)
t = Pmf.from_seq(t) pmf_best3
최솟값, 최댓값 그리고 혼합 분포
확률 통계
넷 중 높은 값
연습문제
7-1
= 10000
n = np.random.randint(1, 7, size=(n, 4))
a =1)
a.sort(axis= a[:, 1:].sum(axis=1)
t = Pmf.from_seq(t)
pmf_best3 = pmf_best3.make_cdf() cdf_best3
from empiricaldist import Cdf
import matplotlib.pyplot as plt
= [15,14,13,12,10,8]
standard = Pmf.from_seq(standard)
standard_pmf
='best 3 of 4', color='C1', ls='--')
cdf_best3.plot(label= Cdf.from_seq(standard)
cdf_standard
='standard set', color='C7')
cdf_standard.step(label'CDF');
plt.ylabel(
= cdf_best3.max_dist(6)
cdf_max_dist6 = cdf_best3.min_dist(6) cdf_min_dist6
print(f"Best 3 of 4 - 평균: {pmf_best3.mean():.2f}, 표준편차: {pmf_best3.std():.2f}")
print(f"Standard array - 평균: {standard_pmf.mean():.2f}, 표준편차: {standard_pmf.std():.2f}")
= cdf_best3(7)
prob_less_than_8 print(f"Best 3 of 4에서 8보다 작은 값이 나올 확률: {prob_less_than_8:.4f}")
print(f"6번 굴렸을 때 최소 하나가 8보다 작을 확률: {(1 - cdf_min_dist6(8)):.4f}")
= 1 - cdf_best3(15)
prob_greater_than_15 print(f"Best 3 of 4에서 15보다 큰 값이 나올 확률: {prob_greater_than_15:.4f}")
print(f"6번 굴렸을 때 최소 하나가 15보다 클 확률: {(1 - cdf_max_dist6(15)):.4f}")
Best 3 of 4 - 평균: 12.25, 표준편차: 2.82
Standard array - 평균: 12.00, 표준편차: 2.38
Best 3 of 4에서 8보다 작은 값이 나올 확률: 0.0569
6번 굴렸을 때 최소 하나가 8보다 작을 확률: 0.5206
Best 3 of 4에서 15보다 큰 값이 나올 확률: 0.1275
6번 굴렸을 때 최소 하나가 15보다 클 확률: 0.5588
7-2
def update_dice(pmf, data):
= pmf.qs
hypos = 1 / hypos
likelihood > hypos] = 0
likelihood[data *= likelihood
pmf
pmf.normalize()
= Pmf.from_seq([6, 8, 10])
pmf 1)
update_dice(pmf, pmf
probs | |
---|---|
6 | 0.425532 |
8 | 0.319149 |
10 | 0.255319 |
import pandas as pd
def make_mixture(pmf, pmf_seq):
= pd.DataFrame(pmf_seq).fillna(0).transpose()
df *= np.array(pmf)
df = df.sum(axis=1)
total return Pmf(total)
= Pmf.from_seq(range(1, 7)) # 6면체 주사위
pmf_6 = Pmf.from_seq(range(1, 9)) # 8면체 주사위
pmf_8 = Pmf.from_seq(range(1, 11)) # 10면체 주사위
pmf_10
= make_mixture(pmf, [pmf_6, pmf_8, pmf_10])
mixture = mixture[6] if 6 in mixture.qs else 0
prob_6_damage print(f"\nProbability of 6 points of damage: {prob_6_damage:.4f}")
Probability of 6 points of damage: 0.1363
7-3
= 950
mean = 50
std
= np.random.normal(mean, std, size=365)
sample = Pmf.from_seq(sample)
pmf = pmf.make_cdf() cdf
= []
means = np.arange(1, 20)
n_values
for n in n_values:
= cdf.max_dist(n)
cdf_max_dist = cdf_max_dist.mean()
mean_max
means.append(mean_max)
= np.array(means)
means = 1000
target = np.argmin(np.abs(means - target))
closest_idx = n_values[closest_idx]
optimal_n = means[closest_idx]
closest_mean
print(f"\n결과:")
print(f"1000g에 가장 가까운 평균을 만드는 n: {optimal_n}")
print(f"해당 n에서의 평균: {closest_mean:.2f}g")
print(f"목표값과의 차이: {abs(closest_mean - target):.2f}g")
결과:
1000g에 가장 가까운 평균을 만드는 n: 3
해당 n에서의 평균: 997.51g
목표값과의 차이: 2.49g