rstatisticsmeanvariancedie

Finding mean and variance in R


Is what I did correct in making a code for this problem?

My goal is to find the mean and variance of the total score of the team when for any pair of gamers who throw the same number, the team scores 1 point.

die <- function(n){sample(1:6, n, replace = TRUE)}

A <- function(Die){
  int <- table(Die)
  sum(as.integer(int/2))
}

rolls <- 10000
players <- 10
scoreA <- sapply(1:rolls, function(x) {
  rolled <- die(players)
  A(rolled)
})

mean(scoreA)
var(scoreA)

(https://i.sstatic.net/PQh78.jpg)


Solution

  • A quick mental run through the problem should tell us approximately the answer we should expect.

    If there are 10 players, then we have 5 pairs of players. Think about any one of those pairs. Since the throws are all independent, it doesn't matter what the first player throws; there is a one-in-six chance that the second player will throw the same number. Since the pair gets one point if the numbers match, the expected value in terms of points for a single pair throwing the dice will be 1 point * 1/6 = 1/6 points. Since there are five pairs on the team, the expected value of the team's score for each round will be 5 * 1/6 points = 5/6, or about 0.8333.

    When I run your code, I get a value for mean(scoreA) of 3.5, so clearly this isn't correct.

    A naive implementation that simulates each throw would be:

    players_roll_once <- function(players) 
    {
      # Ensure that only an even number of players on the team
      if(players %% 2 != 0) stop("Need an even number of players")
      
      # The first member of each pair rolls their dice
      first_members_rolls  <- sample(6, players / 2, replace = TRUE)
    
      # The second member of each pair rolls their dice
      second_members_rolls <- sample(6, players / 2, replace = TRUE)
      
      # Assign one for each time the first and second member of each pair match
      scored_a_point <- as.numeric(first_members_rolls == second_members_rolls)
    
      # Add up all the points the team got and return the answer
      return(sum(scored_a_point))
    }
    

    And we can use the replicate function to run the simulation as many times as we like to get scoreA:

    rolls   <- 100000
    players <- 10
    
    set.seed(1) # Makes this reproducible
    scoreA  <- replicate(rolls, players_roll_once(players))
    

    And the results are:

    mean(scoreA)
    #> [1] 0.83605
    
    var(scoreA)
    #> [1] 0.6985974