(Sections 6.5 - 6.7)
What is the chance that they meet?
The random process is:
From 10 to 11am, watch for Anna and Raj enter/leave the coffeeshop.
Modeling involves getting Anna and Raj’s arrival times (in minutes after 10am):
Anna and Raj will connect if their arrival times are no more than 10 minutes apart.
That is, they connect if and only if
\[-10 < anna - raj < 10,\]
which is the same as
\[\vert\, anna - raj \,\vert < 10.\]
anna raj
12.58343 28.44067
[1] -15.85724
[1] 15.85724
They do not connect.
runif()
can generate many values. Let’s use this fact to repeat eight times.
anna | raj | arrival_diff | do_they_meet |
---|---|---|---|
12.583430 | 31.10838 | 18.524952 | FALSE |
28.440668 | 43.97309 | 15.532418 | FALSE |
8.108124 | 47.08549 | 38.977363 | FALSE |
29.213319 | 40.77307 | 11.559753 | FALSE |
35.928174 | 32.72790 | 3.200269 | TRUE |
47.170376 | 53.82384 | 6.653462 | TRUE |
56.500402 | 38.61780 | 17.882606 | FALSE |
19.749677 | 25.83640 | 6.086727 | TRUE |
meetupSim <- function(reps = 10000, table = FALSE,
seed = NULL) {
if ( !is.null(seed) ) {
set.seed(seed)
}
anna <- runif(reps, 0, 60)
raj <- runif(reps, 0, 60)
connect <- (abs(anna - raj) < 10)
if ( table ) {
cat("Here is a table of the results:\n\n")
print(table(connect))
cat("\n")
}
cat(
"The proportion of times they met was ",
mean(connect), ".\n", sep = ""
)
}
meetupSim2 <- function(reps = 10000, table = FALSE,
seed = NULL) {
if ( !is.null(seed) ) {
set.seed(seed)
}
# set up vector to hold results
connect <- logical(reps)
# loop to repeat random process
for ( i in 1:reps ) {
# get one arrival time for anna
anna <- runif(1, 0, 60)
# and a time for raj
raj <- runif(1, 0, 60)
they_meet <- abs(anna - raj) < 10
# store in results vector:
connect[i] <- they_meet
}
if ( table ) {
cat("Here is a table of the results:\n\n")
print(table(connect))
cat("\n")
}
cat(
"The proportion of times they met was ",
mean(connect), ".\n", sep = ""
)
}
system.time()
records time to perform a task.
Avoid looping if you can repeat with vectorization.
(But it doesn’t matter much, if your job is “small”.)
What’s the chance they will vote the correct way?
Set up the scenario in R:
Imagine (for now) that they they hear seven cases:
b <- sample(
c(0, 1),
size = reps, prob = c(1 - bProb, bProb),
replace = TRUE
) # Judge B
c <- sample(
c(0, 1),
size = reps, prob = c(1 - cProb, cProb),
replace = TRUE
) # Judge C
d <- sample(
c(0, 1),
size = reps, prob = c(1 - dProb, dProb),
replace = TRUE
) # Judge D
e <- sample(
c(0, 1),
size = reps, prob = c(1 - eProb, eProb),
replace = TRUE
) # Judge E
Add to find the number of correct votes in each case:
Now we can find whether they got each case right or wrong:
A | B | C | D | E | sum | correct |
---|---|---|---|---|---|---|
1 | 1 | 1 | 1 | 1 | 5 | TRUE |
1 | 1 | 1 | 1 | 0 | 4 | TRUE |
1 | 1 | 1 | 1 | 1 | 5 | TRUE |
1 | 1 | 1 | 1 | 1 | 5 | TRUE |
0 | 1 | 1 | 1 | 0 | 3 | TRUE |
1 | 1 | 0 | 1 | 1 | 4 | TRUE |
1 | 1 | 1 | 1 | 1 | 5 | TRUE |
courtSim <- function(reps,
probs = c(0.95, 0.94, 0.90, 0.90, 0.80),
seed = NULL) {
if (!is.null(seed)) {
set.seed(seed)
}
# get the probabilities
aProb <- probs[1]
bProb <- probs[2]
cProb <- probs[3]
dProb <- probs[4]
eProb <- probs[5]
# simulate decisions of each judge:
a <- sample(
c(0, 1),
size = reps, prob = c(1 - aProb, aProb),
replace = TRUE
) # Judge A
b <- sample(
c(0, 1),
size = reps, prob = c(1 - bProb, bProb),
replace = TRUE
) # Judge B
c <- sample(
c(0, 1),
size = reps, prob = c(1 - cProb, cProb),
replace = TRUE
) # Judge C
d <- sample(
c(0, 1),
size = reps, prob = c(1 - dProb, dProb),
replace = TRUE
) # Judge D
e <- sample(
c(0, 1),
size = reps, prob = c(1 - eProb, eProb),
replace = TRUE
) # Judge E
# count the number of correct votes in each case:
correctVotes <- a + b + c + d + e
# determine whether court decided correctly, in each case:
courtCorrect <- (correctVotes >= 3)
# report estimate of probability
mean(courtCorrect)
}
Suppose the judges convince Judge E (the weakest one) to always vote the same as Judge A (the best one) does.
Will this increase the court’s chance of making the correct decision?
courtSim2 <- function(reps = 10000,
probs = c(0.95, 0.94, 0.90, 0.90),
seed = NULL) {
if ( !is.null(seed) ) {
set.seed(seed)
}
# get the probabilities
aProb <- probs[1]
bProb <- probs[2]
cProb <- probs[3]
dProb <- probs[4]
# simulate decisions:
a <- sample(
c(0, 1),
size = reps, prob = c(1 - aProb, aProb),
replace = TRUE
) # Judge A
b <- sample(
c(0, 1),
size = reps, prob = c(1 - bProb, bProb),
replace = TRUE
) # Judge B
c <- sample(
c(0, 1),
size = reps, prob = c(1 - cProb, cProb),
replace = TRUE
) # Judge C
d <- sample(
c(0, 1),
size = reps, prob = c(1 - dProb, dProb),
replace = TRUE
) # Judge D
# count the number of correct votes in each case:
correctVotes <- 2*a + b + c + d
# determine whether court decided correctly, in each case:
courtCorrect <- (correctVotes >= 3)
# report estimate of probability
mean(courtCorrect)
}
Interesting:
Kicking out the worst judge made the court not as good!
Consider this chance process:
Let \(X\) be the number of numbers you have to pick.
Here is a function to simulate one “try” of \(X\):
numberNeeded <- function(verbose = FALSE) {
mySum <- 0
count <- 0
while (mySum < 1) {
number <- runif(1)
mySum <- mySum + number
count <- count + 1
if (verbose) {
cat("Just now picked ", number, ".\n", sep = "")
cat(count, " number(s) picked so far.\n", sep = "")
cat("Their sum is ", mySum, ".\n\n", sep = "")
}
}
count
}
Try it a few times.
This estimates the distribution of \(X\), the number needed.
First, revise the function numberNeeded()
to handle any target number.
Now we can make a good simulation fuction:
numberNeededSim <- function(target = 1, reps = 1000,
seed = NULL, report = FALSE) {
if (!is.null(seed)) {
set.seed(seed)
}
needed <- numeric(reps)
for (i in 1:reps) {
needed[i] <- numberNeeded(target)
}
if (report) {
print(prop.table(table(needed)))
cat("\n")
cat("The expected number needed is about ",
mean(needed), ".\n",
sep = ""
)
}
mean(needed)
}
needed
2 3 4 5 6 7 8
0.4937 0.3350 0.1313 0.0317 0.0071 0.0010 0.0002
The expected number needed is about 2.7273.
[1] 2.7273
It is known that when the target number is 1 the expected number of numbers needed is:
\[\mathrm{e} \approx 2.71828 \ldots\]