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

The following examples show how to use org.apache.commons.math3.distribution.IntegerDistribution. 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: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testWithInitialCapacity() {

    ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
    Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());

    final IntegerDistribution randomData = new UniformIntegerDistribution(100, 1000);
    final int iterations = randomData.sample();

    for( int i = 0; i < iterations; i++) {
        eDA2.addElement( i );
    }

    Assert.assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements());

    eDA2.addElement( 2.0 );

    Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations + 1 , eDA2.getNumElements() );
}
 
Example #2
Source File: BufferUtilBenchmark.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
static BenchmarkData generate(int param, int howMany, int smallType, int bigType) {
  IntegerDistribution ud = new UniformIntegerDistribution(new Well19937c(param + 17),
      Short.MIN_VALUE, Short.MAX_VALUE);
  ClusteredDataGenerator cd = new ClusteredDataGenerator();
  IntegerDistribution p = new UniformIntegerDistribution(new Well19937c(param + 123),
      SMALLEST_ARRAY, BIGGEST_ARRAY / param);
  BenchmarkContainer[] smalls = new BenchmarkContainer[howMany];
  BenchmarkContainer[] bigs = new BenchmarkContainer[howMany];
  for (int i = 0; i < howMany; i++) {
    int smallSize = p.sample();
    int bigSize = smallSize * param;
    char[] small =
        smallType == 0 ? generateUniform(ud, smallSize) : generateClustered(cd, smallSize);
    char[] big = bigType == 0 ? generateUniform(ud, bigSize) : generateClustered(cd, bigSize);
    smalls[i] = new BenchmarkContainer(small);
    bigs[i] = new BenchmarkContainer(big);
  }
  return new BenchmarkData(smalls, bigs);
}
 
Example #3
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testWithInitialCapacity() {

    ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
    Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());

    final IntegerDistribution randomData = new UniformIntegerDistribution(100, 1000);
    final int iterations = randomData.sample();

    for( int i = 0; i < iterations; i++) {
        eDA2.addElement( i );
    }

    Assert.assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements());

    eDA2.addElement( 2.0 );

    Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations + 1 , eDA2.getNumElements() );
}
 
Example #4
Source File: SyntheticOptions.java    From beam with Apache License 2.0 6 votes vote down vote up
public static Sampler fromIntegerDistribution(final IntegerDistribution dist) {
  return new Sampler() {
    private static final long serialVersionUID = 0L;

    @Override
    public double sample(long seed) {
      dist.reseedRandomGenerator(seed);
      return dist.sample();
    }

    @Override
    public Object getDistribution() {
      return dist;
    }
  };
}
 
Example #5
Source File: UtilBenchmark.java    From RoaringBitmap with Apache License 2.0 6 votes vote down vote up
static BenchmarkData generate(int param, int howMany, int smallType, int bigType) {
  IntegerDistribution ud = new UniformIntegerDistribution(new Well19937c(param + 17),
      Short.MIN_VALUE, Short.MAX_VALUE);
  ClusteredDataGenerator cd = new ClusteredDataGenerator();
  IntegerDistribution p = new UniformIntegerDistribution(new Well19937c(param + 123),
      SMALLEST_ARRAY, BIGGEST_ARRAY / param);
  BenchmarkContainer[] smalls = new BenchmarkContainer[howMany];
  BenchmarkContainer[] bigs = new BenchmarkContainer[howMany];
  for (int i = 0; i < howMany; i++) {
    int smallSize = p.sample();
    int bigSize = smallSize * param;
    char[] small =
        smallType == 0 ? generateUniform(ud, smallSize) : generateClustered(cd, smallSize);
    char[] big = bigType == 0 ? generateUniform(ud, bigSize) : generateClustered(cd, bigSize);
    smalls[i] = new BenchmarkContainer(small);
    bigs[i] = new BenchmarkContainer(big);
  }
  return new BenchmarkData(smalls, bigs);
}
 
