Comp 08 Sol
Comp 08 Sol
A. Student
1. (a)
#generate Y_i and ordering the random numbers
data<-sort(runif(33,20,40),decreasing = FALSE)
data
## [1] 38.308
(b)
n<-1000
a<-c()
for(i in 1:n){
data<-sort(runif(33,20,40),decreasing = FALSE)
if(data[32]>39.3){
a[i]<-1
}
else{
a[i]<-0
}
}
(c)
sum(a)/n
## [1] 0.32
(d) The result in (c) is close to 0.3, so the theoretical solution h = 39.3 is verified.
2. (a)
data<-runif(3,0,1)
data
## [1] 0.8923524
(b)
n<-1000
R<-c()
for(i in 1:n){
data<-runif(3,0,1)
1
R[i]<-max(data)-min(data)
}
(c)
hist(R,breaks=20, prob=T,main="distribution of range")
distribution of range
1.5
1.0
Density
0.5
0.0
R (d)
#theoretical pdf of range
pdf.range<-function(r){
6*r-6*r^2
}
hist(R,breaks=20, prob=T,main="distribution of range")
curve(pdf.range,0,1,col="red",add=T)
2
distribution of range
1.5
1.0
Density
0.5
0.0
R The his-
togram has the same shape as the theoretical curve, so the numerical experiment verified the result.
3.
a<-rpois(1000,lambda=1.9)
barplot(table(a))
250
200
150
100
50
0
0 1 2 3 4 5 6 7
#calulate the mean
mean(a)
## [1] 1.9
3
#calculate the variance
var(a)
## [1] 1.871872
Theoretical values of mean and variance are both 1.9. The sample mean and variance are close to the
theoretical values.
4.
d<-rpois(200,lambda=2.7)
par(mfrow=c(2,1))
barplot(table(d))
plot(dpois(x=0:20,lambda=2.7),type="b")
40
0
0 1 2 3 4 5 6 7 8
dpois(x = 0:20, lambda = 2.7)
0.00
5 10 15 20
Index
The shape of the barchart is similar to the shape of the theoretical probabilities up to x = 20.
#mean of X^3
mean(d^3)
## [1] 40.56
#theoretical value of expected value of X^3
2.7+3*2.7^2+2.7^3
## [1] 44.253
The sample estimate is close to the theoretical mean.