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

The following examples show how to use org.apache.commons.math3.distribution.RealDistribution#density() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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 12
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 13
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Returns the kernel density normalized so that its integral over each bin
 * equals the bin mass.</p>
 *
 * <p>Algorithm description: <ol>
 * <li>Find the bin B that x belongs to.</li>
 * <li>Compute K(B) = the mass of B with respect to the within-bin kernel (i.e., the
 * integral of the kernel density over B).</li>
 * <li>Return k(x) * P(B) / K(B), where k is the within-bin kernel density
 * and P(B) is the mass of B.</li></ol></p>
 * @since 3.1
 */
public double density(double x) {
    if (x < min || x > max) {
        return 0d;
    }
    final int binIndex = findBin(x);
    final RealDistribution kernel = getKernel(binStats.get(binIndex));
    return kernel.density(x) * pB(binIndex) / kB(binIndex);
}
 
Example 14
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Returns the kernel density normalized so that its integral over each bin
 * equals the bin mass.</p>
 *
 * <p>Algorithm description: <ol>
 * <li>Find the bin B that x belongs to.</li>
 * <li>Compute K(B) = the mass of B with respect to the within-bin kernel (i.e., the
 * integral of the kernel density over B).</li>
 * <li>Return k(x) * P(B) / K(B), where k is the within-bin kernel density
 * and P(B) is the mass of B.</li></ol></p>
 * @since 3.1
 */
public double density(double x) {
    if (x < min || x > max) {
        return 0d;
    }
    final int binIndex = findBin(x);
    final RealDistribution kernel = getKernel(binStats.get(binIndex));
    return kernel.density(x) * pB(binIndex) / kB(binIndex);
}
 
Example 15
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Returns the kernel density normalized so that its integral over each bin
 * equals the bin mass.</p>
 *
 * <p>Algorithm description: <ol>
 * <li>Find the bin B that x belongs to.</li>
 * <li>Compute K(B) = the mass of B with respect to the within-bin kernel (i.e., the
 * integral of the kernel density over B).</li>
 * <li>Return k(x) * P(B) / K(B), where k is the within-bin kernel density
 * and P(B) is the mass of B.</li></ol></p>
 * @since 3.1
 */
public double density(double x) {
    if (x < min || x > max) {
        return 0d;
    }
    final int binIndex = findBin(x);
    final RealDistribution kernel = getKernel(binStats.get(binIndex));
    return kernel.density(x) * pB(binIndex) / kB(binIndex);
}
 
Example 16
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Returns the kernel density normalized so that its integral over each bin
 * equals the bin mass.</p>
 *
 * <p>Algorithm description: <ol>
 * <li>Find the bin B that x belongs to.</li>
 * <li>Compute K(B) = the mass of B with respect to the within-bin kernel (i.e., the
 * integral of the kernel density over B).</li>
 * <li>Return k(x) * P(B) / K(B), where k is the within-bin kernel density
 * and P(B) is the mass of B.</li></ol></p>
 * @since 3.1
 */
public double density(double x) {
    if (x < min || x > max) {
        return 0d;
    }
    final int binIndex = findBin(x);
    final RealDistribution kernel = getKernel(binStats.get(binIndex));
    return kernel.density(x) * pB(binIndex) / kB(binIndex);
}
 
Example 17
Source File: EmpiricalDistribution.java    From astor with GNU General Public License v2.0 3 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * <p>Returns the kernel density normalized so that its integral over each bin
 * equals the bin mass.</p>
 *
 * <p>Algorithm description: <ol>
 * <li>Find the bin B that x belongs to.</li>
 * <li>Compute K(B) = the mass of B with respect to the within-bin kernel (i.e., the
 * integral of the kernel density over B).</li>
 * <li>Return k(x) * P(B) / K(B), where k is the within-bin kernel density
 * and P(B) is the mass of B.</li></ol></p>
 * @since 3.1
 */
public double density(double x) {
    if (x < min || x > max) {
        return 0d;
    }
    final int binIndex = findBin(x);
    final RealDistribution kernel = getKernel(binStats.get(binIndex));
    return kernel.density(x) * pB(binIndex) / kB(binIndex);
}
 
Example 18
Source File: DistributionUtil.java    From MeteoInfo with GNU Lesser General Public License v3.0 2 votes vote down vote up
/**
 * Probability density function at x
 * @param dis Distribution.
 * @param x X.
 * @return Probability density value.
 */
public static double pdf(RealDistribution dis, Number x){
    return dis.density(x.doubleValue());
}