Example #6
Source File: CallGraphGenerator.java    From fasten with Apache License 2.0 6 votes vote down vote up
/** Generate a random DAG using preferential attachment. First an independent set of <code>n0</code> nodes is generated.
 *  Then <code>n-n0</code> more nodes are generated: for each node, the outdegree is determined using <code>outdegreeDistribution.nextInt()</code>
 *  minimized with the number of existing nodes. For each arc, the target is the existing node <code>i</code> with probability proportional to
 *  <code>k+1</code> where <code>k</code> is <code>i</code>'s current outdegree.
 *
 * @param n number of nodes.
 * @param n0 number of initial nodes.
 * @param outdegreeDistribution distribution from which outdegrees are sampled.
 * @param random generator used to produce the arcs.
 * @return the generated DAG.
 */
public static ArrayListMutableGraph preferentialAttachmentDAG(final int n, final int n0, final IntegerDistribution outdegreeDistribution, final RandomGenerator random) {
	final ArrayListMutableGraph g = new ArrayListMutableGraph(n);
	final FenwickTree ft = new FenwickTree(n);
	// Initial independent set
	for (int source = 0; source < n0; source++) ft.incrementCount(source + 1);
	// Rest of the graph
	final IntOpenHashSet s = new IntOpenHashSet();
	for (int source = n0; source < n; source++) {
		final int m = Math.min(outdegreeDistribution.sample(), source - 1); // Outdegree
		s.clear();
		while(s.size() < m) {
			final int t = ft.sample(random);
			if (s.add(t)) {
				ft.incrementCount(t);
				g.addArc(source, t - 1);
			}
		}
		ft.incrementCount(source + 1);
	}
	return g;
}
 
Example #7
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testWithInitialCapacity() {

    ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
    Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());

    final IntegerDistribution randomData = new UniformIntegerDistribution(100, 1000);
    final int iterations = randomData.sample();

    for( int i = 0; i < iterations; i++) {
        eDA2.addElement( i );
    }

    Assert.assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements());

    eDA2.addElement( 2.0 );

    Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations + 1 , eDA2.getNumElements() );
}
 
Example #8
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testWithInitialCapacity() {

    ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
    Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());

    final IntegerDistribution randomData = new UniformIntegerDistribution(100, 1000);
    final int iterations = randomData.sample();

    for( int i = 0; i < iterations; i++) {
        eDA2.addElement( i );
    }

    Assert.assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements());

    eDA2.addElement( 2.0 );

    Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations + 1 , eDA2.getNumElements() );
}
 
Example #9
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testWithInitialCapacity() {

    ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
    Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());

    final IntegerDistribution randomData = new UniformIntegerDistribution(100, 1000);
    final int iterations = randomData.sample();

    for( int i = 0; i < iterations; i++) {
        eDA2.addElement( i );
    }

    Assert.assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements());

    eDA2.addElement( 2.0 );

    Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations + 1 , eDA2.getNumElements() );
}
 
Example #10
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testWithInitialCapacity() {

    ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
    Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());

    final IntegerDistribution randomData = new UniformIntegerDistribution(100, 1000);
    final int iterations = randomData.sample();

    for( int i = 0; i < iterations; i++) {
        eDA2.addElement( i );
    }

    Assert.assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements());

    eDA2.addElement( 2.0 );

    Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations + 1 , eDA2.getNumElements() );
}
 
