%%
clear all
close all 
clc

load('path.mat');
load(fullfile(PathName2,FileName3));
chi2_56 = 74.5; %hodnota chi kvadrat pro chi(p(p+1)(p+2)/6)=chi(56)=74.5
                %pri alfa=5% (alfa=1% chi=83.5 )
z = 1.96;       %pri alfa=5% (alfa=1% z = 2.58)
parametr_n = cell('');
parametr_n{1} = 'f_m_a_x'; parametr_n{2} = 'f_m_i_n'; parametr_n{3} = 'A';
parametr_n{4} = 'D'; parametr_n{5} = 'T_0'; parametr_n{6} = 'PI';
scrsz = get(0,'ScreenSize');
for misto_n = 1:3
    switch misto_n
        case 1
            misto = mat_AVG_test.AF;
            misto_title = 'AF - a. femoralis';
        case 2
            misto = mat_AVG_test.AP;
            misto_title = 'AP - a. poplitea';
        case 3
            misto = mat_AVG_test.ATP;
            misto_title = 'ATP - a. tibialis posterior';
    end
%% sikmost 1
    n = size(misto,1);
    sikmost = skewness(misto,1,1);
    K3 = abs(sqrt((sikmost*(n+1)*(n+3)) / (6*(n-2))))

%% spicatost 1
    spicatost = kurtosis(misto,1,1);
    K4 = abs(sqrt( ((n+1).^2*(n+3)*(n+5)) / (24*n*(n-2)*(n-3)) ) * (spicatost-3+(6/(n+1))))

%% Vykresleni q-q grafu
    for parametr = 1:size(misto,2)
    x = misto(:,parametr);
    figure(misto_n);
    subplot(2,3,parametr)
    normplot(x);
%     title(['Rankitovy q-q graf ',znak(index,:)]); 
    grid on;
%     normalita = vartestn(vstup:,index));
%     normalita = lillietest(x,0.001,'norm');
%     if normalita==1
    if and(K3(parametr)>= 2.58,K4(parametr)>= 2.58)
        normalita = 'zamitnuta';
        barva = 'r';
    else normalita = 'prijata';
        barva = [30 220 30]/255;
    end
    text(min(x),2.5,[['si=';'sp='],num2str([sikmost(parametr);spicatost(parametr)],2)],'VerticalAlignment','top',...
        'HorizontalAlignment', 'Left','BackgroundColor','w',...
        'EdgeColor','k','FontWeight','Bold');
    text(min(x),1.5,['normalita ',normalita],'VerticalAlignment','bottom',...
        'HorizontalAlignment', 'Left','BackgroundColor','w',...
        'EdgeColor','k','Color',barva,'FontWeight','Bold');
    title(parametr_n{parametr},'FontWeight','Bold');
    if parametr==6
        set(figure(misto_n),'NumberTitle','off','Name',misto_title);        
    end
end
%% sikmost 2
    p = size(misto,2);
    n = numel(misto);
    cov_matice = cov(misto);
    prum  = mean(misto,1);
    x = misto - repmat(prum,size(misto,1),1);
    x = (x'*x*inv(cov_matice)).^2;

    sikmost2 = sum(sum(x))/(n^2);
    K5 = n/6*sikmost2;
    if K5>=chi2_56
        normalita_si = 'MAME';
    else
        normalita_si = 'NEMAME';
    end

%% spicatost 2
    spicatost2 = sum(x(:,1))/(n);
    K6 = abs(sqrt((n)/(8*p*(p+2)))*(spicatost2-p*(p+2)));
    if K6>=z
        normalita_sp = 'MAME';
    else
        normalita_sp = 'NEMAME';
    end

%% Vypis hypotez
    fprintf('Hypoteza H0: Ma vyber normalni sikmost vícerozmerneho vyberu 0? \n')
    fprintf('Sikmost vyberu = %.2f \n',sikmost2)
    fprintf('Statistika sikmosti K5 = %.2f \n',K5);
    fprintf('Kvantil Chi2(56)=%.2f \n',chi2_56);
    fprintf('Na hladine vyznamnosti alfa=5%% %s duvod zamitnout hypotezu H0,\n',normalita_si);
    fprintf('ze vyber ma normalni statistiku sikmosti.\n\n')
    fprintf('Hypoteza H1: Ma vyber normalni spicatost vícerozmerneho vyberu %.0f? \n',p*(p+2))
    fprintf('Spicatost vyberu = %.2f \n',spicatost2)
    fprintf('Statistika spicatosti K6 = %.2f \n',K6);
    fprintf('Kvantil z(1-alfa/2)=%.2f \n',z);
    fprintf('Na hladine vyznamnosti alfa=5%% %s duvod zamitnout hypotezu H1,\n',normalita_sp);
    fprintf('ze vyber ma normalni statistiku spicatosti.\n')
    fprintf('--------------------------------------------------------------------------------\n')
end

