function [odhp0,podminka1,dWpp,hWpp,dSpp,hSpp,odhP,podminka2,dW,hW,dS,hS]=odhady(data,alfa)
% funkce odhady pocita bodove a intervalove odhady pocatecnich a prechodovych pravdepodobnosti 
% syntaxe: [odhp0,podminka1,dWpp,hWpp,dSpp,hSpp,odhP,podminka2,dW,hW,dS,hS]=odhady(data,alfa)
% vstupni parametry:
% data ... matice kxk poctu pozorovani prechodu mezi stavy
% alfa ... riziko
% vystupni parametry:
% odhp0 ... odhad vektoru pocatecnich pravdepodobnosti
% podminka1 ... podminky dobre aproximace pro pocatecni pravdepodobnosti
% dWpp, hWpp ... meze Waldovych intervalu spolehlivosti 
% pro pocatecni pravdepodobnosti
% dSpp, hSpp ... meze skorovych intervalu spolehlivosti 
% pro pocatecni pravdepodobnosti
% odhP ... odhad matice prechodu
% podminka2 ... podminky dobre aproximace pro pravdepodobnosti prechodu
% dW, hW ... meze Waldovych intervalu spolehlivosti
% pro pravdepodobnosti prechodu
% dS, hS ... meze skorovych intervalu spolehlivosti
% pro pravdepodobnosti prechodu

k=size(data,1);
ci=sum(data');
c=sum(ci);
odhp0=ci/c;
for i=1:k
podminka1(i)=odhp0(i)*(1-odhp0(i))*c;
end
podminka1;
dWpp=odhp0-sqrt(odhp0.*(1-odhp0)/c)*norminv(0.975,0,1);
hWpp=odhp0+sqrt(odhp0.*(1-odhp0)/c)*norminv(0.975,0,1);
delkaW=hWpp-dWpp;
dSpp=(2*odhp0+norminv(0.975,0,1)^2/c-sqrt((2*odhp0+norminv(0.975,0,1)^2/c).^2-4*(1+norminv(0.975,0,1)^2/c)*odhp0.^2))/(2*(1+norminv(0.975,0,1)^2/c));
hSpp=(2*odhp0+norminv(0.975,0,1)^2/c+sqrt((2*odhp0+norminv(0.975,0,1)^2/c).^2-4*(1+norminv(0.975,0,1)^2/c)*odhp0.^2))/(2*(1+norminv(0.975,0,1)^2/c));
delkaS=hSpp-dSpp;
for i=1:k
    for j=1:k
        odhP(i,j)=data(i,j)/ci(i);
    end
end
odhP;
plot([1:k],dWpp,'b--',[1:k],hWpp,'b--',[1:k],dWpp,'ro',[1:k],hWpp,'ro',[1:k],odhp0,'k*')
title('Odhady pocatecnich pravdep. a meze 95% Waldovych IS')
figure
 plot([1:k],dSpp,'b--',[1:k],hSpp,'b--',[1:k],dSpp,'ro',[1:k],hSpp,'ro',[1:k],odhp0,'k*')
title('Odhady pocatecnich pravdep. a meze 95% skorovych IS')
for i=1:k
    for j=1:k
        podminka2(i,j)=odhP(i,j)*(1-odhP(i,j))*ci(i);
    end
end
podminka2;
dW=[];hW=[];
for i=1:k
    dWald=odhP(i,:)-sqrt(odhP(i,:).*(1-odhP(i,:))/ci(i))*norminv(0.975,0,1);
    hWald=odhP(i,:)+sqrt(odhP(i,:).*(1-odhP(i,:))/ci(i))*norminv(0.975,0,1);
    dW=[dW;dWald];hW=[hW;hWald];
end
dW;
hW;
dS=[];hS=[];
for i=1:k
    dSk=(2*odhP(i,:)+norminv(0.975,0,1)^2/ci(i)-sqrt((2*odhP(i,:)+norminv(0.975,0,1)^2/ci(i)).^2-4*(1+norminv(0.975,0,1)^2/ci(i))*odhP(i,:).^2))/(2*(1+norminv(0.975,0,1)^2/ci(i)));
    hSk=(2*odhP(i,:)+norminv(0.975,0,1)^2/ci(i)+sqrt((2*odhP(i,:)+norminv(0.975,0,1)^2/ci(i)).^2-4*(1+norminv(0.975,0,1)^2/ci(i))*odhP(i,:).^2))/(2*(1+norminv(0.975,0,1)^2/ci(i)));
    dS=[dS;dSk];hS=[hS;hSk];
end
dS;
hS;

    
