% Jackknife
% "...One of the keys to mushroom identification is 
% the spore deposit. Spores of
% Amanita phalloides are colorless, nearly spherical,
% and smooth. Measurements
% in microns of 28 spores (max diameter) are given below."
%
% Find jackknife estimator of CV
clear all
close all force

  disp('Spores of Amanita by Jackknife')
  lw = 2; 
  set(0, 'DefaultAxesFontSize', 14);
  fs = 14;
  msize = 10;  



amanita =[...
9.2 8.8 9.1 10.1 8.5 8.4 9.3 8.7 9.7 9.9 8.4 8.6 8.0 9.5 ...
8.8 8.1 8.3 9.0 8.2 8.6 9.0 8.7 9.1 9.2 7.9 8.6 9.0 9.1];

%function handle for calculating CV statistics
coevar = @(x)  std(x)/mean(x); 
%
n=length(amanita);
pseudo=[]; %collect pseudo-observations
coevarsj=[]; %collect estimators from jackknife samples
for i = 1:n
    jknsa = minusith(amanita, i); %i-th jackknife sample
    pseudoobs = n * coevar(amanita) - (n-1)*coevar(jknsa);
    pseudo =[pseudo pseudoobs];
    coevarsj=[coevarsj coevar(jknsa)];
end
%jackknife bias
bias=(n-1)*(mean(coevarsj)-coevar(amanita) ) %-5.7487e-04
%two equivalent jackknife estimators of CV
pseudomean = mean(pseudo)          %0.0628
jackknifeset=coevar(amanita)-bias   %0.0628

pseudostd = std(pseudo)/sqrt(n)   %0.0081
%Jackknife CI
[pseudomean - tinv(0.975, n-1)*pseudostd,...
         pseudomean + tinv(0.975, n-1)*pseudostd]
%  0.0461    0.0795
%  Compare to Miller 1991 CI for CV.
    theta=coevar(amanita)   %0.0622
    [theta - norminv(0.975)* theta* sqrt( 1/(n-1)*(1/2 + theta^2)),...
    theta + norminv(0.975)* theta* sqrt( 1/(n-1)*(1/2 + theta^2))]
   %0.0456    0.0789
