Dear friends. Please see the program below and answer if it does simulate a population of 1.000.000 families, each with at max 20000 children (typical in Denmark, you know), constructed such that each family stops having children when more boys than girls are present ? Equal numbers of boys and girls are got in the population, according to the simulation, is that obvious ? ND <- NP <- NULL #ND - number boys, P: girls for (j in 1:1000000) # number of families { n1 <- n2 <- NULL for (i in 1:20000) { n1[i] <- rbinom(1,1,0.5) # each equally likely - here number of boys n2[i] <- 1- n1[i] # and girls if(sum(n1)>sum(n2)) break } ND[j] <- sum(n1) NP[j] <- sum(n2) } sum(ND)/sum(NP) j -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Troels Ring <tring at mail1.stofanet.dk> writes:> Dear friends. Please see the program below and answer if it does simulate a > population of 1.000.000 families, each with at max 20000 children (typical > in Denmark, you know), constructed such that each family stops having > children when more boys than girls are present ? Equal numbers of boys and > girls are got in the population, according to the simulation, is that obvious ?I am not an expert in probability or in stochastic processes so I can't say that it is "obvious". I can say it is not unreasonable if you consider the sizes of families. Basically what happens is that large families have nearly equal numbers of girls and boys and also have a high weight in the calculation of the population proportion. (You can only get odd numbered family sizes according to your rule and for a family of size 2K + 1 there will be K girls and K + 1 boys.)>From simulation it seems that there is a non-negligible probability ofvery large family sizes. If you get a high proportion of girls early then it can take a long time for the number of boys to catch up. Most of the families have only 1 or 3 children but about 1% or 2% of the time you get families of size 500 or more. In fact when I simulated 1000 families with a maximum allowable family size of 999, I got 23 families that would have had more than 999 children.> famsz <- double(1000) > ind <- 1:1000 > for (i in seq(along = famsz)) famsz[i] <- min(ind[cumsum(ifelse(runif(1000) > 0.5, 1, 0)) > (ind/2)])There were 23 warnings (use warnings() to see them)> table(famsz)famsz 1 3 5 7 9 11 13 497 141 62 25 31 24 17 15 17 19 21 23 25 27 15 9 6 12 8 10 6 29 31 33 35 37 39 41 2 6 7 2 2 3 3 43 45 47 51 55 57 59 2 1 4 3 5 2 1 61 63 65 69 71 73 75 2 3 1 1 1 1 1 77 91 97 99 103 107 109 5 1 2 3 1 2 1 111 113 121 127 137 139 143 1 1 1 1 2 1 1 147 163 167 169 173 177 191 1 1 1 3 1 1 2 215 235 243 251 275 279 281 1 1 1 1 1 1 1 293 307 321 359 401 411 421 1 1 1 1 1 1 1 459 467 489 537 573 581 603 1 1 1 1 1 1 1 629 631 661 771 839 863 929 1 1 1 1 1 1 1 2147483647 23> ND <- NP <- NULL #ND - number boys, P: girls > for (j in 1:1000000) # number of families > { > n1 <- n2 <- NULL > for (i in 1:20000) { > n1[i] <- rbinom(1,1,0.5) # each equally likely - here number of boys > n2[i] <- 1- n1[i] # and girls > if(sum(n1)>sum(n2)) break > } > ND[j] <- sum(n1) > NP[j] <- sum(n2) > } > sum(ND)/sum(NP) > j > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >-- Douglas Bates bates at stat.wisc.edu Statistics Department 608/262-2598 University of Wisconsin - Madison http://www.stat.wisc.edu/~bates/ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
Seems like a very odd setup but it does seem as though the code would do what you wanted. Also seems as though you could do some combinatorics to get the answer much more efficiently if you wanted to ... (i.e., write down the probabilities of having families that were (1 boy, 0 girl = 50%) (2 boy, 1 girl = dbinom(2,3,0.5) = 3/8), (3 boys, 2 girls), (4 boys, 3 girls), etc., and then divide by the sum. Since you add children one at a time and stop when sum(n1)>sum(n2), you will always have one more boy than girl in the family ...) [That says, by the way, that you won't get equal numbers of boys and girls. You will get between 100% and 50% boys, closer to 100%]. However, after some playing (see slightly augmented code below), it also seems clear that there's something Troels and I are both missing, or a bug in the rbinom code (??) [or a failure of low-order bits to be uncorrelated?]; the distribution of family sizes has some odd characteristics (all family sizes are odd, as expected, but there are more large families -- and some that reach the maximum, which is unexpected). So are we both being boneheaded? NF <- 1000 ND <- NP <- NULL #ND - number boys, P: girls maxfam <- 1000 famdist <- numeric(maxfam) for (j in 1:NF) # number of families { cat(j,"\n") # n1 <- n2 <- NULL n1 <- n2 <- numeric(maxfam) for (i in 1:maxfam) { if (i %% 100 == 0) cat("* ",i,"\n") n1[i] <- rbinom(1,1,0.5) # each equally likely - here number of boys n2[i] <- 1- n1[i] # and girls if(sum(n1)>sum(n2)) break } famsize <- sum(n1)+sum(n2) famdist[famsize] <- famdist[famsize]+1 ND[j] <- sum(n1) NP[j] <- sum(n2) } sum(ND)/sum(NP) j f.odd <- famdist[(1:maxfam) %% 2 == 1] f.even <- famdist[(1:maxfam) %% 2 == 0] plot(f.odd[1:50]) On Wed, 30 Aug 2000, Troels Ring wrote:> Dear friends. Please see the program below and answer if it does simulate a > population of 1.000.000 families, each with at max 20000 children (typical > in Denmark, you know), constructed such that each family stops having > children when more boys than girls are present ? Equal numbers of boys and > girls are got in the population, according to the simulation, is that obvious ? > > ND <- NP <- NULL #ND - number boys, P: girls > for (j in 1:1000000) # number of families > { > n1 <- n2 <- NULL > for (i in 1:20000) { > n1[i] <- rbinom(1,1,0.5) # each equally likely - here number of boys > n2[i] <- 1- n1[i] # and girls > if(sum(n1)>sum(n2)) break > } > ND[j] <- sum(n1) > NP[j] <- sum(n2) > } > sum(ND)/sum(NP) > j > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ > >-- 318 Carr Hall bolker at zoo.ufl.edu Zoology Department, University of Florida http://www.zoo.ufl.edu/bolker Box 118525 (ph) 352-392-5697 Gainesville, FL 32611-8525 (fax) 352-392-3704 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
This is a standard problem in stochastic processes. The distribution of the family size has an infinite mean. See, for example, Cox & Miller, "The theory of stochastic processes", Methuen, 1965, example 2.8. Ted Catchpole. On Thu, 31 Aug 2000, Troels Ring wrote:> Dear friends. Please see the program below and answer if it does simulate a > population of 1.000.000 families, each with at max 20000 children (typical > in Denmark, you know), constructed such that each family stops having > children when more boys than girls are present ? Equal numbers of boys and > girls are got in the population, according to the simulation, is thatobvious ?> > ND <- NP <- NULL #ND - number boys, P: girls > for (j in 1:1000000) # number of families > { > n1 <- n2 <- NULL > for (i in 1:20000) { > n1[i] <- rbinom(1,1,0.5) # each equally likely - here number of boys > n2[i] <- 1- n1[i] # and girls > if(sum(n1)>sum(n2)) break > } > ND[j] <- sum(n1) > NP[j] <- sum(n2) > } > sum(ND)/sum(NP) > j > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._-- Dr E.A. (Ted) Catchpole School of Maths & Stats Honorary Senior Research Fellow University College, UNSW Institute of Maths & Stats Australian Defence Force Academy University of Kent at Canterbury Canberra, ACT 2600, Australia Canterbury CT2 7NF, England e-catchpole at adfa.edu.au E.A.Catchpole at ukc.ac.uk www.ma.adfa.edu.au/~eac fax: +61 2 6268 8886 -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._