本文整理汇总了Java中org.apache.commons.math3.distribution.HypergeometricDistribution类的典型用法代码示例。如果您正苦于以下问题:Java HypergeometricDistribution类的具体用法?Java HypergeometricDistribution怎么用?Java HypergeometricDistribution使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HypergeometricDistribution类属于org.apache.commons.math3.distribution包,在下文中一共展示了HypergeometricDistribution类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testNextHypergeometric
import org.apache.commons.math3.distribution.HypergeometricDistribution; //导入依赖的package包/类
@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);
}
示例2: getHypergeometric
import org.apache.commons.math3.distribution.HypergeometricDistribution; //导入依赖的package包/类
@Override
public RandomNumberDistribution<Integer> getHypergeometric(
final RandomNumberStream rng, final Number populationSize,
final Number numberOfSuccesses, final Number sampleSize)
{
final IntegerDistribution dist = new HypergeometricDistribution(
RandomNumberStream.Util.asCommonsRandomGenerator(rng),
populationSize.intValue(), numberOfSuccesses.intValue(),
sampleSize.intValue());
return new RandomNumberDistribution<Integer>()
{
@Override
public Integer draw()
{
return dist.sample();
}
};
}
示例3: hypergeometric
import org.apache.commons.math3.distribution.HypergeometricDistribution; //导入依赖的package包/类
public static double hypergeometric(int numberOfGenesTotal, int diffExpressedGenes, int genesInSet, int diffExpressedGenesInSet) {
if(genesInSet==0 || diffExpressedGenesInSet==0){return 1.0;}
double sum=0;
cern.jet.random.HyperGeometric hyper=new cern.jet.random.HyperGeometric(numberOfGenesTotal, diffExpressedGenes, genesInSet, new cern.jet.random.engine.DRand() );
HypergeometricDistribution hyper2=new HypergeometricDistribution(numberOfGenesTotal, diffExpressedGenes, genesInSet);
//System.err.println(1-hyper2.cumulativeProbability(diffExpressedGenesInSet));
for(int i=diffExpressedGenesInSet; i<Math.min(genesInSet, diffExpressedGenes); i++){
//System.err.println(i+" "+hyper.pdf(i));
sum+=hyper.pdf(i);
}
return sum;
}
示例4: analyze
import org.apache.commons.math3.distribution.HypergeometricDistribution; //导入依赖的package包/类
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;
}
示例5: twoSidedPValue
import org.apache.commons.math3.distribution.HypergeometricDistribution; //导入依赖的package包/类
/**
* 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);
}
示例6: FisherExactPValueForContingencyTable
import org.apache.commons.math3.distribution.HypergeometricDistribution; //导入依赖的package包/类
/**
* Computes a two-sided p-Value for a Fisher's exact test on the contingency table, after normalizing counts so that the sum does not exceed {@value org.broadinstitute.gatk.tools.walkers.annotator.StrandBiasTableUtils#TARGET_TABLE_SIZE}
* @param originalTable
* @return
*/
public static Double FisherExactPValueForContingencyTable(int[][] originalTable) {
final int[][] normalizedTable = normalizeContingencyTable(originalTable);
int[][] table = copyContingencyTable(normalizedTable);
int[] rowSums = { sumRow(table, 0), sumRow(table, 1) };
int[] colSums = { sumColumn(table, 0), sumColumn(table, 1) };
int N = rowSums[0] + rowSums[1];
int sampleSize = colSums[0];
int numberOfNonSuccesses = rowSums[1];
int numberOfSuccesses = rowSums[0];
/*
* The lowest possible number of successes you can sample is what's left of your sample size after
* drawing every non success in the urn. If the number of non successes in the urn is greater than the sample
* size then the minimum number of drawn successes is 0.
*/
int lo = Math.max(0, sampleSize - numberOfNonSuccesses);
/*
* The highest possible number of successes you can draw is either the total sample size or the number of
* successes in the urn. (Whichever is smaller)
*/
int hi = Math.min(sampleSize, numberOfSuccesses);
/**
* If the support of the distribution is only one value, creating the HypergeometricDistribution
* doesn't make sense. There would be only one possible observation so the p-value has to be 1.
*/
if (lo == hi) {
return 1.0;
}
/**
* For the hypergeometric distribution from which to calculate the probabilities:
* The population (N = a+b+c+d) is the sum of all four numbers in the contingency table. Then the number of
* "successes" (K = a+b) is the sum of the top row, and the sample size (n = a+c) is the sum of the first column.
*/
final HypergeometricDistribution dist = new HypergeometricDistribution(N, numberOfSuccesses, sampleSize);
//Then we determine a given probability with the sampled successes (k = a) from the first entry in the table.
double pCutoff = dist.probability(table[0][0]);
double pValue = 0.0;
/**
* Now cycle through all possible k's and add those whose probabilities are smaller than our observed k
* to the p-value, since this is a two-sided test
*/
for(int i = lo; i <= hi; i++){
double pValuePiece = dist.probability(i);
if(pValuePiece <= pCutoff) {
pValue += pValuePiece;
}
}
// min is necessary as numerical precision can result in pValue being slightly greater than 1.0
return Math.min(pValue, 1.0);
}
示例7: computeBinaryEnrichment
import org.apache.commons.math3.distribution.HypergeometricDistribution; //导入依赖的package包/类
static void computeBinaryEnrichment(NetworkProvider networkProvider,
AnnotationProvider annotationProvider,
ProgressReporter progressReporter,
List<? extends Neighborhood> neighborhoods,
BackgroundMethod backgroundMethod) {
int totalNodes;
IntIntFunction nodeCount;
switch (backgroundMethod) {
case Network:
totalNodes = annotationProvider.getNetworkNodeCount();
nodeCount = j -> annotationProvider.getNetworkNodeCountForAttribute(j);
break;
case Annotation:
totalNodes = annotationProvider.getAnnotationNodeCount();
nodeCount = j -> annotationProvider.getAnnotationNodeCountForAttribute(j);
break;
default:
throw new RuntimeException("Unexpected background method");
}
Stream<? extends Neighborhood> stream = neighborhoods.stream();
if (progressReporter.supportsParallel()) {
stream = stream.parallel();
}
progressReporter.startNeighborhoodScore(networkProvider, annotationProvider);
stream.forEach(neighborhood -> {
int nodeIndex = neighborhood.getNodeIndex();
int neighborhoodSize = neighborhood.getMemberCount();
for (int j = 0; j < annotationProvider.getAttributeCount(); j++) {
int totalNodesForFunction = nodeCount.apply(j);
int totalNeighborhoodNodesForFunction = neighborhood.getMemberCountForAttribute(j, annotationProvider);
HypergeometricDistribution distribution = new HypergeometricDistribution(null, totalNodes,
totalNodesForFunction,
neighborhoodSize);
double p = 1.0 - distribution.cumulativeProbability(totalNeighborhoodNodesForFunction - 1);
if (Double.isFinite(p) && p < 0) {
p = 0;
}
neighborhood.setPValue(j, p);
double score = Neighborhood.computeEnrichmentScore(p);
progressReporter.neighborhoodScore(nodeIndex, j, score);
}
progressReporter.finishNeighborhood(nodeIndex);
});
progressReporter.finishNeighborhoodScore();
}
示例8: nextHypergeometric
import org.apache.commons.math3.distribution.HypergeometricDistribution; //导入依赖的package包/类
/**
* 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();
}
示例9: nextHypergeometric
import org.apache.commons.math3.distribution.HypergeometricDistribution; //导入依赖的package包/类
/**
* 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));
}
示例10: nextHypergeometric
import org.apache.commons.math3.distribution.HypergeometricDistribution; //导入依赖的package包/类
/**
* 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();
}