Problem 1

Extend the simulation of throwing two dice in Section 3.2.1 in each of the following ways (one at a time, not cumulatively).

a. Instead of 50 throws, extend the spreadhseet to make 500 throws and compare your results. Tap the F9 key to get a “fresh” set of random numbers and thus a fresh set of results.

two_dice_throws <- function(throws, 
                            die1_prob = (c(1, 1, 1, 1, 1, 1)/6), 
                            die2_prob = (c(1, 1, 1, 1, 1, 1)/6)){
  dice_throw  <- list(roll  =  seq(1:throws),
                      die1  =  sample(1:6, throws, replace = TRUE, prob = die1_prob), 
                      die2  =  sample(1:6, throws, replace = TRUE, prob = die2_prob))
  dice_throw$sum <- unlist(lapply(seq(1:throws), function(i) unlist(dice_throw$die1[i]) + 
                                                             unlist(dice_throw$die2[i])))
  return(dice_throw)
}

two_dice_throws(500) %>% lapply(function(x) list(mean(x), 
                                                 min(x), 
                                                 max(x))) 
## $roll
## $roll[[1]]
## [1] 250.5
## 
## $roll[[2]]
## [1] 1
## 
## $roll[[3]]
## [1] 500
## 
## 
## $die1
## $die1[[1]]
## [1] 3.566
## 
## $die1[[2]]
## [1] 1
## 
## $die1[[3]]
## [1] 6
## 
## 
## $die2
## $die2[[1]]
## [1] 3.39
## 
## $die2[[2]]
## [1] 1
## 
## $die2[[3]]
## [1] 6
## 
## 
## $sum
## $sum[[1]]
## [1] 6.956
## 
## $sum[[2]]
## [1] 2
## 
## $sum[[3]]
## [1] 12

b. Load the dice by changing the probabilites of the faces to be something other than uniform at 1/6 for each face. Be careful to exam that your probabilties sum to 1.

two_dice_throws(500, 
                die1_prob = (c(.51, 1.7, .29, .5, 1.5, 1.5)/6),
                die2_prob = c(3, .5, .006, 1.03, 1.454, .01)/6) %>% 
  lapply(function(x) list(mean(x), min(x), max(x))) 
## $roll
## $roll[[1]]
## [1] 250.5
## 
## $roll[[2]]
## [1] 1
## 
## $roll[[3]]
## [1] 500
## 
## 
## $die1
## $die1[[1]]
## [1] 3.834
## 
## $die1[[2]]
## [1] 1
## 
## $die1[[3]]
## [1] 6
## 
## 
## $die2
## $die2[[1]]
## [1] 2.578
## 
## $die2[[2]]
## [1] 1
## 
## $die2[[3]]
## [1] 5
## 
## 
## $sum
## $sum[[1]]
## [1] 6.412
## 
## $sum[[2]]
## [1] 2
## 
## $sum[[3]]
## [1] 11

I purposely loaded the dice very heavily for 1 in die 2 so that it would be clear how the impact of loading the dice would affect the outcome. Slight loading in die 1 didn’t have as dramatic of an impact as die 2.

c. Use @RISK or another Excel add-in for status Monte Carlo, spreadsheet simulation, to make 10,000 throws of the pair of fair dice, and compare your results to the true probabilities of getting the sum equal to each of 2,3,…,12 as well as to the true expected value of 7.

t <- two_dice_throws(10000)
hist(t$sum, breaks = 8)

list(mean(t$sum), min(t$sum), max(t$sum)) 
## [[1]]
## [1] 7.0279
## 
## [[2]]
## [1] 2
## 
## [[3]]
## [1] 12

Based on the 10000 simulations of a pair of fair dice, our expected values are very close to the true expected values. The distribution is normal with 7 being the mean expected value of the sum of the two fair dice.

Problem 5

In the Monte Carlo integration of Section 3.2.2, add to the spreadsheet calculation of the standard deviation of the 50 individual values, and use that, together with the mean already in cell H4, to compute a 95% confidence interval on the exact integral in cell I4: does your confidence interval contain, or “cover” the exact integral? Repease all of this, but with a 90% confidence interval, and then with a 99% confidence interval.

set.seed(1234)

mu    <- 5.8
sigma <- 2.30
a     <- 4.50
b     <- 6.70

Xi        <- function(x,y){runif(1, min=0, max=1) * (y - x) + x}
b_a_fm_Xi <- function(mu, sigma, x, y, xi){(y - x) *  dnorm(xi,  mu, sigma)} 

