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

The following examples show how to use org.apache.commons.math3.distribution.RealDistribution. 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: SyntheticOptions.java    From beam with Apache License 2.0 6 votes vote down vote up
public static Sampler fromRealDistribution(final RealDistribution dist) {
  return new Sampler() {
    private static final long serialVersionUID = 0L;

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

    @Override
    public Object getDistribution() {
      return dist;
    }
  };
}
 
Example #3
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** 
 * Modify test integration bounds from the default. Because the distribution
 * has discontinuities at bin boundaries, integrals spanning multiple bins
 * will face convergence problems.  Only test within-bin integrals and spans
 * across no more than 3 bin boundaries.
 */
@Override
@Test
public void testDensityIntegrals() {
    final RealDistribution distribution = makeDistribution();
    final double tol = 1.0e-9;
    final BaseAbstractUnivariateIntegrator integrator =
        new IterativeLegendreGaussIntegrator(5, 1.0e-12, 1.0e-10);
    final UnivariateFunction d = new UnivariateFunction() {
        public double value(double x) {
            return distribution.density(x);
        }
    };
    final double[] lower = {0, 5, 1000, 5001, 9995};
    final double[] upper = {5, 12, 1030, 5010, 10000};
    for (int i = 1; i < 5; i++) {
        Assert.assertEquals(
                distribution.cumulativeProbability( 
                        lower[i], upper[i]),
                        integrator.integrate(
                                1000000, // Triangle integrals are very slow to converge
                                d, lower[i], upper[i]), tol);
    }
}
 
Example #4
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] makeCumulativeTestValues() {
    /* 
     * Bins should be [0, 10], (10, 20], ..., (9990, 10000]
     * Kernels should be N(4.5, 3.02765), N(14.5, 3.02765)...
     * Each bin should have mass 10/10000 = .001
     */
    final double[] testPoints = getCumulativeTestPoints();
    final double[] cumValues = new double[testPoints.length];
    final EmpiricalDistribution empiricalDistribution = (EmpiricalDistribution) makeDistribution();
    final double[] binBounds = empiricalDistribution.getUpperBounds();
    for (int i = 0; i < testPoints.length; i++) {
        final int bin = findBin(testPoints[i]);
        final double lower = bin == 0 ? empiricalDistribution.getSupportLowerBound() :
            binBounds[bin - 1];
        final double upper = binBounds[bin];
        // Compute bMinus = sum or mass of bins below the bin containing the point
        // First bin has mass 11 / 10000, the rest have mass 10 / 10000.
        final double bMinus = bin == 0 ? 0 : (bin - 1) * binMass + firstBinMass;
        final RealDistribution kernel = findKernel(lower, upper);
        final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper);
        final double kernelCum = kernel.cumulativeProbability(lower, testPoints[i]);
        cumValues[i] = bMinus + (bin == 0 ? firstBinMass : binMass) * kernelCum/withinBinKernelMass;
    }
    return cumValues;
}
 
Example #5
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] makeDensityTestValues() {
    final double[] testPoints = getCumulativeTestPoints();
    final double[] densityValues = new double[testPoints.length];
    final EmpiricalDistribution empiricalDistribution = (EmpiricalDistribution) makeDistribution();
    final double[] binBounds = empiricalDistribution.getUpperBounds();
    for (int i = 0; i < testPoints.length; i++) {
        final int bin = findBin(testPoints[i]);
        final double lower = bin == 0 ? empiricalDistribution.getSupportLowerBound() :
            binBounds[bin - 1];
        final double upper = binBounds[bin];
        final RealDistribution kernel = findKernel(lower, upper);
        final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper);
        final double density = kernel.density(testPoints[i]);
        densityValues[i] = density * (bin == 0 ? firstBinMass : binMass) / withinBinKernelMass;   
    }
    return densityValues;
}
 
Example #6
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** 
 * Modify test integration bounds from the default. Because the distribution
 * has discontinuities at bin boundaries, integrals spanning multiple bins
 * will face convergence problems.  Only test within-bin integrals and spans
 * across no more than 3 bin boundaries.
 */
