0% found this document useful (0 votes)
24 views4 pages

Comp 08 Sol

This document contains 4 computer problems involving random number generation and probability distributions. Problem 1 involves generating random numbers between 20 and 40 and calculating probabilities. Problem 2 simulates the distribution of the range of 3 random numbers between 0 and 1. Problem 3 generates Poisson random variables and compares sample and theoretical means and variances. Problem 4 generates Poisson random variables and compares the sample distribution to the theoretical probabilities. All problems verify that the numerical experiments match the theoretical probabilities and distributions.

Uploaded by

tuantuan8951
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views4 pages

Comp 08 Sol

This document contains 4 computer problems involving random number generation and probability distributions. Problem 1 involves generating random numbers between 20 and 40 and calculating probabilities. Problem 2 simulates the distribution of the range of 3 random numbers between 0 and 1. Problem 3 generates Poisson random variables and compares sample and theoretical means and variances. Problem 4 generates Poisson random variables and compares the sample distribution to the theoretical probabilities. All problems verify that the numerical experiments match the theoretical probabilities and distributions.

Uploaded by

tuantuan8951
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

STAT2011 Computer Problems for Week 8

A. Student

1. (a)
#generate Y_i and ordering the random numbers
data<-sort(runif(33,20,40),decreasing = FALSE)
data

## [1] 20.38794 20.92128 20.95966 20.96079 21.97769 22.66469 22.79768


## [8] 25.51315 26.01316 26.29921 28.94193 29.37788 29.80035 30.11394
## [15] 30.50022 30.77982 31.00262 31.17259 32.52861 33.47244 33.58439
## [22] 33.80935 34.12932 34.32379 34.74822 34.79304 35.41126 35.44845
## [29] 37.28728 37.61102 37.74654 38.30800 39.82400
data[32]

## [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.79151731 0.04939862 0.94175103


#range
R<-max(data)-min(data)
R

## [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

0.0 0.2 0.4 0.6 0.8 1.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

0.0 0.2 0.4 0.6 0.8 1.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.

You might also like