%Dane z katastrofy Titanica i test chi^2
tab=[203, 118, 178;
     122, 167, 528];
xsize=size(tab,2);ysize=size(tab,1);
suma_calk=sum(sum(tab));
suma_x=sum(tab,1);suma_y=sum(tab,2);
for x=1:xsize
    for y=1:ysize
        tab_o(y,x)=suma_y(y).*suma_x(x)/suma_calk;
    end
end
chi2=0;
for x=1:xsize
    for y=1:ysize
        chi2=chi2+(tab(y,x)-tab_o(y,x))^2/tab_o(y,x);
    end
end
df=(xsize-1)*(ysize-1); p=chi2cdf(chi2,df); 
disp(sprintf('chi2=%.1f, ilosc stopni swobody=%d, p=%.5f', chi2, df, 1-p))