Complete problems 1, 5, and 17 in 3.5 of Kelton
Extend the simulation
Question 1
A . Extend the spreadsheet to make 500 throws
require(dice)
#randomly reorders the elements passed as the first argument.
samples <- sample(1:6, 500, replace = T)+sample(1:6, 500, replace = T)
mean(samples)
## [1] 6.936
barplot(table(samples))

C . Make 10,000 throws of pair of dice
#Call The Function Repeatedly Using replicate
replicated<-replicate(10000, dice.sum(2))
#Calculate The Probabilities Of All Possible Outcome Sums Of A Dice Roll
probs<-getSumProbs(ndicePerRoll = 2,nsidesPerDie = 6)
table(replicated)/length( replicated)-probs$probabilities[,2]
## replicated
## 2 3 4 5 6
## 0.0008222222 -0.0014555556 0.0030666667 0.0009888889 -0.0005888889
## 7 8 9 10 11
## 0.0002333333 0.0004111111 -0.0020111111 -0.0026333333 0.0014444444
## 12
## -0.0002777778
Question 5
#as given on page number 48
mean<-5.8
standarDeviation<-2.3
a<-4.5
b<-6.7
n<-50
set.seed(2)
#The Uniform Distribution
uniformDistribution<-runif(n, min=a, max=b)
limits<-b-a
#(b-a)fm s(xi)
f<-limits*dnorm(uniformDistribution, mean=mean, sd=standarDeviation)
#monte carlo estimate
arithmeticMean<-mean(f)
arithmeticMean
## [1] 0.3649705
#The Normal Distribution
exact<-pnorm(b,mean,standarDeviation)-pnorm(a,mean,standarDeviation)
exact
## [1] 0.3662509
plot(density(uniformDistribution))

