roll1 = sample(1:6, 500, replace = TRUE)
roll2 = sample(1:6, 500, replace = TRUE)

Q1.1 = data.frame(roll1,roll2)
kable(tail(Q1.1))
roll1 roll2
495 3 6
496 2 1
497 5 4
498 4 2
499 6 6
500 3 3
mean(roll1+roll2)
## [1] 6.906
roll1 = sample(1:6, 500, replace = TRUE, prob = c(.5,.4,.01,.02,.03,.04))
roll2 = sample(1:6, 500, replace = TRUE, prob = c(.04,0.03,.02,0.01,.4,.5))

Q1.2 = data.frame(roll1,roll2)
kable(head(Q1.2))
roll1 roll2
1 5
1 5
2 5
2 5
5 5
2 6
mean(roll1+roll2)
## [1] 6.91
dice = 2

Q1.3 = replicate(10000, (function()sum(sample(1:6,2,replace=TRUE)))())

probabilities = function(x,y){
  return(sum(x == y)/length(x))
}

myProbabilities = c(2:12)
myProbabilities = sapply(myProbabilities,probabilities,x=Q1.3)

trueProbabilites = dice::getSumProbs(2,6)

difference = myProbabilities - trueProbabilites$probabilities[,2]

difference
##  [1] -1.777778e-03 -1.555556e-04  6.666667e-05 -1.711111e-03 -1.288889e-03
##  [6]  3.833333e-03  4.511111e-03 -1.111111e-04  3.266667e-03 -7.355556e-03
## [11]  7.222222e-04

u = 5.8
v = 2.3
a = 4.5
b = 6.7


#my random variable function

ucdrv = function(a,b){
  return(a+(b-a)*runif(1,min=0,max=1))}

ucdrv(a,b)
## [1] 6.173967
 #Exact Integral
exact = (pnorm(b,mean = u,sd =v) -pnorm(a,mean = u,sd=v)) 


Xi = replicate(50,ucdrv(a,b))

# formula from book
bafmsxi = function(xi,a,b,u,v){
  (b-a)*dnorm(xi,mean=u,sd=v)
}

results = sapply(Xi,bafmsxi,a=a,b=b,u=u,v=v)


mean(results)
## [1] 0.364329
plot(density(Xi))

plot(density(results))

CI(Xi,ci=.99)
##    upper     mean    lower 
## 5.903459 5.639698 5.375937
CI(Xi,ci=.9)
##    upper     mean    lower 
## 5.804704 5.639698 5.474691

It looks like the 99% CI exceeds the exact benchmark.

I’ll assume Walther restocks every morning; and calculate daily profit according to what he had to add to the mornings inventory to meet max demand

Data first

oats = c(1.05,1.29,10)
peas = c(3.17,3.76,8)
beans = c(1.99,2.23,14)
barley = c(.95,1.65,11)

Took a formula from the textbook

#Uniform distribution discrete random variable
uddrv = function(a,b){
  return(floor(a+(b+1-a)*runif(1,min=0,max=1)))
  
}

#Lets create a spreadsheet for each product, then combine them
profits = function(item,days){
  spreadsheet = data.frame(matrix(vector(), days, 5,
                dimnames=list(c(), c("New day inventory","cost", "h", 'revenue', "profit"))),
                stringsAsFactors=F)
  
  spreadsheet[1,] = c(0,item[1]*item[3],0,0,-item[1]*item[3])
  
  
  
  for(i in 2:nrow(spreadsheet)) {
    
    previousRow = spreadsheet[i-1,]
    row = spreadsheet[i,]
    
    newDayInventory = item[3]-previousRow[3]
    costToStock = (item[3]-newDayInventory)*item[1]
    itemsSold = uddrv(0,item[3])
    sales = itemsSold*item[2]
    profit = sales-costToStock
    
    newRow = c(newDayInventory,costToStock,itemsSold,sales,profit)
    spreadsheet[i,] =newRow
}
  return(spreadsheet)
}



# Create some spreadsheets per item; along with their totals
oats_summer = profits(oats,90)
oats_summer_totals = colSums(oats_summer)

peas_summer = profits(peas,90)
peas_summer_totals = colSums(peas_summer)

beans_summer = profits(beans,90)
beans_summer_totals = colSums(beans_summer)

barley_summer = profits(barley,90)
barley_summer_totals = colSums(barley_summer)

walthers_food_emporium = barley_summer+oats_summer+peas_summer+beans_summer
walthers_totals =  colSums(walthers_food_emporium)

