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

The following examples show how to use org.apache.commons.math3.distribution.UniformRealDistribution. 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: SimpleCurveFitterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testPolynomialFit() {
    final Random randomizer = new Random(53882150042L);
    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) + 0.1 * randomizer.nextGaussian());
    }

    final ParametricUnivariateFunction function = new PolynomialFunction.Parametric(); 
    // Start fit from initial guesses that are far from the optimal values.
    final SimpleCurveFitter fitter
        = SimpleCurveFitter.create(function,
                                   new double[] { -1e20, 3e15, -5e25 });
    final double[] best = fitter.fit(obs.toList());

    TestUtils.assertEquals("best != coeff", coeff, best, 2e-2);
}
 
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: SyntheticOptionsTest.java    From beam with Apache License 2.0 6 votes vote down vote up
@Test
public void testRealDistributionDeserializerWithUniformDistribution() throws Exception {
  String syntheticOptions =
      "{\"seed\":12345,"
          + "\"delayDistribution\":{\"type\":\"uniform\",\"lower\":0,\"upper\":100}}";
  SyntheticOptions sourceOptions = optionsFromString(syntheticOptions, SyntheticOptions.class);
  assertEquals(
      0,
      (long)
          ((UniformRealDistribution) (sourceOptions.delayDistribution.getDistribution()))
              .getSupportLowerBound());
  assertEquals(
      100,
      (long)
          ((UniformRealDistribution) (sourceOptions.delayDistribution.getDistribution()))
              .getSupportUpperBound());
}
 
Example #4
Source File: TravellingSalesmanSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates the features' initializers: an approximate circle around the
 * barycentre of the cities.
 *
 * @return an array containing the two initializers.
 */
private FeatureInitializer[] makeInitializers() {
    // Barycentre.
    final double[] centre = barycentre(cities);
    // Largest distance from centre.
    final double radius = 0.5 * largestDistance(centre[0], centre[1], cities);

    final double omega = 2 * Math.PI / numberOfNeurons;
    final UnivariateFunction h1 = new HarmonicOscillator(radius, omega, 0);
    final UnivariateFunction h2 = new HarmonicOscillator(radius, omega, 0.5 * Math.PI);

    final UnivariateFunction f1 = FunctionUtils.add(h1, new Constant(centre[0]));
    final UnivariateFunction f2 = FunctionUtils.add(h2, new Constant(centre[1]));

    final RealDistribution u
        = new UniformRealDistribution(random, -0.05 * radius, 0.05 * radius);

    return new FeatureInitializer[] {
        FeatureInitializerFactory.randomize(u, FeatureInitializerFactory.function(f1, 0, 1)),
        FeatureInitializerFactory.randomize(u, FeatureInitializerFactory.function(f2, 0, 1))
    };
}
 
Example #5
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #6
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 #7
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #8
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #9
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 #10
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #11
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #12
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #13
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #14
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #15
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 #16
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #17
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #18
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #19
Source File: TravellingSalesmanSolver.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Creates the features' initializers: an approximate circle around the
 * barycentre of the cities.
 *
 * @return an array containing the two initializers.
 */
private FeatureInitializer[] makeInitializers() {
    // Barycentre.
    final double[] centre = barycentre(cities);
    // Largest distance from centre.
    final double radius = 0.5 * largestDistance(centre[0], centre[1], cities);

    final double omega = 2 * Math.PI / numberOfNeurons;
    final UnivariateFunction h1 = new HarmonicOscillator(radius, omega, 0);
    final UnivariateFunction h2 = new HarmonicOscillator(radius, omega, 0.5 * Math.PI);

    final UnivariateFunction f1 = FunctionUtils.add(h1, new Constant(centre[0]));
    final UnivariateFunction f2 = FunctionUtils.add(h2, new Constant(centre[1]));

    final RealDistribution u
        = new UniformRealDistribution(random, -0.05 * radius, 0.05 * radius);

    return new FeatureInitializer[] {
        FeatureInitializerFactory.randomize(u, FeatureInitializerFactory.function(f1, 0, 1)),
        FeatureInitializerFactory.randomize(u, FeatureInitializerFactory.function(f2, 0, 1))
    };
}
 
