The objective is to practice the use of MATLAB random variables to
better understand random variables, distributions, and their use in
engineering problems.
A random variable
is a variable that can take any one of a set of
values according to some probability distribution. In this lab we
will make things very simple (no integration, for example) and suppose
that random variables only take integer values so that in what
follows, mention of
without modifiers implicitly means
is
considered to be a random integer variable. We use the notation
to denote the probability that
has value
. If
is the
complete set of values that
can take, we must have
Two important parameters of distributions are the mean and the
standard deviation. The mean tells us something about the value we
might expect for
the next time it is assigned a value. The
standard deviation tells us something about how close to the mean the
next value is going to be. The mean of
is defined as
It is straightforward to compute
and
if
is
uniformly distributed between numbers
and
(that is,
). From equation (1) and the fact
that there are
numbers in
,
.
Therefore,
. The sum
written out is
What about the standard deviation? If
is uniformly distributed
between
and
the standard deviation, from equations (2)
and (4), is

Now consider another distribution, called the triangle distribution.
If
has the triangle distribution from
to
, then
Up to now we have considered integer random variables to avoid heavy
duty math you may not have seen yet. But all the results above are
similar to results obtained if
were allowed to take non-integer
values as well. So, from now on we lift the restriction of integer
values.
For many applications the Gaussian distribution is the most important distribution available to us. Avoiding the very complicated definition of the Guassian distribution, it suffices for this lab to state the following amazing properties it has:
There are six problems.
Write an m-file that asks for a mean (
) and then generates
random real values from the uniform distribution with
and
. Use the MATLAB built-in function rand to
generate the values. Create an array v of buckets so
that, at the end, v(i) contains the number of values generated
between the integer
, exclusive, and the integer
, inclusive.
Then plot the result (the
coordinates are the numbers in v(i) and the
coordinates are bucket indices i). Make sure
the
axis takes a range of values from 0 to 1.2 times the maximum
of v(i) over all i. See Figure 1 for an example output
where the mean was set to 100.
Repeat problem 3.1 for the triangle distribution. The distribution
rises linearly to
. Example output is shown in
Figure 2 for a mean of 100. This is trickier than problem 3.1 because
there is no triangle distribution generator of values as there was for
the uniform distribution. But we can use the uniform generator to get
triangle values using the following transformation:
Major hint: Where you used
in the first
problem, now use
.
Repeat problem 3.1 for the Gaussian distribution except the input
should include standard deviation. Example output is shown in Figure
3 for a mean of 100 and standard deviation 50. Use the MATLAB
function randn which generates a normal distribution with mean 0
and standard deviation 1. To translate to a Gaussian distribution of
mean
and standard deviation
use:
Major hint: Just like before, where you used something like
, now use
.
Major hint:
gives a value from minus infinity to plus
infinity. Therefore the vector you are keeping statistics in will
never be able to hold all the values generated. The solution is an
statement that check random values before you attempt to
store them. For example,
if rand_value > 2*mean
high = high + 1;
elseif rand_value < 1
low = low + 1;
else
v(rand_value) = v(rand_value) + 1; % This is where the stats are kept
end
...
disp('[High error=' num2str(high/npts) ' low error=' num2str(low/npts)]);
Repeat problem 3.1 except this time each recorded value is the sum of
values of uniformly distributed random values. Input from the user
should be the number of values to sum and the mean of each of those
values. Make several runs and generate graphs such as those shown in
Figure 4, where the number of values summed is 100 and the mean of
each value is 1, and Figure 5, where the number of values summed is 10
and the mean of each is 10. Observe that for larger means the
distribution of the sum approaches that of a Gaussian distribution.
Experiment to find the relationship between the mean and standard
deviation of the resulting Gaussian distribution and the number of
values summed and their means.
Major hint: You might need an outside for loop to run the experiments and an inside for loop to sum a bunch of random variables.
Repeat problem 3.4 except this time the values to sum are distributed
according to the triangle distribution. Input from the user should be
the number of values to sum and the mean of each of those values.
Figure 6 shows output when summing 100 values, each of mean 100. What
is the relationship between the mean and standard deviation of the
Gaussian distribution and the number of summed values and mean of each?
Major hint: Just a straightforward change to the solution of the previous problem is needed.
Craps is played with 2 dice. Each die is a small cube, marked on its
faces with spots from one to six. Craps is played as a sequence of
betting rounds. The first roll of the dice in a betting round is
called the come out roll. If the sum of the spots on the come
out roll is 2, 3, 7, 11, or 12, the round is over and a new betting
round begins. Otherwise, the sum (one of 4, 5, 6, 8, 9, 10) is called
the point and subsequent rolls follow until either point is made
again or a 7 is rolled. In either case the betting round ends.
The following enumerates some of the types of bets that can be placed in a betting round:
Suppose we wish to manufacture loaded dice that will result in a
positive gain over a long period. We want the dice to be designed so
it is difficult for a roller to tell the dice are loaded. Find a
probability distribution on the numbers from 1 to 6 of a die that will
do this for pass line bets. Assume the same probability change for
all dice. If
is the highest probability that a die lands on
a particular number and
is the lowest, make
as small as possible. Figure 8 shows a history
where the
See if you can beat this (you should
have no problem). Write your probabilities in the table below:
| Number | Probability | Number | Probability | Number | Probability |
| 1 | 2 | 3 | |||
| 4 | 5 | 6 |
At the MATLAB prompt type 'help rand' and 'help randn' to
get help on using the uniform and Gaussian random number generators.
The function rand returns a number between 0 and 1. If you need
a single uniform random number, say r, between numbers a
(low) and b (high) use 'r = (b-a)*rand(1)+a;'. But r will not be an integer. If r must be an integer between
integers a and b, inclusive, use 'r = floor((b-a+1)*rand(1))+a;'. The function randn returns a
Normally distributed random value with mean 0 and standard deviation
1. To create Gaussian values with mean m and standard deviation
s use 'r = s*randn(1)+m;'.
In problems 3.1-3.5 you will need to keep track of the number of trials with values in particular intervals. This can be done with an array. You can initialize the array, say stats of n elements, to all zeros using something like this: 'stats = zeros(1,n);'. Then, if a trial has a real value v, you can do something like this: 'stats(floor(v)) = stats(floor(v)) + 1;'. When finished, you can 'plot(stats)'.
When plotting, you can set the limits on the
and
axis printed
using the axis function. Use 'help axis' to see how to do
this.
Although problems 3.1-3.5 require five separate pieces of code, there is little difference between them. Moreover, the number of lines of each piece, including all input, end, plot, axis, and so on, statements, should be no greater than 20.
For problem 3.6, you might want to develop a getRoll function that returns an integer from 1 to 6. This allows convenient adjustment of the probabilities of returning the numbers without affecting any other code that you write. The getRoll function is simply an if-then-else statement with six tests: each test looks, for example, like 'if 3/6 <= n & n < 4/6 + 0.023'.
The getRoll function can be called by a function called getRounds which returns a cell array, each cell containing a vector of integers representing rolls during a single legal betting round. Use the getRoll like this: 'roll = getRoll + getRoll;'. A round can be constructed using an array, say tmp which may be initialized like this: 'tmp = [];'. Then each roll is added to tmp like this: 'tmp = [tmp roll];'. If getRounds is to create n rounds, a round can be saved in a cell array like this: 'rndsi = tmp;' where i is a for index variable. Then rnds can be returned (that is, the function line of getRounds should look something like this: 'function rnds = getRounds(n);').
A function which plots a bet history need only take a cell array, say rnds, as input and implement a loop, for example, 'for i=1:length(rnds)', the body of which makes the appropriate betting checks. For example, in the case of pass line betting, let 'v = rnds(i);' and test like this:
if v(1) == 7 | v(1) == 11 | length(v) > 1 & v(length(v)) == 7All bet placing functions perform similar actions and make similar tests.
5. Submission
Submit six m files which solve the six problems of Section 3 on or
before June 5 using blackboard. See the course webpage at