In [6]:

```
from scipy.stats import binom, uniform
import numpy as np
#sample from a binomial distribution
#Model: Urn with balls of 2 colors. Draw a ball n times with replacement and note down
#number of successes.
n=1
p=1/2
#we want to model 10000 families. Each family has 2 children. Each time the child
#might be a girl (here counted as success - no offense meant) or a boy. Therefore n must be one, because
#we draw a ball one time from the urn and note down the number of successes, which is if it
#is a girl.
child1 = binom.rvs(n, p, size=10000)
child2 = binom.rvs(n, p, size=10000)
#now we want to compute the probability:
#P(both girls | any one is a girl) according to a frequentists interpretation.
#For that we need the number of cases where a family has 2 girls so (child1=girl and child2=girl)
#occur.
#This quantity must be divided by the number of cases where a family has
#( child1=girl or child2=girl).
numberOfGirlsPerFamily = child1+child2
numberOf2GirlsPerFamily = np.sum(numberOfGirlsPerFamily==2)
numberOfAtLeast1GirlPerFamily = np.sum((numberOfGirlsPerFamily == 2) | (numberOfGirlsPerFamily==1))
#now we can do the computation
#P(both girls | any one is a girl) = Both are girs / any one is a girl
P1 = numberOf2GirlsPerFamily / numberOfAtLeast1GirlPerFamily
#we expect this to be 1/3
#Likewise we compute
#P(both girls | older is girl)
numberOfOlderChildIsGirl=np.sum(child1==1)
P2 = numberOf2GirlsPerFamily / numberOfOlderChildIsGirl
#we expect P2 to be 1/2
print('(Should be 1/3) The probability that both children are girls given that at least one of the children is a girl is: ' +str(P1))
print('(Should be 1/2) The probability that both children are girls given that the older one of the children is a girl is: ' +str(P2))
```

Was this helpful?

1 / 0