10 messages
Open this post in threaded view
|
Report Content as Inappropriate

 This post was updated on . Hi, Reading about a "Heads and Tails" game in Introduction to Probability (Example 1.4, pp. 5-8). You toss a coin 40 times. If heads, Peter wins \$1, tails, he loses \$1. I think I can do that ok with: winnings <- sum(sample(c(-1,1), 40, replace=TRUE)) But I have to do it 10,000 times and I have to record and collect the winnings. In other languages, I would probably use a for loop and "push" each winnings into some sort of collective array or vector. However, for loops seem to be discouraged in R and it seems the same can be said for "pushing" a calculation onto a vector. So, can someone give me some guidance on how to collect 10,000 winnings? The second part of the game asks us to keep track of how often Peter is in the lead during each game. So, I'll probably use: cum_winnings <- cumsum(sample(c(-1,1), 40, replace=TRUE)) Obviously, he is in the lead at any moment his cumulative winnings are positive. But the game requires that we also do something at the moment the cumulative winnings are zero. (1) if the previous cumulative sum was nonnegative, then the zero counts a "staying in the lead." So, for example, during a single game, Peter might be in the lead for say 34 out of the 40 tosses. I must record the 34 and perform the game 9,999 more times, each time recording the number of times that Peter is in the lead. So again, any thoughts on how to do  this without for loops and "pushing?" Thanks for the help. Great list. David Arnold College of the Redwoods Eureka, CA http://msemac.redwoods.edu/~darnold/index.php
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Head or Tails game

Open this post in threaded view
|
Report Content as Inappropriate

## Re: Head or Tails game

 David, set.seed(123)  # always good to make reproducible winnings <- sum(sample(c(-1,1), 10000, replace=TRUE)) Unfortunately, that's not the game. The game requires 40 flips of a coin. Then you have to play the game 10,000 times. D.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Head or Tails game

 On Aug 3, 2012, at 9:14 PM, darnold <[hidden email]> wrote: > David, > > set.seed(123)  # always good to make reproducible > winnings <- sum(sample(c(-1,1), 10000, replace=TRUE)) > > Unfortunately, that's not the game. The game requires 40 flips of a coin. > > Then you have to play the game 10,000 times. > colSums(matrix(sample(c(-1, 1), 40*10000, TRUE), ncol = 10000)) or some such Michael > D. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Head-or-Tails-game-tp4639142p4639145.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Head or Tails game

 In reply to this post by David Arnold HI, You could try this: set.seed(112) list1<-vector("list",1000) for(i in 1:1000){  list1[[i]]<-sample(c(-1,1),40,replace=TRUE)}  dat1<-do.call(rbind,lapply(list1,function(x) sum(x))) dat2<-matrix(dat1,ncol=20,byrow=TRUE) head(dat2)  #    [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] #[1,]   -2   12    0    8    0    4   -8    2   -8    -4   -14     8    -4   -12 #[2,]    8    4   -4    4   12   -2   -6    0   -8     8    -8    12     2     2 #[3,]   -2    8    2   -6  -12   -6    8    6   -4     4     4     8     6    10 #[4,]    6    6    0    4   10   -8   -4   -2    4   -10    -6    -2     4     2 #[5,]   -6   -4   -6   -6    4    0  -14   -2    0    -8    -6     2     4    -8 #[6,]    0   10    0    0  -14   -2    2    0    6   -10     4     0    -4     4   #   [,15] [,16] [,17] [,18] [,19] [,20] #[1,]     4    12    -6     4    10     8 #[2,]    -6     0     4     8     0    12 #[3,]    -2    -4    10   -10     6   -10 #[4,]     2     6     4     6     8     6 #[5,]    10     0    -6    -2    10    -8 #[6,]     0   -12    16    -2     2     4 #system time for 10,000 times system.time({  set.seed(112)  list1<-vector("list",10000)  for(i in 1:10000){   list1[[i]]<-sample(c(-1,1),40,replace=TRUE)}   dat1<-do.call(rbind,lapply(list1,function(x) sum(x)))  dat2<-matrix(dat1,ncol=200,byrow=TRUE)  })    user  system elapsed   0.112   0.000   0.111 A.K. ----- Original Message ----- From: darnold <[hidden email]> To: [hidden email] Cc: Sent: Friday, August 3, 2012 10:14 PM Subject: Re: [R] Head or Tails game David, set.seed(123)  # always good to make reproducible winnings <- sum(sample(c(-1,1), 10000, replace=TRUE)) Unfortunately, that's not the game. The game requires 40 flips of a coin. Then you have to play the game 10,000 times. D. -- View this message in context: http://r.789695.n4.nabble.com/Head-or-Tails-game-tp4639142p4639145.htmlSent from the R help mailing list archive at Nabble.com. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Head or Tails game

 In reply to this post by David Arnold Use a loop to run over the 10,000 reps and in each rep use rle(x>=0) to look at the runs of nonnegative numbers, postprocessing its output a bit to count them. Bill Dunlap Spotfire, TIBCO Software wdunlap tibco.com > -----Original Message----- > From: [hidden email] [mailto:[hidden email]] On Behalf > Of darnold > Sent: Friday, August 03, 2012 7:14 PM > To: [hidden email] > Subject: Re: [R] Head or Tails game > > David, > > set.seed(123)  # always good to make reproducible > winnings <- sum(sample(c(-1,1), 10000, replace=TRUE)) > > Unfortunately, that's not the game. The game requires 40 flips of a coin. > > Then you have to play the game 10,000 times. > > D. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Head-or-Tails-game-> tp4639142p4639145.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.
