3.5 Kinds of Simulation, #17



Create a spreadsheet simulation that will, for each day as well as for the whole season, simulate Walther’s total cost, total revenue and total profit

# Store information provided in a data frame
products <- c("Oats","Peas", "Beans", "Barley")
wprices <- c(1.05, 3.17, 1.99, 0.95)
rprices <- c(1.29, 3.76, 2.23, 1.65)
demands <- c(10,8, 14,11)

prod <- rbind.data.frame(wprices, rprices, demands)
colnames(prod) <- products
rownames(prod) <- c("Wholesale ($)", "Retail ($)","Max Demand")
knitr::kable(prod)
Oats Peas Beans Barley
Wholesale ($) 1.05 3.17 1.99 0.95
Retail ($) 1.29 3.76 2.23 1.65
Max Demand 10.00 8.00 14.00 11.00



Create simulation for whole season (90 days)

# create a simulation for each data for all products
siml <- as.data.frame(lapply(demands, function(x) round(runif(90, min=0, max=x))))

# calculate buying & selling price and profit
buying <- apply(siml * wprices, 1, sum)
selling <- apply(siml * rprices,1,sum)
profit <- selling - buying
siml <- data.frame(siml, buying, selling, profit )
colnames(siml) <- c(products,"Buying Cost", "Selling Price", "Profit")

knitr::kable(siml)
Oats Peas Beans Barley Buying Cost Selling Price Profit
3 3 5 11 36.26 41.54 5.28
5 7 2 8 36.44 51.07 14.63
3 3 5 10 29.57 34.61 5.04
3 3 13 8 50.07 67.76 17.69
6 7 9 7 43.61 50.57 6.96
8 6 0 3 33.91 44.93 11.02
6 5 4 8 33.55 39.07 5.52
5 3 4 9 46.59 59.97 13.38
3 5 0 2 17.08 19.48 2.40
9 8 14 6 86.21 109.58 23.37
1 1 1 3 8.18 9.62 1.44
8 3 7 9 52.29 69.87 17.58
9 8 0 10 45.27 51.75 6.48
5 6 12 6 65.29 83.72 18.43
9 2 11 2 44.00 49.76 5.76
8 5 11 7 56.09 76.47 20.38
8 3 6 8 36.59 42.59 6.00
9 6 10 1 66.88 82.99 16.11
7 1 10 0 34.88 39.20 4.32
9 8 2 9 64.34 82.07 17.73
2 4 12 8 38.58 44.82 6.24
4 6 5 7 40.88 55.29 14.41
8 7 10 10 53.67 62.07 8.40
5 1 5 0 12.67 20.26 7.59
5 0 5 11 32.39 37.43 5.04
6 4 3 11 42.78 58.59 15.81
6 8 0 0 20.34 23.70 3.36
8 2 3 4 29.47 40.71 11.24
3 3 9 6 30.51 35.55 5.04
3 0 5 8 32.96 43.28 10.32
7 3 10 6 43.28 49.52 6.24
9 7 4 10 66.24 85.37 19.13
3 1 3 8 24.21 27.81 3.60
8 6 4 1 44.69 56.67 11.98
3 3 2 7 20.45 24.05 3.60
8 2 4 0 17.74 27.32 9.58
4 3 10 8 36.59 42.59 6.00
2 0 11 9 49.76 63.73 13.97
6 7 11 1 42.23 48.23 6.00
9 2 5 10 51.34 68.22 16.88
10 6 3 7 39.52 45.76 6.24
1 1 13 11 55.78 72.44 16.66
6 2 11 6 42.23 48.23 6.00
5 2 2 5 28.84 37.87 9.03
4 7 3 11 43.17 49.17 6.00
2 7 7 3 38.03 50.34 12.31
5 1 7 3 28.08 31.92 3.84
9 7 12 10 73.84 98.57 24.73
9 4 4 11 43.50 50.22 6.72
2 5 5 2 28.84 37.87 9.03
6 1 9 5 36.15 41.19 5.04
4 3 11 11 58.63 77.39 18.76
7 3 5 10 38.47 44.47 6.00
10 1 12 1 71.64 86.02 14.38
9 3 5 2 33.11 37.67 4.56
6 5 10 7 53.24 71.52 18.28
0 2 6 7 24.21 27.81 3.60
8 4 8 4 58.32 73.36 15.04
6 1 13 4 43.06 48.82 5.76
3 6 7 6 47.54 61.62 14.08
4 2 3 9 29.24 33.56 4.32
6 6 9 6 58.95 76.20 17.25
3 7 8 2 31.34 36.14 4.80
3 2 11 9 48.17 64.46 16.29
7 1 1 5 20.34 23.70 3.36
4 6 12 5 61.17 78.31 17.14
4 1 10 2 31.01 35.09 4.08
5 2 7 9 46.27 61.16 14.89
9 6 10 8 47.81 55.73 7.92
4 7 12 4 61.17 78.31 17.14
8 8 1 2 28.41 32.97 4.56
0 1 2 9 33.60 40.90 7.30
8 3 8 9 40.68 47.40 6.72
0 5 7 8 34.54 47.77 13.23
0 7 2 4 15.53 18.65 3.12
2 7 8 8 57.05 72.90 15.85
6 3 1 5 23.27 26.87 3.60
2 7 2 2 21.23 29.89 8.66
6 0 2 6 22.22 25.58 3.36
9 2 14 3 37.70 56.75 19.05
1 4 9 8 34.38 39.66 5.28
7 2 6 4 46.91 58.78 11.87
9 4 3 5 33.33 38.37 5.04
2 7 4 2 34.23 43.74 9.51
3 2 8 2 19.51 23.11 3.60
7 4 2 6 38.03 50.34 12.31
9 7 8 7 48.53 55.97 7.44
7 3 6 5 37.71 51.53 13.82
9 0 3 3 18.57 22.17 3.60
0 1 5 4 20.60 27.05 6.45



Let’s get the totals

totals <- colSums(siml)
knitr::kable(totals[5:7])
Buying Cost 3585.58
Selling Price 4467.15
Profit 881.57