Assignment 2
Assignment 2
Shreya Singi
04/03/2020
Question 1. Compare the two estimators of parameter theta in a uniform distribution Xi- Uniform(theta, 1),
where i=1,2,. . . ,n and theta <1. Plot MSE, Varience and Bias of the estimators for n=2,5,10,20 with theta
in range from -1 to 1 and use simulations to confirm theoritical MSE/Varience/Bias for theta= -1 and 0.
Solution. Step 1- Calculate the Expectation of MOM. Step 2- Calculate the Varience of MOM.
Step 3- Similarly calculating Expectation of MLE. Step 4- Calculating BIas using a function.
Step 5- Plotting the Graphs.
aexp=1
avar=0.33
bexp=0.5
bvar=0.083
Expectation of MOM
mome = function(y)
{
x = y
}
Varience of MOM
n=20
momv=function(s)
{
(1-s)^2/(3*n)
}
b=runif(1000,-1,1)
# plot(b,mome(b),xlab = "Theta",ylab = "expectation",col.lab="pink",col.axis="violet")
# plot(b,momv(b),xlab = "Theta",ylab = "variance",col.lab="pink",col.axis="violet")
# abline(v=avar,col="violet",lwd=3)
# abline(v=bvar,col="pink",lwd=3)
Expectation of MLE
emle= function(s1)
{
(1+(n*s1))/(n+1)
}
c=runif(1000,-1,1)
1
Calculating Bias
bias=function(i)
{
((1+(n*i))/(n+1))-i
}
xexp=function(z)
{
(((n+2)*(n+1))+(((n)*(n+1))*(1-z)^2)-(2*n*(n+1)*(1-z)))/((n+1)*(n+2))
}
c=runif(1000,-1,1)
0.04
0.02
0.00
Theta
Plotting Varience and Theta
2
0.15
Variance
0.10
0.05
0.00
Theta Plot-
ting Mean Square Error and Theta
0.10
0.05
0.00
Theta
Question 2: Perform the following steps and comment on the observation. Step I. Generate one U(-100,100)
random number. Call it m. Step II. Generate one U(10,50) random number. Call it s. Step III. Generate
one U(10,25) random number. Call it n. Step IV. Generate 1000 N(m,s) random numbers. Call this the
population. Step V. Sample n numbers without replacement from the population. Step VI. Construct
90%, 95%, and 99% confidence intervals for the population mean. Step VII. Construct 90%, 95%, and 99%
confidence intervals for the population variance. Step VIII.Repeat steps V & VI 100/500/1000 times and
3
count the number of times (and percentage)that the population mean is captured by the confidence interval.
Step IX.Repeat steps V & VII 100/500/1000 times and count the number of times (and percentage) that
the population variance is captured by the confidence interval.
Solution. Firstly, we complete Step I-V by generating uniform random numbers using rnorm
function. Then we calculate the mean of 90%, 95%, 99% CI for population mean by using
a formula with reference to the given Text Book and varience by using qchisq function and
df i.e. degree of freedom=1. Then repeat the above steps 100/500/1000 times and count the
number of times (and percentage)that the population mean and varience are captured by the
confidence interval. At last, plotting a Histogram.
## [1] -100.4532
## [1] 86.09509
## [1] 15.62376
a= sample(population,n)
4
standardD= var(a)
standardD
## [1] 10451.21
varience90 = c(1,1)
varience90[1] =qchisq(0.05, df=n-1)
varience90[2] = qchisq(0.95, df=n-1)
varience95 = c(1,1)
varience95[1] =qchisq(0.025, df=n-1)
varience95[2] = qchisq(0.975, df=n-1)
varience99 = c(1,1)
varience99[1] =qchisq(0.005, df=n-1)
varience99[2] = qchisq(0.995, df=n-1)
varience90 = (n-1)*standardD/varience90
varience90
varience95 = (n-1)*standardD/varience95
varience95
varience99 = (n-1)*standardD/varience99
varience99
mean = mean(a)
mean
## [1] -75.69756
co = c(0,0,0)
co1 = c(0,0,0)
for(i in 1:100) {
a= sample(population,n)
mean = mean(population)
standardD= var(population)
5
mean90 =mean(a) + qt(0.95, n - 1) * sd(a) * c( - 1, + 1)/sqrt(n)
mean95 =mean(a) + qt(0.975, n - 1) * sd(a) * c( - 1, + 1)/sqrt(n)
mean99 = mean(a) + qt(0.995, n - 1) * sd(a) * c( - 1, + 1)/sqrt(n)
varience90 = c(1,1)
varience90[1] =qchisq(0.05, df=n-1)
varience90[2] = qchisq(0.95, df=n-1)
varience90 = ((n-1)*standardD)/varience90
varience95 = c(1,1)
varience95[1] =qchisq(0.025, df=n-1)
varience95[2] = qchisq(0.975, df=n-1)
varience95 = ((n-1)*standardD)/varience95
varience99 = c(1,1)
varience99[1] =qchisq(0.005, df=n-1)
varience99[2] = qchisq(0.995, df=n-1)
varience99 = ((n-1)*standardD)/varience99
co
## [1] 85 93 99
hist(population, col='green')
6
Histogram of population
200
150
Frequency
100
50
0
population
Question 4: Consider the following snakes-and-ladders game, with the blue arrows indicating laddersand the
red arrows indicating snakes. Let N be the number of tosses required to reachfrom START (1) to FINISH
(20) using a fair dice. Write a program to calculate the expected value and standard deviation of N. Also,
draw a plot in which the x-axis shows the number of rolls, and the y-axis shows the percentage of games that
were completed in that number of rolls. What can you say about the distribution of the random variable N.
Solution. In this, we will calculate the expectation and varience of the number of tosses by
applying loop. Then we plot a graph between the number of rolls and the percentage of games
completed.
rollc=list()
rollp=list()
for(x in 1:1000)
{
i=1
n=0
count=0
a=list(3,10,11,16,18)
p=list(13,5,17,2,8)
while(n!=20)
{
rand=sample(1:6, 1, replace=TRUE)
if(n %in% a)
{
s=match(n,a)
s1=p[s]
7
d=array(unlist(s1),dim=c(1,1))
n=d[1]
}
if(n+rand>20)
{
n=n
}
else
{
n=n+rand
}
count=count+1
i=i+1
}
rollc[x]=count
}
answer=array(unlist(rollc),dim=c(1,1000))
expect=mean(answer)
v=as.vector(answer)
maximum = max(answer)
maximum
## [1] 55
j=1
while(j<=maximum){
count = 0
for(k in (v)){
if(k==j){
count = count +1
}
}
rollp[j] =((count/10000)*100)
j =j+1
}
standard_deviation=sd(v)
rolls = 1:maximum
Expectation of N
print("expectctation:")
## [1] "expectctation:"
print (expect)
## [1] 12.581
8
Varience of N
print(standard_deviation)
## [1] 7.917204
0.6
0.4
0.2
0.0
0 10 20 30 40 50
Number of Rolls
The distribution of Random Variable N is a Chi- Square Distribution.