Hypergeometric { public double phypergeometric(int n, double p, int k, int r) { if (k >= n) return 1.0; if (r < 1) { return 1.0; } double q = 1.0 - p; int np = (int) Math.round(n * p); int nq = (int) Math.round(n * q); double log_n_choose_k = lNchooseK(n, k); int top = k; if (np < k) { top = np; } double lfoo = lNchooseK(np, top) + lNchooseK(nq, k - top); double sum = 0.0; for (int i = top; i >= r; --i) { sum += Math.exp(lfoo - log_n_choose_k); if (i > r) { lfoo = lfoo + Math.log((double) i / (double) (np - i + 1)) + Math.log((double) (nq - k + i) / (double) (k - i + 1)); } } return sum; } Hypergeometric(); double phypergeometric(int n, double p, int k, int r); double phypergeometric(int m, int m_t, int n, int n_t); double dhyper(int x, int N, int M, int n); double phyper(int x, int N, int M, int n, boolean lowerTail); double lNchooseK(int n, int k); double logfact(int i);  }
@Test void test_equality_phypergeometric_overloaded() { int N = 60; int n = 13; int n_t = 2; int m_t = 5; double r1 = hypergeometric.phypergeometric(N, (double)m_t/N, n, n_t); double r2 = hypergeometric.phypergeometric(N, m_t, n, n_t); assertEquals(r1, r2, EPSILON); }
@Test void compareTermForTerm1() { int m = 13668; int m_t = 184; int n = 1757; int n_t = 59; double expected = 3.81343996188218E-11; assertEquals(expected, hypergeometric.phypergeometric(m, m_t, n, n_t), EPSILON); }
@Test void compareTermForTerm2() { int m = 13668; int m_t = 46; int n = 1757; int n_t = 18; double expected = 1.170702787001301E-5; assertEquals(expected, hypergeometric.phypergeometric(m, m_t, n, n_t), EPSILON); }
@Test void compareTermForTerm3() { int m = 13668; int m_t = 360; int n = 1757; int n_t = 82; double expected = 5.610055205824117E-7; assertEquals(expected, hypergeometric.phypergeometric(m, m_t, n, n_t), EPSILON); }
@Test void compareTermForTerm4() { int m = 13668; int m_t = 70; int n = 1757; int n_t = 19; double expected = 0.001022299; assertEquals(expected, hypergeometric.phypergeometric(m, m_t, n, n_t), EPSILON); }
@Test void compareTermForTerm5() { int m = 13668; int m_t = 19; int n = 1757; int n_t = 8; double expected = 0.001488453; assertEquals(expected, hypergeometric.phypergeometric(m, m_t, n, n_t), EPSILON); }
@Test void compareParentChildIntersection1() { int m = 1366; int m_t = 344; int n = 116; int n_t = 13; int m_pa_t = 5651; int n_pa_t = 60; double proportion = (double) m_t/m_pa_t; double result = hypergeometric.phypergeometric(m_pa_t, m_t, n_pa_t, n_t); double expected = 4.7583683370433105E-5; System.out.printf("%e", result); assertEquals(expected,result, EPSILON); }