⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 labs.htm

📁 matlab bootstrap程序设计方法
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0059)http://www-stat.stanford.edu/~susan/courses/s208/node4.html -->
<!--Converted with LaTeX2HTML 2002-2 (1.70)original version by:  Nikos Drakos, CBLU, University of Leeds* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan* with significant contributions from:  Jens Lippmann, Marek Rouchal, Martin Wilck and others --><HTML><HEAD><TITLE>Labs</TITLE>
<META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
<META content=Labs name=description>
<META content=web1 name=keywords>
<META content=document name=resource-type>
<META content=global name=distribution>
<META content="MSHTML 6.00.2900.2523" name=GENERATOR>
<META http-equiv=Content-Style-Type content=text/css><LINK 
href="Labs.files/web1.css" rel=STYLESHEET><LINK href="node5.html" rel=next><LINK 
href="node3.html" rel=previous><LINK href="node3.html" rel=up><LINK 
href="node5.html" rel=next></HEAD>
<BODY bgColor=#ffffff><!--Navigation Panel--><A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node5.html" 
name=tex2html166><IMG height=24 alt=next src="" width=37 align=bottom 
border=0></A> <A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node3.html" 
name=tex2html164><IMG height=24 alt=up src="" width=26 align=bottom 
border=0></A> <A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node3.html" 
name=tex2html158><IMG height=24 alt=previous src="" width=63 align=bottom 
border=0></A> <BR><B>Next:</B> <A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node5.html" 
name=tex2html167>Homeworks</A> <B>Up:</B> <A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node3.html" 
name=tex2html165>Labs and Homeworks</A> <B>Previous:</B> <A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node3.html" 
name=tex2html159>Labs and Homeworks</A> <BR><BR><!--End of Navigation Panel--><!--Table of Child-Links--><A 
name=CHILD_LINKS><STRONG>Subsections</STRONG></A> 
<UL>
  <LI><A 
  href="http://www-stat.stanford.edu/~susan/courses/s208/node4.html#SECTION00111000000000000000" 
  name=tex2html168>Lab 1, April 8th, 2004: to hand in before April 9th at 
  5pm</A> </LI></UL><!--End of Table of Child-Links-->
<HR>

<H1><A name=SECTION00110000000000000000>Labs</A> </H1>
<H2><A name=SECTION00111000000000000000>Lab 1, April 8th, 2004: to hand in 
before April 9th at 5pm</A> </H2>
<DIV align=center><B>Computing Lab Section: Matlab version #1</B></DIV>
<OL>
  <LI><I>Setting Things Up.</I> <BR><BR>Make a course directory. <PRE>    elaine20:~&gt; mkdir s208
    elaine20:~&gt; cd s208
</PRE>Make a file called <B>drop.m</B> with following lines: <PRE>    function out=drop(n)
    out=randn(1,n);
</PRE>
  <P>If you are working on a leland machine, you could also simply copy the file 
  from the course directory: <PRE>     cp /usr/class/stat208/lab/drop.m .
</PRE>
  <LI><I>Starting Matlab.</I> Matlab is installed on all leland machines - 
  elaine, tree, etc. <BR>Just type: <PRE>    elaine20:~&gt; matlab
</PRE>You should see the matlab prompt: <PRE>    &gt;&gt;
</PRE>
  <P></P>
  <LI><I>Warming Up.</I> We will use the <B>drop</B> function to generate a 
  vector of 10 random numbers. <PRE>    &gt;&gt; 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
</PRE>
  <P>
  <DIV><B>Question 1</B> &nbsp; <I>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? </I></DIV>
  <P></P>
  <P></P>
  <LI><I>Distribution of Sample Mean, Part I</I> <BR><BR>Try typing in: <A 
  name=2></A><PRE>    &gt;&gt; sampling=mean(drop(10))   

    sampling =

        0.2310
</PRE><B>sampling</B> is the mean of a sample of size 10. <BR>
  <P>Next, <A name=1></A><PRE>    &gt;&gt; sampling=[sampling mean(drop(10))] 

    sampling =

       0.2310    0.0253
