warning off
clear

% parametry jednoho dvourozmerneho rozdeleni s nekorelovanymi velicinami
miq = [ 1; 5 ];
sigmaq = [ 10 0 ; 0 100 ];
ppq = [0.5 0.5];
nq = 30;

% hodnoty bodu tohoto rozdeleni
[XMix, IMix] = nmix (miq, sigmaq, ppq, nq);

% spocitej zpet parametry daneho rozdeleni
%[mip, sigmap] = unsuni(XMix, 1);

%odchylka_mi = mip - miq
%odchylak_sigma = sigmap - sigmaq

load isaac;
n=1890;

nuly=[];
jednicky=[];
for ii=1:n
if I(ii)==0 nuly=[nuly X(:,ii)]; end
if I(ii)==1 jednicky=[jednicky X(:,ii)]; end
end

[a,pocet_nul]=size(nuly);
[a,pocet_jednicek]=size(jednicky);
trn0 = fix(pocet_nul/2);
trenovaci_nuly=nuly(:,1:trn0);
trn1 = fix(pocet_jednicek/2);
trenovaci_jednicky=jednicky(:,1:trn1);

%for ii=1:10
%imshow(reshape(nuly(:,ii),8,8),[]);
%end

% osm parametru pro rozpoznavani (8D prostor) - v podstate radky matice obrazce
%XMix = reshape(trenovaci_jednicky(:,1), 8, 8);
%[mi1, sigma1] = unsuni(XMix,1)

sumi1 = zeros(8,1);
susigma1 = zeros(8,8);
for ii=1:trn1
XMix = reshape(trenovaci_jednicky(:,ii), 8, 8);
[mi1, sigma1] = unsuni(XMix,1);
sumi1 = sumi1 + mi1;
susigma1 = susigma1 + sigma1;
end
sumi1 = sumi1/trn1;
susigma1 = susigma1/trn1;

sumi0 = zeros(8,1);
susigma0 = zeros(8,8);
for ii=1:trn0
XMix = reshape(trenovaci_nuly(:,ii), 8, 8);
[mi0, sigma0] = unsuni(XMix,1);
sumi0 = sumi0 + mi0;
susigma0 = susigma0 + sigma0;
end
sumi0 = sumi0/trn0;
susigma0 = susigma0/trn0;

% klasifikuj - pravdepodobnost vyskytu 0 i 1 je stejna = 0.5

tnocc = 0; %total number of correctly classified

nocc = 0; % number of correctly classified
for ii=1:trn0
X = reshape(trenovaci_nuly(:,ii), 8, 8);
I = bayescln(X,[sumi0,sumi1],[susigma0,susigma1],[0.5,0.5]);
jon = sum (I)/8;
if jon<=1.5 
    nocc = nocc + 1; 
end
end

disp(['Dobre rozpoznanych NUL: ',num2str(nocc),' z ', num2str(trn0), ' = ', num2str(nocc/trn0*100),'% z trenovaci mnoziny!']);

tnocc = tnocc + nocc;

nocc = 0;
for ii=trn0+1:pocet_nul
X = reshape(nuly(:,ii), 8, 8);
I = bayescln(X,[sumi0,sumi1],[susigma0,susigma1],[0.5,0.5]);
jon = sum (I)/8;
if jon<=1.5 
    nocc = nocc + 1; 
end
end

disp(['Dobre rozpoznanych NUL: ',num2str(nocc),' z ', num2str(pocet_nul-trn0), ' = ', num2str(nocc/(pocet_nul-trn0)*100),'% z testovaci mnoziny!']);

tnocc = tnocc + nocc;

nocc = 0; % number of correctly classified
for ii=1:trn1
X = reshape(trenovaci_jednicky(:,ii), 8, 8);
I = bayescln(X,[sumi0,sumi1],[susigma0,susigma1],[0.5,0.5]);
jon = sum (I)/8;
if jon>1.5 
    nocc = nocc + 1; 
end
end

disp(['Dobre rozpoznanych JEDNICEK: ',num2str(nocc),' z ', num2str(trn1), ' = ', num2str(nocc/trn1*100),'% z trenovaci mnoziny!']);

tnocc = tnocc + nocc;

nocc = 0;
for ii=trn1+1:pocet_jednicek
X = reshape(jednicky(:,ii), 8, 8);
I = bayescln(X,[sumi0,sumi1],[susigma0,susigma1],[0.5,0.5]);
jon = sum (I)/8;
if jon>1.5 
    nocc = nocc + 1; 
end
end

disp(['Dobre rozpoznanych JEDNICEK: ',num2str(nocc),' z ', num2str(pocet_jednicek-trn1), ' = ', num2str(nocc/(pocet_jednicek-trn1)*100),'% z testovaci mnoziny!']);

tnocc = tnocc + nocc;
celkem01 = pocet_jednicek + pocet_nul;

disp(['Dobre rozpoznanych cislic celkem: ',num2str(tnocc),' z ',num2str(celkem01),' = ',num2str(tnocc/celkem01*100),'%']);