Package org.apache.commons.math.stat

Examples of org.apache.commons.math.stat.Frequency


            randomData.nextPoisson(0);
            fail("zero mean -- expecting IllegalArgumentException");
        } catch (IllegalArgumentException ex) {
            // ignored
        }
        Frequency f = new Frequency();
        for (int i = 0; i < largeSampleSize; i++) {
            f.addValue(randomData.nextPoisson(4.0d));
        }
        long cumFreq = f.getCount(0) + f.getCount(1) + f.getCount(2)
                + f.getCount(3) + f.getCount(4) + f.getCount(5);
        long sumFreq = f.getSumFreq();
        double cumPct = Double.valueOf(cumFreq).doubleValue()
                / Double.valueOf(sumFreq).doubleValue();
        assertEquals("cum Poisson(4)", cumPct, 0.7851, 0.2);
        try {
            randomData.nextPoisson(-1);
View Full Code Here


        // Generate sample values
        final int sampleSize = 1000;        // Number of deviates to generate
        final int minExpectedCount = 7;     // Minimum size of expected bin count
        long maxObservedValue = 0;
        final double alpha = 0.001;         // Probability of false failure
        Frequency frequency = new Frequency();
        for (int i = 0; i < sampleSize; i++) {
            long value = randomData.nextPoisson(mean);
            if (value > maxObservedValue) {
                maxObservedValue = value;
            }
            frequency.addValue(value);
        }

        /*
         *  Set up bins for chi-square test.
         *  Ensure expected counts are all at least minExpectedCount.
         *  Start with upper and lower tail bins.
         *  Lower bin = [0, lower); Upper bin = [upper, +inf).
         */
        PoissonDistribution poissonDistribution = new PoissonDistributionImpl(mean);
        int lower = 1;
        while (poissonDistribution.cumulativeProbability(lower - 1) * sampleSize < minExpectedCount) {
            lower++;
        }
        int upper = (int) (5 * mean)// Even for mean = 1, not much mass beyond 5
        while ((1 - poissonDistribution.cumulativeProbability(upper - 1)) * sampleSize < minExpectedCount) {
            upper--;
        }

        // Set bin width for interior bins.  For poisson, only need to look at end bins.
        int binWidth = 1;
        boolean widthSufficient = false;
        double lowerBinMass = 0;
        double upperBinMass = 0;
        while (!widthSufficient) {
            lowerBinMass = poissonDistribution.cumulativeProbability(lower, lower + binWidth - 1);
            upperBinMass = poissonDistribution.cumulativeProbability(upper - binWidth + 1, upper);
            widthSufficient = FastMath.min(lowerBinMass, upperBinMass) * sampleSize >= minExpectedCount;
            binWidth++;
        }

        /*
         *  Determine interior bin bounds.  Bins are
         *  [1, lower = binBounds[0]), [lower, binBounds[1]), [binBounds[1], binBounds[2]), ... ,
         *    [binBounds[binCount - 2], upper = binBounds[binCount - 1]), [upper, +inf)
         *
         */
        List<Integer> binBounds = new ArrayList<Integer>();
        binBounds.add(lower);
        int bound = lower + binWidth;
        while (bound < upper - binWidth) {
            binBounds.add(bound);
            bound += binWidth;
        }
        binBounds.add(bound);
        binBounds.add(upper);

        // Compute observed and expected bin counts
        final int binCount = binBounds.size() + 1;
        long[] observed = new long[binCount];
        double[] expected = new double[binCount];

        // Bottom bin
        observed[0] = 0;
        for (int i = 0; i < lower; i++) {
            observed[0] += frequency.getCount(i);
        }
        expected[0] = poissonDistribution.cumulativeProbability(lower - 1) * sampleSize;

        // Top bin
        observed[binCount - 1] = 0;
        for (int i = upper; i <= maxObservedValue; i++) {
            observed[binCount - 1] += frequency.getCount(i);
        }
        expected[binCount - 1] = (1 - poissonDistribution.cumulativeProbability(upper - 1)) * sampleSize;

        // Interior bins
        for (int i = 1; i < binCount - 1; i++) {
            observed[i] = 0;
            for (int j = binBounds.get(i - 1); j < binBounds.get(i); j++) {
                observed[i] += frequency.getCount(j);
            } // Expected count is (mass in [binBounds[i], binBounds[i+1])) * sampleSize
            expected[i] = (poissonDistribution.cumulativeProbability(binBounds.get(i) - 1) -
                poissonDistribution.cumulativeProbability(binBounds.get(i - 1) -1)) * sampleSize;
        }

View Full Code Here

            // ignored
        }
        if (hexString.length() != 1) {
            fail("incorrect length for generated string");
        }
        Frequency f = new Frequency();
        for (int i = 0; i < smallSampleSize; i++) {
            hexString = randomData.nextHexString(100);
            if (hexString.length() != 100) {
                fail("incorrect length for generated string");
            }
            for (int j = 0; j < hexString.length(); j++) {
                f.addValue(hexString.substring(j, j + 1));
            }
        }
        double[] expected = new double[16];
        long[] observed = new long[16];
        for (int i = 0; i < 16; i++) {
            expected[i] = (double) smallSampleSize * 100 / 16;
            observed[i] = f.getCount(hex[i]);
        }
        /*
         * Use ChiSquare dist with df = 16-1 = 15, alpha = .001 Change to 30.58
         * for alpha = .01
         */
 
