/** * Generates a random value from the {@link PascalDistribution Pascal Distribution}. * * @param r the number of successes of the Pascal distribution * @param p the probability of success of the Pascal distribution * @return random value sampled from the Pascal(r, p) distribution * @throws NotStrictlyPositiveException if the number of successes is not positive * @throws OutOfRangeException if the probability of success is not in the * range {@code [0, 1]}. */ public int nextPascal(int r, double p) throws NotStrictlyPositiveException, OutOfRangeException { return new PascalDistribution(getRandomGenerator(), r, p).sample(); }
/** * {@inheritDoc} * * For number of successes {@code r} and probability of success {@code p}, * the mean is {@code r * (1 - p) / p}. */ public double getNumericalMean() { final double p = getProbabilityOfSuccess(); final double r = getNumberOfSuccesses(); return (r * (1 - p)) / p; }
public SamplingIterator(RandomWrapper random, Iterator<? extends T> delegate, double samplingRate) { Preconditions.checkNotNull(delegate); Preconditions.checkArgument(samplingRate > 0.0 && samplingRate <= 1.0, "Must be: 0.0 < samplingRate <= 1.0. But samplingRate = " + samplingRate); // Geometric distribution is special case of negative binomial (aka Pascal) with r=1: geometricDistribution = new PascalDistribution(random.getRandomGenerator(), 1, samplingRate); this.delegate = delegate; }
/** * @param param * number of successes * @param param2 * prob. of success * @return Pascal distribution */ protected PascalDistribution getPascalDistribution(int param, double param2) { if (pascal == null || pascal.getNumberOfSuccesses() != param || pascal.getProbabilityOfSuccess() != param2) { pascal = new PascalDistribution(param, param2); } return pascal; }
@Override protected T computeNext() { int toSkip = geometricDistribution.sample(); if (delegate instanceof SkippingIterator<?>) { SkippingIterator<? extends T> skippingDelegate = (SkippingIterator<? extends T>) delegate; skippingDelegate.skip(toSkip); if (skippingDelegate.hasNext()) { return skippingDelegate.next(); } } else { for (int i = 0; i < toSkip && delegate.hasNext(); i++) { delegate.next(); } if (delegate.hasNext()) { return delegate.next(); } } return endOfData(); }
public SamplingIterator(RandomWrapper random, Iterator<? extends T> delegate, double samplingRate) { Preconditions.checkNotNull(delegate); Preconditions.checkArgument(samplingRate > 0.0 && samplingRate <= 1.0, "Must be: 0.0 < samplingRate <= 1.0. But samplingRate = " + samplingRate); // Geometric distribution is special case of negative binomial (aka Pascal) with r=1: geometricDistribution = new PascalDistribution(random.getRandomGenerator(), 1, samplingRate); this.delegate = delegate; }
@Override protected T computeNext() { int toSkip = geometricDistribution.sample(); if (delegate instanceof SkippingIterator<?>) { SkippingIterator<? extends T> skippingDelegate = (SkippingIterator<? extends T>) delegate; skippingDelegate.skip(toSkip); if (skippingDelegate.hasNext()) { return skippingDelegate.next(); } } else { for (int i = 0; i < toSkip && delegate.hasNext(); i++) { delegate.next(); } if (delegate.hasNext()) { return delegate.next(); } } return endOfData(); }
public SamplingIterator(RandomWrapper random, Iterator<? extends T> delegate, double samplingRate) { Preconditions.checkNotNull(delegate); Preconditions.checkArgument(samplingRate > 0.0 && samplingRate <= 1.0, "Must be: 0.0 < samplingRate <= 1.0. But samplingRate = " + samplingRate); // Geometric distribution is special case of negative binomial (aka Pascal) with r=1: geometricDistribution = new PascalDistribution(random.getRandomGenerator(), 1, samplingRate); this.delegate = delegate; }
/** * Generates a random value from the {@link PascalDistribution Pascal Distribution}. * * @param r the number of successes of the Pascal distribution * @param p the probability of success of the Pascal distribution * @return random value sampled from the Pascal(r, p) distribution * @throws NotStrictlyPositiveException if the number of successes is not positive * @throws OutOfRangeException if the probability of success is not in the * range {@code [0, 1]}. */ public int nextPascal(int r, double p) throws NotStrictlyPositiveException, OutOfRangeException { return new PascalDistribution(getRandomGenerator(), r, p).sample(); }
/** * {@inheritDoc} * * For number of successes {@code r} and probability of success {@code p}, * the variance is {@code r * (1 - p) / p^2}. */ public double getNumericalVariance() { final double p = getProbabilityOfSuccess(); final double r = getNumberOfSuccesses(); return r * (1 - p) / (p * p); }
@Override protected T computeNext() { int toSkip = geometricDistribution.sample(); if (delegate instanceof SkippingIterator<?>) { SkippingIterator<? extends T> skippingDelegate = (SkippingIterator<? extends T>) delegate; skippingDelegate.skip(toSkip); if (skippingDelegate.hasNext()) { return skippingDelegate.next(); } } else { for (int i = 0; i < toSkip && delegate.hasNext(); i++) { delegate.next(); } if (delegate.hasNext()) { return delegate.next(); } } return endOfData(); }
public SamplingLongPrimitiveIterator(RandomGenerator random, LongPrimitiveIterator delegate, double samplingRate) { Preconditions.checkNotNull(random); Preconditions.checkNotNull(delegate); Preconditions.checkArgument(samplingRate > 0.0 && samplingRate <= 1.0); // Geometric distribution is special case of negative binomial (aka Pascal) with r=1: geometricDistribution = new PascalDistribution(random, 1, samplingRate); this.delegate = delegate; this.hasNext = true; doNext(); }
/** * Generates a random value from the {@link PascalDistribution Pascal Distribution}. * * @param r the number of successes of the Pascal distribution * @param p the probability of success of the Pascal distribution * @return random value sampled from the Pascal(r, p) distribution * @throws NotStrictlyPositiveException if the number of successes is not positive * @throws OutOfRangeException if the probability of success is not in the * range {@code [0, 1]}. */ public int nextPascal(int r, double p) throws NotStrictlyPositiveException, OutOfRangeException { return new PascalDistribution(getRandomGenerator(), r, p).sample(); }
/** * {@inheritDoc} * * For number of successes {@code r} and probability of success {@code p}, * the mean is {@code r * (1 - p) / p}. */ public double getNumericalMean() { final double p = getProbabilityOfSuccess(); final double r = getNumberOfSuccesses(); return (r * (1 - p)) / p; }
private void doNext() { final int toSkip = geometricDistribution.sample(); delegate.skip(toSkip); if (delegate.hasNext()) { next = delegate.next(); } else { hasNext = false; } }
public SamplingLongPrimitiveIterator(final RandomWrapper random, final LongPrimitiveIterator delegate, final double samplingRate) { Preconditions.checkNotNull(delegate); Preconditions.checkArgument(samplingRate > 0.0 && samplingRate <= 1.0, "Must be: 0.0 < samplingRate <= 1.0"); // Geometric distribution is special case of negative binomial (aka Pascal) with r=1: geometricDistribution = new PascalDistribution( random.getRandomGenerator(), 1, samplingRate); this.delegate = delegate; hasNext = true; doNext(); }
/** * {@inheritDoc} * * For number of successes {@code r} and probability of success {@code p}, * the variance is {@code r * (1 - p) / p^2}. */ public double getNumericalVariance() { final double p = getProbabilityOfSuccess(); final double r = getNumberOfSuccesses(); return r * (1 - p) / (p * p); }
private void doNext() { int toSkip = geometricDistribution.sample(); delegate.skip(toSkip); if (delegate.hasNext()) { next = delegate.next(); } else { hasNext = false; } }
public SamplingLongPrimitiveIterator(RandomWrapper random, LongPrimitiveIterator delegate, double samplingRate) { Preconditions.checkNotNull(delegate); Preconditions.checkArgument(samplingRate > 0.0 && samplingRate <= 1.0, "Must be: 0.0 < samplingRate <= 1.0"); // Geometric distribution is special case of negative binomial (aka Pascal) with r=1: geometricDistribution = new PascalDistribution(random.getRandomGenerator(), 1, samplingRate); this.delegate = delegate; this.hasNext = true; doNext(); }
/** * {@inheritDoc} * * For number of successes {@code r} and probability of success {@code p}, * the mean is {@code r * (1 - p) / p}. */ public double getNumericalMean() { final double p = getProbabilityOfSuccess(); final double r = getNumberOfSuccesses(); return (r * (1 - p)) / p; }