private void checkNextUniformUniform(double min, double max) {
    // Set up bin bounds - min, binBound[0], ..., binBound[binCount-2], max
    final int binCount = 5;
    final double binSize =
        max / binCount - min / binCount; // Prevent overflow in extreme value case
    final double[] binBounds = new double[binCount - 1];
    binBounds[0] = min + binSize;
    for (int i = 1; i < binCount - 1; i++) {
      binBounds[i] = binBounds[i - 1] + binSize; // + instead of * to avoid overflow in extreme case
    }

    final Frequency freq = new Frequency();
    for (int i = 0; i < smallSampleSize; i++) {
      final double value = randomData.nextUniform(min, max);
      Assert.assertTrue("nextUniform range", (value > min) && (value < max));
      // Find bin
      int j = 0;
      while (j < binCount - 1 && value > binBounds[j]) {
        j++;
      }
      freq.addValue(j);
    }

    final long[] observed = new long[binCount];
    for (int i = 0; i < binCount; i++) {
      observed[i] = freq.getCount(i);
    }
    final double[] expected = new double[binCount];
    for (int i = 0; i < binCount; i++) {
      expected[i] = 1d / binCount;
    }

    TestUtils.assertChiSquareAccept(expected, observed, 0.01);
  }
 /** test dispersion and failure modes for nextHex() */
 @Test
 @Retry(3)
 public void testNextSecureHex() {
   try {
     randomData.nextSecureHexString(-1);
     Assert.fail("negative length -- MathIllegalArgumentException expected");
   } catch (MathIllegalArgumentException ex) {
     // ignored
   }
   try {
     randomData.nextSecureHexString(0);
     Assert.fail("zero length -- MathIllegalArgumentException expected");
   } catch (MathIllegalArgumentException ex) {
     // ignored
   }
   String hexString = randomData.nextSecureHexString(3);
   if (hexString.length() != 3) {
     Assert.fail("incorrect length for generated string");
   }
   hexString = randomData.nextSecureHexString(1);
   if (hexString.length() != 1) {
     Assert.fail("incorrect length for generated string");
   }
   try {
     hexString = randomData.nextSecureHexString(0);
     Assert.fail("zero length requested -- expecting MathIllegalArgumentException");
   } catch (MathIllegalArgumentException ex) {
     // ignored
   }
   Frequency f = new Frequency();
   for (int i = 0; i < smallSampleSize; i++) {
     hexString = randomData.nextSecureHexString(100);
     if (hexString.length() != 100) {
       Assert.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]);
   }
   TestUtils.assertChiSquareAccept(expected, observed, 0.001);
 }
  private void checkNextSecureIntUniform(int min, int max) {
    final Frequency freq = new Frequency();
    for (int i = 0; i < smallSampleSize; i++) {
      final int value = randomData.nextSecureInt(min, max);
      Assert.assertTrue("nextInt range", (value >= min) && (value <= max));
      freq.addValue(value);
    }
    final int len = max - min + 1;
    final long[] observed = new long[len];
    for (int i = 0; i < len; i++) {
      observed[i] = freq.getCount(min + i);
    }
    final double[] expected = new double[len];
    for (int i = 0; i < len; i++) {
      expected[i] = 1d / len;
    }

    TestUtils.assertChiSquareAccept(expected, observed, 0.0001);
  }
  private void checkNextLongUniform(long min, long max) {
    final Frequency freq = new Frequency();
    for (int i = 0; i < smallSampleSize; i++) {
      final long value = randomData.nextLong(min, max);
      Assert.assertTrue(
          "nextLong range: " + value + " " + min + " " + max, (value >= min) && (value <= max));
      freq.addValue(value);
    }
    final int len = ((int) (max - min)) + 1;
    final long[] observed = new long[len];
    for (int i = 0; i < len; i++) {
      observed[i] = freq.getCount(min + i);
    }
    final double[] expected = new double[len];
    for (int i = 0; i < len; i++) {
      expected[i] = 1d / len;
    }

    TestUtils.assertChiSquareAccept(expected, observed, 0.01);
  }
  /**
   * Make sure that empirical distribution of random Poisson(4)'s has P(X <= 5) close to actual
   * cumulative Poisson probability and that nextPoisson fails when mean is non-positive.
   */
  @Test
  public void testNextPoisson() {
    try {
      randomData.nextPoisson(0);
      Assert.fail("zero mean -- expecting MathIllegalArgumentException");
    } catch (MathIllegalArgumentException ex) {
      // ignored
    }
    try {
      randomData.nextPoisson(-1);
      Assert.fail("negative mean supplied -- MathIllegalArgumentException expected");
    } catch (MathIllegalArgumentException ex) {
      // ignored
    }
    try {
      randomData.nextPoisson(0);
      Assert.fail("0 mean supplied -- MathIllegalArgumentException expected");
    } catch (MathIllegalArgumentException ex) {
      // ignored
    }

    final double mean = 4.0d;
    final int len = 5;
    PoissonDistribution poissonDistribution = new PoissonDistribution(mean);
    Frequency f = new Frequency();
    randomData.reSeed(1000);
    for (int i = 0; i < largeSampleSize; i++) {
      f.addValue(randomData.nextPoisson(mean));
    }
    final long[] observed = new long[len];
    for (int i = 0; i < len; i++) {
      observed[i] = f.getCount(i + 1);
    }
    final double[] expected = new double[len];
    for (int i = 0; i < len; i++) {
      expected[i] = poissonDistribution.probability(i + 1) * largeSampleSize;
    }

    TestUtils.assertChiSquareAccept(expected, observed, 0.0001);
  }
  @Test
  public void summarizeLSAT7dataTest() {
    System.out.println("Testing summary of LSAT7 data");
    readLsat7Data();

    // true values from mirt package in R
    String[][] trueValues = {
      {"[0, 0, 0, 0, 0]", "12"},
      {"[0, 0, 0, 0, 1]", "19"},
      {"[0, 0, 0, 1, 0]", "1"},
      {"[0, 0, 0, 1, 1]", "7"},
      {"[0, 0, 1, 0, 0]", "3"},
      {"[0, 0, 1, 0, 1]", "19"},
      {"[0, 0, 1, 1, 0]", "3"},
      {"[0, 0, 1, 1, 1]", "17"},
      {"[0, 1, 0, 0, 0]", "10"},
      {"[0, 1, 0, 0, 1]", "5"},
      {"[0, 1, 0, 1, 0]", "3"},
      {"[0, 1, 0, 1, 1]", "7"},
      {"[0, 1, 1, 0, 0]", "7"},
      {"[0, 1, 1, 0, 1]", "23"},
      {"[0, 1, 1, 1, 0]", "8"},
      {"[0, 1, 1, 1, 1]", "28"},
      {"[1, 0, 0, 0, 0]", "7"},
      {"[1, 0, 0, 0, 1]", "39"},
      {"[1, 0, 0, 1, 0]", "11"},
      {"[1, 0, 0, 1, 1]", "34"},
      {"[1, 0, 1, 0, 0]", "14"},
      {"[1, 0, 1, 0, 1]", "51"},
      {"[1, 0, 1, 1, 0]", "15"},
      {"[1, 0, 1, 1, 1]", "90"},
      {"[1, 1, 0, 0, 0]", "6"},
      {"[1, 1, 0, 0, 1]", "25"},
      {"[1, 1, 0, 1, 0]", "7"},
      {"[1, 1, 0, 1, 1]", "35"},
      {"[1, 1, 1, 0, 0]", "18"},
      {"[1, 1, 1, 0, 1]", "136"},
      {"[1, 1, 1, 1, 0]", "32"},
      {"[1, 1, 1, 1, 1]", "308"}
    };

    // summarize response vectors into a frequency object
    Frequency freq = new Frequency();
    for (int i = 0; i < lsat7.length; i++) {
      freq.addValue(Arrays.toString(lsat7[i]));
    }

    assertEquals("Same number of response strings", trueValues.length, freq.getUniqueCount());

    for (int i = 0; i < trueValues.length; i++) {
      assertEquals(
          "Response vector comparison: ",
          Double.parseDouble(trueValues[i][1]),
          Long.valueOf(freq.getCount(trueValues[i][0])).doubleValue(),
          1e-5);
    }

    ItemResponseVector[] responseData = new ItemResponseVector[freq.getUniqueCount()];
    ItemResponseVector irv = null;
    Iterator<Comparable<?>> iter = freq.valuesIterator();
    int index = 0;

    // create array of ItemResponseVector objects
    while (iter.hasNext()) {
      // get response string from frequency summary and convert to byte array
      Comparable<?> value = iter.next();
      String s = value.toString();
      s = s.substring(1, s.lastIndexOf("]"));
      String[] sa = s.split(",");
      byte[] rv = new byte[sa.length];
      for (int i = 0; i < sa.length; i++) {
        rv[i] = Byte.parseByte(sa[i].trim());
      }

      // create response vector objects
      irv = new ItemResponseVector(rv, Long.valueOf(freq.getCount(value)).doubleValue());
      responseData[index] = irv;
      index++;
    }

    // display results of summary
    for (int i = 0; i < responseData.length; i++) {
      System.out.println(responseData[i].toString() + ": " + responseData[i].getFrequency());
    }
  }
  /**
   * Verifies that nextPoisson(mean) generates an empirical distribution of values consistent with
   * PoissonDistributionImpl by generating 1000 values, computing a grouped frequency distribution
   * of the observed values and comparing this distribution to the corresponding expected
   * distribution computed using PoissonDistributionImpl. Uses ChiSquare test of goodness of fit to
   * evaluate the null hypothesis that the distributions are the same. If the null hypothesis can be
   * rejected with confidence 1 - alpha, the check fails.
   */
  public void checkNextPoissonConsistency(double mean) {
    // 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 PoissonDistribution(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 = 0;
    boolean widthSufficient = false;
    double lowerBinMass = 0;
    double upperBinMass = 0;
    while (!widthSufficient) {
      binWidth++;
      lowerBinMass = poissonDistribution.cumulativeProbability(lower - 1, lower + binWidth - 1);
      upperBinMass = poissonDistribution.cumulativeProbability(upper - binWidth - 1, upper - 1);
      widthSufficient = FastMath.min(lowerBinMass, upperBinMass) * sampleSize >= minExpectedCount;
    }

    /*
     *  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(
        upper); // The size of bin [binBounds[binCount - 2], upper) satisfies binWidth <= size <
                // 2*binWidth.

    // 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-1], binBounds[i])) * sampleSize
      expected[i] =
          (poissonDistribution.cumulativeProbability(binBounds.get(i) - 1)
                  - poissonDistribution.cumulativeProbability(binBounds.get(i - 1) - 1))
              * sampleSize;
    }

    // Use chisquare test to verify that generated values are poisson(mean)-distributed
    ChiSquareTest chiSquareTest = new ChiSquareTest();
    // Fail if we can reject null hypothesis that distributions are the same
    if (chiSquareTest.chiSquareTest(expected, observed, alpha)) {
      StringBuilder msgBuffer = new StringBuilder();
      DecimalFormat df = new DecimalFormat("#.##");
      msgBuffer.append("Chisquare test failed for mean = ");
      msgBuffer.append(mean);
      msgBuffer.append(" p-value = ");
      msgBuffer.append(chiSquareTest.chiSquareTest(expected, observed));
      msgBuffer.append(" chisquare statistic = ");
      msgBuffer.append(chiSquareTest.chiSquare(expected, observed));
      msgBuffer.append(". \n");
      msgBuffer.append("bin\t\texpected\tobserved\n");
      for (int i = 0; i < expected.length; i++) {
        msgBuffer.append("[");
        msgBuffer.append(i == 0 ? 1 : binBounds.get(i - 1));
        msgBuffer.append(",");
        msgBuffer.append(i == binBounds.size() ? "inf" : binBounds.get(i));
        msgBuffer.append(")");
        msgBuffer.append("\t\t");
        msgBuffer.append(df.format(expected[i]));
        msgBuffer.append("\t\t");
        msgBuffer.append(observed[i]);
        msgBuffer.append("\n");
      }
      msgBuffer.append("This test can fail randomly due to sampling error with probability ");
      msgBuffer.append(alpha);
      msgBuffer.append(".");
      Assert.fail(msgBuffer.toString());
    }
  }