Advertisement
Guest User

Probability that the sum of 10d4 beat the sum of 7d6

a guest
Feb 18th, 2025
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.70 KB | Source Code | 0 0
  1. ######################################################################
  2. # Calculate the probability that the sum of 10d4 beat the sum of 7d6
  3. # using convolutions to the PMFs and, hence, the joint PMF
  4. ######################################################################
  5.  
  6. #' Function to convolute two discrete probability distributions with
  7. #' support on 0, 1, ... I.e. we compute the PMF of Z = X + Y.
  8. #'
  9. #' @param fX - the PMF of X given as a named vector, where the names
  10. #' represent the support X_min, ..., 0, 1, ..., X_max
  11. #' @param fY - the PMF of Y given as a named vector, where the names
  12. #' represent the support Y_min, ..., 0, 1, ..., Y_max
  13. #'
  14. #' @return A named vector containing the PMF of Z = X+Y.
  15. #'
  16. convolute <- function(fX, fY) {
  17.   fZ <- rep(0, 1 + (length(fX) - 1) + (length(fY) - 1))
  18.   names(fZ) <- as.character(seq(
  19.     min(as.numeric(names(fX))) + min(as.numeric(names(fY))),
  20.     max(as.numeric(names(fX))) + max(as.numeric(names(fY)))
  21.   ))
  22.   for (i in names(fX)) {
  23.     for (k in names(fY)) {
  24.       j <- as.numeric(i) + as.numeric(k)
  25.       fZ[as.character(j)] <- fZ[as.character(j)] + fX[i] * fY[k]
  26.     }
  27.   }
  28.   fZ
  29. }
  30.  
  31. # Function to compute the PMF of X = \sum_{i=1}^n X_i when the
  32. # X_i are iid RV with PMF `pmf_one`.
  33. convolute_n_times <- function(n, pmf_one) {
  34.   i <- 1
  35.   pmf <- pmf_one
  36.   while (i<n) {
  37.     i <- i+1
  38.     pmf <- convolute(pmf, pmf_one)
  39.   }
  40.   return(pmf)
  41. }
  42.  
  43. # Compute PMF for the sum of 10d4, i.e. X=\sum_{i=1}^n X_i with X_i being
  44. # iid U(\{1,2,3,4}) RVs.
  45. pmf_d4 <- c(0,rep(1/4, 4)) %>% setNames(0:4)
  46. pmf_10d4 <- convolute_n_times(n=10, pmf_d4)
  47. plot(pmf_10d4, type="h", xlab="x", ylab=expression(f[X](x)))
  48. # Calculate expectation of X
  49. 10 * sum(0:4 * pmf_d4)
  50. sum(0:40 * pmf_10d4)
  51.  
  52. # Compute PMF for the sum of 7d6, i.e. Y=\sum_{i=1}^n Y_i with Y_i being
  53. # iid U(\{1,2,3,4,5,6}) RVs.
  54. pmf_d6 <- c(0,rep(1/6, 6)) %>% setNames(0:6)
  55. pmf_7d6 <- convolute_n_times(n=7, pmf_d6)
  56. plot(pmf_7d6, type="h", xlab="y", ylab=expression(f[Y](y)))
  57. # Expectations
  58. 7*sum(0:6 * pmf_d6)
  59. sum(0:(7*6) * pmf_7d6)
  60.  
  61. # Calculate the joint probability of X and Y (which are independent)
  62. pmf_XY <- outer(pmf_10d4, pmf_7d6, "*")
  63. image(x=as.numeric(names(pmf_10d4)), y=as.numeric(names(pmf_7d6)), pmf_XY, xlab="x", ylab="y")
  64. # Rows correspond to value of X, columns correspond to columns of Y
  65. rownames(pmf_XY)
  66. colnames(pmf_XY)
  67. # Indicator function I(x>y)
  68. d4_win <- (row(pmf_XY)-1) > (col(pmf_XY)-1)
  69. # P(X>Y) = E(I(X>Y))
  70. # i.e. P(X>Y) = \sum_{x=0}^{40} \sum_{y=0}^{42} I(x>y) f_{X}(x) f_{Y}(y).
  71. sum(d4_win * pmf_XY)
  72.  
  73. # Alternative:
  74. # https://topps.diku.dk/torbenm/troll.msp
  75. # # Variation of Euler problem 205 with dice numbers yielding a prob of 1/2.
  76. # count ((sum 10d4) > (sum 7d6))
  77.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement