
clear all
close all
%
disp('Coverage Probability by Clopper-Pearson Confidence Interval')
  lw = 2; 
  set(0, 'DefaultAxesFontSize', 16);
  fs = 15;
  msize = 10;
% end of preabmle--------------------------------------

n = 20;  %sample
alpha = 0.05; %confidence 1-alpha
M=100000;
covs=[];
range = 0.05:0.01:0.95;
for p=range
        X = binornd(n, p, [1 M]);
        Fs = finv(1-alpha/2, 2*(n-X + 1), 2*X);
        Fss = finv(1-alpha/2, 2*(X+1), 2*(n-X));
        LB = X./(X + (n-X+1).*Fs);  UB =(X+1).*Fss./(n - X + (X+1).*Fss);
         cover =  p > LB & p < UB ;
          covs=[covs  sum(cover)/M];
end
figure(1)
plot(range, covs, 'linewidth', 2)
hold on
plot(range, covs, 'o')
plot(0.00:0.01:1, (1-alpha)*ones(1,101), 'r-','linewidth',3)
 xlabel('p'); ylabel('coverage');
% print -depsc 'C:\Springer\Estim\Estimeps\cloppercover.eps'