org.apache.commons.math3.distribution.HypergeometricDistribution Java Examples

The following examples show how to use org.apache.commons.math3.distribution.HypergeometricDistribution. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RandomDataGeneratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNextHypergeometric() {
    HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest();
    int[] densityPoints = testInstance.makeDensityTestPoints();
    double[] densityValues = testInstance.makeDensityTestValues();
    int sampleSize = 1000;
    int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
    HypergeometricDistribution distribution = (HypergeometricDistribution) testInstance.makeDistribution();
    double[] expectedCounts = new double[length];
    long[] observedCounts = new long[length];
    for (int i = 0; i < length; i++) {
        expectedCounts[i] = sampleSize * densityValues[i];
    }
    randomData.reSeed(1000);
    for (int i = 0; i < sampleSize; i++) {
      int value = randomData.nextHypergeometric(distribution.getPopulationSize(),
              distribution.getNumberOfSuccesses(), distribution.getSampleSize());
      for (int j = 0; j < length; j++) {
          if (value == densityPoints[j]) {
              observedCounts[j]++;
          }
      }
    }
    TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
}
 
Example #2
Source File: RandomDataTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNextHypergeometric() {
    HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest();
    int[] densityPoints = testInstance.makeDensityTestPoints();
    double[] densityValues = testInstance.makeDensityTestValues();
    int sampleSize = 1000;
    int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
    HypergeometricDistribution distribution = (HypergeometricDistribution) testInstance.makeDistribution();
    double[] expectedCounts = new double[length];
    long[] observedCounts = new long[length];
    for (int i = 0; i < length; i++) {
        expectedCounts[i] = sampleSize * densityValues[i];
    }
    randomData.reSeed(1000);
    for (int i = 0; i < sampleSize; i++) {
      int value = randomData.nextHypergeometric(distribution.getPopulationSize(),
              distribution.getNumberOfSuccesses(), distribution.getSampleSize());
      for (int j = 0; j < length; j++) {
          if (value == densityPoints[j]) {
              observedCounts[j]++;
          }
      }
    }
    TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
}
 
Example #3
Source File: RandomDataGeneratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNextHypergeometric() {
    HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest();
    int[] densityPoints = testInstance.makeDensityTestPoints();
    double[] densityValues = testInstance.makeDensityTestValues();
    int sampleSize = 1000;
    int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
    HypergeometricDistribution distribution = (HypergeometricDistribution) testInstance.makeDistribution();
    double[] expectedCounts = new double[length];
    long[] observedCounts = new long[length];
    for (int i = 0; i < length; i++) {
        expectedCounts[i] = sampleSize * densityValues[i];
    }
    randomData.reSeed(1000);
    for (int i = 0; i < sampleSize; i++) {
      int value = randomData.nextHypergeometric(distribution.getPopulationSize(),
              distribution.getNumberOfSuccesses(), distribution.getSampleSize());
      for (int j = 0; j < length; j++) {
          if (value == densityPoints[j]) {
              observedCounts[j]++;
          }
      }
    }
    TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
}
 
Example #4
Source File: RandomDataTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNextHypergeometric() {
    HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest();
    int[] densityPoints = testInstance.makeDensityTestPoints();
    double[] densityValues = testInstance.makeDensityTestValues();
    int sampleSize = 1000;
    int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
    HypergeometricDistribution distribution = (HypergeometricDistribution) testInstance.makeDistribution();
    double[] expectedCounts = new double[length];
    long[] observedCounts = new long[length];
    for (int i = 0; i < length; i++) {
        expectedCounts[i] = sampleSize * densityValues[i];
    }
    randomData.reSeed(1000);
    for (int i = 0; i < sampleSize; i++) {
      int value = randomData.nextHypergeometric(distribution.getPopulationSize(),
              distribution.getNumberOfSuccesses(), distribution.getSampleSize());
      for (int j = 0; j < length; j++) {
          if (value == densityPoints[j]) {
              observedCounts[j]++;
          }
      }
    }
    TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
}
 
Example #5
Source File: RandomDataGeneratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNextHypergeometric() {
    HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest();
    int[] densityPoints = testInstance.makeDensityTestPoints();
    double[] densityValues = testInstance.makeDensityTestValues();
    int sampleSize = 1000;
    int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
    HypergeometricDistribution distribution = (HypergeometricDistribution) testInstance.makeDistribution();
    double[] expectedCounts = new double[length];
    long[] observedCounts = new long[length];
    for (int i = 0; i < length; i++) {
        expectedCounts[i] = sampleSize * densityValues[i];
    }
    randomData.reSeed(1000);
    for (int i = 0; i < sampleSize; i++) {
      int value = randomData.nextHypergeometric(distribution.getPopulationSize(),
              distribution.getNumberOfSuccesses(), distribution.getSampleSize());
      for (int j = 0; j < length; j++) {
          if (value == densityPoints[j]) {
              observedCounts[j]++;
          }
      }
    }
    TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
}
 
Example #6
Source File: FisherExact.java    From VarDictJava with MIT License 6 votes vote down vote up
private List<Double> logdcDhyper(int m, int n, int k) {
    List<Double> logdc = new ArrayList<>();

    for (int element : support) {
        if (m + n == 0) {
            logdc.add(0.0);
            continue;
        }
        // m + n - total number of successes, m - number of successes (reference) k - sample size (forward)
        HypergeometricDistribution dhyper = new HypergeometricDistribution(m + n, m, k);
        Double value = dhyper.logProbability(element);
        if (value.isNaN()) {
            value = 0.0;
        }
        logdc.add(roundHalfEven("0.0000000", value));
    }
    return logdc;
}
 
Example #7
Source File: RandomDataTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNextHypergeometric() throws Exception {
    HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest();
    int[] densityPoints = testInstance.makeDensityTestPoints();
    double[] densityValues = testInstance.makeDensityTestValues();
    int sampleSize = 1000;
    int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
    HypergeometricDistribution distribution = (HypergeometricDistribution) testInstance.makeDistribution();
    double[] expectedCounts = new double[length];
    long[] observedCounts = new long[length];
    for (int i = 0; i < length; i++) {
        expectedCounts[i] = sampleSize * densityValues[i];
    }
    randomData.reSeed(1000);
    for (int i = 0; i < sampleSize; i++) {
      int value = randomData.nextHypergeometric(distribution.getPopulationSize(),
              distribution.getNumberOfSuccesses(), distribution.getSampleSize());
      for (int j = 0; j < length; j++) {
          if (value == densityPoints[j]) {
              observedCounts[j]++;
          }
      }
    }
    TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
}
 
Example #8
Source File: RandomDataGeneratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNextHypergeometric() {
    HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest();
    int[] densityPoints = testInstance.makeDensityTestPoints();
    double[] densityValues = testInstance.makeDensityTestValues();
    int sampleSize = 1000;
    int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
    HypergeometricDistribution distribution = (HypergeometricDistribution) testInstance.makeDistribution();
    double[] expectedCounts = new double[length];
    long[] observedCounts = new long[length];
    for (int i = 0; i < length; i++) {
        expectedCounts[i] = sampleSize * densityValues[i];
    }
    randomData.reSeed(1000);
    for (int i = 0; i < sampleSize; i++) {
      int value = randomData.nextHypergeometric(distribution.getPopulationSize(),
              distribution.getNumberOfSuccesses(), distribution.getSampleSize());
      for (int j = 0; j < length; j++) {
          if (value == densityPoints[j]) {
              observedCounts[j]++;
          }
      }
    }
    TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
}
 
Example #9
Source File: RandomDataGeneratorTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNextHypergeometric() {
    HypergeometricDistributionTest testInstance = new HypergeometricDistributionTest();
    int[] densityPoints = testInstance.makeDensityTestPoints();
    double[] densityValues = testInstance.makeDensityTestValues();
    int sampleSize = 1000;
    int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
    HypergeometricDistribution distribution = (HypergeometricDistribution) testInstance.makeDistribution();
    double[] expectedCounts = new double[length];
    long[] observedCounts = new long[length];
    for (int i = 0; i < length; i++) {
        expectedCounts[i] = sampleSize * densityValues[i];
    }
    randomData.reSeed(1000);
    for (int i = 0; i < sampleSize; i++) {
      int value = randomData.nextHypergeometric(distribution.getPopulationSize(),
              distribution.getNumberOfSuccesses(), distribution.getSampleSize());
      for (int j = 0; j < length; j++) {
          if (value == densityPoints[j]) {
              observedCounts[j]++;
          }
      }
    }
    TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
}
 
Example #10
Source File: FisherExactTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * Computes the 2-sided pvalue of the Fisher's exact test on a normalized table that ensures that the sum of
 * all four entries is less than 2 * 200.
 */