Example #11
Source File: IntHistogramTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Test(dataProvider = "smoothingValues")
public void testEmpiricalDistributionSmoothing(final int smoothing) {
    final IntHistogram largeSample = genNormalSample(480, 25, 10000);
    final IntegerDistribution dist = largeSample.empiricalDistribution(smoothing);
    final long smoothedNumberOfObservations = largeSample.getMaximumTrackedValue() * smoothing + largeSample.getTotalObservations();
    double cumulative = 0;
    double expectation = 0;
    double sqExpectation = 0;
    for (int i = 0; i <= largeSample.getMaximumTrackedValue(); i++) {
        final double distProb = dist.probability(i);
        Assert.assertEquals(distProb, (largeSample.getNObservations(i) + smoothing) / (double) smoothedNumberOfObservations, 0.0001);
        cumulative += distProb;
        Assert.assertEquals(dist.cumulativeProbability(i), cumulative, 0.00001);
        expectation += distProb * i;
        sqExpectation += i * distProb * i;
    }
    Assert.assertEquals(dist.getNumericalMean(), expectation, 0.00001);
    Assert.assertEquals(dist.getNumericalVariance(), sqExpectation - expectation * expectation, 0.00001);
}
 
Example #12
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final IntegerDistribution size = new UniformIntegerDistribution(10, 100);
    final RealDistribution randomData = new UniformRealDistribution(-100, 100);
    final int sampleSize = size.sample();
    final double[] out = randomData.sample(sampleSize);
    return out;
}
 
Example #13
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final IntegerDistribution size = new UniformIntegerDistribution(10, 100);
    final RealDistribution randomData = new UniformRealDistribution(-100, 100);
    final int sampleSize = size.sample();
    final double[] out = randomData.sample(sampleSize);
    return out;
}
 
Example #14
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final IntegerDistribution size = new UniformIntegerDistribution(10, 100);
    final RealDistribution randomData = new UniformRealDistribution(-100, 100);
    final int sampleSize = size.sample();
    final double[] out = randomData.sample(sampleSize);
    return out;
}
 
Example #15
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final IntegerDistribution size = new UniformIntegerDistribution(10, 100);
    final RealDistribution randomData = new UniformRealDistribution(-100, 100);
    final int sampleSize = size.sample();
    final double[] out = randomData.sample(sampleSize);
    return out;
}
 
Example #16
Source File: IntHistogramTest.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Test
public void testEmpiricalDistributionWithoutSmoothingSampling() {
    final IntHistogram largeSample = genNormalSample(480, 25, 10000);
    final IntegerDistribution dist = largeSample.empiricalDistribution(0);
    final IntHistogram distSample = new IntHistogram(MAX_TRACKED_VALUE);
    Arrays.stream(dist.sample(1000)).forEach(distSample::addObservation);
    Assert.assertFalse(largeSample.getCDF().isDifferentByKSStatistic(distSample, SIGNIFICANCE));
}
 
Example #17
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final IntegerDistribution size = new UniformIntegerDistribution(10, 100);
    final RealDistribution randomData = new UniformRealDistribution(-100, 100);
    final int sampleSize = size.sample();
    final double[] out = randomData.sample(sampleSize);
    return out;
}
 
Example #18
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a random sample of double values.
 * Sample size is random, between 10 and 100 and values are
 * uniformly distributed over [-100, 100].
 *
 * @return array of random double values
 */
private double[] generateSample() {
    final IntegerDistribution size = new UniformIntegerDistribution(10, 100);
    final RealDistribution randomData = new UniformRealDistribution(-100, 100);
    final int sampleSize = size.sample();
    final double[] out = randomData.sample(sampleSize);
    return out;
}
 
Example #19
Source File: OryxTest.java    From oryx with Apache License 2.0 5 votes vote down vote up
/**
 * Asserts that the probability of sampling a value as or more extreme than the given value,
 * from the given discrete distribution, is at least 0.001.
 *
 * @param value sample value
 * @param dist discrete distribution
 */
public static void checkDiscreteProbability(int value, IntegerDistribution dist) {
  double probAsExtreme = value <= dist.getNumericalMean() ?
      dist.cumulativeProbability(value) :
      (1.0 - dist.cumulativeProbability(value - 1));
  assertTrue(value + " is not likely (" + probAsExtreme + " ) to differ from expected value " +
             dist.getNumericalMean() + " by chance",
             probAsExtreme >= 0.001);
}
 