#combine em
Walthers_chart = rbind(walthers_totals,oats_summer_totals,peas_summer_totals,beans_summer_totals,barley_summer_totals)

So here is a chart you’ve approximately seen, in every other discussion.

kable(Walthers_chart)
New.day.inventory cost h revenue profit
walthers_totals 2052 3118.14 1806 3867.18 749.04
oats_summer_totals 505 414.75 395 509.55 94.80
peas_summer_totals 389 1049.27 326 1225.76 176.49
beans_summer_totals 667 1180.07 589 1313.47 133.40
barley_summer_totals 491 474.05 496 818.40 344.35

Here is the day by day for peas

peas_summer[nrow(peas_summer)+1,] = peas_summer_totals
kable(peas_summer)
New.day.inventory cost h revenue profit
0 25.36 0 0.00 -25.36
8 0.00 3 11.28 11.28
5 9.51 4 15.04 5.53
4 12.68 6 22.56 9.88
2 19.02 5 18.80 -0.22
3 15.85 6 22.56 6.71
2 19.02 6 22.56 3.54
2 19.02 2 7.52 -11.50
6 6.34 3 11.28 4.94
5 9.51 8 30.08 20.57
0 25.36 1 3.76 -21.60
7 3.17 2 7.52 4.35
6 6.34 4 15.04 8.70
4 12.68 2 7.52 -5.16
6 6.34 2 7.52 1.18
6 6.34 5 18.80 12.46
3 15.85 3 11.28 -4.57
5 9.51 8 30.08 20.57
0 25.36 0 0.00 -25.36
8 0.00 8 30.08 30.08
0 25.36 3 11.28 -14.08
5 9.51 7 26.32 16.81
1 22.19 2 7.52 -14.67
6 6.34 4 15.04 8.70
4 12.68 7 26.32 13.64
1 22.19 7 26.32 4.13
1 22.19 0 0.00 -22.19
8 0.00 1 3.76 3.76
7 3.17 6 22.56 19.39
2 19.02 1 3.76 -15.26
7 3.17 2 7.52 4.35
6 6.34 5 18.80 12.46
3 15.85 8 30.08 14.23
0 25.36 2 7.52 -17.84
6 6.34 8 30.08 23.74
0 25.36 4 15.04 -10.32
4 12.68 3 11.28 -1.40
5 9.51 0 0.00 -9.51
8 0.00 1 3.76 3.76
7 3.17 7 26.32 23.15
1 22.19 6 22.56 0.37
2 19.02 7 26.32 7.30
1 22.19 6 22.56 0.37
2 19.02 0 0.00 -19.02
8 0.00 6 22.56 22.56
2 19.02 4 15.04 -3.98
4 12.68 8 30.08 17.40
0 25.36 0 0.00 -25.36
8 0.00 5 18.80 18.80
3 15.85 3 11.28 -4.57
5 9.51 7 26.32 16.81
1 22.19 6 22.56 0.37
2 19.02 2 7.52 -11.50
6 6.34 4 15.04 8.70
4 12.68 0 0.00 -12.68
8 0.00 6 22.56 22.56
2 19.02 0 0.00 -19.02
8 0.00 3 11.28 11.28
5 9.51 7 26.32 16.81
1 22.19 6 22.56 0.37
2 19.02 0 0.00 -19.02
8 0.00 5 18.80 18.80
3 15.85 3 11.28 -4.57
5 9.51 0 0.00 -9.51
8 0.00 2 7.52 7.52
6 6.34 6 22.56 16.22
2 19.02 1 3.76 -15.26
7 3.17 1 3.76 0.59
7 3.17 8 30.08 26.91
0 25.36 2 7.52 -17.84
6 6.34 4 15.04 8.70
4 12.68 1 3.76 -8.92
7 3.17 3 11.28 8.11
5 9.51 3 11.28 1.77
5 9.51 3 11.28 1.77
5 9.51 4 15.04 5.53
4 12.68 4 15.04 2.36
4 12.68 6 22.56 9.88
2 19.02 0 0.00 -19.02
8 0.00 4 15.04 15.04
4 12.68 1 3.76 -8.92
7 3.17 8 30.08 26.91
0 25.36 1 3.76 -21.60
7 3.17 1 3.76 0.59
7 3.17 1 3.76 0.59
7 3.17 4 15.04 11.87
4 12.68 1 3.76 -8.92
7 3.17 1 3.76 0.59
7 3.17 3 11.28 8.11
5 9.51 3 11.28 1.77
389 1049.27 326 1225.76 176.49