*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 .

*Starting Matlab.*Matlab is installed on all leland machines - elaine, tree, etc.

Just type:elaine20:~> matlab

You should see the matlab prompt:>>

*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?**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.

>> 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 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?**Distribution of Sample Mean, Part II*How could we get the mean and variance of the means of 100 samples of size 10?

*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?**A much cleaner***for**loop.>> sampling=zeros(10000,1); >> for i=1:10000 sampling(i)=mean(drop(10)); end

*Is the loop in (b) actually faster than the one in (a)?*

Try timing it with**cputime**- cpu times;

**hint**>> start=cputime; ... >> ending = cputime-start;

**etime**- elapsed time;**tic toc**- stopwatch timer;**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.*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?*

*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)}

*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:>

*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?*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.

> 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 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?**Distribution of Sample Mean, Part II*How could we get the mean and variance of the means of 100 samples of size 10?

*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?**A much cleaner***for**loop.sampling=rep(0,100) for (i in (1:100)) { sampling[i]=mean(drop(10)) }

*Is the loop in (b) actually faster than the one in (a)?*

Try timing it with**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)); )

**x1**- cpu time;**x2**- user processor time;**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.*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?*