I recently ran into two problems:

  1. given a rand() function that generates 0 and 1 with 50% probability respectively (binary outcome), how to write some code to generate 1 with 90% of the chance and 0 with 10%?

  2. There is a society where people will NOT stop having babies until they have a boy. What's the ratio of boys and girls in that society? Consider it's equal probability to have either a boy or a girl.

If possible, please advise what kind of background prob/stats materials these relate to. Thanks!

UPDATE: Here are some thoughts on the two questions:

1.since rand() only generate 0 or 1, it's useless to write code like the following:

if (rand() < 0.1) {
     return 0;
} else {
     return 1;

I guess this involves Bernoulli trial and Binomial distribution, but the question asks for EXACT distribution for 90% of 1 and 10% of 0. I'm puzzled how to proceed.

2.I put it the wrong way - my bad. My initial thoughts on this is as follows:

Suppose $\text{P}(B) = \text{P}(G) = 0.5$
B' = boys in the society

$$\text{P}(B') = \text{P}(B) + \text{P}(B|G) + \text{P}(B|G,G) + \text{P}(B|G,G,G) + ...$$

But I didn't see how to derive this equation into computable format.

UPDATE 2 (from @David Marx):

  1. Problem 1

    think of this as a binary number with 4 bits (n) - represents range [0, 15] with each equal probability of 1/16. Now to get 1 with 90% probability, we can write something like this:

    if (n < 9) { return 1; } else if (n == 9) { return 0; }

This way, Prob(n in range[0,9)) = 9/16; Prob(n = 9) = 1/16; So the returned value will be 1 at 90% of the time.

  1. Problem 2

The reminding of Geometric Series is appreciated! The equation indeed matches my initial thoughts on the summation of conditional probabilities -- I failed to recognize it as geometric series.

So I guess the ratio should be the result for that summation:

$$\text{P}(B') = (1/2) + (1/2)^2 + (1/2)^3 + ... = 1/(1-(1/2)) - 1 = 1. $$

So the ratio of boys in the society will be 100%? Anything wrong here?

  • 2
    $\begingroup$ If this is homework, please use the tag "self-study" and indicate your initial thoughts on a solution. $\endgroup$
    – Nick Cox
    Aug 8 '13 at 17:16
  • $\begingroup$ No the're not. Please explain them. $\endgroup$
    – galactica
    Aug 8 '13 at 17:29
  • 1
    $\begingroup$ Apparently these are two distinct problems. It might be worth to make one question/post for each of them so that they can be handled separately. $\endgroup$
    – QuantIbex
    Aug 8 '13 at 17:34
  • 1
    $\begingroup$ I agree with @NickCox. Even if these aren't actual homework questions from a class you are taking, they are essentially 'homework questions' from the perspective of CV's definition. Please add the [self-study] tag & indicate what you have thought about these issues & where you are stuck. Note that we treat these types of questions differently (see our help page). $\endgroup$ Aug 8 '13 at 17:42
  • 2
    $\begingroup$ The answer to the second question is that the society has no children: since people stop having babies until they have a boy, they never start having any babies at all! BTW, it doesn't matter whether these questions are homework or not: they are routine (textbook) questions with routine answers and so their interest lies in helping you fill the gaps in your research or solution efforts. To do so, we need to know what progress you have made on these questions so far. That's what @Nick Cox was requesting. $\endgroup$
    – whuber
    Aug 8 '13 at 17:42

The answer to the second question is that the ratio becomes undefined because the society has a 100% chance of dying out. The reason why this happens is fascinating: statistical fluctuations in the balance of males and females (a "second order effect") in a population that otherwise would appear to be stable in size (a "first order effect") will cause it to decrease.


To make it clear what this statement means, here are the assumptions I use:

  • The "society" (let's just call it a population because it acts in a very non-human way) consists of males and females, all of which might potentially breed.

  • Breeding is performed by associating males and females permanently into as many pairs as possible. Each pair produces one offspring sequentially.

  • A pair produces males with 50% probability, independently with each birth and independently of all other pairs.

  • Breeding for a pair stops when either (a) a male is produced or (b) a predetermined finite limit $l$ of offspring is reached. (The latter is a necessary nod to realism: no individual is immortal!)

  • After all pairs stop breeding, the new generation commences breeding in the same way. (That is, there is no overlap of generations and no pairing between generations.)

Under these assumptions, all relevant information is conveyed by the number of males $M$ and number of females $F$ in the current generation. The number of breeding pairs equals $\min(M,F)$. Each pair produces one male (unless it reaches the limit) and a number of females according to a geometric distribution. This is a truncated geometric distribution for the number of females. If there were no limit, it would be a geometric distribution where the probability of $F$ females is

$$\begin{align} \Pr(F=0) &= 1/2 \\ \Pr(F=1) &= 1/4 \\ \cdots \\ \Pr(F=k) &= 2^{-k-1} \\ \cdots \end{align} $$

Because there is a limit, this list stops with $\Pr(F=l) = 2^{-l-1} + 2^{-l-2} + \cdots = 2^{-l}$. Consequently the expected number of males per breeding pair is

$$ \mathbb{E}[M] = \left(1/2 + 1/4 + \cdots + 2^{-l} + 0 \times 2^{-l}\right) =1 - 2^{-l}. $$

and the expected number of females can similarly be computed. It is clear that in a large population, because the expected number of males is less than one per breeding pair, the expected number of breeding pairs is less in each generation.

Furthermore--regardless of whether we include a limit in the model--variation in the numbers of males and females will cause the population eventually to die out anyway. This is because the number of breeding pairs can only equal the smaller of the numbers of males or females produced in the preceding generation and fluctations in both of those (which are up or down from the expected value with roughly equal probability) will cause the smaller of the two to fluctuate downwards much more often than it fluctuates upwards. This mechanism further forces the population down to zero.


Rather than carry out a full and rigorous analysis of the effects of fluctuation, let's do a simulation and look at what happens to small populations of, say, $500$ males and $500$ females. I did this calculation $10,000$ separate times for $81$ new generations (setting the limit to an infinite value, to be conservative):


Clearly the continuation of this population is hopeless: although initially there is some chance that it increases, inevitably the population shrinks. The shrinking quickly is evident: within $12$ new generations, there is almost no chance the latest generation exceeds the size of the first generation (of $1000$). There is almost no chance this particular population will last more than $100$ generations.

(Larger populations will take more than $12$ generations to shrink with near certainty, but the same things eventually happen to them.)

Here is R code to perform the simulation.

next.gen <- function(pop, p=1/2, limit=30) {
  n.females <- rgeom(n <- min(pop), p)
  has.male <- n.females <= limit
           females=sum(n.females[has.male]) + sum(rep(limit, n)[!has.male])))
simulate <- function(pop, n=1, ...) {
  result <- matrix(0, nrow=n, ncol=2)
  colnames(result) <- c("males", "females")
  n.gen <- 1; k <- 0
  while(n.gen <= n && sum(pop <- result[n.gen,] <- next.gen(pop, ...)) > 0) 
    {n.gen <- n.gen+1}
pop <- c(males=500, females=500)
system.time(d <- replicate(10^4, rowSums(simulate(pop, 81, limit=Inf))))
tmp <- sapply(seq(1,81,10), function(i) hist(d[i,], breaks=seq(0,1200,25),
                                    main=paste("Generation", i+1),
                                    xlab="Total population"))
  • $\begingroup$ Very clear explanations and useful simulation! Appreciate it! All is clear except this: "The actual probabilities therefore are the values in this list divided by the sum of its probabilities, equal to ...". Could you illustrate a bit on this and also E(M) and E(F)? $\endgroup$
    – galactica
    Aug 12 '13 at 19:43
  • $\begingroup$ I erred in my first draft so let's look at a simple example, say $l=2$. The probabilities for outcomes $(M,F)$ are $\Pr(1,0)=1/2$, $\Pr(1,1)=1/4$, $\Pr(0,2)=1/4$. The limit and the stop-at-one-male rule prohibit all other possibilities. Expectations are probability-weighted values: $\mathbb{E}(M,F)$ = $(1/2)(1,0)+(1/4)(1,1)+(1/4)(0,2)$ = $(3/4,3/4).$ For a population of $n$ breeding pairs (at least $2n$ people), we obtain $3n/4$ for the expected numbers of Ms and Fs in the next generation. Thus the expectation of $\min(M,F)$ must be less than $3n/4$ = $3/4$ the number of breeding pairs. $\endgroup$
    – whuber
    Aug 12 '13 at 20:02
  • $\begingroup$ wonderful clarification! It's now crystally clear! Thanks a lot! $\endgroup$
    – galactica
    Aug 12 '13 at 21:34

Here are some thoughts to get you started:

Problem 1

The probability of getting a single 1 (or 0) is 50%, and as you've noticed this isn't especially helpful. But what's the probability of getting 00 in two trials? or 010 in three trials? Or at least two 1's in four trials? I'd recommend you start thinking about what you can accomplish if you shift your event space from single events to sequences of experiments. From there, you can look at permutations, combinations, minimal conditions... You can also combine probabilities using AND and OR rules, or even conditional probabilities.

UPDATE: Regarding your current solution, when in doubt: run some simulations and see what happens!

experiments = sample(1:16, N, replace=T)
viable = experiments[experiments <10]
mean(viable<9) #.8888 ~ 8/9

Hmmm... not quite $9/10ths$. Why do you suppose we got $8/9ths$ instead? You're almost there! Also, as whuber mentioned, the direction you are going is not the only solution to this problem. But what you're doing is creative and I think you should stay the course before figuring out a method that doesn't require you to drop samples.

Nevermind, your solution for this part actually works. I forgot that you were counting up from 0 and not 1. Good job! Now work on making whuber happy :)

Problem 2

The number of trials required to achieve a single success can be modeled using a Geometric Distribution. I don't want to give away the whole solution here, but take a look at the calculation for the mean (i.e. the "expected value") for this distribution given a known success probability.

UPDATE: I was suggesting you look at the mean of the geometric distribution, not use ageometric series (well, in all fairness, they're definitely closely related). Your geometric series calculation contains the entire sampling space, so we should expect it to sum to 1 (you're basically on track to derive the pdf of the geometric distribution). Also, just intuitively, you should see why the expected ratio absolutely should not be 100% boys. We're not precluding female births here in any way.

  • $\begingroup$ Very helpful tips David! $\endgroup$
    – galactica
    Aug 8 '13 at 18:56
  • $\begingroup$ Re the update: a sequence of four binary digits gives integers in the range 0:15 rather than 1:16, which is why you obtain $8/9$ instead of $9/10$. $\endgroup$
    – whuber
    Aug 8 '13 at 21:00
  • $\begingroup$ hah, oh right. my bad $\endgroup$
    – David Marx
    Aug 8 '13 at 21:01
  • $\begingroup$ The expect value for Geometric Dist is 1/p = 2, meaning expecting two children to have one boy. So the ratio of boys will be 1/2 = 0.5? $\endgroup$
    – galactica
    Aug 8 '13 at 21:38
  • $\begingroup$ Yes, the fraction of the society that are boys is 1/2! But I interpreted your question to be asking about the ratio of boys to girls, which is slightly different. If this is the ratio you were looking for, then looks like you made it. For extra credit: program a simulation to confirm your results. $\endgroup$
    – David Marx
    Aug 8 '13 at 21:46

The second problem has been discussed in Mathematics StackExchange (https://math.stackexchange.com/questions/218674/ratio-of-boys-and-girls) and in MathOverflow (https://mathoverflow.net/questions/17960/google-question-in-a-country-in-which-people-only-want-boys and https://mathoverflow.net/questions/132297/boys-and-girls-revisited). There seems to be disagreement about what the "correct" answer is; it depends on how you interpret the question.

  • 1
    $\begingroup$ Thanks for your pointers~! This definitely extends my understanding of this problem. $\endgroup$
    – galactica
    Aug 13 '13 at 16:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.