Watts-College / cpp-527-fall-2021

A course shell for CPP 527 Foundations of Data Science II
https://watts-college.github.io/cpp-527-fall-2021/
2 stars 6 forks source link

Lab 02- Monty hall problem with 5 doors #4

Open AhmedRashwanASU opened 2 years ago

AhmedRashwanASU commented 2 years ago

Good day Prof, @lecy

Small confusion with the 5 doors game Hope below can summarize it.

In this new game set-up, is SWITCH still the dominant strategy for contestants?

I have five doors, and I have to choose one. I already know I have a 1/5 chance of being right. Now the presenter must open Two of the other doors that have One Goat and the other has one car, That means the probability that one of the two doors left is a winner is 2/5, while the probability that the door you have chosen is correct is still 1/5.

So if I stick with my first choice, I have a 1/5 chance of winning. Now, if I decide to change to one of the other remaining doors, knowing that I will have a 2/5 chance of winning (meaning if you were allowed to say you would win 2 out of 5 times Only ).

But I still only get to pick ONE door, and I have 2 remaining doors to choose from, so if I'm going to choose to pick from the remaining 2 doors, which together have a 2/5 chance of winning,

I will again have a 1/3 chance of being right from the selection of these two (since it is equally likely that you'll choose any of these Two)?

AhmedRashwanASU commented 2 years ago

After running the simulation of the 5 doors 10k times the outcome seems strange

table( results.df ) Outcome Strategy LOSE WIN

Stay 5917 4083

Switch 7060 2940

lecy commented 2 years ago

You are getting there but not sure your decision tree is exactly correct.

The chance of selecting a car in the first stage is (2/5) because we have 5 doors and 5 cars. The chance of selecting a goat is (3/5).

[ goat car goat car goat ]

After the first selection is made we open one goat door and one car door. Our game will now be some combination of these three doors (order doesn't matter much on the decision tree, only whether your current pick is a car or a goat):

[ goat goat car ]

Now the second-stage probabilities change. They are no longer completely determined by your strategy (if you stay there is a 100% chance of winning, switch there is a 100% chance of losing).

That is true if you selected the car first, then switch because the only option is a goat.

[ car --> GOAT GOAT ]

But if you select the goat first then when you switch you have a 50% chance of selecting another goat.

[ goat --> CAR GOAT ]

So your chance of success if you SWITCH is going to be:

(chance of ending up on a branch) x (chance of winning the new game)

Here your success rates would be:

SWITCH: (3/5)(1/2) + (2/5)(0/2) = 0.3 STAY: (3/5)(0) + (2/5)(1) = 0.4

That's what your simulation returns, no?


One take-away is that the dominant strategy will not always be the same - it depends on what game you are playing.

If you like this these sorts of problems that involve strategy and payoff you might look into game theory. There are lots of cool applications in public policy and data analytics.


Decision tree for the original game with 3 doors:

image

It's easier to read the tables as probabilities. Note that rows sum to 1, not columns.

# results from lab 01

table( results.df ) %>% 
  prop.table( margin=1 ) %>%  # row proportions
  round( 2 )
##         outcome
## strategy LOSE  WIN
##   stay   0.67 0.33
##   switch 0.33 0.67
AhmedRashwanASU commented 2 years ago

Thank you, Prof, for the detailed explanation, I did follow exactly lab 1 solutions to build the new 5 doors game however not sure what I messed in the code in order to produce the correct proportions, can I share the same here? or to be sent in private

lecy commented 2 years ago

These are solutions from the original game (two goats, one car). Not the 5-door game.

Your results above looked correct. I was just noting an easy way to share as proportions instead of counts (I had to calculate the row proportions myself to check your solution).

table( results.df ) %>% 
  prop.table( margin=1 ) %>%  # row proportions
  round( 2 )
##         outcome
## strategy LOSE  WIN
##   stay   0.67 0.33
##   switch 0.33 0.67
AhmedRashwanASU commented 2 years ago

Great then, I will proceed.

AhmedRashwanASU commented 2 years ago

Good day Prof, @lecy

When Trying to simulate the game of 7 goats and 3 cars, getting the below error while running the simulation code, any advice note that I'm following the code provided in the solutions of Lab 1

Code:

library(dplyr) results.df <- NULL # collector for( i in 1:1000 ) # iterator { game.outcome <- play_game() results.df <- rbind( results.df, game.outcome ) }

Console Output:

Error in sample(x = rep(c("goat", "car"), c(num.goats, num.cars)), size = (num.goats + : argument "num.goats" is missing, with no default

lecy commented 2 years ago

You need to make sure you are using the new functions that allow for larger games and not the original. Check back over the solutions. They should be there.

num.goats <- 7
num.cars <- 3
create_game <- function( num.goats, num.cars )
{
    a.game <- sample( x=rep( c("goat","car"), c(num.goats,num.cars) ), 
                      size=(num.goats+num.cars), replace=F )
    return( a.game )
}

create_game( num.goats=7, num.cars=3 )

Also make sure your play_game() function is passing all of the required variables to the new functions.