View Javadoc
1   package nl.tudelft.simulation.jstats.distributions;
2   
3   import org.djutils.exceptions.Throw;
4   
5   import nl.tudelft.simulation.jstats.streams.StreamInterface;
6   
7   /**
8    * The Bernoulli distribution, with p as the probability for success. For more information on this distribution see
9    * <a href="https://mathworld.wolfram.com/BernouilliDistribution.html">
10   * https://mathworld.wolfram.com/BernouilliDistribution.html </a>
11   * <p>
12   * Copyright (c) 2002-2024 Delft University of Technology, Jaffalaan 5, 2628 BX Delft, the Netherlands. All rights reserved. See
13   * for project information <a href="https://simulation.tudelft.nl/" target="_blank"> https://simulation.tudelft.nl</a>. The DSOL
14   * project is distributed under a three-clause BSD-style license, which can be found at
15   * <a href="https://https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">
16   * https://https://simulation.tudelft.nl/dsol/docs/latest/license.html</a>.
17   * </p>
18   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
19   * @author <a href="https://www.tudelft.nl/averbraeck">Alexander Verbraeck</a>
20   */
21  public class DistBernoulli extends DistDiscrete
22  {
23      /** */
24      private static final long serialVersionUID = 1L;
25  
26      /** p is the probability for success (X=1) of the Bernouilli distribution. */
27      private final double p;
28  
29      /**
30       * constructs a new Bernoulli distribution, with p as the probability for success (X=1), where failure is associated with
31       * X=0.
32       * @param stream StreamInterface; is the stream
33       * @param p double; the probability for success of the Bernoulli distribution
34       * @throws IllegalArgumentException when p &lt; 0 or p &gt; 1
35       */
36      public DistBernoulli(final StreamInterface stream, final double p)
37      {
38          super(stream);
39          Throw.when(p < 0.0 || p > 1.0, IllegalArgumentException.class, "Error Bernoulli - p<0 or p>1 (p=" + p + ")");
40          this.p = p;
41      }
42  
43      /**
44       * draws the next value from the Bernoulli distribution. More information on this distribution can be found at <br>
45       * <a href="https://mathworld.wolfram.com/BernoulliDistribution.html">https://mathworld.wolfram.com/
46       * BernoulliDistribution.html</a>.
47       * @return the next value {0,1}, where 1 denotes success (probability p), and 0 denotes failure (probability (1-p).
48       */
49      @Override
50      public long draw()
51      {
52          if (this.stream.nextDouble() <= this.p)
53          {
54              return 1L;
55          }
56          return 0L;
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public double probability(final long observation)
62      {
63          if (observation == 0)
64          {
65              return 1 - this.p;
66          }
67          if (observation == 1)
68          {
69              return this.p;
70          }
71          return 0;
72      }
73  
74      /**
75       * Return p, the probability for success (X=1), where failure is associated with X=0.
76       * @return double; p, the probability for success (X=1)
77       */
78      public double getP()
79      {
80          return this.p;
81      }
82  
83      /** {@inheritDoc} */
84      @Override
85      public String toString()
86      {
87          return "Bernoulli(" + this.p + ")";
88      }
89  }