{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"### The Birthday Problem\n",
"\n",
"Suppose that $15$ people are gathered in a room. \n",
"\n",
"What are the odds that $2$ people in the room share the same birthday?\n",
"\n",
"Estimate the probability that two out of $N$ people in a room share the same birthday, it is guaranteed that $2\\leq\\N\\leq30$\n",
"\n",
"The function $sameBirthday$ takes one parameter, $N$ which is the number of people gathered in a room. An empty vector $peoplesBirthday$ is appended to add $N$ number of random integers from $1\\ to\\ 365$ (with replacement), which is the number of days of the year in which an individual's birthday can fall on, assuming we are not including leap years.\n",
"\n",
"The function then compares each value in the vector. If any two or more of the values match (i.e, a 'shared birthday'), the boolean value $sharedBirthday$ returns $true$, else, it returns $false$.\n",
"\n",
"The function $probabilitySameBirthday$ takes two parameters, the number of repeated $trials$ which will be conducted (the larger this number is the more accurate the estimated probability will be), and the number of people $N$ in the room. Each loop calls upon the function $sameBirthday$. If $sameBirthday$ has returned $true$, a counter variable increases by $1$. The function then returns the cumulative $probability$ which is calculated as the $\\frac{number\\ of \\ succcessful\\ trials\\ (counter)}{total\\ number\\ of\\ trials}$.\n",
"\n",
"I have estimated the probability of $2$ or more out of $15$ people sharing a birthday is $\\approx{0.01042}\\ or\\ \\approx{1\\%}$."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.01053"
]
},
"execution_count": 4,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"import random\n",
"\n",
"\n",
"def sameBirthday(N):\n",
" \n",
" sharedBirthday=False\n",
" peoplesBirthday=[]\n",
" \n",
" for i in range(N):\n",
" peoplesBirthday.append(np.random.randint(1, 365))\n",
" \n",
"# print(peoplesBirthday)\n",
" \n",
" for i in range(N):\n",
" if peoplesBirthday.count(i)>1:\n",
" sharedBirthday=True\n",
" \n",
" return sharedBirthday\n",
" \n",
"def probabilitySameBirthday(trials, N):\n",
" counter=0\n",
" for i in range(trials):\n",
" if sameBirthday(N):\n",
" counter+=1\n",
" probability=(counter/trials)\n",
" return probability\n",
"\n",
"probabilitySameBirthday(100000, 15)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (system-wide)",
"language": "python",
"metadata": {
"cocalc": {
"description": "Python 3 programming language",
"priority": 100,
"url": "https://www.python.org/"
}
},
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}