私はまだPythonには新しく、クラスの問題を解決しています。私は本当に解決策に近いように感じますが、私の数字はまだ私が確率からどのように期待するのか出ていません。numpy.random.choiceを使用して、バッグからアイテムを引き出す代わりに
問題では、2つのチップが入ったバッグがあります。チップの1つが白で、もう1つが黒か白かを知っています。これらの条件は、ゲームをプレイするたびに真です。
ゲームでは、1つのチップをバッグから取り出して白いものにしました。問題は、2つの白いチップをバッグから引き出す確率を近似する関数を書くことです。関数の引数は、ゲームをプレイする回数です。
ここで私がこれまでに書いたコードです:効果的
def prob_two_whites(num):
counter = 0 #counts the number of iterations or games played
first_white = 0 #counts how many times the first pull was white
double_white = 0 #counts how many times the second pull was white, if the first was also white
while counter < num: #runs a game until total number of games is reached
chip_1 = "white" #this is the chip we know is white
chip_2 = np.random.choice(["white","black"]) #chip two has an equal chance of being white or black for each game
result = np.random.choice([chip_1, chip_2], 2, replace = False) #drawing both chips without replacement
if result[0] == "white": #if the first chip pulled is white
first_white += 1 # add one to the first white
if result[1] == "white": #if the second pull was white, given the first was also white
double_white += 1 # add one to double_white
counter +=1 #add one to the counter
return (float(double_white)/float(first_white))/(float(first_white)/float(counter))
は、結果は約66.66パーセント
確率的に、最初の引きが白であることの可能性は75%であるべきです。最初の白を引っ張ると、2番目の引きが白くなる確率は約50%です。
first_whiteとdouble_whiteの別個のカウントを見ると、first_whiteの数値は集計されているように見えます(合計カウントの約75%)。しかし、double_whiteカウントは一貫して高すぎます。私のコードはかなり簡単ですが、何とか私は二重白人を数えているようです。
ご迷惑をおかけして申し訳ございません。
ありがとうございました!
double_whiteをcounterでなくfirst_whiteで区切ってください。 'return(float(double_white)/ float(counter))/(float(first_white)/ float(counter))' – umutto