Java Code Examples for org.apache.commons.math3.util.CombinatoricsUtils#binomialCoefficient()

The following examples show how to use org.apache.commons.math3.util.CombinatoricsUtils#binomialCoefficient() . 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: GlobalClusteringCoefficientTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCompleteGraph() throws Exception {
	long expectedDegree = completeGraphVertexCount - 1;
	long expectedCount = completeGraphVertexCount * CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2);

	validate(completeGraph, expectedCount, expectedCount);
}
 
Example 2
Source File: LocalClusteringCoefficientTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompleteGraph() throws Exception {
	long degree = completeGraphVertexCount - 1;
	long triangleCount = 2 * CombinatoricsUtils.binomialCoefficient((int) degree, 2);

	validate(completeGraph, completeGraphVertexCount, degree, triangleCount);
}
 
Example 3
Source File: AdamicAdarTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithStarGraph() throws Exception {
	// all leaf vertices form a triplet with all other leaf vertices;
	// only the center vertex is excluded
	long expectedCount = CombinatoricsUtils.binomialCoefficient((int) starGraphVertexCount - 1, 2);

	// the intersection includes only the center vertex
	float expectedScore = 1 / (float) Math.log(starGraphVertexCount - 1);

	validate(starGraph, expectedCount, expectedScore);
}
 
Example 4
Source File: TriangleListingTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompleteGraph() throws Exception {
	long expectedDegree = completeGraphVertexCount - 1;
	long expectedCount = completeGraphVertexCount * CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2) / 3;

	DataSet<Result<LongValue>> tl = completeGraph
		.run(new TriangleListing<>());

	Checksum checksum = new ChecksumHashCode<Result<LongValue>>()
		.run(tl)
		.execute();

	assertEquals(expectedCount, checksum.getCount());
}
 
Example 5
Source File: JaccardIndexTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCompleteGraph() throws Exception {
	// all vertex pairs are linked
	long expectedCount = CombinatoricsUtils.binomialCoefficient((int) completeGraphVertexCount, 2);

	// the intersection includes every vertex
	long expectedDistinctNeighborCount = completeGraphVertexCount;

	// the union only excludes the two vertices from the similarity score
	long expectedSharedNeighborCount = completeGraphVertexCount - 2;

	validate(completeGraph, expectedCount, expectedDistinctNeighborCount, expectedSharedNeighborCount);
}
 
Example 6
Source File: EdgeMetricsTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCompleteGraph() throws Exception {
	long expectedDegree = completeGraphVertexCount - 1;
	long expectedMaximumTriplets = CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2);
	long expectedTriplets = completeGraphVertexCount * expectedMaximumTriplets;

	Result expectedResult = new Result(expectedTriplets / 3, 2 * expectedTriplets / 3,
		expectedMaximumTriplets, expectedMaximumTriplets);

	validate(completeGraph, expectedResult);
}
 
Example 7
Source File: VertexMetricsTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCompleteGraph() throws Exception {
	long expectedDegree = completeGraphVertexCount - 1;
	long expectedEdges = completeGraphVertexCount * expectedDegree / 2;
	long expectedMaximumTriplets = CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2);
	long expectedTriplets = completeGraphVertexCount * expectedMaximumTriplets;

	Result expectedResult = new Result(completeGraphVertexCount, expectedEdges, expectedTriplets,
		expectedDegree, expectedMaximumTriplets);

	validate(completeGraph, false, expectedResult, expectedDegree, 1.0f);
}
 
Example 8
Source File: VertexMetricsTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCompleteGraph() throws Exception {
	long expectedDegree = completeGraphVertexCount - 1;
	long expectedBidirectionalEdges = completeGraphVertexCount * expectedDegree / 2;
	long expectedMaximumTriplets = CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2);
	long expectedTriplets = completeGraphVertexCount * expectedMaximumTriplets;

	Result expectedResult = new Result(completeGraphVertexCount, 0, expectedBidirectionalEdges,
		expectedTriplets, expectedDegree, expectedDegree, expectedDegree, expectedMaximumTriplets);

	validate(completeGraph, false, expectedResult, expectedDegree, 1.0f);
}
 
Example 9
Source File: EdgeMetricsTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCompleteGraph() throws Exception {
	long expectedDegree = completeGraphVertexCount - 1;
	long expectedMaximumTriplets = CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2);
	long expectedTriplets = completeGraphVertexCount * expectedMaximumTriplets;

	Result expectedResult = new Result(expectedTriplets / 3, 2 * expectedTriplets / 3,
		expectedMaximumTriplets, expectedMaximumTriplets);

	validate(completeGraph, expectedResult);
}
 
