/** * Provides a negative exponential distribution given a rate parameter lambda and an underlying * random number generator. The mean of this distribution will be equal to 1/lambda. * * @param lambda The rate parameter of the distribution. * @param randomGenerator The PRNG that is used to generate values. */ public Exponential(double lambda, Random randomGenerator) { setRandomGenerator(randomGenerator); this.lambda = lambda; }
/** Returns a random number from the distribution; returns <tt>(double) nextInt()</tt>. */ @Override public double nextDouble() { return nextInt(); }
/** * @param mean The mean of the resulting distribution. * @param standardDeviation The standard deviation of the distribution. * @param randomGenerator The random number generator to use. This can be null if you don't * need to generate any numbers. */ public Normal(double mean, double standardDeviation, Random randomGenerator) { setRandomGenerator(randomGenerator); setState(mean, standardDeviation); }
/** * Constructs a Negative Binomial distribution which describes the probability of getting * a particular number of negative trials (k) before getting a fixed number of positive * trials (r) where each positive trial has probability (p) of being successful. * * @param r the required number of positive trials. * @param p the probability of success. * @param randomGenerator a uniform random number generator. */ public NegativeBinomial(int r, double p, Random randomGenerator) { setRandomGenerator(randomGenerator); this.r = r; this.p = p; this.gamma = new Gamma(r, 1, randomGenerator); this.poisson = new Poisson(0.0, randomGenerator); }
@Test public void testToString() { assertEquals("org.apache.mahout.math.jet.random.Normal(m=1.300000, sd=5.900000)", new Normal(1.3, 5.9, null).toString()); } }
@Test public void testNextDouble() throws Exception { double[] x = { -0.01, 0.1053605, 0.2231436, 0.3566749, 0.5108256, 0.6931472, 0.9162907, 1.2039728, 1.6094379, 2.3025851 }; Exponential dist = new Exponential(1, RandomUtils.getRandom()); for (double lambda : new double[]{13.0, 0.02, 1.6}) { dist.setState(lambda); checkEmpiricalDistribution(dist, 10000, lambda); DistributionChecks.checkDistribution(dist, x, 0, 1 / lambda, 10000); } }
@Test public void testToString() { assertEquals("org.apache.mahout.math.jet.random.Exponential(3.1000)", new Exponential(3.1, null).toString()); assertEquals("org.apache.mahout.math.jet.random.Exponential(3.1000)", new Exponential(3.1, null).toString()); } }
@Test public void testSetState() { Exponential dist = new Exponential(13.0, null); for (double lambda = 0.1; lambda < 1000; lambda *= 1.3) { dist.setState(lambda); checkPdf(dist, lambda); } }
/** Returns a random number from the distribution. */ @Override public double nextDouble() { return nextDouble(alpha, rate); }
/** Constructs a uniform distribution with the given minimum and maximum. */ public Uniform(double min, double max, Random randomGenerator) { setRandomGenerator(randomGenerator); setState(min, max); }
/** * Equivalent to <tt>nextDouble()</tt>. This has the effect that distributions can now be used as function objects, * returning a random number upon function evaluation. */ @Override public double apply(double dummy) { return nextDouble(); }
/** * Returns a uniformly distributed random number in the open interval <tt>(min,max)</tt> (excluding <tt>min</tt> and * <tt>max</tt>). */ @Override public double nextDouble() { return min + (max - min) * randomDouble(); }
/** Sets the internal state. */ public void setState(double min, double max) { if (max < min) { setState(max, min); return; } this.min = min; this.max = max; }
/** * Equivalent to <tt>nextInt()</tt>. This has the effect that distributions can now be used as function objects, * returning a random number upon function evaluation. */ @Override public int apply(int dummy) { return nextInt(); }
/** Sets the uniform random generator internally used. */ @Override public final void setRandomGenerator(Random randomGenerator) { super.setRandomGenerator(randomGenerator); this.cacheFilled = false; }
/** * Returns a uniformly distributed random number in the open interval <tt>(from,to)</tt> (excluding <tt>from</tt> and * <tt>to</tt>). Pre conditions: <tt>from <= to</tt>. */ public float nextFloatFromTo(float from, float to) { return (float) nextDoubleFromTo(from, to); }
/** Constructs a poisson distribution. Example: mean=1.0. */ public Poisson(double mean, Random randomGenerator) { setRandomGenerator(randomGenerator); this.mean = mean; }
@Override public double value(double v) { return dist.pdf(v); } }, xs[i], xs[i + 1]);
/** * Returns a uniformly distributed random number in the closed interval * <tt>[from,to]</tt> (including <tt>from</tt> * and <tt>to</tt>). Pre conditions: <tt>from <= to</tt>. */ public int nextIntFromTo(int from, int to) { return (int) (from + (long) ((1L + to - from) * randomDouble())); }
/** * Returns a uniformly distributed random number in the open interval <tt>(from,to)</tt> (excluding <tt>from</tt> and * <tt>to</tt>). Pre conditions: <tt>from <= to</tt>. */ public double nextDoubleFromTo(double from, double to) { return from + (to - from) * randomDouble(); }