在这种情况下,基于概率的分析解决方案比尝试模拟更容易、更准确。我同意 Harshvardhan 的观点,即您的表述解决了错误的问题。
n 池中至少有一个人在特定目标日期生日的概率是1-P{all n miss the target date}。当P{all n miss the target date} < 0.3 时,这个概率至少为 0.7。假设每个人错过目标的概率为P{miss} = 1-1/365(每年 365 天,所有生日的可能性相同)。如果个人生日是独立的,那么P{all n miss the target date} = P{miss}^n。
我不是 R 程序员,但以下 Ruby 应该很容易翻译:
# Use rationals to avoid cumulative float errors.
# Makes it slower but accurate.
P_MISS_TARGET = 1 - 1/365r
p_all_miss = P_MISS_TARGET
threshold = 3r / 10 # seeking P{all miss target} < 0.3
n = 1
while p_all_miss > threshold
p_all_miss *= P_MISS_TARGET
n += 1
end
puts "With #{n} people, the probability all miss is #{p_all_miss.to_f}"
产生:
439 人,所有未命中的概率为 0.29987476838793214
附录
我很好奇,因为我的答案与公认的不同,所以我写了一个小模拟。同样,我认为即使它不在 R 中,也很容易理解:
require 'quickstats' # Stats "gem" available from rubygems.org
def trial
n = 1
# Keep adding people to the count until one of them hits the target
n += 1 while rand(1..365) != 365
return n
end
def quantile(percentile = 0.7, number_of_trials = 1_000)
# Create an array containing results from specified number of trials.
# Defaults to 1000 trials
counts = Array.new(number_of_trials) { trial }
# Sort the array and determine the empirical target percentile.
# Defaults to 70th percentile
return counts.sort[(percentile * number_of_trials).to_i]
end
# Tally the statistics of 100 quantiles and report results,
# including margin of error, formatted to 3 decimal places.
stats = QuickStats.new
100.times { stats.new_obs(quantile) }
puts "#{"%.3f" % stats.avg}+/-#{"%.3f" % (1.96*stats.std_err)}"
五次运行产生如下输出:
440.120+/-3.336
440.650+/-3.495
435.820+/-3.558
439.500+/-3.738
442.290+/-3.909
这与之前得出的分析结果非常一致,并且似乎与其他响应者的答案存在显着差异。
请注意,在我的机器上,模拟所需的时间大约是解析计算的 40 倍,更复杂,并且引入了不确定性。为了提高精度,您需要更大的样本量,因此需要更长的运行时间。考虑到这些因素,我会重申我的建议,即在这种情况下采用直接解决方案。