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

Practice Problems Week 2 Q1 Unit Testing #8

Open mtwelker opened 2 years ago

mtwelker commented 2 years ago

I have read Unit Testing section in this week's practice problems over and over and still don't understand what the bug is that we're supposed to be testing. At the very beginning, it says:

We encountered a strange bug in the Monty Hall problem that arises from the behavior of the sample function in these scenarios:

# case a
sample( x=2, size=1, replace=FALSE )  # x turns into 1:2 or c(1,2)
# case b
sample( x=3, size=1, replace=FALSE )  # x turns into c(1,2,3)
# would not matter
sample( x=1, size=1, replace=FALSE )  # x is 1:1 so still just 1  

What is the bug here? Is it that sample( x=3, size=1, replace=FALSE ) produced different results than sample( x=1:3, size=1, replace=FALSE )? Because when I run them, they both seem to return a single number randomly sampled from 1, 2, and 3. I don't understand what the bug is that we're investigating.

As the problem continues, it says :

I have made the conjecture that this bug will result in the wrong door opened in 18% of games played.

Again, I'm not sure what this means. When you say it will result in the "wrong" door opened, do you mean a goat door instead of a car door? Or do you mean a different door than the coder intended?

If this was explained in more detail elsewhere, please let me know where. I've looked through previous notes but can't find anything about this bug. Thanks for any explanation you can offer!

lecy commented 2 years ago

It was detailed in the solutions distributed for lab 1.

It come about when we are trying to open a goat door after the first selection was made. There are two scenarios - the contestant selects a goat door or car door.

When they select a goat door there is only one other door that can be opened.

When they select the car door then we have to randomly pick one of the two goat doors.

Many people implemented one general solution for both cases.

The problem arises when only have one door to select, but we use the sample() function. When a single value is used as the object to sample from it generates the sequence ending in that value.

When 2 is the only possible door to open the correct door returned from the function should be 2, but the function will return 1 or 2 if we use sample.

Similarly with three except it will return 1, 2 or 3.

The error is that the function will open the wrong door. It will happen in 18% of the games, leading to improper estimates of the likely success of both strategies.

does that make sense?

mtwelker commented 2 years ago

Yes, that is very helpful. I thought I had read about this bug before but couldn't find the details. I'll go back and read the solutions again. Thank you!