Here is a problem: we have a table with 8 trays. With probability $0.5$, there is a letter somewhere in the table. What is the probability that there is a letter in a last tray, given that there is no letter in first 7 trays?
It looks trivial, but now I'm really confused. Here is how I solved it: let $A$ be the probability that there is a letter in a table (so $P(A) = 0.5$); let $B_i$ be the probability that there is a letter in i-th tray; we need the probability $P(B_8 | \overline{B}_{1-7}) = P(B_8|A\overline{B}_{1-7}) * P(A) + P(B_8|\overline{A}\overline{B}_{1-7}) * P(\overline{A})$. $P(B_8|\overline{A}\overline{B}_{1-7})$ is $0$ (because there is no letter in the table at all), and $P(B_8|A\overline{B}_{1-7})$ is $1$, because we know that the letter is in the table and there is no letter in first 7 trays. So $1*0.5 + 0*0.5 = 0.5$.
But then I've tried a simulation:
#!/usr/bin/python
import random
test_n = 100000
table_empty = [False for x in xrange(8)]
suitable = 0
letter_in_last = 0
for i in xrange(test_n):
has_letter = random.random() >= 0.5
table = table_empty[:]
if has_letter:
table[random.randint(0,7)] = True
if any(table[:7]):
continue # there is a letter in 7 first trays
else:
suitable += 1
if table[-1]:
letter_in_last +=1
print "made %s tests" % test_n
print "%s haven't a letter in first 7 trays" % suitable
print "%s have a letter in last tray" % letter_in_last
print "probability: %s" % (1.0 * letter_in_last / suitable)
And results are like this:
made 100000 tests
56291 haven't a letter in first 7 trays
6185 have a letter in last tray
probability: 0.109875468547
Where is my mistake?