/** * {@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(); }
/** * 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); }
public Builder exponential(double mean, long duration, TimeUnit units) { final ExponentialDistribution distribution = new ExponentialDistribution(mean); return add("exponential(" + mean + ")", () -> (long)distribution.sample(), duration, units); }
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 mean the mean for the distribution. */ public ExponentialDistr(double mean) { numGen = new ExponentialDistribution(mean); }
/** * 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); }
Iterator<Double> iterator() { return new TimeSeriesIterator(new ExponentialDistribution(rng, 1d / intensity, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY), length); }
/** * @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; }
/** * @param lambda The rate parameter. See Wikipedia. * @return Ibid. */ public double nextExponential(double lambda) { return new ExponentialDistribution(randomGenerator, lambda).sample(); }
@Override public Distribution get() { return new DistributionOffsetApache(new ExponentialDistribution(new JDKRandomGenerator(), mean, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY), min, max); } }
@Override public ImmutableList<Double> generate(RandomGenerator rng) { final ExponentialDistribution ed = new ExponentialDistribution(rng, 1d / lambdaMax, ExponentialDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY); double sum = 0d; final ImmutableList.Builder<Double> builder = ImmutableList.builder(); while (sum < length) { sum += ed.sample(); if (sum < length && rng.nextDouble() <= (lambd.apply(sum) / lambdaMax)) { builder.add(sum); } } return builder.build(); }
/** * {@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} * * <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(); }
public RealDistribution getRealDistribution(Context context) { RandomGenerator randomGenerator = RandomUtil.getInstance().getRandomGenerator(); Expression exp1 = getExpressions().get(0); double e1 = exp1.evaluate(context); final ExponentialDistribution distribution = new ExponentialDistribution(randomGenerator, e1); return distribution; } };
public double evaluate(Context context) { RandomGenerator randomGenerator = RandomUtil.getInstance().getRandomGenerator(); Expression exp1 = getExpressions().get(0); double e1 = exp1.evaluate(context); final ExponentialDistribution distribution = new ExponentialDistribution(randomGenerator, e1); return distribution.sample(); }
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); }
@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); }
@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); }
@Test public void logProbGraphMatchesKnownLogDensityOfScalar() { DoubleVertex rate = ConstantVertex.of(1.5); ExponentialVertex tensorExponentialVertex = new ExponentialVertex(rate); LogProbGraph logProbGraph = tensorExponentialVertex.logProbGraph(); LogProbGraphValueFeeder.feedValue(logProbGraph, rate, rate.getValue()); LogProbGraphValueFeeder.feedValue(logProbGraph, tensorExponentialVertex, DoubleTensor.scalar(2.0)); ExponentialDistribution distribution = new ExponentialDistribution(1.5); double expectedDensity = distribution.logDensity(2.0); LogProbGraphContract.matchesKnownLogDensity(logProbGraph, expectedDensity); }
@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); }