Example #20
Source File: EnumeratedDistributionSamplersPerformance.java    From commons-rng with Apache License 2.0 5 votes vote down vote up
/**
 * Creates the probabilities from the distribution.
 *
 * @param dist the distribution
 * @param lower the lower bounds (inclusive)
 * @param upper the upper bounds (inclusive)
 * @return the probabilities
 */
private static double[] createProbabilities(IntegerDistribution dist, int lower, int upper) {
    double[] probabilities = new double[upper - lower + 1];
    int index = 0;
    for (int x = lower; x <= upper; x++) {
        probabilities[index++] = dist.probability(x);
    }
    return probabilities;
}
 
Example #21
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testWithInitialCapacityAndExpansionFactor() {

    ResizableDoubleArray eDA3 = new ResizableDoubleArray(3, 3.0, 3.5);
    Assert.assertEquals("Initial number of elements should be 0", 0, eDA3.getNumElements() );

    final IntegerDistribution randomData = new UniformIntegerDistribution(100, 3000);
    final int iterations = randomData.sample();

    for( int i = 0; i < iterations; i++) {
        eDA3.addElement( i );
    }

    Assert.assertEquals("Number of elements should be equal to " + iterations, iterations,eDA3.getNumElements());

    eDA3.addElement( 2.0 );

    Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations +1, eDA3.getNumElements() );

    Assert.assertEquals("Expansion factor should equal 3.0", 3.0f, eDA3.getExpansionFactor(), Double.MIN_VALUE);
}
 
Example #22
Source File: MultiLabelSynthesizer.java    From pyramid with Apache License 2.0 4 votes vote down vote up
/**
 * y0: w=(0,1)
 * y1: w=(1,1)
 * y2: w=(1,0)
 * y3: w=(1,-1)
 * @param numData
 * @return
 */
public static MultiLabelClfDataSet flipOneNonUniform(int numData){
    int numClass = 4;
    int numFeature = 2;

    MultiLabelClfDataSet dataSet = MLClfDataSetBuilder.getBuilder().numFeatures(numFeature)
            .numClasses(numClass)
            .numDataPoints(numData)
            .build();

    // generate weights
    Vector[] weights = new Vector[numClass];
    for (int k=0;k<numClass;k++){
        Vector vector = new DenseVector(numFeature);
        weights[k] = vector;
    }

    weights[0].set(0,0);
    weights[0].set(1,1);

    weights[1].set(0, 1);
    weights[1].set(1, 1);

    weights[2].set(0, 1);
    weights[2].set(1, 0);

    weights[3].set(0,1);
    weights[3].set(1,-1);


    // generate features
    for (int i=0;i<numData;i++){
        for (int j=0;j<numFeature;j++){
            dataSet.setFeatureValue(i,j,Sampling.doubleUniform(-1, 1));
        }
    }

    // assign labels
    for (int i=0;i<numData;i++){
        for (int k=0;k<numClass;k++){
            double dot = weights[k].dot(dataSet.getRow(i));
            if (dot>=0){
                dataSet.addLabel(i,k);
            }
        }
    }

    int[] indices = {0,1,2,3};
    double[] probs = {0.4,0.2,0.2,0.2};
    IntegerDistribution distribution = new EnumeratedIntegerDistribution(indices,probs);

    // flip
    for (int i=0;i<numData;i++){
        int toChange = distribution.sample();
        MultiLabel label = dataSet.getMultiLabels()[i];
        if (label.matchClass(toChange)){
            label.removeLabel(toChange);
        } else {
            label.addLabel(toChange);
        }

    }


    return dataSet;
}
 
Example #23
Source File: MultiLabelSynthesizer.java    From pyramid with Apache License 2.0 4 votes vote down vote up
/**
 * C0, y0: w=(0,1)
 * C0, y1: w=(1,1)
 * C1, y0: w=(1,0)
 * C1, y1: w=(1,-1)
 * @return
 */
