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