You are on page 1of 9

Assignment Submission 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)

# plot(c,emle(c),xlab = "Theta",ylab = "Expectation",col.lab="pink",col.axis="violet")

Plotting Bias and Theta

plot(c,bias(c),xlab = "Theta",ylab = "Bias",col.lab="pink",col.axis="violet")


0.08
0.06
Bias

0.04
0.02
0.00

−1.0 −0.5 0.0 0.5 1.0

Theta
Plotting Varience and Theta

plot(c,xexp(c)-(emle(c))^2,xlab = "Theta",ylab = "Variance",col.lab="pink",col.axis="violet")


abline(v=avar,col="violet",lwd=3)
abline(v=bvar,col="pink",lwd=3)

2
0.15
Variance

0.10
0.05
0.00

−1.0 −0.5 0.0 0.5 1.0

Theta Plot-
ting Mean Square Error and Theta

plot(c,(xexp(c)-(emle(c))^2)+(bias(c)^2),xlab = "Theta",ylab = "mean_square_error",col.lab="pink",col.ax


abline(v=avar,col="violet",lwd=3)
abline(v=bvar,col="pink",lwd=3)
0.15
mean_square_error

0.10
0.05
0.00

−1.0 −0.5 0.0 0.5 1.0

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.

m <- rnorm(1, mean=-100, sd=100)


m

## [1] -100.4532

s <- rnorm(1, mean=10, sd=50)


s = abs(s)
s

## [1] 86.09509

n <- rnorm(1, mean=10, sd=25)


n = abs(n)
n

## [1] 15.62376

population <- rnorm(1000, mean=m, sd=s)

a= sample(population,n)

Mean of 90% CI.

mean90 =mean(a) + qt(0.95, n - 1) * sd(a) * c( - 1, + 1)/sqrt(n)


mean90

## [1] -121.11464 -30.28049

Mean of 95% CI.

mean95 =mean(a) + qt(0.975, n - 1) * sd(a) * c( - 1, + 1)/sqrt(n)


mean95

## [1] -130.94850 -20.44663

Mean of 99% CI.

mean99 = mean(a) + qt(0.995, n - 1) * sd(a) * c( - 1, + 1)/sqrt(n)


mean99

## [1] -152.1895276 0.7944007

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)

Varience of 90% CI.

varience90 = (n-1)*standardD/varience90
varience90

## [1] 21833.879 6237.223

Varience of 95% CI.

varience95 = (n-1)*standardD/varience95
varience95

## [1] 25378.184 5665.901

Varience of 99% CI.

varience99 = (n-1)*standardD/varience99
varience99

## [1] 34726.925 4739.726

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

if(mean90[1] < mean & mean90[2] > mean){


co[1] = co[1] + 1
}
if(mean95[1] < mean & mean95[2] > mean){
co[2] = co[2] + 1
}
if(mean99[1] < mean & mean99[2] > mean){
co[3] = co[3] + 1
}
if(varience90[1] > standardD & varience90[2] < standardD){
co1[1] = co1[1] + 1
}
if(varience95[1] > standardD & varience95[2] < standardD){
co1[2] = co1[2] + 1
}
if(varience99[1] > standardD & varience99[2] < standardD){
co1[3] = co1[3] + 1
}
}
print("mean[100] for 90 95 and 99 CI")

## [1] "mean[100] for 90 95 and 99 CI"

co

## [1] 85 93 99

Plotting the Histogram.

hist(population, col='green')

6
Histogram of population
200
150
Frequency

100
50
0

−300 −200 −100 0 100 200

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] "standard deviation"

print(standard_deviation)

## [1] 7.917204

Plotting a graph between Number of Rolls and Percentage of games completed

plot(rolls,rollp,col="Blue",type="l", lwd=5, xlab="Number of Rolls", ylab="Percentage of games completed


0.8
Percentage of games completed

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.

You might also like