public static double twoSidedPValue(final int[][] normalizedTable) {
    Utils.nonNull(normalizedTable);
    Utils.validateArg(normalizedTable.length == 2, () -> "input must be 2x2 " + Arrays.deepToString(normalizedTable));
    Utils.validateArg(normalizedTable[0] != null && normalizedTable[0].length == 2, () -> "input must be 2x2 " + Arrays.deepToString(normalizedTable));
    Utils.validateArg(normalizedTable[1] != null && normalizedTable[1].length == 2, () -> "input must be 2x2 " + Arrays.deepToString(normalizedTable));

    //Note: this implementation follows the one in R base package
    final int[][] x= normalizedTable;
    final int m = x[0][0] + x[0][1];
    final int n = x[1][0] + x[1][1];
    final int k = x[0][0] + x[1][0];
    final int lo = Math.max(0, k - n);
    final int hi = Math.min(k, m);
    final IndexRange support = new IndexRange(lo, hi + 1);

    if (support.size() <= 1){     //special case, support has only one value
        return 1.0;
    }

    final AbstractIntegerDistribution dist = new HypergeometricDistribution(null, m+n, m, k);
    final double[] logds = support.mapToDouble(dist::logProbability);
    final double threshold = logds[x[0][0] - lo] * REL_ERR;
    final double[] log10ds = DoubleStream.of(logds).filter(d -> d <= threshold).map(MathUtils::logToLog10).toArray();
    final double pValue = MathUtils.sumLog10(log10ds);

    // min is necessary as numerical precision can result in pValue being slightly greater than 1.0
    return Math.min(pValue, 1.0);
}
 
Example #11
Source File: HyperGeometricAnalyzer.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
public List<AnalyzerResult> analyze(AnalyzeRequest request) {
  List<AnalyzerResult> pValues = new ArrayList<>();
  try (Transaction tx = graphDb.beginTx()) {
    AnalyzeRequest processedRequest = processRequest(request);

    Set<AnalyzerInnerNode> sampleSetNodes = getSampleSetNodes(processedRequest);

    Set<AnalyzerInnerNode> completeSetNodes = getCompleteSetNodes(processedRequest);

    double bonferroniCoeff = computeBonferroniCoeff(completeSetNodes);

    int totalCount = getTotalCount(processedRequest.getOntologyClass());

    // apply the HyperGeometricDistribution for each node
    for (AnalyzerInnerNode n : sampleSetNodes) {
      HypergeometricDistribution hypergeometricDistribution =
          new HypergeometricDistribution(totalCount, (int) getCountFrom(completeSetNodes,
              n.getNodeId()), processedRequest.getSamples().size());
      double p =
          hypergeometricDistribution.upperCumulativeProbability((int) n.getCount())
              * bonferroniCoeff;
      String iri = graph.getNodeProperty(n.getNodeId(), CommonProperties.IRI, String.class).get();
      String curie = curieUtil.getCurie(iri).orElse(iri);
      String labels =
          StringUtils.join(
              graph.getNodeProperties(n.getNodeId(), NodeProperties.LABEL, String.class), ", ");
      pValues.add(new AnalyzerResult(labels, curie, p));
    }

    // sort by p-value
    Collections.sort(pValues, new AnalyzerResultComparator());

    tx.success();
  } catch (Exception e) {
    e.printStackTrace();
  }
  return pValues;
}
 
Example #12
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generates a random value from the {@link HypergeometricDistribution Hypergeometric Distribution}.
 * This implementation uses {@link #nextInversionDeviate(IntegerDistribution) inversion}
 * to generate random values.
 *
 * @param populationSize the population size of the Hypergeometric distribution
 * @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution
 * @param sampleSize the sample size of the Hypergeometric distribution
 * @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution
 * @since 2.2
 */
public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) {
    return nextInversionDeviate(new HypergeometricDistribution(populationSize, numberOfSuccesses, sampleSize));
}
 
Example #13
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generates a random value from the {@link HypergeometricDistribution Hypergeometric Distribution}.
 *
 * @param populationSize the population size of the Hypergeometric distribution
 * @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution
 * @param sampleSize the sample size of the Hypergeometric distribution
 * @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution
 * @throws NumberIsTooLargeException  if {@code numberOfSuccesses > populationSize},
 * or {@code sampleSize > populationSize}.
 * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
 * @throws NotPositiveException  if {@code numberOfSuccesses < 0}.
 */
