1 package nl.tudelft.simulation.jstats.distributions;
2
3 import org.djutils.exceptions.Throw;
4
5 import nl.tudelft.simulation.jstats.math.ProbMath;
6 import nl.tudelft.simulation.jstats.streams.StreamInterface;
7
8 /**
9 * The Negative Binomial distribution. It is also known as the Pascal distribution or Pólya distribution. It gives the
10 * probability of x failures where there are s-1 successes in a total of x+s-1 Bernoulli trials, and trial (x+s) is a success.
11 * The chance for success is p for each trial. For more information on this distribution see
12 * <a href="https://mathworld.wolfram.com/NegativeBinomialDistribution.html">
13 * https://mathworld.wolfram.com/NegativeBinomialDistribution.html </a>
14 * <p>
15 * Copyright (c) 2002-2025 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
16 * for project information <a href="https://simulation.tudelft.nl/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
17 * project is distributed under a three-clause BSD-style license, which can be found at
18 * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
19 * </p>
20 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
21 * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
22 */
23 public class DistNegBinomial extends DistDiscrete
24 {
25 /** s is the number of successes in the sequence of (x+n) trials, where trial (x+n) is a success. */
26 private int s;
27
28 /** p is the probability of success for each individual trial in the negative binomial distribution. */
29 private double p;
30
31 /** lnp is a helper variable equal to ln(1-p) to avoid repetitive calculation. */
32 private double lnp;
33
34 /**
35 * constructs a new negative binomial distribution.
36 * @param stream the random number stream
37 * @param s the number of successes in the sequence of (x+n) trials, where trial (x+n) is a success
38 * @param p the probability of success for each individual trial in the negative binomial distribution
39 * @throws IllegalArgumentException when s <= 0 or p <= 0 or p >= 1
40 */
41 public DistNegBinomial(final StreamInterface stream, final int s, final double p)
42 {
43 super(stream);
44 Throw.when(s <= 0 || p <= 0.0 || p >= 1.0, IllegalArgumentException.class,
45 "Error NegBinomial - s<=0 or p<=0.0 or p>=1.0");
46 this.s = s;
47 this.p = p;
48 this.lnp = Math.log(1.0 - this.p);
49 }
50
51 @Override
52 public long draw()
53 {
54 long x = 0;
55 for (int i = 0; i < this.s; i++)
56 {
57 double u = this.stream.nextDouble();
58 x = x + (long) (Math.floor(Math.log(u) / this.lnp));
59 }
60 return x;
61 }
62
63 @Override
64 public double probability(final long observation)
65 {
66 if (observation >= 0)
67 {
68 return ProbMath.combinations(this.s + observation - 1, observation) * Math.pow(this.p, this.s)
69 * Math.pow(1 - this.p, observation);
70 }
71 return 0.0;
72 }
73
74 /**
75 * Return the number of successes in the sequence of (x+n) trials, where trial (x+n) is a success.
76 * @return the number of successes in the sequence of (x+n) trials, where trial (x+n) is a success
77 */
78 public int getS()
79 {
80 return this.s;
81 }
82
83 /**
84 * Return the probability of success for each individual trial in the negative binomial distribution.
85 * @return the probability of success for each individual trial in the negative binomial distribution
86 */
87 public double getP()
88 {
89 return this.p;
90 }
91
92 @Override
93 public String toString()
94 {
95 return "NegBinomial(" + this.s + "," + this.p + ")";
96 }
97 }