/** * 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 testRate() { Random gen = RandomUtils.getRandom(); Poisson p = new Poisson(5, gen); double lastT = 0; double[] k = new double[1000]; double[] t = new double[1000]; for (int i = 1; i < 1000; i++) { // we sample every 5-15 seconds double dt = gen.nextDouble() * 10 + 5; t[i] = lastT + dt; // at those points, we get a Poisson count k[i] = p.nextInt(dt * 0.2); lastT = t[i]; } OnlineExponentialAverage averager = new OnlineExponentialAverage(2000); for (int i = 1; i < 1000; i++) { averager.add(t[i], k[i]); } assertEquals("Expected rate", 0.2, averager.meanRate(), 0.01); } }
/** * 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); }
/** * 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 testRate() { Random gen = RandomUtils.getRandom(); Poisson p = new Poisson(5, gen); double lastT = 0; double[] k = new double[1000]; double[] t = new double[1000]; for (int i = 1; i < 1000; i++) { // we sample every 5-15 seconds double dt = gen.nextDouble() * 10 + 5; t[i] = lastT + dt; // at those points, we get a Poisson count k[i] = p.nextInt(dt * 0.2); lastT = t[i]; } OnlineExponentialAverage averager = new OnlineExponentialAverage(2000); for (int i = 1; i < 1000; i++) { averager.add(t[i], k[i]); } assertEquals("Expected rate", 0.2, averager.meanRate(), 0.01); } }