outcomes <- 1:6 outcomes toss <- sample(outcomes,size=2,replace=T) # draw a sample of 2 with replacement from outcomes toss sum(toss) # compute the sum n <- 10000 # number of simulated tosses of 2 dice sums <- rep(0,n) # create a vector to store the simulation results sums # loop to repeat the experiment of sampling with replacement from outcomes for (i in 1:n) { toss <- sample(outcomes,size=2,replace=T) sums[i] <- sum(toss) } sums hist(sums) # simple histogram showing frequency hist(sums,freq=F) # simple histogram showing relative frequency s <- seq(from=1.5,by=1,to=12.5) hist(sums,breaks=s,freq=F) # better histogram hist.save <- hist(sums,breaks=s) # hist.save is an object with 8 attributes print(hist.save) empirical <- hist.save$density # relative frequencies of occurence for each of the 11 possible sums theoretical <- c(1,2,3,4,5,6,5,4,3,2,1)/36 # theoretical probabilities assuming an equally likely sample space cbind(empirical,theoretical) # compare probabilities plot(empirical~theoretical,pch=16,xlab="Theoretical probability",ylab= "Empirical probability") abline(a=0,b=1) # add a line with intercept 0 and slope 1 ############## Homework problem for Friday September 18 ########### # Consider tossing a four-sided object with sides labeled 1,2,3,4. Assume that # each side is equally likely to be face-down after a toss, and that the down-side number is observed. # Suppose that the object is tossed 3 times. # 1. Determine the theoretical distribution of the sum of the 3 tosses. # 2. Modify the script above and compute an empirical distribuion. Compare the distributions. # Verify that they are similar. If not, one or both distribbutions are incorrect. # Turn in: the theoretical probability distribution of X = "sum of the 3 down-sides", a histogram of the # empirical distribution, and a plot of the theoretical vs empirical probabilities. ########################################################################################## ## compute an approximation of the expected value of the sum of 2 dice and compare it to the theoretical value values <- 2:12 theoretical.expected.value <- sum(values*theoretical) c(mean(sums),theoretical.expected.value) # Compute variance and std deviation of the sum variance <- sum((values^2)*theoretical) - theoretical.expected.value^2 std <- sqrt(variance) c(variance,std) # compare to sample variance and standard deviation c(var(sums),sd(sums)) # WARNING: var(sums) and sd(sums) are estimators of variance # and standard deviation, and rarely agree exactly with the # theoretical quantities ############################## Simulating the geometric random variable and its expectation p <- .2 # choose the probability of success n <- 10000 n.trials <- rep(0,n) # create a vector to store the simulation results for (i in 1:n) { stop.trials <- F counter <- 0 while (stop.trials == F){ r <- runif(1,0,1) # generate 1 random number between 0 and 1 counter <- counter + 1 if (r < p) { n.trials[i] <- counter stop.trials <- T } } } n.trials s <- seq(from=0.5,by=1,to=max(n.trials)+.5) hist(n.trials,breaks=s,freq=F) # better histogr c(1/p,mean(n.trials))