Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using R to Compute Probabilities of Matching Birthdays.

Similar presentations


Presentation on theme: "Using R to Compute Probabilities of Matching Birthdays."— Presentation transcript:

1 Using R to Compute Probabilities of Matching Birthdays

2 Bruce E. Trumbo* Eric A. Suess Clayton W. Schupp † California State University, Hayward JSM, August 2004 † Presenting at JSM *

3 Statement of Birthday Problem 25 randomly chosen people in a room What is the probability two or more of them have the same birthday? Model simplifications for a simple combinatorial solution: –Ignore leap years –Assume all 365 days equally likely What error from assumptions?

4 Combinatorial Solution Let X = number of matches Seek P{X ≥ 1} = 1 – P{X = 0} Seek P{X ≥ 1} = 1 – = In R: prod(1 - (0:24)/365) Returns

5 For n People in the Room As n increases, clearly P{X ≥ 1} increases. If n = 366 (still ignoring leap years), we are sure to get at least one duplication. But we will see P{X ≥ 1} becomes very close to 1 for much smaller values of n. Easy to show using a loop in R.

6 R Script for Graph of P{Match} p <- numeric(50) for (n in 1:50) { q <- 1 - (0:(n - 1))/365 p[n] <- 1 - prod(q) } plot(p) # Makes Figure 1 p # Makes prinout below > p [1] [21] [23] [25] [49]

7

8 Simulating the Birthday Process Use R to “survey” from m = rooms, each with n = 25 randomly chosen people. Two convenient functions in R: b <- sample(1:365, n, repl=T) samples 25 objects from among {1, 2, …, 365} with replacement, length(unique(b)) counts the number of unique objects.

9 R Code Simulating the Birthday Process n <- 25; m < ; x <- numeric(m) for (i in 1:m) { b <- sample(1:365, n, repl=T) x[i] <- n - length(unique(b)) } mean(x); mean(x==0) cut <- (0:(max(x) + 1)) hist(x, breaks=cut, freq=F, col=8) > mean(x) [1] > mean(x==0) [1]

10

11 Comments on Simulation Results > mean(x) [1] The proportion of rooms with no matches is very close to P{X = 0} = > mean(x==0) [1] The average number of matches per room simulates E(X), which is not easily obtained by combinatorial methods.

12 How Serious Are Simplifying Assumptions? We hope the 25 people in our room are randomly chosen. (For example, not a convention of twins, or Sagittarians.) We know there are February 29th birthdays and that other birthdays are not uniformly distributed. We hope this does not matter much.

13

14 Simulation Easy to Generalize Define a vector of weights: w <- c(rep(4,200), rep(5,165), 1) More extreme variation than actual. Sample according to these weights: sample(1:366, n, repl=T, prob=w) Slight modification of the R code simulates the nonuniform problem.

15 R Code Simulating the Birthday Process With Weighted Probabilities n <- 25; m < ; x <- numeric(m) w <- c(rep(4,200), rep(5,165), 1) for (i in 1:m) { b <- sample(1:366, n, repl=T, prob=w) x[i] <- n - length(unique(b)) } mean(x); mean(x==0) Uniform Simulation > mean(x) [1] 0.819[1] > mean(x==0) [1] [1]

16 Breakdown in Birthday Problem We have seen that mild departures from the uniform do not change substantially. What about for extreme departures from the uniform? –Assume that half the birthdays are distributed over a period of k weeks (k = 1,..., 26). –Remainder are uniformly distributed over the rest of the year.

17 Simulated Departures From Uniform (Red Line Indicates Uniform Distribution)

18 Concluding Remarks Simulation allows us to assess assumptions. Here the result was that realistic departures from the uniform are relatively harmless. Simulating a case with a known answer builds confidence in the simulation model to be used for generalizing results. Using R, simulations are easy enough to use in practical applications and in the classroom.

19 References Peter Dalgaard: Introductory Statistics with R, 2002, Springer. William Feller: An Introduction to Probability Theory and Its Applications, Vol. 1, 1950 (3rd ed., 1968), Wiley. Thomas. S. Nunnikhoven: A birthday problem solution for nonuniform frequencies, The American Statistician, 46, 270–274, Jim Pitman and Michael Camarri: Limit Distributions and Random Trees Derived From the Birthday Problem With Unequal Probabilities, Electron. J. Probab. 5 Paper 2, 1-18, 2000

20 Web Resources R software and manuals are available from Birth frequencies are based on raw monthly totals are available at vsus.html Auxiliary instructional materials for this article are posted at


Download ppt "Using R to Compute Probabilities of Matching Birthdays."

Similar presentations


Ads by Google