public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException {
    return new HypergeometricDistribution(getRandomGenerator(),populationSize,
            numberOfSuccesses, sampleSize).sample();
}
 
Example #14
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generates a random value from the {@link HypergeometricDistribution Hypergeometric Distribution}.
 *
 * @param populationSize the population size of the Hypergeometric distribution
 * @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution
 * @param sampleSize the sample size of the Hypergeometric distribution
 * @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution
 * @throws NumberIsTooLargeException  if {@code numberOfSuccesses > populationSize},
 * or {@code sampleSize > populationSize}.
 * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
 * @throws NotPositiveException  if {@code numberOfSuccesses < 0}.
 */
public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException {
    return new HypergeometricDistribution(getRandomGenerator(),populationSize,
            numberOfSuccesses, sampleSize).sample();
}
 
Example #15
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generates a random value from the {@link HypergeometricDistribution Hypergeometric Distribution}.
 *
 * @param populationSize the population size of the Hypergeometric distribution
 * @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution
 * @param sampleSize the sample size of the Hypergeometric distribution
 * @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution
 * @throws NumberIsTooLargeException  if {@code numberOfSuccesses > populationSize},
 * or {@code sampleSize > populationSize}.
 * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
 * @throws NotPositiveException  if {@code numberOfSuccesses < 0}.
 */
public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException {
    return new HypergeometricDistribution(getRandomGenerator(),populationSize,
            numberOfSuccesses, sampleSize).sample();
}
 
Example #16
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generates a random value from the {@link HypergeometricDistribution Hypergeometric Distribution}.
 *
 * @param populationSize the population size of the Hypergeometric distribution
 * @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution
 * @param sampleSize the sample size of the Hypergeometric distribution
 * @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution
 * @throws NumberIsTooLargeException  if {@code numberOfSuccesses > populationSize},
 * or {@code sampleSize > populationSize}.
 * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
 * @throws NotPositiveException  if {@code numberOfSuccesses < 0}.
 */
public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException {
    return new HypergeometricDistribution(getRandomGenerator(),populationSize,
            numberOfSuccesses, sampleSize).sample();
}
 
Example #17
Source File: RandomDataImpl.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generates a random value from the {@link HypergeometricDistribution Hypergeometric Distribution}.
 * This implementation uses {@link #nextInversionDeviate(IntegerDistribution) inversion}
 * to generate random values.
 *
 * @param populationSize the population size of the Hypergeometric distribution
 * @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution
 * @param sampleSize the sample size of the Hypergeometric distribution
 * @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution
 * @since 2.2
 */
public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) {
    return nextInversionDeviate(new HypergeometricDistribution(populationSize, numberOfSuccesses, sampleSize));
}
 
Example #18
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generates a random value from the {@link HypergeometricDistribution Hypergeometric Distribution}.
 *
 * @param populationSize the population size of the Hypergeometric distribution
 * @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution
 * @param sampleSize the sample size of the Hypergeometric distribution
 * @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution
 * @throws NumberIsTooLargeException  if {@code numberOfSuccesses > populationSize},
 * or {@code sampleSize > populationSize}.
 * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
 * @throws NotPositiveException  if {@code numberOfSuccesses < 0}.
 */
public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException {
    return new HypergeometricDistribution(getRan(),populationSize,
            numberOfSuccesses, sampleSize).sample();
}
 
Example #19
Source File: RandomDataGenerator.java    From astor with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Generates a random value from the {@link HypergeometricDistribution Hypergeometric Distribution}.
 *
 * @param populationSize the population size of the Hypergeometric distribution
 * @param numberOfSuccesses number of successes in the population of the Hypergeometric distribution
 * @param sampleSize the sample size of the Hypergeometric distribution
 * @return random value sampled from the Hypergeometric(numberOfSuccesses, sampleSize) distribution
 * @throws NumberIsTooLargeException  if {@code numberOfSuccesses > populationSize},
 * or {@code sampleSize > populationSize}.
 * @throws NotStrictlyPositiveException if {@code populationSize <= 0}.
 * @throws NotPositiveException  if {@code numberOfSuccesses < 0}.
 */
public int nextHypergeometric(int populationSize, int numberOfSuccesses, int sampleSize) throws NotPositiveException, NotStrictlyPositiveException, NumberIsTooLargeException {
    return new HypergeometricDistribution(getRandomGenerator(),populationSize,
            numberOfSuccesses, sampleSize).sample();
}