View Javadoc
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 Beta distribution. For more information on this distribution see
10   * <a href="https://mathworld.wolfram.com/BetaDistribution.html"> https://mathworld.wolfram.com/BetaDistribution.html </a>
11   * <p>
12   * Copyright (c) 2002-2025 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/dsol/manual/" target="_blank">DSOL Manual</a>. The DSOL
14   * project is distributed under a three-clause BSD-style license, which can be found at
15   * <a href="https://simulation.tudelft.nl/dsol/docs/latest/license.html" target="_blank">DSOL License</a>.
16   * </p>
17   * @author <a href="https://www.linkedin.com/in/peterhmjacobs">Peter Jacobs </a>
18   * @author <a href="https://github.com/averbraeck">Alexander Verbraeck</a>
19   */
20  public class DistBeta extends DistContinuous
21  {
22  
23      /** */
24      private static final long serialVersionUID = 1L;
25  
26      /** dist1 refers to the first Gamma distribution. */
27      private final DistGamma dist1;
28  
29      /** dist2 refers to the second Gamma distribution. */
30      private final DistGamma dist2;
31  
32      /** alpha1 is the first parameter for the Beta distribution. */
33      private final double alpha1;
34  
35      /** alpha2 is the second parameter for the Beta distribution. */
36      private final double alpha2;
37  
38      /**
39       * constructs a new beta distribution.
40       * @param stream StreamInterface; the stream.
41       * @param alpha1 double; the first shape parameter &alpha;<sub>1</sub> for the distribution
42       * @param alpha2 double; the second shape parameter &alpha;<sub>2</sub>for the distribution
43       * @throws IllegalArgumentException when alpha1 &lt;= 0.0 or alpha2 &lt;= 0.0
44       */
45      public DistBeta(final StreamInterface stream, final double alpha1, final double alpha2)
46      {
47          super(stream);
48          Throw.when(alpha1 <= 0.0 || alpha2 <= 0.0, IllegalArgumentException.class, "Error alpha1 <= 0.0 or alpha2 <= 0.0");
49          this.alpha1 = alpha1;
50          this.alpha2 = alpha2;
51          this.dist1 = new DistGamma(stream, this.alpha1, 1.0);
52          this.dist2 = new DistGamma(stream, this.alpha2, 1.0);
53      }
54  
55      @Override
56      public double draw()
57      {
58          // according to Law and Kelton, Simulation Modeling and Analysis, 1991, pages 492-493
59          double y1 = this.dist1.draw();
60          double y2 = this.dist2.draw();
61          return y1 / (y1 + y2);
62      }
63  
64      @Override
65      public double getProbabilityDensity(final double x)
66      {
67          if (x > 0 && x < 1)
68          {
69              return (Math.pow(x, this.alpha1 - 1) * Math.pow(1 - x, this.alpha2 - 1)) / ProbMath.beta(this.alpha1, this.alpha2);
70          }
71          return 0;
72      }
73  
74      /**
75       * Return the first shape parameter &alpha;<sub>1</sub> for the distribution.
76       * @return double; the first shape parameter &alpha;<sub>1</sub> for the distribution
77       */
78      public double getAlpha1()
79      {
80          return this.alpha1;
81      }
82  
83      /**
84       * Return the second shape parameter &alpha;<sub>2</sub>for the distribution.
85       * @return double; the second shape parameter &alpha;<sub>2</sub>for the distribution
86       */
87      public double getAlpha2()
88      {
89          return this.alpha2;
90      }
91  
92      @Override
93      public void setStream(final StreamInterface stream)
94      {
95          super.setStream(stream);
96          this.dist1.setStream(stream);
97          this.dist2.setStream(stream);
98      }
99  
100     @Override
101     public String toString()
102     {
103         return "Beta(" + this.alpha1 + "," + this.alpha2 + ")";
104     }
105 }