function  [Cramer, Pearson, Chuprow, T, pvalue ] = concoef(obs)
% Contingency Table r x c for testing the probabilities
% independence and homogeneity of populations...
%
% Input:
%   obs - r x c matrix of observations.
%  
% Output
%    T  - statistics (approx distributed as st. normal)
%    pvalue  - p - value
%    Cramer -  sqrt(T/(N*(min(r,c)-1))) 
%    Pearson - sqrt(T/(N+T))
% Example of use: [c,p,ch, t,p]=concoef([6 14 17 9; 30 32 17 3])
%------------------------------------------------------------
T=0;
CC = sum(obs);
RR = sum(obs');
N = sum(sum(obs));
[r c]=size(obs);
for i=1:r
    for j=1:c
        E = RR(i)*CC(j)/N;
        T = T + (E - obs(i,j))^2 /E;
    end
end
df=(r-1)*(c-1);
pvalue = 1- cdf_chi2(T, df);
Cramer =  sqrt(T/(N*(min(r,c)-1)));
Pearson = sqrt(T/(N+T));
Chuprow = sqrt( T/(N * sqrt((r-1)*(c-1))));
