我正在尝试编写Python代码,以查看平均需要多少掷硬币才能连续获得N个头部。
我不解的是,我的代码产生的答案与在线给出的答案不匹配,例如在这里(以及其他许多地方)https://math.stackexchange.com/questions/364038/expected-number-of-coin-tosses-to-get-five-consecutive-heads
据此,我应该连续获得各种数量的头部所需的预期投掷次数为:E(1)= 2,E(2)= 6,E(3)= 14,E( 4)= 30,E(5)=62。但是我没有得到这些答案!例如,我得到E(3)= 8,而不是14。下面的代码给出了答案,但是您可以更改n来测试行中其他目标头的数量。
出了什么问题?我的代码逻辑上大概有一些错误,但是我承认我无法弄清楚它是什么。
您可以在这里查看,运行和修改我的代码:https://trinket.io/python/17154b2cbd
下面是该代码本身,位于该可运行的trinket.io页面之外。任何帮助找出问题所在将不胜感激!
非常感谢,
拉杰 附言我能找到的最接近的相关问题是这个:Monte-Carlo Simulation of expected tosses for two consecutive heads in python 但是,据我所知,该问题中的代码实际上并没有测试两个连续头,而是测试了一个以头开始的序列,然后再测试某个以后的序列,可能不是连续的,时间又来了。
# Click here to run and/or modify this code:
# https://trinket.io/python/17154b2cbd
import random
# n is the target number of heads in a row
# Change the value of n, for different target heads-sequences
n = 3
possible_tosses = [ 'h', 't' ]
num_trials = 1000
target_seq = ['h' for i in range(0,n)]
toss_sequence = []
seq_lengths_rec = []
for trial_num in range(0,num_trials):
if (trial_num % 100) == 0:
print 'Trial num', trial_num, 'out of', num_trials
# (The free version of trinket.io uses Python2)
target_reached = 0
toss_num = 0
while target_reached == 0:
toss_num += 1
random.shuffle(possible_tosses)
this_toss = possible_tosses[0]
#print([toss_num, this_toss])
toss_sequence.append(this_toss)
last_n_tosses = toss_sequence[-n:]
#print(last_n_tosses)
if last_n_tosses == target_seq:
#print('Reached target at toss', toss_num)
target_reached = 1
seq_lengths_rec.append(toss_num)
print 'Average', sum(seq_lengths_rec) / len(seq_lengths_rec)
答案 0 :(得分:4)
您不会为每个实验重新初始化toss_sequence
,因此您将以预先存在的头部序列开始每个实验,每次尝试时都有1分之二的机会击中目标序列新实验。
在外部循环中初始化toss_sequence
将解决您的问题:
import random
# n is the target number of heads in a row
# Change the value of n, for different target heads-sequences
n = 4
possible_tosses = [ 'h', 't' ]
num_trials = 1000
target_seq = ['h' for i in range(0,n)]
seq_lengths_rec = []
for trial_num in range(0,num_trials):
if (trial_num % 100) == 0:
print('Trial num {} out of {}'.format(trial_num, num_trials))
# (The free version of trinket.io uses Python2)
target_reached = 0
toss_num = 0
toss_sequence = []
while target_reached == 0:
toss_num += 1
random.shuffle(possible_tosses)
this_toss = possible_tosses[0]
#print([toss_num, this_toss])
toss_sequence.append(this_toss)
last_n_tosses = toss_sequence[-n:]
#print(last_n_tosses)
if last_n_tosses == target_seq:
#print('Reached target at toss', toss_num)
target_reached = 1
seq_lengths_rec.append(toss_num)
print(sum(seq_lengths_rec) / len(seq_lengths_rec))
您可以稍微简化代码,并减少出错的可能性:
import random
# n is the target number of heads in a row
# Change the value of n, for different target heads-sequences
n = 3
possible_tosses = [ 'h', 't' ]
num_trials = 1000
seq_lengths_rec = []
for trial_num in range(0, num_trials):
if (trial_num % 100) == 0:
print('Trial num {} out of {}'.format(trial_num, num_trials))
# (The free version of trinket.io uses Python2)
heads_counter = 0
toss_counter = 0
while heads_counter < n:
toss_counter += 1
this_toss = random.choice(possible_tosses)
if this_toss == 'h':
heads_counter += 1
else:
heads_counter = 0
seq_lengths_rec.append(toss_counter)
print(sum(seq_lengths_rec) / len(seq_lengths_rec))
答案 1 :(得分:0)
我们可以通过将每个实验运行足够长的时间(理想情况下是无限次)来消除一个额外的循环,例如,每次抛硬币 n=1000 次。现在,每次这样的试验中很可能会出现 5 个正面的序列。如果确实出现,我们可以称该试验为有效试验,否则我们可以拒绝该试验。
最后,我们可以取所需的抛掷次数的平均值 w.r.t.有效试验的次数(根据 LLN,它将近似于预期的投掷次数)。考虑以下代码:
N = 100000 # total number of trials
n = 1000 # long enough sequence of tosses
k = 5 # k heads in a row
ntosses = []
pat = ''.join(['1']*k)
effective_trials = 0
for i in range(N): # num of trials
seq = ''.join(map(str,random.choices(range(2),k=n))) # toss a coin n times (long enough times)
if pat in seq:
ntosses.append(seq.index(pat) + k)
effective_trials += 1
print(effective_trials, sum(ntosses) / effective_trials)
# 100000 62.19919
请注意,如果 n 很小,结果可能不正确,因为它试图近似无限次抛硬币(为了找到连续获得 5 次正面的预期抛硬币次数,n=1000 是可以的,因为实际预期值是 62)。