File size: 5,421 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 | function [Nsamp] = EstimateRequiredSamples(Pc,acc,conf,maxiter)
% EstimateRequiredSamples - Estimates the number of required Monte-Carlo
% sample trials needed to receive the accuracy
% and confidence levels needed for the Pc passed
% in.
%
% Syntax: [Nsamp] = EstimateRequiredSamples(Pc,acc,conf,maxiter);
%
% =========================================================================
%
% Copyright (c) 2023-2025 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Description:
%
% 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.'
%
% =========================================================================
%
% Initial version: Mar 2023; Latest update: Aug 2025
%
% ----------------- BEGIN CODE -----------------
% 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 sensible input
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) || (maxiter == Inf)
maxiter = 100;
end
% Calculate the number of sigmas correspond to the confidence interval (for
% one degree of freedom)
Nsig = sqrt(chi2inv(conf,1));
% 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
[~,Pclohi] = binofit(round(Pc*Ns),Ns,alpha);
% 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
% ----------------- END OF CODE ------------------
%
% Please record any changes to the software in the change history
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer | Date | Description
% ---------------------------------------------------
% L. Baars | 2023-Mar-13 | Initial Development
% L. Baars | 2025-Aug-06 | Minor documentation updates necessary for
% public release.
% =========================================================================
%
% Copyright (c) 2023-2025 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% ========================================================================= |