next up previous
Next: Homeworks Up: Labs and Homeworks Previous: Labs and Homeworks

Subsections

Labs

Lab 1, April 8th, 2004: to hand in before April 9th at 5pm

Computing Lab Section: Matlab version #1
  1. Setting Things Up.

    Make a course directory.
        elaine20:~> mkdir s208
        elaine20:~> cd s208
    
    Make a file called drop.m with following lines:
        function out=drop(n)
        out=randn(1,n);
    

    If you are working on a leland machine, you could also simply copy the file from the course directory:

         cp /usr/class/stat208/lab/drop.m .
    
  2. Starting Matlab. Matlab is installed on all leland machines - elaine, tree, etc.
    Just type:
        elaine20:~> matlab
    
    You should see the matlab prompt:
        >>
    

  3. Warming Up. We will use the drop function to generate a vector of 10 random numbers.
        >> drop(10)
    
        ans =
    
          Columns 1 through 7
    
           -0.4326   -1.6656    0.1253    0.2877   -1.1465    1.1909    1.1892
     
          Columns 8 through 10
    
           -0.0376    0.3273    0.1746
    

    Question 1   Did you get the same answer as I did? Many of you will. The random number generator is not really random! How could you explain this?

  4. Distribution of Sample Mean, Part I

    Try typing in:
        >> sampling=mean(drop(10))   
    
        sampling =
    
            0.2310
    
    sampling is the mean of a sample of size 10.

    Next,

        >> sampling=[sampling mean(drop(10))] 
    
        sampling =
    
           0.2310    0.0253
    
    What does this command do?

    Now repeat the previous command 10 more times. Stop when you see sampling is a vector of length 12.
    (hint: your $\uparrow$ key may save you a lot of typing in matlab).

    Question 2   If the original random variables comes from the standard Normal distribution, N(0,1), what is the variance of the means of samples of size 10?

  5. Distribution of Sample Mean, Part II

    How could we get the mean and variance of the means of 100 samples of size 10?

    1. A clumsy approach. Use a for loop to repeat commands in [*].

      If we want to discard the sampling vector we have so far, we could do:

          >> sampling=zeros(0);
          >> for i=1:10000                        
          sampling=[sampling mean(drop(10))];
          end
      

      Question 3   From a computational point of view, this is lousy. Why?

    2. A much cleaner for loop.
      >> sampling=zeros(10000,1);
      >> for i=1:10000
      sampling(i)=mean(drop(10));
      end
      

    3. Is the loop in (b) actually faster than the one in (a)?
      Try timing it with
      1. cputime - cpu times;
        hint
          
            >> start=cputime;
            ...
            >> ending = cputime-start;
        
      2. etime - elapsed time;
      3. tic toc - stopwatch timer;
      4. flops - number of floating point operation count;

      hint: Use the help command in Matlab.

    Question 4   Compare time used to complete loops in (a) and (b) with above commands. Do the two loops run equally fast? Explain why, or why not.
    Find a better method for doing these computations.

  6. Confidence Interval, Quantiles, etc....

    Question 5   Using the results from 5(b), how do you find the 95% confidence interval for the sample mean? How do you find the quantiles of the sample mean?

Computing Lab Section: R/Splus version #1

  1. Setting Things Up.

    Make a course directory.
        elaine20:~> mkdir s208
        elaine20:~> cd s208
    
    Make a file called drop.s with following lines:
        drop=function(n){
        out=rnorm(n)
        return(out)}
    
  2. Starting R. R/Splus is installed both on the leland machines elaine, tree, etc. and the PCs in the lab
    Just type:
        elaine20:~> R
    
    or click on the R icon in the programs section of the start menu. You should see the prompt:
     >
    

  3. Warming Up. We will use the drop function to generate a vector of 10 random numbers.
    > drop(10)
     [1] -0.4710930  0.2545599 -0.1460039 -0.3311608 -0.9997712 -0.9675422
     [7] -0.7016111 -0.4112995 -0.6369118  1.8809128
    

    Question 1   Did you get the same answer as I did?
    The random number generator is not really random! How could you explain this?

  4. Distribution of Sample Mean, Part I

    Try typing in:
        > sampling=mean(drop(10))   
       > sampling
    [1] -0.4255386
    
    sampling is the mean of a sample of size 10.

    Next,

       > sampling=c(sampling, mean(drop(10)))
       > sampling
     [1] -0.425538589 -0.002214149
    
    What does this command do?

    Now repeat the previous command 10 more times. Stop when you see sampling is a vector of length 12.
    (hint: your $\uparrow$ key may save you a lot of typing in R, or you can use the emacs screen and the R mode by typing Esc-x R).

    Question 2   If the original random variables comes from the standard Normal distribution, N(0,1), what is the variance of the means of samples of size 10?

  5. Distribution of Sample Mean, Part II

    How could we get the mean and variance of the means of 100 samples of size 10?

    1. A clumsy approach. Use a for loop to repeat commands in [*].

      If we want to discard the sampling vector we have so far, we could do:

           sampling=NULL
           for (i in (1:100))                        {
          sampling=c(sampling, mean(drop(10)))
          }
      

      Question 3   From a computational point of view, this is lousy. Why?

    2. A much cleaner for loop.
      sampling=rep(0,100)
       for (i in (1:100))                        {
      sampling[i]=mean(drop(10))
      }
      

    3. Is the loop in (b) actually faster than the one in (a)?
      Try timing it with
      1. system.time - cpu times;
        hint
          
        fun1=function(n=1000){
        sampling=NULL
        for (i in (1:n))
        sampling=c(sampling,mean(drop(10)))
         }
        system.time(fun1(10000));
         fun2=function(n=1000){ sampling=rep(0,n)
         for (i in (1:n))
        sampling[i]=mean(drop(10))
         }
           system.time(fun2(10000));
        )
        

      2. x1 - cpu time;
      3. x2 - user processor time;
      4. x3 -

      hint: Use the help command in R/Splus.

      Question 4   Compare time used to complete loops in (a) and (b) with above commands for 10,000 iterations. Do the two loops run equally fast? Explain why, or why not.
      Find a better method for doing these computations.

    4. Confidence Interval, Quantiles, etc....

      Question 5   Using the results from 5(b), how do you find the 95% confidence interval for the sample mean? How do you find the quantiles of the sample mean?


next up previous
Next: Homeworks Up: Labs and Homeworks Previous: Labs and Homeworks
Susan Holmes 2004-05-19