</PRE>What does this command do? 
  <P>Now repeat the previous command 10 more times. Stop when you see 
  <B>sampling</B> is a vector of length 12. <BR>(<B>hint</B>: your <IMG 
  height=35 alt=$\uparrow$ src="Labs.files/img2.png" width=14 align=middle 
  border=0> key may save you a lot of typing in matlab). <BR>
  <P>
  <DIV><B>Question 2</B> &nbsp; <I>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?</I></DIV>
  <P></P>
  <P></P>
  <LI><I>Distribution of Sample Mean, Part II</I> 
  <P>How could we get the mean and variance of the means of 100 samples of size 
  10? <BR>
  <OL>
    <LI><I>A clumsy approach.</I> Use a <B>for</B> loop to repeat commands in <A 
    href="http://www-stat.stanford.edu/~susan/courses/s208/node4.html#1"><IMG 
    alt=[*] src="" align=bottom border=1></A>. 
    <P>If we want to discard the sampling vector we have so far, we could do: <PRE>    &gt;&gt; sampling=zeros(0);
    &gt;&gt; for i=1:10000                        
    sampling=[sampling mean(drop(10))];
    end
</PRE>
    <P>
    <DIV><B>Question 3</B> &nbsp; <I>From a computational point of view, this is 
    lousy. Why?</I></DIV>
    <P></P>
    <LI><I>A much cleaner <B>for</B> loop.</I> <PRE>&gt;&gt; sampling=zeros(10000,1);
&gt;&gt; for i=1:10000
sampling(i)=mean(drop(10));
end
</PRE>
    <P></P>
    <LI><I>Is the loop in (b) actually faster than the one in (a)?</I> <BR>Try 
    timing it with 
    <OL>
      <LI><B>cputime</B> - cpu times; <BR><B>hint</B> <PRE>  
    &gt;&gt; start=cputime;
    ...
    &gt;&gt; ending = cputime-start;
</PRE>
      <LI><B>etime</B> - elapsed time; 
      <LI><B>tic toc</B> - stopwatch timer; 
      <LI><B>flops</B> - number of floating point operation count; 
      <P></P></LI></OL>
    <P><B>hint:</B> Use the <B>help</B> command in Matlab. </P></LI></OL>
  <P>
  <P>
  <DIV><B>Question 4</B> &nbsp; <I>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. <BR>Find a better method for doing these computations.</I></DIV>
  <P></P>
  <P></P>
  <LI><I>Confidence Interval, Quantiles, etc.</I>... 
  <P>
  <DIV><B>Question 5</B> &nbsp; <I>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?</I></DIV>
  <P></P></LI></OL>
<P>
<DIV align=center><B>Computing Lab Section: R/Splus version #1</B></DIV>
<P>
<OL>
  <LI><I>Setting Things Up.</I> <BR><BR>Make a course directory. <PRE>    elaine20:~&gt; mkdir s208
    elaine20:~&gt; cd s208
</PRE>Make a file called <B>drop.s</B> with following lines: <PRE>    drop=function(n){
    out=rnorm(n)
    return(out)}
</PRE>
  <LI><I>Starting R.</I> R/Splus is installed both on the leland machines 
  elaine, tree, etc. and the PCs in the lab <BR>Just type: <PRE>    elaine20:~&gt; R
</PRE>or click on the R icon in the programs section of the start menu. You 
  should see the prompt: <PRE> &gt;
</PRE>
  <P></P>
  <LI><I>Warming Up.</I> We will use the <B>drop</B> function to generate a 
  vector of 10 random numbers. <PRE>&gt; drop(10)
 [1] -0.4710930  0.2545599 -0.1460039 -0.3311608 -0.9997712 -0.9675422
 [7] -0.7016111 -0.4112995 -0.6369118  1.8809128