public static MultiLabelClfDataSet sampleFromMix(){
    int numData = 10000;
    int numClass = 2;
    int numFeature = 2;
    int numClusters = 2;
    double[] proportions = {0.4,0.6};
    int[] indices = {0,1};

    MultiLabelClfDataSet dataSet = MLClfDataSetBuilder.getBuilder()
            .numFeatures(numFeature)
            .numClasses(numClass)
            .numDataPoints(numData)
            .build();

    // generate weights
    Vector[][] weights = new Vector[numClusters][numClass];
    for (int c=0;c<numClusters;c++){
        for (int l=0;l<numClass;l++){
            Vector vector = new DenseVector(numFeature);
            weights[c][l] = vector;
        }
    }


    weights[0][0].set(0, 0);
    weights[0][0].set(1, 1);

    weights[0][1].set(0, 1);
    weights[0][1].set(1, 1);


    weights[1][0].set(0, 1);
    weights[1][0].set(1, 0);

    weights[1][1].set(0, 1);
    weights[1][1].set(1,-1);

    // generate features
    for (int i=0;i<numData;i++){
        for (int j=0;j<numFeature;j++){
            dataSet.setFeatureValue(i,j,Sampling.doubleUniform(-1, 1));
        }
    }
    IntegerDistribution distribution = new EnumeratedIntegerDistribution(indices,proportions);
    // assign labels
    for (int i=0;i<numData;i++){
        int cluster = distribution.sample();
        System.out.println("cluster "+cluster);
        for (int l=0;l<numClass;l++){
            System.out.println("row = "+dataSet.getRow(i));
            System.out.println("weight = "+ weights[cluster][l]);
            double dot = weights[cluster][l].dot(dataSet.getRow(i));
            System.out.println("dot = "+dot);
            if (dot>=0){
                dataSet.addLabel(i,l);
            }
        }
    }

    return dataSet;
}
 
Example #24
Source File: SimpleMLUpdateIT.java    From oryx with Apache License 2.0 4 votes vote down vote up
@Test
public void testMLUpdate() throws Exception {
  Path tempDir = getTempDir();
  Path dataDir = tempDir.resolve("data");
  Map<String,Object> overlayConfig = new HashMap<>();
  overlayConfig.put("oryx.batch.update-class", MockMLUpdate.class.getName());
  ConfigUtils.set(overlayConfig, "oryx.batch.storage.data-dir", dataDir);
  ConfigUtils.set(overlayConfig, "oryx.batch.storage.model-dir", tempDir.resolve("model"));
  overlayConfig.put("oryx.batch.streaming.generation-interval-sec", GEN_INTERVAL_SEC);
  overlayConfig.put("oryx.ml.eval.test-fraction", TEST_FRACTION);
  overlayConfig.put("oryx.ml.eval.threshold", DATA_TO_WRITE / 2); // Should easily pass threshold
  Config config = ConfigUtils.overlayOn(overlayConfig, getConfig());

  startMessaging();

  List<Integer> trainCounts = MockMLUpdate.getResetTrainCounts();
  List<Integer> testCounts = MockMLUpdate.getResetTestCounts();

  startServerProduceConsumeTopics(config, DATA_TO_WRITE, WRITE_INTERVAL_MSEC);

  // If lists are unequal at this point, there must have been an empty test set
  // which yielded no call to evaluate(). Fill in the blank
  while (trainCounts.size() > testCounts.size()) {
    testCounts.add(0);
  }

  log.info("trainCounts = {}", trainCounts);
  log.info("testCounts = {}", testCounts);

  checkOutputData(dataDir, DATA_TO_WRITE);
  checkIntervals(trainCounts.size(), DATA_TO_WRITE, WRITE_INTERVAL_MSEC, GEN_INTERVAL_SEC);

  assertEquals(testCounts.size(), trainCounts.size());

  RandomGenerator random = RandomManager.getRandom();
  int lastTotalTrainCount = 0;
  int lastTestCount = 0;
  for (int i = 0; i < testCounts.size(); i++) {
    int totalTrainCount = trainCounts.get(i);
    int testCount = testCounts.get(i);
    int newTrainInGen = totalTrainCount - (lastTotalTrainCount + lastTestCount);
    if (newTrainInGen == 0) {
      continue;
    }
    lastTotalTrainCount = totalTrainCount;
    lastTestCount = testCount;
    int totalNew = testCount + newTrainInGen;

    IntegerDistribution dist = new BinomialDistribution(random, totalNew, TEST_FRACTION);
    checkDiscreteProbability(testCount, dist);
  }

}
 
