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

The following examples show how to use org.apache.commons.math.random.RandomData#nextGaussian() . 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: 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 2
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 3
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 4
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 5
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();
    Method evaluateMethod = null;
    try {
        evaluateMethod = statistic.getClass().getDeclaredMethod("evaluate",
            double[].class, double[].class, int.class, int.class);
    } catch (NoSuchMethodException ex) {
        return;  // skip test
    }

    // 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
    double weightedResult = (Double) evaluateMethod.invoke(
            statistic, values, weights, 0, values.length);
    TestUtils.assertRelativelyEquals(
            statistic.evaluate(repeatedValues), weightedResult, 10E-14);

}
 
Example 6
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 7
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 8
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 9
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 10
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 11
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 12
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 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.
 */

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);       

}