/** * Returns a random number from the distribution. * * @throws IllegalArgumentException * if <tt>alpha <= 0.0 || lambda <= 0.0</tt>. */ public static double staticNextDouble(double alpha, double lambda) { synchronized (shared) { return shared.nextDouble(alpha, lambda); } }
/** * Constructs a Gamma distribution. Example: alpha=1.0, lambda=1.0. * * @throws IllegalArgumentException * if <tt>alpha <= 0.0 || lambda <= 0.0</tt>. */ public Gamma(double alpha, double lambda, DoubleRandomEngine randomGenerator) { setRandomGenerator(randomGenerator); setState(alpha, lambda); }
/** * Returns a deep copy of the receiver; the copy will produce identical * sequences. After this call has returned, the copy and the receiver have * equal but separate state. * * @return a copy of the receiver. */ public Object clone() { NegativeBinomial copy = (NegativeBinomial) super.clone(); if (this.poisson != null) copy.poisson = (Poisson) this.poisson.clone(); copy.poisson.setRandomGenerator(copy.getRandomGenerator()); if (this.gamma != null) copy.gamma = (Gamma) this.gamma.clone(); copy.gamma.setRandomGenerator(copy.getRandomGenerator()); return copy; }
/** * Sets the uniform random number generated shared by all <b>static</b> * methods. * * @param randomGenerator * the new uniform random number generator to be shared. */ private static void xstaticSetRandomGenerator(DoubleRandomEngine randomGenerator) { synchronized (shared) { shared.setRandomGenerator(randomGenerator); } } }
/** * Constructs a Negative Binomial distribution. Example: n=1, p=0.5. * * @param n * the number of trials. * @param p * the probability of success. * @param randomGenerator * a uniform random number generator. */ public NegativeBinomial(int n, double p, DoubleRandomEngine randomGenerator) { setRandomGenerator(randomGenerator); setNandP(n, p); this.gamma = new Gamma(n, 1.0, randomGenerator); this.poisson = new Poisson(0.0, randomGenerator); }
/** * Returns a deep copy of the receiver; the copy will produce identical * sequences. After this call has returned, the copy and the receiver have * equal but separate state. * * @return a copy of the receiver. */ public Object clone() { NegativeBinomial copy = (NegativeBinomial) super.clone(); if (this.poisson != null) copy.poisson = (Poisson) this.poisson.clone(); copy.poisson.setRandomGenerator(copy.getRandomGenerator()); if (this.gamma != null) copy.gamma = (Gamma) this.gamma.clone(); copy.gamma.setRandomGenerator(copy.getRandomGenerator()); return copy; }
/** * Sets the uniform random number generated shared by all <b>static</b> * methods. * * @param randomGenerator * the new uniform random number generator to be shared. */ private static void xstaticSetRandomGenerator(DoubleRandomEngine randomGenerator) { synchronized (shared) { shared.setRandomGenerator(randomGenerator); } } }
/** * Constructs a Negative Binomial distribution. Example: n=1, p=0.5. * * @param n * the number of trials. * @param p * the probability of success. * @param randomGenerator * a uniform random number generator. */ public NegativeBinomial(int n, double p, DoubleRandomEngine randomGenerator) { setRandomGenerator(randomGenerator); setNandP(n, p); this.gamma = new Gamma(n, 1.0, randomGenerator); this.poisson = new Poisson(0.0, randomGenerator); }
/** * Constructs a Gamma distribution. Example: alpha=1.0, lambda=1.0. * * @throws IllegalArgumentException * if <tt>alpha <= 0.0 || lambda <= 0.0</tt>. */ public Gamma(double alpha, double lambda, DoubleRandomEngine randomGenerator) { setRandomGenerator(randomGenerator); setState(alpha, lambda); }
/** * Returns a random number from the distribution. * * @throws IllegalArgumentException * if <tt>alpha <= 0.0 || lambda <= 0.0</tt>. */ public static double staticNextDouble(double alpha, double lambda) { synchronized (shared) { return shared.nextDouble(alpha, lambda); } }
/** * Returns a random number from the distribution; bypasses the internal * state. */ public int nextInt(int n, double p) { /*********************************************************************** * * Negative Binomial Distribution - Compound method * * * ***************************************************************** * * FUNCTION: - nbp samples a random number from the Negative * Binomial * distribution with parameters r (no. of * failures given) and p * (probability of success) * valid for r > 0, 0 < p < 1. * If G from * Gamma(r) then K from Poiss(pG/(1-p)) * is NB(r,p)--distributed. * * REFERENCE: - J.H. Ahrens, U. Dieter (1974): Computer methods * for * sampling from gamma, beta, Poisson and * binomial distributions, * Computing 12, 223--246. * SUBPROGRAMS: - drand(seed) ... * (0,1)-Uniform generator with * unsigned long integer *seed * - * Gamma(seed,a) ... Gamma generator for a > 0 * unsigned long *seed, * double a * - Poisson(seed,a) ...Poisson generator for a > 0 * * unsigned long *seed, double a. * * **********************************************************************/ double x = p / (1.0 - p); double p1 = p; double y = x * this.gamma.nextDouble(n, 1.0); return this.poisson.nextInt(y); }
/** * Returns a random number from the distribution; bypasses the internal * state. */ public int nextInt(int n, double p) { /*********************************************************************** * * Negative Binomial Distribution - Compound method * * * ***************************************************************** * * FUNCTION: - nbp samples a random number from the Negative * Binomial * distribution with parameters r (no. of * failures given) and p * (probability of success) * valid for r > 0, 0 < p < 1. * If G from * Gamma(r) then K from Poiss(pG/(1-p)) * is NB(r,p)--distributed. * * REFERENCE: - J.H. Ahrens, U. Dieter (1974): Computer methods * for * sampling from gamma, beta, Poisson and * binomial distributions, * Computing 12, 223--246. * SUBPROGRAMS: - drand(seed) ... * (0,1)-Uniform generator with * unsigned long integer *seed * - * Gamma(seed,a) ... Gamma generator for a > 0 * unsigned long *seed, * double a * - Poisson(seed,a) ...Poisson generator for a > 0 * * unsigned long *seed, double a. * * **********************************************************************/ double x = p / (1.0 - p); double p1 = p; double y = x * this.gamma.nextDouble(n, 1.0); return this.poisson.nextInt(y); }
/** * Returns a random number from the distribution; bypasses the internal * state. */ public double nextDouble(double alpha, double lambda) { /*********************************************************************** * * Gamma Distribution - Acceptance Rejection combined with * * Acceptance Complement * * * ***************************************************************** * * FUNCTION: - gds samples a random number from the standard * gamma * distribution with parameter a > 0. * Acceptance Rejection gs for a < * 1 , * Acceptance Complement gd for a >= 1 . * REFERENCES: - J.H. * Ahrens, U. Dieter (1974): Computer methods * for sampling from gamma, * beta, Poisson and * binomial distributions, Computing 12, 223-246. * * - J.H. Ahrens, U. Dieter (1982): Generating gamma * variates by a * modified rejection technique, * Communications of the ACM 25, 47-54. * * SUBPROGRAMS: - drand(seed) ... (0,1)-Uniform generator with * * unsigned long integer *seed * - NORMAL(seed) ... Normal generator * N(0,1). * * **********************************************************************/ double a = alpha; double aa = -1.0, aaa = -1.0, b = 0.0, c = 0.0, d = 0.0, e, r, s = 0.0, si = 0.0, ss = 0.0, q0 = 0.0, q1 = 0.0416666664, q2 = 0.0208333723, q3 = 0.0079849875, q4 = 0.0015746717, q5 = -0.0003349403, q6 = 0.0003340332, q7 = 0.0006053049, q8 = -0.0004701849, q9 = 0.0001710320, a1 = 0.333333333, a2 = -0.249999949, a3 = 0.199999867, a4 = -0.166677482, a5 = 0.142873973, a6 = -0.124385581, a7 = 0.110368310, a8 = -0.112750886, a9 = 0.104089866, e1 = 1.000000000, e2 = 0.499999994, e3 = 0.166666848, e4 = 0.041664508, e5 = 0.008345522, e6 = 0.001353826, e7 = 0.000247453; double gds, p, q, t, sign_u, u, v, w, x; double v1, v2, v12; // Check for invalid input values if (a <= 0.0) throw new IllegalArgumentException();
/** * Returns a random number from the distribution; bypasses the internal * state. */ public double nextDouble(double alpha, double lambda) { /*********************************************************************** * * Gamma Distribution - Acceptance Rejection combined with * * Acceptance Complement * * * ***************************************************************** * * FUNCTION: - gds samples a random number from the standard * gamma * distribution with parameter a > 0. * Acceptance Rejection gs for a < * 1 , * Acceptance Complement gd for a >= 1 . * REFERENCES: - J.H. * Ahrens, U. Dieter (1974): Computer methods * for sampling from gamma, * beta, Poisson and * binomial distributions, Computing 12, 223-246. * * - J.H. Ahrens, U. Dieter (1982): Generating gamma * variates by a * modified rejection technique, * Communications of the ACM 25, 47-54. * * SUBPROGRAMS: - drand(seed) ... (0,1)-Uniform generator with * * unsigned long integer *seed * - NORMAL(seed) ... Normal generator * N(0,1). * * **********************************************************************/ double a = alpha; double aa = -1.0, aaa = -1.0, b = 0.0, c = 0.0, d = 0.0, e, r, s = 0.0, si = 0.0, ss = 0.0, q0 = 0.0, q1 = 0.0416666664, q2 = 0.0208333723, q3 = 0.0079849875, q4 = 0.0015746717, q5 = -0.0003349403, q6 = 0.0003340332, q7 = 0.0006053049, q8 = -0.0004701849, q9 = 0.0001710320, a1 = 0.333333333, a2 = -0.249999949, a3 = 0.199999867, a4 = -0.166677482, a5 = 0.142873973, a6 = -0.124385581, a7 = 0.110368310, a8 = -0.112750886, a9 = 0.104089866, e1 = 1.000000000, e2 = 0.499999994, e3 = 0.166666848, e4 = 0.041664508, e5 = 0.008345522, e6 = 0.001353826, e7 = 0.000247453; double gds, p, q, t, sign_u, u, v, w, x; double v1, v2, v12; // Check for invalid input values if (a <= 0.0) throw new IllegalArgumentException();