The problem was this:
You have three cards, one card is black on both sides, one card is white on both sides, and the last card is black on one side and white on one side. If you draw a card that is white on the front, what is the probability that it is whit on the other side?
The answer seems obvious, 50%, right? Or maybe not. Let's write a short program to test the hypothesis. Scala is the natural choice, of course:
object CardSimulation {
import util.Random
def main(args: Array[String]) {
val stack = Array(new Card(Face.White, Face.White), new Card(Face.White, Face.Black), new Card(Face.Black, Face.Black))
println("Welcome to the card simulation")
print("Enter number of times to pick a card> ")
while (true) {
try {
val line = Console.in.readLine
if (line == "quit") exit()
var tries: Int = line.toInt
var results = List[Card]()
for (x <- 0 until tries) {
val cardNumber = Random.nextInt(3)
val facingUp = Random.nextBoolean()
val card = new Card(stack(cardNumber).front, stack(cardNumber).back, facingUp)
results = card :: results
if (card.upSide == Face.White) print("w")
else print("b")
}
println("\n skipped " + results.count(_.upSide == Face.Black) + " black cards")
val totalWhites = results.count(_.upSide == Face.White)
println(" using " + totalWhites + " white cards")
val otherSideWhites = results.filter(_.upSide == Face.White).count(_.otherSide == Face.White)
println(" of which " + otherSideWhites + " had a white back side, or " + (100D*otherSideWhites/totalWhites) + "%")
println(" ok")
print("> ")
} catch {
case ex: Exception => println("Enter an integer or type quit to exit")
}
}
}
object Face extends Enumeration {
val White = Value
val Black = Value
}
class Card(val front: Face.Value, val back: Face.Value, var faceUp:Boolean = true) {
def upSide: Face.Value = {
if (faceUp) front
else back
}
def otherSide: Face.Value = {
if (faceUp) back
else front
}
}
}A sample run looks like this:
Welcome to the card simulation
Enter number of times to pick a card> 55
bwwbbwwwbwwwbwwbbwbbbbbwbbwbbwwwbwbbbwwwbwbbwbwbwwbbwbb
skipped 29 black cards
using 26 white cards
of which 18 had a white back side, or 69.23076923076923%
ok
> 55
wwwbbwbwwwwbwbwwbbbbwbwbwwwbwbbbbwbwwbwbbbbwbwwwbbbbbbb
skipped 30 black cards
using 25 white cards
of which 18 had a white back side, or 72.0%
ok
> 55
wbwwwwwbwbwbbbbwbwwwbbbbbbbbbbbbwbbwbbbwwwwbwwwbwbbwbww
skipped 30 black cards
using 25 white cards
of which 15 had a white back side, or 60.0%
ok
>
Hm, either I'm extremely lucky, or something is a bit fishy... Lets see, with three cards with two sides each we get 6 possible outcomes, 3 outcomes are black and the problem states that we have a white card, so 3 outcomes remains. 3 outcomes, I think we're on to something. The possibilities are (1)white front of card 1, (2)white back of card 1, and (3)white front of card 2.
Notice that (1) and (2) are white on the other side, and only (3) is black on the other side. I.e. the probability that the color of the other side is 2/3. This explains the output of our test program. Not so difficult to understand once you know you need to go a little bit deeper than just going by first impressions.
The take away, at least for me, is that probability is deceptive, and it is easy to assume wrong, even when it's fairly easy to reason your way to a correct answer.
No comments:
Post a Comment