MC_ <- NULL
for (i in 1:50){x_i      <- Xi(a,b)
                MC_[[i]] <- list(i = i, xi = x_i, bafmXi = b_a_fm_Xi(mu, sigma, a, b, x_i))
}

sd(MC_ %>% map_dbl("bafmXi"))
## [1] 0.01369319
list(Monte_Carlo = mean(MC_ %>% map_dbl("bafmXi")), 
     Exact = pnorm(b, mu, sigma) - pnorm(a, mu, sigma),
     CI_95 = c(left = mean(MC_ %>% map_dbl("bafmXi")) - qnorm(0.95)*(sd(MC_ %>% map_dbl("bafmXi"))/sqrt(50)), 
              right = mean(MC_ %>% map_dbl("bafmXi")) + qnorm(0.95)*(sd(MC_ %>% map_dbl("bafmXi"))/sqrt(50))),
     CI_90 = c(left = mean(MC_ %>% map_dbl("bafmXi")) - qnorm(0.90)*(sd(MC_ %>% map_dbl("bafmXi"))/sqrt(50)), 
              right = mean(MC_ %>% map_dbl("bafmXi")) + qnorm(0.90)*(sd(MC_ %>% map_dbl("bafmXi"))/sqrt(50))),
     CI_99 = c(left = mean(MC_ %>% map_dbl("bafmXi")) - qnorm(0.99)*(sd(MC_ %>% map_dbl("bafmXi"))/sqrt(50)), 
              right = mean(MC_ %>% map_dbl("bafmXi")) + qnorm(0.99)*(sd(MC_ %>% map_dbl("bafmXi"))/sqrt(50))))
## $Monte_Carlo
## [1] 0.3676404
## 
## $Exact
## [1] 0.3662509
## 
## $CI_95
##      left     right 
## 0.3644551 0.3708257 
## 
## $CI_90
##      left     right 
## 0.3651587 0.3701222 
## 
## $CI_99
##      left     right 
## 0.3631354 0.3721454

We do see that the confidence intervals of all three, 90%, 95%, and 99% covers the exact integral. This demonstrates the power of the Monte Carlo simuation.

Problem 17

Walther has a roadside produce stand where he sells oats, peas, beans, and barley. He buys these products at per-point wholesale prices of respectively, $1.05, $3.17, $1.99, and $0.95.; he sells them at per-pound retail prices of, respectively, $1.29, $3.76, $2.23, and $1.65. Each day the amount demanded (in pounds) could be as little as zero for each product, and as much as 10, 8, 14, and 11 for oats, peas, beans, and barley, respectively; he sells only whole-pound amounts, no partial pounds. Assume a discrete uniform distribution for daily demand for each product over its range; assume as well that Walther always has enough inventory to satisfy all demand. The summer selling season is 90 days, and demand each day is independent of demand on other days. Create a spreadsheet simulation that will, for each day as well for the whole season, simulate Walther’s total cost, total revenue, and total profit.

library(tidyverse)
items <- c("oats", "peas", "beans", "barley")
cost <- c(1.05, 3.17, 1.99, .95)
revenue <- c(1.29, 3.76, 2.23, 1.65)
max_demand <- c(10, 8, 14, 11)
df <- as_tibble(cbind(items, cost, revenue, max_demand))
kable(df)
items cost revenue max_demand
oats 1.05 1.29 10
peas 3.17 3.76 8
beans 1.99 2.23 14
barley 0.95 1.65 11

Simulation Total

library(data.table)
library(scales)

days <- 90
sim <- NULL
for (i in items){
  lbs_sold <- sample(0:df$max_demand[df$items == i], days, replace = TRUE)
  sim[[i]] <- list(day       =  seq(1:days),
                   lbs_sold  =  lbs_sold, 
                   cost      =  lbs_sold * as.numeric(df$cost[df$items == i]),
                   revenue   =  lbs_sold * as.numeric(df$revenue[df$items == i]))
}

dollars <- dollar_format(negative_parens = TRUE)

rbindlist(sim, idcol = TRUE) %>% 
      rename(type = .id) %>%
      group_by(type) %>%
      summarise(cost    = -sum(cost),
                revenue =  sum(revenue)) %>%
      mutate(profit = revenue + cost) %>%
      rbind(data.frame(type = "*TOTAL*", 
                       cost    = sum(.$cost),
                       revenue = sum(.$revenue),
                       profit  = sum(.$profit))) %>%
      mutate_at(vars(-type), funs(. %>% dollars())) %>% 
      kable()
type cost revenue profit
barley ($489.25) $849.75 $360.50
beans ($1,273.60) $1,427.20 $153.60
oats ($417.90) $513.42 $95.52
peas ($1,217.28) $1,443.84 $226.56
TOTAL ($3,398.03) $4,234.21 $836.18