Open this post in threaded view
|
Report Content as Inappropriate

## Re: Head or Tails game

Open this post in threaded view
|
Report Content as Inappropriate

## Re: Head or Tails game

Open this post in threaded view
|
Report Content as Inappropriate

## Re: Head or Tails game

 In reply to this post by Michael Weylandt Hi, system.time({ set.seed(111) colSums(matrix(sample(c(-1, 1), 40*10000, TRUE), ncol = 10000)) }) user  system elapsed   0.032   0.012   0.041  system.time({  set.seed(112)  list1<-vector("list",10000)  for(i in 1:10000){   list1[[i]]<-sample(c(-1,1),40,replace=TRUE)}   dat1<-do.call(rbind,lapply(list1,function(x) sum(x)))  dat2<-matrix(dat1,ncol=200,byrow=TRUE)  })    user  system elapsed   0.112   0.000   0.111  #modified version system.time({  set.seed(112)  list1<-vector("list",10000)  for(i in 1:10000){   list1[[i]]<-sample(c(-1,1),40,replace=TRUE)}   dat1<-unlist(lapply(list1,function(x) sum(x)))   })  user  system elapsed  0.092   0.000   0.092 It seems like Michael's solution is better in terms of the CPU utilization.  I guess, the loop created the difference. A.K. ----- Original Message ----- From: Michael Weylandt <[hidden email]> To: darnold <[hidden email]> Cc: "[hidden email]" <[hidden email]> Sent: Friday, August 3, 2012 10:20 PM Subject: Re: [R] Head or Tails game On Aug 3, 2012, at 9:14 PM, darnold <[hidden email]> wrote: > David, > > set.seed(123)  # always good to make reproducible > winnings <- sum(sample(c(-1,1), 10000, replace=TRUE)) > > Unfortunately, that's not the game. The game requires 40 flips of a coin. > > Then you have to play the game 10,000 times. > colSums(matrix(sample(c(-1, 1), 40*10000, TRUE), ncol = 10000)) or some such Michael > D. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Head-or-Tails-game-tp4639142p4639145.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > [hidden email] mailing list > https://stat.ethz.ch/mailman/listinfo/r-help> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code.