File size: 5,878 Bytes
5ab75a9 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 | function [Nsamp] = EstimateRequiredSamples(Pc,acc,conf,maxiter)
%
% Estimate the number of samples required for a Monte Carlo (MC) simulation
% to achieve a Pc-estimation accuracy of 0 < acc < 1, to a confidence level
% of 0 < conf < 1.
%
% INPUT:
%
% Pc = Pc to use for estimation process (typically a 2DPc value)
% acc = Desired accuracy for the MC Pc estimate (typically 0.1 or 0.3)
% conf = Confidence level for the accuracy estimate (typically 0.95)
% maxiter = Maximum number of iterations for binofit analysis [optional]
% (maxiter <= 0) => Perform no binofit iterations
% (maxiter > 0) => Perform at most maxiter binofit iterations
% [default maxiter = 100; typical number of iterations 3 to 10]
%
% OUTPUT:
%
% Nsamp = Recommended number of MC samples to achieve an MC estimation
% of a probability value 'Pc' with an accuracy of 'acc' at a
% confidence level of 'conf.'
%
% Check for sufficient input
Nargin = nargin;
if Nargin < 3
error('Insufficient input');
end
% Return infinity for non-positive Pc
if Pc <= 0
Nsamp = Inf;
return;
end
% Check for valid accuracy and confidence inputs
if (acc <= 0) || (acc >= 1)
error('The input desired accuracy must be 0 <= acc <= 1, typically 0.1 or 0.3');
end
if (conf <= 0) || (conf >= 1)
error('The input desired confidence must be 0 <= confidence <= 1, typically 0.95 or 0.99');
end
% Set default maximum iterations
if (Nargin < 4) || isempty(maxiter) || isnan(maxiter)
maxiter = 100;
elseif (maxiter < 0) || isinf(maxiter)
error('Invalid maxiter parameter, typically 100');
end
% Calculate the number of sigmas correspond to the confidence interval
% (for a chi-squared distribution with one degree of freedom)
use_statistics_toolbox = license('test','statistics_toolbox');
if use_statistics_toolbox
% Calculate Nsig using chi2inv
Nsig = sqrt(chi2inv(conf,1));
else
% Use zeroth iteration estimate without stats toolbox
maxiter = 0;
% Approximate Nsig using table interpolations
if conf < 1e-2
% For very small confidences < 0.01
% (likely never used, but here for completeness)
Nsig = 1.2533*conf;
elseif conf <= 9.99e-01
% For intermediate range confidences: 0.01 to 0.999
cn = [ ...
1.0000e-02 1.2533e-02; ...
1.0000e-01 1.2566e-01; ...
2.0000e-01 2.5335e-01; ...
3.0000e-01 3.8532e-01; ...
4.0000e-01 5.2440e-01; ...
5.0000e-01 6.7449e-01; ...
6.0000e-01 8.4162e-01; ...
6.8269e-01 1.0000e+00; ...
7.0000e-01 1.0364e+00; ...
8.0000e-01 1.2816e+00; ...
8.5000e-01 1.4395e+00; ...
9.0000e-01 1.6449e+00; ...
9.3000e-01 1.8119e+00; ...
9.5000e-01 1.9600e+00; ...
9.6000e-01 2.0537e+00; ...
9.7000e-01 2.1701e+00; ...
9.8000e-01 2.3263e+00; ...
9.9000e-01 2.5758e+00; ...
9.9500e-01 2.8070e+00; ...
9.9900e-01 3.2905e+00];
Nsig = interp1(cn(:,1),cn(:,2),conf);
elseif 1-conf >= 1e-11
% For confidences within 1e-11 to 1e-3 of one
% (likely never used, but here for completeness)
c = log10(1-conf);
cn = [ ...
-3.0000e+00 3.2905e+00; ...
-4.0000e+00 3.8906e+00; ...
-5.0000e+00 4.4172e+00; ...
-6.0000e+00 4.8916e+00; ...
-7.0000e+00 5.3267e+00; ...
-8.0000e+00 5.7307e+00; ...
-9.0000e+00 6.1094e+00; ...
-1.0000e+01 6.4670e+00; ...
-1.1000e+01 6.8065e+00];
Nsig = interp1(cn(:,1),cn(:,2),c);
else
error('Confidence out of tabulated range');
end
end
% Calculate the approximate number of samples to achieve the accuracy at
% the 1-sigma confidence level
Nc1sig = 1/acc^2;
Ns1sig = Nc1sig/Pc;
% Adjust to calculate at the specified confidence interval
Ns0 = max(1,ceil(Nsig^2 * Ns1sig));
Ls = floor(log10(Ns0));
if (Ls > 1)
Ls = Ls-1;
end
Ms = 10^Ls;
Ns1 = ceil(Ns0/Ms);
Ns = Ns1*Ms;
% Adjust to make binofit return both one-sided accuracies to within the
% specified accuracy at the specified confidence
if (maxiter > 0)
% Calculate the alpha value from the confidence
alpha = (1-conf);
% Make the iteration increment for Nsamp approximately 3%
del = max(1,ceil(0.03*Ns1));
% Iterate until binofit indicates the desired accuracy has been
% achieved, or the maximum iteration number has been exceeded.
niter = 0;
iterating = true;
while iterating
% Calculate binofit confidence interval, Pclo < Pc < Pchi
if use_statistics_toolbox
[~,Pclohi] = binofit(round(Pc*Ns),Ns,alpha);
else
Pclohi = sqrt(round(Pc*Ns))/Ns;
end
% disp(['Ns = ' num2str(Ns) ' Acc = ' num2str(max(abs(Pclohi-Pc))/Pc)]);
niter = niter+1;
% Ensure the estimated accuracy is less than the desired accuracy
% on both the low and high sides of the interval
if max(abs(Pclohi-Pc)) > acc*Pc
% The desired accuracy has not been achieved, so increment
% the sample number
Ns = round(Ns/Ms+del)*Ms;
% Discontinue iterations if max value exceeded
if (niter >= maxiter)
iterating = false;
end
else
% The desired accuracy has been achieved, so stop iterating
iterating = false;
end
end
end
% Return final estimate for number of samples
Nsamp = Ns;
return;
end |