%==========================================================================
% Gaussian_CL_Erf_Int.m
%
% Calculates double-sided (DS) and single-sided (SS) Confidence Intervals
% and Confidence Levels (aka P-values) for the Gaussian/Normal dist'n 
% as a fcn of N-sigma cut, using the Erf(x) fcn...
%
% Created by Prof. Steven Errede  09/20/2012 10:00 hr
%
% Last updated:                   09/27/2012 14:50 hr {SME}
%
%==========================================================================
% Please see/read UIUC Physics 598AEM Lecture Notes 8 p. 3-8 for details:
% http://courses.physics.illinois.edu/phys598aem/598aem_lectures.html
%==========================================================================
%
%   Nsig = # sigma = (Xhi-Xo)/Xsig = (Xo-Xlo)/Xsig (Here: Xo = 0, Xsig = 1)
%   DS Prob is the area under the Gaussian within central band of +_ Nsig.
%   DS (1-Prob) is the remaining area - in the hi/lo-side tails of Gaussian.
%
%     Nsig     DS Prob(Nsig)  DS (1-Prob(Nsig))  SS Prob(Nsig)  SS (1-Prob(Nsig))
%   ========   =============  =================  =============  =================
%      0         0.000000        1.000000           0.500000       0.500000
%      1         0.682689        0.317311           0.841345       0.158655
%      2         0.954500        0.045500           0.977250       0.022750
%      3         0.997300        0.002700           0.998650       0.001350
%      4         0.999937        0.000063           0.999968       0.000032
%      5         0.999999        6x10^-7            1.000000       0.000000
%      6         1.000000        2x10^-9            1.000000       0.000000
%
%   0.674500     0.50            0.50               0.750000       0.250000
%   1.644854     0.90            0.10               0.950000       0.050000
%   1.959964     0.95            0.05               0.975000       0.025000
%   2.579964     0.99            0.01               0.995000       0.005000
%   3.290567     0.999           1x10^-3            0.999500       0.000500
%   3.890592     0.9999          1x10^-4            0.999950       0.000050
%   4.417173     0.99999         1x10^-5            0.999995       0.000005
%
%==========================================================================
clear all;
close all;

double Xcut;

Npts  = 12;

Nsig     = zeros(1,Npts);
Prob_ds  = zeros(1,Npts);
Prob_ss  = zeros(1,Npts);

fprintf('\n');
fprintf('\n=================================================================');
fprintf('\n=== Gaussian Double-Sided & Single-Sided Confidence Intervals ===');
fprintf('\n===                    Uses Erf(x) Method                     ===');
fprintf('\n=================================================================');

fprintf('\n');
fprintf('\n  j  Nsig(j)  Prob_DS(j)  1-Prob_DS(j)  Prob_SS(j)  1-Prob_SS(j)');
fprintf('\n === =======  ==========  ============  ==========  ============');

for j = 1:Npts;
    Nsig(j) = 0.5*j;        % # of Sigma
    Xcut    = double(Nsig(j)/sqrt(2.0));
    Prob_ds(j) = erf(Xcut); % carry out erf(x) calculation in double-precision..
    
    Prob_ss(j) = 1.0 - 0.5*(1.0-Prob_ds(j)); % n.b. thus: Prob_ss(j) = 0.5*(1.0 + Prob_ds(j))
   
    fprintf('\n %2i    %3.1f     %8.6f     %8.6f     %8.6f     %8.6f',j,Nsig(j),Prob_ds(j),(1.0-Prob_ds(j)),Prob_ss(j),(1.0-Prob_ss(j)));
end

beep();
fprintf('\n');
fprintf('\n Gaussian_CL_Erf_Int calculations completed! \n');