-
Notifications
You must be signed in to change notification settings - Fork 0
/
first_dd.Rmd
76 lines (56 loc) · 2.51 KB
/
first_dd.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
---
title: "Description of the Birthday Problem"
author: "Fernando Hoces de la Guardia"
date: "9/3/2021"
output:
pdf_document: default
word_document: default
html_document: default
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
set.seed(1234)
n.pers = 16
n.sims = 10000
```
# Description of the problem
What is the probability that at least two people this room share the same birthday?
There are `r n.pers` in this room.
Is it something like $\frac{`r n.pers`}{365} = `r n.pers/365`$
# The mathematical solution
Define p(n) as the probability that at least one pair has the same birthday, then the 1-p(n) is the probability that all are born in a different day. Which we can compute as:
\begin{align}
1 - p(n) &= 1 \times \left(1-\frac{1}{365}\right) \times
\left(1-\frac{2}{365}\right) \times \cdots \times
\left(1-\frac{n-1}{365}\right) \nonumber \\
&= \frac{365 \times 364 \times \cdots \times (365-n+1) }{365^n} \nonumber \\
&= \frac{ 365! }{ 365^n (365-n)!} = \frac{n!\cdot\binom{365}{n}}{365^n}\\
p(n= `r n.pers`) &= `r round(1 - factorial(n.pers) *
choose(365,n.pers)/ 365^n.pers, 3)` \nonumber
\end{align}
# The simulated solution
We will simulate the probability:
1 - Simulate 10^{4} rooms with `r n.pers` random birthdays, and store the results in matrix where each row represents a room.
2 - For each room (row) compute the number of unique birthdays.
3 - Compute the average number of times a room has `r n.pers` unique birthdays, across 10^{4} simulations, and report the complement.
```{r}
birthday.prob = function(n.pers_var, n.sims_var) {
# simulate birthdays
birthdays = matrix(round(runif(n = n.pers_var * n.sims_var, min = 1, max = 365) ),
nrow = n.sims_var, ncol = n.pers_var)
# for each room (row) get unique birthdays
unique.birthdays = apply(birthdays, 1,
function(x) length( unique(x) ) )
# Indicator with 1 if all are unique birthdays
all.different = 1 * (unique.birthdays==n.pers_var)
# Compute average time all have different birthdays
result = 1 - mean(all.different)
return(result)
}
bp_sim = birthday.prob(n.pers_var = 21, n.sims_var = 10000)
print(bp_sim)
```
# Results
- Many people think the solution is $\frac{1}{365} \times n = `r n.pers/365`$
- The math says: `r round(1 - factorial(n.pers) * choose(365,n.pers)/ 365^n.pers, 3)`
- A simulation with `r n.sims` rooms with `r n.pers` people in each room, says: `r bp_sim`