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-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 DistBeta extends DistContinuous
22  {
23  
24      /** */
25      private static final long serialVersionUID = 1L;
26  
27      /** dist1 refers to the first Gamma distribution. */
28      private final DistGamma dist1;
29  
30      /** dist2 refers to the second Gamma distribution. */
31      private final DistGamma dist2;
32  
33      /** alpha1 is the first parameter for the Beta distribution. */
34      private final double alpha1;
35  
36      /** alpha2 is the second parameter for the Beta distribution. */
37      private final double alpha2;
38  
39      /**
40       * constructs a new beta distribution.
41       * @param stream StreamInterface; the stream.
42       * @param alpha1 double; the first shape parameter &alpha;<sub>1</sub> for the distribution
43       * @param alpha2 double; the second shape parameter &alpha;<sub>2</sub>for the distribution
44       * @throws IllegalArgumentException when alpha1 &lt;= 0.0 or alpha2 &lt;= 0.0
45       */
46      public DistBeta(final StreamInterface stream, final double alpha1, final double alpha2)
47      {
48          super(stream);
49          Throw.when(alpha1 <= 0.0 || alpha2 <= 0.0, IllegalArgumentException.class, "Error alpha1 <= 0.0 or alpha2 <= 0.0");
50          this.alpha1 = alpha1;
51          this.alpha2 = alpha2;
52          this.dist1 = new DistGamma(stream, this.alpha1, 1.0);
53          this.dist2 = new DistGamma(stream, this.alpha2, 1.0);
54      }
55  
56      /** {@inheritDoc} */
57      @Override
58      public double draw()
59      {
60          // according to Law and Kelton, Simulation Modeling and Analysis, 1991, pages 492-493
61          double y1 = this.dist1.draw();
62          double y2 = this.dist2.draw();
63          return y1 / (y1 + y2);
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public double getProbabilityDensity(final double x)
69      {
70          if (x > 0 && x < 1)
71          {
72              return (Math.pow(x, this.alpha1 - 1) * Math.pow(1 - x, this.alpha2 - 1)) / ProbMath.beta(this.alpha1, this.alpha2);
73          }
74          return 0;
75      }
76  
77      /**
78       * Return the first shape parameter &alpha;<sub>1</sub> for the distribution.
79       * @return double; the first shape parameter &alpha;<sub>1</sub> for the distribution
80       */
81      public double getAlpha1()
82      {
83          return this.alpha1;
84      }
85  
86      /**
87       * Return the second shape parameter &alpha;<sub>2</sub>for the distribution.
88       * @return double; the second shape parameter &alpha;<sub>2</sub>for the distribution
89       */
90      public double getAlpha2()
91      {
92          return this.alpha2;
93      }
94  
95      /** {@inheritDoc} */
96      @Override
97      public void setStream(final StreamInterface stream)
98      {
99          super.setStream(stream);
100         this.dist1.setStream(stream);
101         this.dist2.setStream(stream);
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public String toString()
107     {
108         return "Beta(" + this.alpha1 + "," + this.alpha2 + ")";
109     }
110 }