3.5.17
dataFrame <- data.frame("crop" = c("oats","peas","beans","barley"),
"buy" = c(1.05,3.17,1.99,0.95),
"sell" = c(1.29,3.76,2.23,1.65),
"maxDemand"=c(10,8,14,11))
set.seed(2)
productSimulation<-function(mydataFrame,days)
{
revenue<-c()
gain<-c()
price<-c()
k<-1
i<-1
n<-nrow(mydataFrame)
for (i in i:n)
{
#Rounding of Numbers
pdf<-ceiling(runif(days, min=0, max=dataFrame[i,"maxDemand"]))
everydaygain<-c()
everydayprice<-c()
everydayRevenue<-c()
for(j in 1:length(pdf))
{
price<-pdf[j]*(dataFrame[i,"buy"])
revenue<-pdf[j]*(dataFrame[i,"sell"])
gain<-revenue-price
everydaygain<-c(everydaygain,gain)
everydayRevenue<-c(everydayRevenue,revenue)
everydayprice<-c(everydayprice,price)
}
gain<-c(gain,sum(everydaygain))
price<-c(price,sum(everydayprice))
revenue<-c(revenue,sum(everydayRevenue))
if(k==1){
mydataFrame2<-data.frame("everydayDemand"=pdf)
mydataFrame2[,"everydayRevenue"]<-everydayRevenue
mydataFrame2[,"everydayprice"]<-everydayprice
mydataFrame2[,"everydaygain"]<-everydaygain
}else{
mydataFrame2[,"everydayDemand"]<-(mydataFrame2[,"everydayDemand"]+pdf)
mydataFrame2[,"everydayRevenue"]<-(mydataFrame2[,"everydayRevenue"]+everydayRevenue)
mydataFrame2[,"everydayprice"]<-(mydataFrame2[,"everydayprice"]+everydayprice)
mydataFrame2[,"everydaygain"]<-(mydataFrame2[,"everydaygain"]+everydaygain)
}
k<-k+1
}
mydataFrame[,"revenue"]<-revenue
mydataFrame[,"price"]<-price
mydataFrame[,"gain"]<-gain
return (c(mydataFrame,mydataFrame2))
}
#selling season is 90 days
productSimulation(dataFrame,90)
## $crop
## [1] oats peas beans barley
## Levels: barley beans oats peas
##
## $buy
## [1] 1.05 3.17 1.99 0.95
##
## $sell
## [1] 1.29 3.76 2.23 1.65
##
## $maxDemand
## [1] 10 8 14 11
##
## $revenue
## [1] 13.20 859.65 13.20 859.65
##
## $price
## [1] 7.60 494.95 7.60 494.95
##
## $gain
## [1] 5.6 364.7 5.6 364.7
##
## $everydayDemand
## [1] 34 32 34 22 25 30 19 24 8 15 14 24 38 20 19 28 27 17 20 9 24 24 30
## [24] 27 25 29 16 9 23 17 26 18 34 37 31 16 22 15 29 9 16 30 18 19 31 27
## [47] 35 23 27 27 21 11 29 23 25 22 30 20 17 19 26 27 34 23 31 23 18 22 17
## [70] 18 30 24 15 11 22 19 12 32 10 25 26 16 18 28 20 35 23 23 28 25
##
## $everydayRevenue
## [1] 80.38 63.58 70.50 52.51 47.25 57.08 40.23 44.85 14.09 32.19 26.32
## [12] 50.44 84.82 48.79 40.52 55.25 59.57 37.15 37.74 19.87 46.73 56.62
## [23] 57.81 62.50 58.06 67.78 42.77 17.26 52.97 40.20 52.77 37.84 76.70
## [34] 80.49 61.33 39.23 46.51 30.58 61.47 22.20 28.76 73.42 34.57 51.57
## [45] 59.68 61.29 72.24 55.34 52.41 56.71 44.31 27.02 65.53 45.32 56.15
## [56] 48.04 63.92 38.05 36.29 38.86 51.78 50.54 75.10 51.69 70.80 50.60
## [67] 45.93 43.36 30.82 38.57 68.99 56.20 28.82 32.78 48.29 41.71 21.41
## [78] 68.76 17.39 54.86 64.72 26.57 40.89 64.18 47.68 75.08 41.26 46.53
## [89] 63.52 55.00
##
## $everydayprice
## [1] 64.82 49.44 56.34 43.64 35.71 46.42 29.21 37.36 11.36 25.81 20.42
## [12] 38.22 69.22 37.86 33.53 41.72 47.53 28.45 28.10 15.17 39.24 46.92
## [23] 45.42 51.51 45.93 56.07 35.10 14.29 43.73 31.61 41.58 30.06 63.44
## [34] 64.21 49.05 32.48 38.10 22.25 47.81 18.53 23.76 61.12 25.76 43.29
## [45] 47.51 52.25 58.87 45.07 41.09 47.91 34.89 22.41 55.09 37.83 46.45
## [56] 39.28 50.94 30.82 29.89 32.79 43.46 38.41 59.08 42.47 60.45 43.57
## [67] 38.24 34.16 21.79 29.06 54.74 43.04 20.73 27.23 39.20 33.21 14.96
## [78] 53.22 13.26 44.81 54.30 19.16 33.22 50.06 40.56 60.09 30.55 37.09
## [89] 54.24 43.57
##
## $everydaygain
## [1] 15.56 14.14 14.16 8.87 11.54 10.66 11.02 7.49 2.73 6.38 5.90
## [12] 12.22 15.60 10.93 6.99 13.53 12.04 8.70 9.64 4.70 7.49 9.70
## [23] 12.39 10.99 12.13 11.71 7.67 2.97 9.24 8.59 11.19 7.78 13.26
## [34] 16.28 12.28 6.75 8.41 8.33 13.66 3.67 5.00 12.30 8.81 8.28
## [45] 12.17 9.04 13.37 10.27 11.32 8.80 9.42 4.61 10.44 7.49 9.70
## [56] 8.76 12.98 7.23 6.40 6.07 8.32 12.13 16.02 9.22 10.35 7.03
## [67] 7.69 9.20 9.03 9.51 14.25 13.16 8.09 5.55 9.09 8.50 6.45
## [78] 15.54 4.13 10.05 10.42 7.41 7.67 14.12 7.12 14.99 10.71 9.44
## [89] 9.28 11.43