Type sold by each day

library(knitr)
rbindlist(sim, idcol = TRUE) %>%  rename(type = .id) %>% kable()
type day lbs_sold cost revenue
oats 1 0 0.00 0.00
oats 2 3 3.15 3.87
oats 3 7 7.35 9.03
oats 4 5 5.25 6.45
oats 5 1 1.05 1.29
oats 6 5 5.25 6.45
oats 7 5 5.25 6.45
oats 8 8 8.40 10.32
oats 9 1 1.05 1.29
oats 10 9 9.45 11.61
oats 11 9 9.45 11.61
oats 12 0 0.00 0.00
oats 13 3 3.15 3.87
oats 14 0 0.00 0.00
oats 15 2 2.10 2.58
oats 16 7 7.35 9.03
oats 17 3 3.15 3.87
oats 18 5 5.25 6.45
oats 19 0 0.00 0.00
oats 20 6 6.30 7.74
oats 21 1 1.05 1.29
oats 22 9 9.45 11.61
oats 23 0 0.00 0.00
oats 24 8 8.40 10.32
oats 25 0 0.00 0.00
oats 26 5 5.25 6.45
oats 27 4 4.20 5.16
oats 28 0 0.00 0.00
oats 29 3 3.15 3.87
oats 30 7 7.35 9.03
oats 31 10 10.50 12.90
oats 32 5 5.25 6.45
oats 33 1 1.05 1.29
oats 34 5 5.25 6.45
oats 35 2 2.10 2.58
oats 36 9 9.45 11.61
oats 37 4 4.20 5.16
oats 38 3 3.15 3.87
oats 39 1 1.05 1.29
oats 40 9 9.45 11.61
oats 41 1 1.05 1.29
oats 42 9 9.45 11.61
oats 43 1 1.05 1.29
oats 44 1 1.05 1.29
oats 45 1 1.05 1.29
oats 46 5 5.25 6.45
oats 47 3 3.15 3.87
oats 48 0 0.00 0.00
oats 49 3 3.15 3.87
oats 50 8 8.40 10.32
oats 51 0 0.00 0.00
oats 52 6 6.30 7.74
oats 53 3 3.15 3.87
oats 54 2 2.10 2.58
oats 55 1 1.05 1.29
oats 56 3 3.15 3.87
oats 57 1 1.05 1.29
oats 58 1 1.05 1.29
oats 59 4 4.20 5.16
oats 60 0 0.00 0.00
oats 61 7 7.35 9.03
oats 62 1 1.05 1.29
oats 63 10 10.50 12.90
oats 64 1 1.05 1.29
oats 65 2 2.10 2.58
oats 66 10 10.50 12.90
oats 67 10 10.50 12.90
oats 68 3 3.15 3.87
oats 69 1 1.05 1.29
oats 70 8 8.40 10.32
oats 71 8 8.40 10.32
oats 72 10 10.50 12.90
oats 73 10 10.50 12.90
oats 74 10 10.50 12.90
oats 75 5 5.25 6.45
oats 76 3 3.15 3.87
oats 77 2 2.10 2.58
oats 78 5 5.25 6.45
oats 79 5 5.25 6.45
oats 80 3 3.15 3.87
oats 81 10 10.50 12.90
oats 82 6 6.30 7.74
oats 83 1 1.05 1.29
oats 84 4 4.20 5.16
oats 85 10 10.50 12.90
oats 86 5 5.25 6.45
oats 87 9 9.45 11.61
oats 88 6 6.30 7.74
oats 89 6 6.30 7.74
oats 90 9 9.45 11.61
peas 1 4 12.68 15.04
peas 2 8 25.36 30.08
peas 3 2 6.34 7.52
peas 4 4 12.68 15.04
peas 5 3 9.51 11.28
peas 6 5 15.85 18.80
peas 7 6 19.02 22.56
peas 8 5 15.85 18.80
peas 9 8 25.36 30.08
peas 10 5 15.85 18.80
peas 11 3 9.51 11.28
peas 12 2 6.34 7.52
peas 13 0 0.00 0.00
peas 14 7 22.19 26.32
peas 15 2 6.34 7.52
peas 16 8 25.36 30.08
peas 17 5 15.85 18.80
peas 18 8 25.36 30.08
peas 19 3 9.51 11.28
peas 20 4 12.68 15.04
peas 21 3 9.51 11.28
peas 22 5 15.85 18.80
peas 23 3 9.51 11.28
peas 24 2 6.34 7.52
peas 25 0 0.00 0.00
peas 26 5 15.85 18.80
peas 27 3 9.51 11.28
peas 28 0 0.00 0.00
peas 29 7 22.19 26.32
peas 30 2 6.34 7.52
peas 31 6 19.02 22.56
peas 32 5 15.85 18.80
peas 33 6 19.02 22.56
peas 34 3 9.51 11.28
peas 35 3 9.51 11.28
peas 36 6 19.02 22.56
peas 37 3 9.51 11.28
peas 38 5 15.85 18.80
peas 39 1 3.17 3.76
peas 40 2 6.34 7.52
peas 41 4 12.68 15.04
peas 42 3 9.51 11.28
peas 43 5 15.85 18.80
peas 44 0 0.00 0.00
peas 45 8 25.36 30.08
peas 46 0 0.00 0.00
peas 47 7 22.19 26.32
peas 48 5 15.85 18.80
peas 49 2 6.34 7.52
peas 50 6 19.02 22.56
peas 51 5 15.85 18.80
peas 52 8 25.36 30.08
peas 53 1 3.17 3.76
peas 54 7 22.19 26.32
peas 55 7 22.19 26.32
peas 56 7 22.19 26.32
peas 57 7 22.19 26.32
peas 58 6 19.02 22.56
peas 59 8 25.36 30.08
peas 60 5 15.85 18.80
peas 61 5 15.85 18.80
peas 62 4 12.68 15.04
peas 63 2 6.34 7.52
peas 64 6 19.02 22.56
peas 65 4 12.68 15.04
peas 66 6 19.02 22.56
peas 67 2 6.34 7.52
peas 68 3 9.51 11.28
peas 69 1 3.17 3.76
peas 70 8 25.36 30.08
peas 71 5 15.85 18.80
peas 72 2 6.34 7.52
peas 73 1 3.17 3.76
peas 74 6 19.02 22.56
peas 75 5 15.85 18.80
peas 76 8 25.36 30.08
peas 77 5 15.85 18.80
peas 78 6 19.02 22.56
peas 79 4 12.68 15.04
peas 80 7 22.19 26.32
peas 81 3 9.51 11.28
peas 82 0 0.00 0.00
peas 83 2 6.34 7.52
peas 84 3 9.51 11.28
peas 85 1 3.17 3.76
peas 86 4 12.68 15.04
peas 87 7 22.19 26.32
peas 88 3 9.51 11.28
peas 89 4 12.68 15.04
peas 90 4 12.68 15.04
beans 1 11 21.89 24.53
beans 2 8 15.92 17.84
beans 3 1 1.99 2.23
beans 4 12 23.88 26.76
beans 5 8 15.92 17.84
beans 6 3 5.97 6.69
beans 7 11 21.89 24.53
beans 8 4 7.96 8.92
beans 9 7 13.93 15.61
beans 10 14 27.86 31.22
beans 11 6 11.94 13.38
beans 12 3 5.97 6.69
beans 13 3 5.97 6.69
beans 14 10 19.90 22.30
beans 15 14 27.86 31.22
beans 16 7 13.93 15.61
beans 17 11 21.89 24.53
beans 18 8 15.92 17.84
beans 19 14 27.86 31.22
beans 20 11 21.89 24.53
beans 21 7 13.93 15.61
beans 22 8 15.92 17.84
beans 23 3 5.97 6.69
beans 24 4 7.96 8.92
beans 25 0 0.00 0.00
beans 26 8 15.92 17.84
beans 27 3 5.97 6.69
beans 28 0 0.00 0.00
beans 29 8 15.92 17.84
beans 30 7 13.93 15.61
beans 31 12 23.88 26.76
beans 32 0 0.00 0.00
beans 33 8 15.92 17.84
beans 34 4 7.96 8.92
beans 35 1 1.99 2.23
beans 36 1 1.99 2.23
beans 37 11 21.89 24.53
beans 38 0 0.00 0.00
beans 39 0 0.00 0.00
beans 40 11 21.89 24.53
beans 41 5 9.95 11.15
beans 42 11 21.89 24.53
beans 43 5 9.95 11.15
beans 44 11 21.89 24.53
beans 45 0 0.00 0.00
beans 46 7 13.93 15.61
beans 47 12 23.88 26.76
beans 48 8 15.92 17.84
beans 49 4 7.96 8.92
beans 50 5 9.95 11.15
beans 51 5 9.95 11.15
beans 52 6 11.94 13.38
beans 53 13 25.87 28.99
beans 54 11 21.89 24.53
beans 55 11 21.89 24.53
beans 56 4 7.96 8.92
beans 57 6 11.94 13.38
beans 58 4 7.96 8.92
beans 59 10 19.90 22.30
beans 60 12 23.88 26.76
beans 61 9 17.91 20.07
beans 62 6 11.94 13.38
beans 63 14 27.86 31.22
beans 64 3 5.97 6.69
beans 65 9 17.91 20.07
beans 66 11 21.89 24.53
beans 67 10 19.90 22.30
beans 68 1 1.99 2.23
beans 69 9 17.91 20.07
beans 70 4 7.96 8.92
beans 71 5 9.95 11.15
beans 72 14 27.86 31.22
beans 73 8 15.92 17.84
beans 74 6 11.94 13.38
beans 75 14 27.86 31.22
beans 76 6 11.94 13.38
beans 77 2 3.98 4.46
beans 78 14 27.86 31.22
beans 79 8 15.92 17.84
beans 80 11 21.89 24.53
beans 81 13 25.87 28.99
beans 82 10 19.90 22.30
beans 83 6 11.94 13.38
beans 84 6 11.94 13.38
beans 85 2 3.98 4.46
beans 86 2 3.98 4.46
beans 87 2 3.98 4.46
beans 88 6 11.94 13.38
beans 89 5 9.95 11.15
beans 90 12 23.88 26.76
barley 1 2 1.90 3.30
barley 2 10 9.50 16.50
barley 3 4 3.80 6.60
barley 4 1 0.95 1.65
barley 5 4 3.80 6.60
barley 6 4 3.80 6.60
barley 7 5 4.75 8.25
barley 8 2 1.90 3.30
barley 9 7 6.65 11.55
barley 10 11 10.45 18.15
barley 11 8 7.60 13.20
barley 12 10 9.50 16.50
barley 13 11 10.45 18.15
barley 14 2 1.90 3.30
barley 15 5 4.75 8.25
barley 16 4 3.80 6.60
barley 17 4 3.80 6.60
barley 18 0 0.00 0.00
barley 19 11 10.45 18.15
barley 20 4 3.80 6.60
barley 21 11 10.45 18.15
barley 22 3 2.85 4.95
barley 23 6 5.70 9.90
barley 24 11 10.45 18.15
barley 25 4 3.80 6.60
barley 26 3 2.85 4.95
barley 27 0 0.00 0.00
barley 28 8 7.60 13.20
barley 29 11 10.45 18.15
barley 30 0 0.00 0.00
barley 31 2 1.90 3.30
barley 32 8 7.60 13.20
barley 33 1 0.95 1.65
barley 34 8 7.60 13.20
barley 35 11 10.45 18.15
barley 36 11 10.45 18.15
barley 37 7 6.65 11.55
barley 38 8 7.60 13.20
barley 39 5 4.75 8.25
barley 40 9 8.55 14.85
barley 41 0 0.00 0.00
barley 42 6 5.70 9.90
barley 43 5 4.75 8.25
barley 44 3 2.85 4.95
barley 45 10 9.50 16.50
barley 46 11 10.45 18.15
barley 47 7 6.65 11.55
barley 48 2 1.90 3.30
barley 49 9 8.55 14.85
barley 50 8 7.60 13.20
barley 51 11 10.45 18.15
barley 52 3 2.85 4.95
barley 53 10 9.50 16.50
barley 54 5 4.75 8.25
barley 55 3 2.85 4.95
barley 56 4 3.80 6.60
barley 57 9 8.55 14.85
barley 58 1 0.95 1.65
barley 59 5 4.75 8.25
barley 60 7 6.65 11.55
barley 61 3 2.85 4.95
barley 62 2 1.90 3.30
barley 63 0 0.00 0.00
barley 64 1 0.95 1.65
barley 65 2 1.90 3.30
barley 66 1 0.95 1.65
barley 67 8 7.60 13.20
barley 68 2 1.90 3.30
barley 69 11 10.45 18.15
barley 70 11 10.45 18.15
barley 71 8 7.60 13.20
barley 72 8 7.60 13.20
barley 73 8 7.60 13.20
barley 74 7 6.65 11.55
barley 75 2 1.90 3.30
barley 76 11 10.45 18.15
barley 77 6 5.70 9.90
barley 78 6 5.70 9.90
barley 79 0 0.00 0.00
barley 80 4 3.80 6.60
barley 81 8 7.60 13.20
barley 82 4 3.80 6.60
barley 83 9 8.55 14.85
barley 84 9 8.55 14.85
barley 85 6 5.70 9.90
barley 86 2 1.90 3.30
barley 87 9 8.55 14.85
barley 88 1 0.95 1.65
barley 89 7 6.65 11.55
barley 90 4 3.80 6.60