It asks for the probability that strategy 1 results in
goat and strategy 2 results in car when \(N=5\), for which I wrote the following
code:
require(magrittr)
require(dplyr)
# provided game function
game <- function(N){
if(N<3) stop("Must have at least 3 doors")
prize <- sample(c(rep("goat",N-1),"car"), N)
guess <- sample(1:N,1)
game <- data.frame(door = 1:N, prize = prize, stringsAsFactors = FALSE) %>%
mutate(first_guess = case_when(
door == guess ~ 1
, TRUE ~ 0
)) %>%
mutate(potential_reveal = case_when(
first_guess == 1 ~ 0
, prize == "car" ~ 0
, TRUE ~ 1
)) %>%
mutate(reveal = 1*(rank(potential_reveal, ties.method = "random") == 3)) %>%
mutate(potential_switch = case_when(
first_guess == 1 ~ 0
, reveal == 1 ~ 0
, TRUE ~ 1
)) %>%
mutate(switch = 1*(rank(potential_switch, ties.method = "random") == 3))
c(game$prize[game$first_guess == 1], game$prize[game$switch == 1])
}
# setting parameters
num_simulations <- 10000
doors <- 5
# initial count of how many results are have both strategies yield "goat"
strat_1_goat_count <- 0
strat_2_car_count <- 0
# loop to run simulation and add onto the goat_and_car_count counter
for (k in 1:num_simulations) {
result <- game(doors)
if(result[1] == "goat") {
if(result[2] == "goat") {
strat_1_goat_count <- strat_1_goat_count + 1
}
}
if(result[2] == "car") {
strat_2_car_count <- strat_2_car_count + 1
}
}
# finding the probability
probability <- goat_and_car_count / num_simulations
strat_1_probability <- strat_1_goat_count / num_simulations
strat_2_probability <- strat_2_car_count / num_simulations
# print probability
cat('The probability of strategy 1 yielding "goat" is:', strat_1_probability)
cat('The probability of strategy 2 yielding "car" is:', strat_2_probability)
The probabilities are \(0.5343\) and \(0.2677\) respectively
We are given the following,
| Strat 1/Strat 2 \((N = n)\) | Car | Goat |
|---|---|---|
| Car | 0 | \(\frac{1}n\) |
| Goat | \(\frac{n-1}{n(n-2)}\) | \(\frac{(n-1)(n-3)}{n(n-2)}\) |
| Strat 1/Strat 2 \((N = 5)\) | Car | Goat |
|---|---|---|
| Car | 0 | \(\frac{1}4 = 0.20\) |
| Goat | \(\frac{5-1}{5(5-2)} = 0.267\) | \(\frac{(n-1)(n-3)}{n(n-2)} = 0.533\) |
| Strat 1/Strat 2 $(N = n)$ | Car | Goat |
|---------------------------|----------------------|-----------------------------|
| Car | 0 | $\frac{1}n$ |
| Goat | $\frac{n-1}{n(n-2)}$ | $\frac{(n-1)(n-3)}{n(n-2)}$ |
| Strat 1/Strat 2 $(N = 5)$ | Car | Goat |
|------------------------|------------------------|------------------------|
| Car | 0 | $\frac{1}4 = 0.20$ |
| Goat | $\frac{5-1}{5(5-2)} = 0.267$ | $\frac{(n-1)(n-3)}{n(n-2)} = 0.533$ |
And using the following code,
goat_goat_count <- 0
goat_car_count <- 0
car_goat_count <- 0
car_car_count <- 0
sim_count = 10000
for (k in 1:sim_count) {
result <- game(doors)
if(result[1] == "goat") {
if(result[2] == "goat") {
goat_goat_count <- goat_goat_count + 1
}
else {
goat_car_count <- goat_car_count + 1
}
}
else {
if(result[2] == "goat") {
car_goat_count <- car_goat_count + 1
}
else {
car_car_count <- car_car_count + 1
}
}
}
cat(car_car_count, car_goat_count, goat_car_count, goat_goat_count)
cat(car_car_count/sim_count, car_goat_count/sim_count, goat_car_count/sim_count, goat_goat_count/sim_count)
I get this contingency table from running,
| Strat 1/Strat 2 \((N = n)\) | Car | Goat |
|---|---|---|
| Car | 0 | \(0.1972\) |
| Goat | \(0.2604\) | \(0.5424\) |
| Strat 1/Strat 2 $(N = n)$ | Car | Goat |
|---------------------------|----------------------|-----------------------------|
| Car | 0 | $0.2039$ |
| Goat
Given that the relative error is \(0\), \(0.014\), \(0.023\), and \(0.018\) respectively, I’d say it fits the proposed solution well.
More specifically, from Problem 6,
Strategy 1 Absolute Error: \(|0.5343 - 0.533| = 0.0013\) Relative Error \(0.0013/0.533 = 0.002\)
Strategy 2 Absolute Error: \(|0.2677 - 0.267| = 0.0007\) Relative Error \(|0.0007/0.267| = 0.002\)
It asks for the probability that strategy 1 results in
goat and strategy 2 results in car when \(N=5\), for which I wrote the following
code:
require(magrittr)
require(dplyr)
# provided game function
game <- function(N){
if(N<3) stop("Must have at least 3 doors")
prize <- sample(c(rep("goat",N-1),"car"), N)
guess <- sample(1:N,1)
game <- data.frame(door = 1:N, prize = prize, stringsAsFactors = FALSE) %>%
mutate(first_guess = case_when(
door == guess ~ 1
, TRUE ~ 0
)) %>%
mutate(potential_reveal = case_when(
first_guess == 1 ~ 0
, prize == "car" ~ 0
, TRUE ~ 1
)) %>%
mutate(reveal = 1*(rank(potential_reveal, ties.method = "random") == 3)) %>%
mutate(potential_switch = case_when(
first_guess == 1 ~ 0
, reveal == 1 ~ 0
, TRUE ~ 1
)) %>%
mutate(switch = 1*(rank(potential_switch, ties.method = "random") == 3))
c(game$prize[game$first_guess == 1], game$prize[game$switch == 1])
}
# setting parameters
num_simulations <- 10000
doors <- 5
# initial count of how many results are have both strategies yield "goat"
strat_1_goat_count <- 0
strat_2_car_count <- 0
# loop to run simulation and add onto the goat_and_car_count counter
for (k in 1:num_simulations) {
result <- game(doors)
if(result[1] == "goat") {
if(result[2] == "goat") {
strat_1_goat_count <- strat_1_goat_count + 1
}
}
if(result[2] == "car") {
strat_2_car_count <- strat_2_car_count + 1
}
}
# finding the probability
probability <- goat_and_car_count / num_simulations
strat_1_probability <- strat_1_goat_count / num_simulations
strat_2_probability <- strat_2_car_count / num_simulations
# print probability
cat('The probability of strategy 1 yielding "goat" is:', strat_1_probability)
cat('The probability of strategy 2 yielding "car" is:', strat_2_probability)
The probabilities are \(0.5281\) and \(0.2683\) respectively
We are given the following,
| Strat 1/Strat 2 \((N = n)\) | Car | Goat |
|---|---|---|
| Car | 0 | \(\frac{1}n\) |
| Goat | \(\frac{n-1}{n(n-2)}\) | \(\frac{(n-1)(n-3)}{n(n-2)}\) |
| Strat 1/Strat 2 \((N = 5)\) | Car | Goat |
|---|---|---|
| Car | 0 | \(\frac{1}4 = 0.20\) |
| Goat | \(\frac{5-1}{5(5-2)} = 0.267\) | \(\frac{(n-1)(n-3)}{n(n-2)} = 0.533\) |
| Strat 1/Strat 2 $(N = n)$ | Car | Goat |
|---------------------------|----------------------|-----------------------------|
| Car | 0 | $\frac{1}n$ |
| Goat | $\frac{n-1}{n(n-2)}$ | $\frac{(n-1)(n-3)}{n(n-2)}$ |
| Strat 1/Strat 2 $(N = 5)$ | Car | Goat |
|------------------------|------------------------|------------------------|
| Car | 0 | $\frac{1}4 = 0.20$ |
| Goat | $\frac{5-1}{5(5-2)} = 0.267$ | $\frac{(n-1)(n-3)}{n(n-2)} = 0.533$ |
And using the following code,
goat_goat_count <- 0
goat_car_count <- 0
car_goat_count <- 0
car_car_count <- 0
sim_count = 10000
for (k in 1:sim_count) {
result <- game(doors)
if(result[1] == "goat") {
if(result[2] == "goat") {
goat_goat_count <- goat_goat_count + 1
}
else {
goat_car_count <- goat_car_count + 1
}
}
else {
if(result[2] == "goat") {
car_goat_count <- car_goat_count + 1
}
else {
car_car_count <- car_car_count + 1
}
}
}
cat(car_car_count, car_goat_count, goat_car_count, goat_goat_count)
cat(car_car_count/sim_count, car_goat_count/sim_count, goat_car_count/sim_count, goat_goat_count/sim_count)
I get this contingency table from running,
| Strat 1/Strat 2 \((N = n)\) | Car | Goat |
|---|---|---|
| Car | 0 | \(0.2039\) |
| Goat | \(0.2588\) | \(0.5373\) |
| Strat 1/Strat 2 $(N = n)$ | Car | Goat |
|---------------------------|----------------------|-----------------------------|
| Car | 0 | $0.2039$ |
| Goat | $0.2588$ | $0.5373$ |
Given that the relative error is \(0\), \(0.0195\), \(0.031\), and \(0.008\) respectively, I’d say it fits the proposed solution well.
More specifically, from Problem 6,
Strategy 1 Absolute Error: \(|0.5281 - 0.533| = 0.005\) Relative Error \(0.005/0.533 = 0.009\)
Strategy 2 Absolute Error: \(|0.2683 - 0.267| = 0.0013\) Relative Error \(|0.0013/0.267| = 0.005\)