Example #20
Source File: RandomCirclePointGenerator.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param x Abscissa of the circle center.
 * @param y Ordinate of the circle center.
 * @param radius Radius of the circle.
 * @param xSigma Error on the x-coordinate of the circumference points.
 * @param ySigma Error on the y-coordinate of the circumference points.
 * @param seed RNG seed.
 */
public RandomCirclePointGenerator(double x,
                                  double y,
                                  double radius,
                                  double xSigma,
                                  double ySigma,
                                  long seed) {
    final RandomGenerator rng = new Well44497b(seed);
    this.radius = radius;
    cX = new NormalDistribution(rng, x, xSigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    cY = new NormalDistribution(rng, y, ySigma,
                                NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    tP = new UniformRealDistribution(rng, 0, MathUtils.TWO_PI,
                                     UniformRealDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}
 
Example #21
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 #22
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 #23
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 #24
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 #25
Source File: KolmogorovSmirnovTestTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Uniform distribution, unit normal dataset */
@Test
public void testOneSampleUniformGaussian() {
    final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest();
    final UniformRealDistribution unif = new UniformRealDistribution(-0.5, 0.5);
    // Value was obtained via exact test, validated against R. Running exact test takes a long
    // time.
    Assert.assertEquals(4.9405812774239166E-11, test.kolmogorovSmirnovTest(unif, gaussian, false), TOLERANCE);
    Assert.assertTrue(test.kolmogorovSmirnovTest(unif, gaussian, 0.05));
    Assert.assertEquals(0.3401058049019608, test.kolmogorovSmirnovStatistic(unif, gaussian), TOLERANCE);
}
 
Example #26
Source File: CoverageDropoutDetectorTest.java    From gatk-protected with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private Object[][] getUnivariateGaussianTargetsWithDropout(final double sigma, final double dropoutRate) {
    Random rng = new Random(337);
    final RandomGenerator randomGenerator = RandomGeneratorFactory.createRandomGenerator(rng);
    NormalDistribution n = new NormalDistribution(randomGenerator, 1, sigma);
    final int numDataPoints = 10000;
    final int numEventPoints = 2000;

    // Randomly select dropoutRate of targets and reduce by 25%-75% (uniformly distributed)
    UniformRealDistribution uniformRealDistribution = new UniformRealDistribution(randomGenerator, 0, 1.0);
    final List<ReadCountRecord.SingleSampleRecord> targetList = new ArrayList<>();
    for (int i = 0; i < numDataPoints; i++){
        double coverage = n.sample() + (i < (numDataPoints - numEventPoints) ? 0.0 : 0.5);
        if (uniformRealDistribution.sample() < dropoutRate) {
            double multiplier = .25 + uniformRealDistribution.sample()/2;
            coverage = coverage * multiplier;
        }
        targetList.add(new ReadCountRecord.SingleSampleRecord(new Target("arbitrary_name", new SimpleInterval("chr1", 100 + 2*i, 101 + 2 * i)), coverage));
    }

    HashedListTargetCollection<ReadCountRecord.SingleSampleRecord> targets = new HashedListTargetCollection<>(targetList);

    List<ModeledSegment> segments = new ArrayList<>();
    segments.add(new ModeledSegment(new SimpleInterval("chr1", 100, 16050), 8000, 1));
    segments.add(new ModeledSegment(new SimpleInterval("chr1", 16100, 20200), 2000, 1.5));

    return new Object [] []{ {targets, segments}};
}
 
Example #27
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 #28
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 #29
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 #30
Source File: KolmogorovSmirnovTestTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/** Uniform distribution, unit normal dataset */
@Test
public void testOneSampleUniformGaussian() {
    final KolmogorovSmirnovTest test = new KolmogorovSmirnovTest();
    final UniformRealDistribution unif = new UniformRealDistribution(-0.5, 0.5);
    // Value was obtained via exact test, validated against R. Running exact test takes a long
    // time.
    Assert.assertEquals(4.9405812774239166E-11, test.kolmogorovSmirnovTest(unif, gaussian, false), TOLERANCE);
    Assert.assertTrue(test.kolmogorovSmirnovTest(unif, gaussian, 0.05));
    Assert.assertEquals(0.3401058049019608, test.kolmogorovSmirnovStatistic(unif, gaussian), TOLERANCE);
}