We are going to flip toast. Let’s make the independent-and-identically-distribution constraint, and let Y be a binomial distributed random variable that denotes the number of successes (butter-side down flips). We say Y ∼ Binomial(θ, N) where θ is the probability of success on any flip and N is the number of flips.

Suppose N = 10 flips, and θ = .8.

  1. Use dbinom() to draw the probability mass function.
N=10
theta=0.8
y=0:10 #vector; all possible values of y (butter-side down flips in N flips)
dbinom(y,10,.8)
##  [1] 0.0000001024 0.0000040960 0.0000737280 0.0007864320 0.0055050240
##  [6] 0.0264241152 0.0880803840 0.2013265920 0.3019898880 0.2684354560
## [11] 0.1073741824
fy=dbinom(y,N,theta) 
plot(y,fy,typ='h',xlab="Value", ylab="Probability")
points(y,fy,pch=19,cex=0.7)

  1. Let’s simulate the distribution using rbinom(). Write the code to do so. You can use my getProps function.
#stimulates binomial function; simulation method; getProps
getProps=function(x,value){
  B=length(value)
  count=1:B
  for (b in 1:B) count[b]=sum(x==value[b])
  prop=count/length(x)
  return(cbind(value,count,prop))}

M=10000
N=10
p=0.8
sampleOfY=rbinom(M,10,.8)
out=getProps(sampleOfY,0:10)
print(out)
##       value count   prop
##  [1,]     0     0 0.0000
##  [2,]     1     0 0.0000
##  [3,]     2     1 0.0001
##  [4,]     3    15 0.0015
##  [5,]     4    54 0.0054
##  [6,]     5   253 0.0253
##  [7,]     6   863 0.0863
##  [8,]     7  1964 0.1964
##  [9,]     8  3092 0.3092
## [10,]     9  2657 0.2657
## [11,]    10  1101 0.1101
  1. Show that the proportion of successes converges to the probability of success from the probability mass function.
#The result of rbinom distribution, using getProps 
M=50
N=10
p=0.8
y=0:10
sample=rbinom(M,N,p)
out=getProps(sample,y)
print(out)
##       value count prop
##  [1,]     0     0 0.00
##  [2,]     1     0 0.00
##  [3,]     2     0 0.00
##  [4,]     3     0 0.00
##  [5,]     4     0 0.00
##  [6,]     5     4 0.08
##  [7,]     6     3 0.06
##  [8,]     7     9 0.18
##  [9,]     8    14 0.28
## [10,]     9    12 0.24
## [11,]    10     8 0.16
M=10000
N=10
p=0.8
y=0:10
sample=rbinom(M,N,p)
out=getProps(sample,y)
print(out)
##       value count   prop
##  [1,]     0     0 0.0000
##  [2,]     1     0 0.0000
##  [3,]     2     0 0.0000
##  [4,]     3     9 0.0009
##  [5,]     4    57 0.0057
##  [6,]     5   270 0.0270
##  [7,]     6   861 0.0861
##  [8,]     7  2041 0.2041
##  [9,]     8  3018 0.3018
## [10,]     9  2684 0.2684
## [11,]    10  1060 0.1060
#The result of dbinom; using the probability mass function
N=10
theta=0.8
y=0:10 #vector; all possible values of y (butter-side down flips in N flips)
dbinom(10,10,.8)
## [1] 0.1073742

Using the rbinomial distribution when M represents a smaller number of experiments, M=50, we receive an output of bigger numbers as the values are closer to zero. Further along, we can make M=10000 and can receive a more accurate representation of the numbers on our distribution as these values get bigger (further away from zero). The greater we increase M, our number of experiments, the more detailed we can reach our ultimate probability of successes using the probability mass function. To support my reasoning, I used 10 as a possible value in the vector y for our dbinom function.

  1. Use the simulation approach to find the mean of the distribution.
#Mean of distribution=expected value using simulation (sum of ALL values + probability of value; This is the estimated value using simulation
M=10000
N=10
theta=0.8
sample=rbinom(10000,10,0.8)
mean(sample) #this is the mean of the sample
## [1] 7.9981

For the remaining questions, please consider the following three estimators for θ. ˆθ1 = Y/N ˆθ2 = (Y + .5)/(N + 1) ˆθ3 = (Y + 1)/ (N + 2)

  1. Use the simulation method to find the sampling distribution of each if θ = .8 and N = 10.
#Result of rbinom distribution using getProps; This is the distribution of Y/N
M=10000
N=10
p=0.8
y=0:10

YoverN=0:N/10
sample1=rbinom(M,10,.8) #dbinom; distribution of Y only 
sample_theta=sample1/10 #sample of Y/N
theta_hat=getProps(sample_theta,YoverN)
print(theta_hat)
##       value count   prop
##  [1,]   0.0     0 0.0000
##  [2,]   0.1     0 0.0000
##  [3,]   0.2     0 0.0000
##  [4,]   0.3     7 0.0007
##  [5,]   0.4    50 0.0050
##  [6,]   0.5   265 0.0265
##  [7,]   0.6   898 0.0898
##  [8,]   0.7  1997 0.1997
##  [9,]   0.8  3057 0.3057
## [10,]   0.9  2652 0.2652
## [11,]   1.0  1074 0.1074
M=10000
N=10
p=0.8
y=0:10

