Skip to content

Instantly share code, notes, and snippets.

@henoc
Created November 22, 2015 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save henoc/8d5dc4c0b52c03769df9 to your computer and use it in GitHub Desktop.
Save henoc/8d5dc4c0b52c03769df9 to your computer and use it in GitHub Desktop.
最大連勝数の期待値
# モンテカルロ法で最大連勝数の期待値を出す
# x : 試合数
# p : 勝率
# n : モンテカルロを試行する回数
import random
def get_max_continuous_true(lst):
"Boolean List を受け取り, Trueの最大連続数を返す"
cnt = 0
max_cnt = 0
for e in lst:
if e:
cnt += 1
else:
max_cnt = cnt if cnt > max_cnt else max_cnt
cnt = 0
max_cnt = cnt if cnt > max_cnt else max_cnt
return max_cnt
p = 0.6
n = 10000
print("| x | E(x) |")
print("|----:|---------:|")
for x in range(10,300,10):
ct_sum = 0
for i in range(n):
scores = [(random.random() < p) for i in range(x)]
ct_sum += get_max_continuous_true(scores)
ans = 1.0 * ct_sum / n
print("|{0:>5}|{1:>10}|".format(x,ans))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment