Package org.apache.commons.math.stat.descriptive

Examples of org.apache.commons.math.stat.descriptive.SummaryStatistics


     *
     * @throws NotCompliantMBeanException not expected
     */
    @Test
    public void test_statistics() throws NotCompliantMBeanException {
        final SummaryStatistics durationStats = new SummaryStatistics();
        final SummaryStatistics servletCallCountStats = new SummaryStatistics();
        final SummaryStatistics peakRecursionDepthStats = new SummaryStatistics();
        final RequestProcessorMBeanImpl bean = new RequestProcessorMBeanImpl();

        assertEquals(0l, bean.getRequestsCount());
        assertEquals(Long.MAX_VALUE, bean.getMinRequestDurationMsec());
        assertEquals(0l, bean.getMaxRequestDurationMsec());
        assertEquals(0.0, bean.getMeanRequestDurationMsec(), 0);
        assertEquals(0.0, bean.getStandardDeviationDurationMsec(), 0);

        assertEquals(Integer.MAX_VALUE, bean.getMinServletCallCount());
        assertEquals(0l, bean.getMaxServletCallCount());
        assertEquals(0.0, bean.getMeanServletCallCount(), 0);
        assertEquals(0.0, bean.getStandardDeviationServletCallCount(), 0);

        assertEquals(Integer.MAX_VALUE, bean.getMinPeakRecursionDepth());
        assertEquals(0l, bean.getMaxPeakRecursionDepth());
        assertEquals(0.0, bean.getMeanPeakRecursionDepth(), 0);
        assertEquals(0.0, bean.getStandardDeviationPeakRecursionDepth(), 0);

        final Random random = new Random(System.currentTimeMillis() / 17);
        final int num = 10000;
        final int min = 85;
        final int max = 250;
        for (int i = 0; i < num; i++) {
            final long durationValue = min + random.nextInt(max - min);
            final int callCountValue = min + random.nextInt(max - min);
            final int peakRecursionDepthValue = min + random.nextInt(max - min);
            durationStats.addValue(durationValue);
            servletCallCountStats.addValue(callCountValue);
            peakRecursionDepthStats.addValue(peakRecursionDepthValue);
           
            final RequestData requestData = context.mock(RequestData.class, "requestData" + i);
            context.checking(new Expectations() {{
                one(requestData).getElapsedTimeMsec();
                will(returnValue(durationValue));
               
                one(requestData).getServletCallCount();
                will(returnValue(callCountValue));
               
                one(requestData).getPeakRecusionDepth();
                will(returnValue(peakRecursionDepthValue));
            }});
           
           
            bean.addRequestData(requestData);
        }

        assertEquals("Number of points must be the same", durationStats.getN(), bean.getRequestsCount());
       
        assertEquals("Min Duration must be equal", (long) durationStats.getMin(), bean.getMinRequestDurationMsec());
        assertEquals("Max Duration must be equal", (long) durationStats.getMax(), bean.getMaxRequestDurationMsec());
        assertAlmostEqual("Mean Duration", durationStats.getMean(), bean.getMeanRequestDurationMsec(), num);
        assertAlmostEqual("Standard Deviation Duration", durationStats.getStandardDeviation(),
            bean.getStandardDeviationDurationMsec(), num);
       
        assertEquals("Min Servlet Call Count must be equal", (long) servletCallCountStats.getMin(), bean.getMinServletCallCount());
        assertEquals("Max Servlet Call Count must be equal", (long) servletCallCountStats.getMax(), bean.getMaxServletCallCount());
        assertAlmostEqual("Mean Servlet Call Count", servletCallCountStats.getMean(), bean.getMeanServletCallCount(), num);
        assertAlmostEqual("Standard Deviation Servlet Call Count", servletCallCountStats.getStandardDeviation(),
            bean.getStandardDeviationServletCallCount(), num);
       
        assertEquals("Min Peak Recursion Depth must be equal", (long) peakRecursionDepthStats.getMin(), bean.getMinPeakRecursionDepth());
        assertEquals("Max Peak Recursion Depth must be equal", (long) peakRecursionDepthStats.getMax(), bean.getMaxPeakRecursionDepth());
        assertAlmostEqual("Mean Peak Recursion Depth", peakRecursionDepthStats.getMean(), bean.getMeanPeakRecursionDepth(), num);
        assertAlmostEqual("Standard Deviation Peak Recursion Depth", peakRecursionDepthStats.getStandardDeviation(),
            bean.getStandardDeviationPeakRecursionDepth(), num);
       
        //check method resetStatistics
        //In the previous test, some requests have been processed, now we reset the statistics so everything statistic is reinitialized
        bean.resetStatistics();
View Full Code Here


    /**
     * Test SummaryStatistics - implementations that do not store the data
     * and use single pass algorithms to compute statistics
    */
    public void testSummaryStatistics() throws Exception {
        SummaryStatistics u = new SummaryStatistics();
        loadStats("data/PiDigits.txt", u);
        assertEquals("PiDigits: std", std, u.getStandardDeviation(), 1E-13);
        assertEquals("PiDigits: mean", mean, u.getMean(), 1E-13);

        loadStats("data/Mavro.txt", u);
        assertEquals("Mavro: std", std, u.getStandardDeviation(), 1E-14);
        assertEquals("Mavro: mean", mean, u.getMean(), 1E-14);

        loadStats("data/Michelso.txt", u);
        assertEquals("Michelso: std", std, u.getStandardDeviation(), 1E-13);
        assertEquals("Michelso: mean", mean, u.getMean(), 1E-13);

        loadStats("data/NumAcc1.txt", u);
        assertEquals("NumAcc1: std", std, u.getStandardDeviation(), 1E-14);
        assertEquals("NumAcc1: mean", mean, u.getMean(), 1E-14);

        loadStats("data/NumAcc2.txt", u);
        assertEquals("NumAcc2: std", std, u.getStandardDeviation(), 1E-14);
        assertEquals("NumAcc2: mean", mean, u.getMean(), 1E-14);
    }
View Full Code Here

     * @param statistical summary
     */
    private void loadStats(String resource, Object u) throws Exception {

        DescriptiveStatistics d = null;
        SummaryStatistics s = null;
        if (u instanceof DescriptiveStatistics) {
            d = (DescriptiveStatistics) u;
        } else {
            s = (SummaryStatistics) u;
        }
        u.getClass().getDeclaredMethod(
                "clear", new Class[]{}).invoke(u, new Object[]{});
        mean = Double.NaN;
        std = Double.NaN;

        BufferedReader in =
            new BufferedReader(
                    new InputStreamReader(
                            CertifiedDataTest.class.getResourceAsStream(resource)));

        String line = null;

        for (int j = 0; j < 60; j++) {
            line = in.readLine();
            if (j == 40) {
                mean =
                    Double.parseDouble(
                            line.substring(line.lastIndexOf(":") + 1).trim());
            }
            if (j == 41) {
                std =
                    Double.parseDouble(
                            line.substring(line.lastIndexOf(":") + 1).trim());
            }
        }

        line = in.readLine();

        while (line != null) {
            if (d != null) {
                d.addValue(Double.parseDouble(line.trim()));
            else {
                s.addValue(Double.parseDouble(line.trim()));
            }
            line = in.readLine();
        }

        in.close();
View Full Code Here

    private Map<String, Double> certifiedValues;

    @Override
    protected void setUp() throws Exception {
        descriptives = new DescriptiveStatistics();
        summaries = new SummaryStatistics();
        certifiedValues = new HashMap<String, Double>();

        loadData();
    }
View Full Code Here

            randomData.nextGaussian(0, 0);
            fail("zero sigma -- IllegalArgumentException expected");
        } catch (IllegalArgumentException ex) {
            // ignored
        }
        SummaryStatistics u = new SummaryStatistics();
        for (int i = 0; i < largeSampleSize; i++) {
            u.addValue(randomData.nextGaussian(0, 1));
        }
        double xbar = u.getMean();
        double s = u.getStandardDeviation();
        double n = u.getN();
        /*
         * t-test at .001-level TODO: replace with externalized t-test, with
         * test statistic defined in TestStatistic
         */
        assertTrue(Math.abs(xbar) / (s / Math.sqrt(n)) < 3.29);
View Full Code Here

public class MersenneTwisterTest {

    @Test
    public void testGaussian() {
        MersenneTwister mt = new MersenneTwister(42853252100l);
        SummaryStatistics sample = new SummaryStatistics();
        for (int i = 0; i < 1000; ++i) {
            sample.addValue(mt.nextGaussian());
        }
        assertEquals(0.0, sample.getMean(), 0.005);
        assertEquals(1.0, sample.getStandardDeviation(), 0.025);
    }
View Full Code Here

    }

    @Test
    public void testDouble() {
        MersenneTwister mt = new MersenneTwister(195357343514l);
        SummaryStatistics sample = new SummaryStatistics();
        for (int i = 0; i < 1000; ++i) {
            sample.addValue(mt.nextDouble());
        }
        assertEquals(0.5, sample.getMean(), 0.02);
        assertEquals(1.0 / (2.0 * Math.sqrt(3.0)),
                     sample.getStandardDeviation(),
                     0.002);
    }
View Full Code Here

        // Solving Ordinary Differential Equations I (Nonstiff problems),
        // the curves dy/dp = g(b) are in figure 6.5
        FirstOrderIntegrator integ =
            new DormandPrince54Integrator(1.0e-8, 100.0, 1.0e-4, 1.0e-4);
        double hP = 1.0e-12;
        SummaryStatistics residualsP0 = new SummaryStatistics();
        SummaryStatistics residualsP1 = new SummaryStatistics();
        for (double b = 2.88; b < 3.08; b += 0.001) {
            Brusselator brusselator = new Brusselator(b);
            double[] y = { 1.3, b };
            integ.integrate(brusselator, 0, y, 20.0, y);
            double[] yP = { 1.3, b + hP };
            brusselator.setParameter(0, b + hP);
            integ.integrate(brusselator, 0, yP, 20.0, yP);
            residualsP0.addValue((yP[0] - y[0]) / hP - brusselator.dYdP0());
            residualsP1.addValue((yP[1] - y[1]) / hP - brusselator.dYdP1());
        }
        Assert.assertTrue((residualsP0.getMax() - residualsP0.getMin()) > 600);
        Assert.assertTrue(residualsP0.getStandardDeviation() > 30);
        Assert.assertTrue((residualsP1.getMax() - residualsP1.getMin()) > 800);
        Assert.assertTrue(residualsP1.getStandardDeviation() > 50);
    }
View Full Code Here

    }

    @Test
    public void testFloat() {
        MersenneTwister mt = new MersenneTwister(4442733263l);
        SummaryStatistics sample = new SummaryStatistics();
        for (int i = 0; i < 1000; ++i) {
            sample.addValue(mt.nextFloat());
        }
        assertEquals(0.5, sample.getMean(), 0.01);
        assertEquals(1.0 / (2.0 * Math.sqrt(3.0)),
                     sample.getStandardDeviation(),
                     0.006);
    }
View Full Code Here

    public void testHighAccuracyExternalDifferentiation()
        throws IntegratorException, DerivativeException {
        FirstOrderIntegrator integ =
            new DormandPrince54Integrator(1.0e-8, 100.0, 1.0e-10, 1.0e-10);
        double hP = 1.0e-12;
        SummaryStatistics residualsP0 = new SummaryStatistics();
        SummaryStatistics residualsP1 = new SummaryStatistics();
        for (double b = 2.88; b < 3.08; b += 0.001) {
            Brusselator brusselator = new Brusselator(b);
            double[] y = { 1.3, b };
            integ.integrate(brusselator, 0, y, 20.0, y);
            double[] yP = { 1.3, b + hP };
            brusselator.setParameter(0, b + hP);
            integ.integrate(brusselator, 0, yP, 20.0, yP);
            residualsP0.addValue((yP[0] - y[0]) / hP - brusselator.dYdP0());
            residualsP1.addValue((yP[1] - y[1]) / hP - brusselator.dYdP1());
        }
        Assert.assertTrue((residualsP0.getMax() - residualsP0.getMin()) > 0.02);
        Assert.assertTrue((residualsP0.getMax() - residualsP0.getMin()) < 0.03);
        Assert.assertTrue(residualsP0.getStandardDeviation() > 0.003);
        Assert.assertTrue(residualsP0.getStandardDeviation() < 0.004);
        Assert.assertTrue((residualsP1.getMax() - residualsP1.getMin()) > 0.04);
        Assert.assertTrue((residualsP1.getMax() - residualsP1.getMin()) < 0.05);
        Assert.assertTrue(residualsP1.getStandardDeviation() > 0.006);
        Assert.assertTrue(residualsP1.getStandardDeviation() < 0.007);
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.math.stat.descriptive.SummaryStatistics

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.