@Override public int nextInt() { return nextInt(r, p); }
/** * 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 testDistributionFunctions() throws Exception { InputSupplier<InputStreamReader> input = Resources.newReaderSupplier(Resources.getResource("negative-binomial-test-data.csv"), Charsets.UTF_8); boolean header = true; for (String line : CharStreams.readLines(input)) { if (header) { // skip header = false; } else { Iterable<String> values = onComma.split(line); int k = Integer.parseInt(Iterables.get(values, 0)); double p = Double.parseDouble(Iterables.get(values, 1)); int r = Integer.parseInt(Iterables.get(values, 2)); double density = Double.parseDouble(Iterables.get(values, 3)); double cume = Double.parseDouble(Iterables.get(values, 4)); NegativeBinomial nb = new NegativeBinomial(r, p, RandomUtils.getRandom()); assertEquals("cumulative " + k + ',' + p + ',' + r, cume, nb.cdf(k), cume * 1.0e-5); assertEquals("density " + k + ',' + p + ',' + r, density, nb.pdf(k), density * 1.0e-5); } } }
@Test public void testDistributionFunctions() throws Exception { InputSupplier<InputStreamReader> input = Resources.newReaderSupplier(Resources.getResource("negative-binomial-test-data.csv"), Charsets.UTF_8); boolean header = true; for (String line : CharStreams.readLines(input)) { if (header) { // skip header = false; } else { Iterable<String> values = onComma.split(line); int k = Integer.parseInt(Iterables.get(values, 0)); double p = Double.parseDouble(Iterables.get(values, 1)); int r = Integer.parseInt(Iterables.get(values, 2)); double density = Double.parseDouble(Iterables.get(values, 3)); double cume = Double.parseDouble(Iterables.get(values, 4)); NegativeBinomial nb = new NegativeBinomial(r, p, RandomUtils.getRandom()); assertEquals("cumulative " + k + ',' + p + ',' + r, cume, nb.cdf(k), cume * 1.0e-5); assertEquals("density " + k + ',' + p + ',' + r, density, nb.pdf(k), density * 1.0e-5); } } }
@Override public int nextInt() { return nextInt(r, p); }
/** * 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); }
@Override public int nextInt() { return nextInt(r, p); }
/** * 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); }