macro hypergeometric x A B n PxABn #This macro calculates the hypergeometric distribution #h(x;A,B,n). The funny order of the different terms is #necessary to avoid overflow errors in the factorials. #Example call: # mtb> %hypergeometric 2 6 24 5 k1 mconstant x A B n PxABn AplusB i nminusx let AplusB=A+B if (x<0) or (x>n) or (x>A) or (n<0) or (n>AplusB) #invalid cases let PxABn=0 elseif (x=0) #special case x=0 let PxABn=1 do i=1:n let PxABn=(B+1-i)/(A+B+1-i)*PxABn enddo else #x>0 let PxABn=1 do i=1:x let PxABn=(A+1-i)/(x+1-i)/(A+B+1-i)*(n+1-i)*PxABn enddo let nminusx=n-x do i=1:nminusx let PxABn=(B+1-i)/(A+B-x+1-i)*PxABn enddo endif print PxABn endmacro