Kinds of Simulation

3.5.1

require(dice)
#unweighted rolls x500 / two dice
dA <- sample(1:6, 500, replace = T)
dB <- sample(1:6, 500, replace = T)
d2 <- dA+dB
mean(d2)
## [1] 7.076
plot(table(d2))

#loading the die
dA <- sample(1:6, 500, replace = T,prob = c(2, 2, 2, 2, 2, 5))
dB <- sample(1:6, 500, replace = T,prob = c(1, 3, 2, 2, 1, 5))
d2 <- dA+dB
mean(d2)
## [1] 8.208
plot(table(d2))

#replicate the simulation
#help from http://ditraglia.com/Econ103Public/Rtutorials/Rtutorial4.html
dice.sum <- function(n.dice){
  dice <- sample(1:6, size = n.dice, replace = TRUE)
  return(sum(dice))
}
set.seed(3)
probQ1<-replicate(10000, dice.sum(2))

#deviations from expectation
expected<-getSumProbs(ndicePerRoll = 2,nsidesPerDie = 6)
table(probQ1)/length(probQ1)-expected$probabilities[,2]
## probQ1
##             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

3.5.5

This is initially work completed in R, but ultimately completed in an .xls spreadsheet.

require(Rmisc)
## Warning: package 'Rmisc' was built under R version 3.3.2
u<-5.8
sdev<-2.3
a<-4.5
b<-6.7
n<-50

set.seed(3)

Xi<-runif(n, min=a, max=b)
interval<-b-a

#(b-a)fm s(xi)
f<-interval*dnorm(Xi, mean=u, sd=sdev)

#monte carlo estimate
mcEstimate<-mean(f)
mcEstimate
## [1] 0.3668261
#exact - I do not believe this is correct, therefore I uploaded the .xls into google sheets and use the author's formulas.
exact<-pnorm(b,u,sdev)-pnorm(a,u,sdev)
exact
## [1] 0.3662509
plot(density(Xi))

#these values were then transplanted into the xls spreadsheet. The specific random values will be changed.
CI(Xi,ci = .99)
##    upper     mean    lower 
## 5.696651 5.479188 5.261726
CI(Xi,ci = .95)
##    upper     mean    lower 
## 5.642254 5.479188 5.316123
CI(Xi,ci = .90)
##    upper     mean    lower 
## 5.615231 5.479188 5.343146

Link to Excel Spreadsheet

Or Google Sheets

After multiple iterations, the monte carlo estimated integral value never exceeds the exact integral at each confidence interval.


3.5.17

Please follow this link

Each of the values are contained inside the returned “val” variable.