Re: Fun With Bayes' Theorem

From: Eliezer S. Yudkowsky (sentience@pobox.com)
Date: Sun May 13 2001 - 22:33:50 MDT


Lee Corbin wrote:
>
> Here is a problem that I left off my original post
> because I thought that it was too similar to one of
> the four. But maybe not. Possibly, it may clarify
> the current discussion.
>
> 5. There are two cards in a hat: one is red on one side
> and blue on the other side. The other is red on both
> sides. You reach into a hat and select one of the
> cards at random, and slap it down on the table. If a
> red surface is now showing, what is the probability
> that the other side is red?

2/3, of course. But just in case, I wrote a computer simulation to test
it, and it's still 2/3. (I just needed a break from thinking in English
for a while.)

**********

# This is the long version of the Python program

from whrandom import randint

red = 'r'
blue = 'b'

class Card:

    def __init__(self, side1, side2):
        self.sides = [side1, side2]

    def randSideUp(self):
        # randint(0, 1) == random int between 0 and 1 inclusive :)
        if randint(0, 1):
            return self.sides
        else:
            return [self.sides[1], self.sides[0]]

class Deck:

    def __init__(self, *cards):
        self.cards = cards

    def randCard(self):
        whichCard = randint(0, len(self.cards) - 1)
        # Python has zero-base arrays
        return self.cards[whichCard]

class Test:

    def __init__(self, deck):
        self.deck = deck
        self.numberRedTop = 0
        self.numberRedBottom = 0

    def tryCard(self):
        card = self.deck.randCard()
        sides = card.randSideUp()
        if sides[0] == red:
            self.numberRedTop = self.numberRedTop + 1
            if sides[1] == red:
                self.numberRedBottom = self.numberRedBottom + 1
                
    def tryCards(self, howMany):
        while howMany > 0:
            howMany = howMany - 1
            self.tryCard()

        percent = self.numberRedBottom * 100.0 / self.numberRedTop
        print "Final result: " + `percent`[0:4]

def main():
    card1 = Card(blue, red)
    card2 = Card(red, red)

    deck = Deck(card1, card2)
    test = Test(deck)
    test.tryCards(10000)

main()

**********************

# This is the pseudo-Perl version

import whrandom

number = 10000
cardtotals = [0, 0]
while number > 0:
    number -= 1
    coin1 = whrandom.randint(0, 1)
    coin2 = whrandom.randint(0, 1)
    if coin1 or coin2:
        cardtotals[coin1] += 1
print `cardtotals[1] * 100.0 / (cardtotals[0] + cardtotals[1])`[0:4]

-- -- -- -- --
Eliezer S. Yudkowsky http://singinst.org/
Research Fellow, Singularity Institute for Artificial Intelligence



This archive was generated by hypermail 2b30 : Mon May 28 2001 - 10:00:05 MDT