/*
   File:        hypergeo.h
   Authors:     Wong / Moore / Schneider
   Created:     Mon Oct 22 04:03:02 EDT 2001
   Description: Header file for hypergeometric distribution functions

   Copyright 2001, The Auton Lab
*/

#ifndef _HYPERGEO_H
#define _HYPERGEO_H

typedef enum hg_pvaltype
{
  hg_one_sided_decrease,
  hg_one_sided_increase,
  hg_two_sided
} hg_pvaltype;

/*
  The hypergeometric series is similar to having a box with A red balls
  and B blue balls.  Choose k balls at random without replacement.  Let X be 
  the number of red balls.  Then the probability of getting x red and k-x 
  blues is:
  f(x) = (A choose x)(B choose k-x)
         --------------------------
         (A+B choose k)
 */
double hypergeometric_pdf(int x, int k, int A, int B);
double hypergeometric_pdf2(int x, int k, int A, int B);
double old_hypergeometric_pval(int x, int k, int A, int B, 
			       hg_pvaltype pvaltype);
double hypergeometric_pval(int x, int k, int A, int B, hg_pvaltype pvaltype);

/* Calculates if the hypergeometric pvalue is lower than comp_pval and does 
   early cutoff if the partial calculation is lower than comp_pval */
bool hypergeometric_is_pval_lower(int x, int k, int A, int B, 
				  hg_pvaltype pvaltype,
				  double comp_pval);
bool old_hypergeometric_is_pval_lower(int x, int k, int A, int B, 
				      hg_pvaltype pvaltype,
				      double comp_pval);
/* 
   Performs fisher's exact test.  This was implemented for the WSARE 
   project hence the today and other naming convention.  Basically the
   following function performs fisher's exact test for the following 2x2
   contingency table:

             | Today          | Other                |
   ---------------------------------------------------
   Match     | match_today    | match_other          |
   ---------------------------------------------------
   No Match  | no_match_today | no_match_other       |
   ---------------------------------------------------
             | num_today      | num_other            |

   It compares how extreme the counts of things matching/not matching
   today is if we assume they were drawn from the "other" distribution.
 */
double fisher_exact_test_pvalue(int num_match_today, int num_today,
				int num_match_other, int num_other,
				hg_pvaltype pvaltype);

/* This is the fast version that stops p-value calculation early if
   it is already lower */
bool fisher_exact_test_pvalue_is_lower(int num_match_today, int num_today,
				       int num_match_other, int num_other,
				       hg_pvaltype pvaltype,
				       double comp_pval);

#endif