View Full Code Here

            // ignored
        }
        if (hexString.length() != 1) {
            fail("incorrect length for generated string");
        }
        Frequency f = new Frequency();
        for (int i = 0; i < smallSampleSize; i++) {
            hexString = randomData.nextSecureHexString(100);
            if (hexString.length() != 100) {
                fail("incorrect length for generated string");
            }
            for (int j = 0; j < hexString.length(); j++) {
                f.addValue(hexString.substring(j, j + 1));
            }
        }
        double[] expected = new double[16];
        long[] observed = new long[16];
        for (int i = 0; i < 16; i++) {
            expected[i] = (double) smallSampleSize * 100 / 16;
            observed[i] = f.getCount(hex[i]);
        }
        /*
         * Use ChiSquare dist with df = 16-1 = 15, alpha = .001 Change to 30.58
         * for alpha = .01
         */
 
View Full Code Here

            testGenerator.nextInt(-1);
            fail("IllegalArgumentException expected");
        } catch (IllegalArgumentException ex) {
            // ignored
        }
        Frequency freq = new Frequency();
        int value = 0;
        for (int i=0; i<smallSampleSize; i++) {
            value = testGenerator.nextInt(4);
            assertTrue("nextInt range",(value >= 0) && (value <= 3));
            freq.addValue(value)
        }
        long[] observed = new long[4];
        for (int i=0; i<4; i++) {
            observed[i] = freq.getCount(i);
        }
       
        /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
         * Change to 11.34 for alpha = .01
         */
 
View Full Code Here

    public void testNextLong() {
        long q1 = Long.MAX_VALUE/4;
        long q2 = 2 *  q1;
        long q3 = 3 * q1;
       
        Frequency freq = new Frequency();
        long val = 0;
        int value = 0;
        for (int i=0; i<smallSampleSize; i++) {
            val = testGenerator.nextLong();
            if (val < q1) {
                value = 0;
            } else if (val < q2) {
                value = 1;
            } else if (val < q3) {
                value = 2;
            } else {
                value = 3;
            }
            freq.addValue(value)
        }
        long[] observed = new long[4];
        for (int i=0; i<4; i++) {
            observed[i] = freq.getCount(i);
        }
       
        /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
         * Change to 11.34 for alpha = .01
         */
 
View Full Code Here

        assertTrue("chi-square test -- will fail about 1 in 1000 times",
                testStatistic.chiSquare(expected,observed) < 10.828);   
    }
   
    public void testNextFloat() {
        Frequency freq = new Frequency();
        float val = 0;
        int value = 0;
        for (int i=0; i<smallSampleSize; i++) {
            val = testGenerator.nextFloat();
            if (val < 0.25) {
                value = 0;
            } else if (val < 0.5) {
                value = 1;
            } else if (val < 0.75) {
                value = 2;
            } else {
                value = 3;
            }
            freq.addValue(value)
        }
        long[] observed = new long[4];
        for (int i=0; i<4; i++) {
            observed[i] = freq.getCount(i);
        }
       
        /* Use ChiSquare dist with df = 4-1 = 3, alpha = .001
         * Change to 11.34 for alpha = .01
         */
 
View Full Code Here

      randomData.nextInt(4, 3);
      fail("IllegalArgumentException expected");
    } catch (IllegalArgumentException ex) {
      // ignored
    }
    Frequency freq = new Frequency();
    int value = 0;
    for (int i = 0; i < smallSampleSize; i++) {
      value = randomData.nextInt(0, 3);
      assertTrue("nextInt range", (value >= 0) && (value <= 3));
      freq.addValue(value);
    }
    long[] observed = new long[4];
    for (int i = 0; i < 4; i++) {
      observed[i] = freq.getCount(i);
    }

    /*
     * Use ChiSquare dist with df = 4-1 = 3, alpha = .001 Change to 11.34
     * for alpha = .01
View Full Code Here

      randomData.nextLong(4, 3);
      fail("IllegalArgumentException expected");
    } catch (IllegalArgumentException ex) {
      // ignored
    }
    Frequency freq = new Frequency();
    long value = 0;
    for (int i = 0; i < smallSampleSize; i++) {
      value = randomData.nextLong(0, 3);
      assertTrue("nextInt range", (value >= 0) && (value <= 3));
      freq.addValue(value);
    }
    long[] observed = new long[4];
    for (int i = 0; i < 4; i++) {
      observed[i] = freq.getCount(i);
    }

    /*
     * Use ChiSquare dist with df = 4-1 = 3, alpha = .001 Change to 11.34
     * for alpha = .01
View Full Code Here

      randomData.nextSecureLong(4, 3);
      fail("IllegalArgumentException expected");
    } catch (IllegalArgumentException ex) {
      // ignored
    }
    Frequency freq = new Frequency();
    long value = 0;
    for (int i = 0; i < smallSampleSize; i++) {
      value = randomData.nextSecureLong(0, 3);
      assertTrue("nextInt range", (value >= 0) && (value <= 3));
      freq.addValue(value);
    }
    long[] observed = new long[4];
    for (int i = 0; i < 4; i++) {
      observed[i] = freq.getCount(i);
    }

    /*
     * Use ChiSquare dist with df = 4-1 = 3, alpha = .001 Change to 11.34
     * for alpha = .01
View Full Code Here

TOP

Related Classes of org.apache.commons.math.stat.Frequency

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.