How to Resolve For Loop Error While Running the Birthday Paradox

94 Views Asked by At
prob_matches = []
num_people = [2,80]
def birthday_sim(num_people , num_sims = 1000):
    
    possible_birthdays = [i for i in range(0,365)]
    for party in range(num_sims):
        birthdays = pd.Series(random.choices(possible_birthdays, k=num_people))
        prob_matches.append('Match') if birthdays.nunique() < num_people else prob_matches.append('No Match')

    return prob_matches.count('Match')/len(prob_matches)

# Test: if 20 people are in a room, what is the approximate probability
# that at least two of them share a birthday?
birthday_sim(num_people = 80)

birthday_problem_df = ['prob_matches', 'num_people']
birthday_problem_df

The problem is to solve the birthday paradox and create a dataframe with one column as number of people in a class and the other problem as probability of sharing a birthday?

The two columns need to be derived from two lists ['prob_matches] and ['num_people'] that are in the defined function. The 'num_people' list refers to the number of people in the class and the 'prob_matches' indicates the odds of people sharing a birthday. My range of people is 2 to 80.

I am wondering what errors I am making in my above code?

1

There are 1 best solutions below

0
Stef On

You need to move this list initialization prob_matches = [] into your function:

def birthday_sim(num_people , num_sims = 1000):
    prob_matches = []
    possible_birthdays = [i for i in range(0,365)]
    for party in range(num_sims):
        birthdays = pd.Series(random.choices(possible_birthdays, k=num_people))
        prob_matches.append('Match') if birthdays.nunique() < num_people else prob_matches.append('No Match')

    return prob_matches.count('Match')/len(prob_matches)

Then you can do

df = pd.DataFrame([[i, birthday_sim(i)] for i in range(2, 81)], columns = ['num_people', 'prob_matches'])

    num_people  prob_matches
0            2         0.003
1            3         0.009
2            4         0.018
3            5         0.021
4            6         0.044
..         ...           ...
74          76         1.000
75          77         1.000
76          78         1.000
77          79         1.000
78          80         1.000