/** * {@inheritDoc} * * <p> * <strong>Algorithm Description</strong>: Uses the Algorithm SA (Ahrens) * from p. 876 in: * [1]: Ahrens, J. H. and Dieter, U. (1972). Computer methods for * sampling from the exponential and normal distributions. * Communications of the ACM, 15, 873-882. * </p> */ public double nextExponential(double mean) throws NotStrictlyPositiveException { return new ExponentialDistribution(getRandomGenerator(), mean, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY).sample(); }
/** * {@inheritDoc} * * For mean parameter {@code k}, the mean is {@code k}. */ public double getNumericalMean() { return getMean(); }
/** {@inheritDoc} */ public double density(double x) { final double logDensity = logDensity(x); return logDensity == Double.NEGATIVE_INFINITY ? 0 : FastMath.exp(logDensity); }
/** * Creates a new Poisson distribution with specified mean, convergence * criterion and maximum number of iterations. * * @param rng Random number generator. * @param p Poisson mean. * @param epsilon Convergence criterion for cumulative probabilities. * @param maxIterations the maximum number of iterations for cumulative * probabilities. * @throws NotStrictlyPositiveException if {@code p <= 0}. * @since 3.1 */ public PoissonDistribution(RandomGenerator rng, double p, double epsilon, int maxIterations) throws NotStrictlyPositiveException { super(rng); if (p <= 0) { throw new NotStrictlyPositiveException(LocalizedFormats.MEAN, p); } mean = p; this.epsilon = epsilon; this.maxIterations = maxIterations; // Use the same RNG instance as the parent class. normal = new NormalDistribution(rng, p, FastMath.sqrt(p), NormalDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); exponential = new ExponentialDistribution(rng, 1, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); }
@Test public void logProbMatchesKnownLogDensityOfVector() { ExponentialDistribution distribution = new ExponentialDistribution(1.0); double expectedLogDensity = distribution.logDensity(0.25) + distribution.logDensity(.75); ExponentialVertex ndExponentialVertex = new ExponentialVertex(1); ProbabilisticDoubleTensorContract.matchesKnownLogDensityOfVector(ndExponentialVertex, new double[]{0.25, .75}, expectedLogDensity); }
final ExponentialDistribution ed = new ExponentialDistribution(1d / gai); ed.reseedRandomGenerator(rng.nextLong()); double sum = 0; final List<Double> arrivalTimes = newArrayList(); sum += ed.sample() * 60d; if (sum < length) { arrivalTimes.add(sum);
/** * @param param * mean * @return exponential distribution */ protected ExponentialDistribution getExponentialDistribution(double param) { if (exponential == null || exponential.getMean() != param) { exponential = new ExponentialDistribution(1.0 / param); } return exponential; }
@Override public DistributionFactory getFactory(List<String> params) { if (params.size() != 1) throw new IllegalArgumentException("Invalid parameter list for gaussian distribution: " + params); try { String[] bounds = params.get(0).split("\\.\\.+"); final long min = parseLong(bounds[0]); final long max = parseLong(bounds[1]); ExponentialDistribution findBounds = new ExponentialDistribution(1d); // max probability should be roughly equal to accuracy of (max-min) to ensure all values are visitable, // over entire range, but this results in overly skewed distribution, so take sqrt final double mean = (max - min) / findBounds.inverseCumulativeProbability(1d - Math.sqrt(1d / (max - min))); return new ExpFactory(min, max, mean); } catch (Exception e) { throw new IllegalArgumentException("Invalid parameter list for uniform distribution: " + params); } } }
/** * Creates a new exponential pseudo random number generator. * * @param seed the seed to be used. * @param mean the mean for the distribution. */ public ExponentialDistr(long seed, double mean) { this(mean); numGen.reseedRandomGenerator(seed); }
/** * Creates a new exponential pseudo random number generator. * * @param mean the mean for the distribution. */ public ExponentialDistr(double mean) { numGen = new ExponentialDistribution(mean); }
@Test public void logProbMatchesKnownLogDensityOfScalar() { ExponentialDistribution distribution = new ExponentialDistribution(1.5); ExponentialVertex tensorExponentialVertex = new ExponentialVertex(1.5); double expectedDensity = distribution.logDensity(2.0); ProbabilisticDoubleTensorContract.matchesKnownLogDensityOfScalar(tensorExponentialVertex, 2.0, expectedDensity); }
static Times generateTimes(RandomGenerator rng, double intensity) { final ExponentialDistribution ed = new ExponentialDistribution( 1000d / intensity); ed.reseedRandomGenerator(rng.nextLong()); final List<Long> times = newArrayList(); long sum = 0; while (sum < 1000) { sum += DoubleMath.roundToLong(ed.sample(), RoundingMode.HALF_DOWN); if (sum < 1000) { times.add(sum); } } return asTimes(1000, times); }
@Override public double sample() { return numGen.sample(); }
public Builder exponential(double mean, long duration, TimeUnit units) { final ExponentialDistribution distribution = new ExponentialDistribution(mean); return add("exponential(" + mean + ")", () -> (long)distribution.sample(), duration, units); }
/** * Creates a new exponential pseudo random number generator. * * @param seed the seed to be used. * @param mean the mean for the distribution. */ public ExponentialDistr(long seed, double mean) { super(new ExponentialDistribution(mean), seed); }
@Test public void logProbGraphMatchesKnownLogDensityOfVector() { DoubleVertex rate = ConstantVertex.of(1.0, 1.0); ExponentialVertex tensorExponentialVertex = new ExponentialVertex(rate); LogProbGraph logProbGraph = tensorExponentialVertex.logProbGraph(); LogProbGraphValueFeeder.feedValue(logProbGraph, rate, rate.getValue()); LogProbGraphValueFeeder.feedValue(logProbGraph, tensorExponentialVertex, DoubleTensor.create(0.25, 0.75)); ExponentialDistribution distribution = new ExponentialDistribution(1.0); double expectedDensity = distribution.logDensity(0.25) + distribution.logDensity(0.75); LogProbGraphContract.matchesKnownLogDensity(logProbGraph, expectedDensity); }
/** * {@inheritDoc} * * For mean parameter {@code k}, the variance is {@code k^2}. */ public double getNumericalVariance() { final double m = getMean(); return m * m; }
/** {@inheritDoc} */ public double density(double x) { final double logDensity = logDensity(x); return logDensity == Double.NEGATIVE_INFINITY ? 0 : Math.exp(logDensity); }