Friday, November 18, 2011

Math, Boys and Girls

Our CTO posted an interesting question on our Yammer feed yesterday:
In a mythical land, parents keep having children until a boy is born, and then they stop.
Without doing the math, does this result in more girls than boys on average, or more boys than girls?
Doing the math?  Man, I haven't "done the math" since school.  And there's a lot that I didn't retain, so I can't say for certain if I ever did this.  But one thing's for sure... I never approached math from this angle back in school.  You remember how it was, right?  There's a textbook, there are problems, you solve them, you're done.  Nothing really thought-provoking like this, at least for us non-math-majors.

So I couldn't really think of an equation or anything to approximate this.  Looking back at some of the responses now, it seems kind of obvious.  It's just not how I've approached problems in the past, so it's not how I think.  No, how I think is to write a program to simulate it:

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace RockysQuestion
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                new Family();
                Console.WriteLine(
                    string.Format(
                        "Total Boys: {0} | Total Girls: {1}",
                        Globals.TotalBoys,
                        Globals.TotalGirls));
            }
        }
    }

    static class Globals
    {
        public static int TotalBoys { get; set; }
        public static int TotalGirls { get; set; }
    }

    class Family
    {
        private static Random _rand = new Random();
        private Task _reproducing;
        
        public int Boys { get; set; }
        public int Girls { get; set; }

        public Family()
        {
            Boys = 0;
            Girls = 0;

            _reproducing = Task.Factory.StartNew(Reproduce);
        }

        private void Reproduce()
        {
            while (Boys == 0)
                HaveBaby();
            Globals.TotalBoys += Boys;
            Globals.TotalGirls += Girls;
        }

        private void HaveBaby()
        {
            if (_rand.Next(2) == 0)
                Boys++;
            else
                Girls++;
        }
    }
}

Watching the output, it would appear to be a 50/50 split with statistically acceptable error.  Note that I'm not 100% sure how thread-safe these lines are:

Globals.TotalBoys += Boys;
Globals.TotalGirls += Girls;

So until I do some more detailed analysis of the code it's possible that some of the child counts may be lost.  But the numbers quickly become large enough that this effect also likely becomes statistically insignificant.

As I watch the numbers tick by, they seem to be neck-and-neck.  Sometimes the boys are ahead, sometimes the girls are ahead.  I'm sure I can make a better visualization of it, but I wanted to keep it as a simple console output for this post.  It looks like one gender can overtake the other by a significant margin for a significant amount of time, but if I re-start then it's just as likely that the other gender will do the same.  So testing seems to indicate that it's "close enough."

Of course, this is an over-simplification of the real-world implications.  Would a family with 4 girls continue to reproduce?  10 girls?  What if there are twins?  Does this account for the overall population of the species (mortality rates, etc.) or just the birth rate?  The question is just about the math, really.  It's not about the real-world scenario.  It is, after all, a mythical land.  I'm sure all of these families also live in perfect spheres resting on frictionless planes.

As the discussion went on, people naturally started to do the math.  After all, we're geeks.

Kevin presented a point indicating that it should be 50/50:
In 50% of the cases there will be one boy and no girls
In 25% of the cases there will be one boy and one girl
In 12.5% of the cases there will be one boy and two girls
In 6.25% of the cases there will be one boy and three girls
In 3.125% of the cases there will be one boy and three girls
Extending into infinity

Each iteration with more girls is less and less likely and offsets the 50% chance of having one boy and no girls. The case that they have ten girls before they have a boy has a probability of about 0.05%
He later continued:
Assuming that infinite couples are able to have kids forever until they have a boy, the ratio will be 1:1. Anything less than an infinite number of couples being able to keep having kids forever will necessarily result in slightly more boys than girls and there will never be more girls than boys (statistically, outliers aside). You have to get back to the idea of a fair coin toss in statistics to understand why this is so.

So let’s run through a few examples. Let's say we have 100 couples, what happens? (Using normal rounding rules)
50 couples will have one boy, no girls
25 couples will have one boy, one girl
13 couples will have one boy, two girls
6 couples will have one boy, three girls
3 couples will have one boy, four girls
2 couples will have one boy, five girls
1 couple will have one boy, six girls.

This is a total of 100 boys (always equal to the number of couples) and 97 girls or a ratio of 1:.97.

So how about 500 couples?
250 couples will have one boy, no girls
125 couples will have one boy, one girl
63 couples will have one boy, two girls
31 couples will have one boy, three girls
16 couples will have one boy, four girls
8 couples will have one boy, five girls
4 couples will have one boy, six girls
2 couples will have one boy, seven girls
1 couple will have one boy, eight girls

This is a total of 500 boys and 494 girls or a ratio of 1:.988. Closer to 1:1 but not there yet.

The key to understanding what is happening is that for every extra girl that is born in the progression, the likelihood of it happening is halved. As you stretch out into more and more couples having the chance to have more and more children the ratio will keep getting closer and closer in a straight progression but never quite getting to 1:1 until you reach out into infinity.
Eric offered another way to look at the numbers:
It might help to look at the problem from the perspective of the kids. If we were to divide theme up into groups by their birth order, 1st children, 2nd children, and so on we know that each group has a 50/50 split. These groups also form a complete partition of the set of kids, no child is in more than one group and every child is in a group. As long as we don’t have infinite kids, that is enough to show that there are equal numbers.
This all made a lot of sense.  Statistically, it's 50/50 within an acceptable margin of error.  But nobody likes margins of error :)  So Sergey offered up an interesting point on the math:
From pure statistical standpoint there will be more boys than girls because there are exactly 50% boys, while number of girls is a progression toward 50% (1/2 of 50% + ½ of 25% + ½ of 12.5%, etc…) never quite reaching it. That assumes exactly 50% probability with no variations.
All in all, pretty interesting stuff.  I look forward to more of these.  In fact, while I was typing this, Jason posted a new one today:
Let's say you're in a game show where you have the choice to pick from one of three rooms. Two of the rooms contain nothing, and the other room has $1,000,000. You're allowed to pick one room, but you're not told what is in it just yet. After you make your choice, you're now told of one of the two remaining rooms that contains nothing. You're now allowed to either keep your original choice, or switch to the room that you don't know what it contains.

Here's a couple of examples:

Example 1
R1: nothing
R2: money
R3: nothing

You pick R3. Then you're told R1 doesn't have money. You can now stick with R3, or go with R2

Example 1
R1: money
R2: nothing
R3: nothing

You pick R1. Then you're told R2 doesn't have money. You can now stick with R1, or go with R3

Do you keep your choice, or switch? The real question is, on average is it more beneficial to keep your original choice, or switch? Why or why not?
This is fun.

2 comments:

  1. Mythbusters is testing the 3 doors problem you have next week

    ReplyDelete
  2. Really? That's kind of disappointing, actually. I know they've tested basic physics on multiple occasions, but just doing a math problem seems kind of silly. Maybe I'll see the point when I see a commercial for the episode, I don't know. They're really running out of things to do.

    If they ever do the logic problem from the two doors in Labyrinth, I'm done with them.

    ReplyDelete