Java Code Examples for org.apache.commons.math.random.RandomData#nextInt()

The following examples show how to use org.apache.commons.math.random.RandomData#nextInt() . 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: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 *
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example 2
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 *
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example 3
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 RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example 4
Source File: AggregateSummaryStatisticsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Generates a partition of <sample> into up to 5 sequentially selected
 * subsamples with randomly selected partition points.
 *
 * @param sample array to partition
 * @return rectangular array with rows = subsamples
 */
private double[][] generatePartition(double[] sample) {
    final int length = sample.length;
    final double[][] out = new double[5][];
    final RandomData randomData = new RandomDataImpl();
    int cur = 0;
    int offset = 0;
    int sampleCount = 0;
    for (int i = 0; i < 5; i++) {
        if (cur == length || offset == length) {
            break;
        }
        final int next = (i == 4 || cur == length - 1) ? length - 1 : randomData.nextInt(cur, length - 1);
        final int subLength = next - cur + 1;
        out[i] = new double[subLength];
        System.arraycopy(sample, offset, out[i], 0, subLength);
        cur = next + 1;
        sampleCount++;
        offset += subLength;
    }
    if (sampleCount < 5) {
        double[][] out2 = new double[sampleCount][];
        for (int j = 0; j < sampleCount; j++) {
            final int curSize = out[j].length;
            out2[j] = new double[curSize];
            System.arraycopy(out[j], 0, out2[j], 0, curSize);
        }
        return out2;
    } else {
        return out;
    }
}
 
Example 5
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 RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example 6
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 RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example 7
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 RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example 8
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 RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example 9
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 RandomData randomData = new RandomDataImpl();
    final int sampleSize = randomData.nextInt(10,100);
    double[] out = new double[sampleSize];
    for (int i = 0; i < out.length; i++) {
        out[i] = randomData.nextUniform(-100, 100);
    }
    return out;
}
 
Example 10
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testWithInitialCapacityAndExpansionFactor() {

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

        RandomData randomData = new RandomDataImpl();
        int iterations = randomData.nextInt(100, 3000);

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

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

        eDA3.addElement( 2.0 );

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

        assertEquals("Expansion factor should equal 3.0", 3.0f, eDA3.getExpansionFactor(), Double.MIN_VALUE);
    }
 
Example 11
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testWithInitialCapacityAndExpansionFactor() {

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

        RandomData randomData = new RandomDataImpl();
        int iterations = randomData.nextInt(100, 3000);

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

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

        eDA3.addElement( 2.0 );

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

        assertEquals("Expansion factor should equal 3.0", 3.0f, eDA3.getExpansionFactor(), Double.MIN_VALUE);
    }
 
Example 12
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testWithInitialCapacityAndExpansionFactor() {
    
    ResizableDoubleArray eDA3 = new ResizableDoubleArray(3, 3.0f, 3.5f);
    assertEquals("Initial number of elements should be 0", 0, eDA3.getNumElements() );
    
    RandomData randomData = new RandomDataImpl();
    int iterations = randomData.nextInt(100, 3000);
    
    for( int i = 0; i < iterations; i++) {
        eDA3.addElement( i );
    }
    
    assertEquals("Number of elements should be equal to " + iterations, iterations,eDA3.getNumElements());
    
    eDA3.addElement( 2.0 );
    
    assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations +1, eDA3.getNumElements() );
    
    assertEquals("Expansion factor should equal 3.0", 3.0f, eDA3.getExpansionFactor(), Double.MIN_VALUE);
}
 
Example 13
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() throws Exception {

    // 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];
    RandomData randomData = new RandomDataImpl();

    // Fill weights array with random int values between 1 and 5
    int[] intWeights = new int[len];
    for (int i = 0; i < len; i++) {
        intWeights[i] = randomData.nextInt(1, 5);
        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
    List<Double> valuesList = new ArrayList<Double>();
    for (int i = 0; i < len; i++) {
        double value = randomData.nextGaussian(mu, sigma);
        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-14);

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

}
 
Example 14
Source File: ResizableDoubleArrayTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void testWithInitialCapacity() {
    
    ResizableDoubleArray eDA2 = new ResizableDoubleArray(2);
    assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());
    
    RandomData randomData = new RandomDataImpl();
    int iterations = randomData.nextInt(100, 1000);
    
    for( int i = 0; i < iterations; i++) {
        eDA2.addElement( i );
    }
    
    assertEquals("Number of elements should be equal to " + iterations, iterations, eDA2.getNumElements());
    
    eDA2.addElement( 2.0 );
    
    assertEquals("Number of elements should be equals to " + (iterations +1),
            iterations + 1 , eDA2.getNumElements() );
}
 
Example 15
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testWithInitialCapacity() {

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

        RandomData randomData = new RandomDataImpl();
        int iterations = randomData.nextInt(100, 1000);

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

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

        eDA2.addElement( 2.0 );

        assertEquals("Number of elements should be equals to " + (iterations +1),
                iterations + 1 , eDA2.getNumElements() );
    }
 
Example 16
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() );

    RandomData randomData = new RandomDataImpl();
    int iterations = randomData.nextInt(100, 3000);

    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 17
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testWithInitialCapacityAndExpansionFactor() {

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

        RandomData randomData = new RandomDataImpl();
        int iterations = randomData.nextInt(100, 3000);

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

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

        eDA3.addElement( 2.0 );

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

        assertEquals("Expansion factor should equal 3.0", 3.0f, eDA3.getExpansionFactor(), Double.MIN_VALUE);
    }
 
Example 18
Source File: ResizableDoubleArrayTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void testWithInitialCapacityAndExpansionFactor() {

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

        RandomData randomData = new RandomDataImpl();
        int iterations = randomData.nextInt(100, 3000);

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

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

        eDA3.addElement( 2.0 );

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

        assertEquals("Expansion factor should equal 3.0", 3.0f, eDA3.getExpansionFactor(), Double.MIN_VALUE);
    }
 
Example 19
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.
 */

public void testWeightedConsistency() throws Exception {

    // 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];
    RandomData randomData = new RandomDataImpl();

    // Fill weights array with random int values between 1 and 5
    int[] intWeights = new int[len];
    for (int i = 0; i < len; i++) {
        intWeights[i] = randomData.nextInt(1, 5);
        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
    List<Double> valuesList = new ArrayList<Double>();
    for (int i = 0; i < len; i++) {
        double value = randomData.nextGaussian(mu, sigma);
        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-14);
    
    // Check consistency of weighted evaluation methods
    assertEquals(weightedStatistic.evaluate(values, weights, 0, values.length),
            weightedStatistic.evaluate(values, weights), Double.MIN_VALUE);       

}
 
Example 20
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.
 */

public void testWeightedConsistency() throws Exception {

    // 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];
    RandomData randomData = new RandomDataImpl();

    // Fill weights array with random int values between 1 and 5
    int[] intWeights = new int[len];
    for (int i = 0; i < len; i++) {
        intWeights[i] = randomData.nextInt(1, 5);
        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
    List<Double> valuesList = new ArrayList<Double>();
    for (int i = 0; i < len; i++) {
        double value = randomData.nextGaussian(mu, sigma);
        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-14);
    
    // Check consistency of weighted evaluation methods
    assertEquals(weightedStatistic.evaluate(values, weights, 0, values.length),
            weightedStatistic.evaluate(values, weights), Double.MIN_VALUE);       

}