-
Notifications
You must be signed in to change notification settings - Fork 20
/
first_dd_test.Rmd
65 lines (44 loc) · 1.92 KB
/
first_dd_test.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
---
title: "Ther birthday problem"
author: "Fernando Hoces de la Guardia"
date: "9/13/2019"
output: html_document
---
# A Description of the birthday ploblem
sdfsdf
sdfsddf
sdfsdsd
```{r}
n.pers = 20
set.seed(1234)
```
There are `r n.pers` people in this room.
## Analytical solution
But actually when we compute the math. We get an surprising result:
\begin{align}
1 - \bar 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}
## Simulations
1 - Simulate 10,000 rooms with n = `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,000 simulations, and report the complement.
```{r}
birthday.prob = function(n.pers, n.sims) {
# simulate birthdays
birthdays = matrix(round(runif(n.pers * n.sims, 1,
365)), nrow = n.sims,
ncol = n.pers)
# for each room (row) get unique birthdays
unique.birthdays = apply(birthdays, 1, unique)
# Indicator with 1 if all are unique birthdays
all.different = (lapply(unique.birthdays, length) == n.pers) # Compute average time all have different birthdays
result = 1 - mean(all.different)
return(result)
}
n.pers.param = n.pers
n.sims.param = 1e4
result_num = birthday.prob(n.pers.param,n.sims.param)
result_num
```
Ok, I am convinced that the probability of atl least two people in this room share the same birthday is `r round( result_num, 2 )`