Example #25
Source File: UnivariateStatisticAbstractTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests consistency of weighted statistic computation.
 * For statistics that support weighted evaluation, this test case compares
 * the result of direct computation on an array with repeated values with
 * a weighted computation on the corresponding (shorter) array with each
 * value appearing only once but with a weight value equal to its multiplicity
 * in the repeating array.
 */

@Test
public void testWeightedConsistency() {

    // See if this statistic computes weighted statistics
    // If not, skip this test
    UnivariateStatistic statistic = getUnivariateStatistic();
    if (!(statistic instanceof WeightedEvaluation)) {
        return;
    }

    // Create arrays of values and corresponding integral weights
    // and longer array with values repeated according to the weights
    final int len = 10;        // length of values array
    final double mu = 0;       // mean of test data
    final double sigma = 5;    // std dev of test data
    double[] values = new double[len];
    double[] weights = new double[len];

    // Fill weights array with random int values between 1 and 5
    int[] intWeights = new int[len];
    final IntegerDistribution weightDist = new UniformIntegerDistribution(1, 5);
    for (int i = 0; i < len; i++) {
        intWeights[i] = weightDist.sample();
        weights[i] = intWeights[i];
    }

    // Fill values array with random data from N(mu, sigma)
    // and fill valuesList with values from values array with
    // values[i] repeated weights[i] times, each i
    final RealDistribution valueDist = new NormalDistribution(mu, sigma);
    List<Double> valuesList = new ArrayList<Double>();
    for (int i = 0; i < len; i++) {
        double value = valueDist.sample();
        values[i] = value;
        for (int j = 0; j < intWeights[i]; j++) {
            valuesList.add(new Double(value));
        }
    }

    // Dump valuesList into repeatedValues array
    int sumWeights = valuesList.size();
    double[] repeatedValues = new double[sumWeights];
    for (int i = 0; i < sumWeights; i++) {
        repeatedValues[i] = valuesList.get(i);
    }

    // Compare result of weighted statistic computation with direct computation
    // on array of repeated values
    WeightedEvaluation weightedStatistic = (WeightedEvaluation) statistic;
    TestUtils.assertRelativelyEquals(statistic.evaluate(repeatedValues),
            weightedStatistic.evaluate(values, weights, 0, values.length),
            10E-12);

    // Check consistency of weighted evaluation methods
    Assert.assertEquals(weightedStatistic.evaluate(values, weights, 0, values.length),
            weightedStatistic.evaluate(values, weights), Double.MIN_VALUE);

}
 
Example #26
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testWithInitialCapacityAndExpansionFactor() {

    ResizableDoubleArray eDA3 = new ResizableDoubleArray(3, 3.0f, 3.5f);
    Assert.assertEquals("Initial number of elements should be 0", 0, eDA3.getNumElements() );

    final IntegerDistribution randomData = new UniformIntegerDistribution(100, 3000);
    final int iterations = randomData.sample();

    for( int i = 0; i < iterations; i++) {
        eDA3.addElement( i );
    }

    Assert.assertEquals("Number of elements should be equal to " + iterations, iterations,eDA3.getNumElements());

    eDA3.addElement( 2.0 );

    Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations +1, eDA3.getNumElements() );

    Assert.assertEquals("Expansion factor should equal 3.0", 3.0f, eDA3.getExpansionFactor(), Double.MIN_VALUE);
}
 
