10.07.2015 Views

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

Using R for Introductory Statistics : John Verzani

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Simulation 1716.5.2 The geometric distributionIn a sequence of i.i.d. Bernoulli trials, there is a time of the first success. This can happenon the first trial, the second trial, or at any other point. Let X be the time of the firstsuccess. Then X is a random variable with distribution on the positive integers. Thedistribution of X is called the geometric distribution and isf(k)=P(X=k)=(1−p) k−1 p.Let’s simulate the random variable X to investigate its mean. To find a single samplefrom the distribution of X we can toss coins until we have a success. A while() loop isideal <strong>for</strong> this type of situation, as we don’t know in advance how many times we willneed to toss the coin.Figure 6.6 Simulation of standarddeviation and IQR <strong>for</strong> Exponential(1)datafirst.success = function(p) {k = a;success = FALSEwhile(success == FALSE) {k = k + 1if(rbinom(1,1,p) == 1) success = TRUE}kThe while loop repeats a block of commands until its condition is met. In this case, thevalue of success is not FALSE. Inside the while loop, an if statement is used. When the ifstatement is true, it sets the value of success to TRUE, effectively terminating the whileloop. The command rbinom(1, 1, p) is one sample from the Binomial(1, p) distribution—basically a coin toss.We should expect that if the success probability is small, then the time of the firstsuccess should be large. We compare the distribution with p=.5 and with p=.05 usingsummary().

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!