2020-수학-가형-홀수-25

문제

2020-수학-가형-홀수-25

풀이

from itertools import product
from collections import Counter
from sympy import Rational
from sympy import stats
freq_a = []
for event in list(product("123456", repeat=5)):
    a = sum(1 for i in event if int(i)%2 == 1)
    freq_a.append(a)
Counter(freq_a)
Counter({5: 243, 4: 1215, 3: 2430, 2: 2430, 1: 1215, 0: 243})
pmf_a = dict(Counter(freq_a))
for a in pmf_a.keys():
    pmf_a.update({a:Rational(pmf_a[a], 6**5)})
pmf_a
{5: 1/32, 4: 5/32, 3: 5/16, 2: 5/16, 1: 5/32, 0: 1/32}
freq_b = []
for event in list(product("HT", repeat=4)):
    b = sum(1 for i in event if i == "H")
    freq_b.append(b)
Counter(freq_b)
Counter({4: 1, 3: 4, 2: 6, 1: 4, 0: 1})
pmf_b = dict(Counter(freq_b))
for b in pmf_b.keys():
    pmf_b.update({b:Rational(pmf_b[b],2**4)})
pmf_b
{4: 1/16, 3: 1/4, 2: 3/8, 1: 1/4, 0: 1/16}
a = stats.FiniteRV('a', pmf_a)
b = stats.FiniteRV('b', pmf_b)
stats.density(a-b)
{0: 63/256,
 1: 63/256,
 2: 21/128,
 3: 9/128,
 4: 9/512,
 5: 1/512,
 -1: 21/128,
 -2: 9/128,
 -3: 9/512,
 -4: 1/512}
ans = stats.density(a-b)[3]
[ans, ans.evalf()]
[9/128, 0.0703125000000000]
ans.numerator()+ans.denominator()
137

실험

import random


dice = [1,2,3,4,5,6]
coin = ['H','T']


counter = 0
num_trials = 100000
for _ in range(num_trials):
    sample_dice = random.choices(dice, k=5)
    sample_coin = random.choices(coin, k=4)
    a = sum([1 for _ in sample_dice if _%2==1])
    b = sum([1 for _ in sample_coin if _=="H"])
    #ab_list.append(a-b)
    if a-b==3 : counter +=1
counter/num_trials
0.0714