Question

In one play of a certain game you win an amount X with distribution:

\[ px = \begin{pmatrix} 1 & 2 & 3\\ 1/4 & 1/4 & 1/2 \end{pmatrix} \]

Using the program NFoldConvolution find the distribution for your total winnings after ten (independent) plays. Plot this distribution.

Christian’s Response:

To calculate the distribution of total winnings after ten plays, we have to create a function that can do this for us since the function NFoldConvolution is no longer part of the distr package.

Below we will re-create and store the probability distribution and possible winnings.

pX <- c(1/4,1/4,1/2)
x <- c(1,2,3)

In the function total_winning, it is programmed to calculate the total winnings after n of plays. The replicate function finds every combination of vector x based on the number n. It is transformed into a matrix object and then all the possible sums of winning are summed together. Finally we multiply the probability of each winning amount and then divide the result by their sum to get the normalized probability of each winning value.

total_winning <- function(n) {
  # calculate sum of total winnings
  sums <- expand.grid(replicate(n, x, simplify = FALSE))
  sums <-  as.matrix(sums)
  rowSums(sums)
  
  # find probability of each sum
  probs <- apply(sums, 1, function(row) prod(pX[row]))
  probs / sum(probs)
}
# find distribution of winnings after ten plays
game_ten <- total_winning(10)
# the distribution
barplot(game_ten, main = "Distribution of Total Winnings after 10 Plays", 
        xlab = "Total Winnings", ylab = "Probability")