YoverN=(0:N+0.5)/(N+1)
sample2=rbinom(M,10,.8)
sample_theta2=(sample2+0.5)/(N +1)
theta_hat2= getProps(sample_theta2,YoverN)
print(theta_hat2)
##            value count   prop
##  [1,] 0.04545455     0 0.0000
##  [2,] 0.13636364     0 0.0000
##  [3,] 0.22727273     1 0.0001
##  [4,] 0.31818182     6 0.0006
##  [5,] 0.40909091    47 0.0047
##  [6,] 0.50000000   251 0.0251
##  [7,] 0.59090909   833 0.0833
##  [8,] 0.68181818  1996 0.1996
##  [9,] 0.77272727  3099 0.3099
## [10,] 0.86363636  2686 0.2686
## [11,] 0.95454545  1081 0.1081
M=1000
N=10
p=0.8
y=0:10

YoverN=(0:N+1)/(N+2)
sample3=rbinom(M,10,.8) 
sample_theta3=(sample3+1)/(N+2) #sample of Y/N (step 2)
theta_hat3=getProps(sample_theta3,YoverN) #(step 3)
print(theta_hat3)
##            value count  prop
##  [1,] 0.08333333     0 0.000
##  [2,] 0.16666667     0 0.000
##  [3,] 0.25000000     0 0.000
##  [4,] 0.33333333     0 0.000
##  [5,] 0.41666667     5 0.005
##  [6,] 0.50000000    29 0.029
##  [7,] 0.58333333    93 0.093
##  [8,] 0.66666667   180 0.180
##  [9,] 0.75000000   284 0.284
## [10,] 0.83333333   296 0.296
## [11,] 0.91666667   113 0.113
plot(theta_hat[,1],theta_hat[,3], type ='h', xlab="ThetaHat =Y/N", ylab="Proportion")

plot(theta_hat2[,1],theta_hat2[,3], type ='h', xlab="ThetaHat =Y/N", ylab="Proportion")

plot(theta_hat3[,1],theta_hat3[,3], type ='h', xlab="ThetaHat =Y/N", ylab="Proportion")

  1. Use the simulation method to find the bias of each. Which is least biased?
new_est=(sample_theta/N)
mean(new_est)
## [1] 0.079977
exp_value= mean(new_est)
exp_value-0.8
## [1] -0.720023
new_est2=(sample_theta2+0.5)/(N+1) #estimator function
mean(new_est2)                     #expected value of estimator 
## [1] 0.1158752
exp_value2=mean(new_est2)          #computing bias
exp_value2-0.8
## [1] -0.6841248
new_est3=(sample_theta3+1)/(N+2)
mean(new_est3)
## [1] 0.1461736
exp_value3=mean(new_est3)
exp_value3-0.8
## [1] -0.6538264
exp_value=mean(sample_theta) #least biased;closer to zero
exp_value-0.8
## [1] -0.00023
exp_value2=mean(sample_theta2)  
exp_value2-0.8
## [1] -0.02537273
exp_value3=mean(sample_theta3)
exp_value3-0.8
## [1] -0.04591667
  1. Use the simulation method to find the efficiency (in terms of RMSE) of each. Which is most efficient?
M=10000
N=10
theta=0.8
sample1=rbinom(M,N,theta)
sample_theta=sample1/N
err=sample_theta-theta
sqrErr=err^2
MSE=mean(sqrErr)
RMSE=sqrt(MSE)
print(RMSE)
## [1] 0.1274166
M=10000
N=10
theta=0.8
sample2=rbinom(M,N,theta)
sample_theta2=(sample2+0.5)/(N +1) #rbinom in estimator
err=sample_theta2-theta
sqrErr=err^2
MSE=mean(sqrErr)
RMSE2=sqrt(MSE)
print(RMSE2) 
## [1] 0.1197794
M=10000
N=10
theta=0.8
sample3=rbinom(M,N,theta)
sample_theta3=(sample3+1)/(N+2)
err=sample_theta3-theta
sqrErr=err^2
MSE=mean(sqrErr)
RMSE3=sqrt(MSE)
print(RMSE3) #this one has a lower RMSE; still less, so more efficient
## [1] 0.117197
  1. Suppose N = 1000 flips. Use the simulation method to find a value of θ for which the efficiency of ˆθ1 < ˆθ3. Find a different value of θ for the reverse, that is, where ˆθ3 < ˆθ1.
#state what they are for each estimator; true value of theta matters on behalf of efficiency; usually 01>03; as you increase theta, it should flip; show us that a theta does exist where 03 is LESS efficient than 01= a HIGH value 
#theta can't be bigger than 1
#show that theta has a lower RMSE (root mean square error)

M=10000
N=1000
theta=0.8 #more efficient
sample1=rbinom(M,N,theta)
sample_theta=sample1/N
err=sample_theta-theta
sqrErr=err^2
MSE=mean(sqrErr)
RMSE=sqrt(MSE)
print(RMSE)
## [1] 0.01287986
M=10000
N=1000
theta=0.989 #HIGHER theta; lower RMSE
sample2=rbinom(M,N,theta)
sample_theta2=(sample2+0.5)/(N +1) #rbinom in estimator
err=sample_theta2-theta
sqrErr=err^2
MSE=mean(sqrErr)
RMSE2=sqrt(MSE)
print(RMSE2)
## [1] 0.00331075
M=10000
N=1000
theta=0.2555 #LOWER theta; higher RMSE
sample3=rbinom(M,N,theta)
sample_theta3=(sample3+1)/(N+2) #rbinom in estimator
err=sample_theta3-theta
sqrErr=err^2
MSE=mean(sqrErr)
RMSE3=sqrt(MSE)
print(RMSE3)
## [1] 0.0136869