</PRE>
  <P>
  <DIV><B>Question 1</B> &nbsp; <I>Did you get the same answer as I did? <BR>The 
  random number generator is not really random! How could you explain this? 
  </I></DIV>
  <P></P>
  <P></P>
  <LI><I>Distribution of Sample Mean, Part I</I> <BR><BR>Try typing in: <A 
  name=2></A><PRE>    &gt; sampling=mean(drop(10))   
   &gt; sampling
[1] -0.4255386
</PRE><B>sampling</B> is the mean of a sample of size 10. <BR>
  <P>Next, <A name=1></A><PRE>   &gt; sampling=c(sampling, mean(drop(10)))
   &gt; sampling
 [1] -0.425538589 -0.002214149
</PRE>What does this command do? 
  <P>Now repeat the previous command 10 more times. Stop when you see 
  <B>sampling</B> is a vector of length 12. <BR>(<B>hint</B>: your <IMG 
  height=35 alt=$\uparrow$ src="Labs.files/img2.png" width=14 align=middle 
  border=0> 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). <BR>
  <P>
  <DIV><B>Question 2</B> &nbsp; <I>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?</I></DIV>
  <P></P>
  <P></P>
  <LI><I>Distribution of Sample Mean, Part II</I> 
  <P>How could we get the mean and variance of the means of 100 samples of size 
  10? <BR>
  <OL>
    <LI><I>A clumsy approach.</I> Use a <B>for</B> loop to repeat commands in <A 
    href="http://www-stat.stanford.edu/~susan/courses/s208/node4.html#1"><IMG 
    alt=[*] src="Labs.files/crossref.png" align=bottom border=1></A>. 
    <P>If we want to discard the sampling vector we have so far, we could do: <PRE>     sampling=NULL
     for (i in (1:100))                        {
    sampling=c(sampling, mean(drop(10)))
    }
</PRE>
    <P>
    <DIV><B>Question 3</B> &nbsp; <I>From a computational point of view, this is 
    lousy. Why?</I></DIV>
    <P></P>
    <LI><I>A much cleaner <B>for</B> loop.</I> <PRE>sampling=rep(0,100)
 for (i in (1:100))                        {
sampling[i]=mean(drop(10))
}
</PRE>
    <P></P>
    <LI><I>Is the loop in (b) actually faster than the one in (a)?</I> <BR>Try 
    timing it with 
    <OL>
      <LI><B>system.time</B> - cpu times; <BR><B>hint</B> <PRE>  
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));
)
</PRE>
      <P></P>
      <LI><B>x1</B> - cpu time; 
      <LI><B>x2</B> - user processor time; 
      <LI><B>x3</B> - </LI></OL>
    <P><B>hint:</B> Use the <B>help</B> command in R/Splus. 
    <P>
    <P>
    <DIV><B>Question 4</B> &nbsp; <I>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. <BR>Find a better method for doing 
    these computations.</I></DIV>
    <P></P>
    <P></P>
    <LI><I>Confidence Interval, Quantiles, etc.</I>... 
    <P>
    <DIV><B>Question 5</B> &nbsp; <I>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?</I></DIV>
    <P></P></LI></OL>
  <P>
  <P></P></LI></OL>
<HR>
<!--Navigation Panel--><A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node5.html" 
name=tex2html166><IMG height=24 alt=next src="Labs.files/next.png" width=37 
align=bottom border=0></A> <A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node3.html" 
name=tex2html164><IMG height=24 alt=up src="Labs.files/up.png" width=26 
align=bottom border=0></A> <A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node3.html" 
name=tex2html158><IMG height=24 alt=previous src="Labs.files/prev.png" width=63 
align=bottom border=0></A> <BR><B>Next:</B> <A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node5.html" 
name=tex2html167>Homeworks</A> <B>Up:</B> <A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node3.html" 
name=tex2html165>Labs and Homeworks</A> <B>Previous:</B> <A 
href="http://www-stat.stanford.edu/~susan/courses/s208/node3.html" 
name=tex2html159>Labs and Homeworks</A> <!--End of Navigation Panel-->
<ADDRESS>Susan Holmes 2004-05-19 </ADDRESS></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -