Java Code Examples for org.apache.commons.math3.distribution.RealDistribution#sample()

The following examples show how to use org.apache.commons.math3.distribution.RealDistribution#sample() . 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: PolynomialFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testFit() {
    final RealDistribution rng = new UniformRealDistribution(-100, 100);
    rng.reseedRandomGenerator(64925784252L);

    final LevenbergMarquardtOptimizer optim = new LevenbergMarquardtOptimizer();
    final PolynomialFitter fitter = new PolynomialFitter(optim);
    final double[] coeff = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2
    final PolynomialFunction f = new PolynomialFunction(coeff);

    // Collect data from a known polynomial.
    for (int i = 0; i < 100; i++) {
        final double x = rng.sample();
        fitter.addObservedPoint(x, f.value(x));
    }

    // Start fit from initial guesses that are far from the optimal values.
    final double[] best = fitter.fit(new double[] { -1e-20, 3e15, -5e25 });

    TestUtils.assertEquals("best != coeff", coeff, best, 1e-12);
}
 
Example 2
Source File: PolynomialFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testFit() {
    final RealDistribution rng = new UniformRealDistribution(-100, 100);
    rng.reseedRandomGenerator(64925784252L);

    final LevenbergMarquardtOptimizer optim = new LevenbergMarquardtOptimizer();
    final PolynomialFitter fitter = new PolynomialFitter(optim);
    final double[] coeff = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2
    final PolynomialFunction f = new PolynomialFunction(coeff);

    // Collect data from a known polynomial.
    for (int i = 0; i < 100; i++) {
        final double x = rng.sample();
        fitter.addObservedPoint(x, f.value(x));
    }

    // Start fit from initial guesses that are far from the optimal values.
    final double[] best = fitter.fit(new double[] { -1e-20, 3e15, -5e25 });

    TestUtils.assertEquals("best != coeff", coeff, best, 1e-12);
}
 
Example 3
Source File: PolynomialFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testFit() {
    final RealDistribution rng = new UniformRealDistribution(-100, 100);
    rng.reseedRandomGenerator(64925784252L);

    final LevenbergMarquardtOptimizer optim = new LevenbergMarquardtOptimizer();
    final PolynomialFitter fitter = new PolynomialFitter(optim);
    final double[] coeff = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2
    final PolynomialFunction f = new PolynomialFunction(coeff);

    // Collect data from a known polynomial.
    for (int i = 0; i < 100; i++) {
        final double x = rng.sample();
        fitter.addObservedPoint(x, f.value(x));
    }

    // Start fit from initial guesses that are far from the optimal values.
    final double[] best = fitter.fit(new double[] { -1e-20, 3e15, -5e25 });

    TestUtils.assertEquals("best != coeff", coeff, best, 1e-12);
}
 
Example 4
Source File: PolynomialFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testFit() {
    final RealDistribution rng = new UniformRealDistribution(-100, 100);
    rng.reseedRandomGenerator(64925784252L);

    final LevenbergMarquardtOptimizer optim = new LevenbergMarquardtOptimizer();
    final PolynomialFitter fitter = new PolynomialFitter(optim);
    final double[] coeff = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2
    final PolynomialFunction f = new PolynomialFunction(coeff);

    // Collect data from a known polynomial.
    for (int i = 0; i < 100; i++) {
        final double x = rng.sample();
        fitter.addObservedPoint(x, f.value(x));
    }

    // Start fit from initial guesses that are far from the optimal values.
    final double[] best = fitter.fit(new double[] { -1e-20, 3e15, -5e25 });

    TestUtils.assertEquals("best != coeff", coeff, best, 1e-12);
}
 
Example 5
Source File: PolynomialCurveFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testFit() {
    final RealDistribution rng = new UniformRealDistribution(-100, 100);
    rng.reseedRandomGenerator(64925784252L);

    final double[] coeff = { 12.9, -3.4, 2.1 }; // 12.9 - 3.4 x + 2.1 x^2
    final PolynomialFunction f = new PolynomialFunction(coeff);

    // Collect data from a known polynomial.
    final WeightedObservedPoints obs = new WeightedObservedPoints();
    for (int i = 0; i < 100; i++) {
        final double x = rng.sample();
        obs.add(x, f.value(x));
    }

    // Start fit from initial guesses that are far from the optimal values.
    final PolynomialCurveFitter fitter
        = PolynomialCurveFitter.create(0).withStartPoint(new double[] { -1e-20, 3e15, -5e25 });
    final double[] best = fitter.fit(obs.toList());

    TestUtils.assertEquals("best != coeff", coeff, best, 1e-12);
}
 
Example 6
Source File: MathUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Make sure that permuted arrays do not hash to the same value.
 */
@Test
public void testPermutedArrayHash() {
    double[] original = new double[10];
    double[] permuted = new double[10];
    RandomDataGenerator random = new RandomDataGenerator();

    // Generate 10 distinct random values
    for (int i = 0; i < 10; i++) {
        final RealDistribution u = new UniformRealDistribution(i + 0.5, i + 0.75);
        original[i] = u.sample();
    }

    // Generate a random permutation, making sure it is not the identity
    boolean isIdentity = true;
    do {
        int[] permutation = random.nextPermutation(10, 10);
        for (int i = 0; i < 10; i++) {
            if (i != permutation[i]) {
                isIdentity = false;
            }
            permuted[i] = original[permutation[i]];
        }
    } while (isIdentity);

    // Verify that permuted array has different hash
    Assert.assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
 
Example 7
Source File: MathUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Make sure that permuted arrays do not hash to the same value.
 */
@Test
public void testPermutedArrayHash() {
    double[] original = new double[10];
    double[] permuted = new double[10];
    RandomDataImpl random = new RandomDataImpl();

    // Generate 10 distinct random values
    for (int i = 0; i < 10; i++) {
        final RealDistribution u = new UniformRealDistribution(i + 0.5, i + 0.75);
        original[i] = u.sample();
    }

    // Generate a random permutation, making sure it is not the identity
    boolean isIdentity = true;
    do {
        int[] permutation = random.nextPermutation(10, 10);
        for (int i = 0; i < 10; i++) {
            if (i != permutation[i]) {
                isIdentity = false;
            }
            permuted[i] = original[permutation[i]];
        }
    } while (isIdentity);

    // Verify that permuted array has different hash
    Assert.assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
 
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 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 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 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 10
Source File: MathUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Make sure that permuted arrays do not hash to the same value.
 */
@Test
public void testPermutedArrayHash() {
    double[] original = new double[10];
    double[] permuted = new double[10];
    RandomDataImpl random = new RandomDataImpl();

    // Generate 10 distinct random values
    for (int i = 0; i < 10; i++) {
        final RealDistribution u = new UniformRealDistribution(i + 0.5, i + 0.75);
        original[i] = u.sample();
    }

    // Generate a random permutation, making sure it is not the identity
    boolean isIdentity = true;
    do {
        int[] permutation = random.nextPermutation(10, 10);
        for (int i = 0; i < 10; i++) {
            if (i != permutation[i]) {
                isIdentity = false;
            }
            permuted[i] = original[permutation[i]];
        }
    } while (isIdentity);

    // Verify that permuted array has different hash
    Assert.assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
 
Example 11
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 12
Source File: MathUtilsTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Make sure that permuted arrays do not hash to the same value.
 */
@Test
public void testPermutedArrayHash() {
    double[] original = new double[10];
    double[] permuted = new double[10];
    RandomDataImpl random = new RandomDataImpl();

    // Generate 10 distinct random values
    for (int i = 0; i < 10; i++) {
        final RealDistribution u = new UniformRealDistribution(i + 0.5, i + 0.75);
        original[i] = u.sample();
    }

    // Generate a random permutation, making sure it is not the identity
    boolean isIdentity = true;
    do {
        int[] permutation = random.nextPermutation(10, 10);
        for (int i = 0; i < 10; i++) {
            if (i != permutation[i]) {
                isIdentity = false;
            }
            permuted[i] = original[permutation[i]];
        }
    } while (isIdentity);

    // Verify that permuted array has different hash
    Assert.assertFalse(MathUtils.hash(original) == MathUtils.hash(permuted));
}
 
Example 13
Source File: PSquarePercentileTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
private void doDistributionTest(RealDistribution distribution) {
    double data[];

    data = distribution.sample(VERY_LARGE);
    doCalculatePercentile(50, data, 0.0001);
    doCalculatePercentile(95, data, 0.0001);

    data = distribution.sample(LARGE);
    doCalculatePercentile(50, data, 0.001);
    doCalculatePercentile(95, data, 0.001);

    data = distribution.sample(VERY_BIG);
    doCalculatePercentile(50, data, 0.001);
    doCalculatePercentile(95, data, 0.001);

    data = distribution.sample(BIG);
    doCalculatePercentile(50, data, 0.001);
    doCalculatePercentile(95, data, 0.001);

    data = distribution.sample(STANDARD);
    doCalculatePercentile(50, data, 0.005);
    doCalculatePercentile(95, data, 0.005);

    data = distribution.sample(MEDIUM);
    doCalculatePercentile(50, data, 0.005);
    doCalculatePercentile(95, data, 0.005);

    data = distribution.sample(NOMINAL);
    doCalculatePercentile(50, data, 0.01);
    doCalculatePercentile(95, data, 0.01);

    data = distribution.sample(SMALL);
    doCalculatePercentile(50, data, 0.01);
    doCalculatePercentile(95, data, 0.01);

    data = distribution.sample(TINY);
    doCalculatePercentile(50, data, 0.05);
    doCalculatePercentile(95, data, 0.05);

}
 
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: FeatureInitializerFactory.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Adds some amount of random data to the given initializer.
 *
 * @param random Random variable distribution.
 * @param orig Original initializer.
 * @return an initializer whose {@link FeatureInitializer#value() value}
 * method will return {@code orig.value() + random.sample()}.
 */
public static FeatureInitializer randomize(final RealDistribution random,
                                           final FeatureInitializer orig) {
    return new FeatureInitializer() {
        /** {@inheritDoc} */
        public double value() {
            return orig.value() + random.sample();
        }
    };
}
 
Example 17
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 18
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 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.
 */

@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 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.
 */

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

}