clear
clc

% Let's look at three samples: the generated normal distribution, the data from column 7 of the dataAll table, and the cat width for crabs from the 'crabs.xlsx' table. Let's find the main characteristics for each sample and select estimated values.

%Generate data (Remember: case of known General Population)
n1=100;
mu1=1, sigma1=2
X1 = (normrnd(mu1,sigma1,1,n1))';
m1=mean(X1), s1=std(X1)

% Let's look at Empirical Probability Distribution and determine outliers:

figure; subplot(1,2,1); histfit(X1);subplot(1,2,2); boxplot(X1);



% Q3-Q1~ iqr(data); If need (if outliers exist and strong influence on probability distribution) we can delete outliers. But not now!
% Just in case, let's remember:

Q=prctile(X1,[25 50 75]), IQR=Q(3)-Q(1)    
minvalue=Q(1)-iqr(X1)*1.5; 
maxvalue=Q(3)+iqr(X1)*1.5; 

% A. Characteristics Tests
% I) t-test
%      two-sides t-test (without ML test)   H0: mean(X)=mu (user implementation)
%           5 percent  significance level (two critical areas) plevel=0.025
%           CriteriaStat=(m-mu)/(s/sqrt(N)) has T(n-1) distribution

CriteriaStat1=(m1-mu1)/(s1/sqrt(n1))
CriteriaStat1CriteriaStat1 = (m1-mu1)/(s1/sqrt(n1))
 Xcrit1=tinv([0.025 0.975],n1-1) 
 if(Xcrit1(1)<CriteriaStat1<Xcrit1(2)),disp('mean=mu'),end;

% H0 is not rejected, m1=mu1, because CriteriaStat1 \in \text{ }\left(\text{Xcrit1}\left(1\right),\text{Xcrit1}\left(2\right)\right)
% 2)  MatLab ttest:
% [Hyp,Prob,ConfIinter1,STATS1] =ttest( X1,'tail', 'both','alpha',0.025 )
% Important:
% The data are assumed to come from a normal distribution with unknown variance (use sample variance)
% MatLab ttest treats NaNs as missing values, and ignores them!

m1, [Hyp,Prob,ConfIinter1,STATS1] =ttest( X1, m1,'tail', 'both','alpha',0.025 )

% II) The way MatLab Stat Toolbox, {\text{ }z}_{test}=\frac{\overline{x} −\mu }{\frac{\sigma }{\sqrt{n}}}; \mu, \sigma- parameters of the Normal distribution. For generated data we know theoretical parameters, so much better (correctly) to use Z-test!
% empirical characteristics are m1, s1

[hz1,pz1]=ztest(X1,m1,s1)

% Conclusion: Assuming that the sample is distributed normally (although the normality of the distribution has not yet been proven), now we proved that the hypothesis about the value of the mean coinciding with theoretical one is not rejected.
% III) Variance: vartest - MatLab function:
%            [h,p,ci,stats] = vartest(x,v,Name,Value)
% Returns a test solution for the null hypothesis that the data in the vector comes from a normal distribution with variance, using the chi-square test of variance. The alternative hypothesis is that it comes from a normal distribution with a different variance.

v1= var(X1);
[hv1,pv1,Ci,stats] = vartest(X1,v1) 

% Results are not good, but according hv1 and pv1 H0 is not rejected, but we remember, that, for vartest two-tailed test, default.
% May be outliers exist  and influence on result! You could check!
%% B. Distributions Tests
% We know, that the linear transformation of variable (or sample)  does not change the normality of the probability distribution, but only changes its parameters!
% Therefore, we need and can standardize the sample! What for?
%        I. Kolmogorov-Smirnov test.
%           kstest -   ML function
% [h,p,ksstat,cv]=kstest (X,name,value); cv - critical value
%  h,p - as before, X comes from a standard normal distribution, against the alternative that it does not come from such a distribution, (one-sample Kolmogorov-Smirnov test).

X1s=(X1-m1)/s1;
[hks1,pks1,ksstat,cv]=kstest(X1s);
r=['H =',num2str(hks1),' P=',num2str(pks1),...
' TestStat=',num2str(ksstat),' CritV=',num2str(cv)];
disp(r);

% This means (TestStat<CritV) that the null hypothesis is not rejected.
%
% C. Chi-square goodness-of-fit test
%        I. chi2gof
% \chi^2=\sum_{i=1}^n \frac{{\left(O_i −E_i \right)}^2 }{E_i } - TestStat; histogram - is the main source for determination observed points.  Result of MatLab test strongly depends on histogram quality!!! We can choose unsuccessfully, for example, the number of bins!!!!
% The chi2gof function returns a test solution for the null hypothesis that the data in the vector x comes from a normal distribution with the mean and variance estimated over Sample using the chi-square goodness-of-fit test.

[hch1,pch1] = chi2gof(X1,'Nbins',8)

%     Number of bins is important! If we have the empty bins, we need reduce their number!

[hch21,pch21] = chi2gof(X1,'Nbins',40)
figure
subplot(1,2,1),histfit(X1,8);
subplot(1,2,2),histfit(X1,40); 



% We get a fairly high probability for the first two samples and h=0. This means that in these cases the null hypothesis is not rejected. For the third sample, h=1 and the probability is very small, this indicates a rejection of the null hypothesis.
%