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 Binomial distribution. The binomial distribution is the probability of the number of successes in a sequence of n
10 * independent experiments, each with success (probability p) or failure (probability q = 1 − p). For more information on this
11 * distribution see <a href="https://mathworld.wolfram.com/BinomialDistribution.html">
12 * https://mathworld.wolfram.com/BinomialDistribution.html </a>
13 * <p>
14 * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
15 * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
16 * project is distributed under a three-clause BSD-style license, which can be found at
17 * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
18 * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
19 * </p>
20 * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
21 * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
22 */
23 public class DistBinomial extends DistDiscrete
24 {
25 /** */
26 private static final long serialVersionUID = 1L;
27
28 /** n is the number of independent experiments for the Binomial distribution. */
29 private final int n;
30
31 /** p is the probability of success for each individual trial in the binomial distribution. */
32 private final double p;
33
34 /**
35 * constructs a Binomial distribution. It calculates the probability for a number of successes in n independent Bernoulli
36 * trials with probability p of success on each trial.
37 * @param stream StreamInterface; the random number stream
38 * @param n long; the number of independent experiments for the Binomial distribution
39 * @param p double; the probability of success for each individual trial in the binomial distribution
40 * @throws IllegalArgumentException when n <= 0 or p <= 0 or p >= 1
41 */
42 public DistBinomial(final StreamInterface stream, final int n, final double p)
43 {
44 super(stream);
45 Throw.when(n <= 0 || p <= 0 || p >= 1, IllegalArgumentException.class, "Error Binomial - n<=0 or p<=0.0 or p>=1.0");
46 this.n = n;
47 this.p = p;
48 }
49
50 /** {@inheritDoc} */
51 @Override
52 public long draw()
53 {
54 long x = 0;
55 for (int i = 0; i < this.n; i++)
56 {
57 if (this.stream.nextDouble() <= this.p)
58 {
59 x++;
60 }
61 }
62 return x;
63 }
64
65 /** {@inheritDoc} */
66 @Override
67 public double probability(final long observation)
68 {
69 if (observation <= this.n && observation >= 0)
70 {
71 return ProbMath.combinations(this.n, observation) * Math.pow(this.p, observation)
72 * Math.pow(1 - this.p, this.n - observation);
73 }
74 return 0.0;
75 }
76
77 /**
78 * Return the number of independent experiments for the Binomial distribution.
79 * @return int; the number of independent experiments for the Binomial distribution
80 */
81 public int getN()
82 {
83 return this.n;
84 }
85
86 /**
87 * Return the probability of success for each individual trial in the binomial distribution.
88 * @return double; the probability of success for each individual trial in the binomial distribution
89 */
90 public double getP()
91 {
92 return this.p;
93 }
94
95 /** {@inheritDoc} */
96 @Override
97 public String toString()
98 {
99 return "Binomial(" + this.n + "," + this.p + ")";
100 }
101 }