# model the process:
outcomes <- c(-1, 0, 2)
probs <- c(0.25, 0.50, 0.25)
# repeat and store results:
winnings <- sample(outcomes, size = 10, replace = TRUE, prob = probs)
winnings
[1] 0 0 0 -1 0 0 0 2 0 0
(Sections 6.1 and 6.3)
Random Variable
A number whose value depends on the outcome of a chance process.
Random Process | Random Variable \(X\) |
---|---|
selecting three balls | # red balls drawn |
ten years in Florida | number of Category 5 storms that hit |
one year in Florida | total insurance claims filed |
Kentucky Derby 2017 | your winnings ($) |
10 people toss hats | # getting own hat |
What can you expect \(X\) to be, on average, in the long run?
Expected Value of a Random Variable
The long-run average of the values of a random variable, where the underlying chance process is repeated many, many times.
Monte Carlo simulation works here, too!
You are about to play a game: you will flip a fair coin twice.
Let \(W\) be the number of dollars you will win.
\(W\) is a random variable. What is its expected value?
When you flip a fair coin twice, there are four equally likely outcomes:
Therefore:
Distribution of a Random Variable
A statement of the probabilities for a random variable to assume its various possible values.
\(w\) | \(P(W = w)\) |
---|---|
-1 | 0.25 |
0 | 0.5 |
2 | 0.25 |
Back to Monte Carlo simulation. sample()
is still good for this!
# model the process:
outcomes <- c(-1, 0, 2)
probs <- c(0.25, 0.50, 0.25)
# repeat and store results:
winnings <- sample(outcomes, size = 10, replace = TRUE, prob = probs)
winnings
[1] 0 0 0 -1 0 0 0 2 0 0
Parameter prob
specifies the chance of getting each of the elements of the first argument outcomes
This is our estimate of the expected value of \(W\).
How about repeating 10,000 times?
The exact expected value is 0.25.
Suppose you could play the coin game two thousand times. Would you want to do this?
To answer this, consider what your NET winnings might be.
\[2000 \times 0.25 = 500 \text{ dollars}.\]
Simulate playing 2000 times and compute net winnings:
table()
table()
that will tally the number of occurrences of each different element of a vector:
prop.table()
When you give this function a table, it will compute the proportion of times each element occurs:
If you like, you can round off to fewer decimal places with the round()
function:
coin_game_sim <- function(reps, table = FALSE) {
possible_winnings <- c(-1, 0, 2)
probabilities <- c(0.25, 0.50, 0.25)
winnings <- sample(
possible_winnings,
size = reps,
prob = probabilities,
replace = TRUE
)
if (table) {
cat("Here is a table of our the simulated winnings:\n\n")
prop_tab <- prop.table(table(winnings))
print(round(prop_tab, digits = 3))
cat("\n")
}
mean(winnings)
}
Try it out:
Compare to the Distribution of \(W\):
\(w\) | \(P(W = w)\) |
---|---|
-1 | 0.25 |
0 | 0.5 |
2 | 0.25 |
Law of Large Numbers says the two should be close.
\(n\) people toss their hats and scramble.
Let \(X\) be the number who pick up their own hats.
What is the expected value of \(X\)?
hat_toss_sim_ev <- function(n, reps) {
## setup scenario as before:
people <- 1:n
hats <- 1:n
## set up results vector:
results <- logical(reps)
## repeat process reps times:
for (i in 1:reps) {
## toss and scramble:
picked_up_hats <-
sample(
hats, size = n,
replace = FALSE)
## figure how many got their own:
got_own <- sum(people == picked_up_hats)
## store the value:
results[i] <- got_own
}
## report estimated expected value:
mean(results)
}
Try it on one hundred people:
In general, when we use a loop to perform a Monte-Carlo simulation to estimate an expected value, our code will follow this template:
results <- numeric(length = reps_desired)
for (i in 1:reps_desired {
## some code to make the random process happen once
## more code to find out the value that the random variable took on
results[i] <- value_that_the_random_variable_took_on
}
mean(results)