% Laboratory Work «Preliminary Analysis»
% see  ExamplePreliminaryAnalysis.mlx in the Teams
% Step  1. Download the data

%load('dataMy','-mat')
load dataMy 

% Step 2. Data Visualization (MatLab function names in the text are written by Italics).
% Use function boxplot for data explanation and add values of  Q1,Q2,Q3 to the graph. Explain if exists data skewness, use different approaches.

boxplot(data)
hold on % it means option - add for a new graph

%  Boxplot  results explanation:
% 1) To evaluate the distribution symmetry, it is necessary to determine the equality of the median and the mean:

             myeps=0.01; %  
       m=mean(data); md=median(data);
       
if abs(m-md)>myeps, disp('mean~=median'), ...
                      else,disp('mean=median'), end 
disp(['mean=',num2str(m), '  median=',num2str(md)])

% 2) Let's define an interval for correct data. Out of this interval  - outliers. First  let's find quartiles Q1,Q2,Q3 or the same  thing – percentiles (prctile),  interquartiles (iqr), which is equal Q3-Q1:

Q2=prctile(data,50); % can compare with median - are the same
Q1=prctile(data,25); Q3=prctile(data,75);
IQR=iqr(data);    % Q3-Q1~ iqr(data)
minvalue=Q1-iqr(data)*1.5; % Presentation - 2, slide - 14
maxvalue=Q3+iqr(data)*1.5; % 
disp(['Q2=median=',num2str(Q2)]);
disp(['data interval=[',num2str(minvalue),',',num2str(maxvalue),']']);

% Step 3. Data Preprocessing
% There are four outliers that we can see on the graph. We can easy exclude them, using one of two ways:
% The first way:

data1=data;
data1(data1<minvalue) =[]; data1(data1>maxvalue) =[];

% The second way:

data2=data; data2=sort(data2); 
%sort(data2,'descend')% descend - alternative to default option 
data2=data2(3:end-2); % witout outliers

%
% Step 4. The study of an edited experimental sample.
% Two way preprocessing result:

subplot(1,2,1), boxplot(data1), title('data1')
subplot(1,2,2), boxplot(data2), title('data2')

% data1 and data2 are the same!
%         2. Graphical normality assessment:
% I will be waiting for your answers here!
% What does it mean that the points are close to a straight line?
% (Learn the properties of the normplot function yourself)

figure, normplot(data1)% explain result!

%         3.  Calculate all the characteristics of the sample: mean, median, variance, skewness, standard deviation. Compare with the corresponding characteristics that were before preprocessing.
%         4. Draw conclussions!
% Step 5. Empirical probability density function.
% Remark!   For histogram should to choose appropriate options.
%         1. Histogram as an empirical probability density function:

 histogram(data1,'Normalization','pdf'), hold on 

%          2. Add the theoretical probability density function (pdf) for  the Normal distribution. (hold on - explain for what?)

 step = 0.02; X=-3:step:3; Y=normpdf(X,0,1);
 plot(X,Y,'r-');
 l=legend('histogram data1','pdf')  % without output
 l.FontSize=14, l.Location='best' 

%         3. Calculate  critical point C on the axes so such that probability P(C<x<+infinity)= 0.05 and fill the area under the  density function on the graph for x> C with any color.
% This approach is valid for any distribution with a given density function f.

 figure,  
 X=-3.2:step:3.2; Y=normpdf(X,0,1);
 plot(X,Y,'b-');hold on
 C=norminv(1-0.05,0,1);
 pp= plot(C,0,'r*','linewidth',2); grid on 
 text(0.9,0.025,num2str(C,3))

% Critical area  visualization:

 Cy=normpdf(C,0,1); plot(C,Cy,'g*','linewidth',2); % green point

% The area under the density function  to the right of the critical point:

 Xc=3.2:-0.01:C; Yc=zeros(size(Xc));
 Yvert=0:0.1:Cy; Xvert=C*ones(size(Yvert));
 Xpdf=C:0.01:3.2; Ypdf=normpdf(Xpdf,0,1);
 Xall=[Xc';Xvert';Xpdf']; Yall=[Yc';Yvert';Ypdf'];
 t=fill(Xall,Yall,'m')

% Control of the equality of probability P(x>C) and the square Sarea under the curve of the probability density function f(x), x>C f\left(x\right)=\frac{1}{\sqrt{2\text{ }\pi }}exp(-x^2/2)

f=@(x) exp(-x.^2/2)./sqrt(2*pi);
%     waiting 0.05. Why?
integral(f,C,inf) % It's correct!

% Step 6. Empirical probability distribution function - ecdf.
% Using function ecdf  for the empirical probability distribution function
% \hat{F}(y) and theoretical normal distribution function F(y), depends on parameters: mean value and standard deviation, calculated by the sample.

figure, ecdf(data1), hold on
m1=mean(data1),s1=std(data1)
Y=normcdf(X,m,s1); plot(X,Y,'r-')
title(['\mu=',num2str(m1,2),'s=',num2str(s1,2)])
legend('ecdf','F(y)')