function  [T,pvalue] = table2x2(obs, alt)
% Contingency Table 2 x 2 for testing the probabilities
%
% H_0: p1 = p2 where p_i is the probability observation
% from Row i belonging to Col 1.
% Input:
%   obs - 2x2 matrix of observations [O11 O12; O21 O22].
%   alt - alternative: -1 p1<p2,  0  p1 ne p2, and 1 p1>p2.
% Output
%    T  - statistics (approx distributed as st. normal)
%    pvalue  - p - value
%------------------------------------------------------------
columns= sum(obs);
rows = sum(obs');
tot = sum(sum(obs));
pro = prod( columns .* rows );
T = sqrt(tot) * det(obs)/sqrt(pro);
p1 = normcdf(T);
p2 = 1 - normcdf(T);
if alt == -1
    pvalue = p2;
elseif alt == 0
    pvalue = 2*min(p1,p2);
elseif alt == 1
    pvalue = p1;
else warning('alternative hypothesis unknown')
end