>>> import random >>> def pickbag(): bag = random.randrange(1,4) if bag == 1: return ['W','B'] #return a bag with a white and a black marble elif bag == 2: return ['W', 'W'] else: return ['B', 'B'] >>> def pickmarble(bag): return random.choice(bag) #pick a random marble from the given bag >>> def seeiftheothermarbleiswhite(): bag = pickbag() marble = pickmarble(bag) if marble == 'W': if bag == ['W','W']: return True # First Marble AND second marble white else: return False # Only First Marble White else: return None #First marble is not white, aborting
Now, lets do the experiment 1 00 000 times and Mark the cases as Yes when the other marble are white, No when only the First marble is white, Other when the first is not white.
>>> Yes = 0 >>> No = 0 >>> Other = 0 >>> for i in xrange(100000): a = seeiftheothermarbleiswhite() if a: Yes += 1 elif a == False: No += 1 else: Other += 1
Now, since we are dealing only with cases when the first marble is white:
>>> Yes 33351 >>> No 16533 >>> Other 50116
>>> Yes/float(Yes + No) 0.6685710849170075
Now, that is very close to 2/3 and the rest is experimental error
'And fun? If maths is fun, then getting a tooth extraction is fun. A viral infection is fun. Rabies shots are fun.'
'God exists because Mathematics is consistent, and the devil exists because we cannot prove it'
'Humanity is still kept intact. It remains within.' -Alokananda
Let's say I originally picked door 1 then door 2 was opened showing the goat so we want to calculate probability of car behind door 1 given that door 2 has a goat and probability of car behind door 3 given that door 2 has a goat.
The flaw in your problem definition is the condition "given that door 2 has a goat", it should be "given Monty opens Door 2"
If we call the 'probability that Monty opens Door 2', p(g2), then:
p(g2) = (1/3*1) + (1/3 * 1/2) + (1/3 * 0) = 1/2 and
p(g2)|(c1) = 1/2 and p(g2)|(c3) = 1
Plug those values into Bayes and you get 1/3 and 2/3