%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% fisher-mac.tex. Version: November 13, 2003.
% Author: Anthony PHAN.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% beginning of macros definitions
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\catcode`\@=11
%
% Language dependent
%
\def\iffrench#1#2{\ifnum\language=1#1\else#2\fi}%
%
% integer variables
%
\newcount\count@amin
\newcount\count@amax
\newcount\count@rone
\newcount\count@rtwo
\newcount\count@cone
\newcount\count@ctwo
\newcount\count@a
\newcount\count@b
\newcount\count@c
\newcount\count@d
\newcount\count@n
\newcount\count@i
%
% ``decimal'' variables
%
\newdimen\num@P
\newdimen\num@Pcutoff
\newdimen\num@Pcurrent
%
% Tricky stuff for printing a dimension register without ``pt''
%
{\catcode`\p=12\catcode`\t=12\gdef\gobbledimen#1pt{#1}}
\def\dimentonumber{\expandafter\gobbledimen\the}%
%
% Auxiliary control sequence
%
\def\@fisherproba#1#2{\count@=#1%
	\loop
	\ifnum\count@<#2%
		\ifnum\count@i<\count@n
			\advance\count@i by 1%
			\divide\num@Pcurrent by\count@i
		\advance\count@ by 1%
		\multiply\num@Pcurrent by \count@
		\fi
	\repeat}
%
% Compute k!(n-k)!l!(n-l)!/(n!a!b!c!d!).
% The corresponding counters must be known.
%
\def\fisherproba{{\num@Pcurrent=1pt
	\count@i=0
	\@fisherproba\count@b\count@rone
	\@fisherproba\count@d\count@ctwo
	\@fisherproba\count@a\count@cone
	\@fisherproba\count@c\count@rtwo
	\global\num@Pcurrent=\num@Pcurrent}}
%
% The control sequence
%
\def\fisher#1#2#3#4{{%
	\bigbreak
	\centerline{\sc\iffrench{Test exact de Fisher}{Fisher's Exact Test}}
	%
	% initialization of the sums
	%
	\count@rone=#1\advance\count@rone by#2
	\count@rtwo=#3\advance\count@rtwo by#4
	\count@cone=#1\advance\count@cone by#3
	\count@ctwo=#2\advance\count@ctwo by#4
	\count@n=\count@rone\advance\count@n by\count@rtwo
	%
	% Original table and
	% Expected table: | kl/n      (n-k)l/n     |
	%                 |                        |
	%                 | k(n-l)/n  (n-k)(n-l)/n |
	%
	\bigbreak
	\noindent
	{\it\iffrench{Tables originale et attendue~:}%
	{Original and Expected Tables:}}%
	\count@a=#1\count@b=#2\count@c=#3\count@d=#4
	\fisherproba\num@Pcutoff=\num@Pcurrent
	$$
	\left(\null\,\vcenter{\normalbaselines\m@th
		\ialign{\strut
		\hfil$##$\quad&\hfil$##$\enspace\vrule\enspace&\hfil$##$\cr
		#1&#2&\the\count@rone\cr
		\dimen@=\count@cone pt
		\multiply\dimen@ by\count@rone\divide\dimen@ by\count@n
		\dimentonumber\dimen@
		&
		\dimen@=\count@ctwo pt
		\multiply\dimen@ by\count@rone\divide\dimen@ by\count@n
		\dimentonumber\dimen@
		&\cr
		#3&#4&\the\count@rtwo\cr
		\dimen@=\count@cone pt
		\multiply\dimen@ by\count@rtwo\divide\dimen@ by\count@n
		\dimentonumber\dimen@
		&
		\dimen@=\count@ctwo pt
		\multiply\dimen@ by\count@rtwo\divide\dimen@ by\count@n
		\dimentonumber\dimen@
		&\cr
		\noalign{\hrule}
		\the\count@cone&\the\count@ctwo&\the\count@n\cr}}\,\right)
	\qquad
	\hbox{\iffrench{Probabilit\'e de coupure~: }{Cutoff Probability: }%
	\dimentonumber\num@Pcutoff.}
	$$
	\bigbreak
	\noindent
	{\it\iffrench{Tables de m\^emes sommes par lignes et colonnes
	et leurs probabilit\'es~:}%
	{Tables with same sums by rows and columns
	and their Probabilities:}}%
	%
	% initialization of the minimum value of a
	%
	\count@=\count@cone\advance\count@ by-\count@rtwo
	\ifnum\count@>0\count@amin=\count@\else\count@=0\fi
	%
	% initialization of the maximum value of a
	%
	\ifnum\count@rone<\count@cone\count@amax=\count@rone
	\else\count@amax=\count@cone\fi
	\count@=\count@amin\advance\count@ by -1
	%
	% list all compatible matrices
	%
	\num@P=0pt
	\loop\ifnum\count@<\count@amax\advance\count@ by 1
	\count@a=\count@
	\count@b=\count@rone\advance\count@b by -\count@
	\count@c=\count@cone\advance\count@c by -\count@
	\count@d=\count@ctwo\advance\count@d by -\count@rone
	\advance\count@d by\count@
	\medbreak\noindent
	$\displaystyle
	\left(\matrix{\the\count@a&\the\count@b\cr
	\the\count@c&\the\count@d}\right)$
	\fisherproba
	\iffrench{probabilit\'e~: }{probability: }%
	\dimentonumber\num@Pcurrent
	\ifnum\count@<\count@amax
	\iffrench{\,;}{;}\else.\fi
	\ifdim\num@Pcurrent>\num@Pcutoff\else
	\global\advance\num@P by \num@Pcurrent\fi
	\repeat
	%
	% p-value
	%
	\bigbreak\noindent
	{\it\iffrench{$p$-valeur bilat\'erale~:}{Two-sided $p$-value:}}\/
	\dimentonumber\num@P.}}
\catcode`\@=12
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% end of macros definitions
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput
