ペアのサイコロシミュレーション
レッスンの例と同様に、2つの袋からそれぞれサイコロを1つずつ取り出して振ります。各袋には偏ったサイコロが3個ずつ入っています。
bag1 = [[1, 2, 3, 6, 6, 6], [1, 2, 3, 4, 4, 6], [1, 2, 3, 3, 3, 5]]
bag2 = [[2, 2, 3, 4, 5, 6], [3, 3, 3, 4, 4, 5], [1, 1, 2, 4, 5, 5]]
違いは、2つの袋のサイコロがペアになっていることです。bag1の2番目のサイコロを選んだら、bag2の2番目のサイコロも同時に選びます。各試行で行うことは次のとおりです。
- 2つの袋からサイコロのペアをランダムに1組選び、振る
dice1とdice2の出目の合計が8なら成功、そうでなければ失敗
あなたのタスクは、roll_paired_biased_dice() 関数内の for ループを完成させ、この関数を使って dice1 と dice2 の各ユニークな組み合わせごとの成功確率を計算することです。
次のライブラリはすでにインポート済みです:random、numpy は np、pandas は pd、seaborn は sns、matplotlib.pyplot は plt。
この演習はコースの一部です
Pythonで学ぶモンテカルロ・シミュレーション
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
def roll_paired_biased_dice(n, seed=1231):
random.seed(seed)
results={}
for i in range(n):
bag_index = random.randint(0, 1)
# Obtain the dice indices
dice_index1 = ____
dice_index2 = ____
# Sample a pair of dice from bag1 and bag2
point1 = ____
point2 = ____
key = "%s_%s" % (point1,point2)
if point1 + point2 == 8:
if key not in results:
results[key] = 1
else:
results[key] += 1
return(pd.DataFrame.from_dict({'dice1_dice2':results.keys(),
'probability_of_success':np.array(list(results.values()))*100.0/n}))