File Exchange

image thumbnail


version (16.3 KB) by Giuseppe Cardillo
the definitive function for the Fisher's exact and conditional test for any RxC matrix


Updated 07 Apr 2018

View Version History

GitHub view license on GitHub

Fisher's Exact Probability Test for a RxC matrix.
Fisher's exact test permits calculation of precise probabilities in situation where, as a consequence of small cell frequencies, the much more rapid normal approximation and chi-square calculations are liable to be inaccurate. The Fisher's exact test involves the computations of several factorials to obtain the probability of the observed and each of the more extreme tables. Factorials growth quickly, so it's necessary use logarithms of factorials. This computations is very easy in Matlab because: x!=gamma(x+1) and log(x!)=gammaln(x+1). Moreover, when the matrix has many Rows and Columns, the computation of all the set of possible matrices is very time expensive. This function uses this strategy:
if the input is a 2x2, 2x3, 2x4 or 3x3 matrix it uses (or download) ad hoc, previously written by me, function;

else it uses a Monte Carlo approach. Finally, this function uses the Peter J. Acklam rldecode function, and so I want to acknowledge him.

Created by Giuseppe Cardillo

To cite this file, this would be an appropriate format: Cardillo G. (2010) MyFisher: the definitive function for the Fisher's exact and conditional test for any RxC matrix

Cite As

Giuseppe Cardillo (2020). MyFisher (, GitHub. Retrieved .

Comments and Ratings (5)

Noritaka Yusa

I guess if-else sentences l.53-l.63 need modification to consider 2xN (N>=5) properly.


On the matrix
x=[6 4 0; 0 1 1; 11 1 3; 1 0 0]
all other programs I've tried (R included) give the answer of p=0.06045. This program was consistently producing values around 0.053, so I went to investigate.
Changing the line
if gpv<=op
if gpv<=op+eps
produced p-values more consistent with other programs. This is a classical floating-point computations issue.

Giuseppe Cardillo


Giuseppe Cardillo

Considering that chi square is an approximation of Fisher's test, in my opinion it is completely useless to have chi square p-value. Anyway, if you want, you can simply set up it using this code:
[r c]=size(x);

Ged Ridgway

Seems very useful! Might be worth (if not too much trouble) also returning the p-value from the approximate Chi-square test for comparison, since it seems that neither the Statistics toolbox nor fileexchange have this for general NxM tables.

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

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!