Ejemplo n.º 1
0
 @Test
 public void testNextBinomial() {
   BinomialDistributionTest testInstance = new BinomialDistributionTest();
   int[] densityPoints = testInstance.makeDensityTestPoints();
   double[] densityValues = testInstance.makeDensityTestValues();
   int sampleSize = 1000;
   int length = TestUtils.eliminateZeroMassPoints(densityPoints, densityValues);
   BinomialDistribution distribution = (BinomialDistribution) testInstance.makeDistribution();
   double[] expectedCounts = new double[length];
   long[] observedCounts = new long[length];
   for (int i = 0; i < length; i++) {
     expectedCounts[i] = sampleSize * densityValues[i];
   }
   randomData.reSeed(1000);
   for (int i = 0; i < sampleSize; i++) {
     int value =
         randomData.nextBinomial(
             distribution.getNumberOfTrials(), distribution.getProbabilityOfSuccess());
     for (int j = 0; j < length; j++) {
       if (value == densityPoints[j]) {
         observedCounts[j]++;
       }
     }
   }
   TestUtils.assertChiSquareAccept(densityPoints, expectedCounts, observedCounts, .001);
 }
  private void testCorrectnessOfErrorFunction(List<Number> inputList) throws Exception {
    int inRange = 0;
    int numberOfRuns = 1000;
    double sampleRatio = 1 / (double) WEIGHT;
    double actual = getExpectedValue(inputList);
    Random rand = new Random(1);

    for (int i = 0; i < numberOfRuns; i++) {
      // Compute Sampled Value using sampledList (numberOfRuns times)
      ImmutableList.Builder<Number> sampledList = ImmutableList.builder();
      for (Number x : inputList) {
        if (rand.nextDouble() < sampleRatio) {
          sampledList.add(x);
        }
      }

      BlockBuilder builder = getType().createBlockBuilder(new BlockBuilderStatus());
      for (Number sample : sampledList.build()) {
        if (getType() == BIGINT) {
          BIGINT.writeLong(builder, sample.longValue());
        } else if (getType() == DOUBLE) {
          DOUBLE.writeDouble(builder, sample.doubleValue());
        } else {
          throw new AssertionError("Can only handle longs and doubles");
        }
      }
      Page page = new Page(builder.build());
      page = OperatorAssertion.appendSampleWeight(ImmutableList.of(page), WEIGHT).get(0);
      Accumulator accumulator =
          getFunction()
              .bind(
                  ImmutableList.of(0),
                  Optional.<Integer>absent(),
                  Optional.of(page.getChannelCount() - 1),
                  getConfidence())
              .createAccumulator();

      accumulator.addInput(page);
      Block result = accumulator.evaluateFinal();

      String approxValue =
          BlockAssertions.toValues(accumulator.getFinalType(), result).get(0).toString();
      double approx = Double.parseDouble(approxValue.split(" ")[0]);
      double error = Double.parseDouble(approxValue.split(" ")[2]);

      // Check if actual answer lies within [approxAnswer - error, approxAnswer + error]
      if (Math.abs(approx - actual) <= error) {
        inRange++;
      }
    }

    BinomialDistribution binomial = new BinomialDistribution(numberOfRuns, getConfidence());
    int lowerBound = binomial.inverseCumulativeProbability(0.01);
    int upperBound = binomial.inverseCumulativeProbability(0.99);
    assertTrue(
        lowerBound < inRange && inRange < upperBound,
        String.format(
            "%d out of %d passed. Expected [%d, %d]",
            inRange, numberOfRuns, lowerBound, upperBound));
  }
Ejemplo n.º 3
0
 /*
  * Authored by yiwu on Oct.8.2014
  * reference:
  * chapter 2.2 of
  * http://www.sciencedirect.com/science/article/pii/016794739390115A
  */
 private ArrayList<Integer> sample_value_use_binomial() {
   BinomialDistribution binom = null;
   int cur = 0;
   double cdf = 0;
   ArrayList<Integer> result = new ArrayList<Integer>(k);
   for (int i = 0; i < k - 1; i++) {
     if (n == cur) {
       result.add(0);
       continue;
     }
     binom = new BinomialDistribution(n - cur, p[i] / (1.0 - cdf));
     int x = binom.sample();
     cur += x;
     cdf += p[i];
     result.add(x);
   }
   result.add(n - cur);
   return result;
 }
Ejemplo n.º 4
0
 public static void main(String[] args) {
   BinomialDistribution b = new BinomialDistribution(5, 0.167);
   double p = b.probability(2);
   System.out.println(p);
 }