File size: 3,622 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 | function [amd,alo,ahi] = ConjDist_resample(xmd,xlo,xhi,Nsamp,alpha,limits)
% ConjDist_resample - Calculate the average for a set of numbers, and
% resample to get the 1-alpha confidence range
%
% Syntax: [amd,alo,ahi] = ConjDist_resample(xmd,xlo,xhi,Nsamp,alpha,limits)
%
% =========================================================================
%
% Copyright (c) 2025 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% =========================================================================
%
% Description: Calculate the average for a set of numbers, and
% resample to get the 1-alpha confidence range
%
% =========================================================================
%
% Input:
%
% xmd - Median values [Nx1]
%
% xlo - low-deviation (-1 sigma) values [Nx1]
%
% xhi - high-deviation (+1 sigma) values [Nx1]
%
% Nsamp - Optional - number of points to resample
% (default = 40)
%
% alpha - Optional - confidence interval range
% (default = 0.05: 1 - alpha = 95% confidence interval)
%
% limits - Optional - Lower/upper limits for resampled data [1x2]
% (default = [-inf, inf])
% =========================================================================
%
% Output:
%
% amd - average median value
%
% alo - average low-deviation (-1 sigma) confidence value
%
% ahi - average high-deviation (+1 sigma) confidence value
%
% =========================================================================
%
% Dependencies:
%
% None
%
% =========================================================================
%
% Subfunctions:
%
% None
%
% =========================================================================
%
% Initial version: Apr 2022; Latest update: Apr 2022
%
% ----------------- BEGIN CODE -----------------
% Initializations
Nargin = nargin;
if (Nargin < 6); limits = [-Inf,Inf]; end
if (Nargin < 5); alpha = 0.05; end
if (Nargin < 4); Nsamp = 40; end
% Median
amd = mean(xmd);
% Calculate 1-sigma low and high deviations
chifact = sqrt(chi2inv(1-alpha,1));
siglo = (xmd-xlo)/chifact;
sighi = (xhi-xmd)/chifact;
% Calculate multiple samples per x point
Nx = numel(xmd);
Nxs = Nx*Nsamp;
xsamp = zeros(Nx,Nsamp);
for nx=1:Nx
rsamp = randn(1,Nsamp);
ndx = (rsamp < 0); % Low deviations
xsamp(nx,ndx) = xmd(nx)+siglo(nx)*rsamp(ndx);
ndx = ~ndx; % High deviations
xsamp(nx,ndx) = xmd(nx)+sighi(nx)*rsamp(ndx);
end
xsrt = sort(xsamp(:));
xsrt(xsrt < limits(1)) = limits(1);
xsrt(xsrt > limits(2)) = limits(2);
al = alpha/2;
n1 = max(floor(al*Nxs),1);
n2 = min(ceil(Nxs-al*Nxs),Nxs);
alo = xsrt(n1);
ahi = xsrt(n2);
return
end
% ----------------- END OF CODE ------------------
%
% Please record any changes to the software in the change history
% shown below:
%
% ----------------- CHANGE HISTORY ------------------
% Developer | Date | Description
% ---------------------------------------------------
% D. Hall | 2022-Apr-11 | Initial version.
% =========================================================================
%
% Copyright (c) 2025 United States Government as represented by the
% Administrator of the National Aeronautics and Space Administration.
% All Rights Reserved.
%
% ========================================================================= |