Example #27
Source File: UtilBenchmark.java    From RoaringBitmap with Apache License 2.0 4 votes vote down vote up
private static char[] generateUniform(IntegerDistribution ud, int howMany) {
  return intArrayToShortArraySorted(ud.sample(howMany));
}
 
Example #28
Source File: BufferUtilBenchmark.java    From RoaringBitmap with Apache License 2.0 4 votes vote down vote up
private static char[] generateUniform(IntegerDistribution ud, int howMany) {
  return intArrayToShortArraySorted(ud.sample(howMany));
}
 
Example #29
Source File: UnivariateStatisticAbstractTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Tests consistency of weighted statistic computation.
 * For statistics that support weighted evaluation, this test case compares
 * the result of direct computation on an array with repeated values with
 * a weighted computation on the corresponding (shorter) array with each
 * value appearing only once but with a weight value equal to its multiplicity
 * in the repeating array.
 */

@Test
public void testWeightedConsistency() {

    // See if this statistic computes weighted statistics
    // If not, skip this test
    UnivariateStatistic statistic = getUnivariateStatistic();
    if (!(statistic instanceof WeightedEvaluation)) {
        return;
    }

    // Create arrays of values and corresponding integral weights
    // and longer array with values repeated according to the weights
    final int len = 10;        // length of values array
    final double mu = 0;       // mean of test data
    final double sigma = 5;    // std dev of test data
    double[] values = new double[len];
    double[] weights = new double[len];

    // Fill weights array with random int values between 1 and 5
    int[] intWeights = new int[len];
    final IntegerDistribution weightDist = new UniformIntegerDistribution(1, 5);
    for (int i = 0; i < len; i++) {
        intWeights[i] = weightDist.sample();
        weights[i] = intWeights[i];
    }

    // Fill values array with random data from N(mu, sigma)
    // and fill valuesList with values from values array with
    // values[i] repeated weights[i] times, each i
    final RealDistribution valueDist = new NormalDistribution(mu, sigma);
    List<Double> valuesList = new ArrayList<Double>();
    for (int i = 0; i < len; i++) {
        double value = valueDist.sample();
        values[i] = value;
        for (int j = 0; j < intWeights[i]; j++) {
            valuesList.add(new Double(value));
        }
    }

    // Dump valuesList into repeatedValues array
    int sumWeights = valuesList.size();
    double[] repeatedValues = new double[sumWeights];
    for (int i = 0; i < sumWeights; i++) {
        repeatedValues[i] = valuesList.get(i);
    }

    // Compare result of weighted statistic computation with direct computation
    // on array of repeated values
    WeightedEvaluation weightedStatistic = (WeightedEvaluation) statistic;
    TestUtils.assertRelativelyEquals(statistic.evaluate(repeatedValues),
            weightedStatistic.evaluate(values, weights, 0, values.length),
            10E-12);

    // Check consistency of weighted evaluation methods
    Assert.assertEquals(weightedStatistic.evaluate(values, weights, 0, values.length),
            weightedStatistic.evaluate(values, weights), Double.MIN_VALUE);

}
 
Example #30
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testWithInitialCapacityAndExpansionFactor() {

    ResizableDoubleArray eDA3 = new ResizableDoubleArray(3, 3.0f, 3.5f);
    Assert.assertEquals("Initial number of elements should be 0", 0, eDA3.getNumElements() );

    final IntegerDistribution randomData = new UniformIntegerDistribution(100, 3000);
    final int iterations = randomData.sample();

    for( int i = 0; i < iterations; i++) {
        eDA3.addElement( i );
    }

    Assert.assertEquals("Number of elements should be equal to " + iterations, iterations,eDA3.getNumElements());

    eDA3.addElement( 2.0 );

    Assert.assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations +1, eDA3.getNumElements() );

    Assert.assertEquals("Expansion factor should equal 3.0", 3.0f, eDA3.getExpansionFactor(), Double.MIN_VALUE);
}