GreyMamba

Thinking Allowed … (under construction)

Thinking Allowed … (under construction)

A computer once beat me at chess, but it was no match for me at kick boxing.
Emo Philips
Snippets covering IT techniques, Python coding and Rapidweaver
Just a section to cover anything related to 'Information Technology'. Computers, Coding, Algorithms, A.I. and so on. There will probably be quite a bit on how to make a web site (one of the reasons I'm actually doing this is to learn how) including much on Rapidweaver and Stacks. There will be code - most likely Python along with general computery items.

I'm quite into Python programming and as such I've written a bit of an interactive Python booklet (link opens in new window) covering some of the stuff needed to program in it.

It's probably appropriate to mention here that I've used RapidWeaver 8 to help me generate this website. I've also made use of the excellent Free DNS service to overcome the limitations imposed by not having a static IP address.

Python, Bayes and Monte Carlo

Sometimes you just can't believe what you've calculated and you fancy a bit of corroboration. In the 'Maths and Stats' section I introduce Bayes' rule and one of the examples I use looks at the probability of a 2 child family having 2 Boys - with some additional information. Here's the 'problem': A family has two children. Given that one of the children is a boy and that he was born on a Tuesday, what is the probability that both of the children are boys?. The Tuesday bit seems irrelevant but it turns out it isn't.

At first glance you might think that the Tuesday bit is irrelevant - I did - but you would be wrong believe it or not. If we ignore the day-of-the-week information it is pretty easy to show that the probability of the 2 children being boys, given that at least one of them is, is \(1 \over 3\). Using Bayes, if we include 'Tuesday' we actually get a probability of \(13 \over 27\) which is much closer to \(1 \over 2\) than the original \( 1 \over 3\)

This really did seem ridiculous to me - why should naming a day have such an effect? So I wrote a simple, uncommented (and pretty cludgy - don't look at it too hard) piece of Python code to check it out using a Monte Carlo simulation technique using 10,000 iterations. A Monte Carlo simulation basically runs a simulation many, many times over and then averages the results to get an idea of what might happen.

Here's the code:
# Monte Carlo simulation to verify a Bayes Theory result from random import randint BOYREPS=10000 # Returns a 'day' number def getDay(): return randint(1,7) # Returns a 'sex' identifier, 0=boy, 1=girl def getSex(): return randint(0,1) # Returns number of boys in family, of which at least 1 boy was born on a Tuesday # We ignore all girls, and any number of boys not born on a Tuesday def getChild(): boys=0 boyTueOK= False # keep trying until at least 1 boy is born on a Tuesday while not boyTueOK: boys=0 # Repeat for 2 children for i in range(2): day=getDay() sex=getSex() if sex==0: #This one's a boy boys += 1 # Add to the total for this pair if day==2: boyTueOK = True # and we have at least 1 boy born on a Tuesday return boys # The main routine. def main(): oneBoy=0 twoBoy=0 count=0 while count < BOYREPS: if getChild()==1: oneBoy += 1 else: twoBoy += 1 count += 1 print (oneBoy) print (twoBoy) if __name__ == '__main__': main()

As I'm sure you'll agree, pretty shoddy coding but it does work and the last time I ran it gave the incidence of 2 boys, one at least born on a Tuesday, as something like \(4800 \over 10000\) which is remarkabley close to \(13 \over 27\). If you run the code your result will be very slightly different of course - it is a series of 10,000 random 'runs'.

So, Bayes and Mathematics 1 : Common sense 0.

Back
 

Nice photo from an unusual perspective. Kudos to Joshua Sortino

RapidWeaver Icon

Made in RapidWeaver