MATH 335: Tank problem simulation


# Define several estimators for a random sample.
# Cut and paste each of these into R.  This will define functions
# which are various estimators for the tank problem (numbered tickets
# in box problem).  All of these estimators are ones, or slight 
# variants of ones, generated by the class discussion, except the
# range.est estimator.


med.est <- function(x) 2*median(x) - 1
mean.est <- function(x) 2*mean(x) - 1 
max.est <- function(x) ((length(x)+1)/length(x))*(max(x) - 1)
avgap2.est <- function(x) {
                 n <- length(x)
                 y <- sort(x)
                 gapsum <- 0
                 for (i in 2:n) {
                        gapsum <- gapsum + y[i]-y[i-1]-1
                 }
                 max(x) + gapsum/(n-1)
              }
iqr.est <- function(x) quantile(x)[3] + 1.5*(quantile(x)[4] - quantile(x)[2])
range.est <- function(x) max(x) + min(x) -1
norm.est <- function(x) mean(x) + 3*sd(x)

# Run a small simulation study:
# Use the following values for N and n and reps.  For each estimator
# generate  reps  estimates.  Then find the mean of these
# estimates and the mean-squared error (MSE) of the estimates.  Below 
# we give the code for the  range.est  estimator.  
#
# Make a short, one page summary table of results, with column headers
#  estimator ,  mean , and  MSE . Then add
# a concise prose summary of the results: which estimators peform well,
# which poorly.  Which estimators are best?
 
N <-50
n <- 5
reps <- 10000


results.range.est <- replicate(reps, range.est(sample(1:N,n)))
mean(results.range.est) # if = N, the estimator is unbiased
mean((results.range.est - N)^2) # smaller values are better.