
# THIS WILL FIND  EFF  OF dnk to S^2. 
set.seed(2010)
# 1. CHANGE VARC OR VARSAM DEPENDS ON  n.
 # 2. change the constant dn,k depending on n.

# To find simulated efficiencies of nC2 est of var
# compared to sample variance under Dexp dist.
# n=15.


varcn15=
function(x,k) {   #*** STAR MEANS CHANGE BEFORE SIMULATION.
     A=round(subsets(length(x),k,x),4) # all subsets of                  
                                       # size k are in
                                       # rows of A.
     varian=apply(A,1,var)             # variance function.                  
                                       #applied to rows.
     ans=median(varian)                # the median of 
     # ans=(k-1)*ans/qchisq(.5,k-1)     #variances.
   if(k==2) c=2.52
   if(k==3) c=1.6
   if(k==4) c=1.34
   if(k==5) c=1.2
   if(k==6) c=1.14 

   ans=c*ans   # using simulated constant
   ans
     }


set.seed(2010)
B=500
n=15
 res=lapply(1:B, function(i) round(rexp(n)-rexp(n),4))


estvar=sapply(res,var,simplify=T) # sample var for each resample.

vars2=var(estvar)  # variance of the sample variance.

msesig=(mean(estvar)-2)^2+var(estvar)  # MSE of the sample variance

msesig
##########*****#######************
k=2
ests=sapply(res,function(x) varcn15(x,k), simplify=T)
vark2=var(ests)
mse2=(mean(ests)-2)^2+var(ests)
bias2=mean(ests)-2
ef2=msesig/mse2
eff2=vars2/vark2

#####*#######*****#######**##
mse2
bias2

eff2  # based on variance.



ef2
   # based on MSE



###########*##########*#####*##*********##*
#######*#######*****#############*##########*#####*##*********##*
######