Example 10
Source File: TriadicCensusTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCompleteGraph() throws Exception {
	long expectedDegree = completeGraphVertexCount - 1;
	long expectedCount = completeGraphVertexCount * CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2) / 3;

	Result expectedResult = new Result(0, 0, 0, expectedCount);

	Result triadCensus = new TriadicCensus<LongValue, NullValue, NullValue>()
		.run(completeGraph)
		.execute();

	assertEquals(expectedResult, triadCensus);
}
 
Example 11
Source File: TriadicCensusTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCompleteGraph() throws Exception {
	long expectedDegree = completeGraphVertexCount - 1;
	long expectedCount = completeGraphVertexCount * CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2) / 3;

	Result expectedResult = new Result(0, 0, 0, expectedCount);

	Result triadCensus = new TriadicCensus<LongValue, NullValue, NullValue>()
		.run(completeGraph)
		.execute();

	assertEquals(expectedResult, triadCensus);
}
 
Example 12
Source File: TriangleListingTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompleteGraph() throws Exception {
	long expectedDegree = completeGraphVertexCount - 1;
	long expectedCount = completeGraphVertexCount * CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2) / 3;

	DataSet<Result<LongValue>> tl = completeGraph
		.run(new TriangleListing<>());

	Checksum checksum = new ChecksumHashCode<Result<LongValue>>()
		.run(tl)
		.execute();

	assertEquals(expectedCount, checksum.getCount());
}
 
Example 13
Source File: LocalClusteringCoefficientTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testCompleteGraph() throws Exception {
	long degree = completeGraphVertexCount - 1;
	long triangleCount = CombinatoricsUtils.binomialCoefficient((int) degree, 2);

	validate(completeGraph, completeGraphVertexCount, degree, triangleCount);
}
 
Example 14
Source File: GlobalClusteringCoefficientTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCompleteGraph() throws Exception {
	long expectedDegree = completeGraphVertexCount - 1;
	long expectedCount = completeGraphVertexCount * CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2);

	validate(completeGraph, expectedCount, expectedCount);
}
 
Example 15
Source File: GlobalClusteringCoefficientTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCompleteGraph() throws Exception {
	long expectedDegree = completeGraphVertexCount - 1;
	long expectedCount = completeGraphVertexCount * CombinatoricsUtils.binomialCoefficient((int) expectedDegree, 2);

	validate(completeGraph, expectedCount, expectedCount);
}
 
Example 16
Source File: PolynomialsUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testJacobiEvaluationAt1() {
    for (int v = 0; v < 10; ++v) {
        for (int w = 0; w < 10; ++w) {
            for (int i = 0; i < 10; ++i) {
                PolynomialFunction jacobi = PolynomialsUtils.createJacobiPolynomial(i, v, w);
                double binomial = CombinatoricsUtils.binomialCoefficient(v + i, i);
                Assert.assertTrue(Precision.equals(binomial, jacobi.value(1.0), 1));
            }
        }
    }
}
 
Example 17
Source File: AdamicAdarTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithCompleteGraph() throws Exception {
	// all vertex pairs are linked
	long expectedCount = CombinatoricsUtils.binomialCoefficient((int) completeGraphVertexCount, 2);

	float expectedScore = (completeGraphVertexCount - 2) / (float) Math.log(completeGraphVertexCount - 1);

	validate(completeGraph, expectedCount, expectedScore);
}
 
Example 18
Source File: JaccardIndexTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithStarGraph() throws Exception {
	// all leaf vertices form a triplet with all other leaf vertices;
	// only the center vertex is excluded
	long expectedCount = CombinatoricsUtils.binomialCoefficient((int) starGraphVertexCount - 1, 2);

	// the intersection includes only the center vertex
	long expectedDistinctNeighborCount = 1;

	// the union includes only the center vertex
	long expectedSharedNeighborCount = 1;

	validate(starGraph, expectedCount, expectedDistinctNeighborCount, expectedSharedNeighborCount);
}
 
