File Exchange

image thumbnail


version (3.88 KB) by Antonio Trujillo-Ortiz
Fisher's Exact Probability Test.


Updated 22 Dec 2005

View License

This file performs the Fisher exact probability test for a table of frequency data cross-classified according to two categorical variables, each of which has two levels or subcategories (2x2). It is a non-parametric statistical test used to determine if there are nonrandom associations between the two categorical variables. Fisher's exact test is used to calculate an exact P-value with small number of expected frequencies, for which the Chi-square test is not appropriate (in case the total number of observations is less than 20 or the number of frequency cells are less than 5). This test is based upon the hypergeometric probability. The test was proposed in the 1934 edition of the famous Ronald Aylmer Fisher's book 'Statistical Methods for Research Workers'.

It needs to input the four frequency cells.

The output consists of the negative, positive and both P-values. [decide to use Left, Right or 2-Tail before collecting (or looking at) the data].

If user does not put any output arguments, file returns a P-value table.

Cite As

Antonio Trujillo-Ortiz (2020). Fisherextest (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (18)


Again, there seems to be a problem with one sided tests:

Fisher's exact test P-values for cells:
a = 10, b = 1, c = 1, d = 10
Left tail Right tail 2-tail
(negative) (positive) (both)
0.0001729437 0.9999985824 0.0003458873
>> Fisherextest(1,10,10,1)

Fisher's exact test P-values for cells:
a = 1, b = 10, c = 10, d = 1
Left tail Right tail 2-tail
(negative) (positive) (both)
0.0001729437 0.9999985824 0.0003458873


I think this function (or rather the help section at the beginning of the function) switches Ppos and Pneg. They are switched around.


I second Trevor Agus' 1st comment. What's up with the 1-sided tests?!

Trevor Agus

Sorry, I've spotted a school-boy error in that second test -- the proportionSignicant were closer to p = 0.05 when I corrected it. I'd still be interested to be corrected on my first post. (And apologies for having a conversation with myself on the comments board.)

Trevor Agus

I've just tested this program with some random numbers, on the assumption that it should give significant (p<.05) results just on 1/20 of the trials. I get significant results on 1/3 of trials. Unless someone corrects me, I'm going to assume that this code does not do what it claims to do.

Here's my test code, in one ugly line:
counter=0; N=10000; for ii=1:N; [ppos,pneg,either]=Fisherextest(ceil(rand*20),ceil(rand*20),ceil(rand*20),ceil(rand*20)); if either<=.05; counter=counter+1; end; end; proportionSignificant=counter/N

Trevor Agus

Am I misunderstanding something, or is there a serious bug in the 1-tailed tests? The following two lines give the same outputs:
In fact, ppos seems to be always greater or equal to pneg, whereas it should be possible to have significant 1-sided results on both sides.

do you have it for 4x2 contingency tables? that would be great!

Ji Cling

Michael, dispite your regular visits the 10 000 self dowloads continue.

(It is not an exagerated figure)

Michael Hamman

My Dear Ji Cling,

Regularly I visit this FEX site in which excellent contributions for the fast solution of diverse types of problems in several disciplines have been given. The accusation that you are causing is very delicate. I do not know you. Neither the authors. We known them only by the references given in their author page. Only what I believe is in the quality of ethics of each one of them and ours. We are serious people dedicated to our work in the most diverse specialties. To be certain what you say. Then not alone this author does cheating but also all the others. Even you, that in principle, I think you are not signing with your true name. Finally, or really the community are honestly dawnloaded the m-files or finally someone are doing a very bad play. This because there exists a lack of control on this.

Best Wishes.


Ji Cling

Shame on you.
Self downloading 1000ths of times to get to the top of the rank.

gideon dror

Very usefull function.
Coding is not "Matlab elegant" - but performs very well.

yair weiss

function can be considerably optimized

James J. Cai

Dorian Arnold

Works very well for simple statistics. Great for grant writers using prelim data or students doing coursework dealing with small sample sizes.

Claudia Lerma

Antonio Fortes

Works very well even for large (>1000) sample sizes.

Stan Letovsky

The authors have modified their code to address correctness and implementation issues, so my previous review is obsolete and should be ignored.

Stan Letovsky

I tested this program and found it to be buggy, returning probs > 1 for various inputs, particularly but not exclusively when one of the cell counts was 0. Also comparing results to an interactive version of Fisher on the web there were significant discrepancies. The implementation also should return values, not print out a table. The statistical toolbox needs a reliable implementation of this test.


Input was improved.

Text was improved.

It was implemented output arguments.

P-value results were improved.

File was improved.

File was updated to work for very large cells value.

Text was improved.

Help text was improved.

Due that Matlab could not work for factorials greater than 170. We use the function sum([log(x+1)......]), in order to avoid further calculation problems.

Output was improved.

It was added an appropriate format to cite this file.

MATLAB Release Compatibility
Created with R11
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired: HWEtest, MyFisher22