function [] = mcnemart2(matr)
A=matr(1,1); B= matr(1,2); C= matr(2,1); D=matr(2,2);

if( A*B*C*D==0 )
    matr = matr + 0.5;
end

N = A + B + C + D;

p1 = (A + B)/N; %prob treat caries
p2 = (A + C)/N; %prob control caries

p11 = A/N;
p12 = B/N;
p21 = C/N;
p22 = D/N;

% risk difference
diffp1p2 = p1 - p2
   delta = p11*p22 - p12*p21
s = sqrt( p1*(1-p1)/N + p2*(1-p2)/N + 2*delta/N )
ssel = sqrt( ((B+C)*N  - (B-C)^2)/N^3 )
   lbd = p1 - p2 - norminv(0.975)*s;
   ubd = p1 - p2 + norminv(0.975)*s;
cidiff = [lbd, ubd]


% odds ratio
or = B/C
%---------------- miettinen approx---------------------
s2lor = (log(B/C))^2 * (B+C)/((abs(B-C)-1)^2 + eps)
slor = sqrt(s2lor)
    lblor = log(or) - norminv(0.975)*slor ;
    ublor = log(or) + norminv(0.975)*slor ;
miettlint = [lblor, ublor]
miettint = [exp(lblor), exp(ublor)]

% RGB approx
slor1 = sqrt(1/B + 1/C)

lblor1 = log(or) - norminv(0.975)*slor1;
ublor1 = log(or) + norminv(0.975)*slor1 ;
rgblint = [lblor1, ublor1]
rgbint = [exp(lblor1), exp(ublor1)]
% --------- exact method ------------------
 df1l = 2*C +2; df2l = 2*B;
 F1 = finv(0.975, df1l, df2l);
 tl = B/(B+(C+1)*F1 );
 orexactl = tl/(1-tl);

 df1u = 2*B +2; df2u = 2*C;
 F2 = finv(0.975, df1u, df2u);
 tu = (B+1)*F2/(C+(B+1)*F2 );
 orexactu = tu/(1-tu);
 exactorci = [orexactl, orexactu]

%--------- approximate prob ---------
phat = B/(B+C);
pl = phat - norminv(0.975)*sqrt(phat * (1-phat)/(B+C)) - 1/(2*B+2*C);
pu = phat + norminv(0.975)*sqrt(phat * (1-phat)/(B+C)) + 1/(2*B+2*C);
intor = [pl/(1-pl), pu/(1-pu)]