Example 19
Source File: LocalitySensitiveHash.java    From oryx with Apache License 2.0 4 votes vote down vote up
LocalitySensitiveHash(double sampleRate, int numFeatures, int numCores) {

    // How many hash functions to use? use as few as possible that still achieve the desired sample
    // rate or less, approximately.
    int numHashes = 0;
    int bitsDiffering = 0;
    for (; numHashes < MAX_HASHES; numHashes++) {

      // For a given number of hashes, consider partitions differing from the target hash in how many bits?
      // Choose enough such that number to test is as large as possible while <= the number of cores
      bitsDiffering = 0;
      // Number of different partitions that are examined when allowing the given number of bits to differ
      long numPartitionsToTry = 1;
      // Make bitsDiffering as large as possible given number of cores
      while (bitsDiffering < numHashes && numPartitionsToTry < numCores) {
        // There are numHashes-choose-bitsDiffering ways for numHashes bits to differ in
        // exactly bitsDiffering bits
        bitsDiffering++;
        numPartitionsToTry += CombinatoricsUtils.binomialCoefficient(numHashes, bitsDiffering);
      }
      // Note that this allows numPartitionsToTry to overshoot numCores by one step

      if (bitsDiffering == numHashes && numPartitionsToTry < numCores) {
        // Can't keep busy enough; keep going
        continue;
      }

      // Consider what fraction of all 2^n partitions is then considered, as a proxy for the
      // sample rate
      // Stop as soon as it's <= target sample rate
      if (numPartitionsToTry <= sampleRate * (1L << numHashes)) {
        break;
      }
    }

    log.info("LSH with {} hashes, querying partitions with up to {} bits differing", numHashes, bitsDiffering);
    this.maxBitsDiffering = bitsDiffering;
    hashVectors = new float[numHashes][];

    RandomGenerator random = RandomManager.getRandom();
    for (int i = 0; i < numHashes; i++) {
      // Pick the most-orthogonal next random vector
      double bestTotalDot = Double.POSITIVE_INFINITY;
      float[] nextBest = null;
      // Try, like, lots of them
      int candidatesSinceBest = 0;
      while (candidatesSinceBest < 1000) {
        float[] candidate = VectorMath.randomVectorF(numFeatures, random);
        // measure by total (absolute) dot product
        double score = totalAbsCos(hashVectors, i, candidate);
        if (score < bestTotalDot) {
          nextBest = candidate;
          // Stop if best possible score
          if (score == 0.0) {
            break;
          }
          bestTotalDot = score;
          candidatesSinceBest = 0;
        } else {
          candidatesSinceBest++;
        }
      }
      hashVectors[i] = nextBest;
    }
    log.info("Chose {} random hash vectors", hashVectors.length);

    // Contains all 2^numHashes integers from 0. The first element has 0 bits set. The next numHashes elements
    // are all such integers with 1 bit sets. Then 2 bits, and so on. This is used as a "mask" on top of an
    // initial candidate index in order to construct results in getCandidateIndices()
    candidateIndicesPrototype = new int[1 << numHashes];
    int[] offsetPerBitsActive = new int[numHashes + 1];
    for (int i = 1; i <= numHashes; i++) {
      offsetPerBitsActive[i] = offsetPerBitsActive[i-1] + (int) CombinatoricsUtils.binomialCoefficient(numHashes, i-1);
    }
    for (int i = 0; i < candidateIndicesPrototype.length; i++) {
      candidateIndicesPrototype[offsetPerBitsActive[Integer.bitCount(i)]++] = i;
    }

    // Contains all 2^numHashes integers from 0
    allIndices = new int[1 << numHashes];
    for (int i = 0; i < allIndices.length; i++) {
      allIndices[i] = i;
    }
  }
 
Example 20
Source File: VariableRolesIT.java    From grakn with GNU Affero General Public License v3.0 3 votes vote down vote up
/**
 *Each role player variable can be mapped to either of the conceptDOF concepts and these can repeat.
 *Each role variable can be mapped to either of RPs roles and only meta roles can repeat.

 *For the case of conceptDOF = 3, roleDOF = 3.
 *We start by considering the number of meta roles we allow.
 *If we consider only non-meta roles, considering each relation player we get:
 *C^3_0 x 3.3 x 3.2 x 3 = 162 combinations
 *
 *If we consider single metarole - C^3_1 = 3 possibilities of assigning them:
 *C^3_1 x 3.3 x 3.2 x 3 = 486 combinations
 *
 *Two metaroles - again C^3_2 = 3 possibilities of assigning them:
 *C^3_2 x 3.3 x 3   x 3 = 243 combinations
 *
 *Three metaroles, C^3_3 = 1 possiblity of assignment:
 *C^3_3 x 3   x 3   x 3 = 81 combinations
 *
 *-> Total = 918 different answers
 *In general, for i allowed meta roles we have:
 *C^{RP}_i PRODUCT_{j = RP-i}{ (conceptDOF)x(roleDOF-j) } x PRODUCT_i{ conceptDOF} } answers.
 *
 *So total number of answers is:
 *SUM_i{ C^{RP}_i PRODUCT_{j = RP-i}{ (conceptDOF)x(roleDOF-j) } x PRODUCT_i{ conceptDOF} }
 *
 * @param RPS number of relation players available
 * @param conceptDOF number of concept degrees of freedom
 * @return number of answer combinations
 */
private int answerCombinations(int RPS, int conceptDOF) {
    int answers = 0;
    //i is the number of meta roles
    for (int i = 0; i <= RPS; i++) {
        int RPProduct = 1;
        //rps with non-meta roles
        for (int j = 0; j < RPS - i; j++) RPProduct *= conceptDOF * (RPS - j);
        //rps with meta roles
        for (int k = 0; k < i; k++) RPProduct *= conceptDOF;
        answers += CombinatoricsUtils.binomialCoefficient(RPS, i) * RPProduct;
    }
    return answers;
}