In: Statistics and Probability
In MAPLE or MATLAB
A family with three children is chosen at random and the number X of daughters is counted. Describe how to simulate an observation on X based on U ~ unif[0,1]. Then, simulate 20 observations on X.
Using Matlab.
The rand() function gives a random number in the range [0, 1] with a uniform distribution. Instead of directly using this function, we can use the randi() function which gives an integers in the range [1, n] with a uniform distribution where n is the first parameter to randi(). As we have 4 possiblilites (0, 1, 2, 3) for number of daughters, n = 4. We will also have to subtract one to get a number in [0, 3] range. So, the Matlab code will be:
>> randi(4,1,20)-1
ans =
3 0 2 2 3 2 1 2 0 3 1 2 1 0 3 1 0 2 3 3
The first parameter to randi is the number of values we need. The second one is the number of rows. As we have passed one here, we will get a row vector. The third one is the number of columns. As we need 20 values, we pass 20 here.
Note: we can switch rows and columns to get a similar result as a column vector.
Note: We can get a similar result directly using rand(), but it will require some extra processing to get numbers in the right range:
>> fix(rand(1,20)*4)%4
ans =
3 3 1 3 2 1 2 3 1 3 2 3 1 2 2 0 3 1 2 2
Here, rand() gives a number in [0,1] with uniform distribution. When we multiply by 4, we get a floating point number in [0. 4]. Using fix() we truncate the number to [0,4]. Note that we will be getting an integer with uniform distribution in [0,3] here. In a rare case when we get 4, we treat it as 0.
The first parameter to rand() is the number of rows. The second parameter to rand() is the number of columns.