lab06.html - 404449070_lab06 Linda Che 1 1 gamblersRuin <...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
404449070_lab06 Linda Che 3/10/2017 1 1. gamblersRuin <- function(n1, n2, p){ len <- 0 repeat{ a <- rbinom(1,1,p) len<-len+1 if(a==1){ n1 <- n1+1 n2 <- n2-1 } if(a==0){ n2 <-n2 + 1 n1<- n1-1 } if(n1==0|n2==0){ break } } v <- c(len=len,n1=n1,n2=n2) return(v) } set.seed(2017) gamblersRuin(n1 = 10, n2 = 10, p = 0.5) len n1 n2 196 0 20 2. res <- matrix(0,1000,3) for(i in 1:1000){ res[i,]<-gamblersRuin(n1 = 5, n2 = 10, p = 0.5) } res[1:10,] [,1] [,2] [,3] [1,] 127 0 15 [2,] 15 0 15 [3,] 90 15 0 [4,] 9 0 15 [5,] 44 15 0 [6,] 27 0 15 [7,] 23 0 15 [8,] 61 0 15 [9,] 12 15 0 [10,] 5 0 15 3.
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
#expected number of tosses mean(res[,1]) [1] 51.41 #probabilty of going broke length(res[,2][res[,2]== 0])/1000 [1] 0.648 4. res1 <- matrix(0,1000,3) for(i in 1:1000){ res1[i,]<-gamblersRuin(n1 = 5, n2 = 1000, p = 0.49) } res1[1:10,] [,1] [,2] [,3] [1,] 59 0 1005 [2,] 475 0 1005 [3,] 19 0 1005 [4,] 9 0 1005 [5,] 455 0 1005 [6,] 7 0 1005 [7,] 73 0 1005 [8,] 29 0 1005 [9,] 15 0 1005 [10,] 51 0 1005 #probabilty of going broke length(res1[,2][res1[,2]== 0])/1000 [1] 1 #expected number of tosses mean(res1[,1]) [1] 258.324 2 1. mat <- as.matrix(iris[, 1:4]) df <- iris[, 1:4] #colSumsFor colSumsFor <- function(x) { v <- vector(length = ncol(x)) for (i in 1:ncol(x)){ v[i]<-sum(x[,i]) } names(v) <- colnames(x) v } colSumsFor(mat) Sepal.Length Sepal.Width Petal.Length Petal.Width 876.5 458.6 563.7 179.9 #colSumsFor2 colSumsFor2 <- function(x) {
Image of page 2
x <- as.matrix(x)
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern