%confint.m

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

%
% LVEF is a good clinical indicator of left ventricular systolic function.
%LVEF, defined as  
%the proportion of its blood volume the left ventricle ejects with each heartbeat,
%may be determined either by echocardiograph 
%or radionuclide ventriculography.
%So if the LVEF is 0.50, the left ventricle is ejecting half its blood volume with 
%each heartbeat. A normal LVEF is 0.50 or greater. The LVEF is a good reflection 
%of the amount of damage the left ventricle has sustained due to a dilated
%cardiomyopathy.


% measurements: X left ventricular ejection fraction (LVEF)

% for 27 patients with acute dilated cardiomyopathy
X = [0.19 0.24 0.17 0.40 0.40 0.23 0.20 0.20 0.30 ...
     0.19 0.24 0.32 0.32 0.28 0.24 0.18 0.22 0.23 ...
     0.14 0.14 0.30 0.07 0.12 0.13 0.17 0.24 0.19];

 figure(1)
 subplot(2,1,1)
 hist(X,10)
 subplot(2,1,2)
 qqplot(X)
 %%
 %assume sigma is known and equal to 0.08.
 % Then 95% z-CI  is
 confidence = 0.95;
 n = length(X); % n is 27
 sigma = 0.08;
 alpha = 1 - confidence; %confidence is 1-alpha
 LBz=mean(X) - norminv(1-alpha/2) * sigma/sqrt(n);
 UBz=mean(X) + norminv(1-alpha/2) * sigma/sqrt(n);
 [LBz, UBz]
 
 Lzint = UBz-LBz
 
 %%
 %assume sigma is not known (most of the time)
  % Then 95% z-CI  is
 confidence = 0.95;
 n = length(X); % n is 27
 alpha = 1 - confidence; %confidence is 1-alpha
 LBt= mean(X) - tinv(1-alpha/2, n-1) * std(X)/sqrt(n);
 UBt= mean(X) + tinv(1-alpha/2, n-1) * std(X)/sqrt(n);
 
 [LBt, UBt]
 
 Ltint = UBt-LBt

 %%

 %Confidence interval for variance
 confidence = 0.95;
 
 s2 = var(X)
 n= length(X); 
 alpha = 1 - confidence;
 
 LBs = s2 * (n-1)/chi2inv(1-alpha/2, n-1)
 UBs = s2 * (n-1)/chi2inv(alpha/2, n-1)
 Lvarint = UBs - LBs
 

%%
 