@Override
@Test
public void testDensityIntegrals() {
    final RealDistribution distribution = makeDistribution();
    final double tol = 1.0e-9;
    final BaseAbstractUnivariateIntegrator integrator =
        new IterativeLegendreGaussIntegrator(5, 1.0e-12, 1.0e-10);
    final UnivariateFunction d = new UnivariateFunction() {
        public double value(double x) {
            return distribution.density(x);
        }
    };
    final double[] lower = {0, 5, 1000, 5001, 9995};
    final double[] upper = {5, 12, 1030, 5010, 10000};
    for (int i = 1; i < 5; i++) {
        Assert.assertEquals(
                distribution.cumulativeProbability( 
                        lower[i], upper[i]),
                        integrator.integrate(
                                1000000, // Triangle integrals are very slow to converge
                                d, lower[i], upper[i]), tol);
    }
}
 
Example #7
Source File: RealDistributionComparison.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static void addPDFSeries(Chart chart, RealDistribution distribution, String desc, int lowerBound, int upperBound) {
    // generates Log data
    List<Number> xData = new ArrayList<Number>();
    List<Number> yData = new ArrayList<Number>();
    int samples = 100;
    double stepSize = (upperBound - lowerBound) / (double) samples;
    for (double x = lowerBound; x <= upperBound; x += stepSize) {
        try {
            double density = distribution.density(x);
            if (! Double.isInfinite(density) && ! Double.isNaN(density)) {
                xData.add(x);
                yData.add(density);
            }
        } catch (Exception e) {
            // ignore
            // some distributions may reject certain values depending on the parameter settings
        }
    }

    Series series = chart.addSeries(desc, xData, yData);
    series.setMarker(SeriesMarker.NONE);
    series.setLineStyle(new BasicStroke(1.2f));
}
 
Example #8
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 #9
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Algorithm description:<ol>
 * <li>Find the bin B that x belongs to.</li>
 * <li>Compute P(B) = the mass of B and P(B-) = the combined mass of the bins below B.</li>
 * <li>Compute K(B) = the probability mass of B with respect to the within-bin kernel
 * and K(B-) = the kernel distribution evaluated at the lower endpoint of B</li>
 * <li>Return P(B-) + P(B) * [K(x) - K(B-)] / K(B) where
 * K(x) is the within-bin kernel distribution function evaluated at x.</li></ol></p>
 *
 * @since 3.1
 */
public double cumulativeProbability(double x) {
    if (x < min) {
        return 0d;
    } else if (x >= max) {
        return 1d;
    }
    final int binIndex = findBin(x);
    final double pBminus = pBminus(binIndex);
    final double pB = pB(binIndex);
    final double[] binBounds = getUpperBounds();
    final double kB = kB(binIndex);
    final double lower = binIndex == 0 ? min : binBounds[binIndex - 1];
    final RealDistribution kernel = k(x);
    final double withinBinCum =
        (kernel.cumulativeProbability(x) -  kernel.cumulativeProbability(lower)) / kB;
    return pBminus + pB * withinBinCum;
}
 
Example #10
Source File: RealDistributionComparison.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static void addPDFSeries(Chart chart, RealDistribution distribution, String desc, int lowerBound, int upperBound) {
    // generates Log data
    List<Number> xData = new ArrayList<Number>();
    List<Number> yData = new ArrayList<Number>();
    int samples = 100;
    double stepSize = (upperBound - lowerBound) / (double) samples;
    for (double x = lowerBound; x <= upperBound; x += stepSize) {
        try {
            double density = distribution.density(x);
            if (! Double.isInfinite(density) && ! Double.isNaN(density)) {
                xData.add(x);
                yData.add(density);
            }
        } catch (Exception e) {
            // ignore
            // some distributions may reject certain values depending on the parameter settings
        }
    }

    Series series = chart.addSeries(desc, xData, yData);
    series.setMarker(SeriesMarker.NONE);
    series.setLineStyle(new BasicStroke(1.2f));
}
 
