function or = unmatch(A, B, C, D)
%
% Miettinens Test-Based CI for OR in paired tables
 
% [A B; C D] matched pairs A -> (event, event)
% p1 = probability of the event on the first coordinate
% p2 = probability of the event on the second coordinate
% for N=A+B+C+D pairs the function unmatches the pairs
%     and forms n contingency tables [a b; c d] in
%     which exactly two of the a, b, c, and d are 1.
%     The Mantel Haenszel Statistics depends on tables only
%     through products ad and bc and the total tot equal to
%     2 in this case.
   

ads = 0; bcs=0;  
for i = 1:A
    ad = 0; bc = 0; tot=2;
    ads = ads + ad/tot;
    bcs = bcs + bc/tot;
end
for i = 1:B
    ad = 1; bc=0; tot=2;
    ads = ads + ad/tot;
    bcs = bcs + bc/tot;
end
for i = 1:C
    ad = 0; bc=1; tot=2;
    ads = ads + ad/tot;
    bcs = bcs + bc/tot;
end
for i = 1:D
    ad = 0; bc=0; tot=2;
    ads = ads + ad/tot;
    bcs = bcs + bc/tot;
end
or.OR1 = ads/bcs;
or.OR2 = B/C;
or.chi2 = (B-C)^2 /(B + C);
or.chi2adj = (abs(B-C) -1)^2 /(B + C);
or.lor = log(or.OR1);
or.varlor = or.lor^2/or.chi2;
or.varrgb = 1/B + 1/C;
or.stdlor = sqrt(or.varlor);
or.cill = or.lor - 1.96 * or.stdlor;
or.cilu = or.lor + 1.96 * or.stdlor;
or.cil = exp(or.cill);
or.ciu = exp(or.cilu);

