A monkey typing randomly manages to type out a line of Shakespeare. What is the chance that the monkey would produce this particular line?
A monkey typing randomly manages to type out a line of Shakespeare. What is the chance that the monkey would produce this particular line?
Mostly we have been estimating probabilities.
Let's switch gears: estimate the expected value of a random variable.
Recall:
Consider this chance process:
Let \(X\) be the number of numbers you have to pick.
Here is a function to simulate \(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
}
numberNeeded(verbose = TRUE)
Try it a few times.
needed <- numeric(1000)
for (i in 1:1000 ) {
needed[i] <- numberNeeded()
}
cat("The expected number needed is about ",
mean(needed), ".\n", sep = "")
## The expected number needed is about 2.697.
table(needed)
## needed ## 2 3 4 5 6 7 ## 528 303 123 37 8 1
prop.table(table(needed))
## needed ## 2 3 4 5 6 7 ## 0.528 0.303 0.123 0.037 0.008 0.001
This estimates the distribution of \(X\), the number needed.
First, generalize numberNeeded() to handle target-numbers other than 1:
numberNeeded <- function(target) {
mySum <- 0
count <- 0
while( mySum < target ) {
number <- runif(1)
mySum <- mySum + number
count <- count + 1
}
count
}
numberNeededSim <- function(target = 1, reps = 1000,
seed = NULL, report = FALSE) {
if ( !is.null(seed) ) {
set.seed(seed)
}
needed <- numeric(reps) # set up vector for results
for (i in 1:reps ) { # loop to repeat random process
needed[i] <- numberNeeded(target) # store result each time through
}
if ( report ) {
print(prop.table(table(needed)))
cat("\n")
cat("The expected number needed is about ",
mean(needed), ".\n", sep = "")
}
invisible(mean(needed))
}
numberNeededSim(target = 1, reps = 10000, seed = 4848, report = TRUE)
## 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.
It is known that when the target number is 1 the expected number of numbers needed is:
\[\mathrm{e} \approx 2.71828 \ldots\]
Here is the template for a function to estimate a probability via Monte Carlo Simulation:
probEstimation <- function(reps) {
results <- logical(reps) # results will go here
for ( 1 in 1:reps ) {
# simulate the random process,
# getting outcome (TRUE if event happens, FALSE if not)
results[i] <- outcome # store the outcome
}
mean(results) # return estimate of probability of event
}
Here is the template for a function to estimate an expected value via Monte Carlo Simulation:
expValEstimation <- function(reps) {
results <- numerical(reps) # results will go here
for ( 1 in 1:reps ) {
# simulate the random process,
# getting a value for the random variable
results[i] <- value # store the value
}
mean(results) # return estimate of the expected value
}
Let's cover this from the textbook.
https://homerhanumat.github.io/r-notes/appeals-court-paradox.html
https://homerhanumat.github.io/r-notes/drunken-turtle-sim.html