Package uk.ac.ebi.utils.statistics
Class FisherExact
java.lang.Object
uk.ac.ebi.utils.statistics.FisherExact
Class for Fisher Exact test.
This was copied and adapted from here, (commit 71b68de, 2023/07/17), since the original class is part of a bigger project concerning a different specific application domain.
TODO: It would be better to have a cached object that depends on maxSize as cache key, ie,
when a requested maxSize is < cached maxSize, the existing instance is returned,
else a new instance is created that replaces the old one. This also requires that such
instance has an expiration time, a cache that evicts automatically and an ability to initialise
a FisherExact
with a bigger size from one with a smaller one. At that point, we
would prefer static wrapper methods for methods like getP(int, int, int, int)
.
Original comments
This does a Fisher Exact test. The Fisher's Exact test procedure calculates an exact probability value for the relationship between two dichotomous variables, as found in a two by two crosstable. The program calculates the difference between the data observed and the data expected, considering the given marginal and the assumptions of the model of independence. It works in exactly the same way as the Chi-square test for independence; however, the Chi-square gives only an estimate of the true probability value, an estimate which might not be very accurate if the marginal is very uneven or if there is a small value (less than five) in one of the cells. It uses an array of factorials initialized at the beginning to provide speed. There could be better ways to do this.- Version:
- $Id: FisherExact.java,v 1
- Author:
- Ed Buckler
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfinal double
getCumlativeP
(int a, int b, int c, int d) Calculates the one-tail P-value for the Fisher Exact test.final double
getLeftTailedP
(int a, int b, int c, int d) Calculates the left-tail P-value for the Fisher Exact test.static double
getOddsRatio
(double a, double b, double c, double d) Measure of how far from independence the 2x2 table is. 1= independent.static double[]
getOddsRatioAnd95thConfidenceInterval
(double a, double b, double c, double d) Returns OR, lower, upperfinal double
getP
(int a, int b, int c, int d) calculates the P-value for this specific statefinal double
getRightTailedP
(int a, int b, int c, int d) Calculates the right-tail P-value for the Fisher Exact test.final double
getTwoTailedP
(int a, int b, int c, int d) Calculates the two-tailed P-value for the Fisher Exact test.
-
Constructor Details
-
FisherExact
public FisherExact(int maxSize) constructor for FisherExact table- Parameters:
maxSize
- is the maximum sum that will be encountered by the table (a+b+c+d)
-
-
Method Details
-
getP
public final double getP(int a, int b, int c, int d) calculates the P-value for this specific state- Parameters:
a
- a, b, c, d are the four cells in a 2x2 matrixb
-c
-d
-- Returns:
- the P-value
-
getCumlativeP
public final double getCumlativeP(int a, int b, int c, int d) Calculates the one-tail P-value for the Fisher Exact test. Determines whether to calculate the right- or left- tail, thereby always returning the smallest p-value. a, b, c, d are the four cells in a 2x2 matrix- Returns:
- one-tailed P-value (right or left, whichever is smallest)
-
getRightTailedP
public final double getRightTailedP(int a, int b, int c, int d) Calculates the right-tail P-value for the Fisher Exact test. a, b, c, d are the four cells in a 2x2 matrix- Returns:
- one-tailed P-value (right-tail)
-
getLeftTailedP
public final double getLeftTailedP(int a, int b, int c, int d) Calculates the left-tail P-value for the Fisher Exact test. a, b, c, d are the four cells in a 2x2 matrix- Returns:
- one-tailed P-value (left-tail)
-
getTwoTailedP
public final double getTwoTailedP(int a, int b, int c, int d) Calculates the two-tailed P-value for the Fisher Exact test. In order for a table under consideration to have its p-value included in the final result, it must have a p-value less than the original table's P-value, i.e. Fisher's exact test computes the probability, given the observed marginal frequencies, of obtaining exactly the frequencies observed and any configuration more extreme. By "more extreme," we mean any configuration (given observed marginals) with a smaller probability of occurrence in the same direction (one-tailed) or in both directions (two-tailed). a, b, c, d are the four cells in a 2x2 matrix- Returns:
- two-tailed P-value or NaN if the table sum exceeds the maxSize
-
getOddsRatioAnd95thConfidenceInterval
public static double[] getOddsRatioAnd95thConfidenceInterval(double a, double b, double c, double d) Returns OR, lower, upper -
getOddsRatio
public static double getOddsRatio(double a, double b, double c, double d) Measure of how far from independence the 2x2 table is. 1= independent. Ratio of ratios. Will return Infinity or 0 if cells are zero.
-