Subject: Fisher's exact test
From: [email-masked]
To: [email-masked]
Date: Tue, 27 Jul 2010 03:59:43 -0400

Hi John,
 
I recently saw an exchange involving you about Fisher's exact test in the Boost Developer's Archive 
http://groups.google.co.uk/group/boost-developers-archive/browse_thread/thread/4a18cf4abf7228e7/4fb04c57f4c77912
http://en.wikipedia.org/wiki/Fisher's_exact_test gives a reasonable description of what Fisher's exact test is about. The sentence "Unfortunately, classification of the tables according to whether or not they are 'as extreme' is problematic" explains why ther is no definitive version of Fisher's exact test.
 
The following is some VBA code for the two-sided 2x2 FET based on Pearson's Chi-Square statistic (i.e. includes all tables which give a value of the Chi-Square statistic which is greater than or equal to that of the table observed)

Public Function Fishers_exact_test(a As Double, b As Double, c As 
Double, d As Double) As Double 
Dim det As Double, temp As Double, sample_size As Double, pop As 
Double 
det = a * d - b * c 
If det > 0 Then 
temp = a 
a = b 
b = temp 
temp = c 
c = d 
d = temp 
det = -det 
End If 
sample_size = a + b 
temp = a + c 
pop = sample_size + c + d 
det = (2 * det + 1) / pop 
If det < -1# Then 
Fishers_exact_test = cdf_hypergeometric(a, sample_size, temp, pop) + 
comp_cdf_hypergeometric(a - det, sample_size, temp, pop) 
Else 
Fishers_exact_test = 1# 
End If 
End Function 
 
Here comp_cdf_hypergeometric(a - det, sample_size, temp, pop) calculates 1-cdf_hypergeometric(a - det, sample_size, temp, pop) avoiding large cancellation errors which can arise from performing the 1- calculation.
 
I also have code for calculating the probability mass, cdf and comp_cdf functions for the hypergeometric distribution which both accurate and quick for large values. You would be most welcome to use any or all of it or just to use the algorithms on which the code is based and code it up yourself.
 
Ian Smith