 disp('Tolerance Intervals')
 lw = 3; 
 set(0, 'DefaultAxesFontSize', 16);
 fs = 15;
 msize = 10;

%Tolerance Intervals
% 1 - alpha: Confidence
% 1 - gamma: Proportion

%% TWOSIDED
n=20;
alpha = 0.01; %confidence 1-alpha=1-0.01=0.99
gam =  0.05  %proportion of 1-gamma=1-0.05=0.95
             %two sided,  1-0.05/2 quantile
z = norminv(1-gam/2)                      
xi = chi2inv(alpha, n-1) %confidence 1-alpha=1-0.01=0.99
k = sqrt( (n^2-1) * z^2/(n * xi))
[12 - k * 0.1  12 + k * 0.1]


%% 
% ONE SIDED
close all;
clear all
n=20;
alpha = 0.01; %confidence 1-alpha=1-0.01=0.99
gam = 0.05; %proportion 1-gamma=1-0.05 = 0.95
a = 1 - norminv(1-alpha)^2/(2 * (n-1));
b = norminv(1-gam)^2 - norminv(1-alpha)^2/n;
k1 = (norminv(1 - gam) + ...
    sqrt( norminv(1 - gam)^2 - a * b ))/a
12 + k1 * 0.1
12 - k1 * 0.1