Example #11
Source File: RealDistributionComparison.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public static void addCDFSeries(Chart chart, RealDistribution distribution, String desc, int lowerBound, int upperBound) {
    // generates Log data
    List<Number> xData = new ArrayList<Number>();
    List<Number> yData = new ArrayList<Number>();
    int samples = 100;
    double stepSize = (upperBound - lowerBound) / (double) samples;
    for (double x = lowerBound; x <= upperBound; x += stepSize) {
      double density = distribution.cumulativeProbability(x);
      if (! Double.isInfinite(density) && ! Double.isNaN(density)) {
          xData.add(x);
          yData.add(density);
      }
    }

    Series series = chart.addSeries(desc, xData, yData);
    series.setMarker(SeriesMarker.NONE);
    series.setLineStyle(new BasicStroke(1.2f));
}
 
Example #12
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 #13
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 #14
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 #15
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 #16
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 #17
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 #18
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] makeCumulativeTestValues() {
    /* 
     * Bins should be [0, 10], (10, 20], ..., (9990, 10000]
     * Kernels should be N(4.5, 3.02765), N(14.5, 3.02765)...
     * Each bin should have mass 10/10000 = .001
     */
    final double[] testPoints = getCumulativeTestPoints();
    final double[] cumValues = new double[testPoints.length];
    final EmpiricalDistribution empiricalDistribution = (EmpiricalDistribution) makeDistribution();
    final double[] binBounds = empiricalDistribution.getUpperBounds();
    for (int i = 0; i < testPoints.length; i++) {
        final int bin = findBin(testPoints[i]);
        final double lower = bin == 0 ? empiricalDistribution.getSupportLowerBound() :
            binBounds[bin - 1];
        final double upper = binBounds[bin];
        // Compute bMinus = sum or mass of bins below the bin containing the point
        // First bin has mass 11 / 10000, the rest have mass 10 / 10000.
        final double bMinus = bin == 0 ? 0 : (bin - 1) * binMass + firstBinMass;
        final RealDistribution kernel = findKernel(lower, upper);
        final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper);
        final double kernelCum = kernel.cumulativeProbability(lower, testPoints[i]);
        cumValues[i] = bMinus + (bin == 0 ? firstBinMass : binMass) * kernelCum/withinBinKernelMass;
    }
    return cumValues;
}
 
Example #19
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** 
 * Modify test integration bounds from the default. Because the distribution
 * has discontinuities at bin boundaries, integrals spanning multiple bins
 * will face convergence problems.  Only test within-bin integrals and spans
 * across no more than 3 bin boundaries.
 */
@Override
@Test
public void testDensityIntegrals() {
    final RealDistribution distribution = makeDistribution();
    final double tol = 1.0e-9;
    final BaseAbstractUnivariateIntegrator integrator =
        new IterativeLegendreGaussIntegrator(5, 1.0e-12, 1.0e-10);
    final UnivariateFunction d = new UnivariateFunction() {
        public double value(double x) {
            return distribution.density(x);
        }
    };
    final double[] lower = {0, 5, 1000, 5001, 9995};
    final double[] upper = {5, 12, 1030, 5010, 10000};
    for (int i = 1; i < 5; i++) {
        Assert.assertEquals(
                distribution.cumulativeProbability( 
                        lower[i], upper[i]),
                        integrator.integrate(
                                1000000, // Triangle integrals are very slow to converge
                                d, lower[i], upper[i]), tol);
    }
}
 
Example #20
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 #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: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** 
 * Modify test integration bounds from the default. Because the distribution
 * has discontinuities at bin boundaries, integrals spanning multiple bins
 * will face convergence problems.  Only test within-bin integrals and spans
 * across no more than 3 bin boundaries.
 */
@Override
@Test
public void testDensityIntegrals() {
    final RealDistribution distribution = makeDistribution();
    final double tol = 1.0e-9;
    final BaseAbstractUnivariateIntegrator integrator =
        new IterativeLegendreGaussIntegrator(5, 1.0e-12, 1.0e-10);
    final UnivariateFunction d = new UnivariateFunction() {
        public double value(double x) {
            return distribution.density(x);
        }
    };
    final double[] lower = {0, 5, 1000, 5001, 9995};
    final double[] upper = {5, 12, 1030, 5010, 10000};
    for (int i = 1; i < 5; i++) {
        Assert.assertEquals(
                distribution.cumulativeProbability( 
                        lower[i], upper[i]),
                        integrator.integrate(
                                1000000, // Triangle integrals are very slow to converge
                                d, lower[i], upper[i]), tol);
    }
}
 
Example #23
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
public double[] makeCumulativeTestValues() {
    /* 
     * Bins should be [0, 10], (10, 20], ..., (9990, 10000]
     * Kernels should be N(4.5, 3.02765), N(14.5, 3.02765)...
     * Each bin should have mass 10/10000 = .001
     */
    final double[] testPoints = getCumulativeTestPoints();
    final double[] cumValues = new double[testPoints.length];
    final EmpiricalDistribution empiricalDistribution = (EmpiricalDistribution) makeDistribution();
    final double[] binBounds = empiricalDistribution.getUpperBounds();
    for (int i = 0; i < testPoints.length; i++) {
        final int bin = findBin(testPoints[i]);
        final double lower = bin == 0 ? empiricalDistribution.getSupportLowerBound() :
            binBounds[bin - 1];
        final double upper = binBounds[bin];
        // Compute bMinus = sum or mass of bins below the bin containing the point
        // First bin has mass 11 / 10000, the rest have mass 10 / 10000.
        final double bMinus = bin == 0 ? 0 : (bin - 1) * binMass + firstBinMass;
        final RealDistribution kernel = findKernel(lower, upper);
        final double withinBinKernelMass = kernel.cumulativeProbability(lower, upper);
        final double kernelCum = kernel.cumulativeProbability(lower, testPoints[i]);
        cumValues[i] = bMinus + (bin == 0 ? firstBinMass : binMass) * kernelCum/withinBinKernelMass;
    }
    return cumValues;
}
 
Example #24
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 #25
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 #26
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The within-bin smoothing kernel. Returns a Gaussian distribution
 * parameterized by {@code bStats}, unless the bin contains only one
 * observation, in which case a constant distribution is returned.
 *
 * @param bStats summary statistics for the bin
 * @return within-bin kernel parameterized by bStats
 */
protected RealDistribution getKernel(SummaryStatistics bStats) {
    if (bStats.getN() == 1) {
        return new ConstantRealDistribution(bStats.getMean());
    } else {
        return new NormalDistribution(randomData.getRandomGenerator(),
            bStats.getMean(), bStats.getStandardDeviation(),
            NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
    }
}
 
Example #27
Source File: EmpiricalDistributionTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Override
public RealDistribution makeDistribution() {
    // Create a uniform distribution on [0, 10,000]
    final double[] sourceData = new double[n + 1];
    for (int i = 0; i < n + 1; i++) {
        sourceData[i] = i;
    }
    EmpiricalDistribution dist = new EmpiricalDistribution();
    dist.load(sourceData);
    return dist;
}
 
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: TestUtils.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Computes the 25th, 50th and 75th percentiles of the given distribution and returns
 * these values in an array.
 */
public static double[] getDistributionQuartiles(RealDistribution distribution) {
    double[] quantiles = new double[3];
    quantiles[0] = distribution.inverseCumulativeProbability(0.25d);
    quantiles[1] = distribution.inverseCumulativeProbability(0.5d);
    quantiles[2] = distribution.inverseCumulativeProbability(0.75d);
    return quantiles;
}
 
Example #30
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * The within-bin smoothing kernel.
 *
 * @param bStats summary statistics for the bin
 * @return within-bin kernel parameterized by bStats
 */
protected RealDistribution getKernel(SummaryStatistics bStats) {
    // Default to Gaussian
    return new NormalDistribution(randomData.getRandomGenerator(),
            bStats.getMean(), bStats.getStandardDeviation(),
            NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
}