Exercise 2


Assigned: 02/09/12 Due: 02/14/12

Using the computer we can generate a large number of samples from a random variable. We will explore some of the concepts from random variables -- the density function, transformation of random variables, and conditional density -- by examining relative frequencies of random numbers.

Please do the following and answer the questions:

  1. Before you do anything else in MATLAB, run the following commands (MATLAB 7.7 and higher):
    s = RandStream.create('mt19937ar','seed',sum(100*clock));
    RandStream.setDefaultStream(s);
    
    This will set a unique starting point for the random number generator in MATLAB. This means that your specific results will be different from those of everyone else in the class.

    For MATLAB 7.6 and lower, use the following command:

    rand('state',sum(100*clock))
    
  2. As we discussed in class, a density function is just a histogram with infinitesimally wide bins and an infinite number of samples, scaled so that the total area under the curve is one. Use rand to generate 100 random numbers. From these numbers, create a 10-bin histogram and display using hist. What is the expected shape of the histogram? (Read the help on rand.) Why does the plot vary from its expected shape? Run the experiment a couple more times with a new set of random numbers. Describe what happens to the plot.

  3. Rerun the experiment above with 1,000,000 random numbers. How does the bar plot change from the exercise above? Why?

  4. Run the experiment with randn, 1,000,000 random numbers, and 30 bins. What is the shape of the histogram?

  5. Generate 1,000,000 random numbers using randn and then multiply each number by 4 and then add 3. Display a 30-bin histogram, and describe the change compared to the exercise above. Explain any differences you observe. What does multiplying by 4 do to the variance?

  6. Some transformations of random variables are difficult to do analytically. However, they are straightforward to simulate. Generate a set of 1,000,000 random values in the range [0,4) using rand and scaling by 4. Transform the variable by computing the square root of the input. NOTE: You don't need to use loops. Just use:

    y = sqrt(x);
    

    where x is the scaled vector obtained from rand.

    Look at the 100-bin histogram of the input and the transformed values. Do they look the same or similar in shape? Submit a plot of the two histograms.

Please include all histograms that you generated. Make them no more than 1/4-page high. Please make your write-up as concise as possible while answering all questions.

If your histograms look like several groups of bars with spaces between the groups, you messed up. Be sure that the random numbers you generated are in a vector, not a matrix. Check by running "whos".

Please submit your report to Canvas electronically before class on the due date in PDF format. Turn in a hard copy at class time.

NOTE: All out-of-class work is to be done independently. Sharing of programming tips and discussing general concepts is ok. Collaborating on experiments or code-writing is not. Any such collaboration on these assignments will be considered an act of academic dishonesty and will be treated accordingly.