2.2 A simpler problem
A simpler problem
Imagine a family with four children.
What is the probability that the family will have exactly three girls?
There are various ways to answer this question. One way, is to use simulation.
Simulation makes a model of the problem. We use the model to generate simulated data. If the model is a good one, the simulated data should look like the real data.
First we need to simulate a family of four children.
Then we need to count the number of girls.
We do this many many times, and see how often we get a count of 3.
For example, we can run the following cell 4 times, to get 4 random numbers between 0 and 1.
Call 0 a boy, and 1 a girl. If we run this four times, then we have one simulated family. We can count how many 1s (girls) we got in the four runs, and that is the simulated number of girls, for this family.
# Return a random number that is either 0 or 1.
# The second arguments, 2, tells the function to return numbers up to, but
# not including 2.
np.random.randint(0, 2)
0
np.random.randint
is a function.
np.random.randint(2)
calls the function, and returns a random number, that is either 0 and 1.
It’s inconvenient to have to run this cell many times. We really need some machinery to make the computer do that for us. We need variables, functions, comparisons and arrays. We will deal with those next.