如何修改生日问题来回答房间内有3、4、5或更多人拥有相同生日的概率

问题描述 投票:0回答:1

生日问题询问一个房间里需要有多少人才能有大于 50% 的机会至少有两个人生日相同。我使用下面的 Python 代码来求解至少两个人生日相同的概率。我想修改代码,以便我可以找到 3 个或更多、4 个或更多(等等)人生日相同的概率。

我尝试将变量 c 更改为下面的 3,但没有成功

import random
import numpy as np
from math import factorial

round = 3    # precision of random numbers
n=10**round  # Number of possible permutations of the random numbers
k=50       # Number of samples in the analysis
c=2          # a constant represents every possible pairs (2) evaluated

# This is an empirical calculation of duplicated values
rvs = np.round(np.random.uniform(0,1,k), round)
unique=np.size(np.unique(rvs))
dups = (k-unique)


possible_combinations=n*(n-1)/2
print('n=', n)
print('k=', k)
print('Possible Pair Combinations = ', "{:,.0f}".format(possible_combinations))
print('Number of duplicate random values=', dups,'of ' +str(k) +' samples')

# Calculate probabilities (Method 1)
P_coeff=(k*(k-1)/c)
P_A = ((n-1)/n)**P_coeff
P_B =1-P_A

# Calculate probabilities (Method 2)
P_B2 = 1 - (factorial(n) / (factorial(n-k) * n**k))

print('P(A)=', "{:,.4f}".format(P_A))
print('P(B)=', "{:,.4f}".format(P_B)+ ' Method 1 - Probability of duplicated values')
print('P(B)=', "{:,.4f}".format(P_B2)+ ' Method 2 - Probability of duplicated values')
probability birthday-paradox
1个回答
0
投票

我终于找到了使用泊松分布的问题的答案:https://math.stackexchange.com/questions/25876/probability-of-3-people-in-a-room-of-30-having -同一生日#:~:text=然后%20这个%20近似%20给%20

我编写并测试了 Python 代码,用于回答 2、3、4(等)个生日相同的人的生日问题:

# Solve for k people having the same birthday in a room with n people in it

from scipy.stats import poisson

# INPUTS
n=23      # Number of people in a room
k=2       # Number of people to evaluate for same birthday

# CALCULATIONS
k_=k-1     # k actuall need to be (k-1) assuming 1 person as the "seed"
b=365      # Days in a year
n_b=n/b    # mu for Poisson distribution

F_k = poisson.cdf(k_, n_b, loc=0)
F_k = F_k**b
P_k = 1 - F_k

print('Mu for Poisson CDF=',"{:,.4f}".format(n/b))
print('Poisson Prob of not having k birthdays=',"{:,.4f}".format(F_k))
print('Probability of k people in with birdays',"{:,.4f}".format(P_k))

答案: 泊松 CDF 的 Mu = 0.0630 没有 k 个生日的泊松概率= 0.4988 k 个人生日的概率 0.5012

© www.soinside.com 